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.

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