您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

load-test-participant.js 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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 room = null;
  19. let numParticipants = 1;
  20. let localTracks = [];
  21. const remoteTracks = {};
  22. let maxFrameHeight = 0;
  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. * Simple emulation of jitsi-meet's screen layout behavior
  60. */
  61. function updateMaxFrameHeight() {
  62. let newMaxFrameHeight;
  63. if (numParticipants <= 2) {
  64. newMaxFrameHeight = 720;
  65. } else if (numParticipants <= 4) {
  66. newMaxFrameHeight = 360;
  67. } else {
  68. newMaxFrameHeight = 180;
  69. }
  70. if (room && maxFrameHeight !== newMaxFrameHeight) {
  71. maxFrameHeight = newMaxFrameHeight;
  72. room.setReceiverVideoConstraint(maxFrameHeight);
  73. }
  74. }
  75. /**
  76. *
  77. */
  78. function setNumberOfParticipants() {
  79. $('#participants').text(numParticipants);
  80. updateMaxFrameHeight();
  81. }
  82. /**
  83. * Handles local tracks.
  84. * @param tracks Array with JitsiTrack objects
  85. */
  86. function onLocalTracks(tracks = []) {
  87. localTracks = tracks;
  88. for (let i = 0; i < localTracks.length; i++) {
  89. if (localTracks[i].getType() === 'video') {
  90. $('body').append(`<video ${autoPlayVideo ? 'autoplay="1" ' : ''}id='localVideo${i}' />`);
  91. localTracks[i].attach($(`#localVideo${i}`)[0]);
  92. } else {
  93. if (!localAudio) {
  94. localTracks[i].mute();
  95. }
  96. $('body').append(
  97. `<audio autoplay='1' muted='true' id='localAudio${i}' />`);
  98. localTracks[i].attach($(`#localAudio${i}`)[0]);
  99. }
  100. room.addTrack(localTracks[i]);
  101. }
  102. }
  103. /**
  104. * Handles remote tracks
  105. * @param track JitsiTrack object
  106. */
  107. function onRemoteTrack(track) {
  108. if (track.isLocal()
  109. || (track.getType() === 'video' && !remoteVideo) || (track.getType() === 'audio' && !remoteAudio)) {
  110. return;
  111. }
  112. const participant = track.getParticipantId();
  113. if (!remoteTracks[participant]) {
  114. remoteTracks[participant] = [];
  115. }
  116. const idx = remoteTracks[participant].push(track);
  117. const id = participant + track.getType() + idx;
  118. if (track.getType() === 'video') {
  119. $('body').append(`<video autoplay='1' id='${id}' />`);
  120. } else {
  121. $('body').append(`<audio autoplay='1' id='${id}' />`);
  122. }
  123. track.attach($(`#${id}`)[0]);
  124. }
  125. /**
  126. * That function is executed when the conference is joined
  127. */
  128. function onConferenceJoined() {
  129. console.log('Conference joined');
  130. }
  131. /**
  132. *
  133. * @param id
  134. */
  135. function onUserLeft(id) {
  136. numParticipants--;
  137. setNumberOfParticipants();
  138. if (!remoteTracks[id]) {
  139. return;
  140. }
  141. const tracks = remoteTracks[id];
  142. for (let i = 0; i < tracks.length; i++) {
  143. const container = $(`#${id}${tracks[i].getType()}${i + 1}`)[0];
  144. if (container) {
  145. tracks[i].detach(container);
  146. container.parentElement.removeChild(container);
  147. }
  148. }
  149. }
  150. /**
  151. * That function is called when connection is established successfully
  152. */
  153. function onConnectionSuccess() {
  154. room = connection.initJitsiConference(roomName.toLowerCase(), config);
  155. room.on(JitsiMeetJS.events.conference.TRACK_ADDED, onRemoteTrack);
  156. room.on(JitsiMeetJS.events.conference.CONFERENCE_JOINED, onConferenceJoined);
  157. room.on(JitsiMeetJS.events.conference.USER_JOINED, id => {
  158. numParticipants++;
  159. setNumberOfParticipants();
  160. remoteTracks[id] = [];
  161. });
  162. room.on(JitsiMeetJS.events.conference.USER_LEFT, onUserLeft);
  163. const devices = [];
  164. if (localVideo) {
  165. devices.push('video');
  166. }
  167. devices.push('audio');
  168. if (devices.length > 0) {
  169. JitsiMeetJS.createLocalTracks({ devices })
  170. .then(onLocalTracks)
  171. .then(() => {
  172. room.join();
  173. })
  174. .catch(error => {
  175. throw error;
  176. });
  177. } else {
  178. room.join();
  179. }
  180. updateMaxFrameHeight();
  181. }
  182. /**
  183. * This function is called when the connection fail.
  184. */
  185. function onConnectionFailed() {
  186. console.error('Connection Failed!');
  187. }
  188. /**
  189. * This function is called when we disconnect.
  190. */
  191. function disconnect() {
  192. console.log('disconnect!');
  193. connection.removeEventListener(
  194. JitsiMeetJS.events.connection.CONNECTION_ESTABLISHED,
  195. onConnectionSuccess);
  196. connection.removeEventListener(
  197. JitsiMeetJS.events.connection.CONNECTION_FAILED,
  198. onConnectionFailed);
  199. connection.removeEventListener(
  200. JitsiMeetJS.events.connection.CONNECTION_DISCONNECTED,
  201. disconnect);
  202. }
  203. /**
  204. *
  205. */
  206. function unload() {
  207. for (let i = 0; i < localTracks.length; i++) {
  208. localTracks[i].dispose();
  209. }
  210. room.leave();
  211. connection.disconnect();
  212. }
  213. $(window).bind('beforeunload', unload);
  214. $(window).bind('unload', unload);
  215. JitsiMeetJS.setLogLevel(JitsiMeetJS.logLevels.ERROR);
  216. JitsiMeetJS.init(config);
  217. config.serviceUrl = config.bosh = `${config.websocket || config.bosh}?room=${roomName.toLowerCase()}`;
  218. connection = new JitsiMeetJS.JitsiConnection(null, null, config);
  219. connection.addEventListener(JitsiMeetJS.events.connection.CONNECTION_ESTABLISHED, onConnectionSuccess);
  220. connection.addEventListener(JitsiMeetJS.events.connection.CONNECTION_FAILED, onConnectionFailed);
  221. connection.addEventListener(JitsiMeetJS.events.connection.CONNECTION_DISCONNECTED, disconnect);
  222. connection.connect();