All files / src/lib/services/webrtc webrtc.service.ts

19.65% Statements 34/173
8.92% Branches 15/168
5.55% Functions 3/54
17.9% Lines 29/162

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 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 3301x 1x 1x 1x 1x   1x                                 1x   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 { Connection, OpenVidu, Publisher, PublisherProperties, Session, SignalOptions } from 'openvidu-browser';
I
import { LoggerService } from '../../services/logger/logger.service';
import { LocalUserService } from '../../services/local-user/local-user.service';
 
import { ILogger } from '../../models/logger.model';
import { ScreenType } from '../../models/video-type.model';
import { Signal } from '../../models/signal.model';
import { LibraryConfigService } from '../library-config/library-config.service';
import { PlatformService } from '../platform/platform.service';

@Injectable({
	providedIn: 'root'
})
export class WebrtcService {
	private OV: OpenVidu = null;
	private OVScreen: OpenVidu = null;
 
	private webcamSession: Session = null;
	private screenSession: Session = null;
 
	private videoSource = undefined;
	private audioSource = undefined;
 
	private screenMediaStream: MediaStream = null;
	private webcamMediaStream: MediaStream = null;
	private log: ILogger;
 
	constructor(
		private libraryConfigSrv: LibraryConfigService,
		private platformService: PlatformService,
		private loggerSrv: LoggerService,
		private localUsersSrv: LocalUserService
	) {
		this.log = this.loggerSrv.get('WebRTCService');
		this.OV = new OpenVidu();
		if (this.libraryConfigSrv.isProduction()) this.OV.enableProdMode();
		this.webcamSession = this.OV.initSession();
I
		// Initialize screen session only if it is not mobile platform
		if (!this.platformService.isMobile()) {
			this.OVScreen = new OpenVidu();
			if (this.libraryConfigSrv.isProduction()) this.OVScreen.enableProdMode();
			this.screenSession = this.OVScreen.initSession();
		}I
	}
 
	getWebcamSession(): Session {
		return this.webcamSession;
	}

	isWebcamSessionConnected(): boolean {
		return !!this.webcamSession.capabilities;
	}
 
	getScreenSession(): Session {
		return this.screenSession;
	}

	isScreenSessionConnected(): boolean {
		return !!this.screenSession.capabilities;
	}

	async connectWebcamSession(token: string): Promise<any> {
		if (!!token) {
			this.log.d('Connecting webcam session');
			const webcamUsername = this.localUsersSrv.getWebcamUserName();
			await this.webcamSession.connect(token, { clientData: webcamUsername });
		}
	}
	disconnectWebcamSession(): void {
		if (this.webcamSession) {
			this.log.d('Disconnecting webcam session');
			this.webcamSession.disconnect();
			this.webcamSession = null;
		}
	}

	async connectScreenSession(token: string): Promise<any> {
		if (!!token) {
			this.log.d('Connecting screen session');
			const screenUsername = this.localUsersSrv.getScreenUserName();
			await this.screenSession.connect(token, { clientData: screenUsername });
		}
	}
	disconnectScreenSession(): void {
		if (this.screenSession) {
			this.log.d('Disconnecting screen session');
			this.screenSession.disconnect();
			this.screenSession = null;
		}
	}
 
	disconnect() {
		this.disconnectWebcamSession();
		this.disconnectScreenSession();
		this.videoSource = undefined;
		this.audioSource = undefined;
		this.stopVideoTracks(this.localUsersSrv.getWebcamPublisher()?.stream?.getMediaStream());
		this.stopVideoTracks(this.localUsersSrv.getScreenPublisher()?.stream?.getMediaStream());
		this.stopAudioTracks(this.localUsersSrv.getWebcamPublisher()?.stream?.getMediaStream());
		this.stopAudioTracks(this.localUsersSrv.getScreenPublisher()?.stream?.getMediaStream());
	}
 
	initPublisher(targetElement: string | HTMLElement, properties: PublisherProperties): Publisher {
		this.log.d('Initializing publisher with properties: ', properties);

		const publisher = this.OV.initPublisher(targetElement, properties);
		// this.localUsersSrv.setWebcamPublisher(publisher);
		publisher.once('streamPlaying', () => {
			(<HTMLElement>publisher.videos[0].video).parentElement.classList.remove('custom-class');
		});
		return publisher;
	}

	async initPublisherAsync(targetElement: string | HTMLElement, properties: PublisherProperties): Promise<Publisher> {
		this.log.d('Initializing publisher with properties: ', properties);
 
		const publisher = await this.OV.initPublisherAsync(targetElement, properties);
		// this.localUsersSrv.setWebcamPublisher(publisher);
		publisher.once('streamPlaying', () => {
			(<HTMLElement>publisher.videos[0].video).parentElement.classList.remove('custom-class');
		});
		return publisher;
	}

	destroyWebcamPublisher(): void {
		const publisher = this.localUsersSrv.getWebcamPublisher();
		if (!!publisher) {
			// publisher.off('streamAudioVolumeChange');
			if (publisher.stream.getWebRtcPeer()) {
				publisher.stream.disposeWebRtcPeer();
			}
			publisher.stream.disposeMediaStream();
			this.localUsersSrv.setWebcamPublisher(publisher);
		}
	}

	destroyScreenPublisher(): void {
		const publisher = this.localUsersSrv.getScreenPublisher();

		if (!!publisher) {
			// publisher.off('streamAudioVolumeChange');
			if (publisher.stream.getWebRtcPeer()) {
				publisher.stream.disposeWebRtcPeer();
			}
			publisher.stream.disposeMediaStream();
			this.localUsersSrv.setScreenPublisher(publisher);
		}
	}

	async publishWebcamPublisher(): Promise<any> {
		if (this.webcamSession?.capabilities?.publish) {
			const publisher = this.localUsersSrv.getWebcamPublisher();
			if (!!publisher) {
				return await this.webcamSession.publish(publisher);
			}
		}
		this.log.e('Webcam publisher cannot be published');
	}
	unpublishWebcamPublisher(): void {
		const publisher = this.localUsersSrv.getWebcamPublisher();
		if (!!publisher) {
			this.publishScreenAudio(this.localUsersSrv.hasWebcamAudioActive());
			this.webcamSession.unpublish(publisher);
		}
	}
	async publishScreenPublisher(): Promise<any> {
		if (this.screenSession?.capabilities?.publish) {
			const publisher = this.localUsersSrv.getScreenPublisher();
			if (!!publisher) {
				return await this.screenSession.publish(publisher);
			}
		}
		this.log.e('Screen publisher cannot be published');
	}
 
	unpublishScreenPublisher(): void {
		const publisher = this.localUsersSrv.getScreenPublisher();
		if (!!publisher) {
			this.screenSession.unpublish(publisher);
		}
	}
	publishWebcamVideo(active: boolean): void {
		this.localUsersSrv.getWebcamPublisher().publishVideo(active);
		// Send event to subscribers because of video has changed and view must update
		this.localUsersSrv.updateUsersStatus();
	}
	publishWebcamAudio(active: boolean): void {
		const publisher = this.localUsersSrv.getWebcamPublisher();
		if (!!publisher) {
			publisher.publishAudio(active);
		}
		this.localUsersSrv.updateUsersStatus();
	}
	publishScreenAudio(active: boolean): void {
		const publisher = this.localUsersSrv.getScreenPublisher();
		if (!!publisher) {
			publisher.publishAudio(active);
		}
		this.localUsersSrv.updateUsersStatus();
	}
	replaceTrack(videoSource: string, audioSource: string, mirror: boolean = true): Promise<void> {
		return new Promise((resolve, reject) => {
			if (!!videoSource) {
				this.log.d('Replacing video track ' + videoSource);
				this.videoSource = videoSource;
				// this.stopVideoTracks(this.webcamUser.getStreamManager().stream.getMediaStream());
			}
			if (!!audioSource) {
				this.log.d('Replacing audio track ' + audioSource);
				this.audioSource = audioSource;
				// this.stopAudioTracks(this.webcamUser.getStreamManager().stream.getMediaStream());
			}
			this.destroyWebcamPublisher();
			const properties = this.createPublisherProperties(
				this.videoSource,
				this.audioSource,
				this.localUsersSrv.hasWebcamVideoActive(),
				this.localUsersSrv.hasWebcamAudioActive(),
				mirror
			);

			const publisher = this.initPublisher(undefined, properties);
			this.localUsersSrv.setWebcamPublisher(publisher);
 
			publisher.once('streamPlaying', () => {
				this.localUsersSrv.setWebcamPublisher(publisher);
				resolve();
			});
 
			publisher.once('accessDenied', () => {
				reject();
			});
 
			// Reeplace track method
			// this.webcamMediaStream = await this.OV.getUserMedia(properties);
			// const track: MediaStreamTrack = !!videoSource
			// 	? this.webcamMediaStream.getVideoTracks()[0]
			// 	: this.webcamMediaStream.getAudioTracks()[0];
 
			// try {
			// 	await (<Publisher>this.webcamUser.getStreamManager()).replaceTrack(track);
			// } catch (error) {
			// 	this.log.e('Error replacing track ', error);
			// }
		});
	}
 
	sendSignal(type: Signal, connection?: Connection, data?: any): void {
		const signalOptions: SignalOptions = {
			data: JSON.stringify(data),
			type: type,
			to: connection ? [connection] : undefined
		};
		this.webcamSession.signal(signalOptions);
 
		if (type === Signal.NICKNAME_CHANGED && !!this.getScreenSession().connection) {
			signalOptions.data = JSON.stringify({ clientData: this.localUsersSrv.getScreenUserName() });
			this.getScreenSession()?.signal(signalOptions);
		}
	}

	createPublisherProperties(
		videoSource: string | MediaStreamTrack | boolean,
		audioSource: string | MediaStreamTrack | boolean,
		publishVideo: boolean,
		publishAudio: boolean,
		mirror: boolean
	): PublisherProperties {
		return {
			videoSource,
			audioSource,
			publishVideo,
			publishAudio,
			mirror
		};
	}
 
	async replaceScreenTrack() {
		const videoSource = ScreenType.SCREEN;
		const hasAudio = !this.localUsersSrv.isWebCamEnabled();
		const properties = this.createPublisherProperties(videoSource, undefined, true, hasAudio, false);
 
		this.stopScreenTracks();
		this.screenMediaStream = await this.OVScreen.getUserMedia(properties);
		await this.localUsersSrv.getScreenPublisher().replaceTrack(this.screenMediaStream.getVideoTracks()[0]);
	}

	stopAudioTracks(mediaStream: MediaStream) {
		mediaStream?.getAudioTracks().forEach((track) => {
			track.stop();

			track.enabled = false;
		});
		this.webcamMediaStream?.getAudioTracks().forEach((track) => {
			track.stop();
		});
	}
 
	stopVideoTracks(mediaStream: MediaStream) {
		mediaStream?.getVideoTracks().forEach((track) => {
			track.stop();
		});
	}
 
	needSendNicknameSignal(): boolean {
		const oldNickname: string = JSON.parse(this.webcamSession.connection.data).clientData;
		return oldNickname !== this.localUsersSrv.getWebcamUserName();
	}
 
	isMyOwnConnection(connectionId: string): boolean {
		return (
			this.webcamSession?.connection?.connectionId === connectionId || this.screenSession?.connection?.connectionId === connectionId
		);
	}
 
	getSessionOfUserConnected(): Session {
		return this.localUsersSrv.isWebCamEnabled() ? this.webcamSession : this.screenSession;
	}
 
	private stopScreenTracks() {
		if (this.screenMediaStream) {
			this.stopAudioTracks(this.screenMediaStream);
			this.stopVideoTracks(this.screenMediaStream);
		}
	}
}