All files / src/lib/services/local-user local-user.service.ts

40% Statements 28/70
13.88% Branches 10/72
11.53% Functions 3/26
35.93% Lines 23/64

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 1571x 1x 1x 1x 1x           1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x                                                                                                                                                                                                                 1x     1x                                          
import { Injectable } from '@angular/core';
import { Publisher } from 'openvidu-browser';
impoIrt { BehaviorSubject, Observable } from 'rxjs';
import { ILogger } from '../../models/logger.model';
import { UserModel } from '../../models/user.model';
import { LoggerService } from '../logger/logger.service';
 
@Injectable({
	providedIn: 'root'
})
export class LocalUserService {
	OVUsers: Observable<UserModel[]>;
	screenShareState: Observable<boolean>;
	webcamVideoActive: Observable<boolean>;
	webcamAudioActive: Observable<boolean>;
	private _OVUsers = <BehaviorSubject<UserModel[]>>new BehaviorSubject([]);
	private _screenShareState = <BehaviorSubject<boolean>>new BehaviorSubject(false);
	private _webcamVideoActive = <BehaviorSubject<boolean>>new BehaviorSubject(true);
	private _webcamAudioActive = <BehaviorSubject<boolean>>new BehaviorSubject(true);
 
	private webcamUser: UserModel = null;
	private screenUser: UserModel = null;
	private log: ILogger;
 
	constructor(private loggerSrv: LoggerService) {
		this.log = this.loggerSrv.get('LocalUserService');
		this.OVUsers = this._OVUsers.asObservable();
		this.screenShareState = this._screenShareState.asObservable();
		this.webcamVideoActive = this._webcamVideoActive.asObservable();
		this.webcamAudioActive = this._webcamAudioActive.asObservable();
		this.webcamUser = new UserModel();
		// Used when the streamManager is null (users without devices)
		this.webcamUser.setLocal(true);
		this._OVUsers.next([this.webcamUser]);
	}
 
	getWebcamPublisher(): Publisher {
		return <Publisher>this.webcamUser?.getStreamManager();
	}
 
	setWebcamPublisher(publisher: Publisher) {
		this.webcamUser.setStreamManager(publisher);
	}

	getScreenPublisher(): Publisher {
		return <Publisher>this.screenUser?.getStreamManager();
	}
 
	setScreenPublisher(publisher: Publisher) {
		this.screenUser.setStreamManager(publisher);
	}
 
	enableWebcamUser() {
		this._OVUsers.next([this.webcamUser, this.screenUser]);
	}

	disableWebcamUser() {
		// this.destryowebcamUser();
		this._OVUsers.next([this.screenUser]);
	}

	enableScreenUser(screenPublisher: Publisher) {
		this.log.d('Enabling screen publisher');
 
		const connectionId = screenPublisher?.session?.connection?.connectionId;

		this.screenUser = new UserModel(connectionId, screenPublisher, this.getScreenUserName());
		this._screenShareState.next(true);

		if (this.isWebCamEnabled()) {
			this._OVUsers.next([this.webcamUser, this.screenUser]);
			return;
		}
		this._OVUsers.next([this.screenUser]);
	}
 
	disableScreenUser() {
		// this.destryoScreenUser();
		this._OVUsers.next([this.webcamUser]);
		this._screenShareState.next(false);
	}
 
	updateUsersStatus() {
		this._webcamVideoActive.next(this.webcamUser.isVideoActive());
		if (this.isWebCamEnabled()) {
			this._webcamAudioActive.next(this.webcamUser.isAudioActive());
		} else {
			this._webcamAudioActive.next(this.hasScreenAudioActive());
		}
	}
 
	clear() {
		this.screenUser = null;
		this.webcamUser = new UserModel();
		// this._OVUsers.next([this.webcamUser]);
		this.disableScreenUser();
	}

	isWebCamEnabled(): boolean {
		return this._OVUsers.getValue()[0].isCamera();
	}
 
	isOnlyScreenConnected(): boolean {
		return this._OVUsers.getValue()[0].isScreen();
	}

	hasWebcamVideoActive(): boolean {
		return this.webcamUser.isVideoActive();
	}
 
	hasWebcamAudioActive(): boolean {
		return this.webcamUser?.isAudioActive();
	}

	hasScreenAudioActive(): boolean {
		return this.screenUser?.isAudioActive();
	}

	areBothConnected(): boolean {
		return this._OVUsers.getValue().length === 2;
	}

	isOnlyWebcamConnected(): boolean {
		return this.isWebCamEnabled() && !this.areBothConnected();
	}

	isScreenShareEnabled(): boolean {
		return this.areBothConnected() || this.isOnlyScreenConnected();
	}

	updateUsersNickname(nickname: string) {
		this.webcamUser.setNickname(nickname);
		this.screenUser?.setNickname(this.getScreenUserName());
	}
 
	getWebcamUserName(): string {
		return this.webcamUser.getNickname();
	}
 
	getScreenUserName() {
		return this.getWebcamUserName() + '_SCREEN';
	}
 
	resetUsersZoom() {
		this.webcamUser?.setVideoSizeBig(false);
		this.screenUser?.setVideoSizeBig(false);
	}
 
	toggleZoom(connectionId: string) {
		if (this.webcamUser.getConnectionId() === connectionId) {
			this.webcamUser.setVideoSizeBig(!this.webcamUser.isVideoSizeBig());
			return;
		}
		this.screenUser.setVideoSizeBig(!this.screenUser.isVideoSizeBig());
	}
}