d8dae1ec90
Firefox doesnt fire the ended evt/onended callback for live video mediastreamtracks. That caused the stream storage to not run the cleanup procedure in some scenarios Manually emit the ended event which works with the onended callback when a track is stopped
47 lines
1.3 KiB
JavaScript
Executable File
47 lines
1.3 KiB
JavaScript
Executable File
const stopMediaStreamTracks = (stream) => {
|
|
if (stream && typeof stream.getTracks === 'function') {
|
|
stream.getTracks().forEach(track => {
|
|
if (typeof track.stop === 'function' && track.readyState !== 'ended') {
|
|
track.stop();
|
|
// Manually emit the event as a safeguard; Firefox doesn't fire it when it
|
|
// should with live MediaStreamTracks...
|
|
const trackStoppedEvt = new MediaStreamTrackEvent('ended', { track });
|
|
track.dispatchEvent(trackStoppedEvt);
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
const getVideoTracks = (stream) => {
|
|
let videoTracks = [];
|
|
|
|
if (stream) {
|
|
if (typeof stream.getVideoTracks === 'function') {
|
|
videoTracks = stream.getVideoTracks();
|
|
} else if (typeof stream.getTracks === 'function') {
|
|
videoTracks = stream.getTracks().filter(track => track.kind === 'video');
|
|
}
|
|
}
|
|
|
|
return videoTracks;
|
|
}
|
|
|
|
const extractVideoDeviceId = (stream) => {
|
|
// An empty string is the browser's default...
|
|
let deviceId = '';
|
|
const tracks = getVideoTracks(stream);
|
|
|
|
if (tracks[0] && typeof tracks[0].getSettings === 'function') {
|
|
const settings = tracks[0].getSettings();
|
|
deviceId = settings.deviceId;
|
|
}
|
|
|
|
return deviceId;
|
|
}
|
|
|
|
export default {
|
|
stopMediaStreamTracks,
|
|
getVideoTracks,
|
|
extractVideoDeviceId,
|
|
};
|