WebRTC:为什么 Offer 需要为 DataChannel 启用音频或视频选项 on/true?

WebRTC: Why Offer needs audio or video option turned on/true for just DataChannel?

我正在尝试了解 WebRTC 的工作原理,主要是为了仅使用 DataChannel 进行游戏网络体验。这就是我到目前为止所做的。它聚集了 ICE 候选人。我有两个问题。

  1. 收集ICE需要offer吗?
  2. 为什么 offerToReceiveAudioofferToReceiveVideo 需要设置为真,我将只使用 Datachannel。 (没有将此选项之一设置为 true,ICE 不会出现)(已解决,请参阅下面的编辑)

这里是 fiddle:

https://jsfiddle.net/t431a815/9/

和代码:

var iceServers = [

] 

var config = {
  iceServers: iceServers,
  iceTransportPolicy: "all",
  rtcpMuxPolicy: 'negotiate'
};

var pcConstraints = {};
var offerOptions = {offerToReceiveAudio: true};

pcConstraints.optional = [{'googIPv6': true}]; // Whether we gather IPv6 candidates.

var pc = new RTCPeerConnection(config, pcConstraints);
pc.onicecandidate = iceCallback;
pc.createOffer(
  offerOptions
).then(
  gotDescription,
  error
);

function gotDescription(desc) {
  console.log("OFFER DESC:", desc);
  pc.setLocalDescription(desc);
}

function error() {
  console.log("sth goes wrong", arguments);
}

function iceCallback(event) {
  console.log("ICE!", JSON.stringify(event.candidate));
}

编辑:

已找到解决方案,但很奇怪,您只需要在提供报价之前创建一个数据通道,然后它就可以与 offerToReceiveAudio: false, offerToReceiveVideo: false

一起使用
var offererDataChannel = pc.createDataChannel('channel', {});

但是为什么呢?如果我想稍后创建它怎么办?

您需要发送一些东西。否则就像运送一个空空如也的盒子,或者流式传输一个没有内容的流。没有好处。

建立对等连接的所有努力都是在为每件需要发送的东西协商工作 IP+端口对。例如。一对用于视频,一对用于音频,and/or 一对用于数据。

在要约 SDP 中,每件作品都需要自己的 "m-line",这最容易形象化。

var pc = new RTCPeerConnection();

pc.createDataChannel("dummy");
pc.createOffer().then(({sdp}) =>
   sdp.split('\r\n').filter(s => s.startsWith('m=')).map(s => console.log(s)));
<script src="https://webrtc.github.io/adapter/adapter-latest.js"></script>

稍后添加视频或音频(或者添加数据,如果您最初没有的话)需要 re-negotiation,这与初始协商的成本相同。

例外是额外的数据通道,它们都在同一个端口上进行多路复用,不需要重新协商,因为,你知道,你现在在对等点之间有一个数据通道。

换句话说,如果您想稍后添加数据通道,请先制作一个虚拟通道。之后所有的数据通道基本上都是免费的。反之,如果你想要以后的成本,请稍后连接。