关于使用socket,io的webrtc中的套接字连接

时间:2018-05-23 16:22:28

标签: javascript node.js socket.io

我正在研究点对点视频聊天,我按照谷歌Codelab进行研究。我只是从html 5 rock网站上阅读关于webrtc的理论部分,所以我对编码部分没有太多了解套接字连接。

代码实验室的链接是 https://codelabs.developers.google.com/codelabs/webrtc-web/#2 请访问第05步。

我并不理解来自" main.js"的部分内容。文件。

socket.on('message', function(message) {
  console.log('Client received message:', message);
  if (message === 'got user media') {
    maybeStart();
  } else if (message.type === 'offer') {
    if (!isInitiator && !isStarted) {
      maybeStart();
    }
    pc.setRemoteDescription(new RTCSessionDescription(message));
    doAnswer();
  } else if (message.type === 'answer' && isStarted) {
    pc.setRemoteDescription(new RTCSessionDescription(message));
  } else if (message.type === 'candidate' && isStarted) {
    var candidate = new RTCIceCandidate({
      sdpMLineIndex: message.label,
      candidate: message.candidate
    });
    pc.addIceCandidate(candidate);
  } else if (message === 'bye' && isStarted) {
    handleRemoteHangup();
  }
});

所以我的问题是

1)什么是message.type ===' offer'什么是提供什么样的字符串?同样的答案'和候选人'也是。

你能告诉我这个代码究竟是如何工作的吗?

1 个答案:

答案 0 :(得分:0)

WebRTC在两个端点(例如:浏览器)之间创建对等(P2P)连接。要做到这一点,它需要一个信令过程来连接对等体,之后它们之间直接相互通信(媒体或数据)。

需要信令,因为由于网络地址转换器(NAT),防火墙和其他网络障碍,因特网上的客户端应用程序之间的直接通信非常棘手。

回答您的问题(信号):

信令用于交换会话控制消息(提供应答),称为SDP(会话描述协议格式),网络配置为 ICE候选者和使用相同会话控制消息的媒体功能。

More details about the signaling process

Anatomy of a WebRTC SDP

示例SDP:

v=0
o=- 7614219274584779017 2 IN IP4 127.0.0.1
s=-
t=0 0
a=group:BUNDLE audio video
a=msid-semantic: WMS
m=audio 1 RTP/SAVPF 111 103 104 0 8 107 106 105 13 126
c=IN IP4 0.0.0.0
a=rtcp:1 IN IP4 0.0.0.0
a=ice-ufrag:W2TGCZw2NZHuwlnf
a=ice-pwd:xdQEccP40E+P0L5qTyzDgfmW
a=extmap:1 urn:ietf:params:rtp-hdrext:ssrc-audio-level
a=mid:audio
a=rtcp-mux
a=crypto:1 AES_CM_128_HMAC_SHA1_80 inline:9c1AHz27dZ9xPI91YNfSlI67/EMkjHHIHORiClQe
a=rtpmap:111 opus/48000/2
…

SDP testing tool

此图显示了整个WebRTC信令流程:

Complete WebRTC signaling