【速成】WebRTC 交互流程

WebRTC 的數(shù)據(jù)交互使用 ICE 進(jìn)行

(refs: https://www.w3.org/TR/webrtc/#intro)
(refs: https://developer.mozilla.org/en-US/docs/Web/API/WebRTC_API/Protocols#ice)

WebRTC(ICE) 的通信由兩部分組成:

1. 信令交互

信令主要用來傳輸媒體交互需要用到的 IP,端口,媒體格式(如分辨率,編碼格式等),WebRTC 協(xié)議本身只規(guī)定了信令數(shù)據(jù)的格式(SDP),并沒有規(guī)定如何傳輸(可以用 TCP 傳輸,也可以微信或用 U 盤手動拷貝傳輸,看你喜歡)。

      Session description
         v=  (protocol version)
         o=  (originator and session identifier)
         s=  (session name)
         i=* (session information)
         u=* (URI of description)
         e=* (email address)
         p=* (phone number)
         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

      Time description
         t=  (time the session is active)
         r=* (zero or more repeat times)

      Media description, if present
         m=  (media name and transport address)
         i=* (media title)
         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)

(refs: https://datatracker.ietf.org/doc/html/rfc4566#section-5)

2. 媒體數(shù)據(jù)交互

通過信令確認(rèn)了數(shù)據(jù)接收的 IP 端口等信息后,就可以開始發(fā)送數(shù)據(jù),WebRTC 使用 RTP 格式傳輸媒體數(shù)據(jù),一般使用 UDP 協(xié)議傳輸(UDP 無法連通時(shí),也有可能選用 TCP)

WebRTC 交互流程:


WebRTC 交互流程
1.【發(fā)送 offerSdp】發(fā)起方生成 sdp ,通過信令服務(wù)發(fā)送到受邀方,主要用于協(xié)商媒體格式。

sdp 里面包含了發(fā)起方可以使用的媒體格式,sdp 里面可以包含 candidate

一個(gè) offerSdp 例子

(refs: https://www.rfc-editor.org/rfc/rfc3264#section-10 - SDP Sample)

2.【回復(fù) answerSdp】受邀方收到發(fā)起方的 sdp,結(jié)合自己可以使用的媒體格式生成一個(gè) answerSdp 回復(fù)到發(fā)起方,至此雙方已經(jīng)溝通好將要使用的媒體格式。

一個(gè) answerSdp 例子

extmap: SDP 擴(kuò)展屬性
(refs: https://datatracker.ietf.org/doc/html/rfc5285 - SDP extmap)
(refs: https://datatracker.ietf.org/doc/html/draft-ietf-avtext-sdes-hdr-ext-07 - SDP mid)
(refs: https://datatracker.ietf.org/doc/html/draft-ietf-mmusic-msid - SDP msid cname)

fingerprint: SRTP 的 DTLS 加密指紋,refs: https://datatracker.ietf.org/doc/html/rfc5763#section-1
rtcp-fb: RTP Feedback ,RTP 數(shù)據(jù)流質(zhì)量控制的相關(guān)算法

3.【交換 candidate】雙方開始檢測自己可以用于收發(fā)媒體數(shù)據(jù)的地址(IP,端口,協(xié)議等),把檢測到的可用地址通過信令服務(wù)發(fā)送到對端

(refs: https://datatracker.ietf.org/doc/html/rfc8445#section-5 - ICE Candidate Gathering and Exchange)

4.【確認(rèn) candidate】當(dāng)收到對方發(fā)過來的 candidate 后,把candidate 分別組成 candidate-pair(相同協(xié)議,可以用于通訊的兩端地址),并對 candidate-pair 進(jìn)行連通性測試,把可以連通的 candidate-pair 作為數(shù)據(jù)收發(fā)地址。

ICE-Controlling 一方會使用 USE-CANDIDATE 標(biāo)識來提名使用某一個(gè)連通的 candidate-pair,收到對端(ICE-Controlled方)回應(yīng)確認(rèn)后,即確定了該 candidate-pair 作為數(shù)據(jù)收發(fā)地址。
(refs: https://datatracker.ietf.org/doc/html/rfc8445#section-6.1.1 - ICE controlling or controlled)

提名 candidate

一個(gè) candidate-pair 列表例子(圖中第一個(gè) pair 被選中使用)

(refs: https://datatracker.ietf.org/doc/html/rfc8445#section-2.2 - Connectivity Checks)
(refs: https://datatracker.ietf.org/doc/html/rfc8445#section-7.2.5.3.4 - USE-CANDIDATE)

5.【發(fā)送/接收 RTP 數(shù)據(jù)】candidate-pair 確認(rèn)后,RTP 數(shù)據(jù)流會根據(jù) SDP 里面設(shè)定的格式編碼開始發(fā)送,RTP 的 SSRC 標(biāo)識也在 SDP 里面得到,SSRC 用于標(biāo)識數(shù)據(jù)屬于哪條數(shù)據(jù)流。

(refs: https://datatracker.ietf.org/doc/html/rfc3550#section-3 - RTP Definitions)

candidate-pair 連通檢測完后,開始發(fā)送 RTP 數(shù)據(jù)

(注:上圖中 88.44 為 controlling 方,在 No.277 Binding Request 時(shí)已經(jīng)附帶 USE-CANDIDATE 標(biāo)示,該 Binding Response 后,就已經(jīng)成功建立連接,開始收發(fā)數(shù)據(jù),No.281 開始發(fā)送數(shù)據(jù),使用 DTLS 加密發(fā)送)

相關(guān)資料:

https://datatracker.ietf.org/doc/html/rfc4566 - SDP: Session Description Protocol
https://www.rfc-editor.org/rfc/rfc3264 - An Offer/Answer Model with the Session Description Protocol (SDP)
https://www.w3.org/TR/webrtc - WebRTC 1.0: Real-Time Communication Between Browsers
https://developer.mozilla.org/en-US/docs/Web/API/WebRTC_API/Protocols - MDN Introduction to WebRTC protocols
https://datatracker.ietf.org/doc/html/rfc8445 - ICE
https://datatracker.ietf.org/doc/html/rfc3550 - RTP: A Transport Protocol for Real-Time Applications

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容