openvidu-components: Refactored names and particicipant observables

pull/707/head
csantosm 2022-02-25 11:19:21 +01:00
parent de4812754b
commit 3318dfd207
8 changed files with 55 additions and 93 deletions

View File

@ -164,12 +164,12 @@ export class SessionComponent implements OnInit {
private async connectToSession(): Promise<void> { private async connectToSession(): Promise<void> {
try { try {
if (this.participantService.areBothEnabled()) { if (this.participantService.haveICameraAndScreenActive()) {
await this.openviduService.connectSession(this.openviduService.getWebcamSession(), this.tokenService.getWebcamToken()); await this.openviduService.connectSession(this.openviduService.getWebcamSession(), this.tokenService.getWebcamToken());
await this.openviduService.connectSession(this.openviduService.getScreenSession(), this.tokenService.getScreenToken()); await this.openviduService.connectSession(this.openviduService.getScreenSession(), this.tokenService.getScreenToken());
await this.openviduService.publish(this.participantService.getMyCameraPublisher()); await this.openviduService.publish(this.participantService.getMyCameraPublisher());
await this.openviduService.publish(this.participantService.getMyScreenPublisher()); await this.openviduService.publish(this.participantService.getMyScreenPublisher());
} else if (this.participantService.isOnlyMyScreenEnabled()) { } else if (this.participantService.isOnlyMyScreenActive()) {
await this.openviduService.connectSession(this.openviduService.getScreenSession(), this.tokenService.getScreenToken()); await this.openviduService.connectSession(this.openviduService.getScreenSession(), this.tokenService.getScreenToken());
await this.openviduService.publish(this.participantService.getMyScreenPublisher()); await this.openviduService.publish(this.participantService.getMyScreenPublisher());
} else { } else {

View File

@ -137,7 +137,7 @@ export class StreamComponent implements OnInit {
const properties: PublisherProperties = { const properties: PublisherProperties = {
videoSource: ScreenType.SCREEN, videoSource: ScreenType.SCREEN,
publishVideo: true, publishVideo: true,
publishAudio: !this.participantService.isMyCameraEnabled(), publishAudio: !this.participantService.isMyCameraActive(),
mirror: false mirror: false
}; };
await this.openviduService.replaceTrack(VideoType.SCREEN, properties); await this.openviduService.replaceTrack(VideoType.SCREEN, properties);

View File

@ -59,10 +59,7 @@ export class ToolbarComponent implements OnInit, OnDestroy {
protected log: ILogger; protected log: ILogger;
protected menuTogglingSubscription: Subscription; protected menuTogglingSubscription: Subscription;
protected chatMessagesSubscription: Subscription; protected chatMessagesSubscription: Subscription;
protected screenShareStateSubscription: Subscription; protected localParticipantSubscription: Subscription;
protected webcamVideoStateSubscription: Subscription;
protected webcamAudioStateSubscription: Subscription;
private currentWindowHeight = window.innerHeight; private currentWindowHeight = window.innerHeight;
constructor( constructor(
@ -87,14 +84,8 @@ export class ToolbarComponent implements OnInit, OnDestroy {
if (this.chatMessagesSubscription) { if (this.chatMessagesSubscription) {
this.chatMessagesSubscription.unsubscribe(); this.chatMessagesSubscription.unsubscribe();
} }
if (this.screenShareStateSubscription) { if (this.localParticipantSubscription) {
this.screenShareStateSubscription.unsubscribe(); this.localParticipantSubscription.unsubscribe();
}
if (this.webcamVideoStateSubscription) {
this.webcamVideoStateSubscription.unsubscribe();
}
if (this.webcamAudioStateSubscription) {
this.webcamAudioStateSubscription.unsubscribe();
} }
} }
@ -132,7 +123,7 @@ export class ToolbarComponent implements OnInit, OnDestroy {
toggleMicrophone() { toggleMicrophone() {
this.onMicClicked.emit(); this.onMicClicked.emit();
if (this.participantService.isMyCameraEnabled()) { if (this.participantService.isMyCameraActive()) {
this.openviduService.publishAudio( this.openviduService.publishAudio(
this.participantService.getMyCameraPublisher(), this.participantService.getMyCameraPublisher(),
!this.participantService.hasCameraAudioActive() !this.participantService.hasCameraAudioActive()
@ -149,7 +140,7 @@ export class ToolbarComponent implements OnInit, OnDestroy {
const publishVideo = !this.participantService.hasCameraVideoActive(); const publishVideo = !this.participantService.hasCameraVideoActive();
const publishAudio = this.participantService.hasCameraAudioActive(); const publishAudio = this.participantService.hasCameraAudioActive();
// Disabling webcam // Disabling webcam
if (this.participantService.areBothEnabled()) { if (this.participantService.haveICameraAndScreenActive()) {
this.openviduService.publishVideo(this.participantService.getMyCameraPublisher(), publishVideo); this.openviduService.publishVideo(this.participantService.getMyCameraPublisher(), publishVideo);
this.participantService.disableWebcamUser(); this.participantService.disableWebcamUser();
this.openviduService.unpublish(this.participantService.getMyCameraPublisher()); this.openviduService.unpublish(this.participantService.getMyCameraPublisher());
@ -157,7 +148,7 @@ export class ToolbarComponent implements OnInit, OnDestroy {
return; return;
} }
// Enabling webcam // Enabling webcam
if (this.participantService.isOnlyMyScreenEnabled()) { if (this.participantService.isOnlyMyScreenActive()) {
const hasAudio = this.participantService.hasScreenAudioActive(); const hasAudio = this.participantService.hasScreenAudioActive();
if (!this.openviduService.isWebcamSessionConnected()) { if (!this.openviduService.isWebcamSessionConnected()) {
@ -181,15 +172,15 @@ export class ToolbarComponent implements OnInit, OnDestroy {
try { try {
// Disabling screenShare // Disabling screenShare
if (this.participantService.areBothEnabled()) { if (this.participantService.haveICameraAndScreenActive()) {
this.participantService.disableScreenUser(); this.participantService.disableScreenUser();
this.openviduService.unpublish(this.participantService.getMyScreenPublisher()); this.openviduService.unpublish(this.participantService.getMyScreenPublisher());
return; return;
} }
// Enabling screenShare // Enabling screenShare
if (this.participantService.isOnlyMyCameraEnabled()) { if (this.participantService.isOnlyMyCameraActive()) {
const willThereBeWebcam = this.participantService.isMyCameraEnabled() && this.participantService.hasCameraVideoActive(); const willThereBeWebcam = this.participantService.isMyCameraActive() && this.participantService.hasCameraVideoActive();
const hasAudio = willThereBeWebcam ? false : this.hasAudioDevices && this.participantService.hasCameraAudioActive(); const hasAudio = willThereBeWebcam ? false : this.hasAudioDevices && this.participantService.hasCameraAudioActive();
const properties: PublisherProperties = { const properties: PublisherProperties = {
videoSource: ScreenType.SCREEN, videoSource: ScreenType.SCREEN,
@ -300,17 +291,10 @@ export class ToolbarComponent implements OnInit, OnDestroy {
}); });
} }
protected subscribeToUserMediaProperties() { protected subscribeToUserMediaProperties() {
this.screenShareStateSubscription = this.participantService.screenShareState.subscribe((active) => { this.localParticipantSubscription = this.participantService.localParticipantObs.subscribe((p) => {
this.isScreenShareActive = active; this.isWebcamVideoActive = p.isCameraVideoActive();
this.cd.markForCheck(); this.isWebcamAudioActive = p.isCameraAudioActive();
}); this.isScreenShareActive = p.isScreenActive();
this.webcamVideoStateSubscription = this.participantService.webcamVideoActive.subscribe((active) => {
this.isWebcamVideoActive = active;
this.cd.markForCheck();
});
this.webcamAudioStateSubscription = this.participantService.webcamAudioActive.subscribe((active) => {
this.isWebcamAudioActive = active;
this.cd.markForCheck(); this.cd.markForCheck();
}); });
} }

View File

@ -48,7 +48,7 @@ export class UserSettingsComponent implements OnInit, OnDestroy {
hasAudioDevices: boolean; hasAudioDevices: boolean;
isLoading = true; isLoading = true;
private log: ILogger; private log: ILogger;
private oVUsersSubscription: Subscription; private localParticipantSubscription: Subscription;
private screenShareStateSubscription: Subscription; private screenShareStateSubscription: Subscription;
constructor( constructor(
@ -83,8 +83,8 @@ export class UserSettingsComponent implements OnInit, OnDestroy {
} }
ngOnDestroy() { ngOnDestroy() {
if (this.oVUsersSubscription) { if (this.localParticipantSubscription) {
this.oVUsersSubscription.unsubscribe(); this.localParticipantSubscription.unsubscribe();
} }
if (this.screenShareStateSubscription) { if (this.screenShareStateSubscription) {
@ -132,11 +132,11 @@ export class UserSettingsComponent implements OnInit, OnDestroy {
const publish = this.isVideoMuted; const publish = this.isVideoMuted;
this.openviduService.publishVideo(this.participantService.getMyCameraPublisher(), publish); this.openviduService.publishVideo(this.participantService.getMyCameraPublisher(), publish);
if (this.participantService.areBothEnabled()) { if (this.participantService.haveICameraAndScreenActive()) {
// Cam will not published, disable webcam with screensharing active // Cam will not published, disable webcam with screensharing active
this.participantService.disableWebcamUser(); this.participantService.disableWebcamUser();
this.openviduService.publishAudio(this.participantService.getMyScreenPublisher(), publish); this.openviduService.publishAudio(this.participantService.getMyScreenPublisher(), publish);
} else if (this.participantService.isOnlyMyScreenEnabled()) { } else if (this.participantService.isOnlyMyScreenActive()) {
// Cam will be published, enable webcam // Cam will be published, enable webcam
this.participantService.enableWebcamUser(); this.participantService.enableWebcamUser();
} }
@ -147,14 +147,14 @@ export class UserSettingsComponent implements OnInit, OnDestroy {
async toggleScreenShare() { async toggleScreenShare() {
// Disabling screenShare // Disabling screenShare
if (this.participantService.areBothEnabled()) { if (this.participantService.haveICameraAndScreenActive()) {
this.participantService.disableScreenUser(); this.participantService.disableScreenUser();
return; return;
} }
// Enabling screenShare // Enabling screenShare
if (this.participantService.isOnlyMyCameraEnabled()) { if (this.participantService.isOnlyMyCameraActive()) {
const willThereBeWebcam = this.participantService.isMyCameraEnabled() && this.participantService.hasCameraVideoActive(); const willThereBeWebcam = this.participantService.isMyCameraActive() && this.participantService.hasCameraVideoActive();
const hasAudio = willThereBeWebcam ? false : this.hasAudioDevices && this.isAudioMuted; const hasAudio = willThereBeWebcam ? false : this.hasAudioDevices && this.isAudioMuted;
const properties: PublisherProperties = { const properties: PublisherProperties = {
videoSource: ScreenType.SCREEN, videoSource: ScreenType.SCREEN,
@ -242,11 +242,9 @@ export class UserSettingsComponent implements OnInit, OnDestroy {
} }
private subscribeToLocalParticipantEvents() { private subscribeToLocalParticipantEvents() {
this.oVUsersSubscription = this.participantService.localParticipantObs.subscribe((p) => { this.localParticipantSubscription = this.participantService.localParticipantObs.subscribe((p) => {
this.localParticipant = p; this.localParticipant = p;
}); this.screenShareEnabled = p.isScreenActive();
this.screenShareStateSubscription = this.participantService.screenShareState.subscribe((enabled) => {
this.screenShareEnabled = enabled;
}); });
} }

View File

@ -37,7 +37,10 @@ export abstract class ParticipantAbstractModel {
public isCameraAudioActive(): boolean { public isCameraAudioActive(): boolean {
const cameraConnection = this.getCameraConnection(); const cameraConnection = this.getCameraConnection();
return cameraConnection.connected && cameraConnection.streamManager.stream.audioActive; if(cameraConnection) {
return cameraConnection.connected && cameraConnection.streamManager.stream.audioActive;
}
return this.isScreenAudioActive();;
} }
public isCameraVideoActive(): boolean { public isCameraVideoActive(): boolean {
@ -46,7 +49,10 @@ export abstract class ParticipantAbstractModel {
} }
isScreenAudioActive(): boolean { isScreenAudioActive(): boolean {
const screenConnection = this.getScreenConnection(); const screenConnection = this.getScreenConnection();
return screenConnection?.connected && screenConnection?.streamManager?.stream?.audioActive; if(screenConnection){
return screenConnection?.connected && screenConnection?.streamManager?.stream?.audioActive;
}
return false;
} }
hasConnectionType(type: VideoType): boolean { hasConnectionType(type: VideoType): boolean {
@ -61,10 +67,10 @@ export abstract class ParticipantAbstractModel {
return this.streams.get(VideoType.SCREEN); return this.streams.get(VideoType.SCREEN);
} }
getConnectionTypesEnabled(): VideoType[] { getConnectionTypesActive(): VideoType[] {
let connType = []; let connType = [];
if (this.isCameraEnabled()) connType.push(VideoType.CAMERA); if (this.isCameraActive()) connType.push(VideoType.CAMERA);
if (this.isScreenEnabled()) connType.push(VideoType.SCREEN); if (this.isScreenActive()) connType.push(VideoType.SCREEN);
return connType; return connType;
} }
@ -131,7 +137,7 @@ export abstract class ParticipantAbstractModel {
} }
} }
isCameraEnabled(): boolean { isCameraActive(): boolean {
return this.getCameraConnection()?.connected; return this.getCameraConnection()?.connected;
} }
@ -145,11 +151,11 @@ export abstract class ParticipantAbstractModel {
if (cameraConnection) cameraConnection.connected = false; if (cameraConnection) cameraConnection.connected = false;
} }
isScreenEnabled(): boolean { isScreenActive(): boolean {
return this.getScreenConnection()?.connected; return this.getScreenConnection()?.connected;
} }
enablescreen() { enableScreen() {
const screenConnection = this.getScreenConnection(); const screenConnection = this.getScreenConnection();
if (screenConnection) screenConnection.connected = true; if (screenConnection) screenConnection.connected = true;
} }

View File

@ -23,7 +23,7 @@ export class StreamsEnabledPipe implements PipeTransform {
constructor() {} constructor() {}
transform(participant: ParticipantAbstractModel): string { transform(participant: ParticipantAbstractModel): string {
return `(${participant?.getConnectionTypesEnabled().toString().replace(',', ', ')})`; return `(${participant?.getConnectionTypesActive().toString().replace(',', ', ')})`;
} }
} }

View File

@ -31,8 +31,6 @@ export class OpenViduService {
protected screenSession: Session = null; protected screenSession: Session = null;
protected videoSource = undefined; protected videoSource = undefined;
protected audioSource = undefined; protected audioSource = undefined;
// protected screenMediaStream: MediaStream = null;
// protected webcamMediaStream: MediaStream = null;
protected log: ILogger; protected log: ILogger;
constructor( constructor(
@ -153,7 +151,7 @@ export class OpenViduService {
if (hasVideoDevices || hasAudioDevices) { if (hasVideoDevices || hasAudioDevices) {
const publisher = await this.initPublisher(targetElement, properties); const publisher = await this.initPublisher(targetElement, properties);
this.participantService.setMyCameraPublisher(publisher); this.participantService.setMyCameraPublisher(publisher);
this.participantService.updateParticipantMediaStatus(); this.participantService.updateLocalParticipant();
return publisher; return publisher;
} else { } else {
this.participantService.setMyCameraPublisher(null); this.participantService.setMyCameraPublisher(null);
@ -211,16 +209,14 @@ export class OpenViduService {
publishVideo(publisher: Publisher, value: boolean): void { publishVideo(publisher: Publisher, value: boolean): void {
if (!!publisher) { if (!!publisher) {
publisher.publishVideo(value); publisher.publishVideo(value);
// Send event to subscribers because of video has changed and view must update this.participantService.updateLocalParticipant();
this.participantService.updateParticipantMediaStatus();
} }
} }
publishAudio(publisher: Publisher, value: boolean): void { publishAudio(publisher: Publisher, value: boolean): void {
if (!!publisher) { if (!!publisher) {
publisher.publishAudio(value); publisher.publishAudio(value);
// Send event to subscribers because of video has changed and view must update this.participantService.updateLocalParticipant();
this.participantService.updateParticipantMediaStatus();
} }
} }

View File

@ -13,13 +13,7 @@ import { LoggerService } from '../logger/logger.service';
export class ParticipantService { export class ParticipantService {
//Local participants observables //Local participants observables
localParticipantObs: Observable<ParticipantAbstractModel>; localParticipantObs: Observable<ParticipantAbstractModel>;
screenShareState: Observable<boolean>;
webcamVideoActive: Observable<boolean>;
webcamAudioActive: Observable<boolean>;
protected _localParticipant = <BehaviorSubject<ParticipantAbstractModel>>new BehaviorSubject(null); protected _localParticipant = <BehaviorSubject<ParticipantAbstractModel>>new BehaviorSubject(null);
protected _screensharing = <BehaviorSubject<boolean>>new BehaviorSubject(false);
protected _cameraVideoActive = <BehaviorSubject<boolean>>new BehaviorSubject(true);
protected _cameraAudioActive = <BehaviorSubject<boolean>>new BehaviorSubject(true);
//Remote participants observable //Remote participants observable
remoteParticipantsObs: Observable<ParticipantAbstractModel[]>; remoteParticipantsObs: Observable<ParticipantAbstractModel[]>;
@ -35,9 +29,6 @@ export class ParticipantService {
this.localParticipantObs = this._localParticipant.asObservable(); this.localParticipantObs = this._localParticipant.asObservable();
this.remoteParticipantsObs = this._remoteParticipants.asObservable(); this.remoteParticipantsObs = this._remoteParticipants.asObservable();
this.screenShareState = this._screensharing.asObservable();
this.webcamVideoActive = this._cameraVideoActive.asObservable();
this.webcamAudioActive = this._cameraAudioActive.asObservable();
this.localParticipant = this.newParticipant(); this.localParticipant = this.newParticipant();
this.updateLocalParticipant(); this.updateLocalParticipant();
@ -101,25 +92,12 @@ export class ParticipantService {
}; };
this.localParticipant.addConnection(steramModel); this.localParticipant.addConnection(steramModel);
this._screensharing.next(true);
this.updateLocalParticipant(); this.updateLocalParticipant();
} }
disableScreenUser() { disableScreenUser() {
this.localParticipant.disableScreen(); this.localParticipant.disableScreen();
this.updateLocalParticipant(); this.updateLocalParticipant();
this._screensharing.next(false);
}
updateParticipantMediaStatus() {
this._cameraVideoActive.next(this.localParticipant.isCameraVideoActive());
if (this.isMyCameraEnabled()) {
this._cameraAudioActive.next(this.localParticipant.isCameraAudioActive());
} else {
this._cameraAudioActive.next(this.hasScreenAudioActive());
}
} }
setNickname(connectionId: string, nickname: string) { setNickname(connectionId: string, nickname: string) {
@ -154,31 +132,31 @@ export class ParticipantService {
clear() { clear() {
this.disableScreenUser(); this.disableScreenUser();
this.localParticipant = this.newParticipant(); this.localParticipant = this.newParticipant();
this._screensharing.next(false); // this._screensharing.next(false);
this.remoteParticipants = []; this.remoteParticipants = [];
this._remoteParticipants = <BehaviorSubject<ParticipantAbstractModel[]>>new BehaviorSubject([]); this._remoteParticipants = <BehaviorSubject<ParticipantAbstractModel[]>>new BehaviorSubject([]);
this.remoteParticipantsObs = this._remoteParticipants.asObservable(); this.remoteParticipantsObs = this._remoteParticipants.asObservable();
this.updateLocalParticipant(); this.updateLocalParticipant();
} }
isMyCameraEnabled(): boolean { isMyCameraActive(): boolean {
return this.localParticipant.isCameraEnabled(); return this.localParticipant.isCameraActive();
} }
isMyScreenEnabled(): boolean { isMyScreenActive(): boolean {
return this.localParticipant.isScreenEnabled(); return this.localParticipant.isScreenActive();
} }
isOnlyMyCameraEnabled(): boolean { isOnlyMyCameraActive(): boolean {
return this.isMyCameraEnabled() && !this.isMyScreenEnabled(); return this.isMyCameraActive() && !this.isMyScreenActive();
} }
isOnlyMyScreenEnabled(): boolean { isOnlyMyScreenActive(): boolean {
return this.isMyScreenEnabled() && !this.isMyCameraEnabled(); return this.isMyScreenActive() && !this.isMyCameraActive();
} }
areBothEnabled(): boolean { haveICameraAndScreenActive(): boolean {
return this.isMyCameraEnabled() && this.isMyScreenEnabled(); return this.isMyCameraActive() && this.isMyScreenActive();
} }
hasCameraVideoActive(): boolean { hasCameraVideoActive(): boolean {