您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

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. 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. const pinnedId = VideoLayout.getPinnedId();
  133. this._nextOnStage(pinnedId, Boolean(pinnedId));
  134. // check whether shared document is enabled/initialized
  135. if(this._UI.getSharedDocumentManager())
  136. this._sharedDocumentToggled
  137. .bind(this, this._UI.getSharedDocumentManager().isVisible());
  138. }
  139. /**
  140. * Adds listeners for the UI states of the local participant which are
  141. * to be followed (by the remote participants). A non-moderator (very
  142. * likely) can become a moderator so it may be easiest to always track
  143. * the states of interest.
  144. * @private
  145. */
  146. _addFollowMeListeners () {
  147. this.filmstripEventHandler = this._filmstripToggled.bind(this);
  148. this._UI.addListener(UIEvents.TOGGLED_FILMSTRIP,
  149. this.filmstripEventHandler);
  150. var self = this;
  151. this.pinnedEndpointEventHandler = function (videoId, isPinned) {
  152. self._nextOnStage(videoId, isPinned);
  153. };
  154. this._UI.addListener(UIEvents.PINNED_ENDPOINT,
  155. this.pinnedEndpointEventHandler);
  156. this.sharedDocEventHandler = this._sharedDocumentToggled.bind(this);
  157. this._UI.addListener( UIEvents.TOGGLED_SHARED_DOCUMENT,
  158. this.sharedDocEventHandler);
  159. }
  160. /**
  161. * Removes all follow me listeners.
  162. * @private
  163. */
  164. _removeFollowMeListeners () {
  165. this._UI.removeListener(UIEvents.TOGGLED_FILMSTRIP,
  166. this.filmstripEventHandler);
  167. this._UI.removeListener(UIEvents.TOGGLED_SHARED_DOCUMENT,
  168. this.sharedDocEventHandler);
  169. this._UI.removeListener(UIEvents.PINNED_ENDPOINT,
  170. this.pinnedEndpointEventHandler);
  171. }
  172. /**
  173. * Enables or disabled the follow me functionality
  174. *
  175. * @param enable {true} to enable the follow me functionality, {false} -
  176. * to disable it
  177. */
  178. enableFollowMe (enable) {
  179. if (enable) {
  180. this._setFollowMeInitialState();
  181. this._addFollowMeListeners();
  182. }
  183. else
  184. this._removeFollowMeListeners();
  185. }
  186. /**
  187. * Notifies this instance that the (visibility of the) filmstrip was
  188. * toggled (in the user interface of the local participant).
  189. *
  190. * @param filmstripVisible {Boolean} {true} if the filmstrip was shown (as a
  191. * result of the toggle) or {false} if the filmstrip was hidden
  192. */
  193. _filmstripToggled (filmstripVisible) {
  194. this._local.filmstripVisible = filmstripVisible;
  195. }
  196. /**
  197. * Notifies this instance that the (visibility of the) shared document was
  198. * toggled (in the user interface of the local participant).
  199. *
  200. * @param sharedDocumentVisible {Boolean} {true} if the shared document was
  201. * shown (as a result of the toggle) or {false} if it was hidden
  202. */
  203. _sharedDocumentToggled (sharedDocumentVisible) {
  204. this._local.sharedDocumentVisible = sharedDocumentVisible;
  205. }
  206. /**
  207. * Changes the nextOnStage property value.
  208. *
  209. * @param smallVideo the {SmallVideo} that was pinned or unpinned
  210. * @param isPinned indicates if the given {SmallVideo} was pinned or
  211. * unpinned
  212. * @private
  213. */
  214. _nextOnStage (videoId, isPinned) {
  215. if (!this._conference.isModerator)
  216. return;
  217. var nextOnStage = null;
  218. if(isPinned)
  219. nextOnStage = videoId;
  220. this._local.nextOnStage = nextOnStage;
  221. }
  222. /**
  223. * Sends the follow-me command, when a local property change occurs.
  224. *
  225. * @param property the property name
  226. * @param oldValue the old value
  227. * @param newValue the new value
  228. * @private
  229. */
  230. // eslint-disable-next-line no-unused-vars
  231. _localPropertyChange (property, oldValue, newValue) {
  232. // Only a moderator is allowed to send commands.
  233. const conference = this._conference;
  234. if (!conference.isModerator)
  235. return;
  236. const commands = conference.commands;
  237. // XXX The "Follow Me" command represents a snapshot of all states
  238. // which are to be followed so don't forget to removeCommand before
  239. // sendCommand!
  240. commands.removeCommand(_COMMAND);
  241. const local = this._local;
  242. commands.sendCommandOnce(
  243. _COMMAND,
  244. {
  245. attributes: {
  246. filmstripVisible: local.filmstripVisible,
  247. nextOnStage: local.nextOnStage,
  248. sharedDocumentVisible: local.sharedDocumentVisible
  249. }
  250. });
  251. }
  252. /**
  253. * Notifies this instance about a &qout;Follow Me&qout; command (delivered
  254. * by the Command(s) API of {this._conference}).
  255. *
  256. * @param attributes the attributes {Object} carried by the command
  257. * @param id the identifier of the participant who issued the command. A
  258. * notable idiosyncrasy of the Command(s) API to be mindful of here is that
  259. * the command may be issued by the local participant.
  260. */
  261. _onFollowMeCommand ({ attributes }, id) {
  262. // We require to know who issued the command because (1) only a
  263. // moderator is allowed to send commands and (2) a command MUST be
  264. // issued by a defined commander.
  265. if (typeof id === 'undefined')
  266. return;
  267. // The Command(s) API will send us our own commands and we don't want
  268. // to act upon them.
  269. if (this._conference.isLocalId(id))
  270. return;
  271. if (!this._conference.isParticipantModerator(id))
  272. {
  273. logger.warn('Received follow-me command ' +
  274. 'not from moderator');
  275. return;
  276. }
  277. // Applies the received/remote command to the user experience/interface
  278. // of the local participant.
  279. this._onFilmstripVisible(attributes.filmstripVisible);
  280. this._onNextOnStage(attributes.nextOnStage);
  281. this._onSharedDocumentVisible(attributes.sharedDocumentVisible);
  282. }
  283. /**
  284. * Process a filmstrip open / close event received from FOLLOW-ME
  285. * command.
  286. * @param filmstripVisible indicates if the filmstrip has been shown or
  287. * hidden
  288. * @private
  289. */
  290. _onFilmstripVisible(filmstripVisible) {
  291. if (typeof filmstripVisible !== 'undefined') {
  292. // XXX The Command(s) API doesn't preserve the types (of
  293. // attributes, at least) at the time of this writing so take into
  294. // account that what originated as a Boolean may be a String on
  295. // receipt.
  296. filmstripVisible = (filmstripVisible == 'true');
  297. // FIXME The UI (module) very likely doesn't (want to) expose its
  298. // eventEmitter as a public field. I'm not sure at the time of this
  299. // writing whether calling UI.toggleFilmstrip() is acceptable (from
  300. // a design standpoint) either.
  301. if (filmstripVisible !== this._UI.isFilmstripVisible())
  302. this._UI.eventEmitter.emit(UIEvents.TOGGLE_FILMSTRIP);
  303. }
  304. }
  305. /**
  306. * Process the id received from a FOLLOW-ME command.
  307. * @param id the identifier of the next participant to show on stage or
  308. * undefined if we're clearing the stage (we're unpining all pined and we
  309. * rely on dominant speaker events)
  310. * @private
  311. */
  312. _onNextOnStage(id) {
  313. var clickId = null;
  314. var pin;
  315. // if there is an id which is not pinned we schedule it for pin only the
  316. // first time
  317. if(typeof id !== 'undefined' && !VideoLayout.isPinned(id)) {
  318. clickId = id;
  319. pin = true;
  320. }
  321. // if there is no id, but we have a pinned one, let's unpin
  322. else if (typeof id == 'undefined' && VideoLayout.getPinnedId()) {
  323. clickId = VideoLayout.getPinnedId();
  324. pin = false;
  325. }
  326. if (clickId)
  327. this._pinVideoThumbnailById(clickId, pin);
  328. }
  329. /**
  330. * Process a shared document open / close event received from FOLLOW-ME
  331. * command.
  332. * @param sharedDocumentVisible indicates if the shared document has been
  333. * opened or closed
  334. * @private
  335. */
  336. _onSharedDocumentVisible(sharedDocumentVisible) {
  337. if (typeof sharedDocumentVisible !== 'undefined') {
  338. // XXX The Command(s) API doesn't preserve the types (of
  339. // attributes, at least) at the time of this writing so take into
  340. // account that what originated as a Boolean may be a String on
  341. // receipt.
  342. sharedDocumentVisible = (sharedDocumentVisible == 'true');
  343. if (sharedDocumentVisible
  344. !== this._UI.getSharedDocumentManager().isVisible())
  345. this._UI.getSharedDocumentManager().toggleEtherpad();
  346. }
  347. }
  348. /**
  349. * Pins / unpins the video thumbnail given by clickId.
  350. *
  351. * @param clickId the identifier of the video thumbnail to pin or unpin
  352. * @param pin {true} to pin, {false} to unpin
  353. * @private
  354. */
  355. _pinVideoThumbnailById(clickId, pin) {
  356. var self = this;
  357. var smallVideo = VideoLayout.getSmallVideo(clickId);
  358. // If the SmallVideo for the given clickId exists we proceed with the
  359. // pin/unpin.
  360. if (smallVideo) {
  361. this.nextOnStageTimer = 0;
  362. clearTimeout(this.nextOnStageTimout);
  363. if (pin && !VideoLayout.isPinned(clickId)
  364. || !pin && VideoLayout.isPinned(clickId))
  365. VideoLayout.handleVideoThumbClicked(clickId);
  366. }
  367. // If there's no SmallVideo object for the given id, lets wait and see
  368. // if it's going to be created in the next 30sec.
  369. else {
  370. this.nextOnStageTimout = setTimeout(function () {
  371. if (self.nextOnStageTimer > _FOLLOW_ME_RECEIVED_TIMEOUT) {
  372. self.nextOnStageTimer = 0;
  373. return;
  374. }
  375. this.nextOnStageTimer++;
  376. self._pinVideoThumbnailById(clickId, pin);
  377. }, 1000);
  378. }
  379. }
  380. }
  381. export default FollowMe;