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

data_channels.js 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. // TODO(gp) we are supposed to tell the bridge about video selections
  22. // so that it can do adaptive simulcast, What if a video selection has
  23. // been made while the data channels are down or broken?
  24. };
  25. dataChannel.onerror = function (error)
  26. {
  27. console.error("Data Channel Error:", error, dataChannel);
  28. };
  29. dataChannel.onmessage = function (event)
  30. {
  31. var data = event.data;
  32. // JSON
  33. var obj;
  34. try
  35. {
  36. obj = JSON.parse(data);
  37. }
  38. catch (e)
  39. {
  40. console.error(
  41. "Failed to parse data channel message as JSON: ",
  42. data,
  43. dataChannel);
  44. }
  45. if (('undefined' !== typeof(obj)) && (null !== obj))
  46. {
  47. var colibriClass = obj.colibriClass;
  48. if ("DominantSpeakerEndpointChangeEvent" === colibriClass)
  49. {
  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 ("LastNEndpointsChangeEvent" === colibriClass)
  60. {
  61. // The new/latest list of last-n endpoint IDs.
  62. var lastNEndpoints = obj.lastNEndpoints;
  63. /*
  64. * The list of endpoint IDs which are entering the list of
  65. * last-n at this time i.e. were not in the old list of last-n
  66. * endpoint IDs.
  67. */
  68. var endpointsEnteringLastN = obj.endpointsEnteringLastN;
  69. var stream = obj.stream;
  70. console.log(
  71. "Data channel new last-n event: ",
  72. lastNEndpoints, endpointsEnteringLastN, obj);
  73. $(document).trigger(
  74. 'lastnchanged',
  75. [lastNEndpoints, endpointsEnteringLastN, stream]);
  76. }
  77. else if ("SimulcastLayersChangedEvent" === colibriClass)
  78. {
  79. var endpointSimulcastLayers = obj.endpointSimulcastLayers;
  80. $(document).trigger('simulcastlayerschanged', [endpointSimulcastLayers]);
  81. }
  82. else if ("StartSimulcastLayerEvent" === colibriClass)
  83. {
  84. var simulcastLayer = obj.simulcastLayer;
  85. $(document).trigger('startsimulcastlayer', simulcastLayer);
  86. }
  87. else if ("StopSimulcastLayerEvent" === colibriClass)
  88. {
  89. var simulcastLayer = obj.simulcastLayer;
  90. $(document).trigger('stopsimulcastlayer', simulcastLayer);
  91. }
  92. else
  93. {
  94. console.debug("Data channel JSON-formatted message: ", obj);
  95. }
  96. }
  97. };
  98. dataChannel.onclose = function ()
  99. {
  100. console.info("The Data Channel closed", dataChannel);
  101. var idx = _dataChannels.indexOf(dataChannel);
  102. if (idx > -1)
  103. _dataChannels = _dataChannels.splice(idx, 1);
  104. };
  105. _dataChannels.push(dataChannel);
  106. }
  107. /**
  108. * Binds "ondatachannel" event listener to given PeerConnection instance.
  109. * @param peerConnection WebRTC peer connection instance.
  110. */
  111. function bindDataChannelListener(peerConnection)
  112. {
  113. peerConnection.ondatachannel = onDataChannel;
  114. // Sample code for opening new data channel from Jitsi Meet to the bridge.
  115. // Although it's not a requirement to open separate channels from both bridge
  116. // and peer as single channel can be used for sending and receiving data.
  117. // So either channel opened by the bridge or the one opened here is enough
  118. // for communication with the bridge.
  119. /*var dataChannelOptions =
  120. {
  121. reliable: true
  122. };
  123. var dataChannel
  124. = peerConnection.createDataChannel("myChannel", dataChannelOptions);
  125. // Can be used only when is in open state
  126. dataChannel.onopen = function ()
  127. {
  128. dataChannel.send("My channel !!!");
  129. };
  130. dataChannel.onmessage = function (event)
  131. {
  132. var msgData = event.data;
  133. console.info("Got My Data Channel Message:", msgData, dataChannel);
  134. };*/
  135. }