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.

load-test-participant.js 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /* global $, config, JitsiMeetJS */
  2. import 'jquery';
  3. import { setConfigFromURLParams } from '../../react/features/base/config/functions';
  4. import { parseURLParams } from '../../react/features/base/util/parseURLParams';
  5. import { parseURIString } from '../../react/features/base/util/uri';
  6. setConfigFromURLParams(config, {}, {}, window.location);
  7. const params = parseURLParams(window.location, false, 'hash');
  8. const { isHuman = false } = params;
  9. const {
  10. localAudio = config.startWithAudioMuted !== true,
  11. localVideo = config.startWithVideoMuted !== true,
  12. remoteVideo = isHuman,
  13. remoteAudio = isHuman,
  14. autoPlayVideo = config.testing.noAutoPlayVideo !== true,
  15. // Whether to create local audio even if muted
  16. autoCreateLocalAudio = config.testing.noAutoLocalAudio !== true
  17. } = params;
  18. const { room: roomName } = parseURIString(window.location.toString());
  19. let connection = null;
  20. let room = null;
  21. let numParticipants = 1;
  22. let localTracks = [];
  23. const remoteTracks = {};
  24. let maxFrameHeight = 0;
  25. window.APP = {
  26. conference: {
  27. getStats() {
  28. return room.connectionQuality.getStats();
  29. },
  30. getConnectionState() {
  31. return room && room.getConnectionState();
  32. },
  33. muteAudio(mute) {
  34. // Note: will have no effect if !autoCreateLocalAudio
  35. localAudio = mute;
  36. for (let i = 0; i < localTracks.length; i++) {
  37. if (localTracks[i].getType() === 'audio') {
  38. if (mute) {
  39. localTracks[i].mute();
  40. }
  41. else {
  42. localTracks[i].unmute();
  43. }
  44. }
  45. }
  46. }
  47. },
  48. get room() {
  49. return room;
  50. },
  51. get connection() {
  52. return connection;
  53. },
  54. get numParticipants() {
  55. return numParticipants;
  56. },
  57. get localTracks() {
  58. return localTracks;
  59. },
  60. get remoteTracks() {
  61. return remoteTracks;
  62. },
  63. get params() {
  64. return {
  65. roomName,
  66. localAudio,
  67. localVideo,
  68. remoteVideo,
  69. remoteAudio,
  70. autoPlayVideo
  71. };
  72. }
  73. };
  74. /**
  75. * Simple emulation of jitsi-meet's screen layout behavior
  76. */
  77. function updateMaxFrameHeight() {
  78. let newMaxFrameHeight;
  79. if (numParticipants <= 2) {
  80. newMaxFrameHeight = 720;
  81. } else if (numParticipants <= 4) {
  82. newMaxFrameHeight = 360;
  83. } else {
  84. newMaxFrameHeight = 180;
  85. }
  86. if (room && maxFrameHeight !== newMaxFrameHeight) {
  87. maxFrameHeight = newMaxFrameHeight;
  88. room.setReceiverVideoConstraint(maxFrameHeight);
  89. }
  90. }
  91. /**
  92. *
  93. */
  94. function setNumberOfParticipants() {
  95. $('#participants').text(numParticipants);
  96. updateMaxFrameHeight();
  97. }
  98. /**
  99. * Handles local tracks.
  100. * @param tracks Array with JitsiTrack objects
  101. */
  102. function onLocalTracks(tracks = []) {
  103. localTracks = tracks;
  104. for (let i = 0; i < localTracks.length; i++) {
  105. if (localTracks[i].getType() === 'video') {
  106. $('body').append(`<video ${autoPlayVideo ? 'autoplay="1" ' : ''}id='localVideo${i}' />`);
  107. localTracks[i].attach($(`#localVideo${i}`)[0]);
  108. } else {
  109. if (!localAudio) {
  110. localTracks[i].mute();
  111. }
  112. $('body').append(
  113. `<audio autoplay='1' muted='true' id='localAudio${i}' />`);
  114. localTracks[i].attach($(`#localAudio${i}`)[0]);
  115. }
  116. room.addTrack(localTracks[i]);
  117. }
  118. }
  119. /**
  120. * Handles remote tracks
  121. * @param track JitsiTrack object
  122. */
  123. function onRemoteTrack(track) {
  124. if (track.isLocal()
  125. || (track.getType() === 'video' && !remoteVideo) || (track.getType() === 'audio' && !remoteAudio)) {
  126. return;
  127. }
  128. const participant = track.getParticipantId();
  129. if (!remoteTracks[participant]) {
  130. remoteTracks[participant] = [];
  131. }
  132. const idx = remoteTracks[participant].push(track);
  133. const id = participant + track.getType() + idx;
  134. if (track.getType() === 'video') {
  135. $('body').append(`<video autoplay='1' id='${id}' />`);
  136. } else {
  137. $('body').append(`<audio autoplay='1' id='${id}' />`);
  138. }
  139. track.attach($(`#${id}`)[0]);
  140. }
  141. /**
  142. * That function is executed when the conference is joined
  143. */
  144. function onConferenceJoined() {
  145. console.log('Conference joined');
  146. }
  147. /**
  148. * Handles start muted events, when audio and/or video are muted due to
  149. * startAudioMuted or startVideoMuted policy.
  150. */
  151. function onStartMuted() {
  152. // Give it some time, as it may be currently in the process of muting
  153. setTimeout(() => {
  154. const localAudioTrack = room.getLocalAudioTrack();
  155. if (localAudio && localAudioTrack && localAudioTrack.isMuted()) {
  156. localAudioTrack.unmute();
  157. }
  158. const localVideoTrack = room.getLocalVideoTrack();
  159. if (localVideo && localVideoTrack && localVideoTrack.isMuted()) {
  160. localVideoTrack.unmute();
  161. }
  162. }, 2000);
  163. }
  164. /**
  165. *
  166. * @param id
  167. */
  168. function onUserLeft(id) {
  169. numParticipants--;
  170. setNumberOfParticipants();
  171. if (!remoteTracks[id]) {
  172. return;
  173. }
  174. const tracks = remoteTracks[id];
  175. for (let i = 0; i < tracks.length; i++) {
  176. const container = $(`#${id}${tracks[i].getType()}${i + 1}`)[0];
  177. if (container) {
  178. tracks[i].detach(container);
  179. container.parentElement.removeChild(container);
  180. }
  181. }
  182. }
  183. /**
  184. * That function is called when connection is established successfully
  185. */
  186. function onConnectionSuccess() {
  187. room = connection.initJitsiConference(roomName.toLowerCase(), config);
  188. room.on(JitsiMeetJS.events.conference.STARTED_MUTED, onStartMuted);
  189. room.on(JitsiMeetJS.events.conference.TRACK_ADDED, onRemoteTrack);
  190. room.on(JitsiMeetJS.events.conference.CONFERENCE_JOINED, onConferenceJoined);
  191. room.on(JitsiMeetJS.events.conference.USER_JOINED, id => {
  192. numParticipants++;
  193. setNumberOfParticipants();
  194. remoteTracks[id] = [];
  195. });
  196. room.on(JitsiMeetJS.events.conference.USER_LEFT, onUserLeft);
  197. const devices = [];
  198. if (localVideo) {
  199. devices.push('video');
  200. }
  201. if (localAudio || autoCreateLocalAudio) {
  202. devices.push('audio');
  203. }
  204. if (devices.length > 0) {
  205. JitsiMeetJS.createLocalTracks({ devices })
  206. .then(onLocalTracks)
  207. .then(() => {
  208. room.join();
  209. })
  210. .catch(error => {
  211. throw error;
  212. });
  213. } else {
  214. room.join();
  215. }
  216. updateMaxFrameHeight();
  217. }
  218. /**
  219. * This function is called when the connection fail.
  220. */
  221. function onConnectionFailed() {
  222. console.error('Connection Failed!');
  223. }
  224. /**
  225. * This function is called when we disconnect.
  226. */
  227. function disconnect() {
  228. console.log('disconnect!');
  229. connection.removeEventListener(
  230. JitsiMeetJS.events.connection.CONNECTION_ESTABLISHED,
  231. onConnectionSuccess);
  232. connection.removeEventListener(
  233. JitsiMeetJS.events.connection.CONNECTION_FAILED,
  234. onConnectionFailed);
  235. connection.removeEventListener(
  236. JitsiMeetJS.events.connection.CONNECTION_DISCONNECTED,
  237. disconnect);
  238. }
  239. /**
  240. *
  241. */
  242. function unload() {
  243. for (let i = 0; i < localTracks.length; i++) {
  244. localTracks[i].dispose();
  245. }
  246. room.leave();
  247. connection.disconnect();
  248. }
  249. $(window).bind('beforeunload', unload);
  250. $(window).bind('unload', unload);
  251. JitsiMeetJS.setLogLevel(JitsiMeetJS.logLevels.ERROR);
  252. JitsiMeetJS.init(config);
  253. config.serviceUrl = config.bosh = `${config.websocket || config.bosh}?room=${roomName.toLowerCase()}`;
  254. if (config.websocketKeepAliveUrl) {
  255. config.websocketKeepAliveUrl += `?room=${roomName.toLowerCase()}`;
  256. }
  257. connection = new JitsiMeetJS.JitsiConnection(null, null, config);
  258. connection.addEventListener(JitsiMeetJS.events.connection.CONNECTION_ESTABLISHED, onConnectionSuccess);
  259. connection.addEventListener(JitsiMeetJS.events.connection.CONNECTION_FAILED, onConnectionFailed);
  260. connection.addEventListener(JitsiMeetJS.events.connection.CONNECTION_DISCONNECTED, disconnect);
  261. connection.connect();