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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. dataChannel.onopen = function () {
  45. logger.info("Data channel opened by the Videobridge!", dataChannel);
  46. // Code sample for sending string and/or binary data
  47. // Sends String message to the bridge
  48. //dataChannel.send("Hello bridge!");
  49. // Sends 12 bytes binary message to the bridge
  50. //dataChannel.send(new ArrayBuffer(12));
  51. self.eventEmitter.emit(RTCEvents.DATA_CHANNEL_OPEN);
  52. };
  53. dataChannel.onerror = function (error) {
  54. logger.error("Data Channel Error:", error, dataChannel);
  55. };
  56. dataChannel.onmessage = function (event) {
  57. var data = event.data;
  58. // JSON
  59. var obj;
  60. try {
  61. obj = JSON.parse(data);
  62. }
  63. catch (e) {
  64. logger.error(
  65. "Failed to parse data channel message as JSON: ",
  66. data,
  67. dataChannel);
  68. }
  69. if (('undefined' !== typeof(obj)) && (null !== obj)) {
  70. var colibriClass = obj.colibriClass;
  71. if ("DominantSpeakerEndpointChangeEvent" === colibriClass) {
  72. // Endpoint ID from the Videobridge.
  73. var dominantSpeakerEndpoint = obj.dominantSpeakerEndpoint;
  74. logger.info(
  75. "Data channel new dominant speaker event: ",
  76. dominantSpeakerEndpoint);
  77. self.eventEmitter.emit(RTCEvents.DOMINANTSPEAKER_CHANGED, dominantSpeakerEndpoint);
  78. }
  79. else if ("InLastNChangeEvent" === colibriClass) {
  80. var oldValue = obj.oldValue;
  81. var newValue = obj.newValue;
  82. // Make sure that oldValue and newValue are of type boolean.
  83. var type;
  84. if ((type = typeof oldValue) !== 'boolean') {
  85. if (type === 'string') {
  86. oldValue = (oldValue == "true");
  87. } else {
  88. oldValue = new Boolean(oldValue).valueOf();
  89. }
  90. }
  91. if ((type = typeof newValue) !== 'boolean') {
  92. if (type === 'string') {
  93. newValue = (newValue == "true");
  94. } else {
  95. newValue = new Boolean(newValue).valueOf();
  96. }
  97. }
  98. self.eventEmitter.emit(RTCEvents.LASTN_CHANGED, oldValue, newValue);
  99. }
  100. else if ("LastNEndpointsChangeEvent" === colibriClass) {
  101. // The new/latest list of last-n endpoint IDs.
  102. var lastNEndpoints = obj.lastNEndpoints;
  103. // The list of endpoint IDs which are entering the list of
  104. // last-n at this time i.e. were not in the old list of last-n
  105. // endpoint IDs.
  106. var endpointsEnteringLastN = obj.endpointsEnteringLastN;
  107. logger.info(
  108. "Data channel new last-n event: ",
  109. lastNEndpoints, endpointsEnteringLastN, obj);
  110. this.eventEmitter.emit(RTCEvents.LASTN_ENDPOINT_CHANGED,
  111. lastNEndpoints, endpointsEnteringLastN, obj);
  112. }
  113. else {
  114. logger.debug("Data channel JSON-formatted message: ", obj);
  115. }
  116. }
  117. };
  118. dataChannel.onclose = function () {
  119. logger.info("The Data Channel closed", dataChannel);
  120. var idx = self._dataChannels.indexOf(dataChannel);
  121. if (idx > -1)
  122. self._dataChannels = self._dataChannels.splice(idx, 1);
  123. };
  124. this._dataChannels.push(dataChannel);
  125. };
  126. DataChannels.prototype.handleSelectedEndpointEvent = function (userResource) {
  127. logger.log('selected endpoint changed: ', userResource);
  128. if (this._dataChannels && this._dataChannels.length != 0) {
  129. this._dataChannels.some(function (dataChannel) {
  130. if (dataChannel.readyState == 'open') {
  131. logger.log('sending selected endpoint changed ' +
  132. 'notification to the bridge: ', userResource);
  133. dataChannel.send(JSON.stringify({
  134. 'colibriClass': 'SelectedEndpointChangedEvent',
  135. 'selectedEndpoint':
  136. (!userResource || userResource === null)?
  137. null : userResource
  138. }));
  139. return true;
  140. }
  141. });
  142. }
  143. }
  144. DataChannels.prototype.handlePinnedEndpointEvent = function (userResource) {
  145. logger.log('pinned endpoint changed: ', userResource);
  146. if (this._dataChannels && this._dataChannels.length !== 0) {
  147. this._dataChannels.some(function (dataChannel) {
  148. if (dataChannel.readyState == 'open') {
  149. dataChannel.send(JSON.stringify({
  150. 'colibriClass': 'PinnedEndpointChangedEvent',
  151. 'pinnedEndpoint':
  152. userResource ? userResource : null
  153. }));
  154. return true;
  155. }
  156. });
  157. }
  158. }
  159. module.exports = DataChannels;