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

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