Webrtc SDP格式解讀

因爲設備端只支持H264編碼方式接入,這裏涉及到webrtc web端要修改默認編碼器,而編碼器就離不開webrtc 信令中SDP 消息的解讀,讀懂了 才能知道自己支持編碼器的情況下對SDP的信息進行二次修改。

本文主要內容來自該說明文檔:https://tools.ietf.org/html/draft-nandakumar-rtcweb-sdp-08
主要涉及到 RFC4566 RFC5888 RFC5761 RFC5245標準文檔


1 簡介

SDP純粹是會話描述的格式,它不包含傳輸協議。
WebRTC 會話就是由 SDP 描述的。對於 WebRTC 來說,最重要的是 SDP 中的媒體信息。
媒體類型(視頻,音頻)

  • 傳輸協議(RTP/UDP/IP,H.320等)
  • 媒體格式(H.261視頻,MPEG視頻等)

如果一上來看到SDP的內容肯定是一臉懵逼的,內容非常多.

典型的基於web的實時多媒體通信會話的特徵如下

  • 它具有零個或多個僅音頻,僅視頻或音頻/視頻RTP會話,可包含零個或多個非媒體數據會話
  • 所有會話使用DTLS-SRTP保護
  • 使用ICE機制支持NAT遍歷
  • 提供基於RTCP的反饋機制
  • 會話可以是僅基於IPv4,僅基於IPv6,基於雙棧的客戶端

所以SDP中就爲爲了解決以上問題,攜帶實現該方案的組成格式協議。

2 SDP組成結構

借用標準文檔中 SDP的構成如下:

                                                 +---------------------+
                                                 |        v=           |
                                                 +---------------------+
                 +---------------------+         +---------------------+
         ====    |   Session Metadata  |  =====  |        o=           |
         |       +---------------------+         +----------------------
         |                                       +---------------------+
         |                                       |        t=           |
         |                                       +---------------------+
         |
         |
         |                                       +---------------------+
         |                                       |        c=           |
         |                                       +---------------------+
         |       +---------------------+
         ====    | Network Description |   =====
         |       +---------------------+
         |                                       +---------------------+
         |                                       |    a=candidate      |
         |                                       +---------------------+
         |
         |
         |                                       +---------------------+
         |                                       |        m=           |
         |                                       +---------------------+
         |        +---------------------+        +---------------------+
         ====     | Stream Description  |  ===== |      a=rtpmap       |
         |        +---------------------+        +----------------------
         |                                       +---------------------+
         |                                       |      a=fmtp         |
         |                                       +---------------------+
         |                                       +---------------------+
         |                                       |      a=sendrecv..   |
         |                                       +---------------------+
 +---------------+
 |    SEMANTIC   |
 | COMPONENTS OF |
 |     SDP       |
 +---------------+
         |                                       +---------------------+
         |                                       |      a=crypto       |
         |                                       +---------------------+
         |         +---------------------+       +---------------------+
         ====      |Security Descriptions|  =====|      a=ice-frag     |
         |         +---------------------+       +----------------------
 				 |                                       +---------------------+
         |                                       |      a=ice-pwd      |
         |                                       +---------------------+
         |                                       +---------------------+
         |                                       |     a=fingerprint   |
         |                                       +---------------------+
         |
         |
         |
         |                                       +---------------------+
         |                                       |      a=rtcp-fb      |
         |                                       +---------------------+
         |         +---------------------+       +---------------------+
         ====      |   Qos,Grouping      |       |                     |
                   |   Descriptions      |  =====|       a=group       |
                   +---------------------+       +----------------------
                                                 +---------------------+
                                                 |       a=rtcpmux     |
                                                 +---------------------+

從上面的組成可以看出來SDP的信息由 五個部分存在:

  • Session Metadata
  • Network Description
  • Stream Description
  • Security Descriptions
  • Qos,Grouping Descriptions

3 參數通讀

這裏先給出一份, RTCPeerConnection.createOffer 產生的SDP 並進行解讀(rfc4566)

//---------------------- Session Metadata -------------------//
// v = 0  “v =”字段給出SDP的版本,默認爲0。
v=0

// o = <username> <sess-id> <sess-version> <nettype> <addrtype> <unicast-address> 
// <username> 是用戶在始發主機上的登錄名
// <sess-id> <sess-id>是一個數字字符串,使得<username><sess-id>nettype><addrtype>和<unicast-address>的元組形成會話的全局唯一標識符。
// <sess-version>是此會話描述的版本號 
// <nettype>是一個給出網絡類型的文本字符串。 “IN”被定義爲具有“Internet”的含義
// <addrtype>是一個文本字符串,給出了後面的地址類型 定義了“IP4”和“IP6”
// <unicast-address>是創建會話的計算機的地址。   
o=mozilla...THIS_IS_SDPARTA-67.0.4 7082731800936198286 0 IN IP4 0.0.0.0

// s = <會話名稱>  “s =”字段是文本會話名稱.每個會話描述必須有一個且只有一個“s =”字段。“s =”字段不能爲空
s=-

//t =<start-time> <stop-time>“t =”行指定會話的開始和停止時間。如果<stop-time>設置爲零,則會話不受限制,但在<start-time>之後纔會生效。如果<start-time>也爲零,則會話被視爲永久會話。
t=0 0
//********************* Session Metadata ********************//


//---------------------- Security Descriptions-------------------//
//a = <attribute>:<value> 
//a = <fingerprint> SRIP 所需的DTLS指紋信息
a=fingerprint:sha-256 16:9A:59:1B:25:91:DD:C3:4D:B6:22:AB:36:0E:74:1A:F0:C1:62:0B:7F:7D:D3:08:93:13:83:B0:A9:4D:94:E7

//Negotiating Media Multiplexing Using the Session Description Protocol
//表示需要共用一個傳輸通道傳輸的媒體,通過ssrc進行區分不同的流。如果沒有這一行,音視頻數據就會分別用單獨udp端口來發送.
a=group:BUNDLE 0 1 2

//Trickle ICE:Incremental Provisioning of Candidates for the Interactive Connectivity Establishment (ICE) Protocol
//ICE建立候選時 採用增量設置的方式
a=ice-options:trickle

//WebRTC MediaStream Identification in the Session Description Protocol
//標識SDP中包含MediaStream的標識
a=msid-semantic:WMS *
//********************* Security Descriptions ********************//


//---------------------- Stream Description -------------------//
// m=<media> <port> <proto> <fmt> ...
//<media>是媒體類型 當前定義的媒體 "audio","video", "text", "application", and "message"
//<port>是傳輸媒體流的傳輸端口。在相關的“c =”字段中指定,以及在媒體字段的<proto>子字段中定義的傳輸協議。
//<proto>是傳輸協議。傳輸協議的含義取決於相關“c =”字段中的地址類型字段。
//<fmt>是媒體格式描述(編碼類型)。第四個和任何後續 如果<proto>子字段是“RTP / AVP”或“RTP / SAVP”,則<fmt> 子字段包含RTP有效載荷類型號。
m=audio 9 UDP/TLS/RTP/SAVPF 109 9 0 8 101

// c=<nettype> <addrtype> <connection-address>
// 會話描述必須包含 每個媒體描述中的至少一個“c =”字段或會話級別的單個“c =”字段
// <nettype>   “IN”被定義爲具有“Internet”的含義
// <addrtype> 爲IP4和IP6時
c=IN IP4 0.0.0.0
//a = sendrecv 這指定應以發送和接收模式啓動工具。對於具有默認爲僅接收模式的工具的交互式會議,這是必需的。
a=sendrecv

//   The URI for declaring this header extension in an extmap attribute is "urn:ietf:params:rtp-hdrext:ssrc-audio-level".
a=extmap:1 urn:ietf:params:rtp-hdrext:ssrc-audio-level
a=extmap:2/recvonly urn:ietf:params:rtp-hdrext:csrc-audio-level
a=extmap:3 urn:ietf:params:rtp-hdrext:sdes:mid

//a=fmtp:<format> <format specific parameters> 用於傳遞特殊參數
// 此屬性允許以SDP不必理解的方式傳達特定於特定格式的參數。格式必須是爲媒體指定的格式之一。格式特定參數可以是SDP要求傳達的任何參數集,
a=fmtp:109 maxplaybackrate=48000;stereo=1;useinbandfec=1
a=fmtp:101 0-15

//---------------------- Security Descriptions-------------------//
a=ice-pwd:141cd92cd90d3f5c446004c3404b13c7
a=ice-ufrag:33748035
a=mid:0
a=msid:{15d330d9-3078-4727-b51e-cdc8a2554dd1} {435405e5-84b9-49bb-85a3-9d06b16970f7}
//********************* Security Descriptions ********************//


//"a = rtcp-mux"屬性以指示需要RTP和RTCP多路複用
a=rtcp-mux
//rtpmap:<payload type> <encoding name> / <clock rate> [/ <encoding parameters>]
//<payload type> 此屬性從RTP有效內容類型編號(在 “m =”行中使用)映射到表示要使用的有效載荷格式,編碼類型 採樣率 編碼參數
a=rtpmap:109 opus/48000/2
a=rtpmap:9 G722/8000/1
a=rtpmap:0 PCMU/8000
a=rtpmap:8 PCMA/8000
a=rtpmap:101 telephone-event/8000/1

//DTLS 握手方式 "active" / "passive" / "actpass"/ "holdconn"   'actpass': 連接或啓動傳出連接。
a=setup:actpass
//同步源(SSRC)標識符識別SDP,將屬性與這些來源相關聯
//新的SDP媒體級屬性“ssrc”,用於標識特定的屬性RTP會話中的同步源並充當元數據屬性將源級屬性信息映射到這些源
a=ssrc:2040182694 cname:{eee1aa1e-411a-47d5-98ad-b26dc639bc67}


m=video 9 UDP/TLS/RTP/SAVPF 120 121 126 97
c=IN IP4 0.0.0.0
a=sendrecv

a=extmap:3 urn:ietf:params:rtp-hdrext:sdes:mid
a=extmap:4 http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time
a=extmap:5 urn:ietf:params:rtp-hdrext:toffset

a=fmtp:126 profile-level-id=42e01f;level-asymmetry-allowed=1;packetization-mode=1
a=fmtp:97 profile-level-id=42e01f;level-asymmetry-allowed=1
a=fmtp:120 max-fs=12288;max-fr=60
a=fmtp:121 max-fs=12288;max-fr=60

//---------------------- Security Descriptions-------------------//
a=ice-pwd:141cd92cd90d3f5c446004c3404b13c7
a=ice-ufrag:33748035

a=mid:1
a=msid:{15d330d9-3078-4727-b51e-cdc8a2554dd1} {47004068-3cac-4ee8-87df-2cdf02ae0921}
//---------------------- Security Descriptions-------------------//



//a = rtcp-fb: RTCP-FB-PT SP RTCP-FB-VAL CRLF
//rtcp-fb-pt是有效負載類型
///rtcp-fb-val定義反饋消息的類型 ack,nack,trr-int和rtcp-fb-id
a=rtcp-fb:120 nack
a=rtcp-fb:120 nack pli
a=rtcp-fb:120 ccm fir
a=rtcp-fb:120 goog-remb
a=rtcp-fb:121 nack
a=rtcp-fb:121 nack pli
a=rtcp-fb:121 ccm fir
a=rtcp-fb:121 goog-remb
a=rtcp-fb:126 nack
a=rtcp-fb:126 nack pli
a=rtcp-fb:126 ccm fir
a=rtcp-fb:126 goog-remb
a=rtcp-fb:97 nack
a=rtcp-fb:97 nack pli
a=rtcp-fb:97 ccm fir
a=rtcp-fb:97 goog-remb


a=rtcp-mux
//rtpmap:<payload type> <encoding name> / <clock rate> [/ <encoding parameters>]
//<payload type> 此屬性從RTP有效內容類型編號(在 “m =”行中使用)映射到表示要使用的有效載荷格式,編碼類型 採樣率 編碼參數
//而以下格式的編碼的格式要去相應編碼格式標準文檔中查看 比如H264 就是RFC3984 https://tools.ietf.org/html/rfc3984
a=rtpmap:120 VP8/90000
a=rtpmap:121 VP9/90000
a=rtpmap:126 H264/90000
a=rtpmap:97 H264/90000

a=setup:actpass
a=ssrc:1381656291 cname:{eee1aa1e-411a-47d5-98ad-b26dc639bc67}
//********************* Stream Description ********************//

m=application 9 UDP/DTLS/SCTP webrtc-datachannel
c=IN IP4 0.0.0.0
a=sendrecv
a=ice-pwd:141cd92cd90d3f5c446004c3404b13c7
a=ice-ufrag:33748035
a=mid:2
a=setup:actpass
a=sctp-port:5000
a=max-message-size:1073741823

3 媒體流信息解讀及標準文檔位置

以下就爲截取關於video部分的 SDP 信息。我們下面以120 編碼描述符進行舉例

m=video 9 UDP/TLS/RTP/SAVPF 120 121 126 97
c=IN IP4 0.0.0.0
a=sendrecv

a=extmap:3 urn:ietf:params:rtp-hdrext:sdes:mid
a=extmap:4 http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time
a=extmap:5 urn:ietf:params:rtp-hdrext:toffset

a=fmtp:126 profile-level-id=42e01f;level-asymmetry-allowed=1;packetization-mode=1
a=fmtp:97 profile-level-id=42e01f;level-asymmetry-allowed=1
a=fmtp:120 max-fs=12288;max-fr=60
a=fmtp:121 max-fs=12288;max-fr=60

a=ice-pwd:141cd92cd90d3f5c446004c3404b13c7
a=ice-ufrag:33748035
a=mid:1
a=msid:{15d330d9-3078-4727-b51e-cdc8a2554dd1} {47004068-3cac-4ee8-87df-2cdf02ae0921}

//a = rtcp-fb: RTCP-FB-PT SP RTCP-FB-VAL CRLF
//rtcp-fb-pt是有效負載類型
///rtcp-fb-val定義反饋消息的類型 ack,nack,trr-int和rtcp-fb-id
a=rtcp-fb:120 nack
a=rtcp-fb:120 nack pli
a=rtcp-fb:120 ccm fir
a=rtcp-fb:120 goog-remb
a=rtcp-fb:121 nack
a=rtcp-fb:121 nack pli
a=rtcp-fb:121 ccm fir
a=rtcp-fb:121 goog-remb
a=rtcp-fb:126 nack
a=rtcp-fb:126 nack pli
a=rtcp-fb:126 ccm fir
a=rtcp-fb:126 goog-remb
a=rtcp-fb:97 nack
a=rtcp-fb:97 nack pli
a=rtcp-fb:97 ccm fir
a=rtcp-fb:97 goog-remb

a=rtcp-mux
//rtpmap:<payload type> <encoding name> / <clock rate> [/ <encoding parameters>]
//<payload type> 此屬性從RTP有效內容類型編號(在 “m =”行中使用)映射到表示要使用的有效載荷格式,編碼類型 採樣率 編碼參數
//而以下格式的編碼的格式要去相應編碼格式標準文檔中查看 比如H264 就是RFC3984 https://tools.ietf.org/html/rfc3984
a=rtpmap:120 VP8/90000
a=rtpmap:121 VP9/90000
a=rtpmap:126 H264/90000
a=rtpmap:97 H264/90000

a=setup:actpass
a=ssrc:1381656291 cname:{eee1aa1e-411a-47d5-98ad-b26dc639bc67}

我們以97編碼格式舉例,可以看到97對應編碼格式爲H264
97 對應的協議rfc3984(H.264視頻的RTP有效載荷格式) https://tools.ietf.org/html/rfc3984

// m=<media> <port> <proto> <fmt> ...
//<media>是媒體類型 當前定義的媒體 "audio","video", "text", "application", and "message"
//<port>是傳輸媒體流的傳輸端口。在相關的“c =”字段中指定,以及在媒體字段的<proto>子字段中定義的傳輸協議。
//<proto>是傳輸協議。傳輸協議的含義取決於相關“c =”字段中的地址類型字段。
//<fmt>是媒體格式描述(編碼類型)。第四個和任何後續 如果<proto>子字段是“RTP / AVP”或“RTP / SAVP”,則<fmt> 子字段包含RTP有效載荷類型號。
m=video 9 UDP/TLS/RTP/SAVPF 120 121 126 97

a=fmtp:97 profile-level-id=42e01f;level-asymmetry-allowed=1

a=rtcp-fb:97 nack
a=rtcp-fb:97 nack pli
a=rtcp-fb:97 ccm fir
a=rtcp-fb:97 goog-remb

a=rtpmap:97 H264/90000

m=video: 描述當前支持媒體類型
a=fmtp: 提供了相應編碼格式所需要的拓展信息
a=rtcp-fb:rtcp-fb-val定義反饋消息的類型
a=rtpmap:而97則根據協議規定了 SDP的a = rtpmap行中的編碼名稱必須是H264MIME子類型。a = rtpmap行中的時鐘速率必須爲90000。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章