Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

example.js 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /* global $, JitsiMeetJS */
  2. const options = {
  3. hosts: {
  4. domain: 'jitsi-meet.example.com',
  5. muc: 'conference.jitsi-meet.example.com', // FIXME: use XEP-0030
  6. },
  7. bosh: '//jitsi-meet.example.com/http-bind', // FIXME: use xep-0156 for that
  8. clientNode: 'http://jitsi.org/jitsimeet', // The name of client node advertised in XEP-0115 'c' stanza
  9. };
  10. const confOptions = {
  11. openSctp: true
  12. };
  13. let connection = null;
  14. let isJoined = false;
  15. let room = null;
  16. let localTracks = [];
  17. const remoteTracks = {};
  18. /**
  19. * Handles local tracks.
  20. * @param tracks Array with JitsiTrack objects
  21. */
  22. function onLocalTracks(tracks) {
  23. localTracks = tracks;
  24. for(let i = 0; i < localTracks.length; i++) {
  25. localTracks[i].addEventListener(JitsiMeetJS.events.track.TRACK_AUDIO_LEVEL_CHANGED,
  26. function(audioLevel) {
  27. console.log('Audio Level local: ' + audioLevel);
  28. });
  29. localTracks[i].addEventListener(JitsiMeetJS.events.track.TRACK_MUTE_CHANGED,
  30. function() {
  31. console.log('local track muted');
  32. });
  33. localTracks[i].addEventListener(JitsiMeetJS.events.track.LOCAL_TRACK_STOPPED,
  34. function() {
  35. console.log('local track stoped');
  36. });
  37. localTracks[i].addEventListener(JitsiMeetJS.events.track.TRACK_AUDIO_OUTPUT_CHANGED,
  38. function(deviceId) {
  39. console.log('track audio output device was changed to ' + deviceId);
  40. });
  41. if(localTracks[i].getType() == 'video') {
  42. $('body').append('<video autoplay=\'1\' id=\'localVideo' + i + '\' />');
  43. localTracks[i].attach($('#localVideo' + i)[0]);
  44. } else {
  45. $('body').append('<audio autoplay=\'1\' muted=\'true\' id=\'localAudio' + i + '\' />');
  46. localTracks[i].attach($('#localAudio' + i)[0]);
  47. }
  48. if(isJoined) {
  49. room.addTrack(localTracks[i]);
  50. }
  51. }
  52. }
  53. /**
  54. * Handles remote tracks
  55. * @param track JitsiTrack object
  56. */
  57. function onRemoteTrack(track) {
  58. if(track.isLocal()) {
  59. return;
  60. }
  61. const participant = track.getParticipantId();
  62. if(!remoteTracks[participant]) {
  63. remoteTracks[participant] = [];
  64. }
  65. const idx = remoteTracks[participant].push(track);
  66. track.addEventListener(JitsiMeetJS.events.track.TRACK_AUDIO_LEVEL_CHANGED,
  67. function(audioLevel) {
  68. console.log('Audio Level remote: ' + audioLevel);
  69. });
  70. track.addEventListener(JitsiMeetJS.events.track.TRACK_MUTE_CHANGED,
  71. function() {
  72. console.log('remote track muted');
  73. });
  74. track.addEventListener(JitsiMeetJS.events.track.LOCAL_TRACK_STOPPED,
  75. function() {
  76. console.log('remote track stoped');
  77. });
  78. track.addEventListener(JitsiMeetJS.events.track.TRACK_AUDIO_OUTPUT_CHANGED,
  79. function(deviceId) {
  80. console.log('track audio output device was changed to ' + deviceId);
  81. });
  82. const id = participant + track.getType() + idx;
  83. if(track.getType() == 'video') {
  84. $('body').append('<video autoplay=\'1\' id=\'' + participant + 'video' + idx + '\' />');
  85. } else {
  86. $('body').append('<audio autoplay=\'1\' id=\'' + participant + 'audio' + idx + '\' />');
  87. }
  88. track.attach($('#' + id)[0]);
  89. }
  90. /**
  91. * That function is executed when the conference is joined
  92. */
  93. function onConferenceJoined() {
  94. console.log('conference joined!');
  95. isJoined = true;
  96. for(let i = 0; i < localTracks.length; i++) {
  97. room.addTrack(localTracks[i]);
  98. }
  99. }
  100. function onUserLeft(id) {
  101. console.log('user left');
  102. if(!remoteTracks[id]) {
  103. return;
  104. }
  105. const tracks = remoteTracks[id];
  106. for(let i = 0; i < tracks.length; i++) {
  107. tracks[i].detach($('#' + id + tracks[i].getType()));
  108. }
  109. }
  110. /**
  111. * That function is called when connection is established successfully
  112. */
  113. function onConnectionSuccess() {
  114. room = connection.initJitsiConference('conference', confOptions);
  115. room.on(JitsiMeetJS.events.conference.TRACK_ADDED, onRemoteTrack);
  116. room.on(JitsiMeetJS.events.conference.TRACK_REMOVED, function(track) {
  117. console.log('track removed!!!' + track);
  118. });
  119. room.on(JitsiMeetJS.events.conference.CONFERENCE_JOINED, onConferenceJoined);
  120. room.on(JitsiMeetJS.events.conference.USER_JOINED, function(id) {
  121. console.log('user join'); remoteTracks[id] = [];
  122. });
  123. room.on(JitsiMeetJS.events.conference.USER_LEFT, onUserLeft);
  124. room.on(JitsiMeetJS.events.conference.TRACK_MUTE_CHANGED, function(track) {
  125. console.log(track.getType() + ' - ' + track.isMuted());
  126. });
  127. room.on(JitsiMeetJS.events.conference.DISPLAY_NAME_CHANGED, function(userID, displayName) {
  128. console.log(userID + ' - ' + displayName);
  129. });
  130. room.on(JitsiMeetJS.events.conference.TRACK_AUDIO_LEVEL_CHANGED,
  131. function(userID, audioLevel) {
  132. console.log(userID + ' - ' + audioLevel);
  133. });
  134. room.on(JitsiMeetJS.events.conference.RECORDER_STATE_CHANGED, function() {
  135. console.log(room.isRecordingSupported() + ' - '
  136. + room.getRecordingState() + ' - '
  137. + room.getRecordingURL());
  138. });
  139. room.on(JitsiMeetJS.events.conference.PHONE_NUMBER_CHANGED, function() {
  140. console.log(
  141. room.getPhoneNumber() + ' - '
  142. + room.getPhonePin());
  143. });
  144. room.join();
  145. }
  146. /**
  147. * This function is called when the connection fail.
  148. */
  149. function onConnectionFailed() {
  150. console.error('Connection Failed!');
  151. }
  152. /**
  153. * This function is called when the connection fail.
  154. */
  155. function onDeviceListChanged(devices) {
  156. console.info('current devices', devices);
  157. }
  158. /**
  159. * This function is called when we disconnect.
  160. */
  161. function disconnect() {
  162. console.log('disconnect!');
  163. connection.removeEventListener(JitsiMeetJS.events.connection.CONNECTION_ESTABLISHED, onConnectionSuccess);
  164. connection.removeEventListener(JitsiMeetJS.events.connection.CONNECTION_FAILED, onConnectionFailed);
  165. connection.removeEventListener(JitsiMeetJS.events.connection.CONNECTION_DISCONNECTED, disconnect);
  166. }
  167. function unload() {
  168. for(let i = 0; i < localTracks.length; i++) {
  169. localTracks[i].stop();
  170. }
  171. room.leave();
  172. connection.disconnect();
  173. }
  174. let isVideo = true;
  175. function switchVideo() { // eslint-disable-line no-unused-vars
  176. isVideo = !isVideo;
  177. if(localTracks[1]) {
  178. localTracks[1].dispose();
  179. localTracks.pop();
  180. }
  181. JitsiMeetJS.createLocalTracks({devices: isVideo ? ['video'] : ['desktop']})
  182. .then(function(tracks) {
  183. localTracks.push(tracks[0]);
  184. localTracks[1].addEventListener(JitsiMeetJS.events.track.TRACK_MUTE_CHANGED,
  185. function() {
  186. console.log('local track muted');
  187. });
  188. localTracks[1].addEventListener(JitsiMeetJS.events.track.LOCAL_TRACK_STOPPED,
  189. function() {
  190. console.log('local track stoped');
  191. });
  192. localTracks[1].attach($('#localVideo1')[0]);
  193. room.addTrack(localTracks[1]);
  194. }).catch(function(error) {
  195. console.log(error);
  196. });
  197. }
  198. function changeAudioOutput(selected) { // eslint-disable-line no-unused-vars
  199. JitsiMeetJS.mediaDevices.setAudioOutputDevice(selected.value);
  200. }
  201. $(window).bind('beforeunload', unload);
  202. $(window).bind('unload', unload);
  203. // JitsiMeetJS.setLogLevel(JitsiMeetJS.logLevels.ERROR);
  204. const initOptions = {
  205. disableAudioLevels: true,
  206. // The ID of the jidesha extension for Chrome.
  207. desktopSharingChromeExtId: 'mbocklcggfhnbahlnepmldehdhpjfcjp',
  208. // Whether desktop sharing should be disabled on Chrome.
  209. desktopSharingChromeDisabled: false,
  210. // The media sources to use when using screen sharing with the Chrome
  211. // extension.
  212. desktopSharingChromeSources: ['screen', 'window'],
  213. // Required version of Chrome extension
  214. desktopSharingChromeMinExtVersion: '0.1',
  215. // The ID of the jidesha extension for Firefox. If null, we assume that no
  216. // extension is required.
  217. desktopSharingFirefoxExtId: null,
  218. // Whether desktop sharing should be disabled on Firefox.
  219. desktopSharingFirefoxDisabled: true,
  220. // The maximum version of Firefox which requires a jidesha extension.
  221. // Example: if set to 41, we will require the extension for Firefox versions
  222. // up to and including 41. On Firefox 42 and higher, we will run without the
  223. // extension.
  224. // If set to -1, an extension will be required for all versions of Firefox.
  225. desktopSharingFirefoxMaxVersionExtRequired: -1,
  226. // The URL to the Firefox extension for desktop sharing.
  227. desktopSharingFirefoxExtensionURL: null
  228. };
  229. JitsiMeetJS.init(initOptions).then(function() {
  230. connection = new JitsiMeetJS.JitsiConnection(null, null, options);
  231. connection.addEventListener(JitsiMeetJS.events.connection.CONNECTION_ESTABLISHED, onConnectionSuccess);
  232. connection.addEventListener(JitsiMeetJS.events.connection.CONNECTION_FAILED, onConnectionFailed);
  233. connection.addEventListener(JitsiMeetJS.events.connection.CONNECTION_DISCONNECTED, disconnect);
  234. JitsiMeetJS.mediaDevices.addEventListener(JitsiMeetJS.events.mediaDevices.DEVICE_LIST_CHANGED, onDeviceListChanged);
  235. connection.connect();
  236. JitsiMeetJS.createLocalTracks({devices: ['audio', 'video']})
  237. .then(onLocalTracks).catch(function(error) {
  238. throw error;
  239. });
  240. }).catch(function(error) {
  241. console.log(error);
  242. });
  243. if (JitsiMeetJS.mediaDevices.isDeviceChangeAvailable('output')) {
  244. JitsiMeetJS.mediaDevices.enumerateDevices(function(devices) {
  245. const audioOutputDevices = devices.filter(function(d) {
  246. return d.kind === 'audiooutput';
  247. });
  248. if (audioOutputDevices.length > 1) {
  249. $('#audioOutputSelect').html(
  250. audioOutputDevices.map(function(d) {
  251. return '<option value="' + d.deviceId + '">' + d.label + '</option>';
  252. }).join('\n')
  253. );
  254. $('#audioOutputSelectWrapper').show();
  255. }
  256. });
  257. }