rtc-tools

The rtc-tools module does most of the heavy lifting within the rtc.io suite. Primarily it handles the logic of coupling a local RTCPeerConnection with it's remote counterpart via an rtc-signaller signalling channel.

NPM

Build Status unstable bitHound Score Gitter chat

Getting Started

If you decide that the rtc-tools module is a better fit for you than either rtc-quickconnect or rtc then the code snippet below will provide you a guide on how to get started using it in conjunction with the rtc-signaller (version 5.0 and above) and rtc-media modules:

var messenger = require('rtc-switchboard-messenger');
var signaller = require('rtc-signaller')(messenger('https://switchboard.rtc.io/'));
var rtc = require('rtc-tools');
var getUserMedia = require('getusermedia');
var attachMedia = require('attachmediastream');

// capture local media first as firefox
// will want a local stream and doesn't support onnegotiationneeded event
getUserMedia({ video: true, audio: true }, function(err, localStream) {
  if (err) {
    return console.error('could not capture media: ', err);
  }

  document.body.appendChild(attachMedia(localStream));

  // look for friends
  signaller.on('peer:announce', function(data) {
    var pc = rtc.createConnection();
    var monitor = rtc.couple(pc, data.id, signaller);

    // add the stream to the connection
    pc.addStream(localStream);

    // once the connection is active, log a console message
    monitor.once('connected', function() {
      console.log('connection active to: ' + data.id);

      pc.getRemoteStreams().forEach(function(stream) {
        document.body.appendChild(attachMedia(stream));
      });
    });


    monitor.createOffer();
  });

  // announce ourself in the rtc-getting-started room
  signaller.announce({ room: 'rtc-getting-started' });
});

This code definitely doesn't cover all the cases that you need to consider (i.e. peers leaving, etc) but it should demonstrate how to:

  1. Capture video and add it to a peer connection
  2. Couple a local peer connection with a remote peer connection
  3. Deal with the remote steam being discovered and how to render that to the local interface.

Reference

createConnection

createConnection(opts?, constraints?) => RTCPeerConnection

Create a new RTCPeerConnection auto generating default opts as required.

var conn;

// this is ok
conn = rtc.createConnection();

// and so is this
conn = rtc.createConnection({
  iceServers: []
});

rtc-tools/cleanup

cleanup(pc)

The cleanup function is used to ensure that a peer connection is properly closed and ready to be cleaned up by the browser.

rtc-tools/couple

couple(pc, targetId, signaller, opts?)

Couple a WebRTC connection with another webrtc connection identified by targetId via the signaller.

The following options can be provided in the opts argument:

  • sdpfilter (default: null)

    A simple function for filtering SDP as part of the peer connection handshake (see the Using Filters details below).

Example Usage
var couple = require('rtc/couple');

couple(pc, '54879965-ce43-426e-a8ef-09ac1e39a16d', signaller);
Using Filters

In certain instances you may wish to modify the raw SDP that is provided by the createOffer and createAnswer calls. This can be done by passing a sdpfilter function (or array) in the options. For example:

// run the sdp from through a local tweakSdp function.
couple(pc, '54879965-ce43-426e-a8ef-09ac1e39a16d', signaller, {
  sdpfilter: tweakSdp
});

rtc-tools/detect

Provide the rtc-core/detect functionality.

rtc-tools/generators

The generators package provides some utility methods for generating constraint objects and similar constructs.

var generators = require('rtc/generators');

generators.config(config)

Generate a configuration object suitable for passing into an W3C RTCPeerConnection constructor first argument, based on our custom config.

In the event that you use short term authentication for TURN, and you want to generate new iceServers regularly, you can specify an iceServerGenerator that will be used prior to coupling. This generator should return a fully compliant W3C (RTCIceServer dictionary)[http://www.w3.org/TR/webrtc/#idl-def-RTCIceServer].

If you pass in both a generator and iceServers, the iceServers _will be ignored and the generator used instead.

generators.connectionConstraints(flags, constraints)

This is a helper function that will generate appropriate connection constraints for a new RTCPeerConnection object which is constructed in the following way:

var conn = new RTCPeerConnection(flags, constraints);

In most cases the constraints object can be left empty, but when creating data channels some additional options are required. This function can generate those additional options and intelligently combine any user defined constraints (in constraints) with shorthand flags that might be passed while using the rtc.createConnection helper.

rtc-tools/monitor

monitor(pc, targetId, signaller, parentBus) => mbus

The monitor is a useful tool for determining the state of pc (an RTCPeerConnection) instance in the context of your application. The monitor uses both the iceConnectionState information of the peer connection and also the various signaller events to determine when the connection has been connected and when it has been disconnected.

A monitor created mbus is returned as the result of a couple between a local peer connection and it's remote counterpart.

License(s)

Apache 2.0

Copyright 2015 National ICT Australia Limited (NICTA)

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.



rtc-tools

rtc-tools 模块在大部分内容中都起了重要的作用 rtc.io 套件。主要处理耦合的逻辑 一个本地的 RTCPeerConnection ,它通过一个远程对应的 rtc-signaller 信号 频道。

构建状态data-canonical-src </一> 不稳定

入门指南

如果您确定 rtc-tools 模块比您更适合您 rtc-quickconnect rtc ,下面的代码片段 将为您提供如何开始使用它的指南 rtc-signaller (版本5.0及更高版本) 和 rtc-media 模块:

var messenger = require('rtc-switchboard-messenger');
var signaller = require('rtc-signaller')(messenger('https://switchboard.rtc.io/'));
var rtc = require('rtc-tools');
var getUserMedia = require('getusermedia');
var attachMedia = require('attachmediastream');

// capture local media first as firefox // will want a local stream and doesn't support onnegotiationneeded event getUserMedia({ video: true, audio: true }, function(err, localStream) { if (err) { return console.error('could not capture media: ', err); }

document.body.appendChild(attachMedia(localStream));

// look for friends signaller.on('peer:announce', function(data) { var pc = rtc.createConnection(); var monitor = rtc.couple(pc, data.id, signaller);

<span class="pl-c"><span class="pl-c">//</span> add the stream to the connection</span>
<span class="pl-smi">pc</span>.<span class="pl-en">addStream</span>(localStream);

<span class="pl-c"><span class="pl-c">//</span> once the connection is active, log a console message</span>
<span class="pl-smi">monitor</span>.<span class="pl-en">once</span>(<span class="pl-s"><span class="pl-pds">&#39;</span>connected<span class="pl-pds">&#39;</span></span>, <span class="pl-k">function</span>() {
  <span class="pl-en">console</span>.<span class="pl-c1">log</span>(<span class="pl-s"><span class="pl-pds">&#39;</span>connection active to: <span class="pl-pds">&#39;</span></span> <span class="pl-k">+</span> <span class="pl-smi">data</span>.<span class="pl-c1">id</span>);

  <span class="pl-smi">pc</span>.<span class="pl-en">getRemoteStreams</span>().<span class="pl-c1">forEach</span>(<span class="pl-k">function</span>(<span class="pl-smi">stream</span>) {
    <span class="pl-c1">document</span>.<span class="pl-c1">body</span>.<span class="pl-c1">appendChild</span>(<span class="pl-en">attachMedia</span>(stream));
  });
});


<span class="pl-smi">monitor</span>.<span class="pl-en">createOffer</span>();

});

// announce ourself in the rtc-getting-started room signaller.announce({ room: 'rtc-getting-started' }); });

这段代码绝对不包括你需要考虑的所有情况 (即同伴离开等),但它应该演示如何:

  1. Capture video and add it to a peer connection
  2. Couple a local peer connection with a remote peer connection
  3. Deal with the remote steam being discovered and how to render that to the local interface.

参考

createConnection

createConnection(opts?, constraints?) => RTCPeerConnection

根据需要创建新的 RTCPeerConnection 自动生成默认选择。

var conn;

// this is ok conn = rtc.createConnection();

// and so is this conn = rtc.createConnection({ iceServers: [] });

rtc-tools / cleanup < h3>
cleanup(pc)

cleanup 功能用于确保对等连接正常 关闭并准备好由浏览器清理。

rtc-tools / couple

couple(pc, targetId, signaller, opts?)

将WebRTC连接与由其标识的另一个webrtc连接相结合 targetId

可以在 opts 参数

中提供以下选项
  • sdpfilter (默认值:null)

    作为对等体一部分过滤SDP的简单功能 连接握手(请参阅下面的使用过滤器详细信息)。

Example Usage
var couple = require('rtc/couple');

couple(pc, '54879965-ce43-426e-a8ef-09ac1e39a16d', signaller);

Using Filters

在某些情况下,您可能希望修改提供的原始SDP 通过 createOffer createAnswer 调用。这可以通过传递 选项中的 sdpfilter 函数(或数组)。例如:

// run the sdp from through a local tweakSdp function.
couple(pc, '54879965-ce43-426e-a8ef-09ac1e39a16d', signaller, {
  sdpfilter: tweakSdp
});

rtc-tools / detect

提供 rtc-core / detect 功能。

rtc-tools / generators

生成器包提供了一些用于生成的实用方法 约束对象和类似结构。

var generators = require('rtc/generators');

generators.config(config)

生成适合传入W3C的配置对象 根据我们的自定义配置,RTCPeerConnection构造函数第一个参数。

如果您对TURN使用短期身份验证,并且您希望 定期生成新的 iceServers ,可以指定一个iceServerGenerator 这将在耦合之前使用。这个发电机应该完全返回 兼容W3C(RTCIceServer字典)[ http://www.w3.org/TR/webrtc/#idl -def-RTCIceServer ]。

如果您同时通过发电机和iceServers,则IceServers将会 忽略并使用生成器。

generators.connectionConstraints(flags, constraints)

这是一个帮助函数,将生成适当的连接 构建的新的 RTCPeerConnection 对象的约束 以下方式:

var conn = new RTCPeerConnection(flags, constraints);

在大多数情况下,约束对象可以保留为空,但在创建时 数据通道需要一些附加选项。这个功能 可以生成这些附加选项,并智能地组合任何 用户定义的约束(在 constraint )中具有简写标记 可能会在使用 rtc.createConnection 帮助器时传递。

rtc-tools / monitor < h3>
monitor(pc, targetId, signaller, parentBus) => mbus

显示器是确定 pc (an。)的状态的有用工具 您的应用程序的上下文中的 RTCPeerConnection )实例。的 监视器同时使用对等体的 iceConnectionState 信息 连接也各种各样 信号事件 确定连接何时已连接以及何时连接 已断开连接

作为a的结果返回显示器创建的 mbus 本地对等体之间的夫妇 连接,它是远程对应的。

许可证

Apache 2.0

版权所有2015国家信息通信技术有限公司(NICTA)

根据Apache许可证2.0版(许可证)许可; 您不得使用此文件,除非符合许可证。 您可以在

获取许可证的副本
 http://www.apache.org/licenses/LICENSE-2.0

除非适用法律要求或书面同意,软件 根据许可证分发的分发是按原样的基础, 无明示或暗示的任何形式的担保或条件。 请参阅有关权限的特定语言的许可证 许可证下的限制。




相关问题推荐