1.1RTMP推流和拉流创建连接对象的⽅式都是创建了SrsRtmpConn,见上面SrsServer::fd_to_resource函数。
1.2每个SrsRtmpConn都绑定⼀个SrsCoroutine,具体的业务处理在SrsCoroutine的循环进⾏,对于RTMP⽽⾔最终的循环为SrsRtmpConn::cycle函数。
1.3而SrsRtmpConn::cycle函数最终会调用SrsRtmpConn::do_cycle函数。
- srs_error_t SrsRtmpConn::do_cycle()
- {
- srs_error_t err = srs_success;
-
- srs_trace("RTMP client ip=%s:%d, fd=%d", ip.c_str(), port, srs_netfd_fileno(stfd));
-
- rtmp->set_recv_timeout(SRS_CONSTS_RTMP_TIMEOUT);
- rtmp->set_send_timeout(SRS_CONSTS_RTMP_TIMEOUT);
-
- if ((err = rtmp->handshake()) != srs_success) { //RTMP握手逻辑
- return srs_error_wrap(err, "rtmp handshake");
- }
-
- uint32_t rip = rtmp->proxy_real_ip();
- if (rip > 0) {
- srs_trace("RTMP proxy real client ip=%d.%d.%d.%d",
- uint8_t(rip>>24), uint8_t(rip>>16), uint8_t(rip>>8), uint8_t(rip));
- }
-
- SrsRequest* req = info->req;
- if ((err = rtmp->connect_app(req)) != srs_success) { //接收connect请求
- return srs_error_wrap(err, "rtmp connect tcUrl");
- }
-
- // set client ip to request.
- req->ip = ip;
-
- srs_trace("connect app, tcUrl=%s, pageUrl=%s, swfUrl=%s, schema=%s, vhost=%s, port=%d, app=%s, args=%s",
- req->tcUrl.c_str(), req->pageUrl.c_str(), req->swfUrl.c_str(),
- req->schema.c_str(), req->vhost.c_str(), req->port,
- req->app.c_str(), (req->args? "(obj)":"null"));
-
- ...
-
- if ((err = service_cycle()) != srs_success) {
- err = srs_error_wrap(err, "service cycle");
- }
-
- srs_error_t r0 = srs_success;
- if ((r0 = on_disconnect()) != srs_success) {
- err = srs_error_wrap(err, "on disconnect %s", srs_error_desc(r0).c_str());
- srs_freep(r0);
- }
-
- // If client is redirect to other servers, we already logged the event.
- if (srs_error_code(err) == ERROR_CONTROL_REDIRECT) {
- srs_error_reset(err);
- }
-
- return err;
- }
-
1.4SrsRtmpConn::cycle函数主要是进行RTMP握手,接收connect请求,判断为有效连接后调用SrsRtmpConn::service_cycle函数。
1.4.1todo:后续会写一篇具体RTMP握手过程,命令控制消息和协议控制消息交互逻辑文章。
- srs_error_t SrsRtmpConn::service_cycle()
- {
- srs_error_t err = srs_success;
-
- SrsRequest* req = info->req;
-
- int out_ack_size = _srs_config->get_out_ack_size(req->vhost);
- if (out_ack_size && (err = rtmp->set_window_ack_size(out_ack_size)) != srs_success) {
- return srs_error_wrap(err, "rtmp: set out window ack size");
- }
-
- int in_ack_size = _srs_config->get_in_ack_size(req->vhost);
- if (in_ack_size && (err = rtmp->set_in_window_ack_size(in_ack_size)) != srs_success) {
- return srs_error_wrap(err, "rtmp: set in window ack size");
- }
-
- if ((err = rtmp->set_peer_bandwidth((int)(2.5 * 1000 * 1000), 2)) != srs_success) {
- return srs_error_wrap(err, "rtmp: set peer bandwidth");
- }
-
- // get the ip which client connected.
- std::string local_ip = srs_get_local_ip(srs_netfd_fileno(stfd));
-
- // do bandwidth test if connect to the vhost which is for bandwidth check.
- if (_srs_config->get_bw_check_enabled(req->vhost)) {
- if ((err = bandwidth->bandwidth_check(rtmp, skt, req, local_ip)) != srs_success) {
- return srs_error_wrap(err, "rtmp: bandwidth check");
- }
- return err;
- }
-
- // set chunk size to larger.
- // set the chunk size before any larger response greater than 128,
- // to make OBS happy, @see https://github.com/ossrs/srs/issues/454
- int chunk_size = _srs_config->get_chunk_size(req->vhost);
- if ((err = rtmp->set_chunk_size(chunk_size)) != srs_success) {
- return srs_error_wrap(err, "rtmp: set chunk size %d", chunk_size);
- }
-
- // response the client connect ok. 响应客户端connect请求
- if ((err = rtmp->response_connect_app(req, local_ip.c_str())) != srs_success) {
- return srs_error_wrap(err, "rtmp: response connect app");
- }
-
- if ((err = rtmp->on_bw_done()) != srs_success) {
- return srs_error_wrap(err, "rtmp: on bw down");
- }
-
- while (true) {
- if ((err = trd->pull()) != srs_success) {
- return srs_error_wrap(err, "rtmp: thread quit");
- }
-
- err = stream_service_cycle();
-
- // stream service must terminated with error, never success.
- // when terminated with success, it's user required to stop.
- // TODO: FIXME: Support RTMP client timeout, https://github.com/ossrs/srs/issues/1134
- if (err == srs_success) {
- continue;
- }
-
- ...
-
- // for other system control message, fata