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';
|
2022-10-14 13:37:51 +02:00
|
|
|
import { Subscription } from 'rxjs';
|
2022-06-16 14:01:07 +02:00
|
|
|
import { CustomDevice } from '../../../models/device.model';
|
|
|
|
import { ParticipantAbstractModel } from '../../../models/participant.model';
|
2022-10-14 13:37:51 +02:00
|
|
|
import { DeviceService } from '../../../services/device/device.service';
|
|
|
|
import { OpenViduService } from '../../../services/openvidu/openvidu.service';
|
|
|
|
import { ParticipantService } from '../../../services/participant/participant.service';
|
|
|
|
import { StorageService } from '../../../services/storage/storage.service';
|
2022-06-16 14:01:07 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
@Component({
|
|
|
|
selector: 'ov-audio-devices-select',
|
|
|
|
templateUrl: './audio-devices.component.html',
|
|
|
|
styleUrls: ['./audio-devices.component.css']
|
|
|
|
})
|
|
|
|
export class AudioDevicesComponent implements OnInit, OnDestroy {
|
2022-07-11 13:05:28 +02:00
|
|
|
@Output() onDeviceSelectorClicked = new EventEmitter<void>();
|
2022-10-14 13:37:51 +02:00
|
|
|
@Output() onAudioMutedClicked = new EventEmitter<boolean>();
|
2022-06-16 14:01:07 +02:00
|
|
|
hasAudioDevices: boolean;
|
|
|
|
isAudioMuted: boolean;
|
2022-10-27 13:32:09 +02:00
|
|
|
microphoneSelected: CustomDevice | null;
|
2022-06-16 14:01:07 +02:00
|
|
|
microphones: CustomDevice[] = [];
|
|
|
|
private localParticipantSubscription: Subscription;
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
private openviduService: OpenViduService,
|
|
|
|
private deviceSrv: DeviceService,
|
|
|
|
private storageSrv: StorageService,
|
2022-07-11 13:05:28 +02:00
|
|
|
private participantService: ParticipantService
|
2022-06-16 14:01:07 +02:00
|
|
|
) {}
|
|
|
|
|
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-06-16 14:01:07 +02:00
|
|
|
this.hasAudioDevices = this.deviceSrv.hasAudioDeviceAvailable();
|
2022-10-27 13:32:09 +02:00
|
|
|
if(this.hasAudioDevices) {
|
|
|
|
this.microphones = this.deviceSrv.getMicrophones();
|
|
|
|
this.microphoneSelected = this.deviceSrv.getMicrophoneSelected();
|
|
|
|
}
|
2022-06-16 14:01:07 +02:00
|
|
|
this.isAudioMuted = this.deviceSrv.isAudioMuted();
|
|
|
|
if (this.openviduService.isSessionConnected()) {
|
|
|
|
this.isAudioMuted = !this.participantService.isMyAudioActive();
|
|
|
|
} else {
|
|
|
|
this.isAudioMuted = this.deviceSrv.isAudioMuted();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ngOnDestroy() {
|
|
|
|
if (this.localParticipantSubscription) this.localParticipantSubscription.unsubscribe();
|
|
|
|
}
|
|
|
|
|
|
|
|
toggleMic() {
|
|
|
|
const publish = this.isAudioMuted;
|
2023-02-27 16:41:53 +01:00
|
|
|
this.participantService.publishAudio(publish);
|
2022-10-14 13:37:51 +02:00
|
|
|
this.onAudioMutedClicked.emit(publish);
|
2022-06-16 14:01:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async onMicrophoneSelected(event: any) {
|
|
|
|
const audioSource = event?.value;
|
|
|
|
if (this.deviceSrv.needUpdateAudioTrack(audioSource)) {
|
2022-06-24 16:12:36 +02:00
|
|
|
const pp: PublisherProperties = { audioSource, videoSource: false };
|
2023-03-10 13:05:57 +01:00
|
|
|
const publisher = this.participantService.getMyCameraPublisher();
|
|
|
|
await this.openviduService.replaceCameraTrack(publisher, pp);
|
2022-06-16 14:01:07 +02:00
|
|
|
this.deviceSrv.setMicSelected(audioSource);
|
|
|
|
this.microphoneSelected = this.deviceSrv.getMicrophoneSelected();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private subscribeToParticipantMediaProperties() {
|
|
|
|
this.localParticipantSubscription = this.participantService.localParticipantObs.subscribe((p: ParticipantAbstractModel) => {
|
|
|
|
if (p) {
|
|
|
|
this.isAudioMuted = !p.hasAudioActive();
|
|
|
|
this.storageSrv.setAudioMuted(this.isAudioMuted);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|