简体   繁体   English

WebRTC peerConnection ontrack 监听器不会触发

[英]WebRTC peerConnection ontrack listener doesn't trigger

First of all, issue here is different from all I found in "google" as I'm using Spring Boot as signaling server to connect two different browser's tabs or they are using two peerConnection to send stream within one tab.首先,这里的问题与我在“google”中发现的所有问题不同,因为我使用 Spring Boot 作为信号服务器来连接两个不同的浏览器选项卡,或者他们使用两个 peerConnection 在一个选项卡中发送流。

Peers seem to be connected and messages are sent correctly.对等点似乎已连接并且消息已正确发送。 However, when I run sendStream with button, second tab track event listener doesn't trigger.但是,当我使用按钮运行 sendStream 时,不会触发第二个选项卡跟踪事件侦听器。

//connecting to our signaling server 
var conn = new WebSocket('ws://localhost:8080/socket');

conn.onopen = function() {
    console.log("Connected to the signaling server");
    initialize();
};

conn.onmessage = function(msg) {
    console.log("Got message", msg.data);
    var content = JSON.parse(msg.data);
    var data = content.data;
    switch (content.event) {
    // when somebody wants to call us
    case "offer":
        handleOffer(data);
        break;
    case "answer":
        handleAnswer(data);
        break;
    // when a remote peer sends an ice candidate to us
    case "candidate":
        handleCandidate(data);
        break;
    default:
        break;
    }
};

function send(message) {
    conn.send(JSON.stringify(message));
}

var peerConnection;
var dataChannel;
var input = document.getElementById("messageInput");

function initialize() {
    var configuration = null;

    peerConnection = new RTCPeerConnection(configuration);

    // Setup ice handling
    peerConnection.onicecandidate = function(event) {
        if (event.candidate) {
            send({
                event : "candidate",
                data : event.candidate
            });
        }
    };

    const remoteVideo = document.getElementById('video');

    peerConnection.addEventListener('track', async (event) => {
        const [remoteStream] = event.streams;
        remoteVideo.srcObject = remoteStream;
        console.log("STREAM RECEIVED");
    });
    
    // creating data channel
    dataChannel = peerConnection.createDataChannel("dataChannel", {
        reliable : true
    });

    dataChannel.onerror = function(error) {
        console.log("Error occured on datachannel:", error);
    };

    // when we receive a message from the other peer, printing it on the console
    dataChannel.onmessage = function(event) {
        console.log("message:", event.data);
    };

    dataChannel.onclose = function() {
        console.log("data channel is closed");
    };
  
    peerConnection.ondatachannel = function (event) {
        dataChannel = event.channel;
    };
    
}

function createOffer() {
    peerConnection.createOffer(function(offer) {
        send({
            event : "offer",
            data : offer
        });
        peerConnection.setLocalDescription(offer);
    }, function(error) {
        alert("Error creating an offer");
    });
}

function handleOffer(offer) {
    peerConnection.setRemoteDescription(new RTCSessionDescription(offer));

    // create and send an answer to an offer
    peerConnection.createAnswer(function(answer) {
        peerConnection.setLocalDescription(answer);
        send({
            event : "answer",
            data : answer
        });
    }, function(error) {
        alert("Error creating an answer");
    });

};

function handleCandidate(candidate) {
    peerConnection.addIceCandidate(new RTCIceCandidate(candidate));
};

function handleAnswer(answer) {
    peerConnection.setRemoteDescription(new RTCSessionDescription(answer));
    console.log("connection established successfully!!");
};

function sendMessage() {
    dataChannel.send(input.value);
    input.value = "";
}

async function sendStream() {
    const localStream = await navigator.mediaDevices.getUserMedia({audio: false, video: true});
    localStream.getTracks().forEach(track => {
        peerConnection.addTrack(track, localStream);
        console.log("STREAM SENT")
        const localVideo = document.getElementById('video2');
        addVideoStream(localVideo, localStream);
    });
}

function addVideoStream(video, stream) {
    video.srcObject = stream;
    video.addEventListener('loadedmetadata', () => {
        video.play();
    })
}

If you already have an established WebRTC connection, renegotiation is needed for the new track to be sent.如果您已经建立了 WebRTC 连接,则需要重新协商才能发送新曲目。

The reason is that peers need to choose codecs and other parameters included in SDP.原因是对等体需要选择编解码器和 SDP 中包含的其他参数。 Otherwise your browser won't tell how to compress the video you want to send so the receiver can decode it.否则,您的浏览器不会告诉您如何压缩您要发送的视频,以便接收者可以对其进行解码。

See the note on top of the page: https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection/addTrack请参阅页面顶部的注释: https ://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection/addTrack

So you probably need to set up onnegotiationneeded callback and then send a new offer / receive a new answer.因此,您可能需要设置onnegotiationneeded回调,然后发送新报价/接收新答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM