meething / StreamSwitcher

Allows MediaStream to switch tracks without setting srcObject this allows MediaRecording to continue recording

Home Page:https://meething.github.io/StreamSwitcher/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

StreamSwitcher

Allows MediaStream to switch tracks without setting srcObject this allows MediaRecording to continue recording.

Problem

The MediaRecorder is designed to stop recording when you switch streams during recording.

Solution

Use RTCPSender to switch stream without MediaRecorder to stop recording. Another solution would be using MediaSourceBuffers but that is way more error sensitive in case of different codes etc..

Result

A stream where you switch for instance camera with screenshare and back without need to stop and start the MediaRecorder

How to use it

HTML file

<head>
<script src="https://cdn.jsdelivr.net/gh/meething/StreamSwitcher@latest/js/ReplaceableMediaStream.js"></script>
</head>

JS file

//Setup video with empty stream
var temporaryStream = new MediaStream();
video.srcObject = temporaryStream.remoteStream;

//Change the track from a stream
video.srcObject.replaceVideoTrack(stream.getVideoTracks()[0])//Video
video.srcObject.replaceAudioTrack(stream.getAudioTracks()[0])//Audio

//If not autoplay
video.play();

Resource

About

Allows MediaStream to switch tracks without setting srcObject this allows MediaRecording to continue recording

https://meething.github.io/StreamSwitcher/

License:MIT License


Languages

Language:JavaScript 81.3%Language:HTML 13.1%Language:CSS 5.5%