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

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