iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 14
2
Software Development

菜雞前端邁入網頁即時通訊(WebRTC)之旅系列 第 14

[知識篇]WebRTC - Session Description Protocol (SDP)

學習目標

前一章講了關於SDP offer/answer 的重要性與作用,但 SDP 究竟是什麼,那就是本章的目的。

  • 了解SDP

SDP

Session Description Protocol (SDP) is a standard for describing the multimedia content of the connection such as resolution, formats, codecs, encryption, etc. so that both peers can understand each other once the data is transferring. This is, in essence, the metadata describing the content and not the media content itself.
引用自MDN - SDP

簡而言之,SDP主要用於描述多媒體會話通信,包括會話邀請或會話宣布等,
而WebRTC主要在建立連結時會用到SDP,讓雙方連結通信時透過Signal server交換彼此多媒體信息,
裡面包括音視頻解碼器(codec)、加密、網路傳輸協定、主機位址等。

接下來介紹SDP的基本格式以及常見屬性跟用途。


基本格式

SDP的內容組很簡單,每raw的格式都是依照以下:

<character>=<value><CR><LF>

其中:

  • <character> : 為單一小寫單字,代表特定屬性
  • <value> : 一串與屬性有關的結構化文字描述,UTF-8編碼。

常見屬性

整個SDP內容主要會由Session descriptionTime descriptionMedia description 這三大類組成,
每個信息可能會包含多個Time descriptionMedia description資訊。

完整的結構如下所示(=*是選填項目,其餘都必須定義):

Session description
    v=  (protocol version number, currently only 0)
    o=  (originator and session identifier : username, id, version number, network address)
    s=  (session name : mandatory with at least one UTF-8-encoded character)
    i=* (session title or short information)
    u=* (URI of description)
    e=* (zero or more email address with optional name of contacts)
    p=* (zero or more phone number with optional name of contacts)
    c=* (connection information—not required if included in all media)
    b=* (zero or more bandwidth information lines)
    One or more Time descriptions ("t=" and "r=" lines; see below)
    z=* (time zone adjustments)
    k=* (encryption key)
    a=* (zero or more session attribute lines)
    Zero or more Media descriptions (each one starting by an "m=" line; see below)
Time description (mandatory)
    t=  (time the session is active)
    r=* (zero or more repeat times)
Media description (optional)
    m=  (media name and transport address)
    i=* (media title or information field)
    c=* (connection information — optional if included at session level)
    b=* (zero or more bandwidth information lines)
    k=* (encryption key)
    a=* (zero or more media attribute lines — overriding the Session attribute lines)

參考自Wiki/Session_Description_Protocol


真實範例

這邊擷取一段常見屬性的實際範例,也可以利用前章節的範例,console看看,
為了方便識別,延續上面的結構劃分了一下session, media區塊:

//session description
v=0
o=- 3771555899104701709 2 IN IP4 127.0.0.1
s=-
t=0 0
a=group:BUNDLE 0 1
a=msid-semantic: WMS RIEdxY3MBJV0S3hCymqN4X2Ed6KqQaWE0GXd

// media description
m=video 58915 UDP/TLS/RTP/SAVPF 96 97 98 99 100 101 102 121 127 120 125 107 108 109 124 119 123 118 114 115 116
c=IN IP4 10.102.251.26
a=rtcp:9 IN IP4 0.0.0.0
a=candidate:3911753873 1 udp 2122260223 10.102.251.26 58915 typ host generation 0 network-id 1 network-cost 10
a=sendrecv
a=rtpmap:96 VP8/90000
// audio 跟 video 的格式一樣,這邊只列出部分
m=audio 55170 UDP/TLS/RTP/SAVPF 111 103 104 9 0 8 106 105 13 110 112 113 126
a=recvonly
a=rtpmap:111 opus/48000/2

其格式介紹:

  • v=: 版本號(格式為number)

    如下:
    v=0

  • o=: 會話發起者

    格式如下:

    o=<username> <sess-id> <sess-version> <nettype> <addrtype> <unicast-address>

    各變數含義如下:
    - username:發送端user name,如果不支持用户名,則為-。
    - sess-id:session id,由使用者自行定義,規範的建議是NTP(Network Time Protocol)時間戳。
    - sess-version:session 版本,用途可以自行定義。
    - nettype:網路類型,比如IN表示Internet。
    - addrtype:port type,比如IP4、IV6
    - unicast-address:IP address

  • s=: 會話名(session name)

  • t=: 時間(通信發起時間即結束時間)

    格式如下:
    t=<start-time> <stop-time>

    如上面的例子,<stop-time>為0則表示還沒有結束,如果<start-time>也是0表示這通信是永久的,
    一般是不建議這樣做,如要設定時間,須依照Network Time Protocol (NTP)格式(the number of seconds since 1900)。

  • m=: 多媒體描述

    格式如下:
    m=<media> <port> <proto> <fmt> ...

    各變數含義如下:
    - media: 多媒體類性(ex: video, audio, text, application,message...)。
    - port: 發送端端口(每個媒體傳輸端口皆不同)
    - proto: 傳輸協定,會依照c=中定義的類型不同(IP4,IP6...)配合不同傳輸協定。
    - 以上面為例: UDP/TLS/RTP/SAVPF: 針對音視頻的SAVPF,附加在UDP協定上。
    - fmt: 多媒體格式描述,可能會有一個至多個,根據proto不同,其描述也會不同。

    以上面為例,視頻的多媒體描述如下,其fmt有多個,其第一個96也就是默認的類型,
    會對應到底下要介紹的a=附加屬性的描述上,代表默認類型是VP8/90000

      m=video 58915 UDP/TLS/RTP/SAVPF 96 97 98 ...
      a=rtpmap:96 VP8/90000
    
  • a=: 附加屬性

    由上面例子可以看出,在session descriptionmedia description的區塊底下都會出現a=,其功能就是針對所謂的session-levelmedia level的屬性擴展。

    有兩種格式:

    • a=<flag>
    • a=<attribute>:<value>

總結

SDP其應用場景不只限於即時通信應用上也存在於mail 溝通...等,
而這邊大概了解一下其在WebRTC扮演著什麼角色,與提供什麼功能跟
裡面所包含了哪些資訊。

參考

新手入門,如有錯誤,歡迎指正~~~

上一篇
[知識篇]WebRTC APIs - RTCPeerConnection object (SDP offer/answer)
下一篇
[實作篇]WebRTC APIs - RTCDataChannel - transfer text data
系列文
菜雞前端邁入網頁即時通訊(WebRTC)之旅30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言