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

DataChannels.js 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 eventEmitter = null;
  6. var DataChannels =
  7. {
  8. /**
  9. * Callback triggered by PeerConnection when new data channel is opened
  10. * on the bridge.
  11. * @param event the event info object.
  12. */
  13. onDataChannel: function (event)
  14. {
  15. var dataChannel = event.channel;
  16. dataChannel.onopen = function () {
  17. console.info("Data channel opened by the Videobridge!", dataChannel);
  18. // Code sample for sending string and/or binary data
  19. // Sends String message to the bridge
  20. //dataChannel.send("Hello bridge!");
  21. // Sends 12 bytes binary message to the bridge
  22. //dataChannel.send(new ArrayBuffer(12));
  23. // when the data channel becomes available, tell the bridge about video
  24. // selections so that it can do adaptive simulcast,
  25. // we want the notification to trigger even if userJid is undefined,
  26. // or null.
  27. var userJid = UI.getLargeVideoState().userJid;
  28. // we want the notification to trigger even if userJid is undefined,
  29. // or null.
  30. onSelectedEndpointChanged(userJid);
  31. };
  32. dataChannel.onerror = function (error) {
  33. console.error("Data Channel Error:", error, dataChannel);
  34. };
  35. dataChannel.onmessage = function (event) {
  36. var data = event.data;
  37. // JSON
  38. var obj;
  39. try {
  40. obj = JSON.parse(data);
  41. }
  42. catch (e) {
  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. var colibriClass = obj.colibriClass;
  50. if ("DominantSpeakerEndpointChangeEvent" === colibriClass) {
  51. // Endpoint ID from the Videobridge.
  52. var dominantSpeakerEndpoint = obj.dominantSpeakerEndpoint;
  53. console.info(
  54. "Data channel new dominant speaker event: ",
  55. dominantSpeakerEndpoint);
  56. eventEmitter.emit(RTC.DOMINANTSPEAKER_CHANGED, dominantSpeakerEndpoint);
  57. }
  58. else if ("InLastNChangeEvent" === colibriClass)
  59. {
  60. var oldValue = obj.oldValue;
  61. var newValue = obj.newValue;
  62. // Make sure that oldValue and newValue are of type boolean.
  63. var type;
  64. if ((type = typeof oldValue) !== 'boolean') {
  65. if (type === 'string') {
  66. oldValue = (oldValue == "true");
  67. } else {
  68. oldValue = new Boolean(oldValue).valueOf();
  69. }
  70. }
  71. if ((type = typeof newValue) !== 'boolean') {
  72. if (type === 'string') {
  73. newValue = (newValue == "true");
  74. } else {
  75. newValue = new Boolean(newValue).valueOf();
  76. }
  77. }
  78. eventEmitter.emit(RTCEvents.LASTN_CHANGED, oldValue, newValue);
  79. }
  80. else if ("LastNEndpointsChangeEvent" === colibriClass)
  81. {
  82. // The new/latest list of last-n endpoint IDs.
  83. var lastNEndpoints = obj.lastNEndpoints;
  84. // The list of endpoint IDs which are entering the list of
  85. // last-n at this time i.e. were not in the old list of last-n
  86. // endpoint IDs.
  87. var endpointsEnteringLastN = obj.endpointsEnteringLastN;
  88. var stream = obj.stream;
  89. console.log(
  90. "Data channel new last-n event: ",
  91. lastNEndpoints, endpointsEnteringLastN, obj);
  92. eventEmitter.emit(RTCEvents.LASTN_ENDPOINT_CHANGED,
  93. lastNEndpoints, endpointsEnteringLastN, obj);
  94. }
  95. else if ("SimulcastLayersChangedEvent" === colibriClass)
  96. {
  97. eventEmitter.emit(RTCEvents.SIMULCAST_LAYER_CHANGED,
  98. obj.endpointSimulcastLayers);
  99. }
  100. else if ("SimulcastLayersChangingEvent" === colibriClass)
  101. {
  102. eventEmitter.emit(RTCEvents.SIMULCAST_LAYER_CHANGING,
  103. obj.endpointSimulcastLayers);
  104. }
  105. else if ("StartSimulcastLayerEvent" === colibriClass)
  106. {
  107. eventEmitter.emit(RTCEvents.SIMULCAST_START, obj.simulcastLayer);
  108. }
  109. else if ("StopSimulcastLayerEvent" === colibriClass)
  110. {
  111. eventEmitter.emit(RTCEvents.SIMULCAST_STOP, obj.simulcastLayer);
  112. }
  113. else
  114. {
  115. console.debug("Data channel JSON-formatted message: ", obj);
  116. }
  117. }
  118. };
  119. dataChannel.onclose = function ()
  120. {
  121. console.info("The Data Channel closed", dataChannel);
  122. var idx = _dataChannels.indexOf(dataChannel);
  123. if (idx > -1)
  124. _dataChannels = _dataChannels.splice(idx, 1);
  125. };
  126. _dataChannels.push(dataChannel);
  127. },
  128. /**
  129. * Binds "ondatachannel" event listener to given PeerConnection instance.
  130. * @param peerConnection WebRTC peer connection instance.
  131. */
  132. init: function (peerConnection, emitter) {
  133. if(!config.openSctp)
  134. retrun;
  135. peerConnection.ondatachannel = this.onDataChannel;
  136. eventEmitter = emitter;
  137. // Sample code for opening new data channel from Jitsi Meet to the bridge.
  138. // Although it's not a requirement to open separate channels from both bridge
  139. // and peer as single channel can be used for sending and receiving data.
  140. // So either channel opened by the bridge or the one opened here is enough
  141. // for communication with the bridge.
  142. /*var dataChannelOptions =
  143. {
  144. reliable: true
  145. };
  146. var dataChannel
  147. = peerConnection.createDataChannel("myChannel", dataChannelOptions);
  148. // Can be used only when is in open state
  149. dataChannel.onopen = function ()
  150. {
  151. dataChannel.send("My channel !!!");
  152. };
  153. dataChannel.onmessage = function (event)
  154. {
  155. var msgData = event.data;
  156. console.info("Got My Data Channel Message:", msgData, dataChannel);
  157. };*/
  158. }
  159. }
  160. function onSelectedEndpointChanged(userResource)
  161. {
  162. console.log('selected endpoint changed: ', userResource);
  163. if (_dataChannels && _dataChannels.length != 0)
  164. {
  165. _dataChannels.some(function (dataChannel) {
  166. if (dataChannel.readyState == 'open')
  167. {
  168. dataChannel.send(JSON.stringify({
  169. 'colibriClass': 'SelectedEndpointChangedEvent',
  170. 'selectedEndpoint':
  171. (!userResource || userResource === null)?
  172. null : userResource
  173. }));
  174. return true;
  175. }
  176. });
  177. }
  178. }
  179. $(document).bind("selectedendpointchanged", function(event, userResource) {
  180. onSelectedEndpointChanged(userResource);
  181. });
  182. function onPinnedEndpointChanged(userResource)
  183. {
  184. console.log('pinned endpoint changed: ', userResource);
  185. if (_dataChannels && _dataChannels.length != 0)
  186. {
  187. _dataChannels.some(function (dataChannel) {
  188. if (dataChannel.readyState == 'open')
  189. {
  190. dataChannel.send(JSON.stringify({
  191. 'colibriClass': 'PinnedEndpointChangedEvent',
  192. 'pinnedEndpoint':
  193. (!userResource || userResource == null)?
  194. null : userResource
  195. }));
  196. return true;
  197. }
  198. });
  199. }
  200. }
  201. $(document).bind("pinnedendpointchanged", function(event, userResource) {
  202. onPinnedEndpointChanged(userResource);
  203. });
  204. module.exports = DataChannels;