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

DataChannels.js 8.4KB

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