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.

DataChannels.js 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. eventEmitter.emit(RTCEvents.DATA_CHANNEL_OPEN);
  25. };
  26. dataChannel.onerror = function (error) {
  27. console.error("Data Channel Error:", error, dataChannel);
  28. };
  29. dataChannel.onmessage = function (event) {
  30. var data = event.data;
  31. // JSON
  32. var obj;
  33. try {
  34. obj = JSON.parse(data);
  35. }
  36. catch (e) {
  37. console.error(
  38. "Failed to parse data channel message as JSON: ",
  39. data,
  40. dataChannel);
  41. }
  42. if (('undefined' !== typeof(obj)) && (null !== obj)) {
  43. var colibriClass = obj.colibriClass;
  44. if ("DominantSpeakerEndpointChangeEvent" === colibriClass) {
  45. // Endpoint ID from the Videobridge.
  46. var dominantSpeakerEndpoint = obj.dominantSpeakerEndpoint;
  47. console.info(
  48. "Data channel new dominant speaker event: ",
  49. dominantSpeakerEndpoint);
  50. eventEmitter.emit(RTCEvents.DOMINANTSPEAKER_CHANGED, dominantSpeakerEndpoint);
  51. }
  52. else if ("InLastNChangeEvent" === colibriClass)
  53. {
  54. var oldValue = obj.oldValue;
  55. var newValue = obj.newValue;
  56. // Make sure that oldValue and newValue are of type boolean.
  57. var type;
  58. if ((type = typeof oldValue) !== 'boolean') {
  59. if (type === 'string') {
  60. oldValue = (oldValue == "true");
  61. } else {
  62. oldValue = new Boolean(oldValue).valueOf();
  63. }
  64. }
  65. if ((type = typeof newValue) !== 'boolean') {
  66. if (type === 'string') {
  67. newValue = (newValue == "true");
  68. } else {
  69. newValue = new Boolean(newValue).valueOf();
  70. }
  71. }
  72. eventEmitter.emit(RTCEvents.LASTN_CHANGED, oldValue, newValue);
  73. }
  74. else if ("LastNEndpointsChangeEvent" === colibriClass)
  75. {
  76. // The new/latest list of last-n endpoint IDs.
  77. var lastNEndpoints = obj.lastNEndpoints;
  78. // The list of endpoint IDs which are entering the list of
  79. // last-n at this time i.e. were not in the old list of last-n
  80. // endpoint IDs.
  81. var endpointsEnteringLastN = obj.endpointsEnteringLastN;
  82. var stream = obj.stream;
  83. console.log(
  84. "Data channel new last-n event: ",
  85. lastNEndpoints, endpointsEnteringLastN, obj);
  86. eventEmitter.emit(RTCEvents.LASTN_ENDPOINT_CHANGED,
  87. lastNEndpoints, endpointsEnteringLastN, obj);
  88. }
  89. else
  90. {
  91. console.debug("Data channel JSON-formatted message: ", obj);
  92. }
  93. }
  94. };
  95. dataChannel.onclose = function ()
  96. {
  97. console.info("The Data Channel closed", dataChannel);
  98. var idx = _dataChannels.indexOf(dataChannel);
  99. if (idx > -1)
  100. _dataChannels = _dataChannels.splice(idx, 1);
  101. };
  102. _dataChannels.push(dataChannel);
  103. },
  104. /**
  105. * Binds "ondatachannel" event listener to given PeerConnection instance.
  106. * @param peerConnection WebRTC peer connection instance.
  107. */
  108. init: function (peerConnection, emitter) {
  109. if(!config.openSctp)
  110. return;
  111. peerConnection.ondatachannel = this.onDataChannel;
  112. eventEmitter = emitter;
  113. // Sample code for opening new data channel from Jitsi Meet to the bridge.
  114. // Although it's not a requirement to open separate channels from both bridge
  115. // and peer as single channel can be used for sending and receiving data.
  116. // So either channel opened by the bridge or the one opened here is enough
  117. // for communication with the bridge.
  118. /*var dataChannelOptions =
  119. {
  120. reliable: true
  121. };
  122. var dataChannel
  123. = peerConnection.createDataChannel("myChannel", dataChannelOptions);
  124. // Can be used only when is in open state
  125. dataChannel.onopen = function ()
  126. {
  127. dataChannel.send("My channel !!!");
  128. };
  129. dataChannel.onmessage = function (event)
  130. {
  131. var msgData = event.data;
  132. console.info("Got My Data Channel Message:", msgData, dataChannel);
  133. };*/
  134. },
  135. handleSelectedEndpointEvent: onSelectedEndpointChanged,
  136. handlePinnedEndpointEvent: onPinnedEndpointChanged
  137. };
  138. function onSelectedEndpointChanged(userResource)
  139. {
  140. console.log('selected endpoint changed: ', userResource);
  141. if (_dataChannels && _dataChannels.length != 0)
  142. {
  143. _dataChannels.some(function (dataChannel) {
  144. if (dataChannel.readyState == 'open')
  145. {
  146. console.log('sending selected endpoint changed '
  147. + 'notification to the bridge: ', userResource);
  148. dataChannel.send(JSON.stringify({
  149. 'colibriClass': 'SelectedEndpointChangedEvent',
  150. 'selectedEndpoint':
  151. (!userResource || userResource === null)?
  152. null : userResource
  153. }));
  154. return true;
  155. }
  156. });
  157. }
  158. }
  159. function onPinnedEndpointChanged(userResource)
  160. {
  161. console.log('pinned endpoint changed: ', userResource);
  162. if (_dataChannels && _dataChannels.length != 0)
  163. {
  164. _dataChannels.some(function (dataChannel) {
  165. if (dataChannel.readyState == 'open')
  166. {
  167. dataChannel.send(JSON.stringify({
  168. 'colibriClass': 'PinnedEndpointChangedEvent',
  169. 'pinnedEndpoint':
  170. (!userResource || userResource == null)?
  171. null : userResource
  172. }));
  173. return true;
  174. }
  175. });
  176. }
  177. }
  178. module.exports = DataChannels;