I am using Webrtc in flutter to share my screen. i have this code
Signaling {
RTCVideoRenderer localRenderer = RTCVideoRenderer();
RTCVideoRenderer remoteRenderer = RTCVideoRenderer();
MediaStream? localStream;
MediaStream? remoteStream;
Future<void> makeScreenSharing() async {
final mediaConstraints = <String, dynamic>{'audio': true, 'video': true};
try {
var stream =
await navigator.mediaDevices.getDisplayMedia(mediaConstraints);
localStream = stream;
localRenderer.srcObject = localStream;
} catch (e) {
print(e.toString());
}
}
this code will result in sharing my screen on my own page but the other connection is still receiving my Camera.
I fix the issue you have to replace the tracks
Future<void> replaceMediaStream(MediaStream newStream) {
return peerConnection?.senders.then((senders) {
senders.forEach((sender) async {
if (sender.track?.kind == 'video') {
if (newStream.getVideoTracks().length > 0) {
await sender.replaceTrack(newStream.getVideoTracks()[0]);
}
} else if (sender.track?.kind == 'audio') {
if (newStream.getAudioTracks().length > 0) {
await sender.replaceTrack(newStream.getAudioTracks()[0]);
}
}
});
return Future.value();
}) ??
Future.error(
Exception('An error occurred during switching the stream'));
}
call this function after you have set local streams