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

VideoLayout.js 37KB

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