2021-02-02 02:47:05 +08:00
|
|
|
import Storage from '/imports/ui/services/storage/session';
|
2024-05-29 21:26:11 +08:00
|
|
|
import { getStorageSingletonInstance } from '/imports/ui/services/storage';
|
2021-02-02 02:47:05 +08:00
|
|
|
import getFromUserSettings from '/imports/ui/services/users-settings';
|
2021-04-03 11:06:39 +08:00
|
|
|
import MediaStreamUtils from '/imports/utils/media-stream-utils';
|
2024-04-20 04:34:43 +08:00
|
|
|
import VideoService from '/imports/ui/components/video-provider/video-provider-graphql/service';
|
2022-07-13 01:50:16 +08:00
|
|
|
import BBBVideoStream from '/imports/ui/services/webrtc-base/bbb-video-stream';
|
2022-09-06 02:52:29 +08:00
|
|
|
import browserInfo from '/imports/utils/browserInfo';
|
2021-07-02 22:05:09 +08:00
|
|
|
|
2022-09-06 02:52:29 +08:00
|
|
|
// GUM retry + delay params (Chrome only for now)
|
|
|
|
const GUM_MAX_RETRIES = 5;
|
|
|
|
const GUM_RETRY_DELAY = 200;
|
2023-04-27 00:09:02 +08:00
|
|
|
const CAMERA_AS_CONTENT_PROFILE_ID = 'fhd';
|
2021-07-02 22:05:09 +08:00
|
|
|
|
|
|
|
const getDefaultProfile = () => {
|
2024-05-29 21:26:11 +08:00
|
|
|
const BBBStorage = getStorageSingletonInstance();
|
|
|
|
// Unfiltered, includes hidden profiles
|
|
|
|
const CAMERA_PROFILES = window.meetingClientSettings.public.kurento.cameraProfiles || [];
|
|
|
|
|
2022-11-04 04:57:54 +08:00
|
|
|
return CAMERA_PROFILES.find(profile => profile.id === BBBStorage.getItem('WebcamProfileId'))
|
2022-10-03 19:09:03 +08:00
|
|
|
|| CAMERA_PROFILES.find(profile => profile.id === VideoService.getUserParameterProfile())
|
2021-07-02 22:05:09 +08:00
|
|
|
|| CAMERA_PROFILES.find(profile => profile.default)
|
|
|
|
|| CAMERA_PROFILES[0];
|
2024-05-29 21:26:11 +08:00
|
|
|
};
|
2021-07-02 22:05:09 +08:00
|
|
|
|
2023-04-27 00:09:02 +08:00
|
|
|
const getCameraAsContentProfile = () => {
|
2024-05-29 21:26:11 +08:00
|
|
|
// Unfiltered, includes hidden profiles
|
|
|
|
const CAMERA_PROFILES = window.meetingClientSettings.public.kurento.cameraProfiles || [];
|
|
|
|
|
2023-04-27 00:09:02 +08:00
|
|
|
return CAMERA_PROFILES.find(profile => profile.id == CAMERA_AS_CONTENT_PROFILE_ID)
|
|
|
|
|| CAMERA_PROFILES.find(profile => profile.default)
|
2024-05-29 21:26:11 +08:00
|
|
|
};
|
2023-04-27 00:09:02 +08:00
|
|
|
|
2021-07-02 22:05:09 +08:00
|
|
|
const getCameraProfile = (id) => {
|
2024-05-29 21:26:11 +08:00
|
|
|
// Unfiltered, includes hidden profiles
|
|
|
|
const CAMERA_PROFILES = window.meetingClientSettings.public.kurento.cameraProfiles || [];
|
2021-07-02 22:05:09 +08:00
|
|
|
return CAMERA_PROFILES.find(profile => profile.id === id);
|
2024-05-29 21:26:11 +08:00
|
|
|
};
|
2021-04-03 11:06:39 +08:00
|
|
|
|
|
|
|
// VIDEO_STREAM_STORAGE: Map<deviceId, MediaStream>. Registers WEBCAM streams.
|
|
|
|
// Easier to keep track of them. Easier to centralize their referencing.
|
|
|
|
// Easier to shuffle them around.
|
|
|
|
const VIDEO_STREAM_STORAGE = new Map();
|
|
|
|
|
|
|
|
const storeStream = (deviceId, stream) => {
|
2021-07-02 22:05:09 +08:00
|
|
|
if (!stream) return false;
|
|
|
|
|
2021-04-03 11:06:39 +08:00
|
|
|
// Check if there's a dangling stream. If there's one and it's active, cool,
|
|
|
|
// return false as it's already stored. Otherwised, clean the derelict stream
|
|
|
|
// and store the new one
|
|
|
|
if (hasStream(deviceId)) {
|
|
|
|
const existingStream = getStream(deviceId);
|
|
|
|
if (existingStream.active) return false;
|
|
|
|
deleteStream(deviceId);
|
|
|
|
}
|
|
|
|
|
|
|
|
VIDEO_STREAM_STORAGE.set(deviceId, stream);
|
|
|
|
|
|
|
|
// Stream insurance: clean it up if it ends (see the events being listened to below)
|
2022-07-13 05:44:20 +08:00
|
|
|
stream.once('inactive', () => {
|
2021-07-20 04:19:06 +08:00
|
|
|
deleteStream(deviceId);
|
|
|
|
});
|
2021-04-03 11:06:39 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const getStream = (deviceId) => {
|
|
|
|
return VIDEO_STREAM_STORAGE.get(deviceId);
|
|
|
|
}
|
|
|
|
|
|
|
|
const hasStream = (deviceId) => {
|
|
|
|
return VIDEO_STREAM_STORAGE.has(deviceId);
|
|
|
|
}
|
|
|
|
|
|
|
|
const deleteStream = (deviceId) => {
|
|
|
|
const stream = getStream(deviceId);
|
|
|
|
if (stream == null) return false;
|
|
|
|
MediaStreamUtils.stopMediaStreamTracks(stream);
|
|
|
|
return VIDEO_STREAM_STORAGE.delete(deviceId);
|
|
|
|
}
|
2021-02-02 02:47:05 +08:00
|
|
|
|
2020-12-22 03:47:00 +08:00
|
|
|
const promiseTimeout = (ms, promise) => {
|
|
|
|
const timeout = new Promise((resolve, reject) => {
|
|
|
|
const id = setTimeout(() => {
|
|
|
|
clearTimeout(id);
|
|
|
|
|
|
|
|
const error = {
|
|
|
|
name: 'TimeoutError',
|
|
|
|
message: 'Promise did not return',
|
|
|
|
};
|
|
|
|
|
|
|
|
reject(error);
|
|
|
|
}, ms);
|
|
|
|
});
|
|
|
|
|
|
|
|
return Promise.race([
|
|
|
|
promise,
|
|
|
|
timeout,
|
|
|
|
]);
|
|
|
|
};
|
|
|
|
|
2021-02-02 02:47:05 +08:00
|
|
|
const getSkipVideoPreview = () => {
|
2024-03-07 01:28:18 +08:00
|
|
|
const KURENTO_CONFIG = window.meetingClientSettings.public.kurento;
|
2021-02-02 02:47:05 +08:00
|
|
|
|
|
|
|
const skipVideoPreviewOnFirstJoin = getFromUserSettings(
|
|
|
|
'bbb_skip_video_preview_on_first_join',
|
|
|
|
KURENTO_CONFIG.skipVideoPreviewOnFirstJoin,
|
|
|
|
);
|
|
|
|
const skipVideoPreview = getFromUserSettings(
|
|
|
|
'bbb_skip_video_preview',
|
|
|
|
KURENTO_CONFIG.skipVideoPreview,
|
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
|
|
|
(Storage.getItem('isFirstJoin') !== false && skipVideoPreviewOnFirstJoin)
|
|
|
|
|| skipVideoPreview
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2021-07-02 22:05:09 +08:00
|
|
|
// Takes a raw list of media devices of any media type coming enumerateDevices
|
|
|
|
// and a deviceId to be prioritized
|
|
|
|
// Outputs an object containing:
|
|
|
|
// webcams: videoinput media devices, priorityDevice being the first member of the array (if it exists)
|
|
|
|
// areLabelled: whether all videoinput devices are labelled
|
|
|
|
// areIdentified: whether all videoinput devices have deviceIds
|
2021-04-03 11:06:39 +08:00
|
|
|
const digestVideoDevices = (devices, priorityDevice) => {
|
|
|
|
const webcams = [];
|
|
|
|
let areLabelled = true;
|
|
|
|
let areIdentified = true;
|
|
|
|
|
|
|
|
devices.forEach((device) => {
|
|
|
|
if (device.kind === 'videoinput') {
|
|
|
|
if (!webcams.some(d => d.deviceId === device.deviceId)) {
|
|
|
|
// We found a priority device. Push it to the beginning of the array so we
|
|
|
|
// can use it as the "initial device"
|
|
|
|
if (priorityDevice && priorityDevice === device.deviceId) {
|
|
|
|
webcams.unshift(device);
|
|
|
|
} else {
|
|
|
|
webcams.push(device);
|
|
|
|
}
|
|
|
|
|
2021-07-02 22:05:09 +08:00
|
|
|
if (!device.label) { areLabelled = false }
|
|
|
|
if (!device.deviceId) { areIdentified = false }
|
2021-04-03 11:06:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Returns the list of devices and whether they are labelled and identified with deviceId
|
|
|
|
return {
|
|
|
|
webcams,
|
|
|
|
areLabelled,
|
|
|
|
areIdentified,
|
|
|
|
};
|
2022-09-06 02:52:29 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
const _retry = (foo, opts) => new Promise((resolve, reject) => {
|
|
|
|
const {
|
|
|
|
retries = 1,
|
|
|
|
delay = 0,
|
|
|
|
error: bubbledError,
|
|
|
|
errorRetryList = [],
|
|
|
|
} = opts;
|
|
|
|
|
|
|
|
if (!retries) return reject(bubbledError);
|
|
|
|
|
|
|
|
return foo().then(resolve).catch((_error) => {
|
|
|
|
if (errorRetryList.length > 0
|
|
|
|
&& !errorRetryList.some((eName) => _error.name === eName)) {
|
|
|
|
reject(_error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const newOpts = {
|
|
|
|
...opts,
|
|
|
|
retries: retries - 1,
|
|
|
|
error: _error,
|
|
|
|
};
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
_retry(foo, newOpts).then(resolve).catch(reject);
|
|
|
|
}, delay);
|
|
|
|
});
|
|
|
|
});
|
2021-04-03 11:06:39 +08:00
|
|
|
|
2021-07-20 04:19:06 +08:00
|
|
|
// Returns a promise that resolves an instance of BBBVideoStream or rejects an *Error
|
2021-07-02 22:05:09 +08:00
|
|
|
const doGUM = (deviceId, profile) => {
|
2024-05-29 21:26:11 +08:00
|
|
|
const GUM_TIMEOUT = window.meetingClientSettings.public.kurento.gUMTimeout;
|
|
|
|
|
2021-07-02 22:05:09 +08:00
|
|
|
// Check if this is an already loaded stream
|
|
|
|
if (deviceId && hasStream(deviceId)) {
|
|
|
|
return Promise.resolve(getStream(deviceId));
|
|
|
|
}
|
|
|
|
|
|
|
|
const constraints = {
|
|
|
|
audio: false,
|
|
|
|
video: { ...profile.constraints },
|
|
|
|
};
|
|
|
|
|
|
|
|
if (deviceId) {
|
|
|
|
constraints.video.deviceId = { exact: deviceId };
|
|
|
|
}
|
|
|
|
|
2021-07-20 04:19:06 +08:00
|
|
|
const postProcessedgUM = (cts) => {
|
2022-09-06 02:52:29 +08:00
|
|
|
const ppGUM = () => navigator.mediaDevices.getUserMedia(cts)
|
|
|
|
.then((stream) => new BBBVideoStream(stream));
|
|
|
|
|
|
|
|
// Chrome/Edge sometimes bork gUM calls when switching camera
|
|
|
|
// profiles. This looks like a browser bug. Track release not
|
|
|
|
// being done synchronously -> quick subsequent gUM calls for the same
|
2024-03-18 21:58:53 +08:00
|
|
|
// device (profile switching) -> device becoming unavailable while previous
|
2022-09-06 02:52:29 +08:00
|
|
|
// tracks aren't finished - prlanzarin
|
|
|
|
if (browserInfo.isChrome || browserInfo.isEdge) {
|
|
|
|
const opts = {
|
|
|
|
retries: GUM_MAX_RETRIES,
|
|
|
|
errorRetryList: ['NotReadableError'],
|
|
|
|
delay: GUM_RETRY_DELAY,
|
|
|
|
};
|
|
|
|
return _retry(ppGUM, opts);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ppGUM();
|
2021-07-20 04:19:06 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
return promiseTimeout(GUM_TIMEOUT, postProcessedgUM(constraints));
|
2022-09-06 02:52:29 +08:00
|
|
|
};
|
2021-07-02 22:05:09 +08:00
|
|
|
|
2021-07-20 04:19:06 +08:00
|
|
|
const terminateCameraStream = (bbbVideoStream, deviceId) => {
|
2021-07-02 22:05:09 +08:00
|
|
|
// Cleanup current stream if it wasn't shared/stored
|
2021-07-20 04:19:06 +08:00
|
|
|
if (bbbVideoStream && !hasStream(deviceId)) {
|
|
|
|
bbbVideoStream.stop()
|
2021-07-02 22:05:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-07 07:10:56 +08:00
|
|
|
export default {
|
2020-12-22 03:47:00 +08:00
|
|
|
promiseTimeout,
|
2018-11-07 07:10:56 +08:00
|
|
|
changeWebcam: (deviceId) => {
|
2024-05-29 21:26:11 +08:00
|
|
|
getStorageSingletonInstance().setItem('WebcamDeviceId', deviceId);
|
2018-11-07 07:10:56 +08:00
|
|
|
},
|
2024-05-29 21:26:11 +08:00
|
|
|
webcamDeviceId: () => getStorageSingletonInstance().getItem('WebcamDeviceId'),
|
2019-04-09 06:07:26 +08:00
|
|
|
changeProfile: (profileId) => {
|
2024-05-29 21:26:11 +08:00
|
|
|
getStorageSingletonInstance().setItem('WebcamProfileId', profileId);
|
2019-04-09 06:07:26 +08:00
|
|
|
},
|
2021-02-02 02:47:05 +08:00
|
|
|
getSkipVideoPreview,
|
2021-04-03 11:06:39 +08:00
|
|
|
storeStream,
|
|
|
|
getStream,
|
|
|
|
hasStream,
|
|
|
|
deleteStream,
|
|
|
|
digestVideoDevices,
|
2021-07-02 22:05:09 +08:00
|
|
|
getDefaultProfile,
|
2023-04-27 00:09:02 +08:00
|
|
|
getCameraAsContentProfile,
|
2021-07-02 22:05:09 +08:00
|
|
|
getCameraProfile,
|
|
|
|
doGUM,
|
|
|
|
terminateCameraStream,
|
2018-11-07 07:10:56 +08:00
|
|
|
};
|