openvidu/openvidu-testapp/src/app/components/dashboard/dashboard.component.ts

192 lines
4.7 KiB
TypeScript
Raw Normal View History

2017-09-26 18:13:00 +02:00
import { Component, OnInit } from '@angular/core';
import { OpenviduRestService } from '../../services/openvidu-rest.service';
2017-09-27 16:24:39 +02:00
import { DataSource } from '@angular/cdk/table';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/of';
2017-09-27 18:42:11 +02:00
import { OpenVidu, Session } from 'openvidu-browser';
2017-09-27 16:24:39 +02:00
import * as colormap from 'colormap';
const numColors = 64;
declare var $: any;
2017-09-26 18:13:00 +02:00
@Component({
selector: 'app-dashboard',
templateUrl: './dashboard.component.html',
2017-09-27 16:24:39 +02:00
styleUrls: ['./dashboard.component.css'],
2017-09-26 18:13:00 +02:00
})
export class DashboardComponent implements OnInit {
2017-09-27 16:24:39 +02:00
openviduURL = 'https://localhost:8443';
openviduSecret = 'MY_SECRET';
serverData = 'data_test';
selectedRole = 'PUBLISHER';
selectedRadioIndex = 0;
openViduRoles = ['SUBSCRIBER', 'PUBLISHER', 'MODERATOR'];
2017-09-27 18:42:11 +02:00
sendAudio = true;
sendVideo = true;
optionVideo = 'video';
activeAudio = true;
activeVideo = true;
2017-09-26 18:13:00 +02:00
// Join form
clientData: string;
2017-09-27 18:42:11 +02:00
sessionName: string;
// OpenVidu objects
OV: OpenVidu;
session: Session;
2017-09-26 18:13:00 +02:00
2017-09-27 16:24:39 +02:00
// API REST data collected
data = [];
cg;
2017-09-26 18:13:00 +02:00
constructor(private openviduRestService: OpenviduRestService) {
this.generateSessionInfo();
2017-09-27 16:24:39 +02:00
const options = {
colormap: [
{ 'index': 0, 'rgb': [135, 196, 213] },
{ 'index': 1, 'rgb': [255, 230, 151] }],
nshades: numColors,
format: 'hex'
};
this.cg = colormap(options);
2017-09-26 18:13:00 +02:00
}
ngOnInit() { }
2017-09-27 18:42:11 +02:00
/* TEST SESSION TAB */
2017-09-26 18:13:00 +02:00
private generateSessionInfo() {
this.sessionName = 'TestSession';
2017-09-27 18:42:11 +02:00
this.clientData = 'TestClient';
2017-09-26 18:13:00 +02:00
}
2017-09-27 18:42:11 +02:00
private removeHttps = input => input.replace(/^https?:\/\//, '');
private joinSession(): void {
this.OV = new OpenVidu();
this.session = this.OV.initSession('wss://'
+ this.removeHttps(this.openviduURL)
+ '/'
+ this.sessionName + '?secret='
+ this.openviduSecret);
this.session.on('streamCreated', (event) => {
const subscriber = this.session.subscribe(event.stream, 'video-container');
subscriber.on('videoElementCreated', (e) => {
this.appendUserData(e.element, subscriber.stream.connection.data, subscriber.stream.connection);
});
});
this.session.on('streamDestroyed', (event) => {
this.removeUserData(event.stream.connection);
});
this.session.connect(null, this.clientData, (error) => {
if (!error) {
const publisher = this.OV.initPublisher('video-container', {
audio: true,
video: true,
quality: 'MEDIUM'
});
publisher.on('videoElementCreated', (event) => {
this.appendUserData(event.element, this.clientData, null);
event.element['muted'] = true;
});
this.session.publish(publisher);
} 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;
}
private appendUserData(videoElement, data, connection) {
const dataNode = document.createElement('div');
dataNode.className = 'data-node';
dataNode.id = 'data-' + (connection ? connection.connectionId : data);
dataNode.innerHTML = '<p>' + data + '</p>';
videoElement.parentNode.insertBefore(dataNode, videoElement.nextSibling);
}
private removeUserData(connection) {
$('#data-' + connection.connectionId).remove();
}
private removeAllUserData() {
const nicknameElements = $('.data-node');
while (nicknameElements[0]) {
nicknameElements[0].remove();
}
}
/* TEST SESSION TAB */
/* API REST TAB */
2017-09-26 18:13:00 +02:00
private getSessionId() {
2017-09-27 16:24:39 +02:00
this.openviduRestService.getSessionId(this.openviduURL, this.openviduSecret)
2017-09-26 18:13:00 +02:00
.then((sessionId) => {
2017-09-27 16:24:39 +02:00
this.updateData();
2017-09-26 18:13:00 +02:00
})
.catch((error) => {
console.error('Error getting a sessionId', error);
});
}
private getToken() {
2017-09-27 16:24:39 +02:00
const sessionId = this.data[this.selectedRadioIndex][0];
this.openviduRestService.getToken(this.openviduURL, this.openviduSecret, sessionId, this.selectedRole, this.serverData)
2017-09-26 18:13:00 +02:00
.then((token) => {
2017-09-27 16:24:39 +02:00
this.updateData();
2017-09-26 18:13:00 +02:00
})
.catch((error) => {
console.error('Error getting a token', error);
});
}
2017-09-27 16:24:39 +02:00
private updateData() {
this.data = Array.from(this.openviduRestService.getAvailableParams());
}
private getTokenDisabled(): boolean {
return ((this.data.length === 0) || this.selectedRadioIndex === undefined);
}
private getBackgroundColor(index: number) {
return this.cg[((index + 1) * 15) % numColors];
}
private cleanAllSessions() {
this.data = [];
this.openviduRestService.sessionIdSession.clear();
this.openviduRestService.sessionIdTokenOpenViduRole.clear();
}
2017-09-27 18:42:11 +02:00
/* API REST TAB */
2017-09-26 18:13:00 +02:00
}