openvidu-components: Fixed switch camera on Android devices

pull/803/head
Carlos Santos 2023-05-04 16:14:19 +02:00
parent 75c9576f4d
commit 4dc98e3272
2 changed files with 34 additions and 23 deletions

View File

@ -20,8 +20,8 @@ export class DeviceService {
private devices: Device[];
private cameras: CustomDevice[] = [];
private microphones: CustomDevice[] = [];
private cameraSelected: CustomDevice | null;
private microphoneSelected: CustomDevice | null;
private cameraSelected: CustomDevice | undefined;
private microphoneSelected: CustomDevice | undefined;
private log: ILogger;
private videoDevicesEnabled: boolean = true;
private audioDevicesEnabled: boolean = true;
@ -169,11 +169,11 @@ export class DeviceService {
return this.hasAudioDeviceAvailable() && this._isAudioMuted;
}
getCameraSelected(): CustomDevice | null {
getCameraSelected(): CustomDevice | undefined {
return this.cameraSelected;
}
getMicrophoneSelected(): CustomDevice | null {
getMicrophoneSelected(): CustomDevice | undefined {
return this.microphoneSelected;
}
@ -228,8 +228,8 @@ export class DeviceService {
this.devices = [];
this.cameras = [];
this.microphones = [];
this.cameraSelected = null;
this.microphoneSelected = null;
this.cameraSelected = undefined;
this.microphoneSelected = undefined;
this.videoDevicesEnabled = true;
this.audioDevicesEnabled = true;
}

View File

@ -582,29 +582,29 @@ export class OpenViduService {
}
private async createMediaStream(pp: PublisherProperties): Promise<MediaStream> {
let mediaStream: MediaStream;
const isFirefoxPlatform = this.platformService.isFirefox();
const isReplacingAudio = !!pp.audioSource;
const isReplacingVideo = !!pp.videoSource;
const currentCameraSelected = this.deviceService.getCameraSelected();
const currentMicSelected = this.deviceService.getMicrophoneSelected();
const isReplacingAudio = Boolean(pp.audioSource);
const isReplacingVideo = Boolean(pp.videoSource);
try {
mediaStream = await this.OV.getUserMedia(pp);
const trackType = isReplacingAudio ? 'audio' : 'video';
this.forceStopMediaTracks(this.participantService.getMyCameraPublisher().stream.getMediaStream(), trackType);
return this.OV.getUserMedia(pp);
} catch (error) {
console.warn('Error creating MediaStream', error);
if ((<OpenViduError>error).name === OpenViduErrorName.DEVICE_ACCESS_DENIED) {
if (isFirefoxPlatform) {
this.log.w('The device requested is not available. Restoring the older one');
// The track requested is not available so we are getting the old tracks ids for recovering the track
if (isReplacingVideo) {
pp.videoSource = this.deviceService.getCameraSelected().device;
} else if (isReplacingAudio) {
pp.audioSource = this.deviceService.getMicrophoneSelected().device;
}
mediaStream = await this.OV.getUserMedia(pp);
// TODO show error alert informing that the new device is not available
this.log.w('The device requested is not available. Restoring the older one');
// The track requested is not available so we are getting the old tracks ids for recovering the track
if (isReplacingVideo) {
pp.videoSource = currentCameraSelected?.device;
} else if (isReplacingAudio) {
pp.audioSource = currentMicSelected?.device;
}
// TODO show error alert informing that the new device is not available
return this.OV.getUserMedia(pp);
}
} finally {
return mediaStream;
throw error;
}
}
@ -663,4 +663,15 @@ export class OpenViduService {
private cleanConnectionData(data: string): string {
return data.split('%/%')[0];
}
private forceStopMediaTracks(stream: MediaStream, type: 'video' | 'audio'): void {
if (stream) {
stream.getTracks().forEach((track) => {
if (track.kind === type) {
track.stop();
track.enabled = false;
}
});
}
}
}