gpt4 book ai didi

javascript - PeerJS 屏幕共享卡在屏幕共享停止

转载 作者:行者123 更新时间:2023-12-03 20:50:07 25 4
gpt4 key购买 nike

我已经通过 peerJS WebRTC 设置了屏幕共享。但是当共享屏幕的人从他的一端停止屏幕共享时,屏幕共享会被其他用户卡住。我想为所有用户从 dom 中删除屏幕共享流视频。
这是我的 script.js 前端代码:

const socket = io("/");
const videoGrid = document.getElementById("video-grid");
var myUserId = "";

const myPeer = new Peer(undefined, {
path: "/peerjs",
host: "/",
port: PORT,
});

let myVideoStream;
const myVideo = document.createElement("video");
myVideo.muted = true;

const peers = {};

navigator.mediaDevices
.getUserMedia({
video: true,
audio: true,
})
.then((stream) => {
myVideoStream = stream;
console.log("test");
addVideoStream(myVideo, stream);
myPeer.on("call", (call) => {
call.answer(stream);
const video = document.createElement("video");
call.on("stream", (userVideoStream) => {
addVideoStream(video, userVideoStream);
});
});

socket.on("user-connected", (userId) => {
myUserId = userId;
connectToNewUser(userId, stream);
});
// input value
let text = $("input");
// when press enter send message
$("html").keydown(function (e) {
if (e.which == 13 && text.val().length !== 0) {
socket.emit("message", text.val());
text.val("");
}
});
socket.on("createMessage", (message) => {
$("ul").append(`<li class="message"><b>user</b><br/>${message}</li>`);
scrollToBottom();
});
});

socket.on("user-disconnected", (userId) => {
if (peers[userId]) peers[userId].close();
});

myPeer.on("open", (id) => {
socket.emit("join-room", ROOM_ID, id);
});

function connectToNewUser(userId, stream) {
const call = myPeer.call(userId, stream);
const video = document.createElement("video");
call.on("stream", (userVideoStream) => {
addVideoStream(video, userVideoStream);
});
call.on("close", () => {
video.remove();
});

peers[userId] = call;
}

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

const scrollToBottom = () => {
var d = $(".main__chat_window");
d.scrollTop(d.prop("scrollHeight"));
};

const muteUnmute = () => {
const enabled = myVideoStream.getAudioTracks()[0].enabled;
console.log(enabled);
if (enabled) {
myVideoStream.getAudioTracks()[0].enabled = false;
setUnmuteButton();
} else {
setMuteButton();
myVideoStream.getAudioTracks()[0].enabled = true;
}
};

const playStop = () => {
// console.log("object");
let enabled = myVideoStream.getVideoTracks()[0].enabled;
if (enabled) {
myVideoStream.getVideoTracks()[0].enabled = false;
setPlayVideo();
} else {
setStopVideo();
myVideoStream.getVideoTracks()[0].enabled = true;
}
};

const exit = () => {
window.location.href = "/exit";
};

const copyInfo = () => {
navigator.clipboard.writeText(window.location.href);
};

const shareScreen = async () => {
let captureStream = null;

try {
captureStream = await navigator.mediaDevices.getDisplayMedia();
} catch (err) {
console.error("Error: " + err);
}
// connectToNewUser(myUserId, captureStream);
myPeer.call(myUserId, captureStream);
};

const setMuteButton = () => {
const html = `
<i class="fas fa-microphone"></i>
`;
document.querySelector(".main__mute_button").innerHTML = html;
document.getElementsByClassName(
"main__mute_button"
)[0].style.backgroundColor = "white";
document.getElementsByClassName("main__mute_button")[0].style.color = "black";
// document.getElementsByClassName("main__mute_button")[0].style.paddingLeft = "1.5rem !important";
// document.getElementsByClassName("main__mute_button")[0].style.paddingRight = "1.5rem !important";
};

const setUnmuteButton = () => {
const html = `
<i class="unmute fas fa-microphone-slash"></i>
`;
document.querySelector(".main__mute_button").innerHTML = html;
document.getElementsByClassName(
"main__mute_button"
)[0].style.backgroundColor = "red";
document.getElementsByClassName("main__mute_button")[0].style.color = "white";
// document.getElementsByClassName("main__mute_button")[0].style.paddingLeft = "0.5rem !important";
// document.getElementsByClassName("main__mute_button")[0].style.paddingRight = "0.5rem !important";
};

const setStopVideo = () => {
const html = `
<i class="fas fa-video"></i>
`;
document.querySelector(".main__video_button").innerHTML = html;
document.getElementsByClassName(
"main__video_button"
)[0].style.backgroundColor = "white";
document.getElementsByClassName("main__video_button")[0].style.color =
"black";
};

const setPlayVideo = () => {
const html = `
<i class="stop fas fa-video-slash"></i>
`;
document.querySelector(".main__video_button").innerHTML = html;
document.getElementsByClassName(
"main__video_button"
)[0].style.backgroundColor = "red";
document.getElementsByClassName("main__video_button")[0].style.color =
"white";
};

我应该包括哪些内容以及在哪里,以便其他用户也可以删除屏幕共享流?

最佳答案

屏幕共享仅适用于 https,您需要添加 ssl 证书以进行屏幕共享。因此 peerjs 无法通过 http://localhost 共享屏幕共享流。

关于javascript - PeerJS 屏幕共享卡在屏幕共享停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63306134/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com