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

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