我在哪里可以找到 WebRTC API 的官方和最新文档?

Where can I find official and up-to-date documentation for the WebRTC API?

我知道 adapter.js,它试图:

insulate apps from spec changes and prefix differences.

但是 adpater.js 只涵盖了非常基本的 WebRTC API。我将仅以 setRemoteDescription 为例。

2013中是这样调用的:

pc.setRemoteDescription(offer);

根据https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection,当前API是?

  pc.setRemoteDescription(new RTCSessionDescription(offer), function() {
    pc.createAnswer(function(answer) {
      pc.setLocalDescription(new RTCSessionDescription(answer), function() {
        // send the answer to a server to be forwarded back to the caller (you)
      }, error);
    }, error);
  }, error);

我目前正在阅读 an article from 2015 似乎是:

pc2.setRemoteDescription(pc1_offer).then(step3, failed);

编辑

http://www.w3.org/TR/webrtc/ 相差不远,但我更愿意寻找可以跟踪 Firefox 和 Chrome 的 API 当前实施状态的东西(is 超过 will-be 如果这是有道理的)。

WebRTC 仍在开发中,因此目前没有比规范更好的资源了,了解最新变化的最佳位置是:http://w3c.github.io/webrtc-pc

当涉及到各个浏览器以及所实现的内容时,它就变得更难了。看看:http://iswebrtcreadyyet.com

您在问题中观察到的主要 API 差异是 2014 年对 promises 的支持。所有异步方法现在 return 一个 Promise,而不是采用一对成功和失败回调。 Chrome 尚未实现,但 Firefox 实现了。

以下是在 Firefox 中运行的完整 WebRTC 调用(注意:使用 arrow functions):

var pc1 = new mozRTCPeerConnection(), pc2 = new mozRTCPeerConnection();

pc1.onicecandidate = e => !e.candidate ||
    pc2.addIceCandidate(e.candidate).catch(failed);
pc2.onicecandidate = e => !e.candidate ||
    pc1.addIceCandidate(e.candidate).catch(failed);
pc2.onaddstream = e => v2.mozSrcObject = e.stream;

function start() {
  navigator.mediaDevices.getUserMedia({ video: true, audio: true })
  .then(stream => pc1.addStream(v1.mozSrcObject = stream))
  .then(() => pc1.createOffer())
  .then(offer => pc1.setLocalDescription(offer))
  .then(() => pc2.setRemoteDescription(pc1.localDescription))
  .then(() => pc2.createAnswer())
  .then(answer => pc2.setLocalDescription(answer))
  .then(() => pc1.setRemoteDescription(pc2.localDescription))
  .then(() => log("Connected!"))
  .catch(failed);
}

var log = msg => div.innerHTML += "<p>" + msg + "</p>";
var failed = e => log(e.name +": "+ e.message +" line "+ e.lineNumber);
<video id="v1" height="120" width="160" autoplay></video>
<video id="v2" height="120" width="160" autoplay></video><br>
<button onclick="start()">Start!</button><div id="div"></div>

那是 20 行代码,比您可能见过的其他版本要少很多。

如果它看起来很神秘,请不要失望,因为 promises 和箭头函数是新概念,需要一些时间来适应,尤其是像这样组合使用时。我建议使用上面的链接分别阅读它们。

旧的回调版本仍然适用于所有 RTCPeerConnection 方法,因此使用 promises 是可选的。 Chrome 支持浏览器中的 promises(只是不支持 WebRTC),但还不支持箭头函数,因此可能需要一段时间才能使上述内容变得普遍。

撇开这种差异不谈,Chrome 和 Firefox 在高级呼叫设置方面相当稳定,甚至是重新协商。仍在变化的规范领域与新的较低级别的控制界面有关,如 RTCRtpSender,以及从指定流到指定轨道的重新聚焦(使用 addTrack 而不是 addStream 等.).

不幸的是,据我所知没有好的特定于浏览器的文档链接。正如您指出的那样 MDN link is outdated (though the MDN link for getUserMedia 最近更新了,所以希望这会改变)。我发现查看工作演示和示例仍然最有助于了解不同浏览器支持的内容。

我知道的一些不同之处:

  • - Chrome 在这里实现了大约 2013 年的规范,而 Firefox 实现了规范(但仅限于 width/height/frameRate 和 facingMode)。
  • - Chrome 的实现虽然更成熟,但却是非标准的,而 Firefox 实现了规范(但仅限于 rtp/rtcp、ice candidates 和其他一些东西)。
  • RTCOfferOptions - Chrome 窒息这些,所以而不是例如{ offerOptions: true } 使用较旧的 { mandatory: OfferOptions: true }(注意大小写差异 'o' 与 'O'),因为这在两种浏览器中都有效。
  • addTrack/removeTrack 现在坚持使用 addStream(stream)(虽然 Firefox 没有实现 removeStream,所以在那里使用 removeTrack)。

由于前缀和其他差异,您仍然需要 adapter.js,但是 adapter.js 可以而且应该做的更多。希望它的较新版本会通过为上述某些差异提供 polyfill 来进一步缩小这种差距。

我对 Firefox 的了解比 Chrome 更好,所以如果我遗漏了什么,我深表歉意。

希望以上内容有所帮助。