2022-10-14 13:37:51 +02:00
|
|
|
import { Component, EventEmitter, OnDestroy, OnInit, Output } from '@angular/core';
|
2022-06-16 14:01:07 +02:00
|
|
|
import { PublisherProperties } from 'openvidu-browser';
|
|
|
|
import { Subscription } from 'rxjs';
|
|
|
|
import { CustomDevice } from '../../../models/device.model';
|
|
|
|
import { PanelType } from '../../../models/panel.model';
|
|
|
|
import { ParticipantAbstractModel } from '../../../models/participant.model';
|
|
|
|
import { DeviceService } from '../../../services/device/device.service';
|
|
|
|
import { OpenViduService } from '../../../services/openvidu/openvidu.service';
|
|
|
|
import { PanelService } from '../../../services/panel/panel.service';
|
|
|
|
import { ParticipantService } from '../../../services/participant/participant.service';
|
|
|
|
import { StorageService } from '../../../services/storage/storage.service';
|
|
|
|
import { VirtualBackgroundService } from '../../../services/virtual-background/virtual-background.service';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
@Component({
|
|
|
|
selector: 'ov-video-devices-select',
|
|
|
|
templateUrl: './video-devices.component.html',
|
|
|
|
styleUrls: ['./video-devices.component.css']
|
|
|
|
})
|
|
|
|
export class VideoDevicesComponent implements OnInit, OnDestroy {
|
2023-03-08 13:04:10 +01:00
|
|
|
@Output() onDeviceSelectorClicked = new EventEmitter<void>();
|
|
|
|
@Output() onVideoMutedClicked = new EventEmitter<boolean>();
|
2022-07-11 13:05:28 +02:00
|
|
|
|
2022-06-16 14:01:07 +02:00
|
|
|
videoMuteChanging: boolean;
|
|
|
|
isVideoMuted: boolean;
|
2022-10-27 13:32:09 +02:00
|
|
|
cameraSelected: CustomDevice | null;
|
2022-06-16 14:01:07 +02:00
|
|
|
hasVideoDevices: boolean;
|
2022-10-27 13:32:09 +02:00
|
|
|
cameras: CustomDevice[] = [];
|
2022-06-16 14:01:07 +02:00
|
|
|
localParticipantSubscription: Subscription;
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
private openviduService: OpenViduService,
|
|
|
|
protected panelService: PanelService,
|
|
|
|
private storageSrv: StorageService,
|
|
|
|
private deviceSrv: DeviceService,
|
|
|
|
protected participantService: ParticipantService,
|
|
|
|
private backgroundService: VirtualBackgroundService
|
|
|
|
) {}
|
|
|
|
|
2022-06-17 12:50:20 +02:00
|
|
|
async ngOnInit() {
|
2022-06-16 14:01:07 +02:00
|
|
|
this.subscribeToParticipantMediaProperties();
|
2022-06-17 12:50:20 +02:00
|
|
|
if (this.openviduService.isSessionConnected()) {
|
|
|
|
// Updating devices only with session connected
|
2022-10-27 17:25:59 +02:00
|
|
|
await this.deviceSrv.refreshDevices();
|
2022-06-17 12:50:20 +02:00
|
|
|
}
|
2022-10-27 17:25:59 +02:00
|
|
|
|
2022-06-16 14:01:07 +02:00
|
|
|
this.hasVideoDevices = this.deviceSrv.hasVideoDeviceAvailable();
|
2023-03-08 13:04:10 +01:00
|
|
|
if (this.hasVideoDevices) {
|
2022-10-27 13:32:09 +02:00
|
|
|
this.cameras = this.deviceSrv.getCameras();
|
|
|
|
this.cameraSelected = this.deviceSrv.getCameraSelected();
|
|
|
|
}
|
2022-06-16 14:01:07 +02:00
|
|
|
if (this.openviduService.isSessionConnected()) {
|
|
|
|
this.isVideoMuted = !this.participantService.getLocalParticipant().isCameraVideoActive();
|
|
|
|
} else {
|
|
|
|
this.isVideoMuted = this.deviceSrv.isVideoMuted();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
async ngOnDestroy() {
|
|
|
|
this.cameras = [];
|
|
|
|
if (this.localParticipantSubscription) this.localParticipantSubscription.unsubscribe();
|
|
|
|
}
|
|
|
|
|
|
|
|
async toggleCam() {
|
|
|
|
this.videoMuteChanging = true;
|
|
|
|
const publish = this.isVideoMuted;
|
2023-03-01 10:55:45 +01:00
|
|
|
await this.participantService.publishVideo(publish);
|
2022-06-16 14:01:07 +02:00
|
|
|
if (this.isVideoMuted && this.panelService.isExternalPanelOpened()) {
|
|
|
|
this.panelService.togglePanel(PanelType.BACKGROUND_EFFECTS);
|
|
|
|
}
|
|
|
|
this.videoMuteChanging = false;
|
2022-10-14 13:37:51 +02:00
|
|
|
this.onVideoMutedClicked.emit(publish);
|
2022-06-16 14:01:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async onCameraSelected(event: any) {
|
2023-03-08 13:04:10 +01:00
|
|
|
const device: CustomDevice = event?.value;
|
|
|
|
|
2022-06-16 14:01:07 +02:00
|
|
|
// Is New deviceId different from the old one?
|
2023-03-08 13:04:10 +01:00
|
|
|
if (this.deviceSrv.needUpdateVideoTrack(device)) {
|
|
|
|
const mirror = this.deviceSrv.cameraNeedsMirror(device.device);
|
2022-06-16 14:01:07 +02:00
|
|
|
// Reapply Virtual Background to new Publisher if necessary
|
|
|
|
const backgroundSelected = this.backgroundService.backgroundSelected.getValue();
|
2023-03-08 13:04:10 +01:00
|
|
|
const isBackgroundApplied = this.backgroundService.isBackgroundApplied();
|
2022-11-02 12:26:00 +01:00
|
|
|
|
|
|
|
if (isBackgroundApplied) {
|
2022-06-16 14:01:07 +02:00
|
|
|
await this.backgroundService.removeBackground();
|
|
|
|
}
|
2023-03-08 13:04:10 +01:00
|
|
|
const pp: PublisherProperties = { videoSource: device.device, audioSource: false, mirror };
|
2023-03-10 13:05:57 +01:00
|
|
|
const publisher = this.participantService.getMyCameraPublisher();
|
|
|
|
await this.openviduService.replaceCameraTrack(publisher, pp);
|
2022-06-24 16:12:36 +02:00
|
|
|
|
2022-11-02 12:26:00 +01:00
|
|
|
if (isBackgroundApplied) {
|
2022-06-16 14:01:07 +02:00
|
|
|
const bgSelected = this.backgroundService.backgrounds.find((b) => b.id === backgroundSelected);
|
|
|
|
if (bgSelected) {
|
|
|
|
await this.backgroundService.applyBackground(bgSelected);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-08 13:04:10 +01:00
|
|
|
this.deviceSrv.setCameraSelected(device.device);
|
2022-06-16 14:01:07 +02:00
|
|
|
this.cameraSelected = this.deviceSrv.getCameraSelected();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-08 13:04:10 +01:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
* Compare two devices to check if they are the same. Used by the mat-select
|
|
|
|
*/
|
|
|
|
compareObjectDevices(o1: CustomDevice, o2: CustomDevice): boolean {
|
|
|
|
return o1.label === o2.label;
|
|
|
|
}
|
|
|
|
|
2022-06-16 14:01:07 +02:00
|
|
|
protected subscribeToParticipantMediaProperties() {
|
|
|
|
this.localParticipantSubscription = this.participantService.localParticipantObs.subscribe((p: ParticipantAbstractModel) => {
|
|
|
|
if (p) {
|
|
|
|
this.isVideoMuted = !p.isCameraVideoActive();
|
2022-11-04 16:24:42 +01:00
|
|
|
this.storageSrv.setVideoMuted(this.isVideoMuted);
|
2022-06-16 14:01:07 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|