# All files / src/lib/services/device device.service.ts

83.19% Statements 99/119
79.12% Branches 72/91
89.79% Functions 44/49
84.84% Lines 84/99

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    2021x
    1x
    1x
    1x
    1x

    1x
    34x
    34x
    34x

    68x
    34x

    1x

    31x
    31x
    31x
    31x
    31x
    31x
    31x
    31x

    5x
    5x
    5x
    5x
    5x
    2x
    2x

    5x
    2x
    4x

    29x
    29x

    37x
    12x
    23x

    15x
    43x
    15x
    29x

    29x

    2x
    1x

    27x
    14x

    29x

    15x

    9x

    9x
    9x
    2x

    7x

    9x
    9x
    9x
    2x

    11x

    11x
    11x
    2x

    9x

    11x
    11x
    11x
    2x

    1x
    1x

    3x

    1x
    1x

    3x

    2x

    2x

    2x

    1x

    19x

    13x

    2x

    2x

    25x

    29x

    5x
    5x

    1x

    1x

     import { Injectable } from '@angular/core';
    import { Device, OpenVidu } from 'openvidu-browser';
    I
    import { CameraType, IDevice } from '../../models/device.model';
    import { ILogger } from '../../models/logger.model';
    import { Storage } from '../../models/storage.model';

    import { LoggerService } from '../logger/logger.service';
    import { PlatformService } from '../platform/platform.service';
    import { StorageService } from '../storage/storage.service';

    @Injectable({
    	providedIn: 'root'
    })
    export class DeviceService {
    	private OV: OpenVidu = null;
    	private devices: Device[];
    	private cameras: IDevice[] = [];
    	private microphones: IDevice[] = [];
    	private camSelected: IDevice;
    	private micSelected: IDevice;
    	private log: ILogger;
    	private videoDevicesDisabled: boolean;
    	private audioDevicesDisabled: boolean;

    	constructor(private loggerSrv: LoggerService, private platformSrv: PlatformService, private storageSrv: StorageService) {
    		this.log = this.loggerSrv.get('DevicesService');
    		this.OV = new OpenVidu();
    	}

    	async initDevices() {
    		await this.initOpenViduDevices();
    		this.devices.length > 0 ? this.log.d('Devices found: ', this.devices) : this.log.w('No devices found!');
    		this.resetDevicesArray();
    		if (this.hasAudioDeviceAvailable()) {
    			this.initAudioDevices();
    			this.micSelected = this.getMicSelected();
    		}
    		if (this.hasVideoDeviceAvailable()) {
    			this.initVideoDevices();
    			this.camSelected = this.cameras.find((device) => device.type === CameraType.FRONT);
    		}
    	}
    	private async initOpenViduDevices() {
    		this.devices = await this.OV.getDevices();
    	}

    	private initAudioDevices() {
    		const audioDevices = this.devices.filter((device) => device.kind === 'audioinput');
    		audioDevices.forEach((device: Device) => {
    			this.microphones.push({ label: device.label, device: device.deviceId });
    		});
    	}

    	private initVideoDevices() {
    		const FIRST_POSITION = 0;
    		const videoDevices = this.devices.filter((device) => device.kind === 'videoinput');
    		videoDevices.forEach((device: Device, index: number) => {
    			const myDevice: IDevice = {
    				label: device.label,
    				device: device.deviceId,
    				type: CameraType.BACK
    			};
    			if (this.platformSrv.isMobile()) {
    				// We assume front video device has 'front' in its label in Mobile devices
    				if (myDevice.label.toLowerCase().includes(CameraType.FRONT.toLowerCase())) {
    					myDevice.type = CameraType.FRONT;
    				}
    			} else {
    				// We assume first device is web camera in Browser Desktop
    				if (index === FIRST_POSITION) {
    					myDevice.type = CameraType.FRONT;
    				}
    			}

    			this.cameras.push(myDevice);
    		});
    		this.log.d('Camera selected', this.camSelected);
    	}

    	getCamSelected(): IDevice {
    		if (this.cameras.length === 0) {
    			this.log.e('No video devices found!');
    			return;
    		}
    		const IstorageDevice = this.getCamFromStorage();
    		if (storageDevice) {
    			return storageDevice;
    		}
    		return this.camSelected || this.cameras[0];
    	}

    	private getCamFromStorage() {
    		let storageDevice = this.storageSrv.get(Storage.VIDEO_DEVICE);
    		storageDevice = this.getCameraByDeviceField(storageDevice?.device);
    		if (storageDevice) {
    			return storageDevice;
    		}
    	}

    	getMicSelected(): IDevice {
    		if (this.microphones.length === 0) {
    			this.log.e('No audio devices found!');
    			returIn;
    		}
    		const storageDevice = this.getMicFromStogare();
    		if (storageDevice) {
    			return storageDevice;
    		}
    		return this.micSelected || this.microphones[0];
    	}

    	private getMicFromStogare(): IDevice {
    		let storageDevice = this.storageSrv.get(Storage.AUDIO_DEVICE);
    		storageDevice = this.getMicrophoneByDeviceField(storageDevice?.device);
    		if (storageDevice) {
    			return storageDevice;
    		}
    	}

    	setCamSelected(deviceField: any) {
    		this.camSelected = this.getCameraByDeviceField(deviceField);
    		this.saveCamToStorage(this.camSelected);
    	}

    	private saveCamToStorage(cam: IDevice) {
    		this.storageSrv.set(Storage.VIDEO_DEVICE, cam);
    	}

    	setMicSelected(deviceField: any) {
    		this.micSelected = this.getMicrophoneByDeviceField(deviceField);
    		this.saveMicToStorage(this.micSelected);
    	}
    	private saveMicToStorage(mic: IDevice) {
    		this.storageSrv.set(Storage.AUDIO_DEVICE, mic);
    	}

    	needUpdateVideoTrack(newVideoSource: string): boolean {
    		return this.getCamSelected().device !== newVideoSource;
    	}

    	needUpdateAudioTrack(newAudioSource: string): boolean {
    		return this.getMicSelected().device !== newAudioSource;
    	}

    	getCameras(): IDevice[] {
    		return this.cameras;
    	}

    	getMicrophones(): IDevice[] {
    		return this.microphones;
    	}

    	hasVideoDeviceAvailable(): boolean {
    		return !this.videoDevicesDisabled && !!this.devices?.find((device) => device.kind === 'videoinput');
    	}

    	hasAudioDeviceAvailable(): boolean {
    		return !this.audioDevicesDisabled && !!this.devices?.find((device) => device.kind === 'audioinput');
    	}

    	cameraNeedsMirror(deviceField: string): boolean {
    		return this.getCameraByDeviceField(deviceField)?.type === CameraType.FRONT;
    	}

    	areEmptyLabels(): boolean {
    		return !!this.cameras.find((device) => device.label === '') || !!this.microphones.find((device) => device.label === '');
    	}

    	disableVideoDevices() {
    		this.videoDevicesDisabled = true;
    	}

    	disableAudioDevices() {
    		this.audioDevicesDisabled = true;
    	}

    	clear() {
    		this.OV = new OpenVidu();
    		this.devices = [];
    		this.cameras = [];
    		this.microphones = [];
    		this.camSelected = null;
    		this.micSelected = null;
    		this.videoDevicesDisabled = false;
    		this.audioDevicesDisabled = false;
    	}

    	private getCameraByDeviceField(deviceField: any): IDevice {
    		return this.cameras.find((opt: IDevice) => opt.device === deviceField || opt.label === deviceField);
    	}

    	private getMicrophoneByDeviceField(deviceField: any): IDevice {
    		return this.microphones.find((opt: IDevice) => opt.device === deviceField || opt.label === deviceField);
    	}

    	private resetDevicesArray() {
    		this.cameras = [{ label: 'None', device: null, type: null }];
    		this.microphones = [{ label: 'None', device: null, type: null }];
    	}
    }