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

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