2018-04-18 14:29:07 +02:00
|
|
|
/*
|
|
|
|
* (C) Copyright 2017-2018 OpenVidu (http://openvidu.io/)
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2018-04-23 11:06:16 +02:00
|
|
|
import { Session } from './Session';
|
|
|
|
import { SessionProperties } from './SessionProperties';
|
|
|
|
import { Recording } from './Recording';
|
|
|
|
import { RecordingLayout } from './RecordingLayout';
|
|
|
|
import { RecordingProperties } from './RecordingProperties';
|
2018-03-14 18:48:29 +01:00
|
|
|
|
|
|
|
declare const Buffer;
|
2018-04-23 11:06:16 +02:00
|
|
|
const https = require('https');
|
2017-06-10 01:44:31 +02:00
|
|
|
|
|
|
|
export class OpenVidu {
|
|
|
|
|
2018-03-14 18:48:29 +01:00
|
|
|
private static readonly API_RECORDINGS: string = '/api/recordings';
|
|
|
|
private static readonly API_RECORDINGS_START: string = '/start';
|
|
|
|
private static readonly API_RECORDINGS_STOP: string = '/stop';
|
|
|
|
|
|
|
|
private hostname: string;
|
|
|
|
private port: number;
|
|
|
|
private basicAuth: string;
|
|
|
|
|
|
|
|
constructor(private urlOpenViduServer: string, secret: string) {
|
|
|
|
this.setHostnameAndPort();
|
|
|
|
this.basicAuth = this.getBasicAuth(secret);
|
|
|
|
}
|
2017-06-10 01:44:31 +02:00
|
|
|
|
2018-01-27 19:39:49 +01:00
|
|
|
public createSession(properties?: SessionProperties): Session {
|
2018-03-14 18:48:29 +01:00
|
|
|
return new Session(this.hostname, this.port, this.basicAuth, properties);
|
|
|
|
}
|
|
|
|
|
2018-04-18 14:23:16 +02:00
|
|
|
public startRecording(sessionId: string): Promise<Recording>;
|
|
|
|
public startRecording(sessionId: string, name: string): Promise<Recording>;
|
|
|
|
public startRecording(sessionId: string, properties: RecordingProperties): Promise<Recording>;
|
|
|
|
|
|
|
|
public startRecording(sessionId: string, param2?: string | RecordingProperties): Promise<Recording> {
|
2018-04-18 10:39:39 +02:00
|
|
|
return new Promise<Recording>((resolve, reject) => {
|
2018-03-14 18:48:29 +01:00
|
|
|
|
2018-04-18 14:23:16 +02:00
|
|
|
let requestBody;
|
|
|
|
|
|
|
|
if (!!param2) {
|
|
|
|
if (!(typeof param2 === 'string')) {
|
2018-04-20 12:04:56 +02:00
|
|
|
const properties = <RecordingProperties>param2;
|
2018-04-18 14:23:16 +02:00
|
|
|
requestBody = JSON.stringify({
|
|
|
|
session: sessionId,
|
2018-04-23 11:06:16 +02:00
|
|
|
name: !!properties.name ? properties.name : '',
|
|
|
|
recordingLayout: !!properties.recordingLayout ? properties.recordingLayout : '',
|
|
|
|
customLayout: !!properties.customLayout ? properties.customLayout : ''
|
2018-04-18 14:23:16 +02:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
requestBody = JSON.stringify({
|
|
|
|
session: sessionId,
|
2018-04-20 12:04:56 +02:00
|
|
|
name: param2,
|
2018-04-20 15:50:56 +02:00
|
|
|
recordingLayout: '',
|
|
|
|
customLayout: ''
|
2018-04-18 14:23:16 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
requestBody = JSON.stringify({
|
|
|
|
session: sessionId,
|
2018-04-20 12:04:56 +02:00
|
|
|
name: '',
|
2018-04-20 15:50:56 +02:00
|
|
|
recordingLayout: '',
|
|
|
|
customLayout: ''
|
2018-04-18 14:23:16 +02:00
|
|
|
});
|
|
|
|
}
|
2018-03-14 18:48:29 +01:00
|
|
|
|
2018-04-23 11:06:16 +02:00
|
|
|
const options = {
|
2018-03-14 18:48:29 +01:00
|
|
|
hostname: this.hostname,
|
|
|
|
port: this.port,
|
|
|
|
path: OpenVidu.API_RECORDINGS + OpenVidu.API_RECORDINGS_START,
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'Authorization': this.basicAuth,
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
'Content-Length': Buffer.byteLength(requestBody)
|
|
|
|
}
|
2018-04-23 11:06:16 +02:00
|
|
|
};
|
|
|
|
|
2018-03-14 18:48:29 +01:00
|
|
|
const req = https.request(options, (res) => {
|
|
|
|
let body = '';
|
|
|
|
res.on('data', (d) => {
|
|
|
|
// Continuously update stream with data
|
|
|
|
body += d;
|
|
|
|
});
|
|
|
|
res.on('end', () => {
|
|
|
|
if (res.statusCode === 200) {
|
2018-04-18 10:39:39 +02:00
|
|
|
// SUCCESS response from openvidu-server (Recording in JSON format). Resolve new Recording
|
|
|
|
resolve(new Recording(JSON.parse(body)));
|
2018-03-14 18:48:29 +01:00
|
|
|
} else {
|
|
|
|
// ERROR response from openvidu-server. Resolve HTTP status
|
|
|
|
reject(new Error(res.statusCode));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
req.on('error', (e) => {
|
|
|
|
reject(new Error(e));
|
|
|
|
});
|
|
|
|
req.write(requestBody);
|
|
|
|
req.end();
|
|
|
|
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-04-18 10:39:39 +02:00
|
|
|
public stopRecording(recordingId: string): Promise<Recording> {
|
|
|
|
return new Promise<Recording>((resolve, reject) => {
|
2018-03-14 18:48:29 +01:00
|
|
|
|
2018-04-23 11:06:16 +02:00
|
|
|
const options = {
|
2018-03-14 18:48:29 +01:00
|
|
|
hostname: this.hostname,
|
|
|
|
port: this.port,
|
|
|
|
path: OpenVidu.API_RECORDINGS + OpenVidu.API_RECORDINGS_STOP + '/' + recordingId,
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'Authorization': this.basicAuth,
|
|
|
|
'Content-Type': 'application/x-www-form-urlencoded'
|
|
|
|
}
|
2018-04-23 11:06:16 +02:00
|
|
|
};
|
|
|
|
|
2018-03-14 18:48:29 +01:00
|
|
|
const req = https.request(options, (res) => {
|
|
|
|
let body = '';
|
|
|
|
res.on('data', (d) => {
|
|
|
|
// Continuously update stream with data
|
|
|
|
body += d;
|
|
|
|
});
|
|
|
|
res.on('end', () => {
|
|
|
|
if (res.statusCode === 200) {
|
2018-04-18 10:39:39 +02:00
|
|
|
// SUCCESS response from openvidu-server (Recording in JSON format). Resolve new Recording
|
|
|
|
resolve(new Recording(JSON.parse(body)));
|
2018-03-14 18:48:29 +01:00
|
|
|
} else {
|
|
|
|
// ERROR response from openvidu-server. Resolve HTTP status
|
|
|
|
reject(new Error(res.statusCode));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
req.on('error', (e) => {
|
|
|
|
reject(new Error(e));
|
|
|
|
});
|
2018-04-23 11:06:16 +02:00
|
|
|
// req.write();
|
2018-03-14 18:48:29 +01:00
|
|
|
req.end();
|
|
|
|
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-04-18 10:39:39 +02:00
|
|
|
public getRecording(recordingId: string): Promise<Recording> {
|
|
|
|
return new Promise<Recording>((resolve, reject) => {
|
2018-03-14 18:48:29 +01:00
|
|
|
|
2018-04-23 11:06:16 +02:00
|
|
|
const options = {
|
2018-03-14 18:48:29 +01:00
|
|
|
hostname: this.hostname,
|
|
|
|
port: this.port,
|
|
|
|
path: OpenVidu.API_RECORDINGS + '/' + recordingId,
|
|
|
|
method: 'GET',
|
|
|
|
headers: {
|
|
|
|
'Authorization': this.basicAuth,
|
|
|
|
'Content-Type': 'application/x-www-form-urlencoded'
|
|
|
|
}
|
2018-04-23 11:06:16 +02:00
|
|
|
};
|
|
|
|
|
2018-03-14 18:48:29 +01:00
|
|
|
const req = https.request(options, (res) => {
|
|
|
|
let body = '';
|
|
|
|
res.on('data', (d) => {
|
|
|
|
// Continuously update stream with data
|
|
|
|
body += d;
|
|
|
|
});
|
|
|
|
res.on('end', () => {
|
|
|
|
if (res.statusCode === 200) {
|
2018-04-18 10:39:39 +02:00
|
|
|
// SUCCESS response from openvidu-server (Recording in JSON format). Resolve new Recording
|
|
|
|
resolve(new Recording(JSON.parse(body)));
|
2018-03-14 18:48:29 +01:00
|
|
|
} else {
|
|
|
|
// ERROR response from openvidu-server. Resolve HTTP status
|
|
|
|
reject(new Error(res.statusCode));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
req.on('error', (e) => {
|
|
|
|
reject(new Error(e));
|
|
|
|
});
|
2018-04-23 11:06:16 +02:00
|
|
|
// req.write();
|
2018-03-14 18:48:29 +01:00
|
|
|
req.end();
|
|
|
|
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-04-18 10:39:39 +02:00
|
|
|
public listRecordings(): Promise<Recording[]> {
|
|
|
|
return new Promise<Recording[]>((resolve, reject) => {
|
2018-03-14 18:48:29 +01:00
|
|
|
|
2018-04-23 11:06:16 +02:00
|
|
|
const options = {
|
2018-03-14 18:48:29 +01:00
|
|
|
hostname: this.hostname,
|
|
|
|
port: this.port,
|
|
|
|
path: OpenVidu.API_RECORDINGS,
|
|
|
|
method: 'GET',
|
|
|
|
headers: {
|
|
|
|
'Authorization': this.basicAuth,
|
|
|
|
'Content-Type': 'application/x-www-form-urlencoded'
|
|
|
|
}
|
2018-04-23 11:06:16 +02:00
|
|
|
};
|
|
|
|
|
2018-03-14 18:48:29 +01:00
|
|
|
const req = https.request(options, (res) => {
|
|
|
|
let body = '';
|
|
|
|
res.on('data', (d) => {
|
|
|
|
// Continuously update stream with data
|
|
|
|
body += d;
|
|
|
|
});
|
|
|
|
res.on('end', () => {
|
|
|
|
if (res.statusCode === 200) {
|
2018-04-18 10:39:39 +02:00
|
|
|
// SUCCESS response from openvidu-server (JSON arrays of recordings in JSON format). Resolve list of new recordings
|
2018-04-23 11:06:16 +02:00
|
|
|
const recordingArray: Recording[] = [];
|
|
|
|
const responseItems = JSON.parse(body).items;
|
|
|
|
for (const item of responseItems) {
|
|
|
|
recordingArray.push(new Recording(item));
|
2018-03-14 18:48:29 +01:00
|
|
|
}
|
2018-04-18 10:39:39 +02:00
|
|
|
resolve(recordingArray);
|
2018-03-14 18:48:29 +01:00
|
|
|
} else {
|
|
|
|
// ERROR response from openvidu-server. Resolve HTTP status
|
|
|
|
reject(new Error(res.statusCode));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
req.on('error', (e) => {
|
|
|
|
reject(new Error(e));
|
|
|
|
});
|
2018-04-23 11:06:16 +02:00
|
|
|
// req.write();
|
2018-03-14 18:48:29 +01:00
|
|
|
req.end();
|
|
|
|
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public deleteRecording(recordingId: string): Promise<Error> {
|
|
|
|
return new Promise<Error>((resolve, reject) => {
|
|
|
|
|
2018-04-23 11:06:16 +02:00
|
|
|
const options = {
|
2018-03-14 18:48:29 +01:00
|
|
|
hostname: this.hostname,
|
|
|
|
port: this.port,
|
|
|
|
path: OpenVidu.API_RECORDINGS + '/' + recordingId,
|
|
|
|
method: 'DELETE',
|
|
|
|
headers: {
|
|
|
|
'Authorization': this.basicAuth,
|
|
|
|
'Content-Type': 'application/x-www-form-urlencoded'
|
|
|
|
}
|
2018-04-23 11:06:16 +02:00
|
|
|
};
|
|
|
|
|
2018-03-14 18:48:29 +01:00
|
|
|
const req = https.request(options, (res) => {
|
|
|
|
let body = '';
|
|
|
|
res.on('data', (d) => {
|
|
|
|
// Continuously update stream with data
|
|
|
|
body += d;
|
|
|
|
});
|
|
|
|
res.on('end', () => {
|
|
|
|
if (res.statusCode === 204) {
|
|
|
|
// SUCCESS response from openvidu-server. Resolve undefined
|
|
|
|
resolve(undefined);
|
|
|
|
} else {
|
|
|
|
// ERROR response from openvidu-server. Resolve HTTP status
|
|
|
|
reject(new Error(res.statusCode));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
req.on('error', (e) => {
|
|
|
|
reject(new Error(e));
|
|
|
|
});
|
2018-04-23 11:06:16 +02:00
|
|
|
// req.write();
|
2018-03-14 18:48:29 +01:00
|
|
|
req.end();
|
|
|
|
|
|
|
|
});
|
2018-01-27 19:39:49 +01:00
|
|
|
}
|
|
|
|
|
2018-03-14 18:48:29 +01:00
|
|
|
private getBasicAuth(secret: string): string {
|
|
|
|
return 'Basic ' + (new Buffer('OPENVIDUAPP:' + secret).toString('base64'));
|
2018-01-27 19:39:49 +01:00
|
|
|
}
|
|
|
|
|
2018-03-14 18:48:29 +01:00
|
|
|
private setHostnameAndPort(): void {
|
2018-04-23 11:06:16 +02:00
|
|
|
const urlSplitted = this.urlOpenViduServer.split(':');
|
2018-03-14 18:48:29 +01:00
|
|
|
if (urlSplitted.length === 3) { // URL has format: http:// + hostname + :port
|
|
|
|
this.hostname = this.urlOpenViduServer.split(':')[1].replace(/\//g, '');
|
|
|
|
this.port = parseInt(this.urlOpenViduServer.split(':')[2].replace(/\//g, ''));
|
2018-04-23 11:06:16 +02:00
|
|
|
} else if (urlSplitted.length === 2) { // URL has format: hostname + :port
|
2018-03-14 18:48:29 +01:00
|
|
|
this.hostname = this.urlOpenViduServer.split(':')[0].replace(/\//g, '');
|
|
|
|
this.port = parseInt(this.urlOpenViduServer.split(':')[1].replace(/\//g, ''));
|
|
|
|
} else {
|
|
|
|
console.error("URL format incorrect: it must contain hostname and port (current value: '" + this.urlOpenViduServer + "')");
|
|
|
|
}
|
2017-06-10 01:44:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|