Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

DataChannels.js 6.6KB

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