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

VideoLayout.js 35KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115
  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 a participant container for the given id and smallVideo.
  325. *
  326. * @param id the id of the participant to add
  327. * @param {SmallVideo} smallVideo optional small video instance to add as a
  328. * remote video, if undefined RemoteVideo will be created
  329. */
  330. addParticipantContainer (id, smallVideo) {
  331. let remoteVideo;
  332. if(smallVideo)
  333. remoteVideo = smallVideo;
  334. else
  335. remoteVideo = new RemoteVideo(id, VideoLayout, eventEmitter);
  336. remoteVideos[id] = remoteVideo;
  337. let videoType = VideoLayout.getRemoteVideoType(id);
  338. if (!videoType) {
  339. // make video type the default one (camera)
  340. // FIXME container type is not a video type
  341. videoType = VIDEO_CONTAINER_TYPE;
  342. }
  343. remoteVideo.setVideoType(videoType);
  344. // In case this is not currently in the last n we don't show it.
  345. if (localLastNCount && localLastNCount > 0 &&
  346. FilmStrip.getThumbs().remoteThumbs.length >= localLastNCount + 2) {
  347. remoteVideo.showPeerContainer('hide');
  348. } else {
  349. VideoLayout.resizeThumbnails(false, true);
  350. }
  351. },
  352. videoactive (videoelem, resourceJid) {
  353. console.info(resourceJid + " video is now active", videoelem);
  354. VideoLayout.resizeThumbnails(
  355. false, false, function() {$(videoelem).show();});
  356. // Update the large video to the last added video only if there's no
  357. // current dominant, focused speaker or update it to
  358. // the current dominant speaker.
  359. if ((!pinnedId &&
  360. !currentDominantSpeaker &&
  361. this.isLargeContainerTypeVisible(VIDEO_CONTAINER_TYPE)) ||
  362. pinnedId === resourceJid ||
  363. (!pinnedId && resourceJid &&
  364. currentDominantSpeaker === resourceJid) ||
  365. /* Playback started while we're on the stage - may need to update
  366. video source with the new stream */
  367. this.isCurrentlyOnLarge(resourceJid)) {
  368. this.updateLargeVideo(resourceJid, true);
  369. }
  370. },
  371. /**
  372. * Shows the presence status message for the given video.
  373. */
  374. setPresenceStatus (id, statusMsg) {
  375. let remoteVideo = remoteVideos[id];
  376. if (remoteVideo)
  377. remoteVideo.setPresenceStatus(statusMsg);
  378. },
  379. /**
  380. * Shows a visual indicator for the moderator of the conference.
  381. * On local or remote participants.
  382. */
  383. showModeratorIndicator () {
  384. let isModerator = APP.conference.isModerator;
  385. if (isModerator) {
  386. localVideoThumbnail.createModeratorIndicatorElement();
  387. } else {
  388. localVideoThumbnail.removeModeratorIndicatorElement();
  389. }
  390. APP.conference.listMembers().forEach(function (member) {
  391. let id = member.getId();
  392. let remoteVideo = remoteVideos[id];
  393. if (!remoteVideo)
  394. return;
  395. if (member.isModerator()) {
  396. remoteVideo.createModeratorIndicatorElement();
  397. }
  398. if (isModerator) {
  399. // We are moderator, but user is not - add menu
  400. if(!remoteVideo.hasRemoteVideoMenu) {
  401. remoteVideo.addRemoteVideoMenu();
  402. }
  403. }
  404. });
  405. },
  406. /*
  407. * Shows or hides the audio muted indicator over the local thumbnail video.
  408. * @param {boolean} isMuted
  409. */
  410. showLocalAudioIndicator (isMuted) {
  411. localVideoThumbnail.showAudioIndicator(isMuted);
  412. },
  413. /**
  414. * Resizes thumbnails.
  415. */
  416. resizeThumbnails ( animate = false,
  417. forceUpdate = false,
  418. onComplete = null) {
  419. let { localVideo, remoteVideo }
  420. = FilmStrip.calculateThumbnailSize();
  421. let {thumbWidth, thumbHeight} = remoteVideo;
  422. FilmStrip.resizeThumbnails(localVideo, remoteVideo,
  423. animate, forceUpdate)
  424. .then(function () {
  425. AudioLevels.updateCanvasSize(localVideo, remoteVideo);
  426. if (onComplete && typeof onComplete === "function")
  427. onComplete();
  428. });
  429. return { localVideo, remoteVideo };
  430. },
  431. /**
  432. * On audio muted event.
  433. */
  434. onAudioMute (id, isMuted) {
  435. if (APP.conference.isLocalId(id)) {
  436. localVideoThumbnail.showAudioIndicator(isMuted);
  437. } else {
  438. let remoteVideo = remoteVideos[id];
  439. if (!remoteVideo)
  440. return;
  441. remoteVideo.showAudioIndicator(isMuted);
  442. if (APP.conference.isModerator) {
  443. remoteVideo.updateRemoteVideoMenu(isMuted);
  444. }
  445. }
  446. },
  447. /**
  448. * On video muted event.
  449. */
  450. onVideoMute (id, value) {
  451. if (APP.conference.isLocalId(id)) {
  452. localVideoThumbnail.setMutedView(value);
  453. } else {
  454. let remoteVideo = remoteVideos[id];
  455. if (remoteVideo)
  456. remoteVideo.setMutedView(value);
  457. }
  458. if (this.isCurrentlyOnLarge(id)) {
  459. // large video will show avatar instead of muted stream
  460. this.updateLargeVideo(id, true);
  461. }
  462. },
  463. /**
  464. * Display name changed.
  465. */
  466. onDisplayNameChanged (id, displayName, status) {
  467. if (id === 'localVideoContainer' ||
  468. APP.conference.isLocalId(id)) {
  469. localVideoThumbnail.setDisplayName(displayName);
  470. } else {
  471. let remoteVideo = remoteVideos[id];
  472. if (remoteVideo)
  473. remoteVideo.setDisplayName(displayName, status);
  474. }
  475. },
  476. /**
  477. * Sets the "raised hand" status for a participant identified by 'id'.
  478. */
  479. setRaisedHandStatus(id, raisedHandStatus) {
  480. var video
  481. = APP.conference.isLocalId(id)
  482. ? localVideoThumbnail : remoteVideos[id];
  483. if (video) {
  484. video.showRaisedHandIndicator(raisedHandStatus);
  485. }
  486. },
  487. /**
  488. * On dominant speaker changed event.
  489. */
  490. onDominantSpeakerChanged (id) {
  491. if (id === currentDominantSpeaker) {
  492. return;
  493. }
  494. let oldSpeakerRemoteVideo = remoteVideos[currentDominantSpeaker];
  495. // We ignore local user events, but just unmark remote user as dominant
  496. // while we are talking
  497. if (APP.conference.isLocalId(id)) {
  498. if(oldSpeakerRemoteVideo)
  499. {
  500. oldSpeakerRemoteVideo.showDominantSpeakerIndicator(false);
  501. currentDominantSpeaker = null;
  502. }
  503. localVideoThumbnail.showDominantSpeakerIndicator(true);
  504. return;
  505. }
  506. let remoteVideo = remoteVideos[id];
  507. if (!remoteVideo) {
  508. return;
  509. }
  510. // Update the current dominant speaker.
  511. remoteVideo.showDominantSpeakerIndicator(true);
  512. localVideoThumbnail.showDominantSpeakerIndicator(false);
  513. // let's remove the indications from the remote video if any
  514. if (oldSpeakerRemoteVideo) {
  515. oldSpeakerRemoteVideo.showDominantSpeakerIndicator(false);
  516. }
  517. currentDominantSpeaker = id;
  518. // Local video will not have container found, but that's ok
  519. // since we don't want to switch to local video.
  520. // Update the large video if the video source is already available,
  521. // otherwise wait for the "videoactive.jingle" event.
  522. if (!pinnedId
  523. && remoteVideo.hasVideoStarted()
  524. && !this.getCurrentlyOnLargeContainer().stayOnStage()) {
  525. this.updateLargeVideo(id);
  526. }
  527. },
  528. /**
  529. * On last N change event.
  530. *
  531. * @param lastNEndpoints the list of last N endpoints
  532. * @param endpointsEnteringLastN the list currently entering last N
  533. * endpoints
  534. */
  535. onLastNEndpointsChanged (lastNEndpoints, endpointsEnteringLastN) {
  536. if (this.lastNCount !== lastNEndpoints.length)
  537. this.lastNCount = lastNEndpoints.length;
  538. lastNEndpointsCache = lastNEndpoints;
  539. // Say A, B, C, D, E, and F are in a conference and LastN = 3.
  540. //
  541. // If LastN drops to, say, 2, because of adaptivity, then E should see
  542. // thumbnails for A, B and C. A and B are in E's server side LastN set,
  543. // so E sees them. C is only in E's local LastN set.
  544. //
  545. // If F starts talking and LastN = 3, then E should see thumbnails for
  546. // F, A, B. B gets "ejected" from E's server side LastN set, but it
  547. // enters E's local LastN ejecting C.
  548. // Increase the local LastN set size, if necessary.
  549. if (this.lastNCount > localLastNCount) {
  550. localLastNCount = this.lastNCount;
  551. }
  552. // Update the local LastN set preserving the order in which the
  553. // endpoints appeared in the LastN/local LastN set.
  554. var nextLocalLastNSet = lastNEndpoints.slice(0);
  555. for (var i = 0; i < localLastNSet.length; i++) {
  556. if (nextLocalLastNSet.length >= localLastNCount) {
  557. break;
  558. }
  559. var resourceJid = localLastNSet[i];
  560. if (nextLocalLastNSet.indexOf(resourceJid) === -1) {
  561. nextLocalLastNSet.push(resourceJid);
  562. }
  563. }
  564. localLastNSet = nextLocalLastNSet;
  565. var updateLargeVideo = false;
  566. // Handle LastN/local LastN changes.
  567. FilmStrip.getThumbs().remoteThumbs.each(( index, element ) => {
  568. var resourceJid = getPeerContainerResourceId(element);
  569. var smallVideo = remoteVideos[resourceJid];
  570. // We do not want to process any logic for our own(local) video
  571. // because the local participant is never in the lastN set.
  572. // The code of this function might detect that the local participant
  573. // has been dropped out of the lastN set and will update the large
  574. // video
  575. // Detected from avatar tests, where lastN event override
  576. // local video pinning
  577. if(APP.conference.isLocalId(resourceJid))
  578. return;
  579. var isReceived = true;
  580. if (resourceJid &&
  581. lastNEndpoints.indexOf(resourceJid) < 0 &&
  582. localLastNSet.indexOf(resourceJid) < 0) {
  583. console.log("Remove from last N", resourceJid);
  584. if (smallVideo)
  585. smallVideo.showPeerContainer('hide');
  586. else if (!APP.conference.isLocalId(resourceJid))
  587. console.error("No remote video for: " + resourceJid);
  588. isReceived = false;
  589. } else if (resourceJid &&
  590. //TOFIX: smallVideo may be undefined
  591. smallVideo.isVisible() &&
  592. lastNEndpoints.indexOf(resourceJid) < 0 &&
  593. localLastNSet.indexOf(resourceJid) >= 0) {
  594. // TOFIX: if we're here we already know that the smallVideo
  595. // exists. Look at the previous FIX above.
  596. if (smallVideo)
  597. smallVideo.showPeerContainer('avatar');
  598. else if (!APP.conference.isLocalId(resourceJid))
  599. console.error("No remote video for: " + resourceJid);
  600. isReceived = false;
  601. }
  602. if (!isReceived) {
  603. // resourceJid has dropped out of the server side lastN set, so
  604. // it is no longer being received. If resourceJid was being
  605. // displayed in the large video we have to switch to another
  606. // user.
  607. if (!updateLargeVideo &&
  608. this.isCurrentlyOnLarge(resourceJid)) {
  609. updateLargeVideo = true;
  610. }
  611. }
  612. });
  613. if (!endpointsEnteringLastN || endpointsEnteringLastN.length < 0)
  614. endpointsEnteringLastN = lastNEndpoints;
  615. if (endpointsEnteringLastN && endpointsEnteringLastN.length > 0) {
  616. endpointsEnteringLastN.forEach(function (resourceJid) {
  617. var remoteVideo = remoteVideos[resourceJid];
  618. if (remoteVideo)
  619. remoteVideo.showPeerContainer('show');
  620. if (!remoteVideo.isVisible()) {
  621. console.log("Add to last N", resourceJid);
  622. remoteVideo.addRemoteStreamElement(remoteVideo.videoStream);
  623. if (lastNPickupId == resourceJid) {
  624. // Clean up the lastN pickup id.
  625. lastNPickupId = null;
  626. VideoLayout.handleVideoThumbClicked(resourceJid);
  627. updateLargeVideo = false;
  628. }
  629. remoteVideo.waitForPlayback(
  630. remoteVideo.selectVideoElement()[0],
  631. remoteVideo.videoStream);
  632. }
  633. });
  634. }
  635. // The endpoint that was being shown in the large video has dropped out
  636. // of the lastN set and there was no lastN pickup jid. We need to update
  637. // the large video now.
  638. if (updateLargeVideo) {
  639. var resource;
  640. // Find out which endpoint to show in the large video.
  641. for (i = 0; i < lastNEndpoints.length; i++) {
  642. resource = lastNEndpoints[i];
  643. if (!resource || APP.conference.isLocalId(resource))
  644. continue;
  645. // videoSrcToSsrc needs to be update for this call to succeed.
  646. this.updateLargeVideo(resource);
  647. break;
  648. }
  649. }
  650. },
  651. /**
  652. * Updates local stats
  653. * @param percent
  654. * @param object
  655. */
  656. updateLocalConnectionStats (percent, object) {
  657. let resolutions = object.resolution;
  658. object.resolution = resolutions[APP.conference.getMyUserId()];
  659. localVideoThumbnail.updateStatsIndicator(percent, object);
  660. Object.keys(resolutions).forEach(function (id) {
  661. if (APP.conference.isLocalId(id)) {
  662. return;
  663. }
  664. let resolution = resolutions[id];
  665. let remoteVideo = remoteVideos[id];
  666. if (resolution && remoteVideo) {
  667. remoteVideo.updateResolution(resolution);
  668. }
  669. });
  670. },
  671. /**
  672. * Updates remote stats.
  673. * @param id the id associated with the stats
  674. * @param percent the connection quality percent
  675. * @param object the stats data
  676. */
  677. updateConnectionStats (id, percent, object) {
  678. let remoteVideo = remoteVideos[id];
  679. if (remoteVideo) {
  680. remoteVideo.updateStatsIndicator(percent, object);
  681. }
  682. },
  683. /**
  684. * Hides the connection indicator
  685. * @param id
  686. */
  687. hideConnectionIndicator (id) {
  688. let remoteVideo = remoteVideos[id];
  689. if (remoteVideo)
  690. remoteVideo.hideConnectionIndicator();
  691. },
  692. /**
  693. * Hides all the indicators
  694. */
  695. hideStats () {
  696. for (var video in remoteVideos) {
  697. let remoteVideo = remoteVideos[video];
  698. if (remoteVideo)
  699. remoteVideo.hideIndicator();
  700. }
  701. localVideoThumbnail.hideIndicator();
  702. },
  703. removeParticipantContainer (id) {
  704. // Unlock large video
  705. if (pinnedId === id) {
  706. console.info("Focused video owner has left the conference");
  707. pinnedId = null;
  708. }
  709. if (currentDominantSpeaker === id) {
  710. console.info("Dominant speaker has left the conference");
  711. currentDominantSpeaker = null;
  712. }
  713. var remoteVideo = remoteVideos[id];
  714. if (remoteVideo) {
  715. // Remove remote video
  716. console.info("Removing remote video: " + id);
  717. delete remoteVideos[id];
  718. remoteVideo.remove();
  719. } else {
  720. console.warn("No remote video for " + id);
  721. }
  722. VideoLayout.resizeThumbnails();
  723. },
  724. onVideoTypeChanged (id, newVideoType) {
  725. if (VideoLayout.getRemoteVideoType(id) === newVideoType) {
  726. return;
  727. }
  728. console.info("Peer video type changed: ", id, newVideoType);
  729. var smallVideo;
  730. if (APP.conference.isLocalId(id)) {
  731. if (!localVideoThumbnail) {
  732. console.warn("Local video not ready yet");
  733. return;
  734. }
  735. smallVideo = localVideoThumbnail;
  736. } else if (remoteVideos[id]) {
  737. smallVideo = remoteVideos[id];
  738. } else {
  739. return;
  740. }
  741. smallVideo.setVideoType(newVideoType);
  742. if (this.isCurrentlyOnLarge(id)) {
  743. this.updateLargeVideo(id, true);
  744. }
  745. },
  746. showMore (id) {
  747. if (id === 'local') {
  748. localVideoThumbnail.connectionIndicator.showMore();
  749. } else {
  750. let remoteVideo = remoteVideos[id];
  751. if (remoteVideo) {
  752. remoteVideo.connectionIndicator.showMore();
  753. } else {
  754. console.info("Error - no remote video for id: " + id);
  755. }
  756. }
  757. },
  758. /**
  759. * Resizes the video area.
  760. *
  761. * @param forceUpdate indicates that hidden thumbnails will be shown
  762. * @param completeFunction a function to be called when the video area is
  763. * resized.
  764. */
  765. resizeVideoArea (forceUpdate = false,
  766. animate = false,
  767. completeFunction = null) {
  768. if (largeVideo) {
  769. largeVideo.updateContainerSize();
  770. largeVideo.resize(animate);
  771. }
  772. // Calculate available width and height.
  773. let availableHeight = window.innerHeight;
  774. let availableWidth = UIUtil.getAvailableVideoWidth();
  775. if (availableWidth < 0 || availableHeight < 0) {
  776. return;
  777. }
  778. // Resize the thumbnails first.
  779. this.resizeThumbnails(false, forceUpdate);
  780. // Resize the video area element.
  781. $('#videospace').animate({
  782. right: window.innerWidth - availableWidth,
  783. width: availableWidth,
  784. height: availableHeight
  785. }, {
  786. queue: false,
  787. duration: animate ? 500 : 1,
  788. complete: completeFunction
  789. });
  790. },
  791. getSmallVideo (id) {
  792. if (APP.conference.isLocalId(id)) {
  793. return localVideoThumbnail;
  794. } else {
  795. return remoteVideos[id];
  796. }
  797. },
  798. changeUserAvatar (id, avatarUrl) {
  799. var smallVideo = VideoLayout.getSmallVideo(id);
  800. if (smallVideo) {
  801. smallVideo.avatarChanged(avatarUrl);
  802. } else {
  803. console.warn(
  804. "Missed avatar update - no small video yet for " + id
  805. );
  806. }
  807. if (this.isCurrentlyOnLarge(id)) {
  808. largeVideo.updateAvatar(avatarUrl);
  809. }
  810. },
  811. /**
  812. * Indicates that the video has been interrupted.
  813. */
  814. onVideoInterrupted () {
  815. if (largeVideo) {
  816. largeVideo.onVideoInterrupted();
  817. }
  818. },
  819. /**
  820. * Indicates that the video has been restored.
  821. */
  822. onVideoRestored () {
  823. if (largeVideo) {
  824. largeVideo.onVideoRestored();
  825. }
  826. },
  827. isLargeVideoVisible () {
  828. return this.isLargeContainerTypeVisible(VIDEO_CONTAINER_TYPE);
  829. },
  830. /**
  831. * @return {LargeContainer} the currently displayed container on large
  832. * video.
  833. */
  834. getCurrentlyOnLargeContainer () {
  835. return largeVideo.getContainer(largeVideo.state);
  836. },
  837. isCurrentlyOnLarge (id) {
  838. return largeVideo && largeVideo.id === id;
  839. },
  840. updateLargeVideo (id, forceUpdate) {
  841. if (!largeVideo) {
  842. return;
  843. }
  844. let isOnLarge = this.isCurrentlyOnLarge(id);
  845. let currentId = largeVideo.id;
  846. if (!isOnLarge || forceUpdate) {
  847. let videoType = this.getRemoteVideoType(id);
  848. // FIXME video type is not the same thing as container type
  849. if (id !== currentId && videoType === VIDEO_CONTAINER_TYPE) {
  850. eventEmitter.emit(UIEvents.SELECTED_ENDPOINT, id);
  851. }
  852. let smallVideo = this.getSmallVideo(id);
  853. let oldSmallVideo;
  854. if (currentId) {
  855. oldSmallVideo = this.getSmallVideo(currentId);
  856. }
  857. smallVideo.waitForResolutionChange();
  858. if (oldSmallVideo)
  859. oldSmallVideo.waitForResolutionChange();
  860. largeVideo.updateLargeVideo(
  861. id,
  862. smallVideo.videoStream,
  863. videoType
  864. ).then(function() {
  865. // update current small video and the old one
  866. smallVideo.updateView();
  867. oldSmallVideo && oldSmallVideo.updateView();
  868. }, function () {
  869. // use clicked other video during update, nothing to do.
  870. });
  871. } else if (currentId) {
  872. let currentSmallVideo = this.getSmallVideo(currentId);
  873. currentSmallVideo.updateView();
  874. }
  875. },
  876. addLargeVideoContainer (type, container) {
  877. largeVideo && largeVideo.addContainer(type, container);
  878. },
  879. removeLargeVideoContainer (type) {
  880. largeVideo && largeVideo.removeContainer(type);
  881. },
  882. /**
  883. * @returns Promise
  884. */
  885. showLargeVideoContainer (type, show) {
  886. if (!largeVideo) {
  887. return Promise.reject();
  888. }
  889. let isVisible = this.isLargeContainerTypeVisible(type);
  890. if (isVisible === show) {
  891. return Promise.resolve();
  892. }
  893. let currentId = largeVideo.id;
  894. if(currentId) {
  895. var oldSmallVideo = this.getSmallVideo(currentId);
  896. }
  897. let containerTypeToShow = type;
  898. // if we are hiding a container and there is focusedVideo
  899. // (pinned remote video) use its video type,
  900. // if not then use default type - large video
  901. if (!show) {
  902. if(pinnedId)
  903. containerTypeToShow = this.getRemoteVideoType(pinnedId);
  904. else
  905. containerTypeToShow = VIDEO_CONTAINER_TYPE;
  906. }
  907. return largeVideo.showContainer(containerTypeToShow)
  908. .then(() => {
  909. if(oldSmallVideo)
  910. oldSmallVideo && oldSmallVideo.updateView();
  911. });
  912. },
  913. isLargeContainerTypeVisible (type) {
  914. return largeVideo && largeVideo.state === type;
  915. },
  916. /**
  917. * Returns the id of the current video shown on large.
  918. * Currently used by tests (torture).
  919. */
  920. getLargeVideoID () {
  921. return largeVideo.id;
  922. },
  923. /**
  924. * Returns the the current video shown on large.
  925. * Currently used by tests (torture).
  926. */
  927. getLargeVideo () {
  928. return largeVideo;
  929. },
  930. /**
  931. * Updates the resolution label, indicating to the user that the large
  932. * video stream is currently HD.
  933. */
  934. updateResolutionLabel(isResolutionHD) {
  935. let videoResolutionLabel = $("#videoResolutionLabel");
  936. if (isResolutionHD && !videoResolutionLabel.is(":visible"))
  937. videoResolutionLabel.css({display: "block"});
  938. else if (!isResolutionHD && videoResolutionLabel.is(":visible"))
  939. videoResolutionLabel.css({display: "none"});
  940. },
  941. /**
  942. * Sets the flipX state of the local video.
  943. * @param {boolean} true for flipped otherwise false;
  944. */
  945. setLocalFlipX: function (val) {
  946. this.localFlipX = val;
  947. },
  948. getEventEmitter: () => {return eventEmitter;}
  949. };
  950. export default VideoLayout;