mediasfu-reactnative
Version:
MediaSFU Prebuilt React Native SDK
87 lines • 3.57 kB
JavaScript
/**
* Switches the user's video device based on the provided video preference.
*
* @param {SwitchVideoOptions} options - The function parameters.
* @returns {Promise<void>}
*
* @example
* ```typescript
* switchVideo({
* videoPreference: "newVideoDeviceID",
* parameters: {
* recordStarted: true,
* recordResumed: false,
* recordStopped: false,
* recordPaused: false,
* recordingMediaOptions: "video",
* videoAlreadyOn: true,
* userDefaultVideoInputDevice: "currentVideoDeviceID",
* defVideoID: "defaultVideoDeviceID",
* allowed: true,
* updateDefVideoID: (deviceId) => setDefVideoID(deviceId),
* updatePrevVideoInputDevice: (deviceId) => setPrevVideoDevice(deviceId),
* updateUserDefaultVideoInputDevice: (deviceId) => setUserDefaultVideo(deviceId),
* updateIsMediaSettingsModalVisible: (isVisible) => setMediaSettingsModal(isVisible),
* showAlert: (alertOptions) => showAlert(alertOptions),
* switchUserVideo: switchUserVideoFunction,
* }
* });
* ```
*/
export const switchVideo = async ({ videoPreference, parameters }) => {
let { recordStarted, recordResumed, recordStopped, recordPaused, recordingMediaOptions, videoAlreadyOn, userDefaultVideoInputDevice, defVideoID, allowed, updateDefVideoID, updatePrevVideoInputDevice, updateUserDefaultVideoInputDevice, updateIsMediaSettingsModalVisible,
// mediasfu functions
showAlert, switchUserVideo, } = parameters;
// Check if recording is in progress and whether the selected video device is the default one
let checkoff = false;
if ((recordStarted || recordResumed) && !recordStopped && !recordPaused) {
if (recordingMediaOptions === 'video') {
checkoff = true;
}
}
// Check camera access permission
if (!allowed) {
showAlert === null || showAlert === void 0 ? void 0 : showAlert({
message: 'Allow access to your camera by starting it for the first time.',
type: 'danger',
duration: 3000,
});
return;
}
// Check video state and display appropriate alert messages
if (checkoff) {
if (videoAlreadyOn) {
showAlert === null || showAlert === void 0 ? void 0 : showAlert({
message: 'Please turn off your video before switching.',
type: 'danger',
duration: 3000,
});
return;
}
}
else if (!videoAlreadyOn) {
showAlert === null || showAlert === void 0 ? void 0 : showAlert({
message: 'Please turn on your video before switching.',
type: 'danger',
duration: 3000,
});
return;
}
// Set default video ID if not already set
if (!defVideoID) {
defVideoID = userDefaultVideoInputDevice !== null && userDefaultVideoInputDevice !== void 0 ? userDefaultVideoInputDevice : 'default';
updateDefVideoID(defVideoID);
}
// Switch video only if the selected video device is different from the default
if (videoPreference !== defVideoID) {
const prevVideoInputDevice = userDefaultVideoInputDevice;
updatePrevVideoInputDevice(prevVideoInputDevice);
userDefaultVideoInputDevice = videoPreference;
updateUserDefaultVideoInputDevice(userDefaultVideoInputDevice);
if (defVideoID) {
updateIsMediaSettingsModalVisible(false);
await switchUserVideo({ videoPreference, checkoff, parameters });
}
}
};
//# sourceMappingURL=switchVideo.js.map