You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FollowMe.js 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. /* global APP */
  2. /*
  3. * Copyright @ 2015 Atlassian Pty Ltd
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. const logger = require('jitsi-meet-logger').getLogger(__filename);
  18. import {
  19. getPinnedParticipant,
  20. pinParticipant
  21. } from '../react/features/base/participants';
  22. import { setTileView } from '../react/features/video-layout';
  23. import UIEvents from '../service/UI/UIEvents';
  24. import VideoLayout from './UI/videolayout/VideoLayout';
  25. /**
  26. * The (name of the) command which transports the state (represented by
  27. * {State} for the local state at the time of this writing) of a {FollowMe}
  28. * (instance) between participants.
  29. */
  30. const _COMMAND = 'follow-me';
  31. /**
  32. * The timeout after which a follow-me command that has been received will be
  33. * ignored if not consumed.
  34. *
  35. * @type {number} in seconds
  36. * @private
  37. */
  38. const _FOLLOW_ME_RECEIVED_TIMEOUT = 30;
  39. /**
  40. * Represents the set of {FollowMe}-related states (properties and their
  41. * respective values) which are to be followed by a participant. {FollowMe}
  42. * will send {_COMMAND} whenever a property of {State} changes (if the local
  43. * participant is in her right to issue such a command, of course).
  44. */
  45. class State {
  46. /**
  47. * Initializes a new {State} instance.
  48. *
  49. * @param propertyChangeCallback {Function} which is to be called when a
  50. * property of the new instance has its value changed from an old value
  51. * into a (different) new value. The function is supplied with the name of
  52. * the property, the old value of the property before the change, and the
  53. * new value of the property after the change.
  54. */
  55. constructor(propertyChangeCallback) {
  56. this._propertyChangeCallback = propertyChangeCallback;
  57. }
  58. /**
  59. *
  60. */
  61. get filmstripVisible() {
  62. return this._filmstripVisible;
  63. }
  64. /**
  65. *
  66. */
  67. set filmstripVisible(b) {
  68. const oldValue = this._filmstripVisible;
  69. if (oldValue !== b) {
  70. this._filmstripVisible = b;
  71. this._firePropertyChange('filmstripVisible', oldValue, b);
  72. }
  73. }
  74. /**
  75. *
  76. */
  77. get nextOnStage() {
  78. return this._nextOnStage;
  79. }
  80. /**
  81. *
  82. */
  83. set nextOnStage(id) {
  84. const oldValue = this._nextOnStage;
  85. if (oldValue !== id) {
  86. this._nextOnStage = id;
  87. this._firePropertyChange('nextOnStage', oldValue, id);
  88. }
  89. }
  90. /**
  91. *
  92. */
  93. get sharedDocumentVisible() {
  94. return this._sharedDocumentVisible;
  95. }
  96. /**
  97. *
  98. */
  99. set sharedDocumentVisible(b) {
  100. const oldValue = this._sharedDocumentVisible;
  101. if (oldValue !== b) {
  102. this._sharedDocumentVisible = b;
  103. this._firePropertyChange('sharedDocumentVisible', oldValue, b);
  104. }
  105. }
  106. /**
  107. * A getter for this object instance to know the state of tile view.
  108. *
  109. * @returns {boolean} True if tile view is enabled.
  110. */
  111. get tileViewEnabled() {
  112. return this._tileViewEnabled;
  113. }
  114. /**
  115. * A setter for {@link tileViewEnabled}. Fires a property change event for
  116. * other participants to follow.
  117. *
  118. * @param {boolean} b - Whether or not tile view is enabled.
  119. * @returns {void}
  120. */
  121. set tileViewEnabled(b) {
  122. const oldValue = this._tileViewEnabled;
  123. if (oldValue !== b) {
  124. this._tileViewEnabled = b;
  125. this._firePropertyChange('tileViewEnabled', oldValue, b);
  126. }
  127. }
  128. /**
  129. * Invokes {_propertyChangeCallback} to notify it that {property} had its
  130. * value changed from {oldValue} to {newValue}.
  131. *
  132. * @param property the name of the property which had its value changed
  133. * from {oldValue} to {newValue}
  134. * @param oldValue the value of {property} before the change
  135. * @param newValue the value of {property} after the change
  136. */
  137. _firePropertyChange(property, oldValue, newValue) {
  138. const propertyChangeCallback = this._propertyChangeCallback;
  139. if (propertyChangeCallback) {
  140. propertyChangeCallback(property, oldValue, newValue);
  141. }
  142. }
  143. }
  144. /**
  145. * Represents the "Follow Me" feature which enables a moderator to
  146. * (partially) control the user experience/interface (e.g. filmstrip
  147. * visibility) of (other) non-moderator particiapnts.
  148. *
  149. * @author Lyubomir Marinov
  150. */
  151. class FollowMe {
  152. /**
  153. * Initializes a new {FollowMe} instance.
  154. *
  155. * @param conference the {conference} which is to transport
  156. * {FollowMe}-related information between participants
  157. * @param UI the {UI} which is the source (model/state) to be sent to
  158. * remote participants if the local participant is the moderator or the
  159. * destination (model/state) to receive from the remote moderator if the
  160. * local participant is not the moderator
  161. */
  162. constructor(conference, UI) {
  163. this._conference = conference;
  164. this._UI = UI;
  165. this.nextOnStageTimer = 0;
  166. // The states of the local participant which are to be followed (by the
  167. // remote participants when the local participant is in her right to
  168. // issue such commands).
  169. this._local = new State(this._localPropertyChange.bind(this));
  170. // Listen to "Follow Me" commands. I'm not sure whether a moderator can
  171. // (in lib-jitsi-meet and/or Meet) become a non-moderator. If that's
  172. // possible, then it may be easiest to always listen to commands. The
  173. // listener will validate received commands before acting on them.
  174. conference.commands.addCommandListener(
  175. _COMMAND,
  176. this._onFollowMeCommand.bind(this));
  177. }
  178. /**
  179. * Sets the current state of all follow-me properties, which will fire a
  180. * localPropertyChangeEvent and trigger a send of the follow-me command.
  181. * @private
  182. */
  183. _setFollowMeInitialState() {
  184. this._filmstripToggled.bind(this, this._UI.isFilmstripVisible());
  185. const pinnedId = VideoLayout.getPinnedId();
  186. this._nextOnStage(pinnedId, Boolean(pinnedId));
  187. // check whether shared document is enabled/initialized
  188. if (this._UI.getSharedDocumentManager()) {
  189. this._sharedDocumentToggled
  190. .bind(this, this._UI.getSharedDocumentManager().isVisible());
  191. }
  192. this._tileViewToggled.bind(
  193. this,
  194. APP.store.getState()['features/video-layout'].tileViewEnabled);
  195. }
  196. /**
  197. * Adds listeners for the UI states of the local participant which are
  198. * to be followed (by the remote participants). A non-moderator (very
  199. * likely) can become a moderator so it may be easiest to always track
  200. * the states of interest.
  201. * @private
  202. */
  203. _addFollowMeListeners() {
  204. this.filmstripEventHandler = this._filmstripToggled.bind(this);
  205. this._UI.addListener(UIEvents.TOGGLED_FILMSTRIP,
  206. this.filmstripEventHandler);
  207. const self = this;
  208. this.pinnedEndpointEventHandler = function(videoId, isPinned) {
  209. self._nextOnStage(videoId, isPinned);
  210. };
  211. this._UI.addListener(UIEvents.PINNED_ENDPOINT,
  212. this.pinnedEndpointEventHandler);
  213. this.sharedDocEventHandler = this._sharedDocumentToggled.bind(this);
  214. this._UI.addListener(UIEvents.TOGGLED_SHARED_DOCUMENT,
  215. this.sharedDocEventHandler);
  216. this.tileViewEventHandler = this._tileViewToggled.bind(this);
  217. this._UI.addListener(UIEvents.TOGGLED_TILE_VIEW,
  218. this.tileViewEventHandler);
  219. }
  220. /**
  221. * Removes all follow me listeners.
  222. * @private
  223. */
  224. _removeFollowMeListeners() {
  225. this._UI.removeListener(UIEvents.TOGGLED_FILMSTRIP,
  226. this.filmstripEventHandler);
  227. this._UI.removeListener(UIEvents.TOGGLED_SHARED_DOCUMENT,
  228. this.sharedDocEventHandler);
  229. this._UI.removeListener(UIEvents.PINNED_ENDPOINT,
  230. this.pinnedEndpointEventHandler);
  231. this._UI.removeListener(UIEvents.TOGGLED_TILE_VIEW,
  232. this.tileViewEventHandler);
  233. }
  234. /**
  235. * Enables or disabled the follow me functionality
  236. *
  237. * @param enable {true} to enable the follow me functionality, {false} -
  238. * to disable it
  239. */
  240. enableFollowMe(enable) {
  241. if (enable) {
  242. this._setFollowMeInitialState();
  243. this._addFollowMeListeners();
  244. } else {
  245. this._removeFollowMeListeners();
  246. }
  247. }
  248. /**
  249. * Notifies this instance that the (visibility of the) filmstrip was
  250. * toggled (in the user interface of the local participant).
  251. *
  252. * @param filmstripVisible {Boolean} {true} if the filmstrip was shown (as a
  253. * result of the toggle) or {false} if the filmstrip was hidden
  254. */
  255. _filmstripToggled(filmstripVisible) {
  256. this._local.filmstripVisible = filmstripVisible;
  257. }
  258. /**
  259. * Notifies this instance that the (visibility of the) shared document was
  260. * toggled (in the user interface of the local participant).
  261. *
  262. * @param sharedDocumentVisible {Boolean} {true} if the shared document was
  263. * shown (as a result of the toggle) or {false} if it was hidden
  264. */
  265. _sharedDocumentToggled(sharedDocumentVisible) {
  266. this._local.sharedDocumentVisible = sharedDocumentVisible;
  267. }
  268. /**
  269. * Notifies this instance that the tile view mode has been enabled or
  270. * disabled.
  271. *
  272. * @param {boolean} enabled - True if tile view has been enabled, false
  273. * if has been disabled.
  274. * @returns {void}
  275. */
  276. _tileViewToggled(enabled) {
  277. this._local.tileViewEnabled = enabled;
  278. }
  279. /**
  280. * Changes the nextOnStage property value.
  281. *
  282. * @param smallVideo the {SmallVideo} that was pinned or unpinned
  283. * @param isPinned indicates if the given {SmallVideo} was pinned or
  284. * unpinned
  285. * @private
  286. */
  287. _nextOnStage(videoId, isPinned) {
  288. if (!this._conference.isModerator) {
  289. return;
  290. }
  291. let nextOnStage = null;
  292. if (isPinned) {
  293. nextOnStage = videoId;
  294. }
  295. this._local.nextOnStage = nextOnStage;
  296. }
  297. /**
  298. * Sends the follow-me command, when a local property change occurs.
  299. *
  300. * @private
  301. */
  302. _localPropertyChange() { // eslint-disable-next-line no-unused-vars
  303. // Only a moderator is allowed to send commands.
  304. const conference = this._conference;
  305. if (!conference.isModerator) {
  306. return;
  307. }
  308. const commands = conference.commands;
  309. // XXX The "Follow Me" command represents a snapshot of all states
  310. // which are to be followed so don't forget to removeCommand before
  311. // sendCommand!
  312. commands.removeCommand(_COMMAND);
  313. const local = this._local;
  314. commands.sendCommandOnce(
  315. _COMMAND,
  316. {
  317. attributes: {
  318. filmstripVisible: local.filmstripVisible,
  319. nextOnStage: local.nextOnStage,
  320. sharedDocumentVisible: local.sharedDocumentVisible,
  321. tileViewEnabled: local.tileViewEnabled
  322. }
  323. });
  324. }
  325. /**
  326. * Notifies this instance about a &qout;Follow Me&qout; command (delivered
  327. * by the Command(s) API of {this._conference}).
  328. *
  329. * @param attributes the attributes {Object} carried by the command
  330. * @param id the identifier of the participant who issued the command. A
  331. * notable idiosyncrasy of the Command(s) API to be mindful of here is that
  332. * the command may be issued by the local participant.
  333. */
  334. _onFollowMeCommand({ attributes }, id) {
  335. // We require to know who issued the command because (1) only a
  336. // moderator is allowed to send commands and (2) a command MUST be
  337. // issued by a defined commander.
  338. if (typeof id === 'undefined') {
  339. return;
  340. }
  341. // The Command(s) API will send us our own commands and we don't want
  342. // to act upon them.
  343. if (this._conference.isLocalId(id)) {
  344. return;
  345. }
  346. if (!this._conference.isParticipantModerator(id)) {
  347. logger.warn('Received follow-me command not from moderator');
  348. return;
  349. }
  350. // Applies the received/remote command to the user experience/interface
  351. // of the local participant.
  352. this._onFilmstripVisible(attributes.filmstripVisible);
  353. this._onNextOnStage(attributes.nextOnStage);
  354. this._onSharedDocumentVisible(attributes.sharedDocumentVisible);
  355. this._onTileViewEnabled(attributes.tileViewEnabled);
  356. }
  357. /**
  358. * Process a filmstrip open / close event received from FOLLOW-ME
  359. * command.
  360. * @param filmstripVisible indicates if the filmstrip has been shown or
  361. * hidden
  362. * @private
  363. */
  364. _onFilmstripVisible(filmstripVisible) {
  365. if (typeof filmstripVisible !== 'undefined') {
  366. // XXX The Command(s) API doesn't preserve the types (of
  367. // attributes, at least) at the time of this writing so take into
  368. // account that what originated as a Boolean may be a String on
  369. // receipt.
  370. // eslint-disable-next-line eqeqeq, no-param-reassign
  371. filmstripVisible = filmstripVisible == 'true';
  372. // FIXME The UI (module) very likely doesn't (want to) expose its
  373. // eventEmitter as a public field. I'm not sure at the time of this
  374. // writing whether calling UI.toggleFilmstrip() is acceptable (from
  375. // a design standpoint) either.
  376. if (filmstripVisible !== this._UI.isFilmstripVisible()) {
  377. this._UI.eventEmitter.emit(UIEvents.TOGGLE_FILMSTRIP);
  378. }
  379. }
  380. }
  381. /**
  382. * Process the id received from a FOLLOW-ME command.
  383. * @param id the identifier of the next participant to show on stage or
  384. * undefined if we're clearing the stage (we're unpining all pined and we
  385. * rely on dominant speaker events)
  386. * @private
  387. */
  388. _onNextOnStage(id) {
  389. let clickId = null;
  390. let pin;
  391. // if there is an id which is not pinned we schedule it for pin only the
  392. // first time
  393. if (typeof id !== 'undefined' && !VideoLayout.isPinned(id)) {
  394. clickId = id;
  395. pin = true;
  396. } else if (typeof id === 'undefined' && VideoLayout.getPinnedId()) {
  397. // if there is no id, but we have a pinned one, let's unpin
  398. clickId = VideoLayout.getPinnedId();
  399. pin = false;
  400. }
  401. if (clickId) {
  402. this._pinVideoThumbnailById(clickId, pin);
  403. }
  404. }
  405. /**
  406. * Process a shared document open / close event received from FOLLOW-ME
  407. * command.
  408. * @param sharedDocumentVisible indicates if the shared document has been
  409. * opened or closed
  410. * @private
  411. */
  412. _onSharedDocumentVisible(sharedDocumentVisible) {
  413. if (typeof sharedDocumentVisible !== 'undefined') {
  414. // XXX The Command(s) API doesn't preserve the types (of
  415. // attributes, at least) at the time of this writing so take into
  416. // account that what originated as a Boolean may be a String on
  417. // receipt.
  418. // eslint-disable-next-line eqeqeq, no-param-reassign
  419. sharedDocumentVisible = sharedDocumentVisible == 'true';
  420. if (sharedDocumentVisible
  421. !== this._UI.getSharedDocumentManager().isVisible()) {
  422. this._UI.getSharedDocumentManager().toggleEtherpad();
  423. }
  424. }
  425. }
  426. /**
  427. * Process a tile view enabled / disabled event received from FOLLOW-ME.
  428. *
  429. * @param {boolean} enabled - Whether or not tile view should be shown.
  430. * @private
  431. * @returns {void}
  432. */
  433. _onTileViewEnabled(enabled) {
  434. if (typeof enabled === 'undefined') {
  435. return;
  436. }
  437. APP.store.dispatch(setTileView(enabled === 'true'));
  438. }
  439. /**
  440. * Pins / unpins the video thumbnail given by clickId.
  441. *
  442. * @param clickId the identifier of the video thumbnail to pin or unpin
  443. * @param pin {true} to pin, {false} to unpin
  444. * @private
  445. */
  446. _pinVideoThumbnailById(clickId, pin) {
  447. const self = this;
  448. const smallVideo = VideoLayout.getSmallVideo(clickId);
  449. // If the SmallVideo for the given clickId exists we proceed with the
  450. // pin/unpin.
  451. if (smallVideo) {
  452. this.nextOnStageTimer = 0;
  453. clearTimeout(this.nextOnStageTimout);
  454. if (pin) {
  455. APP.store.dispatch(pinParticipant(clickId));
  456. } else {
  457. const { id } = getPinnedParticipant(APP.store.getState()) || {};
  458. if (id === clickId) {
  459. APP.store.dispatch(pinParticipant(null));
  460. }
  461. }
  462. } else {
  463. // If there's no SmallVideo object for the given id, lets wait and
  464. // see if it's going to be created in the next 30sec.
  465. this.nextOnStageTimout = setTimeout(function() {
  466. if (self.nextOnStageTimer > _FOLLOW_ME_RECEIVED_TIMEOUT) {
  467. self.nextOnStageTimer = 0;
  468. return;
  469. }
  470. // eslint-disable-next-line no-invalid-this
  471. this.nextOnStageTimer++;
  472. self._pinVideoThumbnailById(clickId, pin);
  473. }, 1000);
  474. }
  475. }
  476. }
  477. export default FollowMe;