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.

load-test-participant.js 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. } = params;
  16. const { room: roomName } = parseURIString(window.location.toString());
  17. let connection = null;
  18. let isJoined = false;
  19. let room = null;
  20. let numParticipants = 1;
  21. let localTracks = [];
  22. const remoteTracks = {};
  23. window.APP = {
  24. conference: {
  25. getStats() {
  26. return room.connectionQuality.getStats();
  27. },
  28. getConnectionState() {
  29. return room && room.getConnectionState();
  30. }
  31. },
  32. get room() {
  33. return room;
  34. },
  35. get connection() {
  36. return connection;
  37. },
  38. get numParticipants() {
  39. return numParticipants;
  40. },
  41. get localTracks() {
  42. return localTracks;
  43. },
  44. get remoteTracks() {
  45. return remoteTracks;
  46. },
  47. get params() {
  48. return {
  49. roomName,
  50. localAudio,
  51. localVideo,
  52. remoteVideo,
  53. remoteAudio,
  54. autoPlayVideo
  55. };
  56. }
  57. };
  58. /**
  59. *
  60. */
  61. function setNumberOfParticipants() {
  62. $('#participants').text(numParticipants);
  63. }
  64. /**
  65. * Handles local tracks.
  66. * @param tracks Array with JitsiTrack objects
  67. */
  68. function onLocalTracks(tracks = []) {
  69. localTracks = tracks;
  70. for (let i = 0; i < localTracks.length; i++) {
  71. if (localTracks[i].getType() === 'video') {
  72. $('body').append(`<video ${autoPlayVideo ? 'autoplay="1" ' : ''}id='localVideo${i}' />`);
  73. localTracks[i].attach($(`#localVideo${i}`)[0]);
  74. } else {
  75. $('body').append(
  76. `<audio autoplay='1' muted='true' id='localAudio${i}' />`);
  77. localTracks[i].attach($(`#localAudio${i}`)[0]);
  78. }
  79. if (isJoined) {
  80. room.addTrack(localTracks[i]);
  81. }
  82. }
  83. }
  84. /**
  85. * Handles remote tracks
  86. * @param track JitsiTrack object
  87. */
  88. function onRemoteTrack(track) {
  89. if (track.isLocal()
  90. || (track.getType() === 'video' && !remoteVideo) || (track.getType() === 'audio' && !remoteAudio)) {
  91. return;
  92. }
  93. const participant = track.getParticipantId();
  94. if (!remoteTracks[participant]) {
  95. remoteTracks[participant] = [];
  96. }
  97. const idx = remoteTracks[participant].push(track);
  98. const id = participant + track.getType() + idx;
  99. if (track.getType() === 'video') {
  100. $('body').append(`<video autoplay='1' id='${id}' />`);
  101. } else {
  102. $('body').append(`<audio autoplay='1' id='${id}' />`);
  103. }
  104. track.attach($(`#${id}`)[0]);
  105. }
  106. /**
  107. * That function is executed when the conference is joined
  108. */
  109. function onConferenceJoined() {
  110. isJoined = true;
  111. for (let i = 0; i < localTracks.length; i++) {
  112. room.addTrack(localTracks[i]);
  113. }
  114. }
  115. /**
  116. *
  117. * @param id
  118. */
  119. function onUserLeft(id) {
  120. numParticipants--;
  121. setNumberOfParticipants();
  122. if (!remoteTracks[id]) {
  123. return;
  124. }
  125. const tracks = remoteTracks[id];
  126. for (let i = 0; i < tracks.length; i++) {
  127. const container = $(`#${id}${tracks[i].getType()}${i + 1}`)[0];
  128. if (container) {
  129. tracks[i].detach(container);
  130. container.parentElement.removeChild(container);
  131. }
  132. }
  133. }
  134. /**
  135. * That function is called when connection is established successfully
  136. */
  137. function onConnectionSuccess() {
  138. room = connection.initJitsiConference(roomName, config);
  139. room.on(JitsiMeetJS.events.conference.TRACK_ADDED, onRemoteTrack);
  140. room.on(JitsiMeetJS.events.conference.CONFERENCE_JOINED, onConferenceJoined);
  141. room.on(JitsiMeetJS.events.conference.USER_JOINED, id => {
  142. numParticipants++;
  143. setNumberOfParticipants();
  144. remoteTracks[id] = [];
  145. });
  146. room.on(JitsiMeetJS.events.conference.USER_LEFT, onUserLeft);
  147. room.join();
  148. }
  149. /**
  150. * This function is called when the connection fail.
  151. */
  152. function onConnectionFailed() {
  153. console.error('Connection Failed!');
  154. }
  155. /**
  156. * This function is called when we disconnect.
  157. */
  158. function disconnect() {
  159. console.log('disconnect!');
  160. connection.removeEventListener(
  161. JitsiMeetJS.events.connection.CONNECTION_ESTABLISHED,
  162. onConnectionSuccess);
  163. connection.removeEventListener(
  164. JitsiMeetJS.events.connection.CONNECTION_FAILED,
  165. onConnectionFailed);
  166. connection.removeEventListener(
  167. JitsiMeetJS.events.connection.CONNECTION_DISCONNECTED,
  168. disconnect);
  169. }
  170. /**
  171. *
  172. */
  173. function unload() {
  174. for (let i = 0; i < localTracks.length; i++) {
  175. localTracks[i].dispose();
  176. }
  177. room.leave();
  178. connection.disconnect();
  179. }
  180. $(window).bind('beforeunload', unload);
  181. $(window).bind('unload', unload);
  182. JitsiMeetJS.setLogLevel(JitsiMeetJS.logLevels.ERROR);
  183. JitsiMeetJS.init(config);
  184. connection = new JitsiMeetJS.JitsiConnection(null, null, config);
  185. connection.addEventListener(JitsiMeetJS.events.connection.CONNECTION_ESTABLISHED, onConnectionSuccess);
  186. connection.addEventListener(JitsiMeetJS.events.connection.CONNECTION_FAILED, onConnectionFailed);
  187. connection.addEventListener(JitsiMeetJS.events.connection.CONNECTION_DISCONNECTED, disconnect);
  188. connection.connect();
  189. const devices = [];
  190. if (localVideo) {
  191. devices.push('video');
  192. }
  193. if (localAudio) {
  194. devices.push('audio');
  195. }
  196. if (devices.length > 0) {
  197. JitsiMeetJS.createLocalTracks({ devices })
  198. .then(onLocalTracks)
  199. .catch(error => {
  200. throw error;
  201. });
  202. }