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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351
  1. var VideoLayout = (function (my) {
  2. var currentDominantSpeaker = null;
  3. var lastNCount = config.channelLastN;
  4. var localLastNCount = config.channelLastN;
  5. var localLastNSet = [];
  6. var lastNEndpointsCache = [];
  7. var lastNPickupJid = null;
  8. var largeVideoState = {
  9. updateInProgress: false,
  10. newSrc: ''
  11. };
  12. var defaultLocalDisplayName = "Me";
  13. my.connectionIndicators = {};
  14. my.isInLastN = function(resource) {
  15. return lastNCount < 0 // lastN is disabled, return true
  16. || (lastNCount > 0 && lastNEndpointsCache.length == 0) // lastNEndpoints cache not built yet, return true
  17. || (lastNEndpointsCache && lastNEndpointsCache.indexOf(resource) !== -1);
  18. };
  19. my.onLocalStreamCreated = function (stream) {
  20. switch(stream.type)
  21. {
  22. case "audio":
  23. VideoLayout.changeLocalAudio(stream.getOriginalStream());
  24. break;
  25. case "video":
  26. VideoLayout.changeLocalVideo(stream.getOriginalStream(), true);
  27. break;
  28. case "stream":
  29. VideoLayout.changeLocalStream(stream.getOriginalStream());
  30. break;
  31. }
  32. };
  33. my.changeLocalStream = function (stream) {
  34. connection.jingle.localAudio = stream;
  35. VideoLayout.changeLocalVideo(stream, true);
  36. };
  37. my.changeLocalAudio = function(stream) {
  38. connection.jingle.localAudio = stream;
  39. RTC.attachMediaStream($('#localAudio'), stream);
  40. document.getElementById('localAudio').autoplay = true;
  41. document.getElementById('localAudio').volume = 0;
  42. if (preMuted) {
  43. setAudioMuted(true);
  44. preMuted = false;
  45. }
  46. };
  47. my.changeLocalVideo = function(stream, flipX) {
  48. connection.jingle.localVideo = stream;
  49. var localVideo = document.createElement('video');
  50. localVideo.id = 'localVideo_' + RTC.getStreamID(stream);
  51. localVideo.autoplay = true;
  52. localVideo.volume = 0; // is it required if audio is separated ?
  53. localVideo.oncontextmenu = function () { return false; };
  54. var localVideoContainer = document.getElementById('localVideoWrapper');
  55. localVideoContainer.appendChild(localVideo);
  56. // Set default display name.
  57. setDisplayName('localVideoContainer');
  58. if(!VideoLayout.connectionIndicators["localVideoContainer"]) {
  59. VideoLayout.connectionIndicators["localVideoContainer"]
  60. = new ConnectionIndicator($("#localVideoContainer")[0], null);
  61. }
  62. AudioLevels.updateAudioLevelCanvas();
  63. var localVideoSelector = $('#' + localVideo.id);
  64. // Add click handler to both video and video wrapper elements in case
  65. // there's no video.
  66. localVideoSelector.click(function (event) {
  67. event.stopPropagation();
  68. VideoLayout.handleVideoThumbClicked(
  69. RTC.getVideoSrc(localVideo),
  70. false,
  71. Strophe.getResourceFromJid(connection.emuc.myroomjid));
  72. });
  73. $('#localVideoContainer').click(function (event) {
  74. event.stopPropagation();
  75. VideoLayout.handleVideoThumbClicked(
  76. RTC.getVideoSrc(localVideo),
  77. false,
  78. Strophe.getResourceFromJid(connection.emuc.myroomjid));
  79. });
  80. // Add hover handler
  81. $('#localVideoContainer').hover(
  82. function() {
  83. VideoLayout.showDisplayName('localVideoContainer', true);
  84. },
  85. function() {
  86. if (!VideoLayout.isLargeVideoVisible()
  87. || RTC.getVideoSrc(localVideo) !== RTC.getVideoSrc($('#largeVideo')[0]))
  88. VideoLayout.showDisplayName('localVideoContainer', false);
  89. }
  90. );
  91. // Add stream ended handler
  92. stream.onended = function () {
  93. localVideoContainer.removeChild(localVideo);
  94. VideoLayout.updateRemovedVideo(RTC.getVideoSrc(localVideo));
  95. };
  96. // Flip video x axis if needed
  97. flipXLocalVideo = flipX;
  98. if (flipX) {
  99. localVideoSelector.addClass("flipVideoX");
  100. }
  101. // Attach WebRTC stream
  102. var videoStream = simulcast.getLocalVideoStream();
  103. RTC.attachMediaStream(localVideoSelector, videoStream);
  104. localVideoSrc = RTC.getVideoSrc(localVideo);
  105. var myResourceJid = null;
  106. if(connection.emuc.myroomjid)
  107. {
  108. myResourceJid = Strophe.getResourceFromJid(connection.emuc.myroomjid);
  109. }
  110. VideoLayout.updateLargeVideo(localVideoSrc, 0,
  111. myResourceJid);
  112. };
  113. /**
  114. * Checks if removed video is currently displayed and tries to display
  115. * another one instead.
  116. * @param removedVideoSrc src stream identifier of the video.
  117. */
  118. my.updateRemovedVideo = function(removedVideoSrc) {
  119. if (removedVideoSrc === RTC.getVideoSrc($('#largeVideo')[0])) {
  120. // this is currently displayed as large
  121. // pick the last visible video in the row
  122. // if nobody else is left, this picks the local video
  123. var pick
  124. = $('#remoteVideos>span[id!="mixedstream"]:visible:last>video')
  125. .get(0);
  126. if (!pick) {
  127. console.info("Last visible video no longer exists");
  128. pick = $('#remoteVideos>span[id!="mixedstream"]>video').get(0);
  129. if (!pick || !RTC.getVideoSrc(pick)) {
  130. // Try local video
  131. console.info("Fallback to local video...");
  132. pick = $('#remoteVideos>span>span>video').get(0);
  133. }
  134. }
  135. // mute if localvideo
  136. if (pick) {
  137. var container = pick.parentNode;
  138. var jid = null;
  139. if(container)
  140. {
  141. if(container.id == "localVideoWrapper")
  142. {
  143. jid = Strophe.getResourceFromJid(connection.emuc.myroomjid);
  144. }
  145. else
  146. {
  147. jid = VideoLayout.getPeerContainerResourceJid(container);
  148. }
  149. }
  150. VideoLayout.updateLargeVideo(RTC.getVideoSrc(pick), pick.volume, jid);
  151. } else {
  152. console.warn("Failed to elect large video");
  153. }
  154. }
  155. };
  156. my.getLargeVideoState = function () {
  157. return largeVideoState;
  158. };
  159. /**
  160. * Updates the large video with the given new video source.
  161. */
  162. my.updateLargeVideo = function(newSrc, vol, resourceJid) {
  163. console.log('hover in', newSrc);
  164. if (RTC.getVideoSrc($('#largeVideo')[0]) !== newSrc) {
  165. $('#activeSpeaker').css('visibility', 'hidden');
  166. // Due to the simulcast the localVideoSrc may have changed when the
  167. // fadeOut event triggers. In that case the getJidFromVideoSrc and
  168. // isVideoSrcDesktop methods will not function correctly.
  169. //
  170. // Also, again due to the simulcast, the updateLargeVideo method can
  171. // be called multiple times almost simultaneously. Therefore, we
  172. // store the state here and update only once.
  173. largeVideoState.newSrc = newSrc;
  174. largeVideoState.isVisible = $('#largeVideo').is(':visible');
  175. largeVideoState.isDesktop = isVideoSrcDesktop(resourceJid);
  176. if(jid2Ssrc[largeVideoState.userResourceJid] ||
  177. (connection && connection.emuc.myroomjid &&
  178. largeVideoState.userResourceJid ===
  179. Strophe.getResourceFromJid(connection.emuc.myroomjid))) {
  180. largeVideoState.oldResourceJid = largeVideoState.userResourceJid;
  181. } else {
  182. largeVideoState.oldResourceJid = null;
  183. }
  184. largeVideoState.userResourceJid = resourceJid;
  185. // Screen stream is already rotated
  186. largeVideoState.flipX = (newSrc === localVideoSrc) && flipXLocalVideo;
  187. var userChanged = false;
  188. if (largeVideoState.oldResourceJid !== largeVideoState.userResourceJid) {
  189. userChanged = true;
  190. // we want the notification to trigger even if userJid is undefined,
  191. // or null.
  192. $(document).trigger("selectedendpointchanged", [largeVideoState.userResourceJid]);
  193. }
  194. if (!largeVideoState.updateInProgress) {
  195. largeVideoState.updateInProgress = true;
  196. var doUpdate = function () {
  197. Avatar.updateActiveSpeakerAvatarSrc(
  198. connection.emuc.findJidFromResource(
  199. largeVideoState.userResourceJid));
  200. if (!userChanged && largeVideoState.preload &&
  201. largeVideoState.preload !== null &&
  202. RTC.getVideoSrc($(largeVideoState.preload)[0]) === newSrc)
  203. {
  204. console.info('Switching to preloaded video');
  205. var attributes = $('#largeVideo').prop("attributes");
  206. // loop through largeVideo attributes and apply them on
  207. // preload.
  208. $.each(attributes, function () {
  209. if (this.name !== 'id' && this.name !== 'src') {
  210. largeVideoState.preload.attr(this.name, this.value);
  211. }
  212. });
  213. largeVideoState.preload.appendTo($('#largeVideoContainer'));
  214. $('#largeVideo').attr('id', 'previousLargeVideo');
  215. largeVideoState.preload.attr('id', 'largeVideo');
  216. $('#previousLargeVideo').remove();
  217. largeVideoState.preload.on('loadedmetadata', function (e) {
  218. currentVideoWidth = this.videoWidth;
  219. currentVideoHeight = this.videoHeight;
  220. VideoLayout.positionLarge(currentVideoWidth, currentVideoHeight);
  221. });
  222. largeVideoState.preload = null;
  223. largeVideoState.preload_ssrc = 0;
  224. } else {
  225. RTC.setVideoSrc($('#largeVideo')[0], largeVideoState.newSrc);
  226. }
  227. var videoTransform = document.getElementById('largeVideo')
  228. .style.webkitTransform;
  229. if (largeVideoState.flipX && videoTransform !== 'scaleX(-1)') {
  230. document.getElementById('largeVideo').style.webkitTransform
  231. = "scaleX(-1)";
  232. }
  233. else if (!largeVideoState.flipX && videoTransform === 'scaleX(-1)') {
  234. document.getElementById('largeVideo').style.webkitTransform
  235. = "none";
  236. }
  237. // Change the way we'll be measuring and positioning large video
  238. getVideoSize = largeVideoState.isDesktop
  239. ? getDesktopVideoSize
  240. : getCameraVideoSize;
  241. getVideoPosition = largeVideoState.isDesktop
  242. ? getDesktopVideoPosition
  243. : getCameraVideoPosition;
  244. // Only if the large video is currently visible.
  245. // Disable previous dominant speaker video.
  246. if (largeVideoState.oldResourceJid) {
  247. VideoLayout.enableDominantSpeaker(
  248. largeVideoState.oldResourceJid,
  249. false);
  250. }
  251. // Enable new dominant speaker in the remote videos section.
  252. if (largeVideoState.userResourceJid) {
  253. VideoLayout.enableDominantSpeaker(
  254. largeVideoState.userResourceJid,
  255. true);
  256. }
  257. if (userChanged && largeVideoState.isVisible) {
  258. // using "this" should be ok because we're called
  259. // from within the fadeOut event.
  260. $(this).fadeIn(300);
  261. }
  262. if(userChanged) {
  263. Avatar.showUserAvatar(
  264. connection.emuc.findJidFromResource(
  265. largeVideoState.oldResourceJid));
  266. }
  267. largeVideoState.updateInProgress = false;
  268. };
  269. if (userChanged) {
  270. $('#largeVideo').fadeOut(300, doUpdate);
  271. } else {
  272. doUpdate();
  273. }
  274. }
  275. } else {
  276. Avatar.showUserAvatar(
  277. connection.emuc.findJidFromResource(
  278. largeVideoState.userResourceJid));
  279. }
  280. };
  281. my.handleVideoThumbClicked = function(videoSrc,
  282. noPinnedEndpointChangedEvent,
  283. resourceJid) {
  284. // Restore style for previously focused video
  285. var oldContainer = null;
  286. if(focusedVideoInfo) {
  287. var focusResourceJid = focusedVideoInfo.resourceJid;
  288. oldContainer = getParticipantContainer(focusResourceJid);
  289. }
  290. if (oldContainer) {
  291. oldContainer.removeClass("videoContainerFocused");
  292. }
  293. // Unlock current focused.
  294. if (focusedVideoInfo && focusedVideoInfo.src === videoSrc)
  295. {
  296. focusedVideoInfo = null;
  297. var dominantSpeakerVideo = null;
  298. // Enable the currently set dominant speaker.
  299. if (currentDominantSpeaker) {
  300. dominantSpeakerVideo
  301. = $('#participant_' + currentDominantSpeaker + '>video')
  302. .get(0);
  303. if (dominantSpeakerVideo) {
  304. VideoLayout.updateLargeVideo(
  305. RTC.getVideoSrc(dominantSpeakerVideo),
  306. 1,
  307. currentDominantSpeaker);
  308. }
  309. }
  310. if (!noPinnedEndpointChangedEvent) {
  311. $(document).trigger("pinnedendpointchanged");
  312. }
  313. return;
  314. }
  315. // Lock new video
  316. focusedVideoInfo = {
  317. src: videoSrc,
  318. resourceJid: resourceJid
  319. };
  320. // Update focused/pinned interface.
  321. if (resourceJid)
  322. {
  323. var container = getParticipantContainer(resourceJid);
  324. container.addClass("videoContainerFocused");
  325. if (!noPinnedEndpointChangedEvent) {
  326. $(document).trigger("pinnedendpointchanged", [resourceJid]);
  327. }
  328. }
  329. if ($('#largeVideo').attr('src') === videoSrc &&
  330. VideoLayout.isLargeVideoOnTop()) {
  331. return;
  332. }
  333. // Triggers a "video.selected" event. The "false" parameter indicates
  334. // this isn't a prezi.
  335. $(document).trigger("video.selected", [false]);
  336. VideoLayout.updateLargeVideo(videoSrc, 1, resourceJid);
  337. $('audio').each(function (idx, el) {
  338. if (el.id.indexOf('mixedmslabel') !== -1) {
  339. el.volume = 0;
  340. el.volume = 1;
  341. }
  342. });
  343. };
  344. /**
  345. * Positions the large video.
  346. *
  347. * @param videoWidth the stream video width
  348. * @param videoHeight the stream video height
  349. */
  350. my.positionLarge = function (videoWidth, videoHeight) {
  351. var videoSpaceWidth = $('#videospace').width();
  352. var videoSpaceHeight = window.innerHeight;
  353. var videoSize = getVideoSize(videoWidth,
  354. videoHeight,
  355. videoSpaceWidth,
  356. videoSpaceHeight);
  357. var largeVideoWidth = videoSize[0];
  358. var largeVideoHeight = videoSize[1];
  359. var videoPosition = getVideoPosition(largeVideoWidth,
  360. largeVideoHeight,
  361. videoSpaceWidth,
  362. videoSpaceHeight);
  363. var horizontalIndent = videoPosition[0];
  364. var verticalIndent = videoPosition[1];
  365. positionVideo($('#largeVideo'),
  366. largeVideoWidth,
  367. largeVideoHeight,
  368. horizontalIndent, verticalIndent);
  369. };
  370. /**
  371. * Shows/hides the large video.
  372. */
  373. my.setLargeVideoVisible = function(isVisible) {
  374. var resourceJid = largeVideoState.userResourceJid;
  375. if (isVisible) {
  376. $('#largeVideo').css({visibility: 'visible'});
  377. $('.watermark').css({visibility: 'visible'});
  378. VideoLayout.enableDominantSpeaker(resourceJid, true);
  379. }
  380. else {
  381. $('#largeVideo').css({visibility: 'hidden'});
  382. $('#activeSpeaker').css('visibility', 'hidden');
  383. $('.watermark').css({visibility: 'hidden'});
  384. VideoLayout.enableDominantSpeaker(resourceJid, false);
  385. if(focusedVideoInfo) {
  386. var focusResourceJid = focusedVideoInfo.resourceJid;
  387. var oldContainer = getParticipantContainer(focusResourceJid);
  388. if (oldContainer && oldContainer.length > 0) {
  389. oldContainer.removeClass("videoContainerFocused");
  390. }
  391. focusedVideoInfo = null;
  392. if(focusResourceJid) {
  393. Avatar.showUserAvatar(
  394. connection.emuc.findJidFromResource(focusResourceJid));
  395. }
  396. }
  397. }
  398. };
  399. /**
  400. * Indicates if the large video is currently visible.
  401. *
  402. * @return <tt>true</tt> if visible, <tt>false</tt> - otherwise
  403. */
  404. my.isLargeVideoVisible = function() {
  405. return $('#largeVideo').is(':visible');
  406. };
  407. my.isLargeVideoOnTop = function () {
  408. return !Prezi.isPresentationVisible() && !Etherpad.isVisible();
  409. };
  410. /**
  411. * Checks if container for participant identified by given peerJid exists
  412. * in the document and creates it eventually.
  413. *
  414. * @param peerJid peer Jid to check.
  415. * @param userId user email or id for setting the avatar
  416. *
  417. * @return Returns <tt>true</tt> if the peer container exists,
  418. * <tt>false</tt> - otherwise
  419. */
  420. my.ensurePeerContainerExists = function(peerJid, userId) {
  421. ContactList.ensureAddContact(peerJid, userId);
  422. var resourceJid = Strophe.getResourceFromJid(peerJid);
  423. var videoSpanId = 'participant_' + resourceJid;
  424. if ($('#' + videoSpanId).length > 0) {
  425. // If there's been a focus change, make sure we add focus related
  426. // interface!!
  427. if (Moderator.isModerator() && !Moderator.isPeerModerator(peerJid)
  428. && $('#remote_popupmenu_' + resourceJid).length <= 0) {
  429. addRemoteVideoMenu(peerJid,
  430. document.getElementById(videoSpanId));
  431. }
  432. }
  433. else {
  434. var container =
  435. VideoLayout.addRemoteVideoContainer(peerJid, videoSpanId, userId);
  436. Avatar.setUserAvatar(peerJid, userId);
  437. // Set default display name.
  438. setDisplayName(videoSpanId);
  439. VideoLayout.connectionIndicators[videoSpanId] = new ConnectionIndicator(container, peerJid);
  440. var nickfield = document.createElement('span');
  441. nickfield.className = "nick";
  442. nickfield.appendChild(document.createTextNode(resourceJid));
  443. container.appendChild(nickfield);
  444. // In case this is not currently in the last n we don't show it.
  445. if (localLastNCount
  446. && localLastNCount > 0
  447. && $('#remoteVideos>span').length >= localLastNCount + 2) {
  448. showPeerContainer(resourceJid, 'hide');
  449. }
  450. else
  451. VideoLayout.resizeThumbnails();
  452. }
  453. };
  454. my.addRemoteVideoContainer = function(peerJid, spanId) {
  455. var container = document.createElement('span');
  456. container.id = spanId;
  457. container.className = 'videocontainer';
  458. var remotes = document.getElementById('remoteVideos');
  459. // If the peerJid is null then this video span couldn't be directly
  460. // associated with a participant (this could happen in the case of prezi).
  461. if (Moderator.isModerator() && peerJid !== null)
  462. addRemoteVideoMenu(peerJid, container);
  463. remotes.appendChild(container);
  464. AudioLevels.updateAudioLevelCanvas(peerJid);
  465. return container;
  466. };
  467. /**
  468. * Creates an audio or video stream element.
  469. */
  470. my.createStreamElement = function (sid, stream) {
  471. var isVideo = stream.getVideoTracks().length > 0;
  472. var element = isVideo
  473. ? document.createElement('video')
  474. : document.createElement('audio');
  475. var id = (isVideo ? 'remoteVideo_' : 'remoteAudio_')
  476. + sid + '_' + RTC.getStreamID(stream);
  477. element.id = id;
  478. element.autoplay = true;
  479. element.oncontextmenu = function () { return false; };
  480. return element;
  481. };
  482. my.addRemoteStreamElement
  483. = function (container, sid, stream, peerJid, thessrc) {
  484. var newElementId = null;
  485. var isVideo = stream.getVideoTracks().length > 0;
  486. if (container) {
  487. var streamElement = VideoLayout.createStreamElement(sid, stream);
  488. newElementId = streamElement.id;
  489. container.appendChild(streamElement);
  490. var sel = $('#' + newElementId);
  491. sel.hide();
  492. // If the container is currently visible we attach the stream.
  493. if (!isVideo
  494. || (container.offsetParent !== null && isVideo)) {
  495. var videoStream = simulcast.getReceivingVideoStream(stream);
  496. RTC.attachMediaStream(sel, videoStream);
  497. if (isVideo)
  498. waitForRemoteVideo(sel, thessrc, stream, peerJid);
  499. }
  500. stream.onended = function () {
  501. console.log('stream ended', this);
  502. VideoLayout.removeRemoteStreamElement(
  503. stream, isVideo, container);
  504. // NOTE(gp) it seems that under certain circumstances, the
  505. // onended event is not fired and thus the contact list is not
  506. // updated.
  507. //
  508. // The onended event of a stream should be fired when the SSRCs
  509. // corresponding to that stream are removed from the SDP; but
  510. // this doesn't seem to always be the case, resulting in ghost
  511. // contacts.
  512. //
  513. // In an attempt to fix the ghost contacts problem, I'm moving
  514. // the removeContact() method call in app.js, inside the
  515. // 'muc.left' event handler.
  516. //if (peerJid)
  517. // ContactList.removeContact(peerJid);
  518. };
  519. // Add click handler.
  520. container.onclick = function (event) {
  521. /*
  522. * FIXME It turns out that videoThumb may not exist (if there is
  523. * no actual video).
  524. */
  525. var videoThumb = $('#' + container.id + '>video').get(0);
  526. if (videoThumb) {
  527. VideoLayout.handleVideoThumbClicked(
  528. RTC.getVideoSrc(videoThumb),
  529. false,
  530. Strophe.getResourceFromJid(peerJid));
  531. }
  532. event.stopPropagation();
  533. event.preventDefault();
  534. return false;
  535. };
  536. // Add hover handler
  537. $(container).hover(
  538. function() {
  539. VideoLayout.showDisplayName(container.id, true);
  540. },
  541. function() {
  542. var videoSrc = null;
  543. if ($('#' + container.id + '>video')
  544. && $('#' + container.id + '>video').length > 0) {
  545. videoSrc = RTC.getVideoSrc($('#' + container.id + '>video').get(0));
  546. }
  547. // If the video has been "pinned" by the user we want to
  548. // keep the display name on place.
  549. if (!VideoLayout.isLargeVideoVisible()
  550. || videoSrc !== RTC.getVideoSrc($('#largeVideo')[0]))
  551. VideoLayout.showDisplayName(container.id, false);
  552. }
  553. );
  554. }
  555. return newElementId;
  556. };
  557. /**
  558. * Removes the remote stream element corresponding to the given stream and
  559. * parent container.
  560. *
  561. * @param stream the stream
  562. * @param isVideo <tt>true</tt> if given <tt>stream</tt> is a video one.
  563. * @param container
  564. */
  565. my.removeRemoteStreamElement = function (stream, isVideo, container) {
  566. if (!container)
  567. return;
  568. var select = null;
  569. var removedVideoSrc = null;
  570. if (isVideo) {
  571. select = $('#' + container.id + '>video');
  572. removedVideoSrc = RTC.getVideoSrc(select.get(0));
  573. }
  574. else
  575. select = $('#' + container.id + '>audio');
  576. // Mark video as removed to cancel waiting loop(if video is removed
  577. // before has started)
  578. select.removed = true;
  579. select.remove();
  580. var audioCount = $('#' + container.id + '>audio').length;
  581. var videoCount = $('#' + container.id + '>video').length;
  582. if (!audioCount && !videoCount) {
  583. console.log("Remove whole user", container.id);
  584. if(VideoLayout.connectionIndicators[container.id])
  585. VideoLayout.connectionIndicators[container.id].remove();
  586. // Remove whole container
  587. container.remove();
  588. Util.playSoundNotification('userLeft');
  589. VideoLayout.resizeThumbnails();
  590. }
  591. if (removedVideoSrc)
  592. VideoLayout.updateRemovedVideo(removedVideoSrc);
  593. };
  594. /**
  595. * Show/hide peer container for the given resourceJid.
  596. */
  597. function showPeerContainer(resourceJid, state) {
  598. var peerContainer = $('#participant_' + resourceJid);
  599. if (!peerContainer)
  600. return;
  601. var isHide = state === 'hide';
  602. var resizeThumbnails = false;
  603. if (!isHide) {
  604. if (!peerContainer.is(':visible')) {
  605. resizeThumbnails = true;
  606. peerContainer.show();
  607. }
  608. if (state == 'show')
  609. {
  610. // peerContainer.css('-webkit-filter', '');
  611. var jid = connection.emuc.findJidFromResource(resourceJid);
  612. Avatar.showUserAvatar(jid, false);
  613. }
  614. else // if (state == 'avatar')
  615. {
  616. // peerContainer.css('-webkit-filter', 'grayscale(100%)');
  617. var jid = connection.emuc.findJidFromResource(resourceJid);
  618. Avatar.showUserAvatar(jid, true);
  619. }
  620. }
  621. else if (peerContainer.is(':visible') && isHide)
  622. {
  623. resizeThumbnails = true;
  624. peerContainer.hide();
  625. if(VideoLayout.connectionIndicators['participant_' + resourceJid])
  626. VideoLayout.connectionIndicators['participant_' + resourceJid].hide();
  627. }
  628. if (resizeThumbnails) {
  629. VideoLayout.resizeThumbnails();
  630. }
  631. // We want to be able to pin a participant from the contact list, even
  632. // if he's not in the lastN set!
  633. // ContactList.setClickable(resourceJid, !isHide);
  634. };
  635. /**
  636. * Sets the display name for the given video span id.
  637. */
  638. function setDisplayName(videoSpanId, displayName) {
  639. var nameSpan = $('#' + videoSpanId + '>span.displayname');
  640. var defaultLocalDisplayName = interfaceConfig.DEFAULT_LOCAL_DISPLAY_NAME;
  641. // If we already have a display name for this video.
  642. if (nameSpan.length > 0) {
  643. var nameSpanElement = nameSpan.get(0);
  644. if (nameSpanElement.id === 'localDisplayName' &&
  645. $('#localDisplayName').text() !== displayName) {
  646. if (displayName && displayName.length > 0)
  647. $('#localDisplayName').html(displayName + ' (me)');
  648. else
  649. $('#localDisplayName').text(defaultLocalDisplayName);
  650. } else {
  651. if (displayName && displayName.length > 0)
  652. $('#' + videoSpanId + '_name').html(displayName);
  653. else
  654. $('#' + videoSpanId + '_name').text(interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME);
  655. }
  656. } else {
  657. var editButton = null;
  658. nameSpan = document.createElement('span');
  659. nameSpan.className = 'displayname';
  660. $('#' + videoSpanId)[0].appendChild(nameSpan);
  661. if (videoSpanId === 'localVideoContainer') {
  662. editButton = createEditDisplayNameButton();
  663. nameSpan.innerText = defaultLocalDisplayName;
  664. }
  665. else {
  666. nameSpan.innerText = interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME;
  667. }
  668. if (displayName && displayName.length > 0) {
  669. nameSpan.innerText = displayName;
  670. }
  671. if (!editButton) {
  672. nameSpan.id = videoSpanId + '_name';
  673. } else {
  674. nameSpan.id = 'localDisplayName';
  675. $('#' + videoSpanId)[0].appendChild(editButton);
  676. var editableText = document.createElement('input');
  677. editableText.className = 'displayname';
  678. editableText.type = 'text';
  679. editableText.id = 'editDisplayName';
  680. if (displayName && displayName.length) {
  681. editableText.value
  682. = displayName.substring(0, displayName.indexOf(' (me)'));
  683. }
  684. editableText.setAttribute('style', 'display:none;');
  685. editableText.setAttribute('placeholder', 'ex. Jane Pink');
  686. $('#' + videoSpanId)[0].appendChild(editableText);
  687. $('#localVideoContainer .displayname')
  688. .bind("click", function (e) {
  689. e.preventDefault();
  690. e.stopPropagation();
  691. $('#localDisplayName').hide();
  692. $('#editDisplayName').show();
  693. $('#editDisplayName').focus();
  694. $('#editDisplayName').select();
  695. $('#editDisplayName').one("focusout", function (e) {
  696. VideoLayout.inputDisplayNameHandler(this.value);
  697. });
  698. $('#editDisplayName').on('keydown', function (e) {
  699. if (e.keyCode === 13) {
  700. e.preventDefault();
  701. VideoLayout.inputDisplayNameHandler(this.value);
  702. }
  703. });
  704. });
  705. }
  706. }
  707. }
  708. my.inputDisplayNameHandler = function (name) {
  709. if (name && nickname !== name) {
  710. nickname = name;
  711. window.localStorage.displayname = nickname;
  712. connection.emuc.addDisplayNameToPresence(nickname);
  713. connection.emuc.sendPresence();
  714. Chat.setChatConversationMode(true);
  715. }
  716. if (!$('#localDisplayName').is(":visible")) {
  717. if (nickname)
  718. $('#localDisplayName').text(nickname + " (me)");
  719. else
  720. $('#localDisplayName')
  721. .text(interfaceConfig.DEFAULT_LOCAL_DISPLAY_NAME);
  722. $('#localDisplayName').show();
  723. }
  724. $('#editDisplayName').hide();
  725. };
  726. /**
  727. * Shows/hides the display name on the remote video.
  728. * @param videoSpanId the identifier of the video span element
  729. * @param isShow indicates if the display name should be shown or hidden
  730. */
  731. my.showDisplayName = function(videoSpanId, isShow) {
  732. var nameSpan = $('#' + videoSpanId + '>span.displayname').get(0);
  733. if (isShow) {
  734. if (nameSpan && nameSpan.innerHTML && nameSpan.innerHTML.length)
  735. nameSpan.setAttribute("style", "display:inline-block;");
  736. }
  737. else {
  738. if (nameSpan)
  739. nameSpan.setAttribute("style", "display:none;");
  740. }
  741. };
  742. /**
  743. * Shows the presence status message for the given video.
  744. */
  745. my.setPresenceStatus = function (videoSpanId, statusMsg) {
  746. if (!$('#' + videoSpanId).length) {
  747. // No container
  748. return;
  749. }
  750. var statusSpan = $('#' + videoSpanId + '>span.status');
  751. if (!statusSpan.length) {
  752. //Add status span
  753. statusSpan = document.createElement('span');
  754. statusSpan.className = 'status';
  755. statusSpan.id = videoSpanId + '_status';
  756. $('#' + videoSpanId)[0].appendChild(statusSpan);
  757. statusSpan = $('#' + videoSpanId + '>span.status');
  758. }
  759. // Display status
  760. if (statusMsg && statusMsg.length) {
  761. $('#' + videoSpanId + '_status').text(statusMsg);
  762. statusSpan.get(0).setAttribute("style", "display:inline-block;");
  763. }
  764. else {
  765. // Hide
  766. statusSpan.get(0).setAttribute("style", "display:none;");
  767. }
  768. };
  769. /**
  770. * Shows a visual indicator for the moderator of the conference.
  771. */
  772. my.showModeratorIndicator = function () {
  773. if (Moderator.isModerator()) {
  774. var indicatorSpan = $('#localVideoContainer .focusindicator');
  775. if (indicatorSpan.children().length === 0)
  776. {
  777. createModeratorIndicatorElement(indicatorSpan[0]);
  778. }
  779. }
  780. Object.keys(connection.emuc.members).forEach(function (jid) {
  781. var member = connection.emuc.members[jid];
  782. if (member.role === 'moderator') {
  783. var moderatorId
  784. = 'participant_' + Strophe.getResourceFromJid(jid);
  785. var moderatorContainer
  786. = document.getElementById(moderatorId);
  787. if (Strophe.getResourceFromJid(jid) === 'focus') {
  788. // Skip server side focus
  789. return;
  790. }
  791. if (!moderatorContainer) {
  792. console.error("No moderator container for " + jid);
  793. return;
  794. }
  795. var menuSpan = $('#' + moderatorId + '>span.remotevideomenu');
  796. if (menuSpan.length) {
  797. removeRemoteVideoMenu(moderatorId);
  798. }
  799. var indicatorSpan
  800. = $('#' + moderatorId + ' .focusindicator');
  801. if (!indicatorSpan || indicatorSpan.length === 0) {
  802. indicatorSpan = document.createElement('span');
  803. indicatorSpan.className = 'focusindicator';
  804. moderatorContainer.appendChild(indicatorSpan);
  805. createModeratorIndicatorElement(indicatorSpan);
  806. }
  807. }
  808. });
  809. };
  810. /**
  811. * Shows video muted indicator over small videos.
  812. */
  813. my.showVideoIndicator = function(videoSpanId, isMuted) {
  814. var videoMutedSpan = $('#' + videoSpanId + '>span.videoMuted');
  815. if (isMuted === 'false') {
  816. if (videoMutedSpan.length > 0) {
  817. videoMutedSpan.remove();
  818. }
  819. }
  820. else {
  821. if(videoMutedSpan.length == 0) {
  822. videoMutedSpan = document.createElement('span');
  823. videoMutedSpan.className = 'videoMuted';
  824. $('#' + videoSpanId)[0].appendChild(videoMutedSpan);
  825. var mutedIndicator = document.createElement('i');
  826. mutedIndicator.className = 'icon-camera-disabled';
  827. Util.setTooltip(mutedIndicator,
  828. "Participant has<br/>stopped the camera.",
  829. "top");
  830. videoMutedSpan.appendChild(mutedIndicator);
  831. }
  832. VideoLayout.updateMutePosition(videoSpanId);
  833. }
  834. };
  835. my.updateMutePosition = function (videoSpanId) {
  836. var audioMutedSpan = $('#' + videoSpanId + '>span.audioMuted');
  837. var connectionIndicator = $('#' + videoSpanId + '>div.connectionindicator');
  838. var videoMutedSpan = $('#' + videoSpanId + '>span.videoMuted');
  839. if(connectionIndicator.length > 0
  840. && connectionIndicator[0].style.display != "none") {
  841. audioMutedSpan.css({right: "23px"});
  842. videoMutedSpan.css({right: ((audioMutedSpan.length > 0? 23 : 0) + 30) + "px"});
  843. }
  844. else
  845. {
  846. audioMutedSpan.css({right: "0px"});
  847. videoMutedSpan.css({right: (audioMutedSpan.length > 0? 30 : 0) + "px"});
  848. }
  849. }
  850. /**
  851. * Shows audio muted indicator over small videos.
  852. * @param {string} isMuted
  853. */
  854. my.showAudioIndicator = function(videoSpanId, isMuted) {
  855. var audioMutedSpan = $('#' + videoSpanId + '>span.audioMuted');
  856. if (isMuted === 'false') {
  857. if (audioMutedSpan.length > 0) {
  858. audioMutedSpan.popover('hide');
  859. audioMutedSpan.remove();
  860. }
  861. }
  862. else {
  863. if(audioMutedSpan.length == 0 ) {
  864. audioMutedSpan = document.createElement('span');
  865. audioMutedSpan.className = 'audioMuted';
  866. Util.setTooltip(audioMutedSpan,
  867. "Participant is muted",
  868. "top");
  869. $('#' + videoSpanId)[0].appendChild(audioMutedSpan);
  870. var mutedIndicator = document.createElement('i');
  871. mutedIndicator.className = 'icon-mic-disabled';
  872. audioMutedSpan.appendChild(mutedIndicator);
  873. }
  874. VideoLayout.updateMutePosition(videoSpanId);
  875. }
  876. };
  877. /*
  878. * Shows or hides the audio muted indicator over the local thumbnail video.
  879. * @param {boolean} isMuted
  880. */
  881. my.showLocalAudioIndicator = function(isMuted) {
  882. VideoLayout.showAudioIndicator('localVideoContainer', isMuted.toString());
  883. };
  884. /**
  885. * Resizes the large video container.
  886. */
  887. my.resizeLargeVideoContainer = function () {
  888. Chat.resizeChat();
  889. var availableHeight = window.innerHeight;
  890. var availableWidth = Util.getAvailableVideoWidth();
  891. if (availableWidth < 0 || availableHeight < 0) return;
  892. $('#videospace').width(availableWidth);
  893. $('#videospace').height(availableHeight);
  894. $('#largeVideoContainer').width(availableWidth);
  895. $('#largeVideoContainer').height(availableHeight);
  896. var avatarSize = interfaceConfig.ACTIVE_SPEAKER_AVATAR_SIZE;
  897. var top = availableHeight / 2 - avatarSize / 4 * 3;
  898. $('#activeSpeaker').css('top', top);
  899. VideoLayout.resizeThumbnails();
  900. };
  901. /**
  902. * Resizes thumbnails.
  903. */
  904. my.resizeThumbnails = function() {
  905. var videoSpaceWidth = $('#remoteVideos').width();
  906. var thumbnailSize = VideoLayout.calculateThumbnailSize(videoSpaceWidth);
  907. var width = thumbnailSize[0];
  908. var height = thumbnailSize[1];
  909. // size videos so that while keeping AR and max height, we have a
  910. // nice fit
  911. $('#remoteVideos').height(height);
  912. $('#remoteVideos>span').width(width);
  913. $('#remoteVideos>span').height(height);
  914. $('.userAvatar').css('left', (width - height) / 2);
  915. $(document).trigger("remotevideo.resized", [width, height]);
  916. };
  917. /**
  918. * Enables the dominant speaker UI.
  919. *
  920. * @param resourceJid the jid indicating the video element to
  921. * activate/deactivate
  922. * @param isEnable indicates if the dominant speaker should be enabled or
  923. * disabled
  924. */
  925. my.enableDominantSpeaker = function(resourceJid, isEnable) {
  926. var videoSpanId = null;
  927. var videoContainerId = null;
  928. if (resourceJid
  929. === Strophe.getResourceFromJid(connection.emuc.myroomjid)) {
  930. videoSpanId = 'localVideoWrapper';
  931. videoContainerId = 'localVideoContainer';
  932. }
  933. else {
  934. videoSpanId = 'participant_' + resourceJid;
  935. videoContainerId = videoSpanId;
  936. }
  937. var displayName = resourceJid;
  938. var nameSpan = $('#' + videoContainerId + '>span.displayname');
  939. if (nameSpan.length > 0)
  940. displayName = nameSpan.html();
  941. console.log("UI enable dominant speaker",
  942. displayName,
  943. resourceJid,
  944. isEnable);
  945. videoSpan = document.getElementById(videoContainerId);
  946. if (!videoSpan) {
  947. return;
  948. }
  949. var video = $('#' + videoSpanId + '>video');
  950. if (video && video.length > 0) {
  951. if (isEnable) {
  952. var isLargeVideoVisible = VideoLayout.isLargeVideoOnTop();
  953. VideoLayout.showDisplayName(videoContainerId, isLargeVideoVisible);
  954. if (!videoSpan.classList.contains("dominantspeaker"))
  955. videoSpan.classList.add("dominantspeaker");
  956. }
  957. else {
  958. VideoLayout.showDisplayName(videoContainerId, false);
  959. if (videoSpan.classList.contains("dominantspeaker"))
  960. videoSpan.classList.remove("dominantspeaker");
  961. }
  962. Avatar.showUserAvatar(
  963. connection.emuc.findJidFromResource(resourceJid));
  964. }
  965. };
  966. /**
  967. * Gets the selector of video thumbnail container for the user identified by
  968. * given <tt>userJid</tt>
  969. * @param resourceJid user's Jid for whom we want to get the video container.
  970. */
  971. function getParticipantContainer(resourceJid)
  972. {
  973. if (!resourceJid)
  974. return null;
  975. if (resourceJid === Strophe.getResourceFromJid(connection.emuc.myroomjid))
  976. return $("#localVideoContainer");
  977. else
  978. return $("#participant_" + resourceJid);
  979. }
  980. /**
  981. * Sets the size and position of the given video element.
  982. *
  983. * @param video the video element to position
  984. * @param width the desired video width
  985. * @param height the desired video height
  986. * @param horizontalIndent the left and right indent
  987. * @param verticalIndent the top and bottom indent
  988. */
  989. function positionVideo(video,
  990. width,
  991. height,
  992. horizontalIndent,
  993. verticalIndent) {
  994. video.width(width);
  995. video.height(height);
  996. video.css({ top: verticalIndent + 'px',
  997. bottom: verticalIndent + 'px',
  998. left: horizontalIndent + 'px',
  999. right: horizontalIndent + 'px'});
  1000. }
  1001. /**
  1002. * Calculates the thumbnail size.
  1003. *
  1004. * @param videoSpaceWidth the width of the video space
  1005. */
  1006. my.calculateThumbnailSize = function (videoSpaceWidth) {
  1007. // Calculate the available height, which is the inner window height minus
  1008. // 39px for the header minus 2px for the delimiter lines on the top and
  1009. // bottom of the large video, minus the 36px space inside the remoteVideos
  1010. // container used for highlighting shadow.
  1011. var availableHeight = 100;
  1012. var numvids = $('#remoteVideos>span:visible').length;
  1013. if (localLastNCount && localLastNCount > 0) {
  1014. numvids = Math.min(localLastNCount + 1, numvids);
  1015. }
  1016. // Remove the 3px borders arround videos and border around the remote
  1017. // videos area and the 4 pixels between the local video and the others
  1018. //TODO: Find out where the 4 pixels come from and remove them
  1019. var availableWinWidth = videoSpaceWidth - 2 * 3 * numvids - 70 - 4;
  1020. var availableWidth = availableWinWidth / numvids;
  1021. var aspectRatio = 16.0 / 9.0;
  1022. var maxHeight = Math.min(160, availableHeight);
  1023. availableHeight = Math.min(maxHeight, availableWidth / aspectRatio);
  1024. if (availableHeight < availableWidth / aspectRatio) {
  1025. availableWidth = Math.floor(availableHeight * aspectRatio);
  1026. }
  1027. return [availableWidth, availableHeight];
  1028. };
  1029. /**
  1030. * Returns an array of the video dimensions, so that it keeps it's aspect
  1031. * ratio and fits available area with it's larger dimension. This method
  1032. * ensures that whole video will be visible and can leave empty areas.
  1033. *
  1034. * @return an array with 2 elements, the video width and the video height
  1035. */
  1036. function getDesktopVideoSize(videoWidth,
  1037. videoHeight,
  1038. videoSpaceWidth,
  1039. videoSpaceHeight) {
  1040. if (!videoWidth)
  1041. videoWidth = currentVideoWidth;
  1042. if (!videoHeight)
  1043. videoHeight = currentVideoHeight;
  1044. var aspectRatio = videoWidth / videoHeight;
  1045. var availableWidth = Math.max(videoWidth, videoSpaceWidth);
  1046. var availableHeight = Math.max(videoHeight, videoSpaceHeight);
  1047. videoSpaceHeight -= $('#remoteVideos').outerHeight();
  1048. if (availableWidth / aspectRatio >= videoSpaceHeight)
  1049. {
  1050. availableHeight = videoSpaceHeight;
  1051. availableWidth = availableHeight * aspectRatio;
  1052. }
  1053. if (availableHeight * aspectRatio >= videoSpaceWidth)
  1054. {
  1055. availableWidth = videoSpaceWidth;
  1056. availableHeight = availableWidth / aspectRatio;
  1057. }
  1058. return [availableWidth, availableHeight];
  1059. }
  1060. /**
  1061. * Creates the edit display name button.
  1062. *
  1063. * @returns the edit button
  1064. */
  1065. function createEditDisplayNameButton() {
  1066. var editButton = document.createElement('a');
  1067. editButton.className = 'displayname';
  1068. Util.setTooltip(editButton,
  1069. 'Click to edit your<br/>display name',
  1070. "top");
  1071. editButton.innerHTML = '<i class="fa fa-pencil"></i>';
  1072. return editButton;
  1073. }
  1074. /**
  1075. * Creates the element indicating the moderator(owner) of the conference.
  1076. *
  1077. * @param parentElement the parent element where the owner indicator will
  1078. * be added
  1079. */
  1080. function createModeratorIndicatorElement(parentElement) {
  1081. var moderatorIndicator = document.createElement('i');
  1082. moderatorIndicator.className = 'fa fa-star';
  1083. parentElement.appendChild(moderatorIndicator);
  1084. Util.setTooltip(parentElement,
  1085. "The owner of<br/>this conference",
  1086. "top");
  1087. }
  1088. /**
  1089. * Updates the remote video menu.
  1090. *
  1091. * @param jid the jid indicating the video for which we're adding a menu.
  1092. * @param isMuted indicates the current mute state
  1093. */
  1094. my.updateRemoteVideoMenu = function(jid, isMuted) {
  1095. var muteMenuItem
  1096. = $('#remote_popupmenu_'
  1097. + Strophe.getResourceFromJid(jid)
  1098. + '>li>a.mutelink');
  1099. var mutedIndicator = "<i class='icon-mic-disabled'></i>";
  1100. if (muteMenuItem.length) {
  1101. var muteLink = muteMenuItem.get(0);
  1102. if (isMuted === 'true') {
  1103. muteLink.innerHTML = mutedIndicator + ' Muted';
  1104. muteLink.className = 'mutelink disabled';
  1105. }
  1106. else {
  1107. muteLink.innerHTML = mutedIndicator + ' Mute';
  1108. muteLink.className = 'mutelink';
  1109. }
  1110. }
  1111. };
  1112. /**
  1113. * Returns the current dominant speaker resource jid.
  1114. */
  1115. my.getDominantSpeakerResourceJid = function () {
  1116. return currentDominantSpeaker;
  1117. };
  1118. /**
  1119. * Returns the corresponding resource jid to the given peer container
  1120. * DOM element.
  1121. *
  1122. * @return the corresponding resource jid to the given peer container
  1123. * DOM element
  1124. */
  1125. my.getPeerContainerResourceJid = function (containerElement) {
  1126. var i = containerElement.id.indexOf('participant_');
  1127. if (i >= 0)
  1128. return containerElement.id.substring(i + 12);
  1129. };
  1130. /**
  1131. * Adds the remote video menu element for the given <tt>jid</tt> in the
  1132. * given <tt>parentElement</tt>.
  1133. *
  1134. * @param jid the jid indicating the video for which we're adding a menu.
  1135. * @param parentElement the parent element where this menu will be added
  1136. */
  1137. function addRemoteVideoMenu(jid, parentElement) {
  1138. var spanElement = document.createElement('span');
  1139. spanElement.className = 'remotevideomenu';
  1140. parentElement.appendChild(spanElement);
  1141. var menuElement = document.createElement('i');
  1142. menuElement.className = 'fa fa-angle-down';
  1143. menuElement.title = 'Remote user controls';
  1144. spanElement.appendChild(menuElement);
  1145. // <ul class="popupmenu">
  1146. // <li><a href="#">Mute</a></li>
  1147. // <li><a href="#">Eject</a></li>
  1148. // </ul>
  1149. var popupmenuElement = document.createElement('ul');
  1150. popupmenuElement.className = 'popupmenu';
  1151. popupmenuElement.id
  1152. = 'remote_popupmenu_' + Strophe.getResourceFromJid(jid);
  1153. spanElement.appendChild(popupmenuElement);
  1154. var muteMenuItem = document.createElement('li');
  1155. var muteLinkItem = document.createElement('a');
  1156. var mutedIndicator = "<i class='icon-mic-disabled'></i>";
  1157. if (!mutedAudios[jid]) {
  1158. muteLinkItem.innerHTML = mutedIndicator + 'Mute';
  1159. muteLinkItem.className = 'mutelink';
  1160. }
  1161. else {
  1162. muteLinkItem.innerHTML = mutedIndicator + ' Muted';
  1163. muteLinkItem.className = 'mutelink disabled';
  1164. }
  1165. muteLinkItem.onclick = function(){
  1166. if ($(this).attr('disabled') != undefined) {
  1167. event.preventDefault();
  1168. }
  1169. var isMute = mutedAudios[jid] == true;
  1170. connection.moderate.setMute(jid, !isMute);
  1171. popupmenuElement.setAttribute('style', 'display:none;');
  1172. if (isMute) {
  1173. this.innerHTML = mutedIndicator + ' Muted';
  1174. this.className = 'mutelink disabled';
  1175. }
  1176. else {
  1177. this.innerHTML = mutedIndicator + ' Mute';
  1178. this.className = 'mutelink';
  1179. }
  1180. };
  1181. muteMenuItem.appendChild(muteLinkItem);
  1182. popupmenuElement.appendChild(muteMenuItem);
  1183. var ejectIndicator = "<i class='fa fa-eject'></i>";
  1184. var ejectMenuItem = document.createElement('li');
  1185. var ejectLinkItem = document.createElement('a');
  1186. ejectLinkItem.innerHTML = ejectIndicator + ' Kick out';
  1187. ejectLinkItem.onclick = function(){
  1188. connection.moderate.eject(jid);
  1189. popupmenuElement.setAttribute('style', 'display:none;');
  1190. };
  1191. ejectMenuItem.appendChild(ejectLinkItem);
  1192. popupmenuElement.appendChild(ejectMenuItem);
  1193. var paddingSpan = document.createElement('span');
  1194. paddingSpan.className = 'popupmenuPadding';
  1195. popupmenuElement.appendChild(paddingSpan);
  1196. }
  1197. /**
  1198. * Removes remote video menu element from video element identified by
  1199. * given <tt>videoElementId</tt>.
  1200. *
  1201. * @param videoElementId the id of local or remote video element.
  1202. */
  1203. function removeRemoteVideoMenu(videoElementId) {
  1204. var menuSpan = $('#' + videoElementId + '>span.remotevideomenu');
  1205. if (menuSpan.length) {
  1206. menuSpan.remove();
  1207. }
  1208. }
  1209. /**
  1210. * On contact list item clicked.
  1211. */
  1212. $(ContactList).bind('contactclicked', function(event, jid) {
  1213. if (!jid) {
  1214. return;
  1215. }
  1216. var resource = Strophe.getResourceFromJid(jid);
  1217. var videoContainer = $("#participant_" + resource);
  1218. if (videoContainer.length > 0) {
  1219. var videoThumb = $('video', videoContainer).get(0);
  1220. // It is not always the case that a videoThumb exists (if there is
  1221. // no actual video).
  1222. if (videoThumb) {
  1223. if (videoThumb.src && videoThumb.src != '') {
  1224. // We have a video src, great! Let's update the large video
  1225. // now.
  1226. VideoLayout.handleVideoThumbClicked(
  1227. videoThumb.src,
  1228. false,
  1229. Strophe.getResourceFromJid(jid));
  1230. } else {
  1231. // If we don't have a video src for jid, there's absolutely
  1232. // no point in calling handleVideoThumbClicked; Quite
  1233. // simply, it won't work because it needs an src to attach
  1234. // to the large video.
  1235. //
  1236. // Instead, we trigger the pinned endpoint changed event to
  1237. // let the bridge adjust its lastN set for myjid and store
  1238. // the pinned user in the lastNPickupJid variable to be
  1239. // picked up later by the lastN changed event handler.
  1240. lastNPickupJid = jid;
  1241. $(document).trigger("pinnedendpointchanged", [jid]);
  1242. }
  1243. } else if (jid == connection.emuc.myroomjid) {
  1244. $("#localVideoContainer").click();
  1245. }
  1246. }
  1247. });
  1248. /**
  1249. * On audio muted event.
  1250. */
  1251. $(document).bind('audiomuted.muc', function (event, jid, isMuted) {
  1252. /*
  1253. // FIXME: but focus can not mute in this case ? - check
  1254. if (jid === connection.emuc.myroomjid) {
  1255. // The local mute indicator is controlled locally
  1256. return;
  1257. }*/
  1258. var videoSpanId = null;
  1259. if (jid === connection.emuc.myroomjid) {
  1260. videoSpanId = 'localVideoContainer';
  1261. } else {
  1262. VideoLayout.ensurePeerContainerExists(jid);
  1263. videoSpanId = 'participant_' + Strophe.getResourceFromJid(jid);
  1264. }
  1265. mutedAudios[jid] = isMuted;
  1266. if (Moderator.isModerator()) {
  1267. VideoLayout.updateRemoteVideoMenu(jid, isMuted);
  1268. }
  1269. if (videoSpanId)
  1270. VideoLayout.showAudioIndicator(videoSpanId, isMuted);
  1271. });
  1272. /**
  1273. * On video muted event.
  1274. */
  1275. $(document).bind('videomuted.muc', function (event, jid, isMuted) {
  1276. if(!RTC.muteRemoteVideoStream(jid, isMuted))
  1277. return;
  1278. Avatar.showUserAvatar(jid, isMuted);
  1279. var videoSpanId = null;
  1280. if (jid === connection.emuc.myroomjid) {
  1281. videoSpanId = 'localVideoContainer';
  1282. } else {
  1283. VideoLayout.ensurePeerContainerExists(jid);
  1284. videoSpanId = 'participant_' + Strophe.getResourceFromJid(jid);
  1285. }
  1286. if (videoSpanId)
  1287. VideoLayout.showVideoIndicator(videoSpanId, isMuted);
  1288. });
  1289. /**
  1290. * Display name changed.
  1291. */
  1292. $(document).bind('displaynamechanged',
  1293. function (event, jid, displayName, status) {
  1294. var name = null;
  1295. if (jid === 'localVideoContainer'
  1296. || jid === connection.emuc.myroomjid) {
  1297. name = nickname;
  1298. setDisplayName('localVideoContainer',
  1299. displayName);
  1300. } else {
  1301. VideoLayout.ensurePeerContainerExists(jid);
  1302. name = $('#participant_' + Strophe.getResourceFromJid(jid) + "_name").text();
  1303. setDisplayName(
  1304. 'participant_' + Strophe.getResourceFromJid(jid),
  1305. displayName,
  1306. status);
  1307. }
  1308. if(APIConnector.isEnabled() && APIConnector.isEventEnabled("displayNameChange"))
  1309. {
  1310. if(jid === 'localVideoContainer')
  1311. jid = connection.emuc.myroomjid;
  1312. if(!name || name != displayName)
  1313. APIConnector.triggerEvent("displayNameChange",{jid: jid, displayname: displayName});
  1314. }
  1315. });
  1316. /**
  1317. * On dominant speaker changed event.
  1318. */
  1319. $(document).bind('dominantspeakerchanged', function (event, resourceJid) {
  1320. // We ignore local user events.
  1321. if (resourceJid
  1322. === Strophe.getResourceFromJid(connection.emuc.myroomjid))
  1323. return;
  1324. // Update the current dominant speaker.
  1325. if (resourceJid !== currentDominantSpeaker) {
  1326. var oldSpeakerVideoSpanId = "participant_" + currentDominantSpeaker,
  1327. newSpeakerVideoSpanId = "participant_" + resourceJid;
  1328. if($("#" + oldSpeakerVideoSpanId + ">span.displayname").text() ===
  1329. interfaceConfig.DEFAULT_DOMINANT_SPEAKER_DISPLAY_NAME) {
  1330. setDisplayName(oldSpeakerVideoSpanId, null);
  1331. }
  1332. if($("#" + newSpeakerVideoSpanId + ">span.displayname").text() ===
  1333. interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME) {
  1334. setDisplayName(newSpeakerVideoSpanId,
  1335. interfaceConfig.DEFAULT_DOMINANT_SPEAKER_DISPLAY_NAME);
  1336. }
  1337. currentDominantSpeaker = resourceJid;
  1338. } else {
  1339. return;
  1340. }
  1341. // Obtain container for new dominant speaker.
  1342. var container = document.getElementById(
  1343. 'participant_' + resourceJid);
  1344. // Local video will not have container found, but that's ok
  1345. // since we don't want to switch to local video.
  1346. if (container && !focusedVideoInfo)
  1347. {
  1348. var video = container.getElementsByTagName("video");
  1349. // Update the large video if the video source is already available,
  1350. // otherwise wait for the "videoactive.jingle" event.
  1351. if (video.length && video[0].currentTime > 0)
  1352. VideoLayout.updateLargeVideo(RTC.getVideoSrc(video[0]), resourceJid);
  1353. }
  1354. });
  1355. /**
  1356. * On last N change event.
  1357. *
  1358. * @param event the event that notified us
  1359. * @param lastNEndpoints the list of last N endpoints
  1360. * @param endpointsEnteringLastN the list currently entering last N
  1361. * endpoints
  1362. */
  1363. $(document).bind('lastnchanged', function ( event,
  1364. lastNEndpoints,
  1365. endpointsEnteringLastN,
  1366. stream) {
  1367. if (lastNCount !== lastNEndpoints.length)
  1368. lastNCount = lastNEndpoints.length;
  1369. lastNEndpointsCache = lastNEndpoints;
  1370. // Say A, B, C, D, E, and F are in a conference and LastN = 3.
  1371. //
  1372. // If LastN drops to, say, 2, because of adaptivity, then E should see
  1373. // thumbnails for A, B and C. A and B are in E's server side LastN set,
  1374. // so E sees them. C is only in E's local LastN set.
  1375. //
  1376. // If F starts talking and LastN = 3, then E should see thumbnails for
  1377. // F, A, B. B gets "ejected" from E's server side LastN set, but it
  1378. // enters E's local LastN ejecting C.
  1379. // Increase the local LastN set size, if necessary.
  1380. if (lastNCount > localLastNCount) {
  1381. localLastNCount = lastNCount;
  1382. }
  1383. // Update the local LastN set preserving the order in which the
  1384. // endpoints appeared in the LastN/local LastN set.
  1385. var nextLocalLastNSet = lastNEndpoints.slice(0);
  1386. for (var i = 0; i < localLastNSet.length; i++) {
  1387. if (nextLocalLastNSet.length >= localLastNCount) {
  1388. break;
  1389. }
  1390. var resourceJid = localLastNSet[i];
  1391. if (nextLocalLastNSet.indexOf(resourceJid) === -1) {
  1392. nextLocalLastNSet.push(resourceJid);
  1393. }
  1394. }
  1395. localLastNSet = nextLocalLastNSet;
  1396. var updateLargeVideo = false;
  1397. // Handle LastN/local LastN changes.
  1398. $('#remoteVideos>span').each(function( index, element ) {
  1399. var resourceJid = VideoLayout.getPeerContainerResourceJid(element);
  1400. var isReceived = true;
  1401. if (resourceJid
  1402. && lastNEndpoints.indexOf(resourceJid) < 0
  1403. && localLastNSet.indexOf(resourceJid) < 0) {
  1404. console.log("Remove from last N", resourceJid);
  1405. showPeerContainer(resourceJid, 'hide');
  1406. isReceived = false;
  1407. } else if (resourceJid
  1408. && $('#participant_' + resourceJid).is(':visible')
  1409. && lastNEndpoints.indexOf(resourceJid) < 0
  1410. && localLastNSet.indexOf(resourceJid) >= 0) {
  1411. showPeerContainer(resourceJid, 'avatar');
  1412. isReceived = false;
  1413. }
  1414. if (!isReceived) {
  1415. // resourceJid has dropped out of the server side lastN set, so
  1416. // it is no longer being received. If resourceJid was being
  1417. // displayed in the large video we have to switch to another
  1418. // user.
  1419. var largeVideoResource = largeVideoState.userResourceJid;
  1420. if (!updateLargeVideo && resourceJid === largeVideoResource) {
  1421. updateLargeVideo = true;
  1422. }
  1423. }
  1424. });
  1425. if (!endpointsEnteringLastN || endpointsEnteringLastN.length < 0)
  1426. endpointsEnteringLastN = lastNEndpoints;
  1427. if (endpointsEnteringLastN && endpointsEnteringLastN.length > 0) {
  1428. endpointsEnteringLastN.forEach(function (resourceJid) {
  1429. var isVisible = $('#participant_' + resourceJid).is(':visible');
  1430. showPeerContainer(resourceJid, 'show');
  1431. if (!isVisible) {
  1432. console.log("Add to last N", resourceJid);
  1433. var jid = connection.emuc.findJidFromResource(resourceJid);
  1434. var mediaStream = RTC.remoteStreams[jid][MediaStreamType.VIDEO_TYPE];
  1435. var sel = $('#participant_' + resourceJid + '>video');
  1436. var videoStream = simulcast.getReceivingVideoStream(
  1437. mediaStream.stream);
  1438. RTC.attachMediaStream(sel, videoStream);
  1439. if (lastNPickupJid == mediaStream.peerjid) {
  1440. // Clean up the lastN pickup jid.
  1441. lastNPickupJid = null;
  1442. // Don't fire the events again, they've already
  1443. // been fired in the contact list click handler.
  1444. VideoLayout.handleVideoThumbClicked(
  1445. $(sel).attr('src'),
  1446. false,
  1447. Strophe.getResourceFromJid(mediaStream.peerjid));
  1448. updateLargeVideo = false;
  1449. }
  1450. waitForRemoteVideo(sel, mediaStream.ssrc, mediaStream.stream, resourceJid);
  1451. }
  1452. })
  1453. }
  1454. // The endpoint that was being shown in the large video has dropped out
  1455. // of the lastN set and there was no lastN pickup jid. We need to update
  1456. // the large video now.
  1457. if (updateLargeVideo) {
  1458. var resource, container, src;
  1459. var myResource
  1460. = Strophe.getResourceFromJid(connection.emuc.myroomjid);
  1461. // Find out which endpoint to show in the large video.
  1462. for (var i = 0; i < lastNEndpoints.length; i++) {
  1463. resource = lastNEndpoints[i];
  1464. if (!resource || resource === myResource)
  1465. continue;
  1466. container = $("#participant_" + resource);
  1467. if (container.length == 0)
  1468. continue;
  1469. src = $('video', container).attr('src');
  1470. if (!src)
  1471. continue;
  1472. // videoSrcToSsrc needs to be update for this call to succeed.
  1473. VideoLayout.updateLargeVideo(src);
  1474. break;
  1475. }
  1476. }
  1477. });
  1478. $(document).bind('videoactive.jingle', function (event, videoelem) {
  1479. if (videoelem.attr('id').indexOf('mixedmslabel') === -1) {
  1480. // ignore mixedmslabela0 and v0
  1481. videoelem.show();
  1482. VideoLayout.resizeThumbnails();
  1483. var videoParent = videoelem.parent();
  1484. var parentResourceJid = null;
  1485. if (videoParent)
  1486. parentResourceJid
  1487. = VideoLayout.getPeerContainerResourceJid(videoParent[0]);
  1488. // Update the large video to the last added video only if there's no
  1489. // current dominant, focused speaker or prezi playing or update it to
  1490. // the current dominant speaker.
  1491. if ((!focusedVideoInfo &&
  1492. !VideoLayout.getDominantSpeakerResourceJid() &&
  1493. !Prezi.isPresentationVisible()) ||
  1494. (parentResourceJid &&
  1495. VideoLayout.getDominantSpeakerResourceJid() === parentResourceJid)) {
  1496. VideoLayout.updateLargeVideo(
  1497. RTC.getVideoSrc(videoelem[0]),
  1498. 1,
  1499. parentResourceJid);
  1500. }
  1501. VideoLayout.showModeratorIndicator();
  1502. }
  1503. });
  1504. $(document).bind('simulcastlayerschanging', function (event, endpointSimulcastLayers) {
  1505. endpointSimulcastLayers.forEach(function (esl) {
  1506. var resource = esl.endpoint;
  1507. // if lastN is enabled *and* the endpoint is *not* in the lastN set,
  1508. // then ignore the event (= do not preload anything).
  1509. //
  1510. // The bridge could probably stop sending this message if it's for
  1511. // an endpoint that's not in lastN.
  1512. if (lastNCount != -1
  1513. && (lastNCount < 1 || lastNEndpointsCache.indexOf(resource) === -1)) {
  1514. return;
  1515. }
  1516. var primarySSRC = esl.simulcastLayer.primarySSRC;
  1517. // Get session and stream from primary ssrc.
  1518. var res = simulcast.getReceivingVideoStreamBySSRC(primarySSRC);
  1519. var session = res.session;
  1520. var electedStream = res.stream;
  1521. if (session && electedStream) {
  1522. var msid = simulcast.getRemoteVideoStreamIdBySSRC(primarySSRC);
  1523. console.info([esl, primarySSRC, msid, session, electedStream]);
  1524. var msidParts = msid.split(' ');
  1525. var preload = (Strophe.getResourceFromJid(ssrc2jid[primarySSRC]) == largeVideoState.userResourceJid);
  1526. if (preload) {
  1527. if (largeVideoState.preload)
  1528. {
  1529. $(largeVideoState.preload).remove();
  1530. }
  1531. console.info('Preloading remote video');
  1532. largeVideoState.preload = $('<video autoplay></video>');
  1533. // ssrcs are unique in an rtp session
  1534. largeVideoState.preload_ssrc = primarySSRC;
  1535. RTC.attachMediaStream(largeVideoState.preload, electedStream)
  1536. }
  1537. } else {
  1538. console.error('Could not find a stream or a session.', session, electedStream);
  1539. }
  1540. });
  1541. });
  1542. /**
  1543. * On simulcast layers changed event.
  1544. */
  1545. $(document).bind('simulcastlayerschanged', function (event, endpointSimulcastLayers) {
  1546. endpointSimulcastLayers.forEach(function (esl) {
  1547. var resource = esl.endpoint;
  1548. // if lastN is enabled *and* the endpoint is *not* in the lastN set,
  1549. // then ignore the event (= do not change large video/thumbnail
  1550. // SRCs).
  1551. //
  1552. // Note that even if we ignore the "changed" event in this event
  1553. // handler, the bridge must continue sending these events because
  1554. // the simulcast code in simulcast.js uses it to know what's going
  1555. // to be streamed by the bridge when/if the endpoint gets back into
  1556. // the lastN set.
  1557. if (lastNCount != -1
  1558. && (lastNCount < 1 || lastNEndpointsCache.indexOf(resource) === -1)) {
  1559. return;
  1560. }
  1561. var primarySSRC = esl.simulcastLayer.primarySSRC;
  1562. // Get session and stream from primary ssrc.
  1563. var res = simulcast.getReceivingVideoStreamBySSRC(primarySSRC);
  1564. var session = res.session;
  1565. var electedStream = res.stream;
  1566. if (session && electedStream) {
  1567. var msid = simulcast.getRemoteVideoStreamIdBySSRC(primarySSRC);
  1568. console.info('Switching simulcast substream.');
  1569. console.info([esl, primarySSRC, msid, session, electedStream]);
  1570. var msidParts = msid.split(' ');
  1571. var selRemoteVideo = $(['#', 'remoteVideo_', session.sid, '_', msidParts[0]].join(''));
  1572. var updateLargeVideo = (Strophe.getResourceFromJid(ssrc2jid[primarySSRC])
  1573. == largeVideoState.userResourceJid);
  1574. var updateFocusedVideoSrc = (focusedVideoInfo && focusedVideoInfo.src && focusedVideoInfo.src != '' &&
  1575. (RTC.getVideoSrc(selRemoteVideo[0]) == focusedVideoInfo.src));
  1576. var electedStreamUrl;
  1577. if (largeVideoState.preload_ssrc == primarySSRC)
  1578. {
  1579. RTC.setVideoSrc(selRemoteVideo[0], RTC.getVideoSrc(largeVideoState.preload[0]));
  1580. }
  1581. else
  1582. {
  1583. if (largeVideoState.preload
  1584. && largeVideoState.preload != null) {
  1585. $(largeVideoState.preload).remove();
  1586. }
  1587. largeVideoState.preload_ssrc = 0;
  1588. RTC.attachMediaStream(selRemoteVideo, electedStream);
  1589. }
  1590. var jid = ssrc2jid[primarySSRC];
  1591. jid2Ssrc[jid] = primarySSRC;
  1592. if (updateLargeVideo) {
  1593. VideoLayout.updateLargeVideo(RTC.getVideoSrc(selRemoteVideo[0]), null,
  1594. Strophe.getResourceFromJid(jid));
  1595. }
  1596. if (updateFocusedVideoSrc) {
  1597. focusedVideoInfo.src = RTC.getVideoSrc(selRemoteVideo[0]);
  1598. }
  1599. var videoId;
  1600. if(resource == Strophe.getResourceFromJid(connection.emuc.myroomjid))
  1601. {
  1602. videoId = "localVideoContainer";
  1603. }
  1604. else
  1605. {
  1606. videoId = "participant_" + resource;
  1607. }
  1608. var connectionIndicator = VideoLayout.connectionIndicators[videoId];
  1609. if(connectionIndicator)
  1610. connectionIndicator.updatePopoverData();
  1611. } else {
  1612. console.error('Could not find a stream or a session.', session, electedStream);
  1613. }
  1614. });
  1615. });
  1616. /**
  1617. * Constructs new connection indicator.
  1618. * @param videoContainer the video container associated with the indicator.
  1619. * @constructor
  1620. */
  1621. function ConnectionIndicator(videoContainer, jid)
  1622. {
  1623. this.videoContainer = videoContainer;
  1624. this.bandwidth = null;
  1625. this.packetLoss = null;
  1626. this.bitrate = null;
  1627. this.showMoreValue = false;
  1628. this.resolution = null;
  1629. this.transport = [];
  1630. this.popover = null;
  1631. this.jid = jid;
  1632. this.create();
  1633. }
  1634. /**
  1635. * Values for the connection quality
  1636. * @type {{98: string, 81: string, 64: string, 47: string, 30: string, 0: string}}
  1637. */
  1638. ConnectionIndicator.connectionQualityValues = {
  1639. 98: "18px", //full
  1640. 81: "15px",//4 bars
  1641. 64: "11px",//3 bars
  1642. 47: "7px",//2 bars
  1643. 30: "3px",//1 bar
  1644. 0: "0px"//empty
  1645. };
  1646. ConnectionIndicator.getIP = function(value)
  1647. {
  1648. return value.substring(0, value.lastIndexOf(":"));
  1649. };
  1650. ConnectionIndicator.getPort = function(value)
  1651. {
  1652. return value.substring(value.lastIndexOf(":") + 1, value.length);
  1653. };
  1654. ConnectionIndicator.getStringFromArray = function (array) {
  1655. var res = "";
  1656. for(var i = 0; i < array.length; i++)
  1657. {
  1658. res += (i == 0? "" : ", ") + array[i];
  1659. }
  1660. return res;
  1661. }
  1662. /**
  1663. * Generates the html content.
  1664. * @returns {string} the html content.
  1665. */
  1666. ConnectionIndicator.prototype.generateText = function () {
  1667. var downloadBitrate, uploadBitrate, packetLoss, resolution;
  1668. if(this.bitrate === null)
  1669. {
  1670. downloadBitrate = "N/A";
  1671. uploadBitrate = "N/A";
  1672. }
  1673. else
  1674. {
  1675. downloadBitrate = this.bitrate.download? this.bitrate.download + " Kbps" : "N/A";
  1676. uploadBitrate = this.bitrate.upload? this.bitrate.upload + " Kbps" : "N/A";
  1677. }
  1678. if(this.packetLoss === null)
  1679. {
  1680. packetLoss = "N/A";
  1681. }
  1682. else
  1683. {
  1684. packetLoss = "<span class='jitsipopover_green'>&darr;</span>" +
  1685. (this.packetLoss.download != null? this.packetLoss.download : "N/A") +
  1686. "% <span class='jitsipopover_orange'>&uarr;</span>" +
  1687. (this.packetLoss.upload != null? this.packetLoss.upload : "N/A") + "%";
  1688. }
  1689. var resolutionValue = null;
  1690. if(this.resolution)
  1691. {
  1692. var keys = Object.keys(this.resolution);
  1693. if(keys.length == 1)
  1694. {
  1695. for(var ssrc in this.resolution)
  1696. {
  1697. resolutionValue = this.resolution[ssrc];
  1698. }
  1699. }
  1700. else if(keys.length > 1)
  1701. {
  1702. var displayedSsrc = simulcast.getReceivingSSRC(this.jid);
  1703. resolutionValue = this.resolution[displayedSsrc];
  1704. }
  1705. }
  1706. if(this.jid==null)
  1707. {
  1708. resolution = "";
  1709. if(this.resolution == null || !Object.keys(this.resolution)
  1710. || Object.keys(this.resolution).length == 0)
  1711. {
  1712. resolution = "N/A";
  1713. }
  1714. else
  1715. for(var i in this.resolution)
  1716. {
  1717. resolutionValue = this.resolution[i];
  1718. if(resolutionValue)
  1719. {
  1720. if(resolutionValue.height &&
  1721. resolutionValue.width)
  1722. {
  1723. resolution += (resolution == ""? "" : ", ")
  1724. + resolutionValue.width + "x" + resolutionValue.height;
  1725. }
  1726. }
  1727. }
  1728. }
  1729. else if(!resolutionValue ||
  1730. !resolutionValue.height ||
  1731. !resolutionValue.width)
  1732. {
  1733. resolution = "N/A";
  1734. }
  1735. else
  1736. {
  1737. resolution = resolutionValue.width + "x" + resolutionValue.height;
  1738. }
  1739. var result = "<table style='width:100%'><tr><td><span class='jitsipopover_blue'>Bitrate:</span></td><td><span class='jitsipopover_green'>&darr;</span>" +
  1740. downloadBitrate + " <span class='jitsipopover_orange'>&uarr;</span>" +
  1741. uploadBitrate + "</td></tr><tr><td>" +
  1742. "<tr><td><span class='jitsipopover_blue'>Packet loss: </span></td><td>" + packetLoss + "</td></tr><tr><td>" +
  1743. "<span class='jitsipopover_blue'>Resolution:</span></td><td>" + resolution + "</td></tr></table>";
  1744. if(this.videoContainer.id == "localVideoContainer")
  1745. result += "<div class=\"jitsipopover_showmore\" onclick = \"VideoLayout.connectionIndicators['" +
  1746. this.videoContainer.id + "'].showMore()\">" + (this.showMoreValue? "Show less" : "Show More") + "</div><br />";
  1747. if(this.showMoreValue)
  1748. {
  1749. var downloadBandwidth, uploadBandwidth, transport;
  1750. if(this.bandwidth === null)
  1751. {
  1752. downloadBandwidth = "N/A";
  1753. uploadBandwidth = "N/A";
  1754. }
  1755. else
  1756. {
  1757. downloadBandwidth = this.bandwidth.download? this.bandwidth.download + " Kbps" : "N/A";
  1758. uploadBandwidth = this.bandwidth.upload? this.bandwidth.upload + " Kbps" : "N/A";
  1759. }
  1760. if(!this.transport || this.transport.length === 0)
  1761. {
  1762. transport = "<tr><td><span class='jitsipopover_blue'>Address:</span></td><td> N/A</td></tr>";
  1763. }
  1764. else
  1765. {
  1766. var data = {remoteIP: [], localIP:[], remotePort:[], localPort:[]};
  1767. for(var i = 0; i < this.transport.length; i++)
  1768. {
  1769. var ip = ConnectionIndicator.getIP(this.transport[i].ip);
  1770. var port = ConnectionIndicator.getPort(this.transport[i].ip);
  1771. var localIP = ConnectionIndicator.getIP(this.transport[i].localip);
  1772. var localPort = ConnectionIndicator.getPort(this.transport[i].localip);
  1773. if(data.remoteIP.indexOf(ip) == -1)
  1774. {
  1775. data.remoteIP.push(ip);
  1776. }
  1777. if(data.remotePort.indexOf(port) == -1)
  1778. {
  1779. data.remotePort.push(port);
  1780. }
  1781. if(data.localIP.indexOf(localIP) == -1)
  1782. {
  1783. data.localIP.push(localIP);
  1784. }
  1785. if(data.localPort.indexOf(localPort) == -1)
  1786. {
  1787. data.localPort.push(localPort);
  1788. }
  1789. }
  1790. var localTransport = "<tr><td><span class='jitsipopover_blue'>Local address" +
  1791. (data.localIP.length > 1? "es" : "") + ": </span></td><td> " +
  1792. ConnectionIndicator.getStringFromArray(data.localIP) + "</td></tr>";
  1793. transport = "<tr><td><span class='jitsipopover_blue'>Remote address"+
  1794. (data.remoteIP.length > 1? "es" : "") + ":</span></td><td> " +
  1795. ConnectionIndicator.getStringFromArray(data.remoteIP) + "</td></tr>";
  1796. if(this.transport.length > 1)
  1797. {
  1798. transport += "<tr><td><span class='jitsipopover_blue'>Remote ports:</span></td><td>";
  1799. localTransport += "<tr><td><span class='jitsipopover_blue'>Local ports:</span></td><td>";
  1800. }
  1801. else
  1802. {
  1803. transport += "<tr><td><span class='jitsipopover_blue'>Remote port:</span></td><td>";
  1804. localTransport += "<tr><td><span class='jitsipopover_blue'>Local port:</span></td><td>";
  1805. }
  1806. transport += ConnectionIndicator.getStringFromArray(data.remotePort);
  1807. localTransport += ConnectionIndicator.getStringFromArray(data.localPort);
  1808. transport += "</td></tr>";
  1809. transport += localTransport + "</td></tr>";
  1810. transport +="<tr><td><span class='jitsipopover_blue'>Transport:</span></td><td>" + this.transport[0].type + "</td></tr>";
  1811. }
  1812. result += "<table style='width:100%'><tr><td><span class='jitsipopover_blue'>Estimated bandwidth:</span> </td><td>" +
  1813. "<span class='jitsipopover_green'>&darr;</span>" + downloadBandwidth +
  1814. " <span class='jitsipopover_orange'>&uarr;</span>" +
  1815. uploadBandwidth + "</td></tr>";
  1816. result += transport + "</table>";
  1817. }
  1818. return result;
  1819. };
  1820. /**
  1821. * Shows or hide the additional information.
  1822. */
  1823. ConnectionIndicator.prototype.showMore = function () {
  1824. this.showMoreValue = !this.showMoreValue;
  1825. this.updatePopoverData();
  1826. };
  1827. /**
  1828. * Creates the indicator
  1829. */
  1830. ConnectionIndicator.prototype.create = function () {
  1831. this.connectionIndicatorContainer = document.createElement("div");
  1832. this.connectionIndicatorContainer.className = "connectionindicator";
  1833. this.connectionIndicatorContainer.style.display = "none";
  1834. this.videoContainer.appendChild(this.connectionIndicatorContainer);
  1835. this.popover = new JitsiPopover($("#" + this.videoContainer.id + " > .connectionindicator"),
  1836. {content: "<div class=\"connection_info\">Come back here for " +
  1837. "connection information once the conference starts</div>", skin: "black"});
  1838. function createIcon(classes)
  1839. {
  1840. var icon = document.createElement("span");
  1841. for(var i in classes)
  1842. {
  1843. icon.classList.add(classes[i]);
  1844. }
  1845. icon.appendChild(document.createElement("i")).classList.add("icon-connection");
  1846. return icon;
  1847. }
  1848. this.emptyIcon = this.connectionIndicatorContainer.appendChild(
  1849. createIcon(["connection", "connection_empty"]));
  1850. this.fullIcon = this.connectionIndicatorContainer.appendChild(
  1851. createIcon(["connection", "connection_full"]));
  1852. };
  1853. /**
  1854. * Removes the indicator
  1855. */
  1856. ConnectionIndicator.prototype.remove = function()
  1857. {
  1858. this.connectionIndicatorContainer.remove();
  1859. this.popover.forceHide();
  1860. };
  1861. /**
  1862. * Updates the data of the indicator
  1863. * @param percent the percent of connection quality
  1864. * @param object the statistics data.
  1865. */
  1866. ConnectionIndicator.prototype.updateConnectionQuality = function (percent, object) {
  1867. if(percent === null)
  1868. {
  1869. this.connectionIndicatorContainer.style.display = "none";
  1870. this.popover.forceHide();
  1871. return;
  1872. }
  1873. else
  1874. {
  1875. if(this.connectionIndicatorContainer.style.display == "none") {
  1876. this.connectionIndicatorContainer.style.display = "block";
  1877. VideoLayout.updateMutePosition(this.videoContainer.id);
  1878. }
  1879. }
  1880. this.bandwidth = object.bandwidth;
  1881. this.bitrate = object.bitrate;
  1882. this.packetLoss = object.packetLoss;
  1883. this.transport = object.transport;
  1884. if(object.resolution)
  1885. {
  1886. this.resolution = object.resolution;
  1887. }
  1888. for(var quality in ConnectionIndicator.connectionQualityValues)
  1889. {
  1890. if(percent >= quality)
  1891. {
  1892. this.fullIcon.style.width = ConnectionIndicator.connectionQualityValues[quality];
  1893. }
  1894. }
  1895. this.updatePopoverData();
  1896. };
  1897. /**
  1898. * Updates the resolution
  1899. * @param resolution the new resolution
  1900. */
  1901. ConnectionIndicator.prototype.updateResolution = function (resolution) {
  1902. this.resolution = resolution;
  1903. this.updatePopoverData();
  1904. };
  1905. /**
  1906. * Updates the content of the popover
  1907. */
  1908. ConnectionIndicator.prototype.updatePopoverData = function () {
  1909. this.popover.updateContent("<div class=\"connection_info\">" + this.generateText() + "</div>");
  1910. };
  1911. /**
  1912. * Hides the popover
  1913. */
  1914. ConnectionIndicator.prototype.hide = function () {
  1915. this.popover.forceHide();
  1916. };
  1917. /**
  1918. * Hides the indicator
  1919. */
  1920. ConnectionIndicator.prototype.hideIndicator = function () {
  1921. this.connectionIndicatorContainer.style.display = "none";
  1922. if(this.popover)
  1923. this.popover.forceHide();
  1924. };
  1925. /**
  1926. * Updates the data for the indicator
  1927. * @param id the id of the indicator
  1928. * @param percent the percent for connection quality
  1929. * @param object the data
  1930. */
  1931. function updateStatsIndicator(id, percent, object) {
  1932. if(VideoLayout.connectionIndicators[id])
  1933. VideoLayout.connectionIndicators[id].updateConnectionQuality(percent, object);
  1934. }
  1935. /**
  1936. * Updates local stats
  1937. * @param percent
  1938. * @param object
  1939. */
  1940. my.updateLocalConnectionStats = function (percent, object) {
  1941. var resolution = null;
  1942. if(object.resolution !== null)
  1943. {
  1944. resolution = object.resolution;
  1945. object.resolution = resolution[connection.emuc.myroomjid];
  1946. delete resolution[connection.emuc.myroomjid];
  1947. }
  1948. updateStatsIndicator("localVideoContainer", percent, object);
  1949. for(var jid in resolution)
  1950. {
  1951. if(resolution[jid] === null)
  1952. continue;
  1953. var id = 'participant_' + Strophe.getResourceFromJid(jid);
  1954. if(VideoLayout.connectionIndicators[id])
  1955. {
  1956. VideoLayout.connectionIndicators[id].updateResolution(resolution[jid]);
  1957. }
  1958. }
  1959. };
  1960. /**
  1961. * Updates remote stats.
  1962. * @param jid the jid associated with the stats
  1963. * @param percent the connection quality percent
  1964. * @param object the stats data
  1965. */
  1966. my.updateConnectionStats = function (jid, percent, object) {
  1967. var resourceJid = Strophe.getResourceFromJid(jid);
  1968. var videoSpanId = 'participant_' + resourceJid;
  1969. updateStatsIndicator(videoSpanId, percent, object);
  1970. };
  1971. /**
  1972. * Removes the connection
  1973. * @param jid
  1974. */
  1975. my.removeConnectionIndicator = function (jid) {
  1976. if(VideoLayout.connectionIndicators['participant_' + Strophe.getResourceFromJid(jid)])
  1977. VideoLayout.connectionIndicators['participant_' + Strophe.getResourceFromJid(jid)].remove();
  1978. };
  1979. /**
  1980. * Hides the connection indicator
  1981. * @param jid
  1982. */
  1983. my.hideConnectionIndicator = function (jid) {
  1984. if(VideoLayout.connectionIndicators['participant_' + Strophe.getResourceFromJid(jid)])
  1985. VideoLayout.connectionIndicators['participant_' + Strophe.getResourceFromJid(jid)].hide();
  1986. };
  1987. /**
  1988. * Hides all the indicators
  1989. */
  1990. my.onStatsStop = function () {
  1991. for(var indicator in VideoLayout.connectionIndicators)
  1992. {
  1993. VideoLayout.connectionIndicators[indicator].hideIndicator();
  1994. }
  1995. };
  1996. return my;
  1997. }(VideoLayout || {}));