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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 RTCEvents = require("../../service/RTC/RTCEvents");
  5. /**
  6. * Binds "ondatachannel" event listener to given PeerConnection instance.
  7. * @param peerConnection WebRTC peer connection instance.
  8. */
  9. function DataChannels(peerConnection, emitter) {
  10. peerConnection.ondatachannel = this.onDataChannel;
  11. this.eventEmitter = emitter;
  12. this._dataChannels = [];
  13. // Sample code for opening new data channel from Jitsi Meet to the bridge.
  14. // Although it's not a requirement to open separate channels from both bridge
  15. // and peer as single channel can be used for sending and receiving data.
  16. // So either channel opened by the bridge or the one opened here is enough
  17. // for communication with the bridge.
  18. /*var dataChannelOptions =
  19. {
  20. reliable: true
  21. };
  22. var dataChannel
  23. = peerConnection.createDataChannel("myChannel", dataChannelOptions);
  24. // Can be used only when is in open state
  25. dataChannel.onopen = function ()
  26. {
  27. dataChannel.send("My channel !!!");
  28. };
  29. dataChannel.onmessage = function (event)
  30. {
  31. var msgData = event.data;
  32. console.info("Got My Data Channel Message:", msgData, dataChannel);
  33. };*/
  34. };
  35. /**
  36. * Callback triggered by PeerConnection when new data channel is opened
  37. * on the bridge.
  38. * @param event the event info object.
  39. */
  40. DataChannels.prototype.onDataChannel = function (event) {
  41. var dataChannel = event.channel;
  42. var self = this;
  43. dataChannel.onopen = function () {
  44. console.info("Data channel opened by the Videobridge!", dataChannel);
  45. // Code sample for sending string and/or binary data
  46. // Sends String message to the bridge
  47. //dataChannel.send("Hello bridge!");
  48. // Sends 12 bytes binary message to the bridge
  49. //dataChannel.send(new ArrayBuffer(12));
  50. self.eventEmitter.emit(RTCEvents.DATA_CHANNEL_OPEN);
  51. };
  52. dataChannel.onerror = function (error) {
  53. console.error("Data Channel Error:", error, dataChannel);
  54. };
  55. dataChannel.onmessage = function (event) {
  56. var data = event.data;
  57. // JSON
  58. var obj;
  59. try {
  60. obj = JSON.parse(data);
  61. }
  62. catch (e) {
  63. console.error(
  64. "Failed to parse data channel message as JSON: ",
  65. data,
  66. dataChannel);
  67. }
  68. if (('undefined' !== typeof(obj)) && (null !== obj)) {
  69. var colibriClass = obj.colibriClass;
  70. if ("DominantSpeakerEndpointChangeEvent" === colibriClass) {
  71. // Endpoint ID from the Videobridge.
  72. var dominantSpeakerEndpoint = obj.dominantSpeakerEndpoint;
  73. console.info(
  74. "Data channel new dominant speaker event: ",
  75. dominantSpeakerEndpoint);
  76. self.eventEmitter.emit(RTCEvents.DOMINANTSPEAKER_CHANGED, dominantSpeakerEndpoint);
  77. }
  78. else if ("InLastNChangeEvent" === colibriClass) {
  79. var oldValue = obj.oldValue;
  80. var newValue = obj.newValue;
  81. // Make sure that oldValue and newValue are of type boolean.
  82. var type;
  83. if ((type = typeof oldValue) !== 'boolean') {
  84. if (type === 'string') {
  85. oldValue = (oldValue == "true");
  86. } else {
  87. oldValue = new Boolean(oldValue).valueOf();
  88. }
  89. }
  90. if ((type = typeof newValue) !== 'boolean') {
  91. if (type === 'string') {
  92. newValue = (newValue == "true");
  93. } else {
  94. newValue = new Boolean(newValue).valueOf();
  95. }
  96. }
  97. self.eventEmitter.emit(RTCEvents.LASTN_CHANGED, oldValue, newValue);
  98. }
  99. else if ("LastNEndpointsChangeEvent" === colibriClass) {
  100. // The new/latest list of last-n endpoint IDs.
  101. var lastNEndpoints = obj.lastNEndpoints;
  102. // The list of endpoint IDs which are entering the list of
  103. // last-n at this time i.e. were not in the old list of last-n
  104. // endpoint IDs.
  105. var endpointsEnteringLastN = obj.endpointsEnteringLastN;
  106. console.log(
  107. "Data channel new last-n event: ",
  108. lastNEndpoints, endpointsEnteringLastN, obj);
  109. this.eventEmitter.emit(RTCEvents.LASTN_ENDPOINT_CHANGED,
  110. lastNEndpoints, endpointsEnteringLastN, obj);
  111. }
  112. else {
  113. console.debug("Data channel JSON-formatted message: ", obj);
  114. }
  115. }
  116. };
  117. dataChannel.onclose = function () {
  118. console.info("The Data Channel closed", dataChannel);
  119. var idx = self._dataChannels.indexOf(dataChannel);
  120. if (idx > -1)
  121. self._dataChannels = self._dataChannels.splice(idx, 1);
  122. };
  123. this._dataChannels.push(dataChannel);
  124. };
  125. DataChannels.prototype.handleSelectedEndpointEvent = function (userResource) {
  126. console.log('selected endpoint changed: ', userResource);
  127. if (this._dataChannels && this._dataChannels.length != 0) {
  128. this._dataChannels.some(function (dataChannel) {
  129. if (dataChannel.readyState == 'open') {
  130. console.log('sending selected endpoint changed ' +
  131. 'notification to the bridge: ', userResource);
  132. dataChannel.send(JSON.stringify({
  133. 'colibriClass': 'SelectedEndpointChangedEvent',
  134. 'selectedEndpoint':
  135. (!userResource || userResource === null)?
  136. null : userResource
  137. }));
  138. return true;
  139. }
  140. });
  141. }
  142. }
  143. DataChannels.prototype.handlePinnedEndpointEvent = function (userResource) {
  144. console.log('pinned endpoint changed: ', userResource);
  145. if (this._dataChannels && this._dataChannels.length != 0) {
  146. this._dataChannels.some(function (dataChannel) {
  147. if (dataChannel.readyState == 'open') {
  148. dataChannel.send(JSON.stringify({
  149. 'colibriClass': 'PinnedEndpointChangedEvent',
  150. 'pinnedEndpoint':
  151. (!userResource || userResource == null)?
  152. null : userResource
  153. }));
  154. return true;
  155. }
  156. });
  157. }
  158. }
  159. module.exports = DataChannels;