選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

FollowMe.js 15KB

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