您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

VideoLayout.js 77KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189
  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. /**
  19. * Currently focused video "src"(displayed in large video).
  20. * @type {String}
  21. */
  22. var focusedVideoInfo = null;
  23. /**
  24. * Indicates if we have muted our audio before the conference has started.
  25. * @type {boolean}
  26. */
  27. var preMuted = false;
  28. var mutedAudios = {};
  29. var flipXLocalVideo = true;
  30. var currentVideoWidth = null;
  31. var currentVideoHeight = null;
  32. var localVideoSrc = null;
  33. var defaultLocalDisplayName = "Me";
  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 () {
  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. };
  428. my.isInLastN = function(resource) {
  429. return lastNCount < 0 // lastN is disabled, return true
  430. || (lastNCount > 0 && lastNEndpointsCache.length == 0) // lastNEndpoints cache not built yet, return true
  431. || (lastNEndpointsCache && lastNEndpointsCache.indexOf(resource) !== -1);
  432. };
  433. my.changeLocalStream = function (stream) {
  434. VideoLayout.changeLocalVideo(stream);
  435. };
  436. my.changeLocalAudio = function(stream) {
  437. RTC.attachMediaStream($('#localAudio'), stream.getOriginalStream());
  438. document.getElementById('localAudio').autoplay = true;
  439. document.getElementById('localAudio').volume = 0;
  440. if (preMuted) {
  441. if(!UI.setAudioMuted(true))
  442. {
  443. preMuted = mute;
  444. }
  445. preMuted = false;
  446. }
  447. };
  448. my.changeLocalVideo = function(stream) {
  449. var flipX = true;
  450. if(stream.videoType == "screen")
  451. flipX = false;
  452. var localVideo = document.createElement('video');
  453. localVideo.id = 'localVideo_' +
  454. RTC.getStreamID(stream.getOriginalStream());
  455. localVideo.autoplay = true;
  456. localVideo.volume = 0; // is it required if audio is separated ?
  457. localVideo.oncontextmenu = function () { return false; };
  458. var localVideoContainer = document.getElementById('localVideoWrapper');
  459. localVideoContainer.appendChild(localVideo);
  460. // Set default display name.
  461. setDisplayName('localVideoContainer');
  462. if(!VideoLayout.connectionIndicators["localVideoContainer"]) {
  463. VideoLayout.connectionIndicators["localVideoContainer"]
  464. = new ConnectionIndicator($("#localVideoContainer")[0], null, VideoLayout);
  465. }
  466. AudioLevels.updateAudioLevelCanvas(null, VideoLayout);
  467. var localVideoSelector = $('#' + localVideo.id);
  468. // Add click handler to both video and video wrapper elements in case
  469. // there's no video.
  470. localVideoSelector.click(function (event) {
  471. event.stopPropagation();
  472. VideoLayout.handleVideoThumbClicked(
  473. RTC.getVideoSrc(localVideo),
  474. false,
  475. xmpp.myResource());
  476. });
  477. $('#localVideoContainer').click(function (event) {
  478. event.stopPropagation();
  479. VideoLayout.handleVideoThumbClicked(
  480. RTC.getVideoSrc(localVideo),
  481. false,
  482. xmpp.myResource());
  483. });
  484. // Add hover handler
  485. $('#localVideoContainer').hover(
  486. function() {
  487. VideoLayout.showDisplayName('localVideoContainer', true);
  488. },
  489. function() {
  490. if (!VideoLayout.isLargeVideoVisible()
  491. || RTC.getVideoSrc(localVideo) !== RTC.getVideoSrc($('#largeVideo')[0]))
  492. VideoLayout.showDisplayName('localVideoContainer', false);
  493. }
  494. );
  495. // Add stream ended handler
  496. stream.getOriginalStream().onended = function () {
  497. localVideoContainer.removeChild(localVideo);
  498. VideoLayout.updateRemovedVideo(RTC.getVideoSrc(localVideo));
  499. };
  500. // Flip video x axis if needed
  501. flipXLocalVideo = flipX;
  502. if (flipX) {
  503. localVideoSelector.addClass("flipVideoX");
  504. }
  505. // Attach WebRTC stream
  506. var videoStream = simulcast.getLocalVideoStream();
  507. RTC.attachMediaStream(localVideoSelector, videoStream);
  508. localVideoSrc = RTC.getVideoSrc(localVideo);
  509. var myResourceJid = xmpp.myResource();
  510. VideoLayout.updateLargeVideo(localVideoSrc, 0,
  511. myResourceJid);
  512. };
  513. /**
  514. * Checks if removed video is currently displayed and tries to display
  515. * another one instead.
  516. * @param removedVideoSrc src stream identifier of the video.
  517. */
  518. my.updateRemovedVideo = function(removedVideoSrc) {
  519. if (removedVideoSrc === RTC.getVideoSrc($('#largeVideo')[0])) {
  520. // this is currently displayed as large
  521. // pick the last visible video in the row
  522. // if nobody else is left, this picks the local video
  523. var pick
  524. = $('#remoteVideos>span[id!="mixedstream"]:visible:last>video')
  525. .get(0);
  526. if (!pick) {
  527. console.info("Last visible video no longer exists");
  528. pick = $('#remoteVideos>span[id!="mixedstream"]>video').get(0);
  529. if (!pick || !RTC.getVideoSrc(pick)) {
  530. // Try local video
  531. console.info("Fallback to local video...");
  532. pick = $('#remoteVideos>span>span>video').get(0);
  533. }
  534. }
  535. // mute if localvideo
  536. if (pick) {
  537. var container = pick.parentNode;
  538. var jid = null;
  539. if(container)
  540. {
  541. if(container.id == "localVideoWrapper")
  542. {
  543. jid = xmpp.myResource();
  544. }
  545. else
  546. {
  547. jid = VideoLayout.getPeerContainerResourceJid(container);
  548. }
  549. }
  550. VideoLayout.updateLargeVideo(RTC.getVideoSrc(pick), pick.volume, jid);
  551. } else {
  552. console.warn("Failed to elect large video");
  553. }
  554. }
  555. };
  556. my.onRemoteStreamAdded = function (stream) {
  557. var container;
  558. var remotes = document.getElementById('remoteVideos');
  559. if (stream.peerjid) {
  560. VideoLayout.ensurePeerContainerExists(stream.peerjid);
  561. container = document.getElementById(
  562. 'participant_' + Strophe.getResourceFromJid(stream.peerjid));
  563. } else {
  564. var id = stream.getOriginalStream().id;
  565. if (id !== 'mixedmslabel'
  566. // FIXME: default stream is added always with new focus
  567. // (to be investigated)
  568. && id !== 'default') {
  569. console.error('can not associate stream',
  570. id,
  571. 'with a participant');
  572. // We don't want to add it here since it will cause troubles
  573. return;
  574. }
  575. // FIXME: for the mixed ms we dont need a video -- currently
  576. container = document.createElement('span');
  577. container.id = 'mixedstream';
  578. container.className = 'videocontainer';
  579. remotes.appendChild(container);
  580. UIUtil.playSoundNotification('userJoined');
  581. }
  582. if (container) {
  583. VideoLayout.addRemoteStreamElement( container,
  584. stream.sid,
  585. stream.getOriginalStream(),
  586. stream.peerjid,
  587. stream.ssrc);
  588. }
  589. }
  590. my.getLargeVideoState = function () {
  591. return largeVideoState;
  592. };
  593. /**
  594. * Updates the large video with the given new video source.
  595. */
  596. my.updateLargeVideo = function(newSrc, vol, resourceJid) {
  597. console.log('hover in', newSrc);
  598. if (RTC.getVideoSrc($('#largeVideo')[0]) !== newSrc) {
  599. $('#activeSpeaker').css('visibility', 'hidden');
  600. // Due to the simulcast the localVideoSrc may have changed when the
  601. // fadeOut event triggers. In that case the getJidFromVideoSrc and
  602. // isVideoSrcDesktop methods will not function correctly.
  603. //
  604. // Also, again due to the simulcast, the updateLargeVideo method can
  605. // be called multiple times almost simultaneously. Therefore, we
  606. // store the state here and update only once.
  607. largeVideoState.newSrc = newSrc;
  608. largeVideoState.isVisible = $('#largeVideo').is(':visible');
  609. largeVideoState.isDesktop = RTC.isVideoSrcDesktop(resourceJid);
  610. if(largeVideoState.userResourceJid) {
  611. largeVideoState.oldResourceJid = largeVideoState.userResourceJid;
  612. } else {
  613. largeVideoState.oldResourceJid = null;
  614. }
  615. largeVideoState.userResourceJid = resourceJid;
  616. // Screen stream is already rotated
  617. largeVideoState.flipX = (newSrc === localVideoSrc) && flipXLocalVideo;
  618. var userChanged = false;
  619. if (largeVideoState.oldResourceJid !== largeVideoState.userResourceJid) {
  620. userChanged = true;
  621. // we want the notification to trigger even if userJid is undefined,
  622. // or null.
  623. $(document).trigger("selectedendpointchanged", [largeVideoState.userResourceJid]);
  624. }
  625. if (!largeVideoState.updateInProgress) {
  626. largeVideoState.updateInProgress = true;
  627. var doUpdate = function () {
  628. Avatar.updateActiveSpeakerAvatarSrc(
  629. xmpp.findJidFromResource(
  630. largeVideoState.userResourceJid));
  631. if (!userChanged && largeVideoState.preload &&
  632. largeVideoState.preload !== null &&
  633. RTC.getVideoSrc($(largeVideoState.preload)[0]) === newSrc)
  634. {
  635. console.info('Switching to preloaded video');
  636. var attributes = $('#largeVideo').prop("attributes");
  637. // loop through largeVideo attributes and apply them on
  638. // preload.
  639. $.each(attributes, function () {
  640. if (this.name !== 'id' && this.name !== 'src') {
  641. largeVideoState.preload.attr(this.name, this.value);
  642. }
  643. });
  644. largeVideoState.preload.appendTo($('#largeVideoContainer'));
  645. $('#largeVideo').attr('id', 'previousLargeVideo');
  646. largeVideoState.preload.attr('id', 'largeVideo');
  647. $('#previousLargeVideo').remove();
  648. largeVideoState.preload.on('loadedmetadata', function (e) {
  649. currentVideoWidth = this.videoWidth;
  650. currentVideoHeight = this.videoHeight;
  651. VideoLayout.positionLarge(currentVideoWidth, currentVideoHeight);
  652. });
  653. largeVideoState.preload = null;
  654. largeVideoState.preload_ssrc = 0;
  655. } else {
  656. RTC.setVideoSrc($('#largeVideo')[0], largeVideoState.newSrc);
  657. }
  658. var videoTransform = document.getElementById('largeVideo')
  659. .style.webkitTransform;
  660. if (largeVideoState.flipX && videoTransform !== 'scaleX(-1)') {
  661. document.getElementById('largeVideo').style.webkitTransform
  662. = "scaleX(-1)";
  663. }
  664. else if (!largeVideoState.flipX && videoTransform === 'scaleX(-1)') {
  665. document.getElementById('largeVideo').style.webkitTransform
  666. = "none";
  667. }
  668. // Change the way we'll be measuring and positioning large video
  669. VideoLayout.getVideoSize = largeVideoState.isDesktop
  670. ? getDesktopVideoSize
  671. : getCameraVideoSize;
  672. VideoLayout.getVideoPosition = largeVideoState.isDesktop
  673. ? getDesktopVideoPosition
  674. : getCameraVideoPosition;
  675. // Only if the large video is currently visible.
  676. // Disable previous dominant speaker video.
  677. if (largeVideoState.oldResourceJid) {
  678. VideoLayout.enableDominantSpeaker(
  679. largeVideoState.oldResourceJid,
  680. false);
  681. }
  682. // Enable new dominant speaker in the remote videos section.
  683. if (largeVideoState.userResourceJid) {
  684. VideoLayout.enableDominantSpeaker(
  685. largeVideoState.userResourceJid,
  686. true);
  687. }
  688. if (userChanged && largeVideoState.isVisible) {
  689. // using "this" should be ok because we're called
  690. // from within the fadeOut event.
  691. $(this).fadeIn(300);
  692. }
  693. if(userChanged) {
  694. Avatar.showUserAvatar(
  695. xmpp.findJidFromResource(
  696. largeVideoState.oldResourceJid));
  697. }
  698. largeVideoState.updateInProgress = false;
  699. };
  700. if (userChanged) {
  701. $('#largeVideo').fadeOut(300, doUpdate);
  702. } else {
  703. doUpdate();
  704. }
  705. }
  706. } else {
  707. Avatar.showUserAvatar(
  708. xmpp.findJidFromResource(
  709. largeVideoState.userResourceJid));
  710. }
  711. };
  712. my.handleVideoThumbClicked = function(videoSrc,
  713. noPinnedEndpointChangedEvent,
  714. resourceJid) {
  715. // Restore style for previously focused video
  716. var oldContainer = null;
  717. if(focusedVideoInfo) {
  718. var focusResourceJid = focusedVideoInfo.resourceJid;
  719. oldContainer = getParticipantContainer(focusResourceJid);
  720. }
  721. if (oldContainer) {
  722. oldContainer.removeClass("videoContainerFocused");
  723. }
  724. // Unlock current focused.
  725. if (focusedVideoInfo && focusedVideoInfo.src === videoSrc)
  726. {
  727. focusedVideoInfo = null;
  728. var dominantSpeakerVideo = null;
  729. // Enable the currently set dominant speaker.
  730. if (currentDominantSpeaker) {
  731. dominantSpeakerVideo
  732. = $('#participant_' + currentDominantSpeaker + '>video')
  733. .get(0);
  734. if (dominantSpeakerVideo) {
  735. VideoLayout.updateLargeVideo(
  736. RTC.getVideoSrc(dominantSpeakerVideo),
  737. 1,
  738. currentDominantSpeaker);
  739. }
  740. }
  741. if (!noPinnedEndpointChangedEvent) {
  742. $(document).trigger("pinnedendpointchanged");
  743. }
  744. return;
  745. }
  746. // Lock new video
  747. focusedVideoInfo = {
  748. src: videoSrc,
  749. resourceJid: resourceJid
  750. };
  751. // Update focused/pinned interface.
  752. if (resourceJid)
  753. {
  754. var container = getParticipantContainer(resourceJid);
  755. container.addClass("videoContainerFocused");
  756. if (!noPinnedEndpointChangedEvent) {
  757. $(document).trigger("pinnedendpointchanged", [resourceJid]);
  758. }
  759. }
  760. if ($('#largeVideo').attr('src') === videoSrc &&
  761. VideoLayout.isLargeVideoOnTop()) {
  762. return;
  763. }
  764. // Triggers a "video.selected" event. The "false" parameter indicates
  765. // this isn't a prezi.
  766. $(document).trigger("video.selected", [false]);
  767. VideoLayout.updateLargeVideo(videoSrc, 1, resourceJid);
  768. $('audio').each(function (idx, el) {
  769. if (el.id.indexOf('mixedmslabel') !== -1) {
  770. el.volume = 0;
  771. el.volume = 1;
  772. }
  773. });
  774. };
  775. /**
  776. * Positions the large video.
  777. *
  778. * @param videoWidth the stream video width
  779. * @param videoHeight the stream video height
  780. */
  781. my.positionLarge = function (videoWidth, videoHeight) {
  782. var videoSpaceWidth = $('#videospace').width();
  783. var videoSpaceHeight = window.innerHeight;
  784. var videoSize = VideoLayout.getVideoSize(videoWidth,
  785. videoHeight,
  786. videoSpaceWidth,
  787. videoSpaceHeight);
  788. var largeVideoWidth = videoSize[0];
  789. var largeVideoHeight = videoSize[1];
  790. var videoPosition = VideoLayout.getVideoPosition(largeVideoWidth,
  791. largeVideoHeight,
  792. videoSpaceWidth,
  793. videoSpaceHeight);
  794. var horizontalIndent = videoPosition[0];
  795. var verticalIndent = videoPosition[1];
  796. positionVideo($('#largeVideo'),
  797. largeVideoWidth,
  798. largeVideoHeight,
  799. horizontalIndent, verticalIndent);
  800. };
  801. /**
  802. * Shows/hides the large video.
  803. */
  804. my.setLargeVideoVisible = function(isVisible) {
  805. var resourceJid = largeVideoState.userResourceJid;
  806. if (isVisible) {
  807. $('#largeVideo').css({visibility: 'visible'});
  808. $('.watermark').css({visibility: 'visible'});
  809. VideoLayout.enableDominantSpeaker(resourceJid, true);
  810. }
  811. else {
  812. $('#largeVideo').css({visibility: 'hidden'});
  813. $('#activeSpeaker').css('visibility', 'hidden');
  814. $('.watermark').css({visibility: 'hidden'});
  815. VideoLayout.enableDominantSpeaker(resourceJid, false);
  816. if(focusedVideoInfo) {
  817. var focusResourceJid = focusedVideoInfo.resourceJid;
  818. var oldContainer = getParticipantContainer(focusResourceJid);
  819. if (oldContainer && oldContainer.length > 0) {
  820. oldContainer.removeClass("videoContainerFocused");
  821. }
  822. focusedVideoInfo = null;
  823. if(focusResourceJid) {
  824. Avatar.showUserAvatar(
  825. xmpp.findJidFromResource(focusResourceJid));
  826. }
  827. }
  828. }
  829. };
  830. /**
  831. * Indicates if the large video is currently visible.
  832. *
  833. * @return <tt>true</tt> if visible, <tt>false</tt> - otherwise
  834. */
  835. my.isLargeVideoVisible = function() {
  836. return $('#largeVideo').is(':visible');
  837. };
  838. my.isLargeVideoOnTop = function () {
  839. var Etherpad = require("../etherpad/Etherpad");
  840. var Prezi = require("../prezi/Prezi");
  841. return !Prezi.isPresentationVisible() && !Etherpad.isVisible();
  842. };
  843. /**
  844. * Checks if container for participant identified by given peerJid exists
  845. * in the document and creates it eventually.
  846. *
  847. * @param peerJid peer Jid to check.
  848. * @param userId user email or id for setting the avatar
  849. *
  850. * @return Returns <tt>true</tt> if the peer container exists,
  851. * <tt>false</tt> - otherwise
  852. */
  853. my.ensurePeerContainerExists = function(peerJid, userId) {
  854. ContactList.ensureAddContact(peerJid, userId);
  855. var resourceJid = Strophe.getResourceFromJid(peerJid);
  856. var videoSpanId = 'participant_' + resourceJid;
  857. if (!$('#' + videoSpanId).length) {
  858. var container =
  859. VideoLayout.addRemoteVideoContainer(peerJid, videoSpanId, userId);
  860. Avatar.setUserAvatar(peerJid, userId);
  861. // Set default display name.
  862. setDisplayName(videoSpanId);
  863. VideoLayout.connectionIndicators[videoSpanId] =
  864. new ConnectionIndicator(container, peerJid, VideoLayout);
  865. var nickfield = document.createElement('span');
  866. nickfield.className = "nick";
  867. nickfield.appendChild(document.createTextNode(resourceJid));
  868. container.appendChild(nickfield);
  869. // In case this is not currently in the last n we don't show it.
  870. if (localLastNCount
  871. && localLastNCount > 0
  872. && $('#remoteVideos>span').length >= localLastNCount + 2) {
  873. showPeerContainer(resourceJid, 'hide');
  874. }
  875. else
  876. VideoLayout.resizeThumbnails();
  877. }
  878. };
  879. my.addRemoteVideoContainer = function(peerJid, spanId) {
  880. var container = document.createElement('span');
  881. container.id = spanId;
  882. container.className = 'videocontainer';
  883. var remotes = document.getElementById('remoteVideos');
  884. // If the peerJid is null then this video span couldn't be directly
  885. // associated with a participant (this could happen in the case of prezi).
  886. if (xmpp.isModerator() && peerJid !== null)
  887. addRemoteVideoMenu(peerJid, container);
  888. remotes.appendChild(container);
  889. AudioLevels.updateAudioLevelCanvas(peerJid, VideoLayout);
  890. return container;
  891. };
  892. /**
  893. * Creates an audio or video stream element.
  894. */
  895. my.createStreamElement = function (sid, stream) {
  896. var isVideo = stream.getVideoTracks().length > 0;
  897. var element = isVideo
  898. ? document.createElement('video')
  899. : document.createElement('audio');
  900. var id = (isVideo ? 'remoteVideo_' : 'remoteAudio_')
  901. + sid + '_' + RTC.getStreamID(stream);
  902. element.id = id;
  903. element.autoplay = true;
  904. element.oncontextmenu = function () { return false; };
  905. return element;
  906. };
  907. my.addRemoteStreamElement
  908. = function (container, sid, stream, peerJid, thessrc) {
  909. var newElementId = null;
  910. var isVideo = stream.getVideoTracks().length > 0;
  911. if (container) {
  912. var streamElement = VideoLayout.createStreamElement(sid, stream);
  913. newElementId = streamElement.id;
  914. container.appendChild(streamElement);
  915. var sel = $('#' + newElementId);
  916. sel.hide();
  917. // If the container is currently visible we attach the stream.
  918. if (!isVideo
  919. || (container.offsetParent !== null && isVideo)) {
  920. var videoStream = simulcast.getReceivingVideoStream(stream);
  921. RTC.attachMediaStream(sel, videoStream);
  922. if (isVideo)
  923. waitForRemoteVideo(sel, thessrc, stream, peerJid);
  924. }
  925. stream.onended = function () {
  926. console.log('stream ended', this);
  927. VideoLayout.removeRemoteStreamElement(
  928. stream, isVideo, container);
  929. // NOTE(gp) it seems that under certain circumstances, the
  930. // onended event is not fired and thus the contact list is not
  931. // updated.
  932. //
  933. // The onended event of a stream should be fired when the SSRCs
  934. // corresponding to that stream are removed from the SDP; but
  935. // this doesn't seem to always be the case, resulting in ghost
  936. // contacts.
  937. //
  938. // In an attempt to fix the ghost contacts problem, I'm moving
  939. // the removeContact() method call in app.js, inside the
  940. // 'muc.left' event handler.
  941. //if (peerJid)
  942. // ContactList.removeContact(peerJid);
  943. };
  944. // Add click handler.
  945. container.onclick = function (event) {
  946. /*
  947. * FIXME It turns out that videoThumb may not exist (if there is
  948. * no actual video).
  949. */
  950. var videoThumb = $('#' + container.id + '>video').get(0);
  951. if (videoThumb) {
  952. VideoLayout.handleVideoThumbClicked(
  953. RTC.getVideoSrc(videoThumb),
  954. false,
  955. Strophe.getResourceFromJid(peerJid));
  956. }
  957. event.stopPropagation();
  958. event.preventDefault();
  959. return false;
  960. };
  961. // Add hover handler
  962. $(container).hover(
  963. function() {
  964. VideoLayout.showDisplayName(container.id, true);
  965. },
  966. function() {
  967. var videoSrc = null;
  968. if ($('#' + container.id + '>video')
  969. && $('#' + container.id + '>video').length > 0) {
  970. videoSrc = RTC.getVideoSrc($('#' + container.id + '>video').get(0));
  971. }
  972. // If the video has been "pinned" by the user we want to
  973. // keep the display name on place.
  974. if (!VideoLayout.isLargeVideoVisible()
  975. || videoSrc !== RTC.getVideoSrc($('#largeVideo')[0]))
  976. VideoLayout.showDisplayName(container.id, false);
  977. }
  978. );
  979. }
  980. return newElementId;
  981. };
  982. /**
  983. * Removes the remote stream element corresponding to the given stream and
  984. * parent container.
  985. *
  986. * @param stream the stream
  987. * @param isVideo <tt>true</tt> if given <tt>stream</tt> is a video one.
  988. * @param container
  989. */
  990. my.removeRemoteStreamElement = function (stream, isVideo, container) {
  991. if (!container)
  992. return;
  993. var select = null;
  994. var removedVideoSrc = null;
  995. if (isVideo) {
  996. select = $('#' + container.id + '>video');
  997. removedVideoSrc = RTC.getVideoSrc(select.get(0));
  998. }
  999. else
  1000. select = $('#' + container.id + '>audio');
  1001. // Mark video as removed to cancel waiting loop(if video is removed
  1002. // before has started)
  1003. select.removed = true;
  1004. select.remove();
  1005. var audioCount = $('#' + container.id + '>audio').length;
  1006. var videoCount = $('#' + container.id + '>video').length;
  1007. if (!audioCount && !videoCount) {
  1008. console.log("Remove whole user", container.id);
  1009. if(VideoLayout.connectionIndicators[container.id])
  1010. VideoLayout.connectionIndicators[container.id].remove();
  1011. // Remove whole container
  1012. container.remove();
  1013. UIUtil.playSoundNotification('userLeft');
  1014. VideoLayout.resizeThumbnails();
  1015. }
  1016. if (removedVideoSrc)
  1017. VideoLayout.updateRemovedVideo(removedVideoSrc);
  1018. };
  1019. /**
  1020. * Show/hide peer container for the given resourceJid.
  1021. */
  1022. function showPeerContainer(resourceJid, state) {
  1023. var peerContainer = $('#participant_' + resourceJid);
  1024. if (!peerContainer)
  1025. return;
  1026. var isHide = state === 'hide';
  1027. var resizeThumbnails = false;
  1028. if (!isHide) {
  1029. if (!peerContainer.is(':visible')) {
  1030. resizeThumbnails = true;
  1031. peerContainer.show();
  1032. }
  1033. if (state == 'show')
  1034. {
  1035. // peerContainer.css('-webkit-filter', '');
  1036. var jid = xmpp.findJidFromResource(resourceJid);
  1037. Avatar.showUserAvatar(jid, false);
  1038. }
  1039. else // if (state == 'avatar')
  1040. {
  1041. // peerContainer.css('-webkit-filter', 'grayscale(100%)');
  1042. var jid = xmpp.findJidFromResource(resourceJid);
  1043. Avatar.showUserAvatar(jid, true);
  1044. }
  1045. }
  1046. else if (peerContainer.is(':visible') && isHide)
  1047. {
  1048. resizeThumbnails = true;
  1049. peerContainer.hide();
  1050. if(VideoLayout.connectionIndicators['participant_' + resourceJid])
  1051. VideoLayout.connectionIndicators['participant_' + resourceJid].hide();
  1052. }
  1053. if (resizeThumbnails) {
  1054. VideoLayout.resizeThumbnails();
  1055. }
  1056. // We want to be able to pin a participant from the contact list, even
  1057. // if he's not in the lastN set!
  1058. // ContactList.setClickable(resourceJid, !isHide);
  1059. };
  1060. my.inputDisplayNameHandler = function (name) {
  1061. NicknameHandler.setNickname(name);
  1062. if (!$('#localDisplayName').is(":visible")) {
  1063. if (NicknameHandler.getNickname())
  1064. $('#localDisplayName').text(NicknameHandler.getNickname() + " (me)");
  1065. else
  1066. $('#localDisplayName')
  1067. .text(interfaceConfig.DEFAULT_LOCAL_DISPLAY_NAME);
  1068. $('#localDisplayName').show();
  1069. }
  1070. $('#editDisplayName').hide();
  1071. };
  1072. /**
  1073. * Shows/hides the display name on the remote video.
  1074. * @param videoSpanId the identifier of the video span element
  1075. * @param isShow indicates if the display name should be shown or hidden
  1076. */
  1077. my.showDisplayName = function(videoSpanId, isShow) {
  1078. var nameSpan = $('#' + videoSpanId + '>span.displayname').get(0);
  1079. if (isShow) {
  1080. if (nameSpan && nameSpan.innerHTML && nameSpan.innerHTML.length)
  1081. nameSpan.setAttribute("style", "display:inline-block;");
  1082. }
  1083. else {
  1084. if (nameSpan)
  1085. nameSpan.setAttribute("style", "display:none;");
  1086. }
  1087. };
  1088. /**
  1089. * Shows the presence status message for the given video.
  1090. */
  1091. my.setPresenceStatus = function (videoSpanId, statusMsg) {
  1092. if (!$('#' + videoSpanId).length) {
  1093. // No container
  1094. return;
  1095. }
  1096. var statusSpan = $('#' + videoSpanId + '>span.status');
  1097. if (!statusSpan.length) {
  1098. //Add status span
  1099. statusSpan = document.createElement('span');
  1100. statusSpan.className = 'status';
  1101. statusSpan.id = videoSpanId + '_status';
  1102. $('#' + videoSpanId)[0].appendChild(statusSpan);
  1103. statusSpan = $('#' + videoSpanId + '>span.status');
  1104. }
  1105. // Display status
  1106. if (statusMsg && statusMsg.length) {
  1107. $('#' + videoSpanId + '_status').text(statusMsg);
  1108. statusSpan.get(0).setAttribute("style", "display:inline-block;");
  1109. }
  1110. else {
  1111. // Hide
  1112. statusSpan.get(0).setAttribute("style", "display:none;");
  1113. }
  1114. };
  1115. /**
  1116. * Shows a visual indicator for the moderator of the conference.
  1117. */
  1118. my.showModeratorIndicator = function () {
  1119. var isModerator = xmpp.isModerator();
  1120. if (isModerator) {
  1121. var indicatorSpan = $('#localVideoContainer .focusindicator');
  1122. if (indicatorSpan.children().length === 0)
  1123. {
  1124. createModeratorIndicatorElement(indicatorSpan[0]);
  1125. }
  1126. }
  1127. var members = xmpp.getMembers();
  1128. Object.keys(members).forEach(function (jid) {
  1129. if (Strophe.getResourceFromJid(jid) === 'focus') {
  1130. // Skip server side focus
  1131. return;
  1132. }
  1133. var resourceJid = Strophe.getResourceFromJid(jid);
  1134. var videoSpanId = 'participant_' + resourceJid;
  1135. var videoContainer = document.getElementById(videoSpanId);
  1136. if (!videoContainer) {
  1137. console.error("No video container for " + jid);
  1138. return;
  1139. }
  1140. var member = members[jid];
  1141. if (member.role === 'moderator') {
  1142. // Remove menu if peer is moderator
  1143. var menuSpan = $('#' + videoSpanId + '>span.remotevideomenu');
  1144. if (menuSpan.length) {
  1145. removeRemoteVideoMenu(videoSpanId);
  1146. }
  1147. // Show moderator indicator
  1148. var indicatorSpan
  1149. = $('#' + videoSpanId + ' .focusindicator');
  1150. if (!indicatorSpan || indicatorSpan.length === 0) {
  1151. indicatorSpan = document.createElement('span');
  1152. indicatorSpan.className = 'focusindicator';
  1153. videoContainer.appendChild(indicatorSpan);
  1154. createModeratorIndicatorElement(indicatorSpan);
  1155. }
  1156. } else if (isModerator) {
  1157. // We are moderator, but user is not - add menu
  1158. if ($('#remote_popupmenu_' + resourceJid).length <= 0) {
  1159. addRemoteVideoMenu(
  1160. jid,
  1161. document.getElementById('participant_' + resourceJid));
  1162. }
  1163. }
  1164. });
  1165. };
  1166. /**
  1167. * Shows video muted indicator over small videos.
  1168. */
  1169. my.showVideoIndicator = function(videoSpanId, isMuted) {
  1170. var videoMutedSpan = $('#' + videoSpanId + '>span.videoMuted');
  1171. if (isMuted === 'false') {
  1172. if (videoMutedSpan.length > 0) {
  1173. videoMutedSpan.remove();
  1174. }
  1175. }
  1176. else {
  1177. if(videoMutedSpan.length == 0) {
  1178. videoMutedSpan = document.createElement('span');
  1179. videoMutedSpan.className = 'videoMuted';
  1180. $('#' + videoSpanId)[0].appendChild(videoMutedSpan);
  1181. var mutedIndicator = document.createElement('i');
  1182. mutedIndicator.className = 'icon-camera-disabled';
  1183. UIUtil.setTooltip(mutedIndicator,
  1184. "Participant has<br/>stopped the camera.",
  1185. "top");
  1186. videoMutedSpan.appendChild(mutedIndicator);
  1187. }
  1188. VideoLayout.updateMutePosition(videoSpanId);
  1189. }
  1190. };
  1191. my.updateMutePosition = function (videoSpanId) {
  1192. var audioMutedSpan = $('#' + videoSpanId + '>span.audioMuted');
  1193. var connectionIndicator = $('#' + videoSpanId + '>div.connectionindicator');
  1194. var videoMutedSpan = $('#' + videoSpanId + '>span.videoMuted');
  1195. if(connectionIndicator.length > 0
  1196. && connectionIndicator[0].style.display != "none") {
  1197. audioMutedSpan.css({right: "23px"});
  1198. videoMutedSpan.css({right: ((audioMutedSpan.length > 0? 23 : 0) + 30) + "px"});
  1199. }
  1200. else
  1201. {
  1202. audioMutedSpan.css({right: "0px"});
  1203. videoMutedSpan.css({right: (audioMutedSpan.length > 0? 30 : 0) + "px"});
  1204. }
  1205. }
  1206. /**
  1207. * Shows audio muted indicator over small videos.
  1208. * @param {string} isMuted
  1209. */
  1210. my.showAudioIndicator = function(videoSpanId, isMuted) {
  1211. var audioMutedSpan = $('#' + videoSpanId + '>span.audioMuted');
  1212. if (isMuted === 'false') {
  1213. if (audioMutedSpan.length > 0) {
  1214. audioMutedSpan.popover('hide');
  1215. audioMutedSpan.remove();
  1216. }
  1217. }
  1218. else {
  1219. if(audioMutedSpan.length == 0 ) {
  1220. audioMutedSpan = document.createElement('span');
  1221. audioMutedSpan.className = 'audioMuted';
  1222. UIUtil.setTooltip(audioMutedSpan,
  1223. "Participant is muted",
  1224. "top");
  1225. $('#' + videoSpanId)[0].appendChild(audioMutedSpan);
  1226. var mutedIndicator = document.createElement('i');
  1227. mutedIndicator.className = 'icon-mic-disabled';
  1228. audioMutedSpan.appendChild(mutedIndicator);
  1229. }
  1230. VideoLayout.updateMutePosition(videoSpanId);
  1231. }
  1232. };
  1233. /*
  1234. * Shows or hides the audio muted indicator over the local thumbnail video.
  1235. * @param {boolean} isMuted
  1236. */
  1237. my.showLocalAudioIndicator = function(isMuted) {
  1238. VideoLayout.showAudioIndicator('localVideoContainer', isMuted.toString());
  1239. };
  1240. /**
  1241. * Resizes the large video container.
  1242. */
  1243. my.resizeLargeVideoContainer = function () {
  1244. Chat.resizeChat();
  1245. var availableHeight = window.innerHeight;
  1246. var availableWidth = UIUtil.getAvailableVideoWidth();
  1247. if (availableWidth < 0 || availableHeight < 0) return;
  1248. $('#videospace').width(availableWidth);
  1249. $('#videospace').height(availableHeight);
  1250. $('#largeVideoContainer').width(availableWidth);
  1251. $('#largeVideoContainer').height(availableHeight);
  1252. var avatarSize = interfaceConfig.ACTIVE_SPEAKER_AVATAR_SIZE;
  1253. var top = availableHeight / 2 - avatarSize / 4 * 3;
  1254. $('#activeSpeaker').css('top', top);
  1255. VideoLayout.resizeThumbnails();
  1256. };
  1257. /**
  1258. * Resizes thumbnails.
  1259. */
  1260. my.resizeThumbnails = function() {
  1261. var videoSpaceWidth = $('#remoteVideos').width();
  1262. var thumbnailSize = VideoLayout.calculateThumbnailSize(videoSpaceWidth);
  1263. var width = thumbnailSize[0];
  1264. var height = thumbnailSize[1];
  1265. // size videos so that while keeping AR and max height, we have a
  1266. // nice fit
  1267. $('#remoteVideos').height(height);
  1268. $('#remoteVideos>span').width(width);
  1269. $('#remoteVideos>span').height(height);
  1270. $('.userAvatar').css('left', (width - height) / 2);
  1271. $(document).trigger("remotevideo.resized", [width, height]);
  1272. };
  1273. /**
  1274. * Enables the dominant speaker UI.
  1275. *
  1276. * @param resourceJid the jid indicating the video element to
  1277. * activate/deactivate
  1278. * @param isEnable indicates if the dominant speaker should be enabled or
  1279. * disabled
  1280. */
  1281. my.enableDominantSpeaker = function(resourceJid, isEnable) {
  1282. var videoSpanId = null;
  1283. var videoContainerId = null;
  1284. if (resourceJid
  1285. === xmpp.myResource()) {
  1286. videoSpanId = 'localVideoWrapper';
  1287. videoContainerId = 'localVideoContainer';
  1288. }
  1289. else {
  1290. videoSpanId = 'participant_' + resourceJid;
  1291. videoContainerId = videoSpanId;
  1292. }
  1293. var displayName = resourceJid;
  1294. var nameSpan = $('#' + videoContainerId + '>span.displayname');
  1295. if (nameSpan.length > 0)
  1296. displayName = nameSpan.html();
  1297. console.log("UI enable dominant speaker",
  1298. displayName,
  1299. resourceJid,
  1300. isEnable);
  1301. videoSpan = document.getElementById(videoContainerId);
  1302. if (!videoSpan) {
  1303. return;
  1304. }
  1305. var video = $('#' + videoSpanId + '>video');
  1306. if (video && video.length > 0) {
  1307. if (isEnable) {
  1308. var isLargeVideoVisible = VideoLayout.isLargeVideoOnTop();
  1309. VideoLayout.showDisplayName(videoContainerId, isLargeVideoVisible);
  1310. if (!videoSpan.classList.contains("dominantspeaker"))
  1311. videoSpan.classList.add("dominantspeaker");
  1312. }
  1313. else {
  1314. VideoLayout.showDisplayName(videoContainerId, false);
  1315. if (videoSpan.classList.contains("dominantspeaker"))
  1316. videoSpan.classList.remove("dominantspeaker");
  1317. }
  1318. Avatar.showUserAvatar(
  1319. xmpp.findJidFromResource(resourceJid));
  1320. }
  1321. };
  1322. /**
  1323. * Calculates the thumbnail size.
  1324. *
  1325. * @param videoSpaceWidth the width of the video space
  1326. */
  1327. my.calculateThumbnailSize = function (videoSpaceWidth) {
  1328. // Calculate the available height, which is the inner window height minus
  1329. // 39px for the header minus 2px for the delimiter lines on the top and
  1330. // bottom of the large video, minus the 36px space inside the remoteVideos
  1331. // container used for highlighting shadow.
  1332. var availableHeight = 100;
  1333. var numvids = $('#remoteVideos>span:visible').length;
  1334. if (localLastNCount && localLastNCount > 0) {
  1335. numvids = Math.min(localLastNCount + 1, numvids);
  1336. }
  1337. // Remove the 3px borders arround videos and border around the remote
  1338. // videos area and the 4 pixels between the local video and the others
  1339. //TODO: Find out where the 4 pixels come from and remove them
  1340. var availableWinWidth = videoSpaceWidth - 2 * 3 * numvids - 70 - 4;
  1341. var availableWidth = availableWinWidth / numvids;
  1342. var aspectRatio = 16.0 / 9.0;
  1343. var maxHeight = Math.min(160, availableHeight);
  1344. availableHeight = Math.min(maxHeight, availableWidth / aspectRatio);
  1345. if (availableHeight < availableWidth / aspectRatio) {
  1346. availableWidth = Math.floor(availableHeight * aspectRatio);
  1347. }
  1348. return [availableWidth, availableHeight];
  1349. };
  1350. /**
  1351. * Updates the remote video menu.
  1352. *
  1353. * @param jid the jid indicating the video for which we're adding a menu.
  1354. * @param isMuted indicates the current mute state
  1355. */
  1356. my.updateRemoteVideoMenu = function(jid, isMuted) {
  1357. var muteMenuItem
  1358. = $('#remote_popupmenu_'
  1359. + Strophe.getResourceFromJid(jid)
  1360. + '>li>a.mutelink');
  1361. var mutedIndicator = "<i class='icon-mic-disabled'></i>";
  1362. if (muteMenuItem.length) {
  1363. var muteLink = muteMenuItem.get(0);
  1364. if (isMuted === 'true') {
  1365. muteLink.innerHTML = mutedIndicator + ' Muted';
  1366. muteLink.className = 'mutelink disabled';
  1367. }
  1368. else {
  1369. muteLink.innerHTML = mutedIndicator + ' Mute';
  1370. muteLink.className = 'mutelink';
  1371. }
  1372. }
  1373. };
  1374. /**
  1375. * Returns the current dominant speaker resource jid.
  1376. */
  1377. my.getDominantSpeakerResourceJid = function () {
  1378. return currentDominantSpeaker;
  1379. };
  1380. /**
  1381. * Returns the corresponding resource jid to the given peer container
  1382. * DOM element.
  1383. *
  1384. * @return the corresponding resource jid to the given peer container
  1385. * DOM element
  1386. */
  1387. my.getPeerContainerResourceJid = function (containerElement) {
  1388. var i = containerElement.id.indexOf('participant_');
  1389. if (i >= 0)
  1390. return containerElement.id.substring(i + 12);
  1391. };
  1392. /**
  1393. * On contact list item clicked.
  1394. */
  1395. $(ContactList).bind('contactclicked', function(event, jid) {
  1396. if (!jid) {
  1397. return;
  1398. }
  1399. var resource = Strophe.getResourceFromJid(jid);
  1400. var videoContainer = $("#participant_" + resource);
  1401. if (videoContainer.length > 0) {
  1402. var videoThumb = $('video', videoContainer).get(0);
  1403. // It is not always the case that a videoThumb exists (if there is
  1404. // no actual video).
  1405. if (videoThumb) {
  1406. if (videoThumb.src && videoThumb.src != '') {
  1407. // We have a video src, great! Let's update the large video
  1408. // now.
  1409. VideoLayout.handleVideoThumbClicked(
  1410. videoThumb.src,
  1411. false,
  1412. Strophe.getResourceFromJid(jid));
  1413. } else {
  1414. // If we don't have a video src for jid, there's absolutely
  1415. // no point in calling handleVideoThumbClicked; Quite
  1416. // simply, it won't work because it needs an src to attach
  1417. // to the large video.
  1418. //
  1419. // Instead, we trigger the pinned endpoint changed event to
  1420. // let the bridge adjust its lastN set for myjid and store
  1421. // the pinned user in the lastNPickupJid variable to be
  1422. // picked up later by the lastN changed event handler.
  1423. lastNPickupJid = jid;
  1424. $(document).trigger("pinnedendpointchanged", [Strophe.getResourceFromJid(jid)]);
  1425. }
  1426. } else if (jid == xmpp.myJid()) {
  1427. $("#localVideoContainer").click();
  1428. }
  1429. }
  1430. });
  1431. /**
  1432. * On audio muted event.
  1433. */
  1434. $(document).bind('audiomuted.muc', function (event, jid, isMuted) {
  1435. /*
  1436. // FIXME: but focus can not mute in this case ? - check
  1437. if (jid === xmpp.myJid()) {
  1438. // The local mute indicator is controlled locally
  1439. return;
  1440. }*/
  1441. var videoSpanId = null;
  1442. if (jid === xmpp.myJid()) {
  1443. videoSpanId = 'localVideoContainer';
  1444. } else {
  1445. VideoLayout.ensurePeerContainerExists(jid);
  1446. videoSpanId = 'participant_' + Strophe.getResourceFromJid(jid);
  1447. }
  1448. mutedAudios[jid] = isMuted;
  1449. if (xmpp.isModerator()) {
  1450. VideoLayout.updateRemoteVideoMenu(jid, isMuted);
  1451. }
  1452. if (videoSpanId)
  1453. VideoLayout.showAudioIndicator(videoSpanId, isMuted);
  1454. });
  1455. /**
  1456. * On video muted event.
  1457. */
  1458. $(document).bind('videomuted.muc', function (event, jid, value) {
  1459. var isMuted = (value === "true");
  1460. if(!RTC.muteRemoteVideoStream(jid, isMuted))
  1461. return;
  1462. Avatar.showUserAvatar(jid, isMuted);
  1463. var videoSpanId = null;
  1464. if (jid === xmpp.myJid()) {
  1465. videoSpanId = 'localVideoContainer';
  1466. } else {
  1467. VideoLayout.ensurePeerContainerExists(jid);
  1468. videoSpanId = 'participant_' + Strophe.getResourceFromJid(jid);
  1469. }
  1470. if (videoSpanId)
  1471. VideoLayout.showVideoIndicator(videoSpanId, value);
  1472. });
  1473. /**
  1474. * Display name changed.
  1475. */
  1476. my.onDisplayNameChanged =
  1477. function (jid, displayName, status) {
  1478. if (jid === 'localVideoContainer'
  1479. || jid === xmpp.myJid()) {
  1480. setDisplayName('localVideoContainer',
  1481. displayName);
  1482. } else {
  1483. VideoLayout.ensurePeerContainerExists(jid);
  1484. setDisplayName(
  1485. 'participant_' + Strophe.getResourceFromJid(jid),
  1486. displayName,
  1487. status);
  1488. }
  1489. };
  1490. /**
  1491. * On dominant speaker changed event.
  1492. */
  1493. $(document).bind('dominantspeakerchanged', function (event, resourceJid) {
  1494. // We ignore local user events.
  1495. if (resourceJid
  1496. === xmpp.myResource())
  1497. return;
  1498. // Update the current dominant speaker.
  1499. if (resourceJid !== currentDominantSpeaker) {
  1500. var oldSpeakerVideoSpanId = "participant_" + currentDominantSpeaker,
  1501. newSpeakerVideoSpanId = "participant_" + resourceJid;
  1502. if($("#" + oldSpeakerVideoSpanId + ">span.displayname").text() ===
  1503. interfaceConfig.DEFAULT_DOMINANT_SPEAKER_DISPLAY_NAME) {
  1504. setDisplayName(oldSpeakerVideoSpanId, null);
  1505. }
  1506. if($("#" + newSpeakerVideoSpanId + ">span.displayname").text() ===
  1507. interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME) {
  1508. setDisplayName(newSpeakerVideoSpanId,
  1509. interfaceConfig.DEFAULT_DOMINANT_SPEAKER_DISPLAY_NAME);
  1510. }
  1511. currentDominantSpeaker = resourceJid;
  1512. } else {
  1513. return;
  1514. }
  1515. // Obtain container for new dominant speaker.
  1516. var container = document.getElementById(
  1517. 'participant_' + resourceJid);
  1518. // Local video will not have container found, but that's ok
  1519. // since we don't want to switch to local video.
  1520. if (container && !focusedVideoInfo)
  1521. {
  1522. var video = container.getElementsByTagName("video");
  1523. // Update the large video if the video source is already available,
  1524. // otherwise wait for the "videoactive.jingle" event.
  1525. if (video.length && video[0].currentTime > 0)
  1526. VideoLayout.updateLargeVideo(RTC.getVideoSrc(video[0]), resourceJid);
  1527. }
  1528. });
  1529. /**
  1530. * On last N change event.
  1531. *
  1532. * @param event the event that notified us
  1533. * @param lastNEndpoints the list of last N endpoints
  1534. * @param endpointsEnteringLastN the list currently entering last N
  1535. * endpoints
  1536. */
  1537. $(document).bind('lastnchanged', function ( event,
  1538. lastNEndpoints,
  1539. endpointsEnteringLastN,
  1540. stream) {
  1541. if (lastNCount !== lastNEndpoints.length)
  1542. lastNCount = lastNEndpoints.length;
  1543. lastNEndpointsCache = lastNEndpoints;
  1544. // Say A, B, C, D, E, and F are in a conference and LastN = 3.
  1545. //
  1546. // If LastN drops to, say, 2, because of adaptivity, then E should see
  1547. // thumbnails for A, B and C. A and B are in E's server side LastN set,
  1548. // so E sees them. C is only in E's local LastN set.
  1549. //
  1550. // If F starts talking and LastN = 3, then E should see thumbnails for
  1551. // F, A, B. B gets "ejected" from E's server side LastN set, but it
  1552. // enters E's local LastN ejecting C.
  1553. // Increase the local LastN set size, if necessary.
  1554. if (lastNCount > localLastNCount) {
  1555. localLastNCount = lastNCount;
  1556. }
  1557. // Update the local LastN set preserving the order in which the
  1558. // endpoints appeared in the LastN/local LastN set.
  1559. var nextLocalLastNSet = lastNEndpoints.slice(0);
  1560. for (var i = 0; i < localLastNSet.length; i++) {
  1561. if (nextLocalLastNSet.length >= localLastNCount) {
  1562. break;
  1563. }
  1564. var resourceJid = localLastNSet[i];
  1565. if (nextLocalLastNSet.indexOf(resourceJid) === -1) {
  1566. nextLocalLastNSet.push(resourceJid);
  1567. }
  1568. }
  1569. localLastNSet = nextLocalLastNSet;
  1570. var updateLargeVideo = false;
  1571. // Handle LastN/local LastN changes.
  1572. $('#remoteVideos>span').each(function( index, element ) {
  1573. var resourceJid = VideoLayout.getPeerContainerResourceJid(element);
  1574. var isReceived = true;
  1575. if (resourceJid
  1576. && lastNEndpoints.indexOf(resourceJid) < 0
  1577. && localLastNSet.indexOf(resourceJid) < 0) {
  1578. console.log("Remove from last N", resourceJid);
  1579. showPeerContainer(resourceJid, 'hide');
  1580. isReceived = false;
  1581. } else if (resourceJid
  1582. && $('#participant_' + resourceJid).is(':visible')
  1583. && lastNEndpoints.indexOf(resourceJid) < 0
  1584. && localLastNSet.indexOf(resourceJid) >= 0) {
  1585. showPeerContainer(resourceJid, 'avatar');
  1586. isReceived = false;
  1587. }
  1588. if (!isReceived) {
  1589. // resourceJid has dropped out of the server side lastN set, so
  1590. // it is no longer being received. If resourceJid was being
  1591. // displayed in the large video we have to switch to another
  1592. // user.
  1593. var largeVideoResource = largeVideoState.userResourceJid;
  1594. if (!updateLargeVideo && resourceJid === largeVideoResource) {
  1595. updateLargeVideo = true;
  1596. }
  1597. }
  1598. });
  1599. if (!endpointsEnteringLastN || endpointsEnteringLastN.length < 0)
  1600. endpointsEnteringLastN = lastNEndpoints;
  1601. if (endpointsEnteringLastN && endpointsEnteringLastN.length > 0) {
  1602. endpointsEnteringLastN.forEach(function (resourceJid) {
  1603. var isVisible = $('#participant_' + resourceJid).is(':visible');
  1604. showPeerContainer(resourceJid, 'show');
  1605. if (!isVisible) {
  1606. console.log("Add to last N", resourceJid);
  1607. var jid = xmpp.findJidFromResource(resourceJid);
  1608. var mediaStream = RTC.remoteStreams[jid][MediaStreamType.VIDEO_TYPE];
  1609. var sel = $('#participant_' + resourceJid + '>video');
  1610. var videoStream = simulcast.getReceivingVideoStream(
  1611. mediaStream.stream);
  1612. RTC.attachMediaStream(sel, videoStream);
  1613. if (lastNPickupJid == mediaStream.peerjid) {
  1614. // Clean up the lastN pickup jid.
  1615. lastNPickupJid = null;
  1616. // Don't fire the events again, they've already
  1617. // been fired in the contact list click handler.
  1618. VideoLayout.handleVideoThumbClicked(
  1619. $(sel).attr('src'),
  1620. false,
  1621. Strophe.getResourceFromJid(mediaStream.peerjid));
  1622. updateLargeVideo = false;
  1623. }
  1624. waitForRemoteVideo(sel, mediaStream.ssrc, mediaStream.stream, resourceJid);
  1625. }
  1626. })
  1627. }
  1628. // The endpoint that was being shown in the large video has dropped out
  1629. // of the lastN set and there was no lastN pickup jid. We need to update
  1630. // the large video now.
  1631. if (updateLargeVideo) {
  1632. var resource, container, src;
  1633. var myResource
  1634. = xmpp.myResource();
  1635. // Find out which endpoint to show in the large video.
  1636. for (var i = 0; i < lastNEndpoints.length; i++) {
  1637. resource = lastNEndpoints[i];
  1638. if (!resource || resource === myResource)
  1639. continue;
  1640. container = $("#participant_" + resource);
  1641. if (container.length == 0)
  1642. continue;
  1643. src = $('video', container).attr('src');
  1644. if (!src)
  1645. continue;
  1646. // videoSrcToSsrc needs to be update for this call to succeed.
  1647. VideoLayout.updateLargeVideo(src);
  1648. break;
  1649. }
  1650. }
  1651. });
  1652. $(document).bind('simulcastlayerschanging', function (event, endpointSimulcastLayers) {
  1653. endpointSimulcastLayers.forEach(function (esl) {
  1654. var resource = esl.endpoint;
  1655. // if lastN is enabled *and* the endpoint is *not* in the lastN set,
  1656. // then ignore the event (= do not preload anything).
  1657. //
  1658. // The bridge could probably stop sending this message if it's for
  1659. // an endpoint that's not in lastN.
  1660. if (lastNCount != -1
  1661. && (lastNCount < 1 || lastNEndpointsCache.indexOf(resource) === -1)) {
  1662. return;
  1663. }
  1664. var primarySSRC = esl.simulcastLayer.primarySSRC;
  1665. // Get session and stream from primary ssrc.
  1666. var res = simulcast.getReceivingVideoStreamBySSRC(primarySSRC);
  1667. var sid = res.sid;
  1668. var electedStream = res.stream;
  1669. if (sid && electedStream) {
  1670. var msid = simulcast.getRemoteVideoStreamIdBySSRC(primarySSRC);
  1671. console.info([esl, primarySSRC, msid, sid, electedStream]);
  1672. var msidParts = msid.split(' ');
  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. $(document).bind('simulcastlayerschanged', function (event, 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;