123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308 |
- // cache datachannels to avoid garbage collection
- // https://code.google.com/p/chromium/issues/detail?id=405545
-
- const logger = require('jitsi-meet-logger').getLogger(__filename);
- const RTCEvents = require('../../service/RTC/RTCEvents');
- const GlobalOnErrorHandler = require('../util/GlobalOnErrorHandler');
-
- /**
- * Binds "ondatachannel" event listener to given PeerConnection instance.
- * @param peerConnection WebRTC peer connection instance.
- */
- function DataChannels(peerConnection, emitter) {
- peerConnection.ondatachannel = this.onDataChannel.bind(this);
- this.eventEmitter = emitter;
-
- this._dataChannels = [];
-
- // 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.
- /* var dataChannelOptions =
- {
- reliable: true
- };
- var dataChannel
- = peerConnection.createDataChannel("myChannel", dataChannelOptions);
-
- // Can be used only when is in open state
- dataChannel.onopen = function ()
- {
- dataChannel.send("My channel !!!");
- };
- dataChannel.onmessage = function (event)
- {
- var msgData = event.data;
- logger.info("Got My Data Channel Message:", msgData, dataChannel);
- };*/
- }
-
- /**
- * Callback triggered by PeerConnection when new data channel is opened
- * on the bridge.
- * @param event the event info object.
- */
- DataChannels.prototype.onDataChannel = function(event) {
- const dataChannel = event.channel;
- const self = this;
-
- dataChannel.onopen = function() {
- logger.info('Data channel opened by the Videobridge!', dataChannel);
-
- // Code sample for sending string and/or binary data
- // Sends String message to the bridge
- // dataChannel.send("Hello bridge!");
- // Sends 12 bytes binary message to the bridge
- // dataChannel.send(new ArrayBuffer(12));
-
- self.eventEmitter.emit(RTCEvents.DATA_CHANNEL_OPEN);
- };
-
- dataChannel.onerror = function(error) {
- // FIXME: this one seems to be generated a bit too often right now
- // so we are temporarily commenting it before we have more clarity
- // on which of the errors we absolutely need to report
- // GlobalOnErrorHandler.callErrorHandler(
- // new Error("Data Channel Error:" + error));
- logger.error('Data Channel Error:', error, dataChannel);
- };
-
- dataChannel.onmessage = function({ data }) {
- // JSON
- let obj;
-
- try {
- obj = JSON.parse(data);
- } catch (e) {
- GlobalOnErrorHandler.callErrorHandler(e);
- logger.error(
- 'Failed to parse data channel message as JSON: ',
- data,
- dataChannel,
- e);
- }
- if ((typeof obj !== 'undefined') && (obj !== null)) {
- const colibriClass = obj.colibriClass;
-
- if (colibriClass === 'DominantSpeakerEndpointChangeEvent') {
- // Endpoint ID from the Videobridge.
- const dominantSpeakerEndpoint = obj.dominantSpeakerEndpoint;
-
- logger.info(
- 'Data channel new dominant speaker event: ',
- dominantSpeakerEndpoint);
- self.eventEmitter.emit(RTCEvents.DOMINANT_SPEAKER_CHANGED,
- dominantSpeakerEndpoint);
- } else if (colibriClass === 'InLastNChangeEvent') {
- let oldValue = obj.oldValue;
- let newValue = obj.newValue;
-
- // Make sure that oldValue and newValue are of type boolean.
- let type;
-
- if ((type = typeof oldValue) !== 'boolean') {
- if (type === 'string') {
- oldValue = oldValue === 'true';
- } else {
- oldValue = Boolean(oldValue);
- }
- }
- if ((type = typeof newValue) !== 'boolean') {
- if (type === 'string') {
- newValue = newValue === 'true';
- } else {
- newValue = Boolean(newValue);
- }
- }
-
- self.eventEmitter.emit(
- RTCEvents.LASTN_CHANGED,
- oldValue,
- newValue);
- } else if (colibriClass === 'LastNEndpointsChangeEvent') {
- // The new/latest list of last-n endpoint IDs.
- const lastNEndpoints = obj.lastNEndpoints;
-
- // 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.
- const endpointsEnteringLastN = obj.endpointsEnteringLastN;
-
- logger.info(
- 'Data channel new last-n event: ',
- lastNEndpoints, endpointsEnteringLastN, obj);
- self.eventEmitter.emit(RTCEvents.LASTN_ENDPOINT_CHANGED,
- lastNEndpoints, endpointsEnteringLastN, obj);
- } else if (colibriClass === 'EndpointMessage') {
- self.eventEmitter.emit(
- RTCEvents.ENDPOINT_MESSAGE_RECEIVED, obj.from,
- obj.msgPayload);
- } else if (colibriClass
- === 'EndpointConnectivityStatusChangeEvent') {
- const endpoint = obj.endpoint;
- const isActive = obj.active === 'true';
-
- logger.info(
- `Endpoint connection status changed: ${endpoint} active ? ${
- isActive}`);
- self.eventEmitter.emit(RTCEvents.ENDPOINT_CONN_STATUS_CHANGED,
- endpoint, isActive);
- } else {
- logger.debug('Data channel JSON-formatted message: ', obj);
-
- // The received message appears to be appropriately formatted
- // (i.e. is a JSON object which assigns a value to the mandatory
- // property colibriClass) so don't just swallow it, expose it to
- // public consumption.
- self.eventEmitter.emit(`rtc.datachannel.${colibriClass}`, obj);
- }
- }
- };
-
- dataChannel.onclose = function() {
- logger.info('The Data Channel closed', dataChannel);
- const idx = self._dataChannels.indexOf(dataChannel);
-
- if (idx > -1) {
- self._dataChannels = self._dataChannels.splice(idx, 1);
- }
- };
- this._dataChannels.push(dataChannel);
- };
-
- /**
- * Closes all currently opened data channels.
- */
- DataChannels.prototype.closeAllChannels = function() {
- this._dataChannels.forEach(dc => {
- // the DC will be removed from the array on 'onclose' event
- dc.close();
- });
- };
-
- /**
- * Sends a "selected endpoint changed" message via the data channel.
- * @param endpointId {string} the id of the selected endpoint
- * @throws NetworkError or InvalidStateError from RTCDataChannel#send (@see
- * {@link https://developer.mozilla.org/en-US/docs/Web/API/RTCDataChannel/send})
- * or Error with "No opened data channels found!" message.
- */
- DataChannels.prototype.sendSelectedEndpointMessage = function(endpointId) {
- this._onXXXEndpointChanged('selected', endpointId);
- };
-
- /**
- * Sends a "pinned endpoint changed" message via the data channel.
- * @param endpointId {string} the id of the pinned endpoint
- * @throws NetworkError or InvalidStateError from RTCDataChannel#send (@see
- * {@link https://developer.mozilla.org/en-US/docs/Web/API/RTCDataChannel/send})
- * or Error with "No opened data channels found!" message.
- */
- DataChannels.prototype.sendPinnedEndpointMessage = function(endpointId) {
- this._onXXXEndpointChanged('pinned', endpointId);
- };
-
- /**
- * Notifies Videobridge about a change in the value of a specific
- * endpoint-related property such as selected endpoint and pinned endpoint.
- *
- * @param xxx the name of the endpoint-related property whose value changed
- * @param userResource the new value of the endpoint-related property after the
- * change
- * @throws NetworkError or InvalidStateError from RTCDataChannel#send (@see
- * {@link https://developer.mozilla.org/en-US/docs/Web/API/RTCDataChannel/send})
- * or Error with "No opened data channels found!" message.
- */
- DataChannels.prototype._onXXXEndpointChanged = function(xxx, userResource) {
- // Derive the correct words from xxx such as selected and Selected, pinned
- // and Pinned.
- const head = xxx.charAt(0);
- const tail = xxx.substring(1);
- const lower = head.toLowerCase() + tail;
- const upper = head.toUpperCase() + tail;
-
- logger.log(
- `sending ${lower} endpoint changed notification to the bridge: `,
- userResource);
-
- const jsonObject = {};
-
- jsonObject.colibriClass = `${upper}EndpointChangedEvent`;
- jsonObject[`${lower}Endpoint`]
- = userResource ? userResource : null;
-
- this.send(jsonObject);
-
- // Notify Videobridge about the specified endpoint change.
- logger.log(`${lower} endpoint changed: `, userResource);
- };
-
- DataChannels.prototype._some = function(callback, thisArg) {
- const dataChannels = this._dataChannels;
-
- if (dataChannels && dataChannels.length !== 0) {
- if (thisArg) {
- return dataChannels.some(callback, thisArg);
- }
-
- return dataChannels.some(callback);
-
- }
-
- return false;
-
- };
-
- /**
- * Sends passed object via the first found open datachannel
- * @param jsonObject {object} the object that will be sent
- * @throws NetworkError or InvalidStateError from RTCDataChannel#send (@see
- * {@link https://developer.mozilla.org/en-US/docs/Web/API/RTCDataChannel/send})
- * or Error with "No opened data channels found!" message.
- */
- DataChannels.prototype.send = function(jsonObject) {
- if (!this._some(dataChannel => {
- if (dataChannel.readyState === 'open') {
- dataChannel.send(JSON.stringify(jsonObject));
-
- return true;
- }
- })) {
- throw new Error('No opened data channels found!');
- }
- };
-
- /**
- * Sends message via the datachannels.
- * @param to {string} the id of the endpoint that should receive the message.
- * If "" the message will be sent to all participants.
- * @param payload {object} the payload of the message.
- * @throws NetworkError or InvalidStateError from RTCDataChannel#send (@see
- * {@link https://developer.mozilla.org/en-US/docs/Web/API/RTCDataChannel/send})
- * or Error with "No opened data channels found!" message.
- */
- DataChannels.prototype.sendDataChannelMessage = function(to, payload) {
- this.send({
- colibriClass: 'EndpointMessage',
- to,
- msgPayload: payload
- });
- };
-
- /**
- * Sends a "lastN value changed" message via the data channel.
- * @param value {int} The new value for lastN. -1 means unlimited.
- */
- DataChannels.prototype.sendSetLastNMessage = function(value) {
- const jsonObject = {
- colibriClass: 'LastNChangedEvent',
- lastN: value
- };
-
- this.send(jsonObject);
- logger.log(`Channel lastN set to: ${value}`);
- };
-
- module.exports = DataChannels;
|