Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

DataChannels.js 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /* global config, APP, Strophe */
  2. /* jshint -W101 */
  3. // cache datachannels to avoid garbage collection
  4. // https://code.google.com/p/chromium/issues/detail?id=405545
  5. var RTCEvents = require("../../service/RTC/RTCEvents");
  6. var _dataChannels = [];
  7. var eventEmitter = null;
  8. var DataChannels = {
  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. 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. eventEmitter.emit(RTCEvents.DATA_CHANNEL_OPEN);
  24. };
  25. dataChannel.onerror = function (error) {
  26. console.error("Data Channel Error:", error, dataChannel);
  27. };
  28. dataChannel.onmessage = function (event) {
  29. var data = event.data;
  30. // JSON
  31. var obj;
  32. try {
  33. obj = JSON.parse(data);
  34. }
  35. catch (e) {
  36. console.error(
  37. "Failed to parse data channel message as JSON: ",
  38. data,
  39. dataChannel);
  40. }
  41. if (('undefined' !== typeof(obj)) && (null !== obj)) {
  42. var colibriClass = obj.colibriClass;
  43. if ("DominantSpeakerEndpointChangeEvent" === colibriClass) {
  44. // Endpoint ID from the Videobridge.
  45. var dominantSpeakerEndpoint = obj.dominantSpeakerEndpoint;
  46. console.info(
  47. "Data channel new dominant speaker event: ",
  48. dominantSpeakerEndpoint);
  49. eventEmitter.emit(RTCEvents.DOMINANTSPEAKER_CHANGED, dominantSpeakerEndpoint);
  50. }
  51. else if ("InLastNChangeEvent" === colibriClass) {
  52. var oldValue = obj.oldValue;
  53. var newValue = obj.newValue;
  54. // Make sure that oldValue and newValue are of type boolean.
  55. var type;
  56. if ((type = typeof oldValue) !== 'boolean') {
  57. if (type === 'string') {
  58. oldValue = (oldValue == "true");
  59. } else {
  60. oldValue = Boolean(oldValue).valueOf();
  61. }
  62. }
  63. if ((type = typeof newValue) !== 'boolean') {
  64. if (type === 'string') {
  65. newValue = (newValue == "true");
  66. } else {
  67. newValue = Boolean(newValue).valueOf();
  68. }
  69. }
  70. eventEmitter.emit(RTCEvents.LASTN_CHANGED, oldValue, newValue);
  71. }
  72. else if ("LastNEndpointsChangeEvent" === colibriClass) {
  73. // The new/latest list of last-n endpoint IDs.
  74. var lastNEndpoints = obj.lastNEndpoints;
  75. // The list of endpoint IDs which are entering the list of
  76. // last-n at this time i.e. were not in the old list of last-n
  77. // endpoint IDs.
  78. var endpointsEnteringLastN = obj.endpointsEnteringLastN;
  79. console.log(
  80. "Data channel new last-n event: ",
  81. lastNEndpoints, endpointsEnteringLastN, obj);
  82. eventEmitter.emit(RTCEvents.LASTN_ENDPOINT_CHANGED,
  83. lastNEndpoints, endpointsEnteringLastN, obj);
  84. }
  85. else {
  86. console.debug("Data channel JSON-formatted message: ", obj);
  87. // The received message appears to be appropriately
  88. // formatted (i.e. is a JSON object which assigns a value to
  89. // the mandatory property colibriClass) so don't just
  90. // swallow it, expose it to public consumption.
  91. eventEmitter.emit("rtc.datachannel." + colibriClass, obj);
  92. }
  93. }
  94. };
  95. dataChannel.onclose = function () {
  96. console.info("The Data Channel closed", dataChannel);
  97. var idx = _dataChannels.indexOf(dataChannel);
  98. if (idx > -1)
  99. _dataChannels = _dataChannels.splice(idx, 1);
  100. };
  101. _dataChannels.push(dataChannel);
  102. },
  103. /**
  104. * Binds "ondatachannel" event listener to given PeerConnection instance.
  105. * @param peerConnection WebRTC peer connection instance.
  106. */
  107. init: function (peerConnection, emitter) {
  108. if(!config.openSctp)
  109. return;
  110. peerConnection.ondatachannel = this.onDataChannel;
  111. eventEmitter = emitter;
  112. // Sample code for opening new data channel from Jitsi Meet to the bridge.
  113. // Although it's not a requirement to open separate channels from both bridge
  114. // and peer as single channel can be used for sending and receiving data.
  115. // So either channel opened by the bridge or the one opened here is enough
  116. // for communication with the bridge.
  117. /*var dataChannelOptions =
  118. {
  119. reliable: true
  120. };
  121. var dataChannel
  122. = peerConnection.createDataChannel("myChannel", dataChannelOptions);
  123. // Can be used only when is in open state
  124. dataChannel.onopen = function ()
  125. {
  126. dataChannel.send("My channel !!!");
  127. };
  128. dataChannel.onmessage = function (event)
  129. {
  130. var msgData = event.data;
  131. console.info("Got My Data Channel Message:", msgData, dataChannel);
  132. };*/
  133. },
  134. handleSelectedEndpointEvent: function (userResource) {
  135. onXXXEndpointChanged("selected", userResource);
  136. },
  137. handlePinnedEndpointEvent: function (userResource) {
  138. onXXXEndpointChanged("pinned", userResource);
  139. },
  140. some: function (callback, thisArg) {
  141. if (_dataChannels && _dataChannels.length !== 0) {
  142. if (thisArg)
  143. return _dataChannels.some(callback, thisArg);
  144. else
  145. return _dataChannels.some(callback);
  146. } else {
  147. return false;
  148. }
  149. }
  150. };
  151. /**
  152. * Notifies Videobridge about a change in the value of a specific
  153. * endpoint-related property such as selected endpoint and pinnned endpoint.
  154. *
  155. * @param xxx the name of the endpoint-related property whose value changed
  156. * @param userResource the new value of the endpoint-related property after the
  157. * change
  158. */
  159. function onXXXEndpointChanged(xxx, userResource) {
  160. // Derive the correct words from xxx such as selected and Selected, pinned
  161. // and Pinned.
  162. var head = xxx.charAt(0);
  163. var tail = xxx.substring(1);
  164. var lower = head.toLowerCase() + tail;
  165. var upper = head.toUpperCase() + tail;
  166. // Notify Videobridge about the specified endpoint change.
  167. console.log(lower + ' endpoint changed: ', userResource);
  168. DataChannels.some(function (dataChannel) {
  169. if (dataChannel.readyState == 'open') {
  170. console.log(
  171. 'sending ' + lower
  172. + ' endpoint changed notification to the bridge: ',
  173. userResource);
  174. var jsonObject = {};
  175. jsonObject.colibriClass = (upper + 'EndpointChangedEvent');
  176. jsonObject[lower + "Endpoint"]
  177. = (userResource ? userResource : null);
  178. dataChannel.send(JSON.stringify(jsonObject));
  179. return true;
  180. }
  181. });
  182. }
  183. module.exports = DataChannels;