Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

DataChannels.js 7.1KB

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