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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172
  1. /* global config, APP, $, interfaceConfig, JitsiMeetJS */
  2. /* jshint -W101 */
  3. import AudioLevels from "../audio_levels/AudioLevels";
  4. import Avatar from "../avatar/Avatar";
  5. import FilmStrip from "./FilmStrip";
  6. import UIEvents from "../../../service/UI/UIEvents";
  7. import UIUtil from "../util/UIUtil";
  8. import RemoteVideo from "./RemoteVideo";
  9. import LargeVideoManager from "./LargeVideoManager";
  10. import {VIDEO_CONTAINER_TYPE} from "./VideoContainer";
  11. import {SHARED_VIDEO_CONTAINER_TYPE} from '../shared_video/SharedVideo';
  12. import LocalVideo from "./LocalVideo";
  13. const RTCUIUtil = JitsiMeetJS.util.RTCUIHelper;
  14. var remoteVideos = {};
  15. var localVideoThumbnail = null;
  16. var currentDominantSpeaker = null;
  17. var localLastNCount = config.channelLastN;
  18. var localLastNSet = [];
  19. var lastNEndpointsCache = [];
  20. var lastNPickupId = null;
  21. var eventEmitter = null;
  22. /**
  23. * Currently focused video jid
  24. * @type {String}
  25. */
  26. var pinnedId = null;
  27. /**
  28. * flipX state of the localVideo
  29. */
  30. let localFlipX = null;
  31. /**
  32. * On contact list item clicked.
  33. */
  34. function onContactClicked (id) {
  35. if (APP.conference.isLocalId(id)) {
  36. $("#localVideoContainer").click();
  37. return;
  38. }
  39. let remoteVideo = remoteVideos[id];
  40. if (remoteVideo && remoteVideo.hasVideo()) {
  41. // It is not always the case that a videoThumb exists (if there is
  42. // no actual video).
  43. if (remoteVideo.hasVideoStarted()) {
  44. // We have a video src, great! Let's update the large video
  45. // now.
  46. VideoLayout.handleVideoThumbClicked(id);
  47. } else {
  48. // If we don't have a video src for jid, there's absolutely
  49. // no point in calling handleVideoThumbClicked; Quite
  50. // simply, it won't work because it needs an src to attach
  51. // to the large video.
  52. //
  53. // Instead, we trigger the pinned endpoint changed event to
  54. // let the bridge adjust its lastN set for myjid and store
  55. // the pinned user in the lastNPickupId variable to be
  56. // picked up later by the lastN changed event handler.
  57. lastNPickupId = id;
  58. eventEmitter.emit(UIEvents.PINNED_ENDPOINT, remoteVideo, true);
  59. }
  60. }
  61. }
  62. /**
  63. * Returns the corresponding resource id to the given peer container
  64. * DOM element.
  65. *
  66. * @return the corresponding resource id to the given peer container
  67. * DOM element
  68. */
  69. function getPeerContainerResourceId (containerElement) {
  70. if (localVideoThumbnail.container === containerElement) {
  71. return localVideoThumbnail.id;
  72. }
  73. let i = containerElement.id.indexOf('participant_');
  74. if (i >= 0) {
  75. return containerElement.id.substring(i + 12);
  76. }
  77. }
  78. let largeVideo;
  79. var VideoLayout = {
  80. init (emitter) {
  81. eventEmitter = emitter;
  82. eventEmitter.addListener(UIEvents.LOCAL_FLIPX_CHANGED, function (val) {
  83. localFlipX = val;
  84. if(largeVideo)
  85. largeVideo.onLocalFlipXChange(val);
  86. });
  87. localVideoThumbnail = new LocalVideo(VideoLayout, emitter);
  88. // sets default video type of local video
  89. // FIXME container type is totally different thing from the video type
  90. localVideoThumbnail.setVideoType(VIDEO_CONTAINER_TYPE);
  91. // if we do not resize the thumbs here, if there is no video device
  92. // the local video thumb maybe one pixel
  93. let { localVideo } = this.resizeThumbnails(false, true);
  94. AudioLevels.createAudioLevelCanvas(
  95. "localVideoContainer",
  96. localVideo.thumbWidth,
  97. localVideo.thumbHeight);
  98. emitter.addListener(UIEvents.CONTACT_CLICKED, onContactClicked);
  99. this.lastNCount = config.channelLastN;
  100. },
  101. initLargeVideo () {
  102. largeVideo = new LargeVideoManager(eventEmitter);
  103. if(localFlipX) {
  104. largeVideo.onLocalFlipXChange(localFlipX);
  105. }
  106. largeVideo.updateContainerSize();
  107. AudioLevels.init();
  108. },
  109. setAudioLevel(id, lvl) {
  110. if (!largeVideo) {
  111. return;
  112. }
  113. AudioLevels.updateAudioLevel(
  114. id, lvl, largeVideo.id
  115. );
  116. },
  117. isInLastN (resource) {
  118. return this.lastNCount < 0 || // lastN is disabled
  119. // lastNEndpoints cache not built yet
  120. (this.lastNCount > 0 && !lastNEndpointsCache.length) ||
  121. (lastNEndpointsCache &&
  122. lastNEndpointsCache.indexOf(resource) !== -1);
  123. },
  124. changeLocalAudio (stream) {
  125. let localAudio = document.getElementById('localAudio');
  126. localAudio = stream.attach(localAudio);
  127. // Now when Temasys plugin is converting also <audio> elements to
  128. // plugin's <object>s, in current layout it will capture click events
  129. // before it reaches the local video object. We hide it here in order
  130. // to prevent that.
  131. //if (RTCBrowserType.isIExplorer()) {
  132. // The issue is not present on Safari. Also if we hide it in Safari
  133. // then the local audio track will have 'enabled' flag set to false
  134. // which will result in audio mute issues
  135. // $(localAudio).hide();
  136. localAudio.width = 1;
  137. localAudio.height = 1;
  138. //}
  139. },
  140. changeLocalVideo (stream) {
  141. let localId = APP.conference.getMyUserId();
  142. this.onVideoTypeChanged(localId, stream.videoType);
  143. if (!stream.isMuted()) {
  144. localVideoThumbnail.changeVideo(stream);
  145. }
  146. /* force update if we're currently being displayed */
  147. if (this.isCurrentlyOnLarge(localId)) {
  148. this.updateLargeVideo(localId, true);
  149. }
  150. },
  151. /**
  152. * Get's the localID of the conference and set it to the local video
  153. * (small one). This needs to be called as early as possible, when muc is
  154. * actually joined. Otherwise events can come with information like email
  155. * and setting them assume the id is already set.
  156. */
  157. mucJoined () {
  158. if (largeVideo && !largeVideo.id) {
  159. this.updateLargeVideo(APP.conference.getMyUserId(), true);
  160. }
  161. },
  162. /**
  163. * Adds or removes icons for not available camera and microphone.
  164. * @param resourceJid the jid of user
  165. * @param devices available devices
  166. */
  167. setDeviceAvailabilityIcons (id, devices) {
  168. if (APP.conference.isLocalId(id)) {
  169. localVideoThumbnail.setDeviceAvailabilityIcons(devices);
  170. return;
  171. }
  172. let video = remoteVideos[id];
  173. if (!video) {
  174. return;
  175. }
  176. video.setDeviceAvailabilityIcons(devices);
  177. },
  178. /**
  179. * Enables/disables device availability icons for the given participant id.
  180. * The default value is {true}.
  181. * @param id the identifier of the participant
  182. * @param enable {true} to enable device availability icons
  183. */
  184. enableDeviceAvailabilityIcons (id, enable) {
  185. let video;
  186. if (APP.conference.isLocalId(id)) {
  187. video = localVideoThumbnail;
  188. }
  189. else {
  190. video = remoteVideos[id];
  191. }
  192. if (video)
  193. video.enableDeviceAvailabilityIcons(enable);
  194. },
  195. /**
  196. * Shows/hides local video.
  197. * @param {boolean} true to make the local video visible, false - otherwise
  198. */
  199. setLocalVideoVisible(visible) {
  200. localVideoThumbnail.setVisible(visible);
  201. },
  202. /**
  203. * Checks if removed video is currently displayed and tries to display
  204. * another one instead.
  205. * Uses focusedID if any or dominantSpeakerID if any,
  206. * otherwise elects new video, in this order.
  207. */
  208. updateAfterThumbRemoved (id) {
  209. if (!this.isCurrentlyOnLarge(id)) {
  210. return;
  211. }
  212. let newId;
  213. if (pinnedId)
  214. newId = pinnedId;
  215. else if (currentDominantSpeaker)
  216. newId = currentDominantSpeaker;
  217. else // Otherwise select last visible video
  218. newId = this.electLastVisibleVideo();
  219. this.updateLargeVideo(newId);
  220. },
  221. electLastVisibleVideo () {
  222. // pick the last visible video in the row
  223. // if nobody else is left, this picks the local video
  224. let remoteThumbs = FilmStrip.getThumbs(true).remoteThumbs;
  225. let thumbs = remoteThumbs.filter('[id!="mixedstream"]');
  226. let lastVisible = thumbs.filter(':visible:last');
  227. if (lastVisible.length) {
  228. let id = getPeerContainerResourceId(lastVisible[0]);
  229. if (remoteVideos[id]) {
  230. console.info("electLastVisibleVideo: " + id);
  231. return id;
  232. }
  233. // The RemoteVideo was removed (but the DOM elements may still
  234. // exist).
  235. }
  236. console.info("Last visible video no longer exists");
  237. thumbs = FilmStrip.getThumbs().remoteThumbs;
  238. if (thumbs.length) {
  239. let id = getPeerContainerResourceId(thumbs[0]);
  240. if (remoteVideos[id]) {
  241. console.info("electLastVisibleVideo: " + id);
  242. return id;
  243. }
  244. // The RemoteVideo was removed (but the DOM elements may
  245. // still exist).
  246. }
  247. // Go with local video
  248. console.info("Fallback to local video...");
  249. let id = APP.conference.getMyUserId();
  250. console.info("electLastVisibleVideo: " + id);
  251. return id;
  252. },
  253. onRemoteStreamAdded (stream) {
  254. let id = stream.getParticipantId();
  255. let remoteVideo = remoteVideos[id];
  256. if (!remoteVideo)
  257. return;
  258. remoteVideo.addRemoteStreamElement(stream);
  259. // if track is muted make sure we reflect that
  260. if(stream.isMuted())
  261. {
  262. if(stream.getType() === "audio")
  263. this.onAudioMute(stream.getParticipantId(), true);
  264. else
  265. this.onVideoMute(stream.getParticipantId(), true);
  266. }
  267. },
  268. onRemoteStreamRemoved (stream) {
  269. let id = stream.getParticipantId();
  270. let remoteVideo = remoteVideos[id];
  271. if (remoteVideo) { // remote stream may be removed after participant left the conference
  272. remoteVideo.removeRemoteStreamElement(stream);
  273. }
  274. },
  275. /**
  276. * Return the type of the remote video.
  277. * @param id the id for the remote video
  278. * @returns {String} the video type video or screen.
  279. */
  280. getRemoteVideoType (id) {
  281. let smallVideo = VideoLayout.getSmallVideo(id);
  282. return smallVideo ? smallVideo.getVideoType() : null;
  283. },
  284. isPinned (id) {
  285. return (pinnedId) ? (id === pinnedId) : false;
  286. },
  287. getPinnedId () {
  288. return pinnedId;
  289. },
  290. /**
  291. * Handles the click on a video thumbnail.
  292. *
  293. * @param id the identifier of the video thumbnail
  294. */
  295. handleVideoThumbClicked (id) {
  296. if(pinnedId) {
  297. var oldSmallVideo = VideoLayout.getSmallVideo(pinnedId);
  298. if (oldSmallVideo && !interfaceConfig.filmStripOnly)
  299. oldSmallVideo.focus(false);
  300. }
  301. var smallVideo = VideoLayout.getSmallVideo(id);
  302. // Unpin if currently pinned.
  303. if (pinnedId === id)
  304. {
  305. pinnedId = null;
  306. // Enable the currently set dominant speaker.
  307. if (currentDominantSpeaker) {
  308. if(smallVideo && smallVideo.hasVideo()) {
  309. this.updateLargeVideo(currentDominantSpeaker);
  310. }
  311. }
  312. eventEmitter.emit(UIEvents.PINNED_ENDPOINT, smallVideo, false);
  313. return;
  314. }
  315. // Lock new video
  316. pinnedId = id;
  317. // Update focused/pinned interface.
  318. if (id) {
  319. if (smallVideo && !interfaceConfig.filmStripOnly)
  320. smallVideo.focus(true);
  321. eventEmitter.emit(UIEvents.PINNED_ENDPOINT, smallVideo, true);
  322. }
  323. this.updateLargeVideo(id);
  324. },
  325. /**
  326. * Creates or adds a participant container for the given id and smallVideo.
  327. *
  328. * @param {JitsiParticipant} user the participant to add
  329. * @param {SmallVideo} smallVideo optional small video instance to add as a
  330. * remote video, if undefined <tt>RemoteVideo</tt> will be created
  331. */
  332. addParticipantContainer (user, smallVideo) {
  333. let id = user.getId();
  334. let remoteVideo;
  335. if(smallVideo)
  336. remoteVideo = smallVideo;
  337. else
  338. remoteVideo = new RemoteVideo(user, VideoLayout, eventEmitter);
  339. this.addRemoteVideoContainer(id, remoteVideo);
  340. },
  341. /**
  342. * Adds remote video container for the given id and <tt>SmallVideo</tt>.
  343. *
  344. * @param {string} the id of the video to add
  345. * @param {SmallVideo} smallVideo the small video instance to add as a
  346. * remote video
  347. */
  348. addRemoteVideoContainer (id, remoteVideo) {
  349. remoteVideos[id] = remoteVideo;
  350. let videoType = VideoLayout.getRemoteVideoType(id);
  351. if (!videoType) {
  352. // make video type the default one (camera)
  353. // FIXME container type is not a video type
  354. videoType = VIDEO_CONTAINER_TYPE;
  355. }
  356. remoteVideo.setVideoType(videoType);
  357. // In case this is not currently in the last n we don't show it.
  358. if (localLastNCount && localLastNCount > 0 &&
  359. FilmStrip.getThumbs().remoteThumbs.length >= localLastNCount + 2) {
  360. remoteVideo.showPeerContainer('hide');
  361. } else {
  362. VideoLayout.resizeThumbnails(false, true);
  363. }
  364. // Initialize the view
  365. remoteVideo.updateView();
  366. },
  367. videoactive (videoelem, resourceJid) {
  368. console.info(resourceJid + " video is now active", videoelem);
  369. VideoLayout.resizeThumbnails(
  370. false, false, function() {$(videoelem).show();});
  371. // Update the large video to the last added video only if there's no
  372. // current dominant, focused speaker or update it to
  373. // the current dominant speaker.
  374. if ((!pinnedId &&
  375. !currentDominantSpeaker &&
  376. this.isLargeContainerTypeVisible(VIDEO_CONTAINER_TYPE)) ||
  377. pinnedId === resourceJid ||
  378. (!pinnedId && resourceJid &&
  379. currentDominantSpeaker === resourceJid) ||
  380. /* Playback started while we're on the stage - may need to update
  381. video source with the new stream */
  382. this.isCurrentlyOnLarge(resourceJid)) {
  383. this.updateLargeVideo(resourceJid, true);
  384. }
  385. },
  386. /**
  387. * Shows the presence status message for the given video.
  388. */
  389. setPresenceStatus (id, statusMsg) {
  390. let remoteVideo = remoteVideos[id];
  391. if (remoteVideo)
  392. remoteVideo.setPresenceStatus(statusMsg);
  393. },
  394. /**
  395. * Shows a visual indicator for the moderator of the conference.
  396. * On local or remote participants.
  397. */
  398. showModeratorIndicator () {
  399. let isModerator = APP.conference.isModerator;
  400. if (isModerator) {
  401. localVideoThumbnail.createModeratorIndicatorElement();
  402. } else {
  403. localVideoThumbnail.removeModeratorIndicatorElement();
  404. }
  405. APP.conference.listMembers().forEach(function (member) {
  406. let id = member.getId();
  407. let remoteVideo = remoteVideos[id];
  408. if (!remoteVideo)
  409. return;
  410. if (member.isModerator()) {
  411. remoteVideo.createModeratorIndicatorElement();
  412. }
  413. if (isModerator) {
  414. // We are moderator, but user is not - add menu
  415. if(!remoteVideo.hasRemoteVideoMenu) {
  416. remoteVideo.addRemoteVideoMenu();
  417. }
  418. }
  419. });
  420. },
  421. /*
  422. * Shows or hides the audio muted indicator over the local thumbnail video.
  423. * @param {boolean} isMuted
  424. */
  425. showLocalAudioIndicator (isMuted) {
  426. localVideoThumbnail.showAudioIndicator(isMuted);
  427. },
  428. /**
  429. * Shows/hides the indication about local connection being interrupted.
  430. *
  431. * @param {boolean} isInterrupted <tt>true</tt> if local connection is
  432. * currently in the interrupted state or <tt>false</tt> if the connection
  433. * is fine.
  434. */
  435. showLocalConnectionInterrupted (isInterrupted) {
  436. localVideoThumbnail.connectionIndicator
  437. .updateConnectionStatusIndicator(!isInterrupted);
  438. },
  439. /**
  440. * Resizes thumbnails.
  441. */
  442. resizeThumbnails ( animate = false,
  443. forceUpdate = false,
  444. onComplete = null) {
  445. let { localVideo, remoteVideo }
  446. = FilmStrip.calculateThumbnailSize();
  447. let {thumbWidth, thumbHeight} = remoteVideo;
  448. FilmStrip.resizeThumbnails(localVideo, remoteVideo,
  449. animate, forceUpdate)
  450. .then(function () {
  451. AudioLevels.updateCanvasSize(localVideo, remoteVideo);
  452. if (onComplete && typeof onComplete === "function")
  453. onComplete();
  454. });
  455. return { localVideo, remoteVideo };
  456. },
  457. /**
  458. * On audio muted event.
  459. */
  460. onAudioMute (id, isMuted) {
  461. if (APP.conference.isLocalId(id)) {
  462. localVideoThumbnail.showAudioIndicator(isMuted);
  463. } else {
  464. let remoteVideo = remoteVideos[id];
  465. if (!remoteVideo)
  466. return;
  467. remoteVideo.showAudioIndicator(isMuted);
  468. if (APP.conference.isModerator) {
  469. remoteVideo.updateRemoteVideoMenu(isMuted);
  470. }
  471. }
  472. },
  473. /**
  474. * On video muted event.
  475. */
  476. onVideoMute (id, value) {
  477. if (APP.conference.isLocalId(id)) {
  478. localVideoThumbnail.setMutedView(value);
  479. } else {
  480. let remoteVideo = remoteVideos[id];
  481. if (remoteVideo)
  482. remoteVideo.setMutedView(value);
  483. }
  484. if (this.isCurrentlyOnLarge(id)) {
  485. // large video will show avatar instead of muted stream
  486. this.updateLargeVideo(id, true);
  487. }
  488. },
  489. /**
  490. * Display name changed.
  491. */
  492. onDisplayNameChanged (id, displayName, status) {
  493. if (id === 'localVideoContainer' ||
  494. APP.conference.isLocalId(id)) {
  495. localVideoThumbnail.setDisplayName(displayName);
  496. } else {
  497. let remoteVideo = remoteVideos[id];
  498. if (remoteVideo)
  499. remoteVideo.setDisplayName(displayName, status);
  500. }
  501. },
  502. /**
  503. * Sets the "raised hand" status for a participant identified by 'id'.
  504. */
  505. setRaisedHandStatus(id, raisedHandStatus) {
  506. var video
  507. = APP.conference.isLocalId(id)
  508. ? localVideoThumbnail : remoteVideos[id];
  509. if (video) {
  510. video.showRaisedHandIndicator(raisedHandStatus);
  511. }
  512. },
  513. /**
  514. * On dominant speaker changed event.
  515. */
  516. onDominantSpeakerChanged (id) {
  517. if (id === currentDominantSpeaker) {
  518. return;
  519. }
  520. let oldSpeakerRemoteVideo = remoteVideos[currentDominantSpeaker];
  521. // We ignore local user events, but just unmark remote user as dominant
  522. // while we are talking
  523. if (APP.conference.isLocalId(id)) {
  524. if(oldSpeakerRemoteVideo)
  525. {
  526. oldSpeakerRemoteVideo.showDominantSpeakerIndicator(false);
  527. currentDominantSpeaker = null;
  528. }
  529. localVideoThumbnail.showDominantSpeakerIndicator(true);
  530. return;
  531. }
  532. let remoteVideo = remoteVideos[id];
  533. if (!remoteVideo) {
  534. return;
  535. }
  536. // Update the current dominant speaker.
  537. remoteVideo.showDominantSpeakerIndicator(true);
  538. localVideoThumbnail.showDominantSpeakerIndicator(false);
  539. // let's remove the indications from the remote video if any
  540. if (oldSpeakerRemoteVideo) {
  541. oldSpeakerRemoteVideo.showDominantSpeakerIndicator(false);
  542. }
  543. currentDominantSpeaker = id;
  544. // Local video will not have container found, but that's ok
  545. // since we don't want to switch to local video.
  546. // Update the large video if the video source is already available,
  547. // otherwise wait for the "videoactive.jingle" event.
  548. if (!pinnedId
  549. && remoteVideo.hasVideoStarted()
  550. && !this.getCurrentlyOnLargeContainer().stayOnStage()) {
  551. this.updateLargeVideo(id);
  552. }
  553. },
  554. /**
  555. * Shows/hides warning about remote user's connectivity issues.
  556. *
  557. * @param {string} id the ID of the remote participant(MUC nickname)
  558. * @param {boolean} isActive true if the connection is ok or false when
  559. * the user is having connectivity issues.
  560. */
  561. onParticipantConnectionStatusChanged (id, isActive) {
  562. // Show/hide warning on the large video
  563. if (this.isCurrentlyOnLarge(id)) {
  564. if (largeVideo) {
  565. // We have to trigger full large video update to transition from
  566. // avatar to video on connectivity restored.
  567. this.updateLargeVideo(id, true /* force update */);
  568. }
  569. }
  570. // Show/hide warning on the thumbnail
  571. let remoteVideo = remoteVideos[id];
  572. if (remoteVideo) {
  573. // Updating only connection status indicator is not enough, because
  574. // when we the connection is restored while the avatar was displayed
  575. // (due to 'muted while disconnected' condition) we may want to show
  576. // the video stream again and in order to do that the display mode
  577. // must be updated.
  578. //remoteVideo.updateConnectionStatusIndicator(isActive);
  579. remoteVideo.updateView();
  580. }
  581. },
  582. /**
  583. * On last N change event.
  584. *
  585. * @param lastNEndpoints the list of last N endpoints
  586. * @param endpointsEnteringLastN the list currently entering last N
  587. * endpoints
  588. */
  589. onLastNEndpointsChanged (lastNEndpoints, endpointsEnteringLastN) {
  590. if (this.lastNCount !== lastNEndpoints.length)
  591. this.lastNCount = lastNEndpoints.length;
  592. lastNEndpointsCache = lastNEndpoints;
  593. // Say A, B, C, D, E, and F are in a conference and LastN = 3.
  594. //
  595. // If LastN drops to, say, 2, because of adaptivity, then E should see
  596. // thumbnails for A, B and C. A and B are in E's server side LastN set,
  597. // so E sees them. C is only in E's local LastN set.
  598. //
  599. // If F starts talking and LastN = 3, then E should see thumbnails for
  600. // F, A, B. B gets "ejected" from E's server side LastN set, but it
  601. // enters E's local LastN ejecting C.
  602. // Increase the local LastN set size, if necessary.
  603. if (this.lastNCount > localLastNCount) {
  604. localLastNCount = this.lastNCount;
  605. }
  606. // Update the local LastN set preserving the order in which the
  607. // endpoints appeared in the LastN/local LastN set.
  608. var nextLocalLastNSet = lastNEndpoints.slice(0);
  609. for (var i = 0; i < localLastNSet.length; i++) {
  610. if (nextLocalLastNSet.length >= localLastNCount) {
  611. break;
  612. }
  613. var resourceJid = localLastNSet[i];
  614. if (nextLocalLastNSet.indexOf(resourceJid) === -1) {
  615. nextLocalLastNSet.push(resourceJid);
  616. }
  617. }
  618. localLastNSet = nextLocalLastNSet;
  619. var updateLargeVideo = false;
  620. // Handle LastN/local LastN changes.
  621. FilmStrip.getThumbs().remoteThumbs.each(( index, element ) => {
  622. var resourceJid = getPeerContainerResourceId(element);
  623. var smallVideo = remoteVideos[resourceJid];
  624. // We do not want to process any logic for our own(local) video
  625. // because the local participant is never in the lastN set.
  626. // The code of this function might detect that the local participant
  627. // has been dropped out of the lastN set and will update the large
  628. // video
  629. // Detected from avatar tests, where lastN event override
  630. // local video pinning
  631. if(APP.conference.isLocalId(resourceJid))
  632. return;
  633. var isReceived = true;
  634. if (resourceJid &&
  635. lastNEndpoints.indexOf(resourceJid) < 0 &&
  636. localLastNSet.indexOf(resourceJid) < 0) {
  637. console.log("Remove from last N", resourceJid);
  638. if (smallVideo)
  639. smallVideo.showPeerContainer('hide');
  640. else if (!APP.conference.isLocalId(resourceJid))
  641. console.error("No remote video for: " + resourceJid);
  642. isReceived = false;
  643. } else if (resourceJid &&
  644. //TOFIX: smallVideo may be undefined
  645. smallVideo.isVisible() &&
  646. lastNEndpoints.indexOf(resourceJid) < 0 &&
  647. localLastNSet.indexOf(resourceJid) >= 0) {
  648. // TOFIX: if we're here we already know that the smallVideo
  649. // exists. Look at the previous FIX above.
  650. if (smallVideo)
  651. smallVideo.showPeerContainer('avatar');
  652. else if (!APP.conference.isLocalId(resourceJid))
  653. console.error("No remote video for: " + resourceJid);
  654. isReceived = false;
  655. }
  656. if (!isReceived) {
  657. // resourceJid has dropped out of the server side lastN set, so
  658. // it is no longer being received. If resourceJid was being
  659. // displayed in the large video we have to switch to another
  660. // user.
  661. if (!updateLargeVideo &&
  662. this.isCurrentlyOnLarge(resourceJid)) {
  663. updateLargeVideo = true;
  664. }
  665. }
  666. });
  667. if (!endpointsEnteringLastN || endpointsEnteringLastN.length < 0)
  668. endpointsEnteringLastN = lastNEndpoints;
  669. if (endpointsEnteringLastN && endpointsEnteringLastN.length > 0) {
  670. endpointsEnteringLastN.forEach(function (resourceJid) {
  671. var remoteVideo = remoteVideos[resourceJid];
  672. if (remoteVideo)
  673. remoteVideo.showPeerContainer('show');
  674. if (!remoteVideo.isVisible()) {
  675. console.log("Add to last N", resourceJid);
  676. remoteVideo.addRemoteStreamElement(remoteVideo.videoStream);
  677. if (lastNPickupId == resourceJid) {
  678. // Clean up the lastN pickup id.
  679. lastNPickupId = null;
  680. VideoLayout.handleVideoThumbClicked(resourceJid);
  681. updateLargeVideo = false;
  682. }
  683. remoteVideo.waitForPlayback(
  684. remoteVideo.selectVideoElement()[0],
  685. remoteVideo.videoStream);
  686. }
  687. });
  688. }
  689. // The endpoint that was being shown in the large video has dropped out
  690. // of the lastN set and there was no lastN pickup jid. We need to update
  691. // the large video now.
  692. if (updateLargeVideo) {
  693. var resource;
  694. // Find out which endpoint to show in the large video.
  695. for (i = 0; i < lastNEndpoints.length; i++) {
  696. resource = lastNEndpoints[i];
  697. if (!resource || APP.conference.isLocalId(resource))
  698. continue;
  699. // videoSrcToSsrc needs to be update for this call to succeed.
  700. this.updateLargeVideo(resource);
  701. break;
  702. }
  703. }
  704. },
  705. /**
  706. * Updates local stats
  707. * @param percent
  708. * @param object
  709. */
  710. updateLocalConnectionStats (percent, object) {
  711. let resolutions = object.resolution;
  712. object.resolution = resolutions[APP.conference.getMyUserId()];
  713. localVideoThumbnail.updateStatsIndicator(percent, object);
  714. Object.keys(resolutions).forEach(function (id) {
  715. if (APP.conference.isLocalId(id)) {
  716. return;
  717. }
  718. let resolution = resolutions[id];
  719. let remoteVideo = remoteVideos[id];
  720. if (resolution && remoteVideo) {
  721. remoteVideo.updateResolution(resolution);
  722. }
  723. });
  724. },
  725. /**
  726. * Updates remote stats.
  727. * @param id the id associated with the stats
  728. * @param percent the connection quality percent
  729. * @param object the stats data
  730. */
  731. updateConnectionStats (id, percent, object) {
  732. let remoteVideo = remoteVideos[id];
  733. if (remoteVideo) {
  734. remoteVideo.updateStatsIndicator(percent, object);
  735. }
  736. },
  737. /**
  738. * Hides the connection indicator
  739. * @param id
  740. */
  741. hideConnectionIndicator (id) {
  742. let remoteVideo = remoteVideos[id];
  743. if (remoteVideo)
  744. remoteVideo.hideConnectionIndicator();
  745. },
  746. /**
  747. * Hides all the indicators
  748. */
  749. hideStats () {
  750. for (var video in remoteVideos) {
  751. let remoteVideo = remoteVideos[video];
  752. if (remoteVideo)
  753. remoteVideo.hideIndicator();
  754. }
  755. localVideoThumbnail.hideIndicator();
  756. },
  757. removeParticipantContainer (id) {
  758. // Unlock large video
  759. if (pinnedId === id) {
  760. console.info("Focused video owner has left the conference");
  761. pinnedId = null;
  762. }
  763. if (currentDominantSpeaker === id) {
  764. console.info("Dominant speaker has left the conference");
  765. currentDominantSpeaker = null;
  766. }
  767. var remoteVideo = remoteVideos[id];
  768. if (remoteVideo) {
  769. // Remove remote video
  770. console.info("Removing remote video: " + id);
  771. delete remoteVideos[id];
  772. remoteVideo.remove();
  773. } else {
  774. console.warn("No remote video for " + id);
  775. }
  776. VideoLayout.resizeThumbnails();
  777. },
  778. onVideoTypeChanged (id, newVideoType) {
  779. if (VideoLayout.getRemoteVideoType(id) === newVideoType) {
  780. return;
  781. }
  782. console.info("Peer video type changed: ", id, newVideoType);
  783. var smallVideo;
  784. if (APP.conference.isLocalId(id)) {
  785. if (!localVideoThumbnail) {
  786. console.warn("Local video not ready yet");
  787. return;
  788. }
  789. smallVideo = localVideoThumbnail;
  790. } else if (remoteVideos[id]) {
  791. smallVideo = remoteVideos[id];
  792. } else {
  793. return;
  794. }
  795. smallVideo.setVideoType(newVideoType);
  796. if (this.isCurrentlyOnLarge(id)) {
  797. this.updateLargeVideo(id, true);
  798. }
  799. },
  800. showMore (id) {
  801. if (id === 'local') {
  802. localVideoThumbnail.connectionIndicator.showMore();
  803. } else {
  804. let remoteVideo = remoteVideos[id];
  805. if (remoteVideo) {
  806. remoteVideo.connectionIndicator.showMore();
  807. } else {
  808. console.info("Error - no remote video for id: " + id);
  809. }
  810. }
  811. },
  812. /**
  813. * Resizes the video area.
  814. *
  815. * @param forceUpdate indicates that hidden thumbnails will be shown
  816. * @param completeFunction a function to be called when the video area is
  817. * resized.
  818. */
  819. resizeVideoArea (forceUpdate = false,
  820. animate = false,
  821. completeFunction = null) {
  822. if (largeVideo) {
  823. largeVideo.updateContainerSize();
  824. largeVideo.resize(animate);
  825. }
  826. // Calculate available width and height.
  827. let availableHeight = window.innerHeight;
  828. let availableWidth = UIUtil.getAvailableVideoWidth();
  829. if (availableWidth < 0 || availableHeight < 0) {
  830. return;
  831. }
  832. // Resize the thumbnails first.
  833. this.resizeThumbnails(false, forceUpdate);
  834. // Resize the video area element.
  835. $('#videospace').animate({
  836. right: window.innerWidth - availableWidth,
  837. width: availableWidth,
  838. height: availableHeight
  839. }, {
  840. queue: false,
  841. duration: animate ? 500 : 1,
  842. complete: completeFunction
  843. });
  844. },
  845. getSmallVideo (id) {
  846. if (APP.conference.isLocalId(id)) {
  847. return localVideoThumbnail;
  848. } else {
  849. return remoteVideos[id];
  850. }
  851. },
  852. changeUserAvatar (id, avatarUrl) {
  853. var smallVideo = VideoLayout.getSmallVideo(id);
  854. if (smallVideo) {
  855. smallVideo.avatarChanged(avatarUrl);
  856. } else {
  857. console.warn(
  858. "Missed avatar update - no small video yet for " + id
  859. );
  860. }
  861. if (this.isCurrentlyOnLarge(id)) {
  862. largeVideo.updateAvatar(avatarUrl);
  863. }
  864. },
  865. /**
  866. * Indicates that the video has been interrupted.
  867. */
  868. onVideoInterrupted () {
  869. if (largeVideo) {
  870. largeVideo.onVideoInterrupted();
  871. }
  872. },
  873. /**
  874. * Indicates that the video has been restored.
  875. */
  876. onVideoRestored () {
  877. if (largeVideo) {
  878. largeVideo.onVideoRestored();
  879. }
  880. },
  881. isLargeVideoVisible () {
  882. return this.isLargeContainerTypeVisible(VIDEO_CONTAINER_TYPE);
  883. },
  884. /**
  885. * @return {LargeContainer} the currently displayed container on large
  886. * video.
  887. */
  888. getCurrentlyOnLargeContainer () {
  889. return largeVideo.getContainer(largeVideo.state);
  890. },
  891. isCurrentlyOnLarge (id) {
  892. return largeVideo && largeVideo.id === id;
  893. },
  894. updateLargeVideo (id, forceUpdate) {
  895. if (!largeVideo) {
  896. return;
  897. }
  898. let isOnLarge = this.isCurrentlyOnLarge(id);
  899. let currentId = largeVideo.id;
  900. if (!isOnLarge || forceUpdate) {
  901. let videoType = this.getRemoteVideoType(id);
  902. // FIXME video type is not the same thing as container type
  903. if (id !== currentId && videoType === VIDEO_CONTAINER_TYPE) {
  904. eventEmitter.emit(UIEvents.SELECTED_ENDPOINT, id);
  905. }
  906. let smallVideo = this.getSmallVideo(id);
  907. let oldSmallVideo;
  908. if (currentId) {
  909. oldSmallVideo = this.getSmallVideo(currentId);
  910. }
  911. smallVideo.waitForResolutionChange();
  912. if (oldSmallVideo)
  913. oldSmallVideo.waitForResolutionChange();
  914. largeVideo.updateLargeVideo(
  915. id,
  916. smallVideo.videoStream,
  917. videoType
  918. ).then(function() {
  919. // update current small video and the old one
  920. smallVideo.updateView();
  921. oldSmallVideo && oldSmallVideo.updateView();
  922. }, function () {
  923. // use clicked other video during update, nothing to do.
  924. });
  925. } else if (currentId) {
  926. let currentSmallVideo = this.getSmallVideo(currentId);
  927. currentSmallVideo.updateView();
  928. }
  929. },
  930. addLargeVideoContainer (type, container) {
  931. largeVideo && largeVideo.addContainer(type, container);
  932. },
  933. removeLargeVideoContainer (type) {
  934. largeVideo && largeVideo.removeContainer(type);
  935. },
  936. /**
  937. * @returns Promise
  938. */
  939. showLargeVideoContainer (type, show) {
  940. if (!largeVideo) {
  941. return Promise.reject();
  942. }
  943. let isVisible = this.isLargeContainerTypeVisible(type);
  944. if (isVisible === show) {
  945. return Promise.resolve();
  946. }
  947. let currentId = largeVideo.id;
  948. if(currentId) {
  949. var oldSmallVideo = this.getSmallVideo(currentId);
  950. }
  951. let containerTypeToShow = type;
  952. // if we are hiding a container and there is focusedVideo
  953. // (pinned remote video) use its video type,
  954. // if not then use default type - large video
  955. if (!show) {
  956. if(pinnedId)
  957. containerTypeToShow = this.getRemoteVideoType(pinnedId);
  958. else
  959. containerTypeToShow = VIDEO_CONTAINER_TYPE;
  960. }
  961. return largeVideo.showContainer(containerTypeToShow)
  962. .then(() => {
  963. if(oldSmallVideo)
  964. oldSmallVideo && oldSmallVideo.updateView();
  965. });
  966. },
  967. isLargeContainerTypeVisible (type) {
  968. return largeVideo && largeVideo.state === type;
  969. },
  970. /**
  971. * Returns the id of the current video shown on large.
  972. * Currently used by tests (torture).
  973. */
  974. getLargeVideoID () {
  975. return largeVideo.id;
  976. },
  977. /**
  978. * Returns the the current video shown on large.
  979. * Currently used by tests (torture).
  980. */
  981. getLargeVideo () {
  982. return largeVideo;
  983. },
  984. /**
  985. * Updates the resolution label, indicating to the user that the large
  986. * video stream is currently HD.
  987. */
  988. updateResolutionLabel(isResolutionHD) {
  989. let videoResolutionLabel = $("#videoResolutionLabel");
  990. if (isResolutionHD && !videoResolutionLabel.is(":visible"))
  991. videoResolutionLabel.css({display: "block"});
  992. else if (!isResolutionHD && videoResolutionLabel.is(":visible"))
  993. videoResolutionLabel.css({display: "none"});
  994. },
  995. /**
  996. * Sets the flipX state of the local video.
  997. * @param {boolean} true for flipped otherwise false;
  998. */
  999. setLocalFlipX: function (val) {
  1000. this.localFlipX = val;
  1001. },
  1002. getEventEmitter: () => {return eventEmitter;}
  1003. };
  1004. export default VideoLayout;