Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

VideoLayout.js 37KB

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