【问题标题】:WebRTC with 3 users connection具有 3 个用户连接的 WebRTC
【发布时间】:2016-08-21 11:42:34
【问题描述】:

我现在正在实现WebRTC Samples 的源代码,通过使用网状拓扑成为 3 个用户连接。

但是,我的代码并没有像我想象的那样工作。我想我被困在调用函数iceCallBack#(# 指的是数字 1、2、3),因为输出结果与原始结果相同。它只能连接2个用户。

我不知道如何以适当的方式修复它。

这是我一直在处理的一些 JavaScript 代码:

    var audio2 = document.querySelector('audio#audio2');
    var audio3 = document.querySelector('audio#audio3');
    var pc1;
    var pc2;
    var pc3;

    function call() {
      callButton.disabled = true;
      codecSelector.disabled = true;
      trace('Starting call');
      var servers = null;
      var pcConstraints = {
        'optional': []
      };
      pc1 = new RTCPeerConnection(servers, pcConstraints);
      trace('Created local peer connection object pc1');
      pc1.onicecandidate = iceCallback1;

      pc2 = new RTCPeerConnection(servers, pcConstraints);
      trace('Created remote peer connection object pc2');
      pc2.onicecandidate = iceCallback2;
      pc2.onaddstream = gotRemoteStream;
      trace('Requesting local stream');

      pc3 = new RTCPeerConnection(servers, pcConstraints);
      trace('Created remote peer connection object pc2');
      pc3.onicecandidate = iceCallback3; 
      pc3.onaddstream = gotRemoteStream2;
      trace('Requesting local stream');

      navigator.mediaDevices.getUserMedia({
        audio: true,
        video: false
      })
      .then(gotStream)
      .catch(function(e) {
        alert('getUserMedia() error: ' + e.name);
      });
    }


    //Description of pc1 creating offer to pc2
    function gotDescription1(desc) {
      desc.sdp = forceChosenAudioCodec(desc.sdp);
      trace('Offer from pc1 \n' + desc.sdp);
      pc1.setLocalDescription(desc, function() {
        pc2.setRemoteDescription(desc, function() {
          pc2.createAnswer(gotDescription2, onCreateSessionDescriptionError);
        }, onSetSessionDescriptionError);
      }, onSetSessionDescriptionError);
    }

    //Description of pc1 creating offer to pc3
    function gotDescription3(desc) {
      desc.sdp = forceChosenAudioCodec(desc.sdp);
      trace('Offer from pc1 \n' + desc.sdp);
      pc1.setLocalDescription(desc, function() {
        pc3.setRemoteDescription(desc, function() {
          pc3.createAnswer(gotDescription4, onCreateSessionDescriptionError); //Must edit gotDescription4
        }, onSetSessionDescriptionError);
      }, onSetSessionDescriptionError);
    }

    //Creating answer from pc2
    function gotDescription2(desc) {
      desc.sdp = forceChosenAudioCodec(desc.sdp);
      pc2.setLocalDescription(desc, function() {
        trace('Answer from pc2 \n' + desc.sdp);
        pc1.setRemoteDescription(desc, function() {
        }, onSetSessionDescriptionError);
      }, onSetSessionDescriptionError);
    }

    //Creating answer from pc3
    function gotDescription4(desc) {
      desc.sdp = forceChosenAudioCodec(desc.sdp);
      pc3.setLocalDescription(desc, function() {
        trace('Answer from pc2 \n' + desc.sdp);
        pc1.setRemoteDescription(desc, function() {
        }, onSetSessionDescriptionError);
      }, onSetSessionDescriptionError);
    }

    function iceCallback1(event) {
      if (event.candidate) {
        pc2.addIceCandidate(new RTCIceCandidate(event.candidate),
            onAddIceCandidateSuccess, onAddIceCandidateError);
        pc3.addIceCandidate(new RTCIceCandidate(event.candidate),
            onAddIceCandidateSuccess, onAddIceCandidateError);
        trace('Local ICE candidate: \n' + event.candidate.candidate);
      }
    }

    function iceCallback2(event) {
      if (event.candidate) {
        pc1.addIceCandidate(new RTCIceCandidate(event.candidate),
            onAddIceCandidateSuccess, onAddIceCandidateError);
        pc3.addIceCandidate(new RTCIceCandidate(event.candidate),
            onAddIceCandidateSuccess, onAddIceCandidateError);
        trace('Remote ICE candidate: \n ' + event.candidate.candidate);
      }
    }

    function iceCallback3(event) {
      if (event.candidate) {
        pc1.addIceCandidate(new RTCIceCandidate(event.candidate),
            onAddIceCandidateSuccess, onAddIceCandidateError);
        pc2.addIceCandidate(new RTCIceCandidate(event.candidate),
            onAddIceCandidateSuccess, onAddIceCandidateError);
        trace('Remote ICE candidate: \n ' + event.candidate.candidate);
      }
    }
<div id="audio">
      <div>
        <div class="label">Local audio:</div><audio id="audio1" autoplay controls muted></audio>
      </div>
      <div>
        <div class="label">Remote audio2:</div><audio id="audio2" autoplay controls></audio>
      </div>
      <div>
        <div class="label">Remote audio3:</div><audio id="audio3" autoplay controls></audio>
      </div>
</div>

注意:我是 webRTC 的新手。我可能有点笨,请见谅。

非常感谢。

【问题讨论】:

  • 抱歉,这不是调试站点。介意问一个问题吗?
  • 'RTCPeerConnection().addIceCandidate()'可以同时使用2次吗?因为我希望它双向通信。例如,A --> B,C。 B --> A,C。 C --> A,B
  • 你有一个有效的三向示例吗?我一定是愚蠢的,我被困在你所在的地方,似乎无法超越它
  • @kaioker2 你有没有想办法让它发挥作用?
  • @JatinGarg 不,我没有srry

标签: javascript webrtc p2p


【解决方案1】:

3 个用户的网格意味着每个用户建立两个连接,一个连接到其他两个用户。在每个客户端,这是两个完全不同的 RTCPeerConnections,您不能在它们之间重复使用候选者,因为每个候选者都包含专门为媒体分配的端口号和要发送到的目标。

如果您知道如何建立一个连接,那么您就会知道如何建立两个。只是把事情分开。

【讨论】:

  • 此评论是关键,还记得跟踪谁已连接和谁未连接,因此当新客户进来时,您可以添加他们并直接从他们那里流式传输,而无需重新发送优惠和什么不是。
  • 我显然一无所知 iv 已经尝试与三个用户一起工作 2 周了。我找不到任何教程或说明。你有一个工作的3路例子吗?每次我遇到信号问题并且无法解开流。 2个很容易,三个似乎几乎不可能
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-24
  • 2021-12-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多