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

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