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

VideoLayout.js 37KB

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