RTMP的create stream协议

作者原创,转载请联系作者

如上文所说,建立了NetConnection,就可以建立NetStream了。在ngx_rtmp是如何实现的呢?

CMD模块回调注册

nginx在初始化启动的时候调用ngx_rtmp_cmd_module模块ngx_rtmp_cmd_postconfiguration()中进行回调注册,具体回调如下:

static ngx_rtmp_amf_handler_t ngx_rtmp_cmd_map[] = {
   { ngx_string("connect"),            ngx_rtmp_cmd_connect_init        },
   { ngx_string("createStream"),       ngx_rtmp_cmd_create_stream_init  },
   { ngx_string("closeStream"),        ngx_rtmp_cmd_close_stream_init   },
   { ngx_string("deleteStream"),       ngx_rtmp_cmd_delete_stream_init  },
   { ngx_string("publish"),            ngx_rtmp_cmd_publish_init        },
   { ngx_string("play"),               ngx_rtmp_cmd_play_init           },
   { ngx_string("play2"),              ngx_rtmp_cmd_play2_init          },
   { ngx_string("seek"),               ngx_rtmp_cmd_seek_init           },
   { ngx_string("pause"),              ngx_rtmp_cmd_pause_init          },
   { ngx_string("pauseraw"),           ngx_rtmp_cmd_pause_init          },
};

create stream流程

createStream大体流程如下:

  • 如本人的前文《RTMP的connection协议》中所讲,客户端收到客户端回复的"connect"回应消息后,给服务器发送createstream消息
  • 服务端通过ngx_rtmp_recv收到消息后进行组包、头解析后调用ngx_rtmp_receive_message
  • ngx_rtmp_receive_message根据上一步解析出来的消息头带的type定位到回调函数ngx_rtmp_cmd_create_stream_init
    而ngx_rtmp_cmd_create_stream_init主要做的工作如下:
  • 具体回调是就是从缓冲区中读取数据ngx_rtmp_receive_amf()
    -调用ngx_rtmp_cmd_create_stream()
  • ngx_rtmp_cmd_create_stream()功能相对简单,就是回复消息给客户端,amf消息如下:
    static ngx_rtmp_amf_elt_t out_elts[] = {
    { NGX_RTMP_AMF_STRING,
    ngx_null_string,
    "_result", 0 },
    { NGX_RTMP_AMF_NUMBER,
    ngx_null_string,
    &trans, 0 },
    { NGX_RTMP_AMF_NULL,
    ngx_null_string,
    NULL, 0 },
    { NGX_RTMP_AMF_NUMBER,
    ngx_null_string,
    &stream, sizeof(stream) },
    };

Stream状态

对于创建的stream,存在各个状态,用于服务端和客户端进行应对,具体如下,如何应对后文在详讲。

  • "NetStream.Play.Reset" --- 播放列表重置
  • "NetStream.Play.Start" --- 播放开始
  • "NetStream.Buffer.Empty" --- 视频正在缓冲
  • "NetStream.Buffer.Full" --- 缓冲区已填满
  • "NetStream.Play.StreamNotFound" --- 找不到此视频
  • "NetStream.Play.Stop" --- 视频播放完成
  • "NetStream.Pause.Notify" --- 流已暂停
  • "NetStream.Unpause.Notify" --- 流已恢复
  • "NetStream.Seek.Failed" --- 搜寻失败
  • "NetStream.SeekStart.Notify" --- 搜寻开始
  • "NetStream.Seek.Notify" --- 正在搜寻
  • "NetStream.Seek.Complete" --- 搜寻完毕
  • "NetStream.Publish.Start" --- 发布开始
  • "NetStream.Unpublish.Success" --- 停止发布
  • "NetStream.Record.Start" --- 开始录制
  • "NetStream.Record.Stop" --- 停止录制
  • "NetStream.Publish.BadName" --- 警告!试图发布已经被他人发布的流
  • "NetStream.Play.PublishNotify" --- 发布开始,信息已经发送到所有订阅者
  • "NetStream.Play.UnpublishNotify" --- 发布停止,信息已经发送到所有订阅者
  • "NetStream.Play.InsufficientBW" --- 警告!客户端没有足够的带宽,无法以正常速度播放数据"

你可能感兴趣的:(RTMP的create stream协议)