3.SRS流媒体服务器源码的重要函数和类说明
RTMP不管是推流还是拉流都是对应一个连接实现,那HTTP-FLV也是一个客户端对应一个连接,如果是HLS,那client也会对应一个连接 。
(1)源码中重要函数和文件说明
在SRS流媒体服务器源码中,关于处理数据的重要函数说明:
SrsLiveStream::do_serve_http:处理客户端的数据发送 。
SrsHttpConn:表示每个http client或RTMP client都有这个连接 。
SrsConsumer:每个SrsHttpConn都对应一个消费者SrsConsumer,对应RTMP或HTTP 。关于SrsConsumer前面文章已经讲过,这里相当于中间数据的缓存 。
(2)源码中重要类说说明
SrsBufferCache:HTTP直播流编码器的缓存 。
SrsFlvStreamEncoder:将RTMP转成HTTP FLV流 。
SrsTsStreamEncoder:将RTMP转成HTTP TS流 。
SrsAacStreamEncoder:将RTMP含有的AAC成分转成HTTP AAC流 。
SrsMp3StreamEncoder:将RTMP含有的MP3成分转成HTTP MP3流 。
SrsBufferWriter:将流直接写?到HTTP响应的数据中 。
SrsLiveStream:HTTP直播流,将RTMP转成HTTP-FLV或者其他格式,其实际是handler SrsLiveEntry 直播??,?来处理HTTP 直播流 。
SrsHttpStreamServer:HTTP直播流服务,服务FLV/TS/MP3/AAC流的合成 。
SrsHttpResponseWriter: 负责将数据发送给客户端,本质是调?SrsStSocket进?发送
SrsHttpServeMux:HTTP请求多路复?器,实际就是路由,??记录了path以及对应handler 。
4.SRS流媒体服务器源码解析
根据源码可以得到,http和RTMP都是继承SrsConnection 。源码如下:
// The http connection which request the static or stream content.class SrsHttpConn : public SrsConnection{protected:SrsHttpParser* parser;ISrsHttpServeMux* http_mux;SrsHttpCorsMux* cors;public:SrsHttpConn(IConnectionManager* cm, srs_netfd_t fd, ISrsHttpServeMux* m, std::string cip);virtual ~SrsHttpConn();// Interface ISrsKbpsDeltapublic:virtual void remark(int64_t* in, int64_t* out);protected:virtual srs_error_t do_cycle();protected:// When got http message,// for the static service or api, discard any body.// for the stream caster, for instance, http flv streaming, may discard the flv header or not.virtual srs_error_t on_got_http_message(ISrsHttpMessage* msg) = 0;private:virtual srs_error_t process_request(ISrsHttpResponseWriter* w, ISrsHttpMessage* r);// When the connection disconnect, call this method.// e.g. log msg of connection and report to other system.// @param request: request which is converted by the last http message.virtual srs_error_t on_disconnect(SrsRequest* req);// Interface ISrsReloadHandlerpublic:virtual srs_error_t on_reload_http_stream_crossdomain();};SrsRtmpConn继承SrsConnection,源码如下:
// The client provides the main logic control for RTMP clients.class SrsRtmpConn : virtual public SrsConnection, virtual public ISrsReloadHandler{// For the thread to directly access any field of connection.friend class SrsPublishRecvThread;private:SrsServer* server;SrsRtmpServer* rtmp;SrsRefer* refer;SrsBandwidth* bandwidth;SrsSecurity* security;// The wakable handler, maybe NULL.// TODO: FIXME: Should refine the state for receiving thread.ISrsWakable* wakable;// The elapsed duration in srs_utime_t// For live play duration, for instance, rtmpdump to record.// @see https://github.com/ossrs/srs/issues/47srs_utime_t duration;// The MR(merged-write) sleep time in srs_utime_t.srs_utime_t mw_sleep;// The MR(merged-write) only enabled for play.int mw_enabled;// For realtime// @see https://github.com/ossrs/srs/issues/257bool realtime;// The minimal interval in srs_utime_t for delivery stream.srs_utime_t send_min_interval;// The publish 1st packet timeout in srs_utime_tsrs_utime_t publish_1stpkt_timeout;// The publish normal packet timeout in srs_utime_tsrs_utime_t publish_normal_timeout;// Whether enable the tcp_nodelay.bool tcp_nodelay;// About the rtmp client.SrsClientInfo* info;public:SrsRtmpConn(SrsServer* svr, srs_netfd_t c, std::string cip);virtual ~SrsRtmpConn();public:virtual void dispose();protected:virtual srs_error_t do_cycle();// Interface ISrsReloadHandlerpublic:virtual srs_error_t on_reload_vhost_removed(std::string vhost);virtual srs_error_t on_reload_vhost_play(std::string vhost);virtual srs_error_t on_reload_vhost_tcp_nodelay(std::string vhost);virtual srs_error_t on_reload_vhost_realtime(std::string vhost);virtual srs_error_t on_reload_vhost_publish(std::string vhost);// Interface ISrsKbpsDeltapublic:virtual void remark(int64_t* in, int64_t* out);private:// When valid and connected to vhost/app, service the client.virtual srs_error_t service_cycle();// The stream(play/publish) service cycle, identify client first.virtual srs_error_t stream_service_cycle();virtual srs_error_t check_vhost(bool try_default_vhost);virtual srs_error_t playing(SrsSource* source);virtual srs_error_t do_playing(SrsSource* source, SrsConsumer* consumer, SrsQueueRecvThread* trd);virtual srs_error_t publishing(SrsSource* source);virtual srs_error_t do_publishing(SrsSource* source, SrsPublishRecvThread* trd);virtual srs_error_t acquire_publish(SrsSource* source);virtual void release_publish(SrsSource* source);virtual srs_error_t handle_publish_message(SrsSource* source, SrsCommonMessage* msg);virtual srs_error_t process_publish_message(SrsSource* source, SrsCommonMessage* msg);virtual srs_error_t process_play_control_msg(SrsConsumer* consumer, SrsCommonMessage* msg);virtual void change_mw_sleep(srs_utime_t sleep_v);virtual void set_sock_options();private:virtual srs_error_t check_edge_token_traverse_auth();virtual srs_error_t do_token_traverse_auth(SrsRtmpClient* client);private:// When the connection disconnect, call this method.// e.g. log msg of connection and report to other system.virtual srs_error_t on_disconnect();private:virtual srs_error_t http_hooks_on_connect();virtual void http_hooks_on_close();virtual srs_error_t http_hooks_on_publish();virtual void http_hooks_on_unpublish();virtual srs_error_t http_hooks_on_play();virtual void http_hooks_on_stop();};
推荐阅读
- 建立数据中转服务器的详细方法
- 阿里云、腾讯云、等云服务器有什么区别
- 网站服务器遇到CC攻击时可能发生的两大误区
- frp远程办公,访问内网服务器
- 互联网云服务器JVM监控——JDK自带工具
- 4 个打造多媒体和共享服务器的开源工具
- Linux服务器集群系统——LVS集群中的IP负载均衡技术
- CentOS系统下PXE服务器的搭建与部署
- 5分钟搭建公网https网页文件服务器,免费权威TLS证书
- 一个比 Nginx 功能更强大的 Web 服务器
