Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

DataChannels.js 8.4KB

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