bigbluebutton-Github/bigbluebutton-html5/imports/utils/media-stream-utils.js
prlanzarin d8dae1ec90 fix(webcams): handle Firefox video stream cleanup edge case
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
2021-07-05 16:26:24 +00:00

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,
};