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.

DataChannels.js 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. var lastSelectedEndpoint = null;
  45. dataChannel.onopen = function () {
  46. logger.info("Data channel opened by the Videobridge!", dataChannel);
  47. // Code sample for sending string and/or binary data
  48. // Sends String message to the bridge
  49. //dataChannel.send("Hello bridge!");
  50. // Sends 12 bytes binary message to the bridge
  51. //dataChannel.send(new ArrayBuffer(12));
  52. self.eventEmitter.emit(RTCEvents.DATA_CHANNEL_OPEN);
  53. // when the data channel becomes available, tell the bridge about video
  54. // selections so that it can do adaptive simulcast,
  55. // we want the notification to trigger even if userJid is undefined,
  56. // or null.
  57. self.handleSelectedEndpointEvent(self.lastSelectedEndpoint);
  58. };
  59. dataChannel.onerror = function (error) {
  60. logger.error("Data Channel Error:", error, dataChannel);
  61. };
  62. dataChannel.onmessage = function (event) {
  63. var data = event.data;
  64. // JSON
  65. var obj;
  66. try {
  67. obj = JSON.parse(data);
  68. }
  69. catch (e) {
  70. logger.error(
  71. "Failed to parse data channel message as JSON: ",
  72. data,
  73. dataChannel);
  74. }
  75. if (('undefined' !== typeof(obj)) && (null !== obj)) {
  76. var colibriClass = obj.colibriClass;
  77. if ("DominantSpeakerEndpointChangeEvent" === colibriClass) {
  78. // Endpoint ID from the Videobridge.
  79. var dominantSpeakerEndpoint = obj.dominantSpeakerEndpoint;
  80. logger.info(
  81. "Data channel new dominant speaker event: ",
  82. dominantSpeakerEndpoint);
  83. self.eventEmitter.emit(RTCEvents.DOMINANTSPEAKER_CHANGED, dominantSpeakerEndpoint);
  84. }
  85. else if ("InLastNChangeEvent" === colibriClass) {
  86. var oldValue = obj.oldValue;
  87. var newValue = obj.newValue;
  88. // Make sure that oldValue and newValue are of type boolean.
  89. var type;
  90. if ((type = typeof oldValue) !== 'boolean') {
  91. if (type === 'string') {
  92. oldValue = (oldValue == "true");
  93. } else {
  94. oldValue = new Boolean(oldValue).valueOf();
  95. }
  96. }
  97. if ((type = typeof newValue) !== 'boolean') {
  98. if (type === 'string') {
  99. newValue = (newValue == "true");
  100. } else {
  101. newValue = new Boolean(newValue).valueOf();
  102. }
  103. }
  104. self.eventEmitter.emit(RTCEvents.LASTN_CHANGED, oldValue, newValue);
  105. }
  106. else if ("LastNEndpointsChangeEvent" === colibriClass) {
  107. // The new/latest list of last-n endpoint IDs.
  108. var lastNEndpoints = obj.lastNEndpoints;
  109. // The list of endpoint IDs which are entering the list of
  110. // last-n at this time i.e. were not in the old list of last-n
  111. // endpoint IDs.
  112. var endpointsEnteringLastN = obj.endpointsEnteringLastN;
  113. logger.info(
  114. "Data channel new last-n event: ",
  115. lastNEndpoints, endpointsEnteringLastN, obj);
  116. self.eventEmitter.emit(RTCEvents.LASTN_ENDPOINT_CHANGED,
  117. lastNEndpoints, endpointsEnteringLastN, obj);
  118. }
  119. else {
  120. logger.debug("Data channel JSON-formatted message: ", obj);
  121. // The received message appears to be appropriately formatted
  122. // (i.e. is a JSON object which assigns a value to the mandatory
  123. // property colibriClass) so don't just swallow it, expose it to
  124. // public consumption.
  125. self.eventEmitter.emit("rtc.datachannel." + colibriClass, obj);
  126. }
  127. }
  128. };
  129. dataChannel.onclose = function () {
  130. logger.info("The Data Channel closed", dataChannel);
  131. var idx = self._dataChannels.indexOf(dataChannel);
  132. if (idx > -1)
  133. self._dataChannels = self._dataChannels.splice(idx, 1);
  134. };
  135. this._dataChannels.push(dataChannel);
  136. };
  137. DataChannels.prototype.handleSelectedEndpointEvent = function (userResource) {
  138. this.lastSelectedEndpoint = userResource;
  139. this._onXXXEndpointChanged("selected", userResource);
  140. }
  141. DataChannels.prototype.handlePinnedEndpointEvent = function (userResource) {
  142. this._onXXXEndpointChanged("pinnned", userResource);
  143. }
  144. /**
  145. * Notifies Videobridge about a change in the value of a specific
  146. * endpoint-related property such as selected endpoint and pinnned endpoint.
  147. *
  148. * @param xxx the name of the endpoint-related property whose value changed
  149. * @param userResource the new value of the endpoint-related property after the
  150. * change
  151. */
  152. DataChannels.prototype._onXXXEndpointChanged = function (xxx, userResource) {
  153. // Derive the correct words from xxx such as selected and Selected, pinned
  154. // and Pinned.
  155. var head = xxx.charAt(0);
  156. var tail = xxx.substring(1);
  157. var lower = head.toLowerCase() + tail;
  158. var upper = head.toUpperCase() + tail;
  159. // Notify Videobridge about the specified endpoint change.
  160. console.log(lower + ' endpoint changed: ', userResource);
  161. this._some(function (dataChannel) {
  162. if (dataChannel.readyState == 'open') {
  163. console.log(
  164. 'sending ' + lower
  165. + ' endpoint changed notification to the bridge: ',
  166. userResource);
  167. var jsonObject = {};
  168. jsonObject.colibriClass = (upper + 'EndpointChangedEvent');
  169. jsonObject[lower + "Endpoint"]
  170. = (userResource ? userResource : null);
  171. dataChannel.send(JSON.stringify(jsonObject));
  172. return true;
  173. }
  174. });
  175. }
  176. DataChannels.prototype._some = function (callback, thisArg) {
  177. var dataChannels = this._dataChannels;
  178. if (dataChannels && dataChannels.length !== 0) {
  179. if (thisArg)
  180. return dataChannels.some(callback, thisArg);
  181. else
  182. return dataChannels.some(callback);
  183. } else {
  184. return false;
  185. }
  186. }
  187. module.exports = DataChannels;