Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

DataChannels.js 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 DataChannels =
  6. {
  7. /**
  8. * Callback triggered by PeerConnection when new data channel is opened
  9. * on the bridge.
  10. * @param event the event info object.
  11. */
  12. onDataChannel: function (event)
  13. {
  14. var dataChannel = event.channel;
  15. dataChannel.onopen = function () {
  16. console.info("Data channel opened by the Videobridge!", dataChannel);
  17. // Code sample for sending string and/or binary data
  18. // Sends String message to the bridge
  19. //dataChannel.send("Hello bridge!");
  20. // Sends 12 bytes binary message to the bridge
  21. //dataChannel.send(new ArrayBuffer(12));
  22. // when the data channel becomes available, tell the bridge about video
  23. // selections so that it can do adaptive simulcast,
  24. // we want the notification to trigger even if userJid is undefined,
  25. // or null.
  26. var userJid = UI.getLargeVideoState().userJid;
  27. // we want the notification to trigger even if userJid is undefined,
  28. // or null.
  29. onSelectedEndpointChanged(userJid);
  30. };
  31. dataChannel.onerror = function (error) {
  32. console.error("Data Channel Error:", error, dataChannel);
  33. };
  34. dataChannel.onmessage = function (event) {
  35. var data = event.data;
  36. // JSON
  37. var obj;
  38. try {
  39. obj = JSON.parse(data);
  40. }
  41. catch (e) {
  42. console.error(
  43. "Failed to parse data channel message as JSON: ",
  44. data,
  45. dataChannel);
  46. }
  47. if (('undefined' !== typeof(obj)) && (null !== obj)) {
  48. var colibriClass = obj.colibriClass;
  49. if ("DominantSpeakerEndpointChangeEvent" === colibriClass) {
  50. // Endpoint ID from the Videobridge.
  51. var dominantSpeakerEndpoint = obj.dominantSpeakerEndpoint;
  52. console.info(
  53. "Data channel new dominant speaker event: ",
  54. dominantSpeakerEndpoint);
  55. $(document).trigger(
  56. 'dominantspeakerchanged',
  57. [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. UI.onLastNChanged(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. $(document).trigger(
  94. 'lastnchanged',
  95. [lastNEndpoints, endpointsEnteringLastN, stream]);
  96. }
  97. else if ("SimulcastLayersChangedEvent" === colibriClass)
  98. {
  99. $(document).trigger(
  100. 'simulcastlayerschanged',
  101. [obj.endpointSimulcastLayers]);
  102. }
  103. else if ("SimulcastLayersChangingEvent" === colibriClass)
  104. {
  105. $(document).trigger(
  106. 'simulcastlayerschanging',
  107. [obj.endpointSimulcastLayers]);
  108. }
  109. else if ("StartSimulcastLayerEvent" === colibriClass)
  110. {
  111. $(document).trigger('startsimulcastlayer', obj.simulcastLayer);
  112. }
  113. else if ("StopSimulcastLayerEvent" === colibriClass)
  114. {
  115. $(document).trigger('stopsimulcastlayer', obj.simulcastLayer);
  116. }
  117. else
  118. {
  119. console.debug("Data channel JSON-formatted message: ", obj);
  120. }
  121. }
  122. };
  123. dataChannel.onclose = function ()
  124. {
  125. console.info("The Data Channel closed", dataChannel);
  126. var idx = _dataChannels.indexOf(dataChannel);
  127. if (idx > -1)
  128. _dataChannels = _dataChannels.splice(idx, 1);
  129. };
  130. _dataChannels.push(dataChannel);
  131. },
  132. /**
  133. * Binds "ondatachannel" event listener to given PeerConnection instance.
  134. * @param peerConnection WebRTC peer connection instance.
  135. */
  136. bindDataChannelListener: function (peerConnection) {
  137. if(!config.openSctp)
  138. return;
  139. peerConnection.ondatachannel = this.onDataChannel;
  140. // Sample code for opening new data channel from Jitsi Meet to the bridge.
  141. // Although it's not a requirement to open separate channels from both bridge
  142. // and peer as single channel can be used for sending and receiving data.
  143. // So either channel opened by the bridge or the one opened here is enough
  144. // for communication with the bridge.
  145. /*var dataChannelOptions =
  146. {
  147. reliable: true
  148. };
  149. var dataChannel
  150. = peerConnection.createDataChannel("myChannel", dataChannelOptions);
  151. // Can be used only when is in open state
  152. dataChannel.onopen = function ()
  153. {
  154. dataChannel.send("My channel !!!");
  155. };
  156. dataChannel.onmessage = function (event)
  157. {
  158. var msgData = event.data;
  159. console.info("Got My Data Channel Message:", msgData, dataChannel);
  160. };*/
  161. }
  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. $(document).bind("selectedendpointchanged", function(event, userResource) {
  183. onSelectedEndpointChanged(userResource);
  184. });
  185. function onPinnedEndpointChanged(userResource)
  186. {
  187. console.log('pinned endpoint changed: ', userResource);
  188. if (_dataChannels && _dataChannels.length != 0)
  189. {
  190. _dataChannels.some(function (dataChannel) {
  191. if (dataChannel.readyState == 'open')
  192. {
  193. dataChannel.send(JSON.stringify({
  194. 'colibriClass': 'PinnedEndpointChangedEvent',
  195. 'pinnedEndpoint':
  196. (!userResource || userResource == null)?
  197. null : userResource
  198. }));
  199. return true;
  200. }
  201. });
  202. }
  203. }
  204. $(document).bind("pinnedendpointchanged", function(event, userResource) {
  205. onPinnedEndpointChanged(userResource);
  206. });
  207. module.exports = DataChannels;