openvidu/openvidu-browser/src/main/resources/ts/OpenVidu/OpenVidu.ts

358 lines
9.7 KiB
TypeScript
Raw Normal View History

/*
* (C) Copyright 2016 OpenVidu (http://kurento.org/)
*
* 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.
*
*/
import { Session, SessionOptions } from './Session';
import { Stream } from './Stream';
import * as RpcBuilder from 'kurento-jsonrpc';
2017-04-12 11:45:08 +02:00
export type Callback<T> = (error?: any, openVidu?: T) => void;
export class OpenVidu {
private session: Session;
private jsonRpcClient: any;
private rpcParams: any;
private callback: Callback<OpenVidu>;
private camera: Stream;
2017-03-28 20:17:44 +02:00
private remoteStreams: Stream[] = [];
2017-04-12 11:45:08 +02:00
constructor(private wsUri: string) {
if (this.wsUri.charAt(wsUri.length - 1) != '/') {
this.wsUri += '/';
}
this.wsUri += 'room';
}
2017-03-28 20:17:44 +02:00
/* NEW METHODS */
initSession(sessionId) {
console.log("Session initialized!");
this.session = new Session(this, sessionId);
return this.session;
}
2017-04-12 00:54:35 +02:00
initPublisherTagged(parentId: string, cameraOptions: any, callback?) {
2017-03-28 20:17:44 +02:00
console.log("Publisher tagged initialized!");
2017-04-03 00:42:50 +02:00
this.getCamera(cameraOptions);
2017-04-12 00:54:35 +02:00
if (callback == null) {
this.camera.requestCameraAccess((error, camera) => {
if (error) {
console.log("Error accessing the camera");
}
else {
this.camera.setVideoElement(this.cameraReady(camera!, parentId));
2017-04-12 00:54:35 +02:00
}
});
return this.camera;
} else {
this.camera.requestCameraAccess((error, camera) => {
2017-04-12 11:45:08 +02:00
if (error) {
2017-04-12 00:54:35 +02:00
callback(error);
}
else {
this.camera.setVideoElement(this.cameraReady(camera!, parentId));
2017-04-12 00:54:35 +02:00
callback(undefined);
}
});
return this.camera;
}
2017-03-28 20:17:44 +02:00
}
2017-04-12 11:45:08 +02:00
cameraReady(camera: Stream, parentId: string) {
this.camera = camera;
let videoElement = this.camera.playOnlyVideo(parentId, null);
2017-04-12 11:45:08 +02:00
this.camera.emitStreamReadyEvent();
return videoElement;
2017-04-12 11:45:08 +02:00
}
2017-04-03 00:42:50 +02:00
initPublisher(cameraOptions: any, callback) {
2017-03-28 20:17:44 +02:00
console.log("Publisher initialized!");
2017-04-03 00:42:50 +02:00
this.getCamera(cameraOptions);
this.camera.requestCameraAccess((error, camera) => {
if (error) callback(error);
else callback(undefined);
2017-03-28 20:17:44 +02:00
});
}
getLocalStream() {
return this.camera;
}
getRemoteStreams() {
return this.remoteStreams;
}
/* NEW METHODS */
getRoom() {
return this.session;
}
2017-04-12 11:45:08 +02:00
connect(callback: Callback<OpenVidu>): void {
this.callback = callback;
2017-04-12 11:45:08 +02:00
this.initJsonRpcClient(this.wsUri);
}
2017-04-12 11:45:08 +02:00
private initJsonRpcClient(wsUri: string): void {
let config = {
heartbeat: 3000,
sendCloseMessage: false,
ws: {
uri: wsUri,
useSockJS: false,
2017-04-12 11:45:08 +02:00
onconnected: this.connectCallback.bind(this),
ondisconnect: this.disconnectCallback.bind(this),
onreconnecting: this.reconnectingCallback.bind(this),
onreconnected: this.reconnectedCallback.bind(this)
},
rpc: {
requestTimeout: 15000,
//notifications
2017-04-12 11:45:08 +02:00
participantJoined: this.onParticipantJoined.bind(this),
participantPublished: this.onParticipantPublished.bind(this),
participantUnpublished: this.onParticipantLeft.bind(this),
participantLeft: this.onParticipantLeft.bind(this),
participantEvicted: this.onParticipantEvicted.bind(this),
sendMessage: this.onNewMessage.bind(this),
iceCandidate: this.iceCandidateEvent.bind(this),
mediaError: this.onMediaError.bind(this),
custonNotification: this.customNotification.bind(this)
}
};
2017-04-12 11:45:08 +02:00
this.jsonRpcClient = new RpcBuilder.clients.JsonRpcClient(config);
}
2017-04-12 11:45:08 +02:00
private customNotification(params) {
if (this.isRoomAvailable()) {
this.session.emitEvent("custom-message-received", [{ params: params }]);
}
}
2017-04-12 11:45:08 +02:00
private connectCallback(error) {
if (error) {
this.callback(error);
} else {
2017-04-12 11:45:08 +02:00
this.callback(null);
}
}
private isRoomAvailable() {
2017-04-12 11:45:08 +02:00
if (this.session !== undefined && this.session instanceof Session) {
return true;
} else {
2017-04-12 11:45:08 +02:00
console.warn('Room instance not found');
return false;
}
}
private disconnectCallback() {
2017-04-12 11:45:08 +02:00
console.log('Websocket connection lost');
if (this.isRoomAvailable()) {
this.session.onLostConnection();
} else {
2017-04-12 11:45:08 +02:00
alert('Connection error. Please reload page.');
}
}
private reconnectingCallback() {
2017-04-12 11:45:08 +02:00
console.log('Websocket connection lost (reconnecting)');
if (this.isRoomAvailable()) {
this.session.onLostConnection();
} else {
2017-04-12 11:45:08 +02:00
alert('Connection error. Please reload page.');
}
}
private reconnectedCallback() {
2017-04-12 11:45:08 +02:00
console.log('Websocket reconnected');
}
2017-04-12 11:45:08 +02:00
private onParticipantJoined(params) {
if (this.isRoomAvailable()) {
this.session.onParticipantJoined(params);
}
}
2017-04-12 11:45:08 +02:00
private onParticipantPublished(params) {
if (this.isRoomAvailable()) {
this.session.onParticipantPublished(params);
}
}
2017-04-12 11:45:08 +02:00
private onParticipantLeft(params) {
if (this.isRoomAvailable()) {
this.session.onParticipantLeft(params);
}
}
2017-04-12 11:45:08 +02:00
private onParticipantEvicted(params) {
if (this.isRoomAvailable()) {
this.session.onParticipantEvicted(params);
}
}
2017-04-12 11:45:08 +02:00
private onNewMessage(params) {
if (this.isRoomAvailable()) {
this.session.onNewMessage(params);
}
}
2017-04-12 11:45:08 +02:00
private iceCandidateEvent(params) {
if (this.isRoomAvailable()) {
this.session.recvIceCandidate(params);
}
}
2017-04-12 11:45:08 +02:00
private onRoomClosed(params) {
if (this.isRoomAvailable()) {
this.session.onRoomClosed(params);
}
}
2017-04-12 11:45:08 +02:00
private onMediaError(params) {
if (this.isRoomAvailable()) {
this.session.onMediaError(params);
}
}
2017-04-12 11:45:08 +02:00
setRpcParams(params: any) {
this.rpcParams = params;
}
2017-04-12 11:45:08 +02:00
sendRequest(method, params, callback?) {
2017-04-12 11:45:08 +02:00
if (params && params instanceof Function) {
callback = params;
params = undefined;
}
params = params || {};
2017-04-12 11:45:08 +02:00
if (this.rpcParams && this.rpcParams !== null && this.rpcParams !== undefined) {
for (let index in this.rpcParams) {
if (this.rpcParams.hasOwnProperty(index)) {
params[index] = this.rpcParams[index];
2017-04-12 11:45:08 +02:00
console.log('RPC param added to request {' + index + ': ' + this.rpcParams[index] + '}');
}
}
}
2017-04-12 11:45:08 +02:00
console.log('Sending request: { method:"' + method + '", params: ' + JSON.stringify(params) + ' }');
2017-04-12 11:45:08 +02:00
this.jsonRpcClient.send(method, params, callback);
}
2017-04-12 11:45:08 +02:00
close(forced) {
if (this.isRoomAvailable()) {
this.session.leave(forced, this.jsonRpcClient);
}
};
2017-04-12 11:45:08 +02:00
disconnectParticipant(stream) {
if (this.isRoomAvailable()) {
this.session.disconnect(stream);
}
}
getCamera(options?) {
2017-04-12 11:45:08 +02:00
if (this.camera) {
return this.camera;
}
2017-04-12 11:45:08 +02:00
options = options || {
audio: true,
video: true,
2017-04-12 11:45:08 +02:00
data: true,
mediaConstraints: {
audio: true,
video: { width: { ideal: 1280 } }
}
}
options.participant = this.session.getLocalParticipant();
2017-04-12 11:45:08 +02:00
this.camera = new Stream(this, true, this.session, options);
return this.camera;
};
2017-03-28 20:17:44 +02:00
/*joinSession(options: SessionOptions, callback: Callback<Session>) {
this.session.configure(options);
2017-04-03 00:42:50 +02:00
this.session.connect2();
this.session.addEventListener('room-connected', roomEvent => callback(undefined,this.session));
this.session.addEventListener('error-room', error => callback(error));
return this.session;
2017-03-28 20:17:44 +02:00
};*/
//CHAT
2017-04-12 11:45:08 +02:00
sendMessage(room, user, message) {
this.sendRequest('sendMessage', {
message: message,
userMessage: user,
roomMessage: room
2017-04-12 11:45:08 +02:00
}, function (error, response) {
if (error) {
console.error(error);
}
});
};
2017-04-12 11:45:08 +02:00
sendCustomRequest(params, callback) {
this.sendRequest('customRequest', params, callback);
};
2017-04-12 11:45:08 +02:00
toggleLocalVideoTrack(activate: boolean) {
this.getCamera().getWebRtcPeer().videoEnabled = activate;
}
2017-04-12 11:45:08 +02:00
toggleLocalAudioTrack(activate: boolean) {
this.getCamera().getWebRtcPeer().audioEnabled = activate;
}
publishLocalVideoAudio() {
this.toggleLocalVideoTrack(true);
2017-04-12 11:45:08 +02:00
this.toggleLocalAudioTrack(true);
}
2017-04-12 11:45:08 +02:00
unpublishLocalVideoAudio() {
this.toggleLocalVideoTrack(false);
this.toggleLocalAudioTrack(false);
}
}