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

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