2017-09-28 19:13:29 +02:00
|
|
|
import { Component, Input, HostListener, ChangeDetectorRef, OnInit, OnDestroy } from '@angular/core';
|
2017-09-29 13:54:22 +02:00
|
|
|
import { OpenVidu, Session, Subscriber, Publisher, Stream } from 'openvidu-browser';
|
2017-09-28 19:13:29 +02:00
|
|
|
|
|
|
|
declare var $: any;
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'app-openvidu-instance',
|
|
|
|
templateUrl: './openvidu-instance.component.html',
|
|
|
|
styleUrls: ['./openvidu-instance.component.css']
|
|
|
|
})
|
|
|
|
export class OpenviduInstanceComponent implements OnInit, OnDestroy {
|
|
|
|
|
|
|
|
@Input()
|
|
|
|
openviduURL: string;
|
|
|
|
|
|
|
|
@Input()
|
|
|
|
openviduSecret: string;
|
|
|
|
|
2017-09-29 13:54:22 +02:00
|
|
|
subscribeTo = true;
|
|
|
|
publishTo = true;
|
2017-09-28 19:13:29 +02:00
|
|
|
sendAudio = true;
|
|
|
|
sendVideo = true;
|
|
|
|
activeAudio = true;
|
|
|
|
activeVideo = true;
|
2017-09-29 13:54:22 +02:00
|
|
|
optionVideo = 'video';
|
2017-09-28 19:13:29 +02:00
|
|
|
sendVideoRadio = true;
|
2017-09-29 13:54:22 +02:00
|
|
|
subscribeToRemote = false;
|
2017-09-28 19:13:29 +02:00
|
|
|
|
|
|
|
// Join form
|
|
|
|
clientData: string;
|
|
|
|
sessionName: string;
|
|
|
|
|
|
|
|
// OpenVidu objects
|
|
|
|
OV: OpenVidu;
|
|
|
|
session: Session;
|
2017-09-29 13:54:22 +02:00
|
|
|
publisher: Publisher;
|
|
|
|
|
|
|
|
audioMuted = false;
|
|
|
|
videoMuted = false;
|
|
|
|
audioIcon = 'mic';
|
|
|
|
videoIcon = 'videocam';
|
2017-09-28 19:13:29 +02:00
|
|
|
|
|
|
|
constructor(private changeDetector: ChangeDetectorRef) {
|
|
|
|
this.generateSessionInfo();
|
|
|
|
}
|
|
|
|
|
|
|
|
ngOnInit() { }
|
|
|
|
|
|
|
|
ngOnDestroy() {
|
|
|
|
this.leaveSession();
|
|
|
|
}
|
|
|
|
|
|
|
|
@HostListener('window:beforeunload')
|
|
|
|
beforeunloadHandler() {
|
|
|
|
this.leaveSession();
|
|
|
|
}
|
|
|
|
|
|
|
|
private generateSessionInfo(): void {
|
|
|
|
this.sessionName = 'TestSession';
|
|
|
|
this.clientData = 'TestClient';
|
|
|
|
}
|
|
|
|
|
|
|
|
private removeHttps = input => input.replace(/^https?:\/\//, '');
|
|
|
|
|
|
|
|
private joinSession(): void {
|
|
|
|
|
|
|
|
if (this.session) {
|
|
|
|
this.leaveSession();
|
|
|
|
}
|
|
|
|
|
|
|
|
const OV: OpenVidu = new OpenVidu();
|
|
|
|
|
|
|
|
this.session = OV.initSession('wss://'
|
|
|
|
+ this.removeHttps(this.openviduURL)
|
|
|
|
+ '/'
|
|
|
|
+ this.sessionName + '?secret='
|
|
|
|
+ this.openviduSecret);
|
|
|
|
|
|
|
|
this.session.on('streamCreated', (event) => {
|
|
|
|
|
|
|
|
this.changeDetector.detectChanges();
|
|
|
|
|
2017-09-29 13:54:22 +02:00
|
|
|
if (this.subscribeTo) {
|
|
|
|
const subscriber: Subscriber = this.session.subscribe(event.stream, 'remote-vid-' + this.session.connection.connectionId);
|
|
|
|
subscriber.on('videoElementCreated', (e) => {
|
|
|
|
this.appendUserData(e.element, subscriber.stream.connection.data, subscriber.stream.connection);
|
|
|
|
});
|
|
|
|
subscriber.on('videoPlaying', (e) => {
|
|
|
|
});
|
|
|
|
}
|
2017-09-28 19:13:29 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
this.session.on('streamDestroyed', (event) => {
|
|
|
|
this.removeUserData(event.stream.connection);
|
|
|
|
});
|
|
|
|
|
|
|
|
this.session.on('connectionCreated', (event) => { });
|
|
|
|
this.session.on('connetionDestroyed', (event) => { });
|
|
|
|
this.session.on('sessionDisconnected', (event) => { });
|
|
|
|
|
|
|
|
this.session.connect(null, this.clientData, (error) => {
|
|
|
|
if (!error) {
|
2017-09-29 13:54:22 +02:00
|
|
|
if (this.publishTo) {
|
2017-09-28 19:13:29 +02:00
|
|
|
|
2017-09-29 13:54:22 +02:00
|
|
|
this.audioMuted = !this.activeAudio;
|
|
|
|
this.videoMuted = !this.activeVideo;
|
|
|
|
this.updateAudioIcon();
|
|
|
|
this.updateVideoIcon();
|
2017-09-28 19:13:29 +02:00
|
|
|
|
2017-09-29 13:54:22 +02:00
|
|
|
this.publisher = OV.initPublisher('local-vid-' + this.session.connection.connectionId, {
|
|
|
|
audio: this.activeAudio,
|
|
|
|
video: this.activeVideo,
|
|
|
|
quality: 'MEDIUM'
|
|
|
|
});
|
2017-09-28 19:13:29 +02:00
|
|
|
|
2017-09-29 13:54:22 +02:00
|
|
|
this.publisher.on('videoElementCreated', (event) => {
|
|
|
|
});
|
|
|
|
|
|
|
|
this.publisher.on('videoPlaying', (e) => {
|
|
|
|
});
|
|
|
|
|
|
|
|
if (this.subscribeToRemote) {
|
|
|
|
this.publisher.subscribeToRemote();
|
|
|
|
}
|
2017-09-28 19:13:29 +02:00
|
|
|
|
2017-09-29 13:54:22 +02:00
|
|
|
this.session.publish(this.publisher);
|
2017-09-28 19:13:29 +02:00
|
|
|
|
2017-09-29 13:54:22 +02:00
|
|
|
}
|
2017-09-28 19:13:29 +02:00
|
|
|
} else {
|
|
|
|
console.log('There was an error connecting to the session:', error.code, error.message);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private leaveSession(): void {
|
|
|
|
if (this.session) {
|
|
|
|
this.session.disconnect();
|
|
|
|
}
|
|
|
|
this.session = null;
|
|
|
|
this.OV = null;
|
2017-09-29 13:54:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private toggleAudio() {
|
|
|
|
this.publisher.publishAudio(this.audioMuted);
|
|
|
|
this.audioMuted = !this.audioMuted;
|
|
|
|
this.updateAudioIcon();
|
|
|
|
}
|
|
|
|
|
|
|
|
private updateAudioIcon() {
|
|
|
|
this.audioMuted ? this.audioIcon = 'mic_off' : this.audioIcon = 'mic';
|
|
|
|
}
|
|
|
|
|
|
|
|
private toggleVideo() {
|
|
|
|
this.publisher.publishVideo(this.videoMuted);
|
|
|
|
this.videoMuted = !this.videoMuted;
|
|
|
|
this.updateVideoIcon();
|
|
|
|
}
|
|
|
|
|
|
|
|
private updateVideoIcon() {
|
|
|
|
this.videoMuted ? this.videoIcon = 'videocam_off' : this.videoIcon = 'videocam';
|
2017-09-28 19:13:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private appendUserData(videoElement, data, connection): void {
|
2017-09-29 13:54:22 +02:00
|
|
|
const dataNode = document.createElement('div');
|
2017-09-28 19:13:29 +02:00
|
|
|
dataNode.className = 'data-node';
|
|
|
|
dataNode.id = 'data-' + (connection ? connection.connectionId : data);
|
|
|
|
dataNode.innerHTML = '<p>' + data + '</p>';
|
2017-09-29 13:54:22 +02:00
|
|
|
videoElement.parentNode.insertBefore(dataNode, videoElement.nextSibling);
|
2017-09-28 19:13:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private removeUserData(connection): void {
|
2017-09-29 13:54:22 +02:00
|
|
|
$('#remote-vid-' + this.session.connection.connectionId).find('#data-' + connection.connectionId).remove();
|
2017-09-28 19:13:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private toggleRadio(): void {
|
2017-09-29 13:54:22 +02:00
|
|
|
if (this.publishTo && this.sendVideo) {
|
|
|
|
this.optionVideo = 'video';
|
|
|
|
}
|
2017-09-28 19:13:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|