You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

videolayout.js 67KB

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