您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

videolayout.js 64KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824
  1. var VideoLayout = (function (my) {
  2. var currentDominantSpeaker = null;
  3. var lastNCount = config.channelLastN;
  4. var lastNEndpointsCache = [];
  5. var largeVideoState = {
  6. updateInProgress: false,
  7. newSrc: ''
  8. };
  9. my.connectionIndicators = {};
  10. my.changeLocalAudio = function(stream) {
  11. connection.jingle.localAudio = stream;
  12. RTC.attachMediaStream($('#localAudio'), stream);
  13. document.getElementById('localAudio').autoplay = true;
  14. document.getElementById('localAudio').volume = 0;
  15. };
  16. my.changeLocalVideo = function(stream, flipX) {
  17. connection.jingle.localVideo = stream;
  18. var localVideo = document.createElement('video');
  19. localVideo.id = 'localVideo_' + stream.id;
  20. localVideo.autoplay = true;
  21. localVideo.volume = 0; // is it required if audio is separated ?
  22. localVideo.oncontextmenu = function () { return false; };
  23. var localVideoContainer = document.getElementById('localVideoWrapper');
  24. localVideoContainer.appendChild(localVideo);
  25. // Set default display name.
  26. setDisplayName('localVideoContainer');
  27. if(!VideoLayout.connectionIndicators["localVideoContainer"]) {
  28. VideoLayout.connectionIndicators["localVideoContainer"]
  29. = new ConnectionIndicator($("#localVideoContainer")[0]);
  30. }
  31. AudioLevels.updateAudioLevelCanvas();
  32. var localVideoSelector = $('#' + localVideo.id);
  33. // Add click handler to both video and video wrapper elements in case
  34. // there's no video.
  35. localVideoSelector.click(function () {
  36. VideoLayout.handleVideoThumbClicked(localVideo.src);
  37. });
  38. $('#localVideoContainer').click(function () {
  39. VideoLayout.handleVideoThumbClicked(localVideo.src);
  40. });
  41. // Add hover handler
  42. $('#localVideoContainer').hover(
  43. function() {
  44. VideoLayout.showDisplayName('localVideoContainer', true);
  45. },
  46. function() {
  47. if (!VideoLayout.isLargeVideoVisible()
  48. || localVideo.src !== $('#largeVideo').attr('src'))
  49. VideoLayout.showDisplayName('localVideoContainer', false);
  50. }
  51. );
  52. // Add stream ended handler
  53. stream.onended = function () {
  54. localVideoContainer.removeChild(localVideo);
  55. VideoLayout.updateRemovedVideo(localVideo.src);
  56. };
  57. // Flip video x axis if needed
  58. flipXLocalVideo = flipX;
  59. if (flipX) {
  60. localVideoSelector.addClass("flipVideoX");
  61. }
  62. // Attach WebRTC stream
  63. var videoStream = simulcast.getLocalVideoStream();
  64. RTC.attachMediaStream(localVideoSelector, videoStream);
  65. localVideoSrc = localVideo.src;
  66. VideoLayout.updateLargeVideo(localVideoSrc, 0);
  67. };
  68. /**
  69. * Checks if removed video is currently displayed and tries to display
  70. * another one instead.
  71. * @param removedVideoSrc src stream identifier of the video.
  72. */
  73. my.updateRemovedVideo = function(removedVideoSrc) {
  74. if (removedVideoSrc === $('#largeVideo').attr('src')) {
  75. // this is currently displayed as large
  76. // pick the last visible video in the row
  77. // if nobody else is left, this picks the local video
  78. var pick
  79. = $('#remoteVideos>span[id!="mixedstream"]:visible:last>video')
  80. .get(0);
  81. if (!pick) {
  82. console.info("Last visible video no longer exists");
  83. pick = $('#remoteVideos>span[id!="mixedstream"]>video').get(0);
  84. if (!pick || !pick.src) {
  85. // Try local video
  86. console.info("Fallback to local video...");
  87. pick = $('#remoteVideos>span>span>video').get(0);
  88. }
  89. }
  90. // mute if localvideo
  91. if (pick) {
  92. VideoLayout.updateLargeVideo(pick.src, pick.volume);
  93. } else {
  94. console.warn("Failed to elect large video");
  95. }
  96. }
  97. };
  98. /**
  99. * Updates the large video with the given new video source.
  100. */
  101. my.updateLargeVideo = function(newSrc, vol) {
  102. console.log('hover in', newSrc);
  103. if ($('#largeVideo').attr('src') != newSrc) {
  104. // Due to the simulcast the localVideoSrc may have changed when the
  105. // fadeOut event triggers. In that case the getJidFromVideoSrc and
  106. // isVideoSrcDesktop methods will not function correctly.
  107. //
  108. // Also, again due to the simulcast, the updateLargeVideo method can
  109. // be called multiple times almost simultaneously. Therefore, we
  110. // store the state here and update only once.
  111. largeVideoState.newSrc = newSrc;
  112. largeVideoState.isVisible = $('#largeVideo').is(':visible');
  113. largeVideoState.isDesktop = isVideoSrcDesktop(newSrc);
  114. largeVideoState.userJid = getJidFromVideoSrc(newSrc);
  115. // Screen stream is already rotated
  116. largeVideoState.flipX = (newSrc === localVideoSrc) && flipXLocalVideo;
  117. var oldSrc = $('#largeVideo').attr('src');
  118. largeVideoState.oldJid = getJidFromVideoSrc(oldSrc);
  119. var fade = false;
  120. if (largeVideoState.oldJid != largeVideoState.userJid) {
  121. fade = true;
  122. // we want the notification to trigger even if userJid is undefined,
  123. // or null.
  124. $(document).trigger("selectedendpointchanged", [largeVideoState.userJid]);
  125. }
  126. if (!largeVideoState.updateInProgress) {
  127. largeVideoState.updateInProgress = true;
  128. var doUpdate = function () {
  129. $('#largeVideo').attr('src', largeVideoState.newSrc);
  130. var videoTransform = document.getElementById('largeVideo')
  131. .style.webkitTransform;
  132. if (largeVideoState.flipX && videoTransform !== 'scaleX(-1)') {
  133. document.getElementById('largeVideo').style.webkitTransform
  134. = "scaleX(-1)";
  135. }
  136. else if (!largeVideoState.flipX && videoTransform === 'scaleX(-1)') {
  137. document.getElementById('largeVideo').style.webkitTransform
  138. = "none";
  139. }
  140. // Change the way we'll be measuring and positioning large video
  141. getVideoSize = largeVideoState.isDesktop
  142. ? getDesktopVideoSize
  143. : getCameraVideoSize;
  144. getVideoPosition = largeVideoState.isDesktop
  145. ? getDesktopVideoPosition
  146. : getCameraVideoPosition;
  147. if (largeVideoState.isVisible) {
  148. // Only if the large video is currently visible.
  149. // Disable previous dominant speaker video.
  150. if (largeVideoState.oldJid) {
  151. var oldResourceJid = Strophe.getResourceFromJid(largeVideoState.oldJid);
  152. VideoLayout.enableDominantSpeaker(oldResourceJid, false);
  153. if(VideoLayout.connectionIndicators) {
  154. var videoContainerId = null;
  155. if (oldResourceJid == Strophe.getResourceFromJid(connection.emuc.myroomjid)) {
  156. videoContainerId = 'localVideoContainer';
  157. }
  158. else {
  159. videoContainerId = 'participant_' + oldResourceJid;
  160. }
  161. if(VideoLayout.connectionIndicators[videoContainerId])
  162. VideoLayout.connectionIndicators[videoContainerId].setShowHQ(false);
  163. }
  164. }
  165. // Enable new dominant speaker in the remote videos section.
  166. if (largeVideoState.userJid) {
  167. var resourceJid = Strophe.getResourceFromJid(largeVideoState.userJid);
  168. VideoLayout.enableDominantSpeaker(resourceJid, true);
  169. if(VideoLayout.connectionIndicators)
  170. {
  171. var videoContainerId = null;
  172. if (resourceJid
  173. === Strophe.getResourceFromJid(connection.emuc.myroomjid)) {
  174. videoContainerId = 'localVideoContainer';
  175. }
  176. else {
  177. videoContainerId = 'participant_' + resourceJid;
  178. }
  179. if(VideoLayout.connectionIndicators[videoContainerId])
  180. VideoLayout.connectionIndicators[videoContainerId].setShowHQ(true);
  181. }
  182. }
  183. largeVideoState.updateInProgress = false;
  184. if (fade) {
  185. // using "this" should be ok because we're called
  186. // from within the fadeOut event.
  187. $(this).fadeIn(300);
  188. }
  189. }
  190. };
  191. if (fade) {
  192. $('#largeVideo').fadeOut(300, doUpdate);
  193. } else {
  194. doUpdate();
  195. }
  196. }
  197. }
  198. };
  199. my.handleVideoThumbClicked = function(videoSrc) {
  200. // Restore style for previously focused video
  201. var focusJid = getJidFromVideoSrc(focusedVideoSrc);
  202. var oldContainer = getParticipantContainer(focusJid);
  203. if (oldContainer) {
  204. oldContainer.removeClass("videoContainerFocused");
  205. }
  206. // Unlock current focused.
  207. if (focusedVideoSrc === videoSrc)
  208. {
  209. focusedVideoSrc = null;
  210. var dominantSpeakerVideo = null;
  211. // Enable the currently set dominant speaker.
  212. if (currentDominantSpeaker) {
  213. dominantSpeakerVideo
  214. = $('#participant_' + currentDominantSpeaker + '>video')
  215. .get(0);
  216. if (dominantSpeakerVideo) {
  217. VideoLayout.updateLargeVideo(dominantSpeakerVideo.src, 1);
  218. }
  219. }
  220. return;
  221. }
  222. // Lock new video
  223. focusedVideoSrc = videoSrc;
  224. // Update focused/pinned interface.
  225. var userJid = getJidFromVideoSrc(videoSrc);
  226. if (userJid)
  227. {
  228. var container = getParticipantContainer(userJid);
  229. container.addClass("videoContainerFocused");
  230. }
  231. // Triggers a "video.selected" event. The "false" parameter indicates
  232. // this isn't a prezi.
  233. $(document).trigger("video.selected", [false]);
  234. VideoLayout.updateLargeVideo(videoSrc, 1);
  235. $('audio').each(function (idx, el) {
  236. if (el.id.indexOf('mixedmslabel') !== -1) {
  237. el.volume = 0;
  238. el.volume = 1;
  239. }
  240. });
  241. };
  242. /**
  243. * Positions the large video.
  244. *
  245. * @param videoWidth the stream video width
  246. * @param videoHeight the stream video height
  247. */
  248. my.positionLarge = function (videoWidth, videoHeight) {
  249. var videoSpaceWidth = $('#videospace').width();
  250. var videoSpaceHeight = window.innerHeight;
  251. var videoSize = getVideoSize(videoWidth,
  252. videoHeight,
  253. videoSpaceWidth,
  254. videoSpaceHeight);
  255. var largeVideoWidth = videoSize[0];
  256. var largeVideoHeight = videoSize[1];
  257. var videoPosition = getVideoPosition(largeVideoWidth,
  258. largeVideoHeight,
  259. videoSpaceWidth,
  260. videoSpaceHeight);
  261. var horizontalIndent = videoPosition[0];
  262. var verticalIndent = videoPosition[1];
  263. positionVideo($('#largeVideo'),
  264. largeVideoWidth,
  265. largeVideoHeight,
  266. horizontalIndent, verticalIndent);
  267. };
  268. /**
  269. * Shows/hides the large video.
  270. */
  271. my.setLargeVideoVisible = function(isVisible) {
  272. var largeVideoJid = getJidFromVideoSrc($('#largeVideo').attr('src'));
  273. var resourceJid = Strophe.getResourceFromJid(largeVideoJid);
  274. if (isVisible) {
  275. $('#largeVideo').css({visibility: 'visible'});
  276. $('.watermark').css({visibility: 'visible'});
  277. VideoLayout.enableDominantSpeaker(resourceJid, true);
  278. }
  279. else {
  280. $('#largeVideo').css({visibility: 'hidden'});
  281. $('.watermark').css({visibility: 'hidden'});
  282. VideoLayout.enableDominantSpeaker(resourceJid, false);
  283. }
  284. };
  285. /**
  286. * Indicates if the large video is currently visible.
  287. *
  288. * @return <tt>true</tt> if visible, <tt>false</tt> - otherwise
  289. */
  290. my.isLargeVideoVisible = function() {
  291. return $('#largeVideo').is(':visible');
  292. };
  293. /**
  294. * Checks if container for participant identified by given peerJid exists
  295. * in the document and creates it eventually.
  296. *
  297. * @param peerJid peer Jid to check.
  298. *
  299. * @return Returns <tt>true</tt> if the peer container exists,
  300. * <tt>false</tt> - otherwise
  301. */
  302. my.ensurePeerContainerExists = function(peerJid) {
  303. ContactList.ensureAddContact(peerJid);
  304. var resourceJid = Strophe.getResourceFromJid(peerJid);
  305. var videoSpanId = 'participant_' + resourceJid;
  306. if ($('#' + videoSpanId).length > 0) {
  307. // If there's been a focus change, make sure we add focus related
  308. // interface!!
  309. if (focus && $('#remote_popupmenu_' + resourceJid).length <= 0)
  310. addRemoteVideoMenu( peerJid,
  311. document.getElementById(videoSpanId));
  312. }
  313. else {
  314. var container
  315. = VideoLayout.addRemoteVideoContainer(peerJid, videoSpanId);
  316. // Set default display name.
  317. setDisplayName(videoSpanId);
  318. VideoLayout.connectionIndicators[videoSpanId] = new ConnectionIndicator(container);
  319. var nickfield = document.createElement('span');
  320. nickfield.className = "nick";
  321. nickfield.appendChild(document.createTextNode(resourceJid));
  322. container.appendChild(nickfield);
  323. // In case this is not currently in the last n we don't show it.
  324. if (lastNCount
  325. && lastNCount > 0
  326. && $('#remoteVideos>span').length >= lastNCount + 2) {
  327. showPeerContainer(resourceJid, false);
  328. }
  329. else
  330. VideoLayout.resizeThumbnails();
  331. }
  332. };
  333. my.addRemoteVideoContainer = function(peerJid, spanId) {
  334. var container = document.createElement('span');
  335. container.id = spanId;
  336. container.className = 'videocontainer';
  337. var remotes = document.getElementById('remoteVideos');
  338. // If the peerJid is null then this video span couldn't be directly
  339. // associated with a participant (this could happen in the case of prezi).
  340. if (focus && peerJid != null)
  341. addRemoteVideoMenu(peerJid, container);
  342. remotes.appendChild(container);
  343. AudioLevels.updateAudioLevelCanvas(peerJid);
  344. return container;
  345. };
  346. /**
  347. * Creates an audio or video stream element.
  348. */
  349. my.createStreamElement = function (sid, stream) {
  350. var isVideo = stream.getVideoTracks().length > 0;
  351. var element = isVideo
  352. ? document.createElement('video')
  353. : document.createElement('audio');
  354. var id = (isVideo ? 'remoteVideo_' : 'remoteAudio_')
  355. + sid + '_' + stream.id;
  356. element.id = id;
  357. element.autoplay = true;
  358. element.oncontextmenu = function () { return false; };
  359. return element;
  360. };
  361. my.addRemoteStreamElement
  362. = function (container, sid, stream, peerJid, thessrc) {
  363. var newElementId = null;
  364. var isVideo = stream.getVideoTracks().length > 0;
  365. if (container) {
  366. var streamElement = VideoLayout.createStreamElement(sid, stream);
  367. newElementId = streamElement.id;
  368. container.appendChild(streamElement);
  369. var sel = $('#' + newElementId);
  370. sel.hide();
  371. // If the container is currently visible we attach the stream.
  372. if (!isVideo
  373. || (container.offsetParent !== null && isVideo)) {
  374. var videoStream = simulcast.getReceivingVideoStream(stream);
  375. RTC.attachMediaStream(sel, videoStream);
  376. if (isVideo)
  377. waitForRemoteVideo(sel, thessrc, stream);
  378. }
  379. stream.onended = function () {
  380. console.log('stream ended', this);
  381. VideoLayout.removeRemoteStreamElement(
  382. stream, isVideo, container);
  383. if (peerJid)
  384. ContactList.removeContact(peerJid);
  385. };
  386. // Add click handler.
  387. container.onclick = function (event) {
  388. /*
  389. * FIXME It turns out that videoThumb may not exist (if there is
  390. * no actual video).
  391. */
  392. var videoThumb = $('#' + container.id + '>video').get(0);
  393. if (videoThumb)
  394. VideoLayout.handleVideoThumbClicked(videoThumb.src);
  395. event.preventDefault();
  396. return false;
  397. };
  398. // Add hover handler
  399. $(container).hover(
  400. function() {
  401. VideoLayout.showDisplayName(container.id, true);
  402. },
  403. function() {
  404. var videoSrc = null;
  405. if ($('#' + container.id + '>video')
  406. && $('#' + container.id + '>video').length > 0) {
  407. videoSrc = $('#' + container.id + '>video').get(0).src;
  408. }
  409. // If the video has been "pinned" by the user we want to
  410. // keep the display name on place.
  411. if (!VideoLayout.isLargeVideoVisible()
  412. || videoSrc !== $('#largeVideo').attr('src'))
  413. VideoLayout.showDisplayName(container.id, false);
  414. }
  415. );
  416. }
  417. return newElementId;
  418. };
  419. /**
  420. * Removes the remote stream element corresponding to the given stream and
  421. * parent container.
  422. *
  423. * @param stream the stream
  424. * @param isVideo <tt>true</tt> if given <tt>stream</tt> is a video one.
  425. * @param container
  426. */
  427. my.removeRemoteStreamElement = function (stream, isVideo, container) {
  428. if (!container)
  429. return;
  430. var select = null;
  431. var removedVideoSrc = null;
  432. if (isVideo) {
  433. select = $('#' + container.id + '>video');
  434. removedVideoSrc = select.get(0).src;
  435. }
  436. else
  437. select = $('#' + container.id + '>audio');
  438. // Remove video source from the mapping.
  439. delete videoSrcToSsrc[removedVideoSrc];
  440. // Mark video as removed to cancel waiting loop(if video is removed
  441. // before has started)
  442. select.removed = true;
  443. select.remove();
  444. var audioCount = $('#' + container.id + '>audio').length;
  445. var videoCount = $('#' + container.id + '>video').length;
  446. if (!audioCount && !videoCount) {
  447. console.log("Remove whole user", container.id);
  448. if(VideoLayout.connectionIndicators[container.id])
  449. VideoLayout.connectionIndicators[container.id].remove();
  450. // Remove whole container
  451. container.remove();
  452. Util.playSoundNotification('userLeft');
  453. VideoLayout.resizeThumbnails();
  454. }
  455. if (removedVideoSrc)
  456. VideoLayout.updateRemovedVideo(removedVideoSrc);
  457. };
  458. /**
  459. * Show/hide peer container for the given resourceJid.
  460. */
  461. function showPeerContainer(resourceJid, isShow) {
  462. var peerContainer = $('#participant_' + resourceJid);
  463. if (!peerContainer)
  464. return;
  465. if (!peerContainer.is(':visible') && isShow)
  466. peerContainer.show();
  467. else if (peerContainer.is(':visible') && !isShow)
  468. {
  469. peerContainer.hide();
  470. if(VideoLayout.connectionIndicators['participant_' + resourceJid])
  471. VideoLayout.connectionIndicators['participant_' + resourceJid].hide();
  472. }
  473. VideoLayout.resizeThumbnails();
  474. ContactList.setClickable(resourceJid, isShow);
  475. };
  476. /**
  477. * Sets the display name for the given video span id.
  478. */
  479. function setDisplayName(videoSpanId, displayName) {
  480. var nameSpan = $('#' + videoSpanId + '>span.displayname');
  481. var defaultLocalDisplayName = "Me";
  482. // If we already have a display name for this video.
  483. if (nameSpan.length > 0) {
  484. var nameSpanElement = nameSpan.get(0);
  485. if (nameSpanElement.id === 'localDisplayName' &&
  486. $('#localDisplayName').text() !== displayName) {
  487. if (displayName && displayName.length > 0)
  488. $('#localDisplayName').text(displayName + ' (me)');
  489. else
  490. $('#localDisplayName').text(defaultLocalDisplayName);
  491. } else {
  492. if (displayName && displayName.length > 0)
  493. $('#' + videoSpanId + '_name').text(displayName);
  494. else
  495. $('#' + videoSpanId + '_name').text(interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME);
  496. }
  497. } else {
  498. var editButton = null;
  499. nameSpan = document.createElement('span');
  500. nameSpan.className = 'displayname';
  501. $('#' + videoSpanId)[0].appendChild(nameSpan);
  502. if (videoSpanId === 'localVideoContainer') {
  503. editButton = createEditDisplayNameButton();
  504. nameSpan.innerText = defaultLocalDisplayName;
  505. }
  506. else {
  507. nameSpan.innerText = interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME;
  508. }
  509. if (displayName && displayName.length > 0) {
  510. nameSpan.innerText = displayName;
  511. }
  512. if (!editButton) {
  513. nameSpan.id = videoSpanId + '_name';
  514. } else {
  515. nameSpan.id = 'localDisplayName';
  516. $('#' + videoSpanId)[0].appendChild(editButton);
  517. var editableText = document.createElement('input');
  518. editableText.className = 'displayname';
  519. editableText.type = 'text';
  520. editableText.id = 'editDisplayName';
  521. if (displayName && displayName.length) {
  522. editableText.value
  523. = displayName.substring(0, displayName.indexOf(' (me)'));
  524. }
  525. editableText.setAttribute('style', 'display:none;');
  526. editableText.setAttribute('placeholder', 'ex. Jane Pink');
  527. $('#' + videoSpanId)[0].appendChild(editableText);
  528. $('#localVideoContainer .displayname')
  529. .bind("click", function (e) {
  530. e.preventDefault();
  531. $('#localDisplayName').hide();
  532. $('#editDisplayName').show();
  533. $('#editDisplayName').focus();
  534. $('#editDisplayName').select();
  535. var inputDisplayNameHandler = function (name) {
  536. if (nickname !== name) {
  537. nickname = name;
  538. window.localStorage.displayname = nickname;
  539. connection.emuc.addDisplayNameToPresence(nickname);
  540. connection.emuc.sendPresence();
  541. Chat.setChatConversationMode(true);
  542. }
  543. if (!$('#localDisplayName').is(":visible")) {
  544. if (nickname)
  545. $('#localDisplayName').text(nickname + " (me)");
  546. else
  547. $('#localDisplayName')
  548. .text(defaultLocalDisplayName);
  549. $('#localDisplayName').show();
  550. }
  551. $('#editDisplayName').hide();
  552. };
  553. $('#editDisplayName').one("focusout", function (e) {
  554. inputDisplayNameHandler(this.value);
  555. });
  556. $('#editDisplayName').on('keydown', function (e) {
  557. if (e.keyCode === 13) {
  558. e.preventDefault();
  559. inputDisplayNameHandler(this.value);
  560. }
  561. });
  562. });
  563. }
  564. }
  565. };
  566. /**
  567. * Shows/hides the display name on the remote video.
  568. * @param videoSpanId the identifier of the video span element
  569. * @param isShow indicates if the display name should be shown or hidden
  570. */
  571. my.showDisplayName = function(videoSpanId, isShow) {
  572. var nameSpan = $('#' + videoSpanId + '>span.displayname').get(0);
  573. if (isShow) {
  574. if (nameSpan && nameSpan.innerHTML && nameSpan.innerHTML.length)
  575. nameSpan.setAttribute("style", "display:inline-block;");
  576. }
  577. else {
  578. if (nameSpan)
  579. nameSpan.setAttribute("style", "display:none;");
  580. }
  581. };
  582. /**
  583. * Shows the presence status message for the given video.
  584. */
  585. my.setPresenceStatus = function (videoSpanId, statusMsg) {
  586. if (!$('#' + videoSpanId).length) {
  587. // No container
  588. return;
  589. }
  590. var statusSpan = $('#' + videoSpanId + '>span.status');
  591. if (!statusSpan.length) {
  592. //Add status span
  593. statusSpan = document.createElement('span');
  594. statusSpan.className = 'status';
  595. statusSpan.id = videoSpanId + '_status';
  596. $('#' + videoSpanId)[0].appendChild(statusSpan);
  597. statusSpan = $('#' + videoSpanId + '>span.status');
  598. }
  599. // Display status
  600. if (statusMsg && statusMsg.length) {
  601. $('#' + videoSpanId + '_status').text(statusMsg);
  602. statusSpan.get(0).setAttribute("style", "display:inline-block;");
  603. }
  604. else {
  605. // Hide
  606. statusSpan.get(0).setAttribute("style", "display:none;");
  607. }
  608. };
  609. /**
  610. * Shows a visual indicator for the focus of the conference.
  611. * Currently if we're not the owner of the conference we obtain the focus
  612. * from the connection.jingle.sessions.
  613. */
  614. my.showFocusIndicator = function() {
  615. if (focus !== null) {
  616. var indicatorSpan = $('#localVideoContainer .focusindicator');
  617. if (indicatorSpan.children().length === 0)
  618. {
  619. createFocusIndicatorElement(indicatorSpan[0]);
  620. }
  621. }
  622. else if (Object.keys(connection.jingle.sessions).length > 0) {
  623. // If we're only a participant the focus will be the only session we have.
  624. var session
  625. = connection.jingle.sessions
  626. [Object.keys(connection.jingle.sessions)[0]];
  627. var focusId
  628. = 'participant_' + Strophe.getResourceFromJid(session.peerjid);
  629. var focusContainer = document.getElementById(focusId);
  630. if (!focusContainer) {
  631. console.error("No focus container!");
  632. return;
  633. }
  634. var indicatorSpan = $('#' + focusId + ' .focusindicator');
  635. if (!indicatorSpan || indicatorSpan.length === 0) {
  636. indicatorSpan = document.createElement('span');
  637. indicatorSpan.className = 'focusindicator';
  638. focusContainer.appendChild(indicatorSpan);
  639. createFocusIndicatorElement(indicatorSpan);
  640. }
  641. }
  642. };
  643. /**
  644. * Shows video muted indicator over small videos.
  645. */
  646. my.showVideoIndicator = function(videoSpanId, isMuted) {
  647. var videoMutedSpan = $('#' + videoSpanId + '>span.videoMuted');
  648. if (isMuted === 'false') {
  649. if (videoMutedSpan.length > 0) {
  650. videoMutedSpan.remove();
  651. }
  652. }
  653. else {
  654. if(videoMutedSpan.length == 0) {
  655. videoMutedSpan = document.createElement('span');
  656. videoMutedSpan.className = 'videoMuted';
  657. $('#' + videoSpanId)[0].appendChild(videoMutedSpan);
  658. var mutedIndicator = document.createElement('i');
  659. mutedIndicator.className = 'icon-camera-disabled';
  660. Util.setTooltip(mutedIndicator,
  661. "Participant has<br/>stopped the camera.",
  662. "top");
  663. videoMutedSpan.appendChild(mutedIndicator);
  664. }
  665. var audioMutedSpan = $('#' + videoSpanId + '>span.audioMuted');
  666. videoMutedSpan = $('#' + videoSpanId + '>span.videoMuted');
  667. videoMutedSpan.css({right: ((audioMutedSpan.length > 0)?'50px':'30px')});
  668. }
  669. };
  670. /**
  671. * Shows audio muted indicator over small videos.
  672. * @param {string} isMuted
  673. */
  674. my.showAudioIndicator = function(videoSpanId, isMuted) {
  675. var audioMutedSpan = $('#' + videoSpanId + '>span.audioMuted');
  676. if (isMuted === 'false') {
  677. if (audioMutedSpan.length > 0) {
  678. audioMutedSpan.popover('hide');
  679. audioMutedSpan.remove();
  680. }
  681. }
  682. else {
  683. if(audioMutedSpan.length > 0 )
  684. return;
  685. audioMutedSpan = document.createElement('span');
  686. audioMutedSpan.className = 'audioMuted';
  687. Util.setTooltip(audioMutedSpan,
  688. "Participant is muted",
  689. "top");
  690. $('#' + videoSpanId)[0].appendChild(audioMutedSpan);
  691. var mutedIndicator = document.createElement('i');
  692. mutedIndicator.className = 'icon-mic-disabled';
  693. audioMutedSpan.appendChild(mutedIndicator);
  694. }
  695. };
  696. /*
  697. * Shows or hides the audio muted indicator over the local thumbnail video.
  698. * @param {boolean} isMuted
  699. */
  700. my.showLocalAudioIndicator = function(isMuted) {
  701. VideoLayout.showAudioIndicator('localVideoContainer', isMuted.toString());
  702. };
  703. /**
  704. * Resizes the large video container.
  705. */
  706. my.resizeLargeVideoContainer = function () {
  707. Chat.resizeChat();
  708. var availableHeight = window.innerHeight;
  709. var availableWidth = Util.getAvailableVideoWidth();
  710. if (availableWidth < 0 || availableHeight < 0) return;
  711. $('#videospace').width(availableWidth);
  712. $('#videospace').height(availableHeight);
  713. $('#largeVideoContainer').width(availableWidth);
  714. $('#largeVideoContainer').height(availableHeight);
  715. VideoLayout.resizeThumbnails();
  716. };
  717. /**
  718. * Resizes thumbnails.
  719. */
  720. my.resizeThumbnails = function() {
  721. var videoSpaceWidth = $('#remoteVideos').width();
  722. var thumbnailSize = VideoLayout.calculateThumbnailSize(videoSpaceWidth);
  723. var width = thumbnailSize[0];
  724. var height = thumbnailSize[1];
  725. // size videos so that while keeping AR and max height, we have a
  726. // nice fit
  727. $('#remoteVideos').height(height);
  728. $('#remoteVideos>span').width(width);
  729. $('#remoteVideos>span').height(height);
  730. $(document).trigger("remotevideo.resized", [width, height]);
  731. };
  732. /**
  733. * Enables the dominant speaker UI.
  734. *
  735. * @param resourceJid the jid indicating the video element to
  736. * activate/deactivate
  737. * @param isEnable indicates if the dominant speaker should be enabled or
  738. * disabled
  739. */
  740. my.enableDominantSpeaker = function(resourceJid, isEnable) {
  741. var videoSpanId = null;
  742. var videoContainerId = null;
  743. if (resourceJid
  744. === Strophe.getResourceFromJid(connection.emuc.myroomjid)) {
  745. videoSpanId = 'localVideoWrapper';
  746. videoContainerId = 'localVideoContainer';
  747. }
  748. else {
  749. videoSpanId = 'participant_' + resourceJid;
  750. videoContainerId = videoSpanId;
  751. }
  752. var displayName = resourceJid;
  753. var nameSpan = $('#' + videoContainerId + '>span.displayname');
  754. if (nameSpan.length > 0)
  755. displayName = nameSpan.text();
  756. console.log("UI enable dominant speaker",
  757. displayName,
  758. resourceJid,
  759. isEnable);
  760. videoSpan = document.getElementById(videoContainerId);
  761. if (!videoSpan) {
  762. console.error("No video element for jid", resourceJid);
  763. return;
  764. }
  765. var video = $('#' + videoSpanId + '>video');
  766. if (video && video.length > 0) {
  767. if (isEnable) {
  768. VideoLayout.showDisplayName(videoContainerId, true);
  769. if (!videoSpan.classList.contains("dominantspeaker"))
  770. videoSpan.classList.add("dominantspeaker");
  771. video.css({visibility: 'hidden'});
  772. }
  773. else {
  774. VideoLayout.showDisplayName(videoContainerId, false);
  775. if (videoSpan.classList.contains("dominantspeaker"))
  776. videoSpan.classList.remove("dominantspeaker");
  777. video.css({visibility: 'visible'});
  778. }
  779. }
  780. };
  781. /**
  782. * Gets the selector of video thumbnail container for the user identified by
  783. * given <tt>userJid</tt>
  784. * @param userJid user's Jid for whom we want to get the video container.
  785. */
  786. function getParticipantContainer(userJid)
  787. {
  788. if (!userJid)
  789. return null;
  790. if (userJid === connection.emuc.myroomjid)
  791. return $("#localVideoContainer");
  792. else
  793. return $("#participant_" + Strophe.getResourceFromJid(userJid));
  794. }
  795. /**
  796. * Sets the size and position of the given video element.
  797. *
  798. * @param video the video element to position
  799. * @param width the desired video width
  800. * @param height the desired video height
  801. * @param horizontalIndent the left and right indent
  802. * @param verticalIndent the top and bottom indent
  803. */
  804. function positionVideo(video,
  805. width,
  806. height,
  807. horizontalIndent,
  808. verticalIndent) {
  809. video.width(width);
  810. video.height(height);
  811. video.css({ top: verticalIndent + 'px',
  812. bottom: verticalIndent + 'px',
  813. left: horizontalIndent + 'px',
  814. right: horizontalIndent + 'px'});
  815. }
  816. /**
  817. * Calculates the thumbnail size.
  818. *
  819. * @param videoSpaceWidth the width of the video space
  820. */
  821. my.calculateThumbnailSize = function (videoSpaceWidth) {
  822. // Calculate the available height, which is the inner window height minus
  823. // 39px for the header minus 2px for the delimiter lines on the top and
  824. // bottom of the large video, minus the 36px space inside the remoteVideos
  825. // container used for highlighting shadow.
  826. var availableHeight = 100;
  827. var numvids = $('#remoteVideos>span:visible').length;
  828. if (lastNCount && lastNCount > 0) {
  829. numvids = Math.min(lastNCount + 1, numvids);
  830. }
  831. // Remove the 3px borders arround videos and border around the remote
  832. // videos area and the 4 pixels between the local video and the others
  833. //TODO: Find out where the 4 pixels come from and remove them
  834. var availableWinWidth = videoSpaceWidth - 2 * 3 * numvids - 70 - 4;
  835. var availableWidth = availableWinWidth / numvids;
  836. var aspectRatio = 16.0 / 9.0;
  837. var maxHeight = Math.min(160, availableHeight);
  838. availableHeight = Math.min(maxHeight, availableWidth / aspectRatio);
  839. if (availableHeight < availableWidth / aspectRatio) {
  840. availableWidth = Math.floor(availableHeight * aspectRatio);
  841. }
  842. return [availableWidth, availableHeight];
  843. };
  844. /**
  845. * Returns an array of the video dimensions, so that it keeps it's aspect
  846. * ratio and fits available area with it's larger dimension. This method
  847. * ensures that whole video will be visible and can leave empty areas.
  848. *
  849. * @return an array with 2 elements, the video width and the video height
  850. */
  851. function getDesktopVideoSize(videoWidth,
  852. videoHeight,
  853. videoSpaceWidth,
  854. videoSpaceHeight) {
  855. if (!videoWidth)
  856. videoWidth = currentVideoWidth;
  857. if (!videoHeight)
  858. videoHeight = currentVideoHeight;
  859. var aspectRatio = videoWidth / videoHeight;
  860. var availableWidth = Math.max(videoWidth, videoSpaceWidth);
  861. var availableHeight = Math.max(videoHeight, videoSpaceHeight);
  862. videoSpaceHeight -= $('#remoteVideos').outerHeight();
  863. if (availableWidth / aspectRatio >= videoSpaceHeight)
  864. {
  865. availableHeight = videoSpaceHeight;
  866. availableWidth = availableHeight * aspectRatio;
  867. }
  868. if (availableHeight * aspectRatio >= videoSpaceWidth)
  869. {
  870. availableWidth = videoSpaceWidth;
  871. availableHeight = availableWidth / aspectRatio;
  872. }
  873. return [availableWidth, availableHeight];
  874. }
  875. /**
  876. * Creates the edit display name button.
  877. *
  878. * @returns the edit button
  879. */
  880. function createEditDisplayNameButton() {
  881. var editButton = document.createElement('a');
  882. editButton.className = 'displayname';
  883. Util.setTooltip(editButton,
  884. 'Click to edit your<br/>display name',
  885. "top");
  886. editButton.innerHTML = '<i class="fa fa-pencil"></i>';
  887. return editButton;
  888. }
  889. /**
  890. * Creates the element indicating the focus of the conference.
  891. *
  892. * @param parentElement the parent element where the focus indicator will
  893. * be added
  894. */
  895. function createFocusIndicatorElement(parentElement) {
  896. var focusIndicator = document.createElement('i');
  897. focusIndicator.className = 'fa fa-star';
  898. parentElement.appendChild(focusIndicator);
  899. Util.setTooltip(parentElement,
  900. "The owner of<br/>this conference",
  901. "top");
  902. }
  903. /**
  904. * Updates the remote video menu.
  905. *
  906. * @param jid the jid indicating the video for which we're adding a menu.
  907. * @param isMuted indicates the current mute state
  908. */
  909. my.updateRemoteVideoMenu = function(jid, isMuted) {
  910. var muteMenuItem
  911. = $('#remote_popupmenu_'
  912. + Strophe.getResourceFromJid(jid)
  913. + '>li>a.mutelink');
  914. var mutedIndicator = "<i class='icon-mic-disabled'></i>";
  915. if (muteMenuItem.length) {
  916. var muteLink = muteMenuItem.get(0);
  917. if (isMuted === 'true') {
  918. muteLink.innerHTML = mutedIndicator + ' Muted';
  919. muteLink.className = 'mutelink disabled';
  920. }
  921. else {
  922. muteLink.innerHTML = mutedIndicator + ' Mute';
  923. muteLink.className = 'mutelink';
  924. }
  925. }
  926. };
  927. /**
  928. * Returns the current dominant speaker resource jid.
  929. */
  930. my.getDominantSpeakerResourceJid = function () {
  931. return currentDominantSpeaker;
  932. };
  933. /**
  934. * Returns the corresponding resource jid to the given peer container
  935. * DOM element.
  936. *
  937. * @return the corresponding resource jid to the given peer container
  938. * DOM element
  939. */
  940. my.getPeerContainerResourceJid = function (containerElement) {
  941. var i = containerElement.id.indexOf('participant_');
  942. if (i >= 0)
  943. return containerElement.id.substring(i + 12);
  944. };
  945. /**
  946. * Adds the remote video menu element for the given <tt>jid</tt> in the
  947. * given <tt>parentElement</tt>.
  948. *
  949. * @param jid the jid indicating the video for which we're adding a menu.
  950. * @param parentElement the parent element where this menu will be added
  951. */
  952. function addRemoteVideoMenu(jid, parentElement) {
  953. var spanElement = document.createElement('span');
  954. spanElement.className = 'remotevideomenu';
  955. parentElement.appendChild(spanElement);
  956. var menuElement = document.createElement('i');
  957. menuElement.className = 'fa fa-angle-down';
  958. menuElement.title = 'Remote user controls';
  959. spanElement.appendChild(menuElement);
  960. // <ul class="popupmenu">
  961. // <li><a href="#">Mute</a></li>
  962. // <li><a href="#">Eject</a></li>
  963. // </ul>
  964. var popupmenuElement = document.createElement('ul');
  965. popupmenuElement.className = 'popupmenu';
  966. popupmenuElement.id
  967. = 'remote_popupmenu_' + Strophe.getResourceFromJid(jid);
  968. spanElement.appendChild(popupmenuElement);
  969. var muteMenuItem = document.createElement('li');
  970. var muteLinkItem = document.createElement('a');
  971. var mutedIndicator = "<i class='icon-mic-disabled'></i>";
  972. if (!mutedAudios[jid]) {
  973. muteLinkItem.innerHTML = mutedIndicator + 'Mute';
  974. muteLinkItem.className = 'mutelink';
  975. }
  976. else {
  977. muteLinkItem.innerHTML = mutedIndicator + ' Muted';
  978. muteLinkItem.className = 'mutelink disabled';
  979. }
  980. muteLinkItem.onclick = function(){
  981. if ($(this).attr('disabled') != undefined) {
  982. event.preventDefault();
  983. }
  984. var isMute = !mutedAudios[jid];
  985. connection.moderate.setMute(jid, isMute);
  986. popupmenuElement.setAttribute('style', 'display:none;');
  987. if (isMute) {
  988. this.innerHTML = mutedIndicator + ' Muted';
  989. this.className = 'mutelink disabled';
  990. }
  991. else {
  992. this.innerHTML = mutedIndicator + ' Mute';
  993. this.className = 'mutelink';
  994. }
  995. };
  996. muteMenuItem.appendChild(muteLinkItem);
  997. popupmenuElement.appendChild(muteMenuItem);
  998. var ejectIndicator = "<i class='fa fa-eject'></i>";
  999. var ejectMenuItem = document.createElement('li');
  1000. var ejectLinkItem = document.createElement('a');
  1001. ejectLinkItem.innerHTML = ejectIndicator + ' Kick out';
  1002. ejectLinkItem.onclick = function(){
  1003. connection.moderate.eject(jid);
  1004. popupmenuElement.setAttribute('style', 'display:none;');
  1005. };
  1006. ejectMenuItem.appendChild(ejectLinkItem);
  1007. popupmenuElement.appendChild(ejectMenuItem);
  1008. var paddingSpan = document.createElement('span');
  1009. paddingSpan.className = 'popupmenuPadding';
  1010. popupmenuElement.appendChild(paddingSpan);
  1011. }
  1012. /**
  1013. * On audio muted event.
  1014. */
  1015. $(document).bind('audiomuted.muc', function (event, jid, isMuted) {
  1016. if (jid === connection.emuc.myroomjid) {
  1017. // The local mute indicator is controlled locally
  1018. return;
  1019. }
  1020. VideoLayout.ensurePeerContainerExists(jid);
  1021. if (focus) {
  1022. mutedAudios[jid] = isMuted;
  1023. VideoLayout.updateRemoteVideoMenu(jid, isMuted);
  1024. }
  1025. var videoSpanId = 'participant_' + Strophe.getResourceFromJid(jid);
  1026. if (videoSpanId)
  1027. VideoLayout.showAudioIndicator(videoSpanId, isMuted);
  1028. });
  1029. /**
  1030. * On video muted event.
  1031. */
  1032. $(document).bind('videomuted.muc', function (event, jid, isMuted) {
  1033. var videoSpanId = null;
  1034. if (jid === connection.emuc.myroomjid) {
  1035. videoSpanId = 'localVideoContainer';
  1036. } else {
  1037. VideoLayout.ensurePeerContainerExists(jid);
  1038. videoSpanId = 'participant_' + Strophe.getResourceFromJid(jid);
  1039. }
  1040. if (videoSpanId)
  1041. VideoLayout.showVideoIndicator(videoSpanId, isMuted);
  1042. });
  1043. /**
  1044. * Display name changed.
  1045. */
  1046. $(document).bind('displaynamechanged',
  1047. function (event, jid, displayName, status) {
  1048. if (jid === 'localVideoContainer'
  1049. || jid === connection.emuc.myroomjid) {
  1050. setDisplayName('localVideoContainer',
  1051. displayName);
  1052. } else {
  1053. VideoLayout.ensurePeerContainerExists(jid);
  1054. setDisplayName(
  1055. 'participant_' + Strophe.getResourceFromJid(jid),
  1056. displayName,
  1057. status);
  1058. }
  1059. });
  1060. /**
  1061. * On dominant speaker changed event.
  1062. */
  1063. $(document).bind('dominantspeakerchanged', function (event, resourceJid) {
  1064. // We ignore local user events.
  1065. if (resourceJid
  1066. === Strophe.getResourceFromJid(connection.emuc.myroomjid))
  1067. return;
  1068. // Update the current dominant speaker.
  1069. if (resourceJid !== currentDominantSpeaker) {
  1070. var oldSpeakerVideoSpanId = "participant_" + currentDominantSpeaker,
  1071. newSpeakerVideoSpanId = "participant_" + resourceJid;
  1072. if($("#" + oldSpeakerVideoSpanId + ">span.displayname").text() ===
  1073. interfaceConfig.DEFAULT_DOMINANT_SPEAKER_DISPLAY_NAME) {
  1074. setDisplayName(oldSpeakerVideoSpanId, null);
  1075. }
  1076. if($("#" + newSpeakerVideoSpanId + ">span.displayname").text() ===
  1077. interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME) {
  1078. setDisplayName(newSpeakerVideoSpanId,
  1079. interfaceConfig.DEFAULT_DOMINANT_SPEAKER_DISPLAY_NAME);
  1080. }
  1081. currentDominantSpeaker = resourceJid;
  1082. } else {
  1083. return;
  1084. }
  1085. // Obtain container for new dominant speaker.
  1086. var container = document.getElementById(
  1087. 'participant_' + resourceJid);
  1088. // Local video will not have container found, but that's ok
  1089. // since we don't want to switch to local video.
  1090. if (container && !focusedVideoSrc)
  1091. {
  1092. var video = container.getElementsByTagName("video");
  1093. // Update the large video if the video source is already available,
  1094. // otherwise wait for the "videoactive.jingle" event.
  1095. if (video.length && video[0].currentTime > 0)
  1096. VideoLayout.updateLargeVideo(video[0].src);
  1097. }
  1098. });
  1099. /**
  1100. * On last N change event.
  1101. *
  1102. * @param event the event that notified us
  1103. * @param lastNEndpoints the list of last N endpoints
  1104. * @param endpointsEnteringLastN the list currently entering last N
  1105. * endpoints
  1106. */
  1107. $(document).bind('lastnchanged', function ( event,
  1108. lastNEndpoints,
  1109. endpointsEnteringLastN,
  1110. stream) {
  1111. if (lastNCount !== lastNEndpoints.length)
  1112. lastNCount = lastNEndpoints.length;
  1113. lastNEndpointsCache = lastNEndpoints;
  1114. $('#remoteVideos>span').each(function( index, element ) {
  1115. var resourceJid = VideoLayout.getPeerContainerResourceJid(element);
  1116. if (resourceJid
  1117. && lastNEndpoints.indexOf(resourceJid) < 0) {
  1118. console.log("Remove from last N", resourceJid);
  1119. showPeerContainer(resourceJid, false);
  1120. }
  1121. });
  1122. if (!endpointsEnteringLastN || endpointsEnteringLastN.length < 0)
  1123. endpointsEnteringLastN = lastNEndpoints;
  1124. if (endpointsEnteringLastN && endpointsEnteringLastN.length > 0) {
  1125. endpointsEnteringLastN.forEach(function (resourceJid) {
  1126. if (!$('#participant_' + resourceJid).is(':visible')) {
  1127. console.log("Add to last N", resourceJid);
  1128. showPeerContainer(resourceJid, true);
  1129. mediaStreams.some(function (mediaStream) {
  1130. if (mediaStream.peerjid
  1131. && Strophe.getResourceFromJid(mediaStream.peerjid)
  1132. === resourceJid
  1133. && mediaStream.type === mediaStream.VIDEO_TYPE) {
  1134. var sel = $('#participant_' + resourceJid + '>video');
  1135. var videoStream = simulcast.getReceivingVideoStream(mediaStream.stream);
  1136. RTC.attachMediaStream(sel, videoStream);
  1137. waitForRemoteVideo(
  1138. sel,
  1139. mediaStream.ssrc,
  1140. mediaStream.stream);
  1141. return true;
  1142. }
  1143. });
  1144. }
  1145. });
  1146. }
  1147. });
  1148. $(document).bind('videoactive.jingle', function (event, videoelem) {
  1149. if (videoelem.attr('id').indexOf('mixedmslabel') === -1) {
  1150. // ignore mixedmslabela0 and v0
  1151. videoelem.show();
  1152. VideoLayout.resizeThumbnails();
  1153. var videoParent = videoelem.parent();
  1154. var parentResourceJid = null;
  1155. if (videoParent)
  1156. parentResourceJid
  1157. = VideoLayout.getPeerContainerResourceJid(videoParent[0]);
  1158. // Update the large video to the last added video only if there's no
  1159. // current dominant or focused speaker or update it to the current
  1160. // dominant speaker.
  1161. if ((!focusedVideoSrc && !VideoLayout.getDominantSpeakerResourceJid())
  1162. || (parentResourceJid
  1163. && VideoLayout.getDominantSpeakerResourceJid()
  1164. === parentResourceJid)) {
  1165. VideoLayout.updateLargeVideo(videoelem.attr('src'), 1);
  1166. }
  1167. VideoLayout.showFocusIndicator();
  1168. }
  1169. });
  1170. /**
  1171. * On simulcast layers changed event.
  1172. */
  1173. $(document).bind('simulcastlayerschanged', function (event, endpointSimulcastLayers) {
  1174. endpointSimulcastLayers.forEach(function (esl) {
  1175. var primarySSRC = esl.simulcastLayer.primarySSRC;
  1176. var msid = simulcast.getRemoteVideoStreamIdBySSRC(primarySSRC);
  1177. // Get session and stream from msid.
  1178. var session, electedStream;
  1179. var i, j, k;
  1180. if (connection.jingle) {
  1181. var keys = Object.keys(connection.jingle.sessions);
  1182. for (i = 0; i < keys.length; i++) {
  1183. var sid = keys[i];
  1184. if (electedStream) {
  1185. // stream found, stop.
  1186. break;
  1187. }
  1188. session = connection.jingle.sessions[sid];
  1189. if (session.remoteStreams) {
  1190. for (j = 0; j < session.remoteStreams.length; j++) {
  1191. var remoteStream = session.remoteStreams[j];
  1192. if (electedStream) {
  1193. // stream found, stop.
  1194. break;
  1195. }
  1196. var tracks = remoteStream.getVideoTracks();
  1197. if (tracks) {
  1198. for (k = 0; k < tracks.length; k++) {
  1199. var track = tracks[k];
  1200. if (msid === [remoteStream.id, track.id].join(' ')) {
  1201. electedStream = new webkitMediaStream([track]);
  1202. // stream found, stop.
  1203. break;
  1204. }
  1205. }
  1206. }
  1207. }
  1208. }
  1209. }
  1210. }
  1211. if (session && electedStream) {
  1212. console.info('Switching simulcast substream.');
  1213. console.info([esl, primarySSRC, msid, session, electedStream]);
  1214. var msidParts = msid.split(' ');
  1215. var selRemoteVideo = $(['#', 'remoteVideo_', session.sid, '_', msidParts[0]].join(''));
  1216. var updateLargeVideo = (ssrc2jid[videoSrcToSsrc[selRemoteVideo.attr('src')]]
  1217. == ssrc2jid[videoSrcToSsrc[largeVideoState.newSrc]]);
  1218. var updateFocusedVideoSrc = (selRemoteVideo.attr('src') == focusedVideoSrc);
  1219. var electedStreamUrl = webkitURL.createObjectURL(electedStream);
  1220. selRemoteVideo.attr('src', electedStreamUrl);
  1221. videoSrcToSsrc[selRemoteVideo.attr('src')] = primarySSRC;
  1222. if (updateLargeVideo) {
  1223. VideoLayout.updateLargeVideo(electedStreamUrl);
  1224. }
  1225. if (updateFocusedVideoSrc) {
  1226. focusedVideoSrc = electedStreamUrl;
  1227. }
  1228. } else {
  1229. console.error('Could not find a stream or a session.', session, electedStream);
  1230. }
  1231. });
  1232. });
  1233. /**
  1234. * Constructs new connection indicator.
  1235. * @param videoContainer the video container associated with the indicator.
  1236. * @constructor
  1237. */
  1238. function ConnectionIndicator(videoContainer)
  1239. {
  1240. this.videoContainer = videoContainer;
  1241. this.bandwidth = null;
  1242. this.packetLoss = null;
  1243. this.bitrate = null;
  1244. this.showMoreValue = false;
  1245. this.resolution = null;
  1246. this.transport = [];
  1247. this.popover = null;
  1248. this.showHQ = false;
  1249. this.create();
  1250. }
  1251. /**
  1252. * Values for the connection quality
  1253. * @type {{98: string, 81: string, 64: string, 47: string, 30: string, 0: string}}
  1254. */
  1255. ConnectionIndicator.connectionQualityValues = {
  1256. 98: "18px", //full
  1257. 81: "15px",//4 bars
  1258. 64: "11px",//3 bars
  1259. 47: "7px",//2 bars
  1260. 30: "3px",//1 bar
  1261. 0: "0px"//empty
  1262. };
  1263. /**
  1264. * Sets the value of the property that indicates whether the displayed resolution si the
  1265. * resolution of High Quality stream or Low Quality
  1266. * @param value boolean.
  1267. */
  1268. ConnectionIndicator.prototype.setShowHQ = function (value) {
  1269. this.showHQ = value;
  1270. this.updatePopoverData();
  1271. };
  1272. /**
  1273. * Generates the html content.
  1274. * @returns {string} the html content.
  1275. */
  1276. ConnectionIndicator.prototype.generateText = function () {
  1277. var downloadBitrate, uploadBitrate, packetLoss, resolution;
  1278. if(this.bitrate === null)
  1279. {
  1280. downloadBitrate = "N/A";
  1281. uploadBitrate = "N/A";
  1282. }
  1283. else
  1284. {
  1285. downloadBitrate = this.bitrate.download? this.bitrate.download + " Kbps" : "N/A";
  1286. uploadBitrate = this.bitrate.upload? this.bitrate.upload + " Kbps" : "N/A";
  1287. }
  1288. if(this.packetLoss === null)
  1289. {
  1290. packetLoss = "N/A";
  1291. }
  1292. else
  1293. {
  1294. packetLoss = "<span class='jitsipopover_green'>&darr;</span>" +
  1295. (this.packetLoss.download != null? this.packetLoss.download : "N/A") +
  1296. "% <span class='jitsipopover_orange'>&uarr;</span>" +
  1297. (this.packetLoss.upload != null? this.packetLoss.upload : "N/A") + "%";
  1298. }
  1299. var resolutionValue = null;
  1300. if(this.resolution)
  1301. {
  1302. if(this.showHQ && this.resolution.hq)
  1303. {
  1304. resolutionValue = this.resolution.hq;
  1305. }
  1306. else if(!this.showHQ && this.resolution.lq)
  1307. {
  1308. resolutionValue = this.resolution.lq;
  1309. }
  1310. }
  1311. if(!resolutionValue ||
  1312. !resolutionValue.height ||
  1313. !resolutionValue.width)
  1314. {
  1315. resolution = "N/A";
  1316. }
  1317. else
  1318. {
  1319. resolution = resolutionValue.width + "x" + resolutionValue.height;
  1320. }
  1321. var result = "<span class='jitsipopover_blue'>Bitrate:</span> <span class='jitsipopover_green'>&darr;</span>" +
  1322. downloadBitrate + " <span class='jitsipopover_orange'>&uarr;</span>" +
  1323. uploadBitrate + "<br />" +
  1324. "<span class='jitsipopover_blue'>Packet loss: </span>" + packetLoss + "<br />" +
  1325. "<span class='jitsipopover_blue'>Resolution:</span> " + resolution + "<br />";
  1326. if(this.videoContainer.id == "localVideoContainer")
  1327. result += "<div class=\"jitsipopover_showmore\" onclick = \"VideoLayout.connectionIndicators['" +
  1328. this.videoContainer.id + "'].showMore()\">" + (this.showMoreValue? "Show less" : "Show More") + "</div><br />";
  1329. if(this.showMoreValue)
  1330. {
  1331. var downloadBandwidth, uploadBandwidth, transport;
  1332. if(this.bandwidth === null)
  1333. {
  1334. downloadBandwidth = "N/A";
  1335. uploadBandwidth = "N/A";
  1336. }
  1337. else
  1338. {
  1339. downloadBandwidth = this.bandwidth.download? this.bandwidth.download + " Kbps" : "N/A";
  1340. uploadBandwidth = this.bandwidth.upload? this.bandwidth.upload + " Kbps" : "N/A";
  1341. }
  1342. if(!this.transport || this.transport.length === 0)
  1343. {
  1344. transport = "<span class='jitsipopover_blue'>Address:</span> N/A";
  1345. }
  1346. else
  1347. {
  1348. transport = "<span class='jitsipopover_blue'>Address:</span> " + this.transport[0].ip.substring(0,this.transport[0].ip.indexOf(":")) + "<br />";
  1349. if(this.transport.length > 1)
  1350. {
  1351. transport += "<span class='jitsipopover_blue'>Ports:</span> ";
  1352. }
  1353. else
  1354. {
  1355. transport += "<span class='jitsipopover_blue'>Port:</span> ";
  1356. }
  1357. for(var i = 0; i < this.transport.length; i++)
  1358. {
  1359. transport += ((i !== 0)? ", " : "") +
  1360. this.transport[i].ip.substring(this.transport[i].ip.indexOf(":")+1,
  1361. this.transport[i].ip.length);
  1362. }
  1363. transport += "<br /><span class='jitsipopover_blue'>Transport:</span> " + this.transport[0].type + "<br />";
  1364. }
  1365. result += "<span class='jitsipopover_blue'>Estimated bandwidth:</span> " +
  1366. "<span class='jitsipopover_green'>&darr;</span>" + downloadBandwidth +
  1367. " <span class='jitsipopover_orange'>&uarr;</span>" +
  1368. uploadBandwidth + "<br />";
  1369. result += transport;
  1370. }
  1371. return result;
  1372. };
  1373. /**
  1374. * Shows or hide the additional information.
  1375. */
  1376. ConnectionIndicator.prototype.showMore = function () {
  1377. this.showMoreValue = !this.showMoreValue;
  1378. this.updatePopoverData();
  1379. };
  1380. /**
  1381. * Creates the indicator
  1382. */
  1383. ConnectionIndicator.prototype.create = function () {
  1384. this.connectionIndicatorContainer = document.createElement("div");
  1385. this.connectionIndicatorContainer.className = "connectionindicator";
  1386. this.connectionIndicatorContainer.style.display = "none";
  1387. this.videoContainer.appendChild(this.connectionIndicatorContainer);
  1388. this.popover = new JitsiPopover($("#" + this.videoContainer.id + " > .connectionindicator"),
  1389. {content: "<div class=\"connection_info\">Come back here for " +
  1390. "connection information once the conference starts</div>", skin: "black"});
  1391. function createIcon(classes)
  1392. {
  1393. var icon = document.createElement("span");
  1394. for(var i in classes)
  1395. {
  1396. icon.classList.add(classes[i]);
  1397. }
  1398. icon.appendChild(document.createElement("i")).classList.add("icon-connection");
  1399. return icon;
  1400. }
  1401. this.emptyIcon = this.connectionIndicatorContainer.appendChild(
  1402. createIcon(["connection", "connection_empty"]));
  1403. this.fullIcon = this.connectionIndicatorContainer.appendChild(
  1404. createIcon(["connection", "connection_full"]));
  1405. };
  1406. /**
  1407. * Removes the indicator
  1408. */
  1409. ConnectionIndicator.prototype.remove = function()
  1410. {
  1411. this.popover.hide();
  1412. this.connectionIndicatorContainer.remove();
  1413. };
  1414. /**
  1415. * Updates the data of the indicator
  1416. * @param percent the percent of connection quality
  1417. * @param object the statistics data.
  1418. */
  1419. ConnectionIndicator.prototype.updateConnectionQuality = function (percent, object) {
  1420. if(percent === null)
  1421. {
  1422. this.connectionIndicatorContainer.style.display = "none";
  1423. return;
  1424. }
  1425. else
  1426. {
  1427. this.connectionIndicatorContainer.style.display = "block";
  1428. }
  1429. this.bandwidth = object.bandwidth;
  1430. this.bitrate = object.bitrate;
  1431. this.packetLoss = object.packetLoss;
  1432. this.transport = object.transport;
  1433. if(object.resolution)
  1434. {
  1435. this.resolution = object.resolution;
  1436. }
  1437. for(var quality in ConnectionIndicator.connectionQualityValues)
  1438. {
  1439. if(percent >= quality)
  1440. {
  1441. this.fullIcon.style.width = ConnectionIndicator.connectionQualityValues[quality];
  1442. }
  1443. }
  1444. this.updatePopoverData();
  1445. };
  1446. /**
  1447. * Updates the resolution
  1448. * @param resolution the new resolution
  1449. */
  1450. ConnectionIndicator.prototype.updateResolution = function (resolution) {
  1451. this.resolution = resolution;
  1452. this.updatePopoverData();
  1453. };
  1454. /**
  1455. * Updates the content of the popover
  1456. */
  1457. ConnectionIndicator.prototype.updatePopoverData = function () {
  1458. this.popover.updateContent("<div class=\"connection_info\">" + this.generateText() + "</div>");
  1459. };
  1460. /**
  1461. * Hides the popover
  1462. */
  1463. ConnectionIndicator.prototype.hide = function () {
  1464. this.popover.hide();
  1465. };
  1466. /**
  1467. * Hides the indicator
  1468. */
  1469. ConnectionIndicator.prototype.hideIndicator = function () {
  1470. this.connectionIndicatorContainer.style.display = "none";
  1471. };
  1472. /**
  1473. * Updates the data for the indicator
  1474. * @param id the id of the indicator
  1475. * @param percent the percent for connection quality
  1476. * @param object the data
  1477. */
  1478. function updateStatsIndicator(id, percent, object) {
  1479. if(VideoLayout.connectionIndicators[id])
  1480. VideoLayout.connectionIndicators[id].updateConnectionQuality(percent, object);
  1481. }
  1482. /**
  1483. * Updates local stats
  1484. * @param percent
  1485. * @param object
  1486. */
  1487. my.updateLocalConnectionStats = function (percent, object) {
  1488. var resolution = null;
  1489. if(object.resolution !== null)
  1490. {
  1491. resolution = object.resolution;
  1492. object.resolution = resolution[connection.emuc.myroomjid];
  1493. delete resolution[connection.emuc.myroomjid];
  1494. }
  1495. updateStatsIndicator("localVideoContainer", percent, object);
  1496. for(var jid in resolution)
  1497. {
  1498. if(resolution[jid] === null)
  1499. continue;
  1500. var id = 'participant_' + Strophe.getResourceFromJid(jid);
  1501. if(VideoLayout.connectionIndicators[id])
  1502. {
  1503. VideoLayout.connectionIndicators[id].updateResolution(resolution[jid]);
  1504. }
  1505. }
  1506. };
  1507. /**
  1508. * Updates remote stats.
  1509. * @param jid the jid associated with the stats
  1510. * @param percent the connection quality percent
  1511. * @param object the stats data
  1512. */
  1513. my.updateConnectionStats = function (jid, percent, object) {
  1514. var resourceJid = Strophe.getResourceFromJid(jid);
  1515. var videoSpanId = 'participant_' + resourceJid;
  1516. updateStatsIndicator(videoSpanId, percent, object);
  1517. };
  1518. /**
  1519. * Removes the connection
  1520. * @param jid
  1521. */
  1522. my.removeConnectionIndicator = function (jid) {
  1523. if(VideoLayout.connectionIndicators['participant_' + Strophe.getResourceFromJid(jid)])
  1524. VideoLayout.connectionIndicators['participant_' + Strophe.getResourceFromJid(jid)].remove();
  1525. };
  1526. /**
  1527. * Hides the connection indicator
  1528. * @param jid
  1529. */
  1530. my.hideConnectionIndicator = function (jid) {
  1531. if(VideoLayout.connectionIndicators['participant_' + Strophe.getResourceFromJid(jid)])
  1532. VideoLayout.connectionIndicators['participant_' + Strophe.getResourceFromJid(jid)].hide();
  1533. };
  1534. /**
  1535. * Hides all the indicators
  1536. */
  1537. my.onStatsStop = function () {
  1538. for(var indicator in VideoLayout.connectionIndicators)
  1539. {
  1540. VideoLayout.connectionIndicators[indicator].hideIndicator();
  1541. }
  1542. };
  1543. return my;
  1544. }(VideoLayout || {}));