int SrsPublishEdge::initialize(SrsSource* source, SrsRequest* req) { int ret = ERROR_SUCCESS; if ((ret = forwarder->initialize(source, this, req)) != ERROR_SUCCESS) { return ret; } return ret; } bool SrsPublishEdge::can_publish() { return state != SrsEdgeStatePublish; } int SrsPublishEdge::on_client_publish() { int ret = ERROR_SUCCESS; // error when not init state. if (state != SrsEdgeStateInit) { ret = ERROR_RTMP_EDGE_PUBLISH_STATE; srs_error("invalid state for client to publish stream on edge. " "state=%d, ret=%d", state, ret); return ret; } // @see https://github.com/ossrs/srs/issues/180 // to avoid multiple publish the same stream on the same edge, // directly enter the publish stage. if (true) { SrsEdgeState pstate = state; state = SrsEdgeStatePublish; srs_trace("edge change from %d to state %d (push).", pstate, state); } // start to forward stream to origin. ret = forwarder->start(); // @see https://github.com/ossrs/srs/issues/180 // when failed, revert to init if (ret != ERROR_SUCCESS) { SrsEdgeState pstate = state; state = SrsEdgeStateInit; srs_trace("edge revert from %d to state %d (push). ret=%d", pstate, state, ret); } return ret; }
SrsPublishEdge::on_client_publish这个函数在encoder 向 edge 推流是会调用这个函数,用来启动farwarder,在 encoder 和 origin 之间转发 rtmp message.
这一篇分析 SrsRtmpConn::process_publish_message函数
对SrsSouce类的几个处理函数的代码
1. SrsSource::on_edge_proxy_publish
int SrsSource::on_edge_proxy_publish(SrsCommonMessage* msg) { return publish_edge->on_proxy_publish(msg); }
如果是edge,直接调用SrsPublishEdge类的on_proxy_publish方法,将msg转发到origin
int SrsPublishEdge::on_proxy_publish(SrsCommonMessage* msg) { return forwarder->proxy(msg); }
调用 SrsEdgeForwarder类的proxy函数
int SrsEdgeForwarder::proxy(SrsCommonMessage* msg) { int ret = ERROR_SUCCESS; if ((ret = send_error_code) != ERROR_SUCCESS) { srs_error("publish edge proxy thread send error, ret=%d", ret); return ret; } // the msg is auto free by source, // so we just ignore, or copy then send it. if (msg->size <= 0 || msg->header.is_set_chunk_size() || msg->header.is_window_ackledgement_size() || msg->header.is_ackledgement() ) { return ret; } SrsSharedPtrMessage copy; if ((ret = copy.create(msg)) != ERROR_SUCCESS) { srs_error("initialize the msg failed. ret=%d", ret); return ret; } srs_verbose("initialize shared ptr msg success."); copy.stream_id = stream_id; if ((ret = queue->enqueue(copy.copy())) != ERROR_SUCCESS) { srs_error("enqueue edge publish msg failed. ret=%d", ret); } return ret; }
int SrsEdgeForwarder::proxy(SrsCommonMessage* msg)函数用来将从 encoder接收的 rtmp msg 保存在 queue中
int SrsEdgeForwarder::start() { int ret = ERROR_SUCCESS; send_error_code = ERROR_SUCCESS; std::string ep_server, ep_port; if ((ret = connect_server(ep_server, ep_port)) != ERROR_SUCCESS) { return ret; } srs_assert(client); client->set_recv_timeout(SRS_CONSTS_RTMP_RECV_TIMEOUT_US); client->set_send_timeout(SRS_CONSTS_RTMP_SEND_TIMEOUT_US); SrsRequest* req = _req; if ((ret = client->handshake()) != ERROR_SUCCESS) { srs_error("handshake with server failed. ret=%d", ret); return ret; } if ((ret = connect_app(ep_server, ep_port)) != ERROR_SUCCESS) { srs_error("connect with server failed. ret=%d", ret); return ret; } if ((ret = client->create_stream(stream_id)) != ERROR_SUCCESS) { srs_error("connect with server failed, stream_id=%d. ret=%d", stream_id, ret); return ret; } if ((ret = client->publish(req->stream, stream_id)) != ERROR_SUCCESS) { srs_error("publish failed, stream=%s, stream_id=%d. ret=%d", req->stream.c_str(), stream_id, ret); return ret; } return pthread->start(); }
int SrsEdgeForwarder::start()函数
在encoder 向 edge推流是调用
作用是用 SrsRtmpClient类连接到 origin
SrsEdgeForwarder包含
SrsReusableThread2类
#define SYS_MAX_EDGE_SEND_MSGS 128 int SrsEdgeForwarder::cycle() { int ret = ERROR_SUCCESS; client->set_recv_timeout(SRS_CONSTS_RTMP_PULSE_TIMEOUT_US); SrsPithyPrint* pprint = SrsPithyPrint::create_edge(); SrsAutoFree(SrsPithyPrint, pprint); SrsMessageArray msgs(SYS_MAX_EDGE_SEND_MSGS); while (!pthread->interrupted()) { if (send_error_code != ERROR_SUCCESS) { st_usleep(SRS_EDGE_FORWARDER_ERROR_US); continue; } // read from client. if (true) { SrsCommonMessage* msg = NULL; ret = client->recv_message(&msg); srs_verbose("edge loop recv message. ret=%d", ret); if (ret != ERROR_SUCCESS && ret != ERROR_SOCKET_TIMEOUT) { srs_error("edge push get server control message failed. ret=%d", ret); send_error_code = ret; continue; } //为什么没见到msg的处理函数直接就释放了 srs_freep(msg); } // forward all messages. // each msg in msgs.msgs must be free, for the SrsMessageArray never free them. int count = 0; if ((ret = queue->dump_packets(msgs.max, msgs.msgs, count)) != ERROR_SUCCESS) { srs_error("get message to push to origin failed. ret=%d", ret); return ret; } pprint->elapse(); // pithy print if (pprint->can_print()) { kbps->sample(); srs_trace("-> "SRS_CONSTS_LOG_EDGE_PUBLISH " time=%"PRId64", msgs=%d, okbps=%d,%d,%d, ikbps=%d,%d,%d", pprint->age(), count, kbps->get_send_kbps(), kbps->get_send_kbps_30s(), kbps->get_send_kbps_5m(), kbps->get_recv_kbps(), kbps->get_recv_kbps_30s(), kbps->get_recv_kbps_5m()); } // count 是 queue中消息的个数,也就是encoder发送到edge的rtmp msg得个数// count<=0 没有包需要有 edge 转发到 origin // ignore when no messages. if (count <= 0) { srs_verbose("no packets to push."); continue; } // sendout messages, all messages are freed by send_and_free_messages(). if ((ret = client->send_and_free_messages(msgs.msgs, count, stream_id)) != ERROR_SUCCESS) { srs_error("edge publish push message to server failed. ret=%d", ret); return ret; } } return ret; }
SrsEdgeForwarder::cycle的主要功能
有 SrsRtmpClient类来完成
1. 调用SrsRtmpClient::recv_message从 origin接收消息
2.
queue->dump_packets(msgs.max, msgs.msgs, count)将从encoder接收到的rtmp msg 保存在类型为
SrsMessageArray的变量msgs中,
client->send_and_free_messages(msgs.msgs, count, stream_id)将rtmp msg 转发到originclient是 edge到 origin rtmp 连接,类型是SrsRtmpClient,主要用于在encoder和origin转发 rtmp msg edge 是的推流rtmp msg就分析完了,没做过多处理转发到origin接下来分析origin的rtmp msg处理
int SrsRtmpConn::acquire_publish(SrsSource* source, bool is_edge) { int ret = ERROR_SUCCESS; if (!source->can_publish(is_edge)) { ret = ERROR_SYSTEM_STREAM_BUSY; srs_warn("stream %s is already publishing. ret=%d", req->get_stream_url().c_str(), ret); return ret; } // when edge, ignore the publish event, directly proxy it. if (is_edge) {//在这里启动一个SrsEdgeForwarder转发encoder的rtmp msg if ((ret = source->on_edge_start_publish()) != ERROR_SUCCESS) { srs_error("notice edge start publish stream failed. ret=%d", ret); return ret; } } else {// origin 会运行到此处,从SrsSource的on_publish开始分析 if ((ret = source->on_publish()) != ERROR_SUCCESS) { srs_error("notify publish failed. ret=%d", ret); return ret; } } return ret; }
int SrsSource::on_publish() { int ret = ERROR_SUCCESS; // update the request object. srs_assert(_req); _can_publish = false; // whatever, the publish thread is the source or edge source, // save its id to srouce id.// 更新_source_id,并通知所有的SrsConsumer(call SrsConsumer::update_source_id) on_source_id_changed(_srs_context->get_id()); // reset the mix queue. mix_queue->clear(); // detect the monotonically again. is_monotonically_increase = true; last_packet_time = 0; // create forwarders// 在vhost 里配置 forward 127.0.0.1:1936 127.0.0.1:1937;// 以后再具体分析 if ((ret = create_forwarders()) != ERROR_SUCCESS) { srs_error("create forwarders failed. ret=%d", ret); return ret; } // 用 ffmpeg对流进行转码// 配置 transcode --> engine --> output rtmp://127.0.0.1:[port]/[app]?vhost=[vhost]/[stream]_[engine];// 以后具体分析 // TODO: FIXME: use initialize to set req. #ifdef SRS_AUTO_TRANSCODE if ((ret = encoder->on_publish(_req)) != ERROR_SUCCESS) { srs_error("start encoder failed. ret=%d", ret); return ret; } #endif // TODO: FIXME: use initialize to set req.// hls相关,以后具体分析 #ifdef SRS_AUTO_HLS if ((ret = hls->on_publish(_req, false)) != ERROR_SUCCESS) { srs_error("start hls failed. ret=%d", ret); return ret; } #endif //DVR 相关,以后具体分析 // TODO: FIXME: use initialize to set req. #ifdef SRS_AUTO_DVR if ((ret = dvr->on_publish(_req)) != ERROR_SUCCESS) { srs_error("start dvr failed. ret=%d", ret); return ret; } #endif //HDS相关,以后具体分析ifdef SRS_AUTO_HDS if ((ret = hds->on_publish(_req)) != ERROR_SUCCESS) { srs_error("start hds failed. ret=%d", ret); return ret; } #endif // notify the handler. srs_assert(handler); if ((ret = handler->on_publish(this, _req)) != ERROR_SUCCESS) { srs_error("handle on publish failed. ret=%d", ret); return ret; } SrsStatistic* stat = SrsStatistic::instance(); stat->on_stream_publish(_req, _source_id); return ret; }
主要分析
handler->on_publish(this, _req)
b srs_app_source.cpp:2203
推流后断下
bt command output above:
#0 SrsServer::on_publish (this=0x8d53e0, s=0x8fa0f0, r=0x8fcf00) at src/app/srs_app_server.cpp:1426 #1 0x000000000048e817 in SrsSource::on_publish (this=0x8fa0f0) at src/app/srs_app_source.cpp:2203 #2 0x0000000000481fb6 in SrsRtmpConn::acquire_publish (this=0x8f4ec0, source=0x8fa0f0, is_edge=false) at src/app/srs_app_rtmp_conn.cpp:980 #3 0x000000000048122e in SrsRtmpConn::publishing (this=0x8f4ec0, source=0x8fa0f0) at src/app/srs_app_rtmp_conn.cpp:847 #4 0x000000000047f759 in SrsRtmpConn::stream_service_cycle (this=0x8f4ec0) at src/app/srs_app_rtmp_conn.cpp:547 #5 0x000000000047e9d8 in SrsRtmpConn::service_cycle (this=0x8f4ec0) at src/app/srs_app_rtmp_conn.cpp:416 #6 0x000000000047d936 in SrsRtmpConn::do_cycle (this=0x8f4ec0) at src/app/srs_app_rtmp_conn.cpp:211 #7 0x000000000047bc6d in SrsConnection::cycle (this=0x8f4f48) at src/app/srs_app_conn.cpp:89 #8 0x00000000004ad6a3 in SrsOneCycleThread::cycle (this=0x8f4f90) at src/app/srs_app_thread.cpp:372 #9 0x00000000004ace3b in internal::SrsThread::thread_cycle (this=0x8f4fb0) at src/app/srs_app_thread.cpp:207 #10 0x00000000004ad049 in internal::SrsThread::thread_fun (arg=0x8f4fb0) at src/app/srs_app_thread.cpp:245 #11 0x0000000000535371 in _st_thread_main () at sched.c:327 #12 0x0000000000535ae1 in st_thread_create (start=0x7ffff7fdbc20, arg=0x1f7fdbb60, joinable=0, stk_size=5480568) at sched.c:591
SrsServer::on_publish代码如下
int SrsServer::on_publish(SrsSource* s, SrsRequest* r) { int ret = ERROR_SUCCESS; #ifdef SRS_AUTO_HTTP_SERVER if ((ret = http_server->http_mount(s, r)) != ERROR_SUCCESS) { return ret; } #endif return ret; }
SrsHttpServer::http_mount
参见http://www.cnblogs.com/yan-shi-yi/p/6837359.html
时间: 2024-11-05 13:33:18