Better solution for multiple audio stream in mediaRecord for WebRTC call, which I found :
const startRecording = async () => {
try {
const screenStream = await navigator.mediaDevices.getDisplayMedia({
video: true,
audio: true,
});
const audioCtx = new AudioContext();
const source1 = audioCtx.createMediaStreamSource(stream.current.srcObject);
const source2 = audioCtx.createMediaStreamSource(participantStream.current.srcObject);
const destination = audioCtx.createMediaStreamDestination();
source1.connect(destination);
source2.connect(destination);
const outputStream = new MediaStream();
outputStream.addTrack(screenStream.getVideoTracks()[0]);
outputStream.addTrack(destination.stream.getAudioTracks()[0]);
mediaRecorderRef.current = new MediaRecorder(outputStream);
mediaRecorderRef.current.ondataavailable = (event) => {
if (event.data.size > 0) {
recordedChunksRef.current.push(event.data);
}
};
mediaRecorderRef.current.onstop = () => {
console.log("dddd");
setActiveOption({ index: -1 });
const recordedBlob = new Blob(recordedChunksRef.current, {
type: "video/mp4",
});
const downloadLink = document.createElement("a");
downloadLink.href = URL.createObjectURL(recordedBlob);
downloadLink.download = "recorded-screen.mp4";
document.body.appendChild(downloadLink);
downloadLink.click();
document.body.removeChild(downloadLink);
setRecording(false);
};
recordedChunksRef.current = [];
mediaRecorderRef.current.start();
screenStream.getVideoTracks()[0].addEventListener("ended", () => {
setIsOpenRecordingModal(true);
});
setRecording(true);
} catch (error) {
setActiveOption({ index: -1 });
console.error("Error accessing screen:", error);
}
};