【问题标题】:WebRTC & Peer JS Calling IssuesWebRTC & Peer JS 调用问题
【发布时间】:2020-08-22 05:55:36
【问题描述】:

在我的函数 connect(id) 中:我使用 var call = this.peer.call(id, this.mediastream); 开始通话,然后我使用

call.on('stream', function(stream) 
    {
        // `stream` is the MediaStream of the remote peer.
        // Here you'd add it to an HTML video/canvas element.
        peerVideo.srcObject = stream;
        console.log("Call stream has started! CLinet end!!!!!" + stream);
        output.innerHTML = "Streaming audio from other client...";
      });

从对等端获取音频。此 call.on 流永远不会执行,因此这意味着该调用不会返回任何内容。

在对等方面,他们可以很好地听到呼叫者的声音。不知道怎么回事!

这里是完整的代码:

const video = document.getElementById("video");
const peerVideo = document.getElementById("peervideo");
const output = document.getElementById("output");
const peerText = document.getElementById("peerid");
const peerButton = document.getElementById("peersubmit");


var promise = null;
var mediastream = null;
var peer = null;
var myId = null;

async function init()
{
    //init the media devices
    initMediaDevices();

    //init peer
    initPeer();

    //Add timeout to ensure the peer.on method had time to get id from server
    setTimeout(() => {  console.log("My ID is: " + this.myId); output.innerHTML = "My ID: " + formatString(this.myId)}, 2000);
}

function initMediaDevices()
{

    //Setup stream for usermedia
    try
    {
        this.promise = navigator.mediaDevices.getUserMedia({audio: true})

        promise.then((stream) =>
        {
            setStream(stream);
            //video.srcObject = stream;
            console.log(stream);
        })

        output.innerHTML = "Audio was established!";
    }
    catch(err)
    {
        console.log(err)
        output.innerHTML = "Audio Failed!"
    }
}

function initPeer()
{
    this.peer = new Peer();
    this.peer.on('open', function(id)
    {
        setID(id)
    });

    peer.on('call', function(call) 
    {
        // Answer the call, providing our mediaStream
        call.answer(this.mediastream);
        console.log(call + "-------------------- got here peer call");
        output.innerHTML = "Connected to Peer";

        call.on('stream', function(stream) 
        {
            // `stream` is the MediaStream of the remote peer.
            // Here you'd add it to an HTML video/canvas element.
            peerVideo.srcObject = stream;
            console.log("Call stream has started! Peer end");
            output.innerHTML = "Streaming audio from other client...";
          });
      });
}

function setID(id)
{
    this.myId = id;
}

function setStream(stream)
{
    this.mediastream = stream;
    console.log("Media Stream Set! " + this.mediastream);
}

function connect(id)
{
    var call = this.peer.call(id, this.mediastream);

    call.on('stream', function(stream) 
    {
        // `stream` is the MediaStream of the remote peer.
        // Here you'd add it to an HTML video/canvas element.
        peerVideo.srcObject = stream;
        console.log("Call stream has started! CLinet end!!!!!" + stream);
        output.innerHTML = "Streaming audio from other client...";
      });

      console.log(call + "----------------------" + this.mediastream + " This is the person that connected");
}

init();

//Event listeners
peerButton.addEventListener("click", () => 
{
    let id = peerText.value;
    console.log("Button Pressed!")
    connect(id);
});

//unrelated
function formatString(string)
{
    var newString = "";
    for (var i = 0; i < string.length; i++) 
    {
        var letter = string.charAt(i);
        if(isNaN(letter))
        {
            newString += letter.fontcolor("red");
        }  
        else
        {
            newString += letter;
        }
    }
    return newString;
}

还有html

<video id="video" autoplay>Video Stream no available</video>
<video id="peervideo" autoplay>Video Stream no available</video>
<h3 id="output"></h3>

<input type="text" id="peerid">
<input type="submit" value="Submit" id="peersubmit">

【问题讨论】:

    标签: javascript webrtc getusermedia peerjs


    【解决方案1】:

    我有同样的问题。 当您决定设置调试器并在周围设置断点时

     var call = this.peer.call(id, this.mediastream);
    
    call.on('stream', function(s
    

    您将收到流,听起来您需要设置一些 setTimeout 来防止这种行为,我知道它有效......但这是不好的做法

    【讨论】:

      【解决方案2】:

      添加超时连接功能它将起作用

      您的connect(id) 在新用户完成navigator.getUserMedia(); 之前触发

      【讨论】:

        【解决方案3】:

        其实是因为你在navigator.mediaDevices.getUserMedia之前执行了call.on

        所以尝试使用await initMediaDevices 来确保initPeer(); 之后执行。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2016-07-20
          • 2016-02-10
          • 1970-01-01
          • 2018-02-16
          • 1970-01-01
          • 1970-01-01
          • 2013-01-30
          相关资源
          最近更新 更多