WebRTC和对等JS调用问题

时间:2020-05-07 03:15:31

标签: javascript webrtc getusermedia peerjs

在函数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">

2 个答案:

答案 0 :(得分:0)

我有同样的问题。 当您决定设置调试器并在其中创建断点时

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

call.on('stream', function(s

您将收到流,听起来像您需要放置一些setTimeout来防止这种行为,而且我知道它可以工作...但是它的坏习惯

答案 1 :(得分:0)

添加超时连接功能即可使用

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