kurento-backend/static/js/index.js

235 lines
5.3 KiB
JavaScript
Raw Permalink Normal View History

2018-05-25 18:58:19 +00:00
/*
* (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;
2018-08-14 18:34:56 +00:00
var timerInterceptor = true;
2018-05-30 19:21:10 +00:00
function onError(msg){
console.log(msg);
}
2018-05-25 18:58:19 +00:00
$(function() {
video = $('#video')[0];
2018-05-30 19:21:10 +00:00
let url_string = window.location.href;
let url = new URL(url_string);
2018-06-18 17:05:47 +00:00
room = url.searchParams.get('r');
2018-05-30 19:21:10 +00:00
2018-06-18 17:05:47 +00:00
socket = io('https://gto.to/');
2018-05-25 18:58:19 +00:00
$('#call').on('click', function(e) { presenter(); e.preventDefault(); } );
$('#viewer').on('click', function(e) { viewer(); e.preventDefault(); } );
$('#terminate').on('click', function(e) { stop(); e.preventDefault();} );
2018-05-30 19:21:10 +00:00
socket.on('connect', function(){
console.log('Connected to socket');
2018-05-25 18:58:19 +00:00
2018-05-30 19:21:10 +00:00
socket.emit('subscribeToStream', currentRoom());
2018-08-14 18:34:56 +00:00
// Instant start stream
viewer();
setTimeout(() => {
if(timerInterceptor){
console.log('INTERCEPTOR');
document.location.reload(true);
} else {
console.log('timerInterceptor is false');
}
2018-08-15 16:25:10 +00:00
}, 3000);
2018-05-30 19:21:10 +00:00
});
2018-05-25 18:58:19 +00:00
2018-08-14 18:34:56 +00:00
socket.on('disconnect', function(data){
console.log('Disconnected from socket' + data);
2018-05-30 19:21:10 +00:00
dispose();
});
2018-05-25 18:58:19 +00:00
2018-05-30 19:21:10 +00:00
socket.on('presenterResponse', function(data) {
presenterResponse(data);
});
2018-05-25 18:58:19 +00:00
2018-05-30 19:21:10 +00:00
socket.on('viewerResponse', function(data) {
2018-08-14 18:34:56 +00:00
console.log('viewerResponse');
2018-05-30 19:21:10 +00:00
viewerResponse(data);
});
2018-05-25 18:58:19 +00:00
2018-05-30 19:21:10 +00:00
socket.on('stopCommunication', function(data) {
console.log('stopCommunication');
dispose();
});
2018-05-25 18:58:19 +00:00
2018-05-30 19:21:10 +00:00
socket.on('iceCandidate', function(data) {
2018-08-14 18:34:56 +00:00
console.log('iceCandidate');
timerInterceptor = false;
2018-05-30 19:21:10 +00:00
webRtcPeer.addIceCandidate(data.candidate)
});
2018-05-25 18:58:19 +00:00
2018-05-30 19:21:10 +00:00
socket.on('streamStarted', function(data) {
2018-08-14 18:34:56 +00:00
console.log('streamStarted');
2018-05-30 19:21:10 +00:00
if (autoView) {
viewer();
}
});
2018-06-21 08:30:11 +00:00
socket.on('streamEnded', function(data) {
dispose();
});
2018-05-25 18:58:19 +00:00
});
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';
2018-08-15 16:25:10 +00:00
timerInterceptor = false;
2018-05-25 18:58:19 +00:00
console.warn('Call not accepted for the following reason: ' + errorMsg);
dispose();
} else {
2018-08-14 18:34:56 +00:00
console.log('processAnswer');
2018-05-25 18:58:19 +00:00
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) {
2018-08-14 18:34:56 +00:00
console.log('viewer webRrcPeer');
2018-05-25 18:58:19 +00:00
showSpinner(video);
var options = {
remoteVideo: video,
2018-06-07 12:49:18 +00:00
onicecandidate : onIceCandidate
2018-05-25 18:58:19 +00:00
};
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()
};
2018-08-14 18:34:56 +00:00
console.log('onOfferVIewer');
2018-05-25 18:58:19 +00:00
socket.emit('viewer', message);
2018-08-14 18:34:56 +00:00
console.log(socket);
console.log(message);
2018-05-25 18:58:19 +00:00
}
function onIceCandidate(candidate) {
//console.log('Local candidate' + JSON.stringify(candidate));
2018-08-14 18:34:56 +00:00
console.log(candidate);
2018-05-25 18:58:19 +00:00
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 = '';
2018-07-24 08:07:42 +00:00
arguments[i].poster = './img/logo.png';
2018-05-25 18:58:19 +00:00
arguments[i].style.background = '';
}
}
function currentRoom() {
2018-05-30 19:21:10 +00:00
return room;
2018-05-25 18:58:19 +00:00
}
2018-06-18 17:05:47 +00:00
2018-05-25 18:58:19 +00:00
/**
* Lightbox utility (to display media pipeline image in a modal dialog)
*/
$(document).delegate('*[data-toggle="lightbox"]', 'click', function(event) {
event.preventDefault();
$(this).ekkoLightbox();
2018-06-07 12:52:54 +00:00
});