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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. var options = {
  2. hosts: {
  3. domain: 'hristo.jitsi.net',
  4. muc: 'conference.hristo.jitsi.net', // FIXME: use XEP-0030
  5. bridge: 'jitsi-videobridge.hristo.jitsi.net', // FIXME: use XEP-0030
  6. },
  7. bosh: '//hristo.jitsi.net/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. disableAudioLevels: true
  13. }
  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. if(localTracks[i].getType() == "video") {
  24. $("body").append("<video autoplay='1' id='localVideo" + i + "' />");
  25. localTracks[i].attach($("#localVideo" + i ));
  26. } else {
  27. $("body").append("<audio autoplay='1' id='localAudio" + i + "' />");
  28. localTracks[i].attach($("#localAudio" + i ));
  29. }
  30. console.log(localTracks[i]);
  31. }
  32. }
  33. /**
  34. * Handles remote tracks
  35. * @param track JitsiTrack object
  36. */
  37. function onRemoteTrack(track) {
  38. var participant = track.getParitcipantId();
  39. if(!remoteTracks[participant])
  40. remoteTracks[participant] = [];
  41. var idx = remoteTracks[participant].push(track);
  42. var id = participant + track.getType() + idx;
  43. if(track.getType() == "video") {
  44. $("body").append("<video autoplay='1' id='" + participant + "video" + idx + "' />");
  45. } else {
  46. $("body").append("<audio autoplay='1' id='" + participant + "audio' />");
  47. }
  48. track.attach($("#" + id));
  49. }
  50. /**
  51. * That function is executed when the conference is joined
  52. */
  53. function onConferenceJoined () {
  54. console.log("conference joined!");
  55. for(var i = 0; i < localTracks.length; i++)
  56. room.addTrack(localTracks[i]);
  57. }
  58. function onUserLeft(id) {
  59. if(!remoteTracks[id])
  60. return;
  61. var tracks = remoteTracks[id];
  62. for(var i = 0; i< tracks.length; i++)
  63. tracks[i].detach($("#" + id + tracks[i].getType()))
  64. }
  65. /**
  66. * That function is called when connection is established successfully
  67. */
  68. function onConnectionSuccess(){
  69. room = connection.initJitsiConference("conference2", confOptions);
  70. room.on(JitsiMeetJS.events.conference.TRACK_ADDED, onRemoteTrack);
  71. room.on(JitsiMeetJS.events.conference.TRACK_REMOVED, function () {
  72. console.debug("track removed!!!");
  73. });
  74. room.on(JitsiMeetJS.events.conference.CONFERENCE_JOINED, onConferenceJoined);
  75. room.on(JitsiMeetJS.events.conference.USER_JOINED, function(id){ remoteTracks[id] = [];});
  76. room.on(JitsiMeetJS.events.conference.USER_LEFT, onUserLeft);
  77. room.on(JitsiMeetJS.events.conference.TRACK_MUTE_CHANGED, function (track) {
  78. console.debug(track.getType() + " - " + track.isMuted());
  79. });
  80. room.on(JitsiMeetJS.events.conference.DISPLAY_NAME_CHANGED, function (userID, displayName) {
  81. console.debug(userID + " - " + displayName);
  82. });
  83. room.on(JitsiMeetJS.events.conference.TRACK_AUDIO_LEVEL_CHANGED,
  84. function(userID, audioLevel){
  85. // console.log(userID + " - " + audioLevel);
  86. });
  87. room.join();
  88. };
  89. /**
  90. * This function is called when the connection fail.
  91. */
  92. function onConnectionFailed(){console.error("Connection Failed!")};
  93. /**
  94. * This function is called when we disconnect.
  95. */
  96. function disconnect(){
  97. console.log("disconnect!");
  98. connection.removeEventListener(JitsiMeetJS.events.connection.CONNECTION_ESTABLISHED, onConnectionSuccess);
  99. connection.removeEventListener(JitsiMeetJS.events.connection.CONNECTION_FAILED, onConnectionFailed);
  100. connection.removeEventListener(JitsiMeetJS.events.connection.CONNECTION_DISCONNECTED, disconnect);
  101. }
  102. function unload() {
  103. // room.leave();
  104. connection.disconnect();
  105. }
  106. $(window).bind('beforeunload', unload);
  107. $(window).bind('unload', unload);
  108. // JitsiMeetJS.setLogLevel(JitsiMeetJS.logLevels.ERROR);
  109. var initOptions = {
  110. // Desktop sharing method. Can be set to 'ext', 'webrtc' or false to disable.
  111. desktopSharingChromeMethod: 'ext',
  112. // The ID of the jidesha extension for Chrome.
  113. desktopSharingChromeExtId: 'mbocklcggfhnbahlnepmldehdhpjfcjp',
  114. // The media sources to use when using screen sharing with the Chrome
  115. // extension.
  116. desktopSharingChromeSources: ['screen', 'window'],
  117. // Required version of Chrome extension
  118. desktopSharingChromeMinExtVersion: '0.1',
  119. // The ID of the jidesha extension for Firefox. If null, we assume that no
  120. // extension is required.
  121. desktopSharingFirefoxExtId: null,
  122. // Whether desktop sharing should be disabled on Firefox.
  123. desktopSharingFirefoxDisabled: true,
  124. // The maximum version of Firefox which requires a jidesha extension.
  125. // Example: if set to 41, we will require the extension for Firefox versions
  126. // up to and including 41. On Firefox 42 and higher, we will run without the
  127. // extension.
  128. // If set to -1, an extension will be required for all versions of Firefox.
  129. desktopSharingFirefoxMaxVersionExtRequired: -1,
  130. // The URL to the Firefox extension for desktop sharing.
  131. desktopSharingFirefoxExtensionURL: null
  132. }
  133. JitsiMeetJS.init(initOptions).then(function(){
  134. JitsiMeetJS.createLocalTracks({devices: ["audio", "video"]}).
  135. then(onLocalTracks).catch(function (error) {
  136. console.log(error);
  137. });
  138. }).catch(function (error) {
  139. console.log(error);
  140. });
  141. var connection = null;
  142. var room = null;
  143. var localTracks = [];
  144. var remoteTracks = {};
  145. connection = new JitsiMeetJS.JitsiConnection(null, null, options);
  146. connection.addEventListener(JitsiMeetJS.events.connection.CONNECTION_ESTABLISHED, onConnectionSuccess);
  147. connection.addEventListener(JitsiMeetJS.events.connection.CONNECTION_FAILED, onConnectionFailed);
  148. connection.addEventListener(JitsiMeetJS.events.connection.CONNECTION_DISCONNECTED, disconnect);
  149. connection.connect();