2021-04-03 11:06:39 +08:00
|
|
|
const stopMediaStreamTracks = (stream) => {
|
|
|
|
if (stream && typeof stream.getTracks === 'function') {
|
|
|
|
stream.getTracks().forEach(track => {
|
2021-07-06 00:01:13 +08:00
|
|
|
if (typeof track.stop === 'function' && track.readyState !== 'ended') {
|
2021-04-03 11:06:39 +08:00
|
|
|
track.stop();
|
2021-07-06 00:01:13 +08:00
|
|
|
// 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);
|
2021-04-03 11:06:39 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-07-02 22:05:09 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-04-03 11:06:39 +08:00
|
|
|
export default {
|
|
|
|
stopMediaStreamTracks,
|
|
|
|
getVideoTracks,
|
2021-07-02 22:05:09 +08:00
|
|
|
extractVideoDeviceId,
|
2021-04-03 11:06:39 +08:00
|
|
|
};
|