RTSP/RTMP直播系統中使用SEI傳輸用戶自定義數據方案討論

  • 2019 年 10 月 5 日
  • 筆記

在直播系統中,除了直播音影片之外,有時候還想從主播端發布文本資訊等,這些資訊可以不通過影片傳輸通道發送給用戶播放端,但如果傳輸的數據想和影片保持精準同步,那最好的辦法就是這些資訊和影片數據打包在一起傳輸, 通過h264 sei方式就可以把數據放入h264 Access Unit中傳輸。

H264 SEI 基本知識介紹:

SEI 全稱: Supplemental Enhancement Information

SEI Nal Unit Type: 6

SEI 語法定義:

Supplemental enhancement information RBSP syntax:

sei_rbsp( ) {

C

Descriptor

do

sei_message( )

5

while( more_rbsp_data( ) )

rbsp_trailing_bits( )

5

}

Supplemental enhancement information message syntax:

sei_message( ) {

C

Descriptor

payloadType = 0

while( next_bits( 8 ) = = 0xFF ) {

ff_byte /* equal to 0xFF */

5

f(8)

payloadType += 255

}

last_payload_type_byte

5

u(8)

payloadType += last_payload_type_byte

payloadSize = 0

while( next_bits( 8 ) = = 0xFF ) {

ff_byte /* equal to 0xFF */

5

f(8)

payloadSize += 255

}

last_payload_size_byte

5

u(8)

payloadSize += last_payload_size_byte

sei_payload( payloadType, payloadSize )

5

}

SEI 語義:

Supplemental enhancement information RBSP semantics

Supplemental Enhancement Information (SEI) contains information that is not necessary to decode the samples of coded pictures from VCL NAL units.

Supplemental enhancement information message semantics

An SEI NAL unit contains one or more SEI messages. Each SEI message consists of the variables specifying the type payloadType and size payloadSize of the SEI payload. SEI payloads are specified in Annex D. The derived SEI payload size payloadSize is specified in bytes and shall be equal to the number of bytes in the SEI payload.ff_byte is a byte equal to 0xFF identifying a need for a longer representation of the syntax structure that it is used within. last_payload_type_byte is the last byte of the payload type of an SEI message.

last_payload_size_byte is the last byte of the size of an SEI message

從上面的描述可以看出一個Sei Nal Unit中可以包含多個SEI消息,每個SEI消息都有一個payloadType,目前h264規定payloadType為5時,sei_playload可以使用戶自定義數據, 那麼我們就可以利用它來傳輸數據。

到此為止SEI基本知識介紹完畢,如果要自己實現程式碼的話,還需要了解更多細節,建議仔細閱讀h264文檔,這裡不再深入討論,也歡迎一起交流討論。接下來進入實踐環節。

先下載軟體: https://github.com/daniulive/SmarterStreaming 為方便測試, 下載windows版本就可以.

rtmp 傳輸文本資訊:

1. 啟動推送端軟體: SmartPublisherDemo.exe

2. 做如下配置:

3. 可以點擊自動發送文本按鈕

4. 打開播放端SmartPlayer.exe查看數據傳輸播放效果:

rtsp 傳輸文本資訊:

1. 啟動推送端軟體: SmartPublisherDemo.exe

2. 做如下配置:

3. 可以點擊自動發送文本按鈕

4. 打開播放端SmartPlayer.exe查看數據傳輸播放效果:

總結

從上面的實驗可以看出SEI的優勢來, 第一個優勢是並不依賴於相關協議,rtsp和rtmp都可以,其他協議只要播放端支援SEI解析的都可以使用。 第二個是兼容性很好,如果播放端不支援自定義SEI數據解析,把SEI數據丟給H264解碼器,解碼器只是忽略掉,並不影響正常播放. 上述操作也可以用VLC來播放,播放正常,只是不顯示SEI消息而已。第三個是完全和影片保持同步,這個是其他傳輸通道無法做到的.