具有 3 个用户连接的 WebRTC

2024-04-03

我现在正在实施源代码WebRTC 示例 https://github.com/webrtc/samples/tree/gh-pages/src/content/peerconnection/audio通过使用网状拓扑成为 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>

Note:我是 webRTC 的新手。我可能在某些方面比较笨,请原谅我。

太感谢了。


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

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

具有 3 个用户连接的 WebRTC 的相关文章

随机推荐

  • Keras 用于语义分割,flow_from_directory() 错误

    我试图使用我对 Keras 文档中示例代码的修改 该文档展示了在使用图像掩码代替标签的情况下如何设置 image datagen flow from directory 用于图像分割 我们在其中预测一个类 对于每个像素 顺便说一下 我设置了
  • 如何在 Android 应用程序中使用 yahoo contact api?

    我想从 yahoo contact api 获取所有电子邮件 ID 和姓名here http developer yahoo com social contacts 我已经阅读了 yahoo contact api 的文档 通过我的帐户登录
  • 在 iPhone 应用程序中播放 YouTube 视频而不使用 UIWebView?

    我想通过我的 iPhone 应用程序播放 YouTube 视频 我必须尝试使用 以下代码在我的 iPhone 应用程序中播放 YouTube 视频 self playVideo http www youtube com watch v WL
  • 限制sql窗口函数中的结果集

    假设我想重写以下聚合查询 select id max hittime from status group by id 使用聚合窗口函数 例如 select id max hittime over partition by id order
  • 我什么时候应该和不应该使用这个 C# 实用程序类通过 Interlocked 控制线程

    我试图理解这个类的编写方式背后的逻辑 以及何时应该和不应该使用它 任何见解将不胜感激 internal struct SpinLock private volatile int lockHeld private readonly stati
  • NiFi:ReplaceTextWithMapping 处理器

    我有以下插入语句 insert into temp1 values test1 test2 insert into temp2 values test3 预期成绩 insert into temp1 values 100 200 inser
  • 上传的音频文件无法在 rshiny 中播放

    我开发了一个应用程序 它采用 wav 文件作为输入并播放它 然而它似乎不起作用 另一方面 如果音频文件放置在 www 文件夹中并给出路径名 则可以正常播放 我究竟做错了什么 app R library shinydashboard ui s
  • datagridview vb.net 中特定单元格的单击事件

    我在 datagridview 中有一个单元格 它位于第 8 行第 2 列 该单元格和该单元格仅在单击时我想显示为另存为对话框 但实际上我可以让特定单元格发生单击事件 我该如何执行此操作vb net 在 dataGridView 事件 Da
  • 具有动态适配器的动态列表视图

    我想创建一个动态列表视图 它在滚动到初始列表末尾时添加动态元素 每次滚动位置到达上一个列表的末尾时 都应添加新项目 我怎样才能实现这个目标 谢谢 您需要添加一个滚动侦听器并覆盖onscroll
  • 在 LightningChartJs 中获取一系列的可见点

    存在一个函数LightningChartJs得到所有visible points来自图表中的线或点系列 如果我缩放图表 如果没有可用的可见点 我想显示一些内容 在某些情况下 我的数据会出现中断 现在我必须检查范围并过滤该范围内的所有点 但这
  • Spring 集成超时客户端

    我的 Spring 集成场景是 使用自定义协议发送数据的数十个生产者 大小和内容 我必须解码这个自定义协议 然后处理结果 所以我尝试了很多配置 目前最好的配置如下
  • 如何在不同身份下运行iisexpress应用程序池

    有没有办法以当前登录用户以外的不同身份运行 iisexpress 应用程序池 我目前正在使用 runas 命令解决这个问题 但我想知道 iisexpress 是否有内置的东西可以利用 看起来这应该是可能的 在 IIS Express 站点的
  • 如何在FabricJS中将旋转点位置更改为底部?

    How to change Rotating point position to the bottom see image below 这里是选择对象的配置控制点的链接指南 http fabricjs com fabric intro pa
  • 在 shell 脚本中执行 Vim 命令

    我正在编写一个 Bash 脚本 该脚本运行命令行程序 Gromacs 保存结果 修改输入文件 然后再次循环该过程 我正在尝试使用 Vim 修改输入文本文件 但在打开输入文件后 我无法找到从 sh 文件执行内部 Vim 命令的方法 例如 12
  • get_map 未传递 API 密钥(HTTP 状态为“403 禁止”)

    我一直在面临这个问题get map 功能 ggmap库 在 R 中 我的代码无需指定 API 密钥即可运行 例如source google 持续了几个月 然而 该代码在几周前停止工作 我了解到 Google 已强制要求 API 密钥 或者可
  • Linux C/C++ 在动态库中分配/释放内存

    我必须将我的应用程序分成几个逻辑模块 mainapp module1 so module2 so module3 so 等等 其中每个模块是一个 so库 将在运行时加载 每个模块共享相同的接口 并将返回一些数据数组 例如 int ptr m
  • 有没有一种方法可以在一行中从数组值设置对象键

    假设我有一个像这样的数组 const myArray HP QP PS 我想要一个对象 其键是myArray的值如 HP 0 QP 0 PS 0 有没有办法在一行中执行以下操作 const myObj myArray forEach ite
  • 是否有标准方法可以在编译时确定系统是 32 位还是 64 位?

    我需要设置 ifdef 检查条件编译 我想自动化该过程 但无法指定目标操作系统 机器 有什么方法可以让预编译器判断它是在 32 位还是 64 位上运行吗 解释 我需要定义一个 64 位大小的类型 在 64 位操作系统上它是一个 long 在
  • 在 Stylus 中访问 JavaScript 的原生数学库

    我最近问了一个问题是否可以用手写笔计算平方根 https stackoverflow com questions 21033346 得到这个问题的答案后 我想知道是否有一种方法可以完全访问 Stylus 中的 JavaScript 原生数学
  • 具有 3 个用户连接的 WebRTC

    我现在正在实施源代码WebRTC 示例 https github com webrtc samples tree gh pages src content peerconnection audio通过使用网状拓扑成为 3 个用户连接 但是