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

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