I am working on a project in which we are using an audio call feature of WebRTC. Audio call works fine. On button click we need to enable share screen and replaceTracks with share screen track only, but replaceTrack web api replaces tracks of same kind, for example audio track can only be replaced with audio track or video track can only be replaced with video track. In my case, I am trying to achieve two things: #1: I want to enable share screen while the audio track is on. #2: If I can replace the audio track with share screen track appended in it without renegotiations.
Please check the code :-
(async () => {
try {
await navigator.mediaDevices.getDisplayMedia(
{
cursor: true
}).then(stream => {
let videoTrack = stream.getVideoTracks()[0];
var senders = peerConn.getSenders();
var sender = senders.find(function (s) {
return s.track.kind == videoTrack.kind;
});
sender.replaceTrack(videoTrack);
videoTrack.onended = function () {
sender.replaceTrack(localStream.getTracks()[1]);
}
});
} catch (err) {
console.log('(async () =>: ' + err);
}
})();
In this s.track.kind is audio and videoTrack.kind is video. sender.replaceTrack(videoTrack) gives error as it replace track of same kind