RTMP的Publish協議

作者原創,轉載請聯系作者
經過建立Netconnection和CreateStream之后,客戶端就可以進行相應的流操作,本文講解推流操作。

涉及模塊

針對推流操作,NGX-RTMP處理比較復雜,涉及的模塊也比較多,先羅列如下。至于回調如何注冊、如何調用請參考前文。

  • 模塊名:ngx_rtmp_access_module
    回 調:ngx_rtmp_access_publish
  • 模塊名:ngx_rtmp_auto_push_module
    回 調:ngx_rtmp_auto_push_publish
  • 模塊名:ngx_rtmp_cfms_module
    回 調:ngx_rtmp_cfms_publish
  • 模塊名:ngx_rtmp_cmd_module
    回 調:ngx_rtmp_cmd_publish
  • 模塊名:ngx_rtmp_dash_publish
    回 調:ngx_rtmp_dash_publish
  • 模塊名:ngx_rtmp_exec_module
    回 調:ngx_rtmp_exec_publish
  • 模塊名:ngx_rtmp_hls_module
    回 調:ngx_rtmp_hls_publish
  • 模塊名:ngx_rtmp_live_module
    回 調:ngx_rtmp_live_publish
  • 模塊名:ngx_rtmp_log_module
    回 調:ngx_rtmp_log_publish
  • 模塊名:ngx_rtmp_notify_module
    回 調:ngx_rtmp_notify_publish
  • 模塊名:ngx_rtmp_record_module
    回 調:ngx_rtmp_record_publish
  • 模塊名:ngx_rtmp_relay_module
    回 調:ngx_rtmp_relay_publish

具體處理

在這小節主要弄懂下面幾個問題:

  • 上述那些模塊如何順序調用?
    在注冊回調時,將原回調ngx_rtmp_publish保存起來為next_publish。在ngx_rtmp_live_publish回調處理完畢后調用next_publish,從而下一個模塊繼續調用。見代碼:

    next_publish = ngx_rtmp_publish;
    ngx_rtmp_publish = ngx_rtmp_live_publish;

  • 上述那些模塊調用順序如何?
    這個問題較簡單,查看我的前文《RTMP添加到NGINX》中描述,主要根據編譯時生成的 ngx_module_t *ngx_modules[] 變量在啟動的時候一次執行

  • 做什么什么工作?
    因為模塊比較多,在此挑選ngx_rtmp_live_module進行敘述,其他模塊處理細節請各位看官閱讀代碼,如有問題可以消息我,如有必要可以再撰文描述。

    • 合法性校驗主要有:
      NetStream.Play.StreamNotFound
      NetStream.Publish.BadName

    • 設置ctx

      ctx->stream = *stream;
      ctx->publishing = publisher;
      ctx->next = (*stream)->ctx;
      ctx->cs[0].csid = NGX_RTMP_CSID_VIDEO;
      ctx->cs[1].csid = NGX_RTMP_CSID_AUDIO;
      
    • 設置狀態ngx_rtmp_live_set_status,包括NetStream.Play.Start和NetStream.Play.PublishNotify,并發送給客戶端

  • 給pulbisher回復消息NetStream.Publish.Start

  • 客戶端收到 NetStream.Publish.Start后,給服務端發送Audio和/或Video的報文,具體如何發送詳情后續再撰文敘述

  • 服務端接受AUDIO/VIDEO
    在nginx啟動的時候,NGX-RTMP通過將處理AUDIO/VIDEO的回調注冊,即NGX_RTMP_MSG_VIDEO和NGX_RTMP_MSG_AUDIO的回調,具體見下:

    • 模塊名:ngx_rtmp_codec_module
      回 調:ngx_rtmp_codec_av
    • 模塊名:ngx_rtmp_dash_module
      回 調:ngx_rtmp_dash_video&ngx_rtmp_dash_audio
    • 模塊名:ngx_rtmp_hls_module
      回 調:ngx_rtmp_hls_video&ngx_rtmp_hls_audio
    • 模塊名:ngx_rtmp_live_module
      回 調:ngx_rtmp_live_av
    • 模塊名:ngx_rtmp_record_module
      回 調:ngx_rtmp_record_av

因為模塊比較多,在此挑選ngx_rtmp_live_module進行敘述,即ngx_rtmp_live_av,而ngx_rtmp_live_av其注冊和調用時機請查看前文,不在敘述。其主要做了如下工作:

  • 構造回應消息,主要在函數ngx_rtmp_prepare_message(...)中進行處理:
     探測包大小:
          mlen = 0;
          nbufs = 0;
          for(l = out; l; l = l->next) {
              mlen += (l->buf->last - l->buf->pos);
              ++nbufs;
          }
          fmt = 0;
          if (lh && lh->csid && h->msid == lh->msid) {
            ++fmt;
            if (h->type == lh->type && mlen && mlen == lh->mlen) {
              ++fmt;
              if (h->timestamp == lh->timestamp) {
                ++fmt;
              }
            }
            timestamp = h->timestamp - lh->timestamp;
          } else {
            timestamp = h->timestamp;
          }
  構造base header
      *p = (fmt << 6);
      if (h->csid >= 2 && h->csid <= 63) {
          *p++ |= (((uint8_t)h->csid) & 0x3f);
      } else if (h->csid >= 64 && h->csid < 320) {
          ++p;
          *p++ = (uint8_t)(h->csid - 64);
      } else {
          *p++ |= 1;
          *p++ = (uint8_t)(h->csid - 64);
          *p++ = (uint8_t)((h->csid - 64) >> 8);
      }
構造消息頭
    if (fmt <= 2) {
        pp = (u_char*)×tamp;
        *p++ = pp[2];
        *p++ = pp[1];
        *p++ = pp[0];
        if (fmt <= 1) {
            pp = (u_char*)&mlen;
            *p++ = pp[2];
            *p++ = pp[1];
            *p++ = pp[0];
            *p++ = h->type;
            if (fmt == 0) {
                pp = (u_char*)&h->msid;
                *p++ = pp[0];
                *p++ = pp[1];
                *p++ = pp[2];
                *p++ = pp[3];
            }
        }
    }
構造extended header 
    if (ext_timestamp) {
        pp = (u_char*)&ext_timestamp;
        *p++ = pp[3];
        *p++ = pp[2];
        *p++ = pp[1];
        *p++ = pp[0];
    }
  • 給所有subscribe廣播
  • send metadata(略)
  • sync stream(略)
  • send absolute codec header(略)
  • send absolute packet(略)
  • send relative packet(略)
  • ngx_rtmp_update_bandwidth
ngx_rtmp_update_bandwidth(&ctx->stream->bw_in, h->mlen);
ngx_rtmp_update_bandwidth(&ctx->stream->bw_out, h->mlen * peers);
ngx_rtmp_update_bandwidth(h->type == NGX_RTMP_MSG_AUDIO ?
                              &ctx->stream->bw_in_audio :
                              &ctx->stream->bw_in_video,
                              h->mlen);

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容