import { Injectable } from '@angular/core' ; import { UserModel } from '../../models/user-model' ; import { LoggerService } from '../logger/logger.service' ; import { ILogger } from '../../types/logger-type' ; import { Observable , BehaviorSubject } from 'rxjs' ; import { Publisher } from 'openvidu-browser/lib/OpenVidu/Publisher' ; @ Injectable ( { providedIn : 'root' } ) export class LocalUsersService { OVUsers : Observable < UserModel [ ] > ; screenShareState : Observable < boolean > ; webcamVideoActive : Observable < boolean > ; private _OVUsers = < BehaviorSubject < UserModel [ ] > > new BehaviorSubject ( [ ] ) ; private _screenShareState = < BehaviorSubject < boolean > > new BehaviorSubject ( false ) ; private _webcamVideoActive = < BehaviorSubject < boolean > > new BehaviorSubject ( true ) ; private webcamUser : UserModel = null ; private screenUser : UserModel = null ; private log : ILogger ; constructor ( private loggerSrv ) { this . log = this . loggerSrv . get ( 'LocalUsersService' ) ; } initialize ( ) { this . OVUsers = this . _OVUsers . asObservable ( ) ; this . screenShareState = this . _screenShareState . asObservable ( ) ; this . webcamVideoActive = this . _webcamVideoActive . asObservable ( ) ; this . webcamUser = new UserModel ( ) ; this . webcamUser . setLocal ( true ) ; this . _OVUsers . next ( [ this . webcamUser ] ) ; } getWebcamPublisher ( ) { return < Publisher > this . webcamUser ?. getStreamManager ( ) ; } setWebcamPublisher ( publisher ) { this . webcamUser . setStreamManager ( publisher ) ; } getScreenPublisher ( ) { return < Publisher > this . screenUser ?. getStreamManager ( ) ; } setScreenPublisher ( publisher ) { this . screenUser . setStreamManager ( publisher ) ; } enableWebcamUser ( ) { this . _OVUsers . next ( [ this . webcamUser , this . screenUser ] ) ; } disableWebcamUser ( ) { this . _OVUsers . next ( [ this . screenUser ] ) ; } enableScreenUser ( screenPublisher ) { this . log . d ( 'Enabling screen publisher' ) ; const connectionId = screenPublisher ?. session ?. connection ?. connectionId ; this . screenUser = new UserModel ( connectionId , screenPublisher , this . getScreenUserName ( ) ) ; this . screenUser . setAvatar ( this . webcamUser . getAvatar ( ) ) ; this . _screenShareState . next ( true ) ; if ( this . isWebCamEnabled ( ) ) { this . _OVUsers . next ( [ this . webcamUser , this . screenUser ] ) ; return ; } this . _OVUsers . next ( [ this . screenUser ] ) ; } disableScreenUser ( ) { this . _OVUsers . next ( [ this . webcamUser ] ) ; this . _screenShareState . next ( false ) ; } updateUsersStatus ( ) { this . _webcamVideoActive . next ( this . webcamUser . isVideoActive ( ) ) ; } clear ( ) { this . screenUser = null ; this . webcamUser = new UserModel ( ) ; this . disableScreenUser ( ) ; } isWebCamEnabled ( ) { return this . _OVUsers . getValue ( ) [ 0 ] . isCamera ( ) ; } isOnlyScreenConnected ( ) { return this . _OVUsers . getValue ( ) [ 0 ] . isScreen ( ) ; } hasWebcamVideoActive ( ) { return this . webcamUser . isVideoActive ( ) ; } hasWebcamAudioActive ( ) { return this . webcamUser ?. isAudioActive ( ) ; } hasScreenAudioActive ( ) { return this . screenUser ?. isAudioActive ( ) ; } areBothConnected ( ) { return this . _OVUsers . getValue ( ) . length === 2 ; } isOnlyWebcamConnected ( ) { return this . isWebCamEnabled ( ) && ! this . areBothConnected ( ) ; } isScreenShareEnabled ( ) { return this . areBothConnected ( ) || this . isOnlyScreenConnected ( ) ; } setAvatar ( avatar ) { this . webcamUser ?. setAvatar ( avatar ) ; this . screenUser ?. setAvatar ( avatar ) ; } updateUsersNickname ( nickname ) { this . webcamUser . setNickname ( nickname ) ; this . screenUser ?. setNickname ( this . getScreenUserName ( ) ) ; } getAvatar ( ) { return this . webcamUser . getAvatar ( ) ; } getWebcamUserName ( ) { return this . webcamUser . getNickname ( ) ; } getScreenUserName ( ) { return this . getWebcamUserName ( ) + '_SCREEN' ; } resetUsersZoom ( ) { this . webcamUser ?. setVideoSizeBig ( false ) ; this . screenUser ?. setVideoSizeBig ( false ) ; } toggleZoom ( connectionId ) { if ( this . webcamUser . getConnectionId ( ) === connectionId ) { this . webcamUser . setVideoSizeBig ( ! this . webcamUser . isVideoSizeBig ( ) ) ; return ; } this . screenUser . setVideoSizeBig ( ! this . screenUser . isVideoSizeBig ( ) ) ; } }