Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

DataChannels.js 7.0KB

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