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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 largeVideoSrc = $('#largeVideo').attr('src');
  24. var userJid = getJidFromVideoSrc(largeVideoSrc);
  25. // we want the notification to trigger even if userJid is undefined,
  26. // or null.
  27. onSelectedEndpointChanged(userJid);
  28. };
  29. dataChannel.onerror = function (error)
  30. {
  31. console.error("Data Channel Error:", error, dataChannel);
  32. };
  33. dataChannel.onmessage = function (event)
  34. {
  35. var data = event.data;
  36. // JSON
  37. var obj;
  38. try
  39. {
  40. obj = JSON.parse(data);
  41. }
  42. catch (e)
  43. {
  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. {
  51. var colibriClass = obj.colibriClass;
  52. if ("DominantSpeakerEndpointChangeEvent" === colibriClass)
  53. {
  54. // Endpoint ID from the Videobridge.
  55. var dominantSpeakerEndpoint = obj.dominantSpeakerEndpoint;
  56. console.info(
  57. "Data channel new dominant speaker event: ",
  58. dominantSpeakerEndpoint);
  59. $(document).trigger(
  60. 'dominantspeakerchanged',
  61. [dominantSpeakerEndpoint]);
  62. }
  63. else if ("LastNEndpointsChangeEvent" === colibriClass)
  64. {
  65. // The new/latest list of last-n endpoint IDs.
  66. var lastNEndpoints = obj.lastNEndpoints;
  67. /*
  68. * The list of endpoint IDs which are entering the list of
  69. * last-n at this time i.e. were not in the old list of last-n
  70. * endpoint IDs.
  71. */
  72. var endpointsEnteringLastN = obj.endpointsEnteringLastN;
  73. var stream = obj.stream;
  74. console.log(
  75. "Data channel new last-n event: ",
  76. lastNEndpoints, endpointsEnteringLastN, obj);
  77. $(document).trigger(
  78. 'lastnchanged',
  79. [lastNEndpoints, endpointsEnteringLastN, stream]);
  80. }
  81. else if ("SimulcastLayersChangedEvent" === colibriClass)
  82. {
  83. var endpointSimulcastLayers = obj.endpointSimulcastLayers;
  84. $(document).trigger('simulcastlayerschanged', [endpointSimulcastLayers]);
  85. }
  86. else if ("SimulcastLayersChangingEvent" === colibriClass)
  87. {
  88. var endpointSimulcastLayers = obj.endpointSimulcastLayers;
  89. $(document).trigger('simulcastlayerschanging', [endpointSimulcastLayers]);
  90. }
  91. else if ("StartSimulcastLayerEvent" === colibriClass)
  92. {
  93. var simulcastLayer = obj.simulcastLayer;
  94. $(document).trigger('startsimulcastlayer', simulcastLayer);
  95. }
  96. else if ("StopSimulcastLayerEvent" === colibriClass)
  97. {
  98. var simulcastLayer = obj.simulcastLayer;
  99. $(document).trigger('stopsimulcastlayer', simulcastLayer);
  100. }
  101. else
  102. {
  103. console.debug("Data channel JSON-formatted message: ", obj);
  104. }
  105. }
  106. };
  107. dataChannel.onclose = function ()
  108. {
  109. console.info("The Data Channel closed", dataChannel);
  110. var idx = _dataChannels.indexOf(dataChannel);
  111. if (idx > -1)
  112. _dataChannels = _dataChannels.splice(idx, 1);
  113. };
  114. _dataChannels.push(dataChannel);
  115. }
  116. /**
  117. * Binds "ondatachannel" event listener to given PeerConnection instance.
  118. * @param peerConnection WebRTC peer connection instance.
  119. */
  120. function bindDataChannelListener(peerConnection)
  121. {
  122. peerConnection.ondatachannel = onDataChannel;
  123. // Sample code for opening new data channel from Jitsi Meet to the bridge.
  124. // Although it's not a requirement to open separate channels from both bridge
  125. // and peer as single channel can be used for sending and receiving data.
  126. // So either channel opened by the bridge or the one opened here is enough
  127. // for communication with the bridge.
  128. /*var dataChannelOptions =
  129. {
  130. reliable: true
  131. };
  132. var dataChannel
  133. = peerConnection.createDataChannel("myChannel", dataChannelOptions);
  134. // Can be used only when is in open state
  135. dataChannel.onopen = function ()
  136. {
  137. dataChannel.send("My channel !!!");
  138. };
  139. dataChannel.onmessage = function (event)
  140. {
  141. var msgData = event.data;
  142. console.info("Got My Data Channel Message:", msgData, dataChannel);
  143. };*/
  144. }