kurento-backend/static/js/index.js

235 lines
5.3 KiB
JavaScript
Executable File

/*
* (C) Copyright 2014-2015 Kurento (http://kurento.org/)
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Lesser General Public License
* (LGPL) version 2.1 which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/lgpl-2.1.html
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
*/
//var ws = new WebSocket('wss://' + location.host + '/one2many');
var video;
var webRtcPeer;
var socket;
var autoView = true;
var room;
var timerInterceptor = true;
function onError(msg){
console.log(msg);
}
$(function() {
video = $('#video')[0];
let url_string = window.location.href;
let url = new URL(url_string);
room = url.searchParams.get('r');
socket = io('https://gto.to/');
$('#call').on('click', function(e) { presenter(); e.preventDefault(); } );
$('#viewer').on('click', function(e) { viewer(); e.preventDefault(); } );
$('#terminate').on('click', function(e) { stop(); e.preventDefault();} );
socket.on('connect', function(){
console.log('Connected to socket');
socket.emit('subscribeToStream', currentRoom());
// Instant start stream
viewer();
setTimeout(() => {
if(timerInterceptor){
console.log('INTERCEPTOR');
document.location.reload(true);
} else {
console.log('timerInterceptor is false');
}
}, 3000);
});
socket.on('disconnect', function(data){
console.log('Disconnected from socket' + data);
dispose();
});
socket.on('presenterResponse', function(data) {
presenterResponse(data);
});
socket.on('viewerResponse', function(data) {
console.log('viewerResponse');
viewerResponse(data);
});
socket.on('stopCommunication', function(data) {
console.log('stopCommunication');
dispose();
});
socket.on('iceCandidate', function(data) {
console.log('iceCandidate');
timerInterceptor = false;
webRtcPeer.addIceCandidate(data.candidate)
});
socket.on('streamStarted', function(data) {
console.log('streamStarted');
if (autoView) {
viewer();
}
});
socket.on('streamEnded', function(data) {
dispose();
});
});
function presenterResponse(message) {
if (message.response != 'accepted') {
var errorMsg = message.message ? message.message : 'Unknown error';
console.warn('Call not accepted for the following reason: ' + errorMsg);
dispose();
} else {
webRtcPeer.processAnswer(message.sdpAnswer);
}
}
function viewerResponse(message) {
if (message.response != 'accepted') {
var errorMsg = message.message ? message.message : 'Unknown error';
timerInterceptor = false;
console.warn('Call not accepted for the following reason: ' + errorMsg);
dispose();
} else {
console.log('processAnswer');
webRtcPeer.processAnswer(message.sdpAnswer);
}
}
function presenter() {
if (!webRtcPeer) {
showSpinner(video);
var options = {
localVideo: video,
onicecandidate : onIceCandidate
};
webRtcPeer = kurentoUtils.WebRtcPeer.WebRtcPeerSendonly(options, function(error) {
if(error) return onError(error);
this.generateOffer(onOfferPresenter);
});
}
}
function onOfferPresenter(error, offerSdp) {
if (error) return onError(error);
var message = {
sdpOffer : offerSdp,
room: currentRoom()
};
socket.emit('presenter', message);
}
function viewer() {
autoView = true;
if (!webRtcPeer) {
console.log('viewer webRrcPeer');
showSpinner(video);
var options = {
remoteVideo: video,
onicecandidate : onIceCandidate
};
webRtcPeer = kurentoUtils.WebRtcPeer.WebRtcPeerRecvonly(options, function(error) {
if(error) return onError(error);
this.generateOffer(onOfferViewer);
});
}
}
function onOfferViewer(error, offerSdp) {
if (error) return onError(error);
var message = {
sdpOffer : offerSdp,
room: currentRoom()
};
console.log('onOfferVIewer');
socket.emit('viewer', message);
console.log(socket);
console.log(message);
}
function onIceCandidate(candidate) {
//console.log('Local candidate' + JSON.stringify(candidate));
console.log(candidate);
socket.emit('onIceCandidate', {candidate : candidate});
}
function stop() {
autoView = false;
if (webRtcPeer) {
socket.emit('stop');
dispose();
}
}
function dispose() {
if (webRtcPeer) {
webRtcPeer.dispose();
webRtcPeer = null;
}
hideSpinner(video);
}
function sendMessage(payload) {
var event = payload.id,
message = payload;
console.log('Sending message - ' + event + ': ', message);
socket.emit(event, payload);
}
function showSpinner() {
for (var i = 0; i < arguments.length; i++) {
arguments[i].poster = './img/transparent-1px.png';
arguments[i].style.background = 'center transparent url("./img/spinner.gif") no-repeat';
}
}
function hideSpinner() {
for (var i = 0; i < arguments.length; i++) {
arguments[i].src = '';
arguments[i].poster = './img/logo.png';
arguments[i].style.background = '';
}
}
function currentRoom() {
return room;
}
/**
* Lightbox utility (to display media pipeline image in a modal dialog)
*/
$(document).delegate('*[data-toggle="lightbox"]', 'click', function(event) {
event.preventDefault();
$(this).ekkoLightbox();
});