简体   繁体   English

在javascript中进行音频呼叫时使用webRTC进行屏幕共享

[英]Screen sharing using webRTC when audio call in javascript

I am going to make screen sharing function using webRTC.我将使用 webRTC 制作屏幕共享功能。 My code is working well when video calling But in audio call status, that is not working.我的代码在视频通话时运行良好但在音频通话状态下,这不起作用。 Here is my code.这是我的代码。

This is for create peer Connection and add stream for audio calling这是用于创建对等连接并为音频呼叫添加流

const senders = [];
var mediaConstraints = {audio: true, video: false}
navigator.mediaDevices.getUserMedia(mediaConstraints)
    .then(function (localStream) {
        localLiveStream = localStream;
        document.getElementById("local_video").srcObject = localLiveStream;
        localLiveStream.getTracks().forEach(track => senders.push(myPeerConnection.addTrack(track, localLiveStream)));
            })
    .catch(handleGetUserMediaError);

when screen share field当屏幕共享字段

mediaConstraints.video = true;
let displayStream = await navigator.mediaDevices.getDisplayMedia(mediaConstraints)
if (displayStream) {
    document.getElementById("local_video").srcObject = displayStream;
    console.log("senders: ", senders);
    try {
        senders.find(sender => sender.track.kind === 'video').replaceTrack(displayStream.getTracks()[0]);
    } catch (e) {
        console.log("Error: ", e)
    }
}

In screen sharing status, sender.track.kind is "audio" So在屏幕共享状态下,sender.track.kind 是“音频”所以

senders.find(sender => sender.track.kind === 'video') = null.

As this, replaceTrack makes error is there any other way for screen share?因此,replaceTrack 出错了还有其他屏幕共享方式吗?

You need to add a video track in order to achieve this.您需要添加视频轨道才能实现此目的。 It will require renegotiation.这将需要重新谈判。

So add the screen track (not replace) to the connection and then create the offer again!因此,将屏幕轨道(而不是替换)添加到连接中,然后再次创建报价!

connection.addTrack(screenVideoTrack);

Check this for reference:检查此以供参考:

https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection/onnegotiationneeded https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection/onnegotiationneeded

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

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