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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /* global $, JitsiMeetJS */
  2. var 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. 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() {
  140. console.error("Connection Failed!");
  141. }
  142. /**
  143. * This function is called when the connection fail.
  144. */
  145. function onDeviceListChanged(devices) {
  146. console.info('current devices', devices);
  147. }
  148. /**
  149. * This function is called when we disconnect.
  150. */
  151. function disconnect(){
  152. console.log("disconnect!");
  153. connection.removeEventListener(JitsiMeetJS.events.connection.CONNECTION_ESTABLISHED, onConnectionSuccess);
  154. connection.removeEventListener(JitsiMeetJS.events.connection.CONNECTION_FAILED, onConnectionFailed);
  155. connection.removeEventListener(JitsiMeetJS.events.connection.CONNECTION_DISCONNECTED, disconnect);
  156. }
  157. function unload() {
  158. for(var i = 0; i < localTracks.length; i++)
  159. localTracks[i].stop();
  160. room.leave();
  161. connection.disconnect();
  162. }
  163. var isVideo = true;
  164. function switchVideo() { // eslint-disable-line no-unused-vars
  165. isVideo = !isVideo;
  166. if(localTracks[1]) {
  167. localTracks[1].dispose();
  168. localTracks.pop();
  169. }
  170. JitsiMeetJS.createLocalTracks({devices: isVideo? ["video"] : ["desktop"]}).
  171. then(function (tracks) {
  172. localTracks.push(tracks[0]);
  173. localTracks[1].addEventListener(JitsiMeetJS.events.track.TRACK_MUTE_CHANGED,
  174. function () {
  175. console.log("local track muted");
  176. });
  177. localTracks[1].addEventListener(JitsiMeetJS.events.track.LOCAL_TRACK_STOPPED,
  178. function () {
  179. console.log("local track stoped");
  180. });
  181. localTracks[1].attach($("#localVideo1")[0]);
  182. room.addTrack(localTracks[1]);
  183. }).catch(function (error) {
  184. console.log(error);
  185. });
  186. }
  187. function changeAudioOutput(selected) { // eslint-disable-line no-unused-vars
  188. JitsiMeetJS.mediaDevices.setAudioOutputDevice(selected.value);
  189. }
  190. $(window).bind('beforeunload', unload);
  191. $(window).bind('unload', unload);
  192. // JitsiMeetJS.setLogLevel(JitsiMeetJS.logLevels.ERROR);
  193. var initOptions = {
  194. disableAudioLevels: true,
  195. // Desktop sharing method. Can be set to 'ext', 'webrtc' or false to disable.
  196. desktopSharingChromeMethod: 'ext',
  197. // The ID of the jidesha extension for Chrome.
  198. desktopSharingChromeExtId: 'mbocklcggfhnbahlnepmldehdhpjfcjp',
  199. // The media sources to use when using screen sharing with the Chrome
  200. // extension.
  201. desktopSharingChromeSources: ['screen', 'window'],
  202. // Required version of Chrome extension
  203. desktopSharingChromeMinExtVersion: '0.1',
  204. // The ID of the jidesha extension for Firefox. If null, we assume that no
  205. // extension is required.
  206. desktopSharingFirefoxExtId: null,
  207. // Whether desktop sharing should be disabled on Firefox.
  208. desktopSharingFirefoxDisabled: true,
  209. // The maximum version of Firefox which requires a jidesha extension.
  210. // Example: if set to 41, we will require the extension for Firefox versions
  211. // up to and including 41. On Firefox 42 and higher, we will run without the
  212. // extension.
  213. // If set to -1, an extension will be required for all versions of Firefox.
  214. desktopSharingFirefoxMaxVersionExtRequired: -1,
  215. // The URL to the Firefox extension for desktop sharing.
  216. desktopSharingFirefoxExtensionURL: null
  217. };
  218. JitsiMeetJS.init(initOptions).then(function(){
  219. connection = new JitsiMeetJS.JitsiConnection(null, null, options);
  220. connection.addEventListener(JitsiMeetJS.events.connection.CONNECTION_ESTABLISHED, onConnectionSuccess);
  221. connection.addEventListener(JitsiMeetJS.events.connection.CONNECTION_FAILED, onConnectionFailed);
  222. connection.addEventListener(JitsiMeetJS.events.connection.CONNECTION_DISCONNECTED, disconnect);
  223. JitsiMeetJS.mediaDevices.addEventListener(JitsiMeetJS.events.mediaDevices.DEVICE_LIST_CHANGED, onDeviceListChanged);
  224. connection.connect();
  225. JitsiMeetJS.createLocalTracks({devices: ["audio", "video"]}).
  226. then(onLocalTracks).catch(function (error) {
  227. throw error;
  228. });
  229. }).catch(function (error) {
  230. console.log(error);
  231. });
  232. if (JitsiMeetJS.mediaDevices.isDeviceChangeAvailable('output')) {
  233. JitsiMeetJS.mediaDevices.enumerateDevices(function(devices) {
  234. var audioOutputDevices = devices.filter(function(d) { return d.kind === 'audiooutput'; });
  235. if (audioOutputDevices.length > 1) {
  236. $('#audioOutputSelect').html(
  237. audioOutputDevices.map(function (d) {
  238. return '<option value="' + d.deviceId + '">' + d.label + '</option>';
  239. }).join('\n')
  240. );
  241. $('#audioOutputSelectWrapper').show();
  242. }
  243. });
  244. }
  245. var connection = null;
  246. var room = null;
  247. var localTracks = [];
  248. var remoteTracks = {};