選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

DataChannels.js 7.6KB

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