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

videolayout.js 68KB

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