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.

data_channels.js 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /* global connection, 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. /**
  6. * Callback triggered by PeerConnection when new data channel is opened
  7. * on the bridge.
  8. * @param event the event info object.
  9. */
  10. function onDataChannel(event)
  11. {
  12. var dataChannel = event.channel;
  13. dataChannel.onopen = function ()
  14. {
  15. console.info("Data channel opened by the Videobridge!", dataChannel);
  16. // Code sample for sending string and/or binary data
  17. // Sends String message to the bridge
  18. //dataChannel.send("Hello bridge!");
  19. // Sends 12 bytes binary message to the bridge
  20. //dataChannel.send(new ArrayBuffer(12));
  21. // when the data channel becomes available, tell the bridge about video
  22. // selections so that it can do adaptive simulcast,
  23. var userJid = VideoLayout.getLargeVideoState().userJid;
  24. // we want the notification to trigger even if userJid is undefined,
  25. // or null.
  26. onSelectedEndpointChanged(userJid);
  27. };
  28. dataChannel.onerror = function (error)
  29. {
  30. console.error("Data Channel Error:", error, dataChannel);
  31. };
  32. dataChannel.onmessage = function (event)
  33. {
  34. var data = event.data;
  35. // JSON
  36. var obj;
  37. try
  38. {
  39. obj = JSON.parse(data);
  40. }
  41. catch (e)
  42. {
  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. {
  50. var colibriClass = obj.colibriClass;
  51. if ("DominantSpeakerEndpointChangeEvent" === colibriClass)
  52. {
  53. // Endpoint ID from the Videobridge.
  54. var dominantSpeakerEndpoint = obj.dominantSpeakerEndpoint;
  55. console.info(
  56. "Data channel new dominant speaker event: ",
  57. dominantSpeakerEndpoint);
  58. $(document).trigger(
  59. 'dominantspeakerchanged',
  60. [dominantSpeakerEndpoint]);
  61. }
  62. else if ("InLastNChangeEvent" === colibriClass)
  63. {
  64. var oldValue = obj.oldValue;
  65. var newValue = obj.newValue;
  66. // Make sure that oldValue and newValue are of type boolean.
  67. var type;
  68. if ((type = typeof oldValue) !== 'boolean') {
  69. if (type === 'string') {
  70. oldValue = (oldValue == "true");
  71. } else {
  72. oldValue = new Boolean(oldValue).valueOf();
  73. }
  74. }
  75. if ((type = typeof newValue) !== 'boolean') {
  76. if (type === 'string') {
  77. newValue = (newValue == "true");
  78. } else {
  79. newValue = new Boolean(newValue).valueOf();
  80. }
  81. }
  82. $(document).trigger('inlastnchanged', [oldValue, newValue]);
  83. }
  84. else if ("LastNEndpointsChangeEvent" === colibriClass)
  85. {
  86. // The new/latest list of last-n endpoint IDs.
  87. var lastNEndpoints = obj.lastNEndpoints;
  88. // The list of endpoint IDs which are entering the list of
  89. // last-n at this time i.e. were not in the old list of last-n
  90. // endpoint IDs.
  91. var endpointsEnteringLastN = obj.endpointsEnteringLastN;
  92. var stream = obj.stream;
  93. console.log(
  94. "Data channel new last-n event: ",
  95. lastNEndpoints, endpointsEnteringLastN, obj);
  96. $(document).trigger(
  97. 'lastnchanged',
  98. [lastNEndpoints, endpointsEnteringLastN, stream]);
  99. }
  100. else if ("SimulcastLayersChangedEvent" === colibriClass)
  101. {
  102. $(document).trigger(
  103. 'simulcastlayerschanged',
  104. [obj.endpointSimulcastLayers]);
  105. }
  106. else if ("SimulcastLayersChangingEvent" === colibriClass)
  107. {
  108. $(document).trigger(
  109. 'simulcastlayerschanging',
  110. [obj.endpointSimulcastLayers]);
  111. }
  112. else if ("StartSimulcastLayerEvent" === colibriClass)
  113. {
  114. $(document).trigger('startsimulcastlayer', obj.simulcastLayer);
  115. }
  116. else if ("StopSimulcastLayerEvent" === colibriClass)
  117. {
  118. $(document).trigger('stopsimulcastlayer', obj.simulcastLayer);
  119. }
  120. else
  121. {
  122. console.debug("Data channel JSON-formatted message: ", obj);
  123. }
  124. }
  125. };
  126. dataChannel.onclose = function ()
  127. {
  128. console.info("The Data Channel closed", dataChannel);
  129. var idx = _dataChannels.indexOf(dataChannel);
  130. if (idx > -1)
  131. _dataChannels = _dataChannels.splice(idx, 1);
  132. };
  133. _dataChannels.push(dataChannel);
  134. }
  135. /**
  136. * Binds "ondatachannel" event listener to given PeerConnection instance.
  137. * @param peerConnection WebRTC peer connection instance.
  138. */
  139. function bindDataChannelListener(peerConnection)
  140. {
  141. peerConnection.ondatachannel = onDataChannel;
  142. // Sample code for opening new data channel from Jitsi Meet to the bridge.
  143. // Although it's not a requirement to open separate channels from both bridge
  144. // and peer as single channel can be used for sending and receiving data.
  145. // So either channel opened by the bridge or the one opened here is enough
  146. // for communication with the bridge.
  147. /*
  148. var dataChannelOptions = { reliable: true };
  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. }