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.

example.js 9.5KB

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