Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

VideoLayout.js 37KB

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