kurento-backend/static/js/index.js

210 lines
4.7 KiB
JavaScript
Raw 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-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);
room = url.searchParams.get('room');
console.log(room);
2018-06-06 15:48:51 +00:00
socket = io('http://kurento-prod.fishrungames.com/');
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-05-25 18:58:19 +00:00
2018-05-30 19:21:10 +00:00
socket.on('disconnect', function(){
console.log('Disconnected from socket');
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) {
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) {
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) {
if (autoView) {
viewer();
}
});
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';
console.warn('Call not accepted for the following reason: ' + errorMsg);
dispose();
} else {
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) {
showSpinner(video);
var options = {
remoteVideo: video,
2018-06-06 15:48:51 +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()
};
socket.emit('viewer', message);
}
function onIceCandidate(candidate) {
//console.log('Local candidate' + JSON.stringify(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/webrtc.png';
arguments[i].style.background = '';
}
}
function currentRoom() {
2018-05-30 19:21:10 +00:00
return room;
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();
});