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 8.2KB

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