您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

data_channels.js 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 ("LastNEndpointsChangeEvent" === colibriClass)
  63. {
  64. // The new/latest list of last-n endpoint IDs.
  65. var lastNEndpoints = obj.lastNEndpoints;
  66. /*
  67. * The list of endpoint IDs which are entering the list of
  68. * last-n at this time i.e. were not in the old list of last-n
  69. * endpoint IDs.
  70. */
  71. var endpointsEnteringLastN = obj.endpointsEnteringLastN;
  72. var stream = obj.stream;
  73. console.log(
  74. "Data channel new last-n event: ",
  75. lastNEndpoints, endpointsEnteringLastN, obj);
  76. $(document).trigger(
  77. 'lastnchanged',
  78. [lastNEndpoints, endpointsEnteringLastN, stream]);
  79. }
  80. else if ("SimulcastLayersChangedEvent" === colibriClass)
  81. {
  82. var endpointSimulcastLayers = obj.endpointSimulcastLayers;
  83. $(document).trigger('simulcastlayerschanged', [endpointSimulcastLayers]);
  84. }
  85. else if ("SimulcastLayersChangingEvent" === colibriClass)
  86. {
  87. var endpointSimulcastLayers = obj.endpointSimulcastLayers;
  88. $(document).trigger('simulcastlayerschanging', [endpointSimulcastLayers]);
  89. }
  90. else if ("StartSimulcastLayerEvent" === colibriClass)
  91. {
  92. var simulcastLayer = obj.simulcastLayer;
  93. $(document).trigger('startsimulcastlayer', simulcastLayer);
  94. }
  95. else if ("StopSimulcastLayerEvent" === colibriClass)
  96. {
  97. var simulcastLayer = obj.simulcastLayer;
  98. $(document).trigger('stopsimulcastlayer', simulcastLayer);
  99. }
  100. else
  101. {
  102. console.debug("Data channel JSON-formatted message: ", obj);
  103. }
  104. }
  105. };
  106. dataChannel.onclose = function ()
  107. {
  108. console.info("The Data Channel closed", dataChannel);
  109. var idx = _dataChannels.indexOf(dataChannel);
  110. if (idx > -1)
  111. _dataChannels = _dataChannels.splice(idx, 1);
  112. };
  113. _dataChannels.push(dataChannel);
  114. }
  115. /**
  116. * Binds "ondatachannel" event listener to given PeerConnection instance.
  117. * @param peerConnection WebRTC peer connection instance.
  118. */
  119. function bindDataChannelListener(peerConnection)
  120. {
  121. peerConnection.ondatachannel = onDataChannel;
  122. // Sample code for opening new data channel from Jitsi Meet to the bridge.
  123. // Although it's not a requirement to open separate channels from both bridge
  124. // and peer as single channel can be used for sending and receiving data.
  125. // So either channel opened by the bridge or the one opened here is enough
  126. // for communication with the bridge.
  127. /*var dataChannelOptions =
  128. {
  129. reliable: true
  130. };
  131. var dataChannel
  132. = peerConnection.createDataChannel("myChannel", dataChannelOptions);
  133. // Can be used only when is in open state
  134. dataChannel.onopen = function ()
  135. {
  136. dataChannel.send("My channel !!!");
  137. };
  138. dataChannel.onmessage = function (event)
  139. {
  140. var msgData = event.data;
  141. console.info("Got My Data Channel Message:", msgData, dataChannel);
  142. };*/
  143. }