Reputation: 1
I am creating a video chat application and I need to have screen share functionality. The problem is when I am screen sharing, the updated stream won't be available for other peers through the "connection" method. I need a way to replace the stream of all the peers who are currently connected with me.
Below is the code related to that. it says. connection is deprecated
const switchStream = (stream: MediaStream) => {
setStream(stream);
//have to repalce the current stream for every conection
// Check if currentPeer exists and has connections before proceeding
if (currentPeer && currentPeer.connections) {
// Loop through each connection
Object.values(currentPeer.connections).forEach((connection: any) => {
// Find the video track in the stream
const videoTrack: any = stream
?.getTracks()
.find((track: any) => track.kind === "video");
// Replace the video track for each sender in the connection
connection[0].peerConnection.getSenders().forEach((sender: any) => {
if (sender.track && sender.track.kind === "video") {
sender
.replaceTrack(videoTrack)
.catch((err: any) => console.error(err));
}
});
});
} else {
console.error("currentPeer or currentPeer.connections is undefined.");
}
};
Upvotes: 0
Views: 63
Reputation: 1
Alternative Solution: instead of replacing the current stream with a display media Stream, I have connected the screen sharing as a new peer to the same room. then that real-time screen share feed will be shared to all the connected peers.
Upvotes: 0