srs rtmp转flv

2024-02-05 23:04
文章标签 flv rtmp srs

本文主要是介绍srs rtmp转flv,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

SrsLiveStream 转换启动类

srs是通过SrsLiveStream来转换rtmp到flv,该类会判断http请求参数,根据后缀".flv"来开启

flv转换相关逻辑。

SrsLiveStream的实现如下:

// HTTP Live Streaming, to transmux RTMP to HTTP FLV or other format.
// TODO: FIXME: Rename to SrsHttpLive
class SrsLiveStream : public ISrsHttpHandler
{
private:SrsRequest* req;SrsLiveSource* source;SrsBufferCache* cache;
public:SrsLiveStream(SrsLiveSource* s, SrsRequest* r, SrsBufferCache* c);virtual ~SrsLiveStream();virtual srs_error_t update_auth(SrsLiveSource* s, SrsRequest* r);
public:virtual srs_error_t serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r);
private:virtual srs_error_t do_serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r);virtual srs_error_t http_hooks_on_play(ISrsHttpMessage* r);virtual void http_hooks_on_stop(ISrsHttpMessage* r);virtual srs_error_t streaming_send_messages(ISrsBufferEncoder* enc, SrsSharedPtrMessage** msgs, int nb_msgs);
};

调用堆栈

srs!SrsFlvTransmuxer::write_tags(SrsSharedPtrMessage**, int) (/Users/changan1/working/program/srs/trunk/src/kernel/srs_kernel_flv.cpp:568)
srs!SrsFlvStreamEncoder::write_tags(SrsSharedPtrMessage**, int) (/Users/changan1/working/program/srs/trunk/src/app/srs_app_http_stream.cpp:380)
srs!SrsLiveStream::do_serve_http(ISrsHttpResponseWriter*, ISrsHttpMessage*) (/Users/changan1/working/program/srs/trunk/src/app/srs_app_http_stream.cpp:750)
srs!SrsLiveStream::serve_http(ISrsHttpResponseWriter*, ISrsHttpMessage*) (/Users/changan1/working/program/srs/trunk/src/app/srs_app_http_stream.cpp:602)
srs!SrsHttpServeMux::serve_http(ISrsHttpResponseWriter*, ISrsHttpMessage*) (/Users/changan1/working/program/srs/trunk/src/protocol/srs_protocol_http_stack.cpp:788)
srs!SrsHttpServer::serve_http(ISrsHttpResponseWriter*, ISrsHttpMessage*) (/Users/changan1/working/program/srs/trunk/src/app/srs_app_http_conn.cpp:571)
srs!SrsHttpAuthMux::serve_http(ISrsHttpResponseWriter*, ISrsHttpMessage*) (/Users/changan1/working/program/srs/trunk/src/protocol/srs_protocol_http_stack.cpp:977)
srs!SrsHttpCorsMux::serve_http(ISrsHttpResponseWriter*, ISrsHttpMessage*) (/Users/changan1/working/program/srs/trunk/src/protocol/srs_protocol_http_stack.cpp:944)
srs!SrsHttpConn::process_request(ISrsHttpResponseWriter*, ISrsHttpMessage*, int) (/Users/changan1/working/program/srs/trunk/src/app/srs_app_http_conn.cpp:258)
srs!SrsHttpConn::process_requests(SrsRequest**) (/Users/changan1/working/program/srs/trunk/src/app/srs_app_http_conn.cpp:211)
srs!SrsHttpConn::do_cycle() (/Users/changan1/working/program/srs/trunk/src/app/srs_app_http_conn.cpp:162)
srs!SrsHttpConn::cycle() (/Users/changan1/working/program/srs/trunk/src/app/srs_app_http_conn.cpp:107)
srs!SrsFastCoroutine::cycle() (/Users/changan1/working/program/srs/trunk/src/app/srs_app_st.cpp:285)
srs!SrsFastCoroutine::pfn(void*) (/Users/changan1/working/program/srs/trunk/src/app/srs_app_st.cpp:300)
srs!_st_thread_main (/Users/changan1/working/program/srs/trunk/objs/Platform-SRS5-Darwin-22.6.0-Clang15.0.0-x86_64/st-srs/sched.c:380)
srs!st_thread_create (/Users/changan1/working/program/srs/trunk/objs/Platform-SRS5-Darwin-22.6.0-Clang15.0.0-x86_64/st-srs/sched.c:666)

主要函数

srs_error_t SrsLiveStream::do_serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r)
{//根据请求参数,开启flv功能srs_assert(entry);bool drop_if_not_match = _srs_config->get_vhost_http_remux_drop_if_not_match(req->vhost);bool has_audio = _srs_config->get_vhost_http_remux_has_audio(req->vhost);bool has_video = _srs_config->get_vhost_http_remux_has_video(req->vhost);bool guess_has_av = _srs_config->get_vhost_http_remux_guess_has_av(req->vhost);if (srs_string_ends_with(entry->pattern, ".flv")) {w->header()->set_content_type("video/x-flv");enc_desc = "FLV";enc = new SrsFlvStreamEncoder();((SrsFlvStreamEncoder*)enc)->set_drop_if_not_match(drop_if_not_match);((SrsFlvStreamEncoder*)enc)->set_has_audio(has_audio);((SrsFlvStreamEncoder*)enc)->set_has_video(has_video);((SrsFlvStreamEncoder*)enc)->set_guess_has_av(guess_has_av);} else if (srs_string_ends_with(entry->pattern, ".aac")) {w->header()->set_content_type("audio/x-aac");enc_desc = "AAC";enc = new SrsAacStreamEncoder();//创建该 flv转换对应的consumerSrsAutoFree(SrsLiveConsumer, consumer);if ((err = source->create_consumer(hc, consumer)) != srs_success) {return srs_error_wrap(err, "create consumer");}if ((err = source->consumer_dumps(consumer, true, true, !enc->has_cache())) != srs_success) {return srs_error_wrap(err, "dumps consumer");}// the memory writer.SrsBufferWriter writer(w);if ((err = enc->initialize(&writer, cache)) != srs_success) {return srs_error_wrap(err, "init encoder");}//把gop缓存发给新创建的consumer// if gop cache enabled for encoder, dump to consumer.if (enc->has_cache()) {if ((err = enc->dump_cache(consumer, source->jitter())) != srs_success) {return srs_error_wrap(err, "encoder dump cache");}}//开启一个while 循环,一直轮训读取rtmp数据,进行转换while (/*entry->enabled*/ true) {// Whether client closed the FD.if ((err = trd->pull()) != srs_success) {return srs_error_wrap(err, "recv thread");}// get messages from consumer.// each msg in msgs.msgs must be free, for the SrsMessageArray never free them.int count = 0;if ((err = consumer->dump_packets(&msgs, count)) != srs_success) {return srs_error_wrap(err, "consumer dump packets");}//写入flv 对应的tag// sendout all messages.if (ffe) {err = ffe->write_tags(msgs.msgs, count);}

SrsFlvStreamEncoder

最终会调用到:SrsFlvStreamEncoder

srs_error_t SrsFlvStreamEncoder::write_tags(SrsSharedPtrMessage** msgs, int count)
{// For https://github.com/ossrs/srs/issues/939//首先写入 flv headerif (!header_written) {bool has_video = has_video_; bool has_audio = has_audio_;if ((err = write_header(has_video, has_audio))  != srs_success) {return srs_error_wrap(err, "write header");}//再写入对应的 tag// Write tags after header is done.return enc->write_tags(msgs, count);
}

SrsFlvTransmuxer

写tag,协议层面见类SrsFlvTransmuxer

srs_error_t SrsFlvTransmuxer::write_tags(SrsSharedPtrMessage** msgs, int count)
{srs_error_t err = srs_success;// Do realloc the iovss if required.iovec* iovss = iovss_cache;do {int nn_might_iovss = 3 * count;if (nb_iovss_cache < nn_might_iovss) {srs_freepa(iovss_cache);nb_iovss_cache = nn_might_iovss;iovss = iovss_cache = new iovec[nn_might_iovss];}} while (false);// Do realloc the tag headers if required.char* cache = tag_headers;if (nb_tag_headers < count) {srs_freepa(tag_headers);nb_tag_headers = count;cache = tag_headers = new char[SRS_FLV_TAG_HEADER_SIZE * count];}// Do realloc the pts if required.char* pts = ppts;if (nb_ppts < count) {srs_freepa(ppts);nb_ppts = count;pts = ppts = new char[SRS_FLV_PREVIOUS_TAG_SIZE * count];}// Now all caches are ok, start to write all messages.iovec* iovs = iovss; int nn_real_iovss = 0;for (int i = 0; i < count; i++) {SrsSharedPtrMessage* msg = msgs[i];// Cache FLV packet header.if (msg->is_audio()) {if (drop_if_not_match_ && !has_audio_) continue; // Ignore audio packets if no audio stream.cache_audio(msg->timestamp, msg->payload, msg->size, cache);} else if (msg->is_video()) {if (drop_if_not_match_ && !has_video_) continue; // Ignore video packets if no video stream.cache_video(msg->timestamp, msg->payload, msg->size, cache);} else {cache_metadata(SrsFrameTypeScript, msg->payload, msg->size, cache);}// Cache FLV pts.cache_pts(SRS_FLV_TAG_HEADER_SIZE + msg->size, pts);// Set cache to iovec.iovs[0].iov_base = cache;iovs[0].iov_len = SRS_FLV_TAG_HEADER_SIZE;iovs[1].iov_base = msg->payload;iovs[1].iov_len = msg->size;iovs[2].iov_base = pts;iovs[2].iov_len = SRS_FLV_PREVIOUS_TAG_SIZE;// Move to next cache.cache += SRS_FLV_TAG_HEADER_SIZE;pts += SRS_FLV_PREVIOUS_TAG_SIZE;iovs += 3; nn_real_iovss += 3;}// Send out all data carried by iovec.if ((err = writer->writev(iovss, nn_real_iovss, NULL)) != srs_success) {return srs_error_wrap(err, "write flv tags failed");}return err;
}

这篇关于srs rtmp转flv的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/682438

相关文章

CRtmpServer转推流到Nginx Rtmp及SRS(SimpleRtmpServer)的经历

转自:http://blog.csdn.net/fengyily/article/details/42557841 本人一直用的是CRtmpServer服务,在CRtmpServer服务中根据自已的想法也加入了许多功能,如通过http接口来加载配置等,苦于不支持HLS,自已添加ts分片水平又有限,思来想去决定借助SimpleRtmpServer的HLS功能。说干就干,马上查找相关资源

rtmp流媒体编程相关整理2013(crtmpserver,rtmpdump,x264,faac)

转自:http://blog.163.com/zhujiatc@126/blog/static/1834638201392335213119/ 相关资料在线版(不定时更新,其实也不会很多,也许一两个月也不会改) http://www.zhujiatc.esy.es/crtmpserver/index.htm 去年在这进行rtmp相关整理,其实内容早有了,只是整理一下看着方

RTMP流媒体服务器 crtmpserver

http://www.oschina.net/p/crtmpserver crtmpserver又称rtmpd是Evostream Media Server(www.evostream.com)的社区版本采用GPLV3授权 其主要作用为一个高性能的RTMP流媒体服务器,可以实现直播与点播功能多终端支持功能,在特定情况下是FMS的良好替代品。 支持RTMP的一堆协议(RT

【IPV6从入门到起飞】4-RTMP推流,ffmpeg拉流,纯HTML网页HLS实时直播

【IPV6从入门到起飞】4-RTMP推流,ffmpeg拉流,纯HTML网页HLS实时直播 1 背景2 搭建rtmp服务器2.1 nginx方案搭建2.1.1 windows 配置2.1.2 linux 配置 2.2 Docker方案搭建2.2.1 docker 下载2.2.2 宝塔软件商店下载 3 rtmp推流3.1 EV录屏推流3.2 OBS Studio推流 4 ffmpeg拉流转格式

浅析网页不安装插件播放RTSP/FLV视频的方法

早期很多摄像头视频流使用的是RTSP、RTMP协议,播放这类协议的视频通常是在网页上安装插件。但现在越来越多的用户,对于网页安装插件比较反感,且随着移动设备的普及,用户更多的希望使用手机、平板等移动设备,直接可以查看这些协议的视频。那是否有什么方案可以直接网页打开RTSP、RTMP协议的视频,直接观看不用安装插件呢?而且对于摄像头的数据,尽可能低延迟的获取实时画面。  其实很多摄像头厂家也注意到

LiveQing视频点播流媒体RTMP推流服务功能-支持大疆等无人机RTMP推流支持OBS推流一步一步搭建RTMP视频流媒体服务示例

LiveQing支持大疆等无人机RTMP推流支持OBS推流一步一步搭建RTMP视频流媒体服务示例 1、流媒体服务搭建2、推流工具准备3、创建鉴权直播间4、获取推流地址5、配置OBS推流6、推流及播放7、获取播放地址7.1 页面查看视频源地址7.2 接口查询 8、相关问题8.1、大疆无人机推流花屏 9、RTMP推流视频直播和点播流媒体服务 1、流媒体服务搭建 Windows/Lin

深入浅出SRS—RTMP实现

RTMP 直播是 SRS 最典型的使用场景,客户端使用 RTMP 协议向 SRS 推流,使用 RTMP 协议从 SRS 拉流,SRS 作为一个 RTMP 直播服务器实现媒体的转发。同时,RTMP 是 SRS 的中转协议,其他协议之间的互通需要先转为 RTMP,因此,理解 SRS RTMP 直播实现是理解其他协议实现的重要前提。本文主要分析 SRS RTMP 直播功能的实现原理,相关概念和配置请参考

FLV 格式详解资料整理,关键帧格式解析写入库等等

FLV 是一种比较简单的视频封装格式。大致可以分为 FLV 文件头,Metadata元数据,然后一系列的音视频数据。 资料够多: FLV格式解析图 知乎用户 @Linux服务器研究 画了一张格式解析图,比较全,但默认背景是白色,太过刺眼。我用 photopea 改为黑暗模式,更适合程序员参考(请拖拽到新标签页,放大食用): 一个 C# 写的FLV转录库,我把他转换为 Java Flv

js-获取flv流文件二进制数据并分析

js-获取flv流文件二进制数据并分析 目录 文章目录 前言效果展示代码实现`index.html` 前言 对flv流文件,进行16进制转换,并用js解析 效果展示 代码实现 index.html <!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>Video</title><

828华为云征文|华为云Flexus X实例docker部署srs6并调优,协议使用webrtc与rtmp

828华为云征文|华为云Flexus X实例docker部署srs6并调优,协议使用webrtc与rtmp 华为云最近正在举办828 B2B企业节,Flexus X实例的促销力度非常大,特别适合那些对算力性能有高要求的小伙伴。如果你有自建MySQL、Redis、Nginx等服务的需求,一定不要错过这个机会。赶紧去看看吧! 什么是华为云Flexus X实例 华为云Flexus X实例云服务是新