openvidu-components: Refactored participants and openvidu service

- Migrated publishAudio method from OpenViduService to ParticipantService
pull/809/head
Carlos Santos 2023-02-27 16:41:53 +01:00
parent 1f6f151ce2
commit fd0d23563e
7 changed files with 48 additions and 34 deletions

View File

@ -194,11 +194,11 @@ export class SessionComponent implements OnInit, OnDestroy {
this.cd.markForCheck(); this.cd.markForCheck();
} }
ngOnDestroy() { async ngOnDestroy() {
// Reconnecting session is received in Firefox // Reconnecting session is received in Firefox
// To avoid 'Connection lost' message uses session.off() // To avoid 'Connection lost' message uses session.off()
this.session?.off('reconnecting'); this.session?.off('reconnecting');
this.participantService.clear(); await this.participantService.clear();
this.session = null; this.session = null;
this.sessionScreen = null; this.sessionScreen = null;
if (this.menuSubscription) this.menuSubscription.unsubscribe(); if (this.menuSubscription) this.menuSubscription.unsubscribe();

View File

@ -58,7 +58,7 @@ export class AudioDevicesComponent implements OnInit, OnDestroy {
toggleMic() { toggleMic() {
const publish = this.isAudioMuted; const publish = this.isAudioMuted;
this.openviduService.publishAudio(publish); this.participantService.publishAudio(publish);
this.onAudioMutedClicked.emit(publish); this.onAudioMutedClicked.emit(publish);
} }

View File

@ -500,7 +500,7 @@ export class ToolbarComponent implements OnInit, OnDestroy, AfterViewInit {
async toggleMicrophone() { async toggleMicrophone() {
this.onMicrophoneButtonClicked.emit(); this.onMicrophoneButtonClicked.emit();
try { try {
await this.openviduService.publishAudio(!this.isAudioActive); this.participantService.publishAudio(!this.isAudioActive);
} catch (error) { } catch (error) {
this.log.e('There was an error toggling microphone:', error.code, error.message); this.log.e('There was an error toggling microphone:', error.code, error.message);
this.actionService.openDialog( this.actionService.openDialog(

View File

@ -601,7 +601,14 @@ export class VideoconferenceComponent implements OnInit, OnDestroy, AfterViewIni
await this.handlePublisherError(e); await this.handlePublisherError(e);
resolve(); resolve();
}); });
publisher.once('accessAllowed', () => resolve()); publisher.once('accessAllowed', () => {
this.participantService.setMyCameraPublisher(publisher);
this.participantService.updateLocalParticipant();
resolve();
});
} else {
this.participantService.setMyCameraPublisher(undefined);
this.participantService.updateLocalParticipant();
} }
} catch (error) { } catch (error) {
this.actionService.openDialog(error.name.replace(/_/g, ' '), error.message, true); this.actionService.openDialog(error.name.replace(/_/g, ' '), error.message, true);

View File

@ -238,7 +238,7 @@ export abstract class ParticipantAbstractModel {
/** /**
* @internal * @internal
*/ */
setCameraPublisher(publisher: Publisher) { setCameraPublisher(publisher: Publisher | undefined) {
const cameraConnection = this.getCameraConnection(); const cameraConnection = this.getCameraConnection();
if (cameraConnection) cameraConnection.streamManager = publisher; if (cameraConnection) cameraConnection.streamManager = publisher;
} }

View File

@ -140,8 +140,8 @@ export class OpenViduService {
async clear() { async clear() {
this.videoSource = undefined; this.videoSource = undefined;
this.audioSource = undefined; this.audioSource = undefined;
await this.participantService.getMyCameraPublisher()?.stream?.disposeMediaStream(); // await this.participantService.getMyCameraPublisher()?.stream?.disposeMediaStream();
await this.participantService.getMyScreenPublisher()?.stream?.disposeMediaStream(); // await this.participantService.getMyScreenPublisher()?.stream?.disposeMediaStream();
} }
/** /**
@ -273,12 +273,7 @@ export class OpenViduService {
mirror mirror
}; };
if (hasVideoDevices || hasAudioDevices) { if (hasVideoDevices || hasAudioDevices) {
const publisher = await this.initPublisher(properties); return this.initPublisher(properties);
this.participantService.setMyCameraPublisher(publisher);
this.participantService.updateLocalParticipant();
return publisher;
} else {
this.participantService.setMyCameraPublisher(null);
} }
} }
@ -372,25 +367,11 @@ export class OpenViduService {
} }
} }
/**
* Publish or unpublish the audio stream (if available).
* See openvidu-browser {@link https://docs.openvidu.io/en/stable/api/openvidu-browser/classes/Publisher.html#publishAudio publishAudio}.
*/
async publishAudio(publish: boolean): Promise<void> {
if (this.participantService.isMyCameraActive()) {
if (this.participantService.isMyScreenActive() && this.participantService.hasScreenAudioActive()) {
this.publishAudioAux(this.participantService.getMyScreenPublisher(), false);
}
this.publishAudioAux(this.participantService.getMyCameraPublisher(), publish);
} else {
this.publishAudioAux(this.participantService.getMyScreenPublisher(), publish);
}
}
/** /**
* Share or unshare the screen. * Share or unshare the screen.
* Hide the camera muted stream when screen is sharing. * Hide the camera muted stream when screen is sharing.
*
* TODO: This method should be in participant service
*/ */
async toggleScreenshare() { async toggleScreenshare() {
if (this.participantService.haveICameraAndScreenActive()) { if (this.participantService.haveICameraAndScreenActive()) {
@ -459,6 +440,7 @@ export class OpenViduService {
/** /**
* @internal * @internal
* TODO: Remove when it is in participant service
*/ */
private publishAudioAux(publisher: Publisher, value: boolean): void { private publishAudioAux(publisher: Publisher, value: boolean): void {
if (!!publisher) { if (!!publisher) {

View File

@ -41,7 +41,6 @@ export class ParticipantService {
*/ */
constructor(protected openviduAngularConfigSrv: OpenViduAngularConfigService, protected loggerSrv: LoggerService) { constructor(protected openviduAngularConfigSrv: OpenViduAngularConfigService, protected loggerSrv: LoggerService) {
this.log = this.loggerSrv.get('ParticipantService'); this.log = this.loggerSrv.get('ParticipantService');
this.localParticipantObs = this._localParticipant.asObservable(); this.localParticipantObs = this._localParticipant.asObservable();
this.remoteParticipantsObs = this._remoteParticipants.asObservable(); this.remoteParticipantsObs = this._remoteParticipants.asObservable();
} }
@ -58,6 +57,24 @@ export class ParticipantService {
return this.localParticipant; return this.localParticipant;
} }
/**
* Publish or unpublish the audio stream (if available).
* See openvidu-browser {@link https://docs.openvidu.io/en/stable/api/openvidu-browser/classes/Publisher.html#publishAudio publishAudio}.
*
*/
publishAudio(publish: boolean): void {
if (this.isMyCameraActive()) {
if (this.isMyScreenActive() && this.hasScreenAudioActive()) {
this.publishAudioAux(this.getMyScreenPublisher(), false);
}
this.publishAudioAux(this.getMyCameraPublisher(), publish);
} else {
this.publishAudioAux(this.getMyScreenPublisher(), publish);
}
this.updateLocalParticipant();
}
/** /**
* @internal * @internal
*/ */
@ -68,7 +85,7 @@ export class ParticipantService {
/** /**
* @internal * @internal
*/ */
setMyCameraPublisher(publisher: Publisher) { setMyCameraPublisher(publisher: Publisher | undefined) {
this.localParticipant.setCameraPublisher(publisher); this.localParticipant.setCameraPublisher(publisher);
} }
/** /**
@ -186,11 +203,13 @@ export class ParticipantService {
/** /**
* @internal * @internal
*/ */
clear() { async clear() {
await this.getMyCameraPublisher()?.stream?.disposeMediaStream();
await this.getMyScreenPublisher()?.stream?.disposeMediaStream();
this.disableScreenStream(); this.disableScreenStream();
this.remoteParticipants = []; this.remoteParticipants = [];
this.updateRemoteParticipants(); this.updateRemoteParticipants();
this.updateLocalParticipant(); // this.updateLocalParticipant();
} }
/** /**
@ -252,6 +271,12 @@ export class ParticipantService {
); );
} }
private publishAudioAux(publisher: Publisher, value: boolean): void {
if (!!publisher) {
publisher.publishAudio(value);
}
}
/** /**
* REMOTE USERS * REMOTE USERS
*/ */