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 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /* global Strophe, updateLargeVideo, focusedVideoSrc*/
  2. // cache datachannels to avoid garbage collection
  3. // https://code.google.com/p/chromium/issues/detail?id=405545
  4. var _dataChannels = [];
  5. var eventEmitter = null;
  6. var DataChannels =
  7. {
  8. /**
  9. * Callback triggered by PeerConnection when new data channel is opened
  10. * on the bridge.
  11. * @param event the event info object.
  12. */
  13. onDataChannel: function (event)
  14. {
  15. var dataChannel = event.channel;
  16. dataChannel.onopen = function () {
  17. console.info("Data channel opened by the Videobridge!", dataChannel);
  18. // Code sample for sending string and/or binary data
  19. // Sends String message to the bridge
  20. //dataChannel.send("Hello bridge!");
  21. // Sends 12 bytes binary message to the bridge
  22. //dataChannel.send(new ArrayBuffer(12));
  23. // when the data channel becomes available, tell the bridge about video
  24. // selections so that it can do adaptive simulcast,
  25. // we want the notification to trigger even if userJid is undefined,
  26. // or null.
  27. var userJid = UI.getLargeVideoState().userJid;
  28. // we want the notification to trigger even if userJid is undefined,
  29. // or null.
  30. onSelectedEndpointChanged(userJid);
  31. };
  32. dataChannel.onerror = function (error) {
  33. console.error("Data Channel Error:", error, dataChannel);
  34. };
  35. dataChannel.onmessage = function (event) {
  36. var data = event.data;
  37. // JSON
  38. var obj;
  39. try {
  40. obj = JSON.parse(data);
  41. }
  42. catch (e) {
  43. console.error(
  44. "Failed to parse data channel message as JSON: ",
  45. data,
  46. dataChannel);
  47. }
  48. if (('undefined' !== typeof(obj)) && (null !== obj)) {
  49. var colibriClass = obj.colibriClass;
  50. if ("DominantSpeakerEndpointChangeEvent" === colibriClass) {
  51. // Endpoint ID from the Videobridge.
  52. var dominantSpeakerEndpoint = obj.dominantSpeakerEndpoint;
  53. console.info(
  54. "Data channel new dominant speaker event: ",
  55. dominantSpeakerEndpoint);
  56. eventEmitter.emit(RTC.DOMINANTSPEAKER_CHANGED, dominantSpeakerEndpoint);
  57. }
  58. else if ("InLastNChangeEvent" === colibriClass)
  59. {
  60. var oldValue = obj.oldValue;
  61. var newValue = obj.newValue;
  62. // Make sure that oldValue and newValue are of type boolean.
  63. var type;
  64. if ((type = typeof oldValue) !== 'boolean') {
  65. if (type === 'string') {
  66. oldValue = (oldValue == "true");
  67. } else {
  68. oldValue = new Boolean(oldValue).valueOf();
  69. }
  70. }
  71. if ((type = typeof newValue) !== 'boolean') {
  72. if (type === 'string') {
  73. newValue = (newValue == "true");
  74. } else {
  75. newValue = new Boolean(newValue).valueOf();
  76. }
  77. }
  78. eventEmitter.emit(RTCEvents.LASTN_CHANGED, oldValue, newValue);
  79. }
  80. else if ("LastNEndpointsChangeEvent" === colibriClass)
  81. {
  82. // The new/latest list of last-n endpoint IDs.
  83. var lastNEndpoints = obj.lastNEndpoints;
  84. // The list of endpoint IDs which are entering the list of
  85. // last-n at this time i.e. were not in the old list of last-n
  86. // endpoint IDs.
  87. var endpointsEnteringLastN = obj.endpointsEnteringLastN;
  88. var stream = obj.stream;
  89. console.log(
  90. "Data channel new last-n event: ",
  91. lastNEndpoints, endpointsEnteringLastN, obj);
  92. eventEmitter.emit(RTCEvents.LASTN_ENDPOINT_CHANGED,
  93. lastNEndpoints, endpointsEnteringLastN, obj);
  94. }
  95. else if ("SimulcastLayersChangedEvent" === colibriClass)
  96. {
  97. eventEmitter.emit(RTCEvents.SIMULCAST_LAYER_CHANGED,
  98. obj.endpointSimulcastLayers);
  99. }
  100. else if ("SimulcastLayersChangingEvent" === colibriClass)
  101. {
  102. eventEmitter.emit(RTCEvents.SIMULCAST_LAYER_CHANGING,
  103. obj.endpointSimulcastLayers);
  104. }
  105. else if ("StartSimulcastLayerEvent" === colibriClass)
  106. {
  107. eventEmitter.emit(RTCEvents.SIMULCAST_START, obj.simulcastLayer);
  108. }
  109. else if ("StopSimulcastLayerEvent" === colibriClass)
  110. {
  111. eventEmitter.emit(RTCEvents.SIMULCAST_STOP, obj.simulcastLayer);
  112. }
  113. else
  114. {
  115. console.debug("Data channel JSON-formatted message: ", obj);
  116. }
  117. }
  118. };
  119. dataChannel.onclose = function ()
  120. {
  121. console.info("The Data Channel closed", dataChannel);
  122. var idx = _dataChannels.indexOf(dataChannel);
  123. if (idx > -1)
  124. _dataChannels = _dataChannels.splice(idx, 1);
  125. };
  126. _dataChannels.push(dataChannel);
  127. },
  128. /**
  129. * Binds "ondatachannel" event listener to given PeerConnection instance.
  130. * @param peerConnection WebRTC peer connection instance.
  131. */
  132. init: function (peerConnection, emitter) {
  133. if(!config.openSctp)
  134. return;
  135. peerConnection.ondatachannel = this.onDataChannel;
  136. eventEmitter = emitter;
  137. // Sample code for opening new data channel from Jitsi Meet to the bridge.
  138. // Although it's not a requirement to open separate channels from both bridge
  139. // and peer as single channel can be used for sending and receiving data.
  140. // So either channel opened by the bridge or the one opened here is enough
  141. // for communication with the bridge.
  142. /*var dataChannelOptions =
  143. {
  144. reliable: true
  145. };
  146. var dataChannel
  147. = peerConnection.createDataChannel("myChannel", dataChannelOptions);
  148. // Can be used only when is in open state
  149. dataChannel.onopen = function ()
  150. {
  151. dataChannel.send("My channel !!!");
  152. };
  153. dataChannel.onmessage = function (event)
  154. {
  155. var msgData = event.data;
  156. console.info("Got My Data Channel Message:", msgData, dataChannel);
  157. };*/
  158. },
  159. handleSelectedEndpointEvent: onSelectedEndpointChanged,
  160. handlePinnedEndpointEvent: onPinnedEndpointChanged
  161. };
  162. function onSelectedEndpointChanged(userResource)
  163. {
  164. console.log('selected endpoint changed: ', userResource);
  165. if (_dataChannels && _dataChannels.length != 0)
  166. {
  167. _dataChannels.some(function (dataChannel) {
  168. if (dataChannel.readyState == 'open')
  169. {
  170. dataChannel.send(JSON.stringify({
  171. 'colibriClass': 'SelectedEndpointChangedEvent',
  172. 'selectedEndpoint':
  173. (!userResource || userResource === null)?
  174. null : userResource
  175. }));
  176. return true;
  177. }
  178. });
  179. }
  180. }
  181. function onPinnedEndpointChanged(userResource)
  182. {
  183. console.log('pinned endpoint changed: ', userResource);
  184. if (_dataChannels && _dataChannels.length != 0)
  185. {
  186. _dataChannels.some(function (dataChannel) {
  187. if (dataChannel.readyState == 'open')
  188. {
  189. dataChannel.send(JSON.stringify({
  190. 'colibriClass': 'PinnedEndpointChangedEvent',
  191. 'pinnedEndpoint':
  192. (!userResource || userResource == null)?
  193. null : userResource
  194. }));
  195. return true;
  196. }
  197. });
  198. }
  199. }
  200. module.exports = DataChannels;