openvidu/openvidu-node-client/lib/OpenVidu.js

331 lines
16 KiB
JavaScript
Raw Normal View History

2017-06-10 01:44:31 +02:00
"use strict";
/*
* (C) Copyright 2017-2018 OpenVidu (https://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.
*
*/
2017-06-10 01:44:31 +02:00
Object.defineProperty(exports, "__esModule", { value: true });
var Session_1 = require("./Session");
var Recording_1 = require("./Recording");
var axios_1 = require("axios");
var OpenVidu = /** @class */ (function () {
/**
* @param urlOpenViduServer Public accessible IP where your instance of OpenVidu Server is up an running
* @param secret Secret used on OpenVidu Server initialization
*/
2017-06-10 01:44:31 +02:00
function OpenVidu(urlOpenViduServer, secret) {
this.urlOpenViduServer = urlOpenViduServer;
this.Buffer = require('buffer/').Buffer;
2018-03-14 18:48:29 +01:00
this.setHostnameAndPort();
this.basicAuth = this.getBasicAuth(secret);
2017-06-10 01:44:31 +02:00
}
/**
* Creates an OpenVidu session. You can call [[Session.getSessionId]] in the resolved promise to retrieve the `sessionId`
*
2018-05-03 11:48:57 +02:00
* @returns A Promise that is resolved to the [[Session]] if success and rejected with an Error object if not.
*/
OpenVidu.prototype.createSession = function (properties) {
var _this = this;
return new Promise(function (resolve, reject) {
var session = new Session_1.Session(_this.hostname, _this.port, _this.basicAuth, properties);
session.getSessionIdHttp()
.then(function (sessionId) {
resolve(session);
})
.catch(function (error) {
reject(error);
});
});
};
/**
* Starts the recording of a [[Session]]
*
* @param sessionId The `sessionId` of the [[Session]] you want to start recording
* @param name The name you want to give to the video file. You can access this same value in your clients on recording events (`recordingStarted`, `recordingStopped`)
*
* @returns A Promise that is resolved to the [[Recording]] if it successfully started (the recording can be stopped with guarantees) and rejected with an Error object if not. This Error object has as `message` property with the following values:
* - `404`: no session exists for the passed `sessionId`
* - `400`: the session has no connected participants
* - `409`: the session is not configured for using [[MediaMode.ROUTED]] or it is already being recorded
* - `501`: OpenVidu Server recording module is disabled (`openvidu.recording` property set to `false`)
*/
2018-04-18 14:23:16 +02:00
OpenVidu.prototype.startRecording = function (sessionId, param2) {
2018-03-14 18:48:29 +01:00
var _this = this;
return new Promise(function (resolve, reject) {
var data;
2018-04-18 14:23:16 +02:00
if (!!param2) {
if (!(typeof param2 === 'string')) {
var properties = param2;
data = JSON.stringify({
2018-04-18 14:23:16 +02:00
session: sessionId,
name: !!properties.name ? properties.name : '',
recordingLayout: !!properties.recordingLayout ? properties.recordingLayout : '',
customLayout: !!properties.customLayout ? properties.customLayout : ''
2018-04-18 14:23:16 +02:00
});
}
else {
data = JSON.stringify({
2018-04-18 14:23:16 +02:00
session: sessionId,
name: param2,
recordingLayout: '',
customLayout: ''
2018-04-18 14:23:16 +02:00
});
}
}
else {
data = JSON.stringify({
2018-04-18 14:23:16 +02:00
session: sessionId,
name: '',
recordingLayout: '',
customLayout: ''
2018-04-18 14:23:16 +02:00
});
}
axios_1.default.post('https://' + _this.hostname + ':' + _this.port + OpenVidu.API_RECORDINGS + OpenVidu.API_RECORDINGS_START, data, {
2018-03-14 18:48:29 +01:00
headers: {
'Authorization': _this.basicAuth,
'Content-Type': 'application/json'
}
})
.then(function (res) {
if (res.status === 200) {
// SUCCESS response from openvidu-server (Recording in JSON format). Resolve new Recording
resolve(new Recording_1.Recording(res.data));
}
else {
// ERROR response from openvidu-server. Resolve HTTP status
reject(new Error(res.status.toString()));
}
}).catch(function (error) {
if (error.response) {
// The request was made and the server responded with a status code (not 2xx)
reject(new Error(error.response.status.toString()));
}
else if (error.request) {
// The request was made but no response was received
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of
// http.ClientRequest in node.js
console.error(error.request);
}
else {
// Something happened in setting up the request that triggered an Error
console.error('Error', error.message);
}
2018-03-14 18:48:29 +01:00
});
});
};
/**
* Stops the recording of a [[Session]]
*
* @param recordingId The `id` property of the [[Recording]] you want to stop
*
* @returns A Promise that is resolved to the [[Recording]] if it successfully stopped and rejected with an Error object if not. This Error object has as `message` property with the following values:
* - `404`: no recording exists for the passed `recordingId`
* - `406`: recording has `starting` status. Wait until `started` status before stopping the recording
*/
2018-03-14 18:48:29 +01:00
OpenVidu.prototype.stopRecording = function (recordingId) {
var _this = this;
return new Promise(function (resolve, reject) {
axios_1.default.post('https://' + _this.hostname + ':' + _this.port + OpenVidu.API_RECORDINGS + OpenVidu.API_RECORDINGS_STOP + '/' + recordingId, undefined, {
2018-03-14 18:48:29 +01:00
headers: {
'Authorization': _this.basicAuth,
'Content-Type': 'application/x-www-form-urlencoded'
}
})
.then(function (res) {
if (res.status === 200) {
// SUCCESS response from openvidu-server (Recording in JSON format). Resolve new Recording
resolve(new Recording_1.Recording(res.data));
}
else {
// ERROR response from openvidu-server. Resolve HTTP status
reject(new Error(res.status.toString()));
}
}).catch(function (error) {
if (error.response) {
// The request was made and the server responded with a status code (not 2xx)
reject(new Error(error.response.status.toString()));
}
else if (error.request) {
// The request was made but no response was received
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of
// http.ClientRequest in node.js
console.error(error.request);
}
else {
// Something happened in setting up the request that triggered an Error
console.error('Error', error.message);
}
2018-03-14 18:48:29 +01:00
});
});
2017-06-10 01:44:31 +02:00
};
/**
* Gets an existing [[Recording]]
*
* @param recordingId The `id` property of the [[Recording]] you want to retrieve
*
* @returns A Promise that is resolved to the [[Recording]] if it successfully stopped and rejected with an Error object if not. This Error object has as `message` property with the following values:
* - `404`: no recording exists for the passed `recordingId`
*/
2018-03-14 18:48:29 +01:00
OpenVidu.prototype.getRecording = function (recordingId) {
var _this = this;
return new Promise(function (resolve, reject) {
axios_1.default.get('https://' + _this.hostname + ':' + _this.port + OpenVidu.API_RECORDINGS + '/' + recordingId, {
2018-03-14 18:48:29 +01:00
headers: {
'Authorization': _this.basicAuth,
'Content-Type': 'application/x-www-form-urlencoded'
}
})
.then(function (res) {
if (res.status === 200) {
// SUCCESS response from openvidu-server (Recording in JSON format). Resolve new Recording
resolve(new Recording_1.Recording(res.data));
}
else {
// ERROR response from openvidu-server. Resolve HTTP status
reject(new Error(res.status.toString()));
}
}).catch(function (error) {
if (error.response) {
// The request was made and the server responded with a status code (not 2xx)
reject(new Error(error.response.status.toString()));
}
else if (error.request) {
// The request was made but no response was received
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of
// http.ClientRequest in node.js
console.error(error.request);
}
else {
// Something happened in setting up the request that triggered an Error
console.error('Error', error.message);
}
2018-03-14 18:48:29 +01:00
});
});
};
/**
* Lists all existing recordings
*
* @returns A Promise that is resolved to an array with all existing recordings
*/
2018-03-14 18:48:29 +01:00
OpenVidu.prototype.listRecordings = function () {
var _this = this;
return new Promise(function (resolve, reject) {
axios_1.default.get('https://' + _this.hostname + ':' + _this.port + OpenVidu.API_RECORDINGS, {
2018-03-14 18:48:29 +01:00
headers: {
'Authorization': _this.basicAuth,
'Content-Type': 'application/x-www-form-urlencoded'
}
})
.then(function (res) {
if (res.status === 200) {
// SUCCESS response from openvidu-server (JSON arrays of recordings in JSON format). Resolve list of new recordings
var recordingArray = [];
var responseItems = res.data.items;
for (var _i = 0, responseItems_1 = responseItems; _i < responseItems_1.length; _i++) {
var item = responseItems_1[_i];
recordingArray.push(new Recording_1.Recording(item));
2018-03-14 18:48:29 +01:00
}
resolve(recordingArray);
}
else {
// ERROR response from openvidu-server. Resolve HTTP status
reject(new Error(res.status.toString()));
}
}).catch(function (error) {
if (error.response) {
// The request was made and the server responded with a status code (not 2xx)
reject(new Error(error.response.status.toString()));
}
else if (error.request) {
// The request was made but no response was received
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of
// http.ClientRequest in node.js
console.error(error.request);
}
else {
// Something happened in setting up the request that triggered an Error
console.error('Error', error.message);
}
2018-03-14 18:48:29 +01:00
});
});
};
/**
* Deletes a [[Recording]]. The recording must have status `stopped` or `available`
*
* @param recordingId
*
* @returns A Promise that is resolved if the Recording was successfully deleted and rejected with an Error object if not. This Error object has as `message` property with the following values:
* - `404`: no recording exists for the passed `recordingId`
* - `409`: the recording has `started` status. Stop it before deletion
*/
2018-03-14 18:48:29 +01:00
OpenVidu.prototype.deleteRecording = function (recordingId) {
var _this = this;
return new Promise(function (resolve, reject) {
axios_1.default.delete('https://' + _this.hostname + ':' + _this.port + OpenVidu.API_RECORDINGS + '/' + recordingId, {
2018-03-14 18:48:29 +01:00
headers: {
'Authorization': _this.basicAuth,
'Content-Type': 'application/x-www-form-urlencoded'
}
})
.then(function (res) {
if (res.status === 204) {
// SUCCESS response from openvidu-server. Resolve undefined
resolve(undefined);
}
else {
// ERROR response from openvidu-server. Resolve HTTP status
reject(new Error(res.status.toString()));
}
}).catch(function (error) {
if (error.response) {
// The request was made and the server responded with a status code (not 2xx)
reject(new Error(error.response.status.toString()));
}
else if (error.request) {
// The request was made but no response was received
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of
// http.ClientRequest in node.js
console.error(error.request);
}
else {
// Something happened in setting up the request that triggered an Error
console.error('Error', error.message);
}
2018-03-14 18:48:29 +01:00
});
});
};
OpenVidu.prototype.getBasicAuth = function (secret) {
return 'Basic ' + this.Buffer('OPENVIDUAPP:' + secret).toString('base64');
2018-03-14 18:48:29 +01:00
};
OpenVidu.prototype.setHostnameAndPort = function () {
var urlSplitted = this.urlOpenViduServer.split(':');
2018-05-03 11:48:57 +02:00
if (urlSplitted.length === 3) { // URL has format: http:// + hostname + :port
2018-03-14 18:48:29 +01:00
this.hostname = this.urlOpenViduServer.split(':')[1].replace(/\//g, '');
this.port = parseInt(this.urlOpenViduServer.split(':')[2].replace(/\//g, ''));
}
2018-05-03 11:48:57 +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 + "')");
}
};
OpenVidu.API_RECORDINGS = '/api/recordings';
OpenVidu.API_RECORDINGS_START = '/start';
OpenVidu.API_RECORDINGS_STOP = '/stop';
2017-06-10 01:44:31 +02:00
return OpenVidu;
}());
exports.OpenVidu = OpenVidu;
//# sourceMappingURL=OpenVidu.js.map