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

View File

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