2014-05-19 13:53:45 +00:00
|
|
|
/* global connection, Strophe, updateLargeVideo, focusedVideoSrc*/
|
2014-08-21 15:04:27 +00:00
|
|
|
|
|
|
|
// cache datachannels to avoid garbage collection
|
|
|
|
// https://code.google.com/p/chromium/issues/detail?id=405545
|
|
|
|
var _dataChannels = [];
|
|
|
|
|
2014-05-08 09:26:15 +00:00
|
|
|
/**
|
|
|
|
* Callback triggered by PeerConnection when new data channel is opened
|
|
|
|
* on the bridge.
|
|
|
|
* @param event the event info object.
|
|
|
|
*/
|
2014-08-21 15:04:27 +00:00
|
|
|
|
2014-05-08 09:26:15 +00:00
|
|
|
function onDataChannel(event)
|
|
|
|
{
|
|
|
|
var dataChannel = event.channel;
|
|
|
|
|
|
|
|
dataChannel.onopen = function ()
|
|
|
|
{
|
2014-07-15 22:36:51 +00:00
|
|
|
console.info("Data channel opened by the Videobridge!", dataChannel);
|
2014-05-08 09:26:15 +00:00
|
|
|
|
2014-06-04 09:03:33 +00:00
|
|
|
// Code sample for sending string and/or binary data
|
2014-05-08 09:26:15 +00:00
|
|
|
// Sends String message to the bridge
|
2014-06-04 09:03:33 +00:00
|
|
|
//dataChannel.send("Hello bridge!");
|
2014-05-08 09:26:15 +00:00
|
|
|
// Sends 12 bytes binary message to the bridge
|
2014-06-04 09:03:33 +00:00
|
|
|
//dataChannel.send(new ArrayBuffer(12));
|
2014-09-12 17:54:40 +00:00
|
|
|
|
2014-09-16 13:43:41 +00:00
|
|
|
// when the data channel becomes available, tell the bridge about video
|
|
|
|
// selections so that it can do adaptive simulcast,
|
2014-11-21 13:29:05 +00:00
|
|
|
var userJid = VideoLayout.getLargeVideoState().userJid;
|
2014-09-16 13:43:41 +00:00
|
|
|
// we want the notification to trigger even if userJid is undefined,
|
|
|
|
// or null.
|
|
|
|
onSelectedEndpointChanged(userJid);
|
2014-05-08 09:26:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
dataChannel.onerror = function (error)
|
|
|
|
{
|
|
|
|
console.error("Data Channel Error:", error, dataChannel);
|
|
|
|
};
|
|
|
|
|
|
|
|
dataChannel.onmessage = function (event)
|
|
|
|
{
|
2014-07-15 22:36:51 +00:00
|
|
|
var data = event.data;
|
2014-07-16 14:35:54 +00:00
|
|
|
// JSON
|
|
|
|
var obj;
|
2014-07-15 22:36:51 +00:00
|
|
|
|
2014-07-16 14:35:54 +00:00
|
|
|
try
|
2014-05-13 14:54:35 +00:00
|
|
|
{
|
2014-07-16 14:35:54 +00:00
|
|
|
obj = JSON.parse(data);
|
|
|
|
}
|
|
|
|
catch (e)
|
|
|
|
{
|
|
|
|
console.error(
|
|
|
|
"Failed to parse data channel message as JSON: ",
|
|
|
|
data,
|
|
|
|
dataChannel);
|
2014-05-13 14:54:35 +00:00
|
|
|
}
|
2014-07-16 14:35:54 +00:00
|
|
|
if (('undefined' !== typeof(obj)) && (null !== obj))
|
2014-07-15 22:36:51 +00:00
|
|
|
{
|
2014-07-16 14:35:54 +00:00
|
|
|
var colibriClass = obj.colibriClass;
|
2014-07-15 22:36:51 +00:00
|
|
|
|
2014-07-16 14:35:54 +00:00
|
|
|
if ("DominantSpeakerEndpointChangeEvent" === colibriClass)
|
2014-07-15 22:36:51 +00:00
|
|
|
{
|
2014-07-16 14:35:54 +00:00
|
|
|
// Endpoint ID from the Videobridge.
|
|
|
|
var dominantSpeakerEndpoint = obj.dominantSpeakerEndpoint;
|
|
|
|
|
|
|
|
console.info(
|
|
|
|
"Data channel new dominant speaker event: ",
|
|
|
|
dominantSpeakerEndpoint);
|
|
|
|
$(document).trigger(
|
|
|
|
'dominantspeakerchanged',
|
|
|
|
[dominantSpeakerEndpoint]);
|
2014-07-15 22:36:51 +00:00
|
|
|
}
|
2014-12-11 19:50:08 +00:00
|
|
|
else if ("InLastNChangeEvent" === colibriClass)
|
|
|
|
{
|
|
|
|
var oldValue = obj.oldValue;
|
|
|
|
var newValue = obj.newValue;
|
|
|
|
// Make sure that oldValue and newValue are of type boolean.
|
|
|
|
var type;
|
|
|
|
|
|
|
|
if ((type = typeof oldValue) !== 'boolean') {
|
|
|
|
if (type === 'string') {
|
|
|
|
oldValue = (oldValue == "true");
|
|
|
|
} else {
|
|
|
|
oldValue = new Boolean(oldValue).valueOf();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ((type = typeof newValue) !== 'boolean') {
|
|
|
|
if (type === 'string') {
|
|
|
|
newValue = (newValue == "true");
|
|
|
|
} else {
|
|
|
|
newValue = new Boolean(newValue).valueOf();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$(document).trigger('inlastnchanged', [oldValue, newValue]);
|
|
|
|
}
|
2014-07-16 14:35:54 +00:00
|
|
|
else if ("LastNEndpointsChangeEvent" === colibriClass)
|
2014-07-15 22:36:51 +00:00
|
|
|
{
|
2014-07-16 14:35:54 +00:00
|
|
|
// The new/latest list of last-n endpoint IDs.
|
|
|
|
var lastNEndpoints = obj.lastNEndpoints;
|
2014-12-11 19:50:08 +00:00
|
|
|
// The list of endpoint IDs which are entering the list of
|
|
|
|
// last-n at this time i.e. were not in the old list of last-n
|
|
|
|
// endpoint IDs.
|
2014-07-16 14:35:54 +00:00
|
|
|
var endpointsEnteringLastN = obj.endpointsEnteringLastN;
|
2014-08-22 15:37:11 +00:00
|
|
|
var stream = obj.stream;
|
|
|
|
|
|
|
|
console.log(
|
2014-07-16 14:35:54 +00:00
|
|
|
"Data channel new last-n event: ",
|
2014-08-22 15:37:11 +00:00
|
|
|
lastNEndpoints, endpointsEnteringLastN, obj);
|
|
|
|
$(document).trigger(
|
2014-12-11 19:50:08 +00:00
|
|
|
'lastnchanged',
|
|
|
|
[lastNEndpoints, endpointsEnteringLastN, stream]);
|
2014-07-15 22:36:51 +00:00
|
|
|
}
|
2014-08-21 09:58:33 +00:00
|
|
|
else if ("SimulcastLayersChangedEvent" === colibriClass)
|
|
|
|
{
|
2014-12-11 19:50:08 +00:00
|
|
|
$(document).trigger(
|
|
|
|
'simulcastlayerschanged',
|
|
|
|
[obj.endpointSimulcastLayers]);
|
2014-08-21 09:58:33 +00:00
|
|
|
}
|
2014-10-26 19:57:13 +00:00
|
|
|
else if ("SimulcastLayersChangingEvent" === colibriClass)
|
|
|
|
{
|
2014-12-11 19:50:08 +00:00
|
|
|
$(document).trigger(
|
|
|
|
'simulcastlayerschanging',
|
|
|
|
[obj.endpointSimulcastLayers]);
|
2014-10-26 19:57:13 +00:00
|
|
|
}
|
2014-09-12 17:54:40 +00:00
|
|
|
else if ("StartSimulcastLayerEvent" === colibriClass)
|
|
|
|
{
|
2014-12-11 19:50:08 +00:00
|
|
|
$(document).trigger('startsimulcastlayer', obj.simulcastLayer);
|
2014-09-12 17:54:40 +00:00
|
|
|
}
|
|
|
|
else if ("StopSimulcastLayerEvent" === colibriClass)
|
|
|
|
{
|
2014-12-11 19:50:08 +00:00
|
|
|
$(document).trigger('stopsimulcastlayer', obj.simulcastLayer);
|
2014-09-12 17:54:40 +00:00
|
|
|
}
|
2014-07-16 14:35:54 +00:00
|
|
|
else
|
2014-07-15 22:36:51 +00:00
|
|
|
{
|
|
|
|
console.debug("Data channel JSON-formatted message: ", obj);
|
|
|
|
}
|
|
|
|
}
|
2014-05-08 09:26:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
dataChannel.onclose = function ()
|
|
|
|
{
|
|
|
|
console.info("The Data Channel closed", dataChannel);
|
2014-08-21 15:04:27 +00:00
|
|
|
var idx = _dataChannels.indexOf(dataChannel);
|
|
|
|
if (idx > -1)
|
|
|
|
_dataChannels = _dataChannels.splice(idx, 1);
|
2014-05-08 09:26:15 +00:00
|
|
|
};
|
2014-08-21 15:04:27 +00:00
|
|
|
_dataChannels.push(dataChannel);
|
2014-05-08 09:26:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Binds "ondatachannel" event listener to given PeerConnection instance.
|
|
|
|
* @param peerConnection WebRTC peer connection instance.
|
|
|
|
*/
|
|
|
|
function bindDataChannelListener(peerConnection)
|
|
|
|
{
|
|
|
|
peerConnection.ondatachannel = onDataChannel;
|
|
|
|
|
|
|
|
// Sample code for opening new data channel from Jitsi Meet to the bridge.
|
|
|
|
// Although it's not a requirement to open separate channels from both bridge
|
|
|
|
// and peer as single channel can be used for sending and receiving data.
|
|
|
|
// So either channel opened by the bridge or the one opened here is enough
|
|
|
|
// for communication with the bridge.
|
2014-12-11 19:50:08 +00:00
|
|
|
/*
|
|
|
|
var dataChannelOptions = { reliable: true };
|
2014-05-08 09:26:15 +00:00
|
|
|
var dataChannel
|
|
|
|
= peerConnection.createDataChannel("myChannel", dataChannelOptions);
|
|
|
|
|
|
|
|
// Can be used only when is in open state
|
|
|
|
dataChannel.onopen = function ()
|
|
|
|
{
|
|
|
|
dataChannel.send("My channel !!!");
|
|
|
|
};
|
2014-05-13 14:54:35 +00:00
|
|
|
dataChannel.onmessage = function (event)
|
|
|
|
{
|
|
|
|
var msgData = event.data;
|
|
|
|
console.info("Got My Data Channel Message:", msgData, dataChannel);
|
2014-12-11 19:50:08 +00:00
|
|
|
};
|
|
|
|
*/
|
2014-07-15 22:36:51 +00:00
|
|
|
}
|
|
|
|
|