From d78774b21841aaeeb88e62ac8448a6123b65b3a7 Mon Sep 17 00:00:00 2001 From: Carlos Santos <4a.santos@gmail.com> Date: Wed, 1 Mar 2023 18:04:17 +0100 Subject: [PATCH] openvidu-components: Refactored participant service Moved some methods to participant model --- .../components/session/session.component.ts | 11 +++--- .../src/lib/models/participant.model.ts | 25 +++++++++++- .../participant/participant.service.ts | 38 +++---------------- 3 files changed, 35 insertions(+), 39 deletions(-) diff --git a/openvidu-components-angular/projects/openvidu-angular/src/lib/components/session/session.component.ts b/openvidu-components-angular/projects/openvidu-angular/src/lib/components/session/session.component.ts index 63eb1407..2524a7f7 100644 --- a/openvidu-components-angular/projects/openvidu-angular/src/lib/components/session/session.component.ts +++ b/openvidu-components-angular/projects/openvidu-angular/src/lib/components/session/session.component.ts @@ -253,12 +253,14 @@ export class SessionComponent implements OnInit, OnDestroy { private async connectToSession(): Promise { try { - const nickname = this.participantService.getMyNickname(); - const participantId = this.participantService.getLocalParticipant().id; + const participant = this.participantService.getLocalParticipant(); + const nickname = participant.getNickname(); + const participantId = participant.id; const screenPublisher = this.participantService.getMyScreenPublisher(); const cameraPublisher = this.participantService.getMyCameraPublisher(); - if (this.participantService.haveICameraAndScreenActive()) { + + if (participant.hasCameraAndScreenActives()) { const webcamSessionId = await this.openviduService.connectWebcamSession(participantId, nickname); if (webcamSessionId) this.participantService.setMyCameraConnectionId(webcamSessionId); @@ -266,10 +268,9 @@ export class SessionComponent implements OnInit, OnDestroy { const screenSessionId = await this.openviduService.connectScreenSession(participantId, nickname); if (screenSessionId) this.participantService.setMyScreenConnectionId(screenSessionId); - await this.openviduService.publishCamera(cameraPublisher); await this.openviduService.publishScreen(screenPublisher); - } else if (this.participantService.isOnlyMyScreenActive()) { + } else if (participant.hasOnlyScreenActive()) { await this.openviduService.connectScreenSession(participantId, nickname); await this.openviduService.publishScreen(screenPublisher); } else { diff --git a/openvidu-components-angular/projects/openvidu-angular/src/lib/models/participant.model.ts b/openvidu-components-angular/projects/openvidu-angular/src/lib/models/participant.model.ts index 360ea8cc..c6f6734c 100644 --- a/openvidu-components-angular/projects/openvidu-angular/src/lib/models/participant.model.ts +++ b/openvidu-components-angular/projects/openvidu-angular/src/lib/models/participant.model.ts @@ -218,7 +218,6 @@ export abstract class ParticipantAbstractModel { */ isLocal(): boolean { return this.local; - // return Array.from(this.streams.values()).every((conn) => conn.local); } /** @@ -307,6 +306,30 @@ export abstract class ParticipantAbstractModel { if (screenConnection) screenConnection.connected = false; } + /** + * @internal + * @returns true if both camera and screen are active + */ + hasCameraAndScreenActives(): boolean { + return this.isCameraActive() && this.isScreenActive(); + } + + /** + * @internal + * @returns true if only screen is active + */ + hasOnlyScreenActive(): boolean { + return this.isScreenActive() && !this.isCameraActive(); + } + + /** + * @internal + * @returns true if only camera is active + */ + hasOnlyCameraActive(): boolean { + return this.isCameraActive() && !this.isScreenActive(); + } + /** * @internal */ diff --git a/openvidu-components-angular/projects/openvidu-angular/src/lib/services/participant/participant.service.ts b/openvidu-components-angular/projects/openvidu-angular/src/lib/services/participant/participant.service.ts index d55a6c93..ca9c8a77 100644 --- a/openvidu-components-angular/projects/openvidu-angular/src/lib/services/participant/participant.service.ts +++ b/openvidu-components-angular/projects/openvidu-angular/src/lib/services/participant/participant.service.ts @@ -76,12 +76,12 @@ export class ParticipantService { const screenPublisher = this.getMyScreenPublisher(); // Disabling webcam - if (this.haveICameraAndScreenActive()) { + if (this.localParticipant.hasCameraAndScreenActives()) { await this.publishVideoAux(cameraPublisher, publish); this.disableWebcamStream(); this.openviduService.unpublishCamera(cameraPublisher); this.publishAudioAux(screenPublisher, publishAudio); - } else if (this.isOnlyMyScreenActive()) { + } else if (this.localParticipant.hasOnlyScreenActive()) { // Enabling webcam const hasAudio = this.hasScreenAudioActive(); const sessionId = await this.openviduService.connectWebcamSession(this.getMyNickname(), this.getLocalParticipant().id); @@ -106,7 +106,7 @@ export class ParticipantService { */ publishAudio(publish: boolean): void { if (this.isMyCameraActive()) { - if (this.isMyScreenActive() && this.hasScreenAudioActive()) { + if (this.localParticipant.isScreenActive() && this.hasScreenAudioActive()) { this.publishAudioAux(this.getMyScreenPublisher(), false); } @@ -129,12 +129,12 @@ export class ParticipantService { const participantNickname = this.getMyNickname(); const participantId = this.getLocalParticipant().id; - if (this.haveICameraAndScreenActive()) { + if (this.localParticipant.hasCameraAndScreenActives()) { // Disabling screenShare this.disableScreenStream(); this.updateLocalParticipant(); this.openviduService.unpublishScreen(screenPublisher); - } else if (this.isOnlyMyCameraActive()) { + } else if (this.localParticipant.hasOnlyCameraActive()) { // I only have the camera published const willWebcamBePresent = this.isMyCameraActive() && this.isMyVideoActive(); const hasAudio = willWebcamBePresent ? false : this.isMyAudioActive(); @@ -336,34 +336,6 @@ export class ParticipantService { return this.localParticipant?.hasAudioActive(); } - /** - * @internal - */ - isMyScreenActive(): boolean { - return this.localParticipant.isScreenActive(); - } - - /** - * @internal - */ - isOnlyMyCameraActive(): boolean { - return this.isMyCameraActive() && !this.isMyScreenActive(); - } - - /** - * @internal - */ - isOnlyMyScreenActive(): boolean { - return this.isMyScreenActive() && !this.isMyCameraActive(); - } - - /** - * @internal - */ - haveICameraAndScreenActive(): boolean { - return this.isMyCameraActive() && this.isMyScreenActive(); - } - /** * @internal */