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 16KB

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