You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /* global config, APP, Strophe */
  2. // cache datachannels to avoid garbage collection
  3. // https://code.google.com/p/chromium/issues/detail?id=405545
  4. var logger = require("jitsi-meet-logger").getLogger(__filename);
  5. var RTCEvents = require("../../service/RTC/RTCEvents");
  6. /**
  7. * Binds "ondatachannel" event listener to given PeerConnection instance.
  8. * @param peerConnection WebRTC peer connection instance.
  9. */
  10. function DataChannels(peerConnection, emitter) {
  11. peerConnection.ondatachannel = this.onDataChannel.bind(this);
  12. this.eventEmitter = emitter;
  13. this._dataChannels = [];
  14. // Sample code for opening new data channel from Jitsi Meet to the bridge.
  15. // Although it's not a requirement to open separate channels from both bridge
  16. // and peer as single channel can be used for sending and receiving data.
  17. // So either channel opened by the bridge or the one opened here is enough
  18. // for communication with the bridge.
  19. /*var dataChannelOptions =
  20. {
  21. reliable: true
  22. };
  23. var dataChannel
  24. = peerConnection.createDataChannel("myChannel", dataChannelOptions);
  25. // Can be used only when is in open state
  26. dataChannel.onopen = function ()
  27. {
  28. dataChannel.send("My channel !!!");
  29. };
  30. dataChannel.onmessage = function (event)
  31. {
  32. var msgData = event.data;
  33. logger.info("Got My Data Channel Message:", msgData, dataChannel);
  34. };*/
  35. };
  36. /**
  37. * Callback triggered by PeerConnection when new data channel is opened
  38. * on the bridge.
  39. * @param event the event info object.
  40. */
  41. DataChannels.prototype.onDataChannel = function (event) {
  42. var dataChannel = event.channel;
  43. var self = this;
  44. dataChannel.onopen = function () {
  45. logger.info("Data channel opened by the Videobridge!", dataChannel);
  46. // Code sample for sending string and/or binary data
  47. // Sends String message to the bridge
  48. //dataChannel.send("Hello bridge!");
  49. // Sends 12 bytes binary message to the bridge
  50. //dataChannel.send(new ArrayBuffer(12));
  51. self.eventEmitter.emit(RTCEvents.DATA_CHANNEL_OPEN);
  52. };
  53. dataChannel.onerror = function (error) {
  54. logger.error("Data Channel Error:", error, dataChannel);
  55. };
  56. dataChannel.onmessage = function (event) {
  57. var data = event.data;
  58. // JSON
  59. var obj;
  60. try {
  61. obj = JSON.parse(data);
  62. }
  63. catch (e) {
  64. logger.error(
  65. "Failed to parse data channel message as JSON: ",
  66. data,
  67. dataChannel);
  68. }
  69. if (('undefined' !== typeof(obj)) && (null !== obj)) {
  70. var colibriClass = obj.colibriClass;
  71. if ("DominantSpeakerEndpointChangeEvent" === colibriClass) {
  72. // Endpoint ID from the Videobridge.
  73. var dominantSpeakerEndpoint = obj.dominantSpeakerEndpoint;
  74. logger.info(
  75. "Data channel new dominant speaker event: ",
  76. dominantSpeakerEndpoint);
  77. self.eventEmitter.emit(RTCEvents.DOMINANTSPEAKER_CHANGED, dominantSpeakerEndpoint);
  78. }
  79. else if ("InLastNChangeEvent" === colibriClass) {
  80. var oldValue = obj.oldValue;
  81. var newValue = obj.newValue;
  82. // Make sure that oldValue and newValue are of type boolean.
  83. var type;
  84. if ((type = typeof oldValue) !== 'boolean') {
  85. if (type === 'string') {
  86. oldValue = (oldValue == "true");
  87. } else {
  88. oldValue = new Boolean(oldValue).valueOf();
  89. }
  90. }
  91. if ((type = typeof newValue) !== 'boolean') {
  92. if (type === 'string') {
  93. newValue = (newValue == "true");
  94. } else {
  95. newValue = new Boolean(newValue).valueOf();
  96. }
  97. }
  98. self.eventEmitter.emit(RTCEvents.LASTN_CHANGED, oldValue, newValue);
  99. }
  100. else if ("LastNEndpointsChangeEvent" === colibriClass) {
  101. // The new/latest list of last-n endpoint IDs.
  102. var lastNEndpoints = obj.lastNEndpoints;
  103. // The list of endpoint IDs which are entering the list of
  104. // last-n at this time i.e. were not in the old list of last-n
  105. // endpoint IDs.
  106. var endpointsEnteringLastN = obj.endpointsEnteringLastN;
  107. logger.info(
  108. "Data channel new last-n event: ",
  109. lastNEndpoints, endpointsEnteringLastN, obj);
  110. self.eventEmitter.emit(RTCEvents.LASTN_ENDPOINT_CHANGED,
  111. lastNEndpoints, endpointsEnteringLastN, obj);
  112. }
  113. else {
  114. logger.debug("Data channel JSON-formatted message: ", obj);
  115. // The received message appears to be appropriately formatted
  116. // (i.e. is a JSON object which assigns a value to the mandatory
  117. // property colibriClass) so don't just swallow it, expose it to
  118. // public consumption.
  119. self.eventEmitter.emit("rtc.datachannel." + colibriClass, obj);
  120. }
  121. }
  122. };
  123. dataChannel.onclose = function () {
  124. logger.info("The Data Channel closed", dataChannel);
  125. var idx = self._dataChannels.indexOf(dataChannel);
  126. if (idx > -1)
  127. self._dataChannels = self._dataChannels.splice(idx, 1);
  128. };
  129. this._dataChannels.push(dataChannel);
  130. };
  131. DataChannels.prototype.handleSelectedEndpointEvent = function (userResource) {
  132. this._onXXXEndpointChanged("selected", userResource);
  133. }
  134. DataChannels.prototype.handlePinnedEndpointEvent = function (userResource) {
  135. this._onXXXEndpointChanged("pinnned", userResource);
  136. }
  137. /**
  138. * Notifies Videobridge about a change in the value of a specific
  139. * endpoint-related property such as selected endpoint and pinnned endpoint.
  140. *
  141. * @param xxx the name of the endpoint-related property whose value changed
  142. * @param userResource the new value of the endpoint-related property after the
  143. * change
  144. */
  145. DataChannels.prototype._onXXXEndpointChanged = function (xxx, userResource) {
  146. // Derive the correct words from xxx such as selected and Selected, pinned
  147. // and Pinned.
  148. var head = xxx.charAt(0);
  149. var tail = xxx.substring(1);
  150. var lower = head.toLowerCase() + tail;
  151. var upper = head.toUpperCase() + tail;
  152. // Notify Videobridge about the specified endpoint change.
  153. console.log(lower + ' endpoint changed: ', userResource);
  154. this._some(function (dataChannel) {
  155. if (dataChannel.readyState == 'open') {
  156. console.log(
  157. 'sending ' + lower
  158. + ' endpoint changed notification to the bridge: ',
  159. userResource);
  160. var jsonObject = {};
  161. jsonObject.colibriClass = (upper + 'EndpointChangedEvent');
  162. jsonObject[lower + "Endpoint"]
  163. = (userResource ? userResource : null);
  164. dataChannel.send(JSON.stringify(jsonObject));
  165. return true;
  166. }
  167. });
  168. }
  169. DataChannels.prototype._some = function (callback, thisArg) {
  170. var dataChannels = this._dataChannels;
  171. if (dataChannels && dataChannels.length !== 0) {
  172. if (thisArg)
  173. return dataChannels.some(callback, thisArg);
  174. else
  175. return dataChannels.some(callback);
  176. } else {
  177. return false;
  178. }
  179. }
  180. module.exports = DataChannels;