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.6KB

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