WebRTC之点对点连接

简介


一个 RTCPeerConnection 实例运行应用程序与另一个浏览器的RTCPeerConnection(或者另一个实现了该协议的端点)建立点对点连接。通信是通过在信令通道上交换控制消息(称为信令协议)来协调的,信令通道需要服务器提供(协议中并未特殊规定),例如使用XMLHttpRequest 或Web Sockets。


配置信息


  1. RTCConfiguration

    定义了一组参数,用于配置如何建立或重新建立通过RTCPeerConnection建立的对等通信。

  2. RTCIceServer

    用于描述ICE代理可以用来建立与对等点连接的 STUN 和 TURN 服务器。


状态定义


  1. RTCSignalingState

    信令状态

    • stable:offer/answer交换进程还没有执行,仅仅是初始化状态,local 和 remote 的 desc 都为空。

    • have-local-offer:local 成功应用 type 为 offer 的desc

      const offerOptions = {
          offerToReceiveAudio: 1,
          offerToReceiveVideo: 1
      };
      // 创建type为offer的desc
      const offerDesc =  localRTCPeerConnection.createOffer(offerOptions);
      // 本地连接
      localRTCPeerConnection.setLocalDescription(offerDesc);
      
    • have-remote-offer:remote 成功应用 type 为offer 的desc

      // 远程连接
      remoteRTCPeerConnection.setRemoteDescription(offerDesc);
      
    • have-local-pranswer:remote 成功应用 type 为offer的desc(上面程序的最后一行) ,并且 local的 type 为 pranswer 的 desc被成功应用。

      const answerDesc = await remoteRTCPeerConnection.createAnswer();
      remoteRTCPeerConnection.setLocalDescription(answerDesc);
      
    • have-remote-pranswer:local 的 type 为 pranswer 的desc被成功应用(上面程序的最后一行),并且remote 的 pranswer 也被成功应用。

      localRTCPeerConnection.setRemoteDescription(answerDesc);
      
    • closed:RTCPeerConnection 关闭。

      localRTCPeerConnection.close();
      remoteRTCPeerConnection.close();
      

非规范的信令状态转换图

一个简单的转换集可能是:

  • Caller transition:

    new RTCPeerConnection(): stable

    setLocalDescription(offer): have-local-offer

    setRemoteDescription(pranswer): have-remote-pranswer

    setRemoteDescription(answer): stable

  • Callee transition:

    new RTCPeerConnection(): stable

    setRemoteDescription(offer): have-remote-offer

    setLocalDescription(pranswer): have-local-pranswer

    setLocalDescription(answer): stable

    这可能与之前的描述有点不符,可我查阅到的文档确实是这样写的;我想 stable 应该是代表稳定的状态,并且我并没有找到创建 type 为 pranswer的方法,我希望你知道这些,并形成自己的判断。

RTCPeerConnection 接口


调用 new RTCPeerConnection(configuration), 创建一个RTCPeerConnection对象。

configuration.servers 包含用于查找和访问ICE使用的服务器的信息。应用程序可以提供每种类型的多个服务器,任何TRUN服务器也可以用作STUN服务器,用于收集服务器自反候选服务器(这句话我也不太理解…)。

一个RTCPeerConnection对象拥有一个 signaling state,一个connection state,一个ICE gathering state,和一个ICE connection state。当对象被创建时,这些都将初始化。

RTCPeerConnection的ICE协议实现由ICE代理表示。某些RTCPeerConnection方法涉及与ICE代理的交互,即addIceCandidatesetConfigurationsetLocalDescriptionsetRemoteDescriptionclose


Session Description Model


  1. RTCSdpType

    该枚举对象描述了 RTCSessionDescriptionInit或者RTCSessionDescription实例的类型。

    • offer:表明一个desc必须被作为 SDP offer 对待。
    • pranswer:表明一个desc必须被作为SDP answer对待,但不是最终的answer。 用作SDP pranswer的desc可以应用于对SDP offer 的响应,或对先前发送的SDP pranswer的更新。
    • answer:表明一个desc必须被作为最终的answer 对待。offer-answer 交互必须被认为是完整的。作为SDP answer的desc被用于响应 SDP offer或者作为更新之前发送的 SDP pranswer。
  2. RTCSessionDescription 类

    该类被用于RTCPeerConnection暴露local和remote 的 session descriptions; 值得一提的是,该类包含了一个toJSON()方法,这可在信令服务器要求传输该对象时使用。



点对点连接远远不止上面所写的内容,还有许多高级的特性,我没有写到,但灵活运用上面的知识已经能够创建基于WebRTC协议的对等连接了。


推荐博文


https://github.com/webrtc/samples WebRTC demo


参考博文


https://w3c.github.io/webrtc-pc/#peer-to-peer-connections 官方文档

  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值