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

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