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.

Recording.js 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. /* global APP, $, config, interfaceConfig, JitsiMeetJS */
  2. /*
  3. * Copyright @ 2015 Atlassian Pty Ltd
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. const logger = require("jitsi-meet-logger").getLogger(__filename);
  18. import UIEvents from "../../../service/UI/UIEvents";
  19. import UIUtil from '../util/UIUtil';
  20. import VideoLayout from '../videolayout/VideoLayout';
  21. import Feedback from '../feedback/Feedback.js';
  22. import { setToolboxEnabled } from '../../../react/features/toolbox';
  23. import { setNotificationsEnabled } from '../../../react/features/notifications';
  24. /**
  25. * The dialog for user input.
  26. */
  27. let dialog = null;
  28. /**
  29. * Indicates if the recording button should be enabled.
  30. *
  31. * @returns {boolean} {true} if the
  32. * @private
  33. */
  34. function _isRecordingButtonEnabled() {
  35. return (
  36. interfaceConfig.TOOLBAR_BUTTONS.indexOf("recording") !== -1
  37. && config.enableRecording
  38. && APP.conference.isRecordingSupported());
  39. }
  40. /**
  41. * Request live stream token from the user.
  42. * @returns {Promise}
  43. */
  44. function _requestLiveStreamId() {
  45. const cancelButton
  46. = APP.translation.generateTranslationHTML("dialog.Cancel");
  47. const backButton = APP.translation.generateTranslationHTML("dialog.Back");
  48. const startStreamingButton
  49. = APP.translation.generateTranslationHTML("dialog.startLiveStreaming");
  50. const streamIdRequired
  51. = APP.translation.generateTranslationHTML(
  52. "liveStreaming.streamIdRequired");
  53. const streamIdHelp
  54. = APP.translation.generateTranslationHTML(
  55. "liveStreaming.streamIdHelp");
  56. return new Promise(function (resolve, reject) {
  57. dialog = APP.UI.messageHandler.openDialogWithStates({
  58. state0: {
  59. titleKey: "dialog.liveStreaming",
  60. html:
  61. `<input class="input-control"
  62. name="streamId" type="text"
  63. data-i18n="[placeholder]dialog.streamKey"
  64. autofocus><div style="text-align: right">
  65. <a class="helper-link" target="_new"
  66. href="${interfaceConfig.LIVE_STREAMING_HELP_LINK}">`
  67. + streamIdHelp
  68. + `</a></div>`,
  69. persistent: false,
  70. buttons: [
  71. {title: cancelButton, value: false},
  72. {title: startStreamingButton, value: true}
  73. ],
  74. focus: ':input:first',
  75. defaultButton: 1,
  76. submit: function (e, v, m, f) {
  77. e.preventDefault();
  78. if (v) {
  79. if (f.streamId && f.streamId.length > 0) {
  80. resolve(UIUtil.escapeHtml(f.streamId));
  81. dialog.close();
  82. return;
  83. }
  84. else {
  85. dialog.goToState('state1');
  86. return false;
  87. }
  88. } else {
  89. reject(APP.UI.messageHandler.CANCEL);
  90. dialog.close();
  91. return false;
  92. }
  93. }
  94. },
  95. state1: {
  96. titleKey: "dialog.liveStreaming",
  97. html: streamIdRequired,
  98. persistent: false,
  99. buttons: [
  100. {title: cancelButton, value: false},
  101. {title: backButton, value: true}
  102. ],
  103. focus: ':input:first',
  104. defaultButton: 1,
  105. submit: function (e, v) {
  106. e.preventDefault();
  107. if (v === 0) {
  108. reject(APP.UI.messageHandler.CANCEL);
  109. dialog.close();
  110. } else {
  111. dialog.goToState('state0');
  112. }
  113. }
  114. }
  115. }, {
  116. close: function () {
  117. dialog = null;
  118. }
  119. });
  120. });
  121. }
  122. /**
  123. * Request recording token from the user.
  124. * @returns {Promise}
  125. */
  126. function _requestRecordingToken() {
  127. let titleKey = "dialog.recordingToken";
  128. let msgString = (
  129. `<input name="recordingToken" type="text"
  130. data-i18n="[placeholder]dialog.token"
  131. class="input-control"
  132. autofocus>`
  133. );
  134. return new Promise(function (resolve, reject) {
  135. dialog = APP.UI.messageHandler.openTwoButtonDialog({
  136. titleKey,
  137. msgString,
  138. leftButtonKey: 'dialog.Save',
  139. submitFunction: function (e, v, m, f) {
  140. if (v && f.recordingToken) {
  141. resolve(UIUtil.escapeHtml(f.recordingToken));
  142. } else {
  143. reject(APP.UI.messageHandler.CANCEL);
  144. }
  145. },
  146. closeFunction: function () {
  147. dialog = null;
  148. },
  149. focus: ':input:first'
  150. });
  151. });
  152. }
  153. /**
  154. * Shows a prompt dialog to the user when they have toggled off the recording.
  155. *
  156. * @param recordingType the recording type
  157. * @returns {Promise}
  158. * @private
  159. */
  160. function _showStopRecordingPrompt(recordingType) {
  161. var title;
  162. var message;
  163. var buttonKey;
  164. if (recordingType === "jibri") {
  165. title = "dialog.liveStreaming";
  166. message = "dialog.stopStreamingWarning";
  167. buttonKey = "dialog.stopLiveStreaming";
  168. }
  169. else {
  170. title = "dialog.recording";
  171. message = "dialog.stopRecordingWarning";
  172. buttonKey = "dialog.stopRecording";
  173. }
  174. return new Promise((resolve, reject) => {
  175. dialog = APP.UI.messageHandler.openTwoButtonDialog({
  176. titleKey: title,
  177. msgKey: message,
  178. leftButtonKey: buttonKey,
  179. submitFunction: (e, v) => (v ? resolve : reject)(),
  180. closeFunction: () => {
  181. dialog = null;
  182. }
  183. });
  184. });
  185. }
  186. /**
  187. * Moves the element given by {selector} to the top right corner of the screen.
  188. * Set additional classes that can be used to style the selector relative to the
  189. * state of the filmstrip.
  190. *
  191. * @param selector the selector for the element to move
  192. * @param move {true} to move the element, {false} to move it back to its intial
  193. * position
  194. */
  195. function moveToCorner(selector, move) {
  196. let moveToCornerClass = "moveToCorner";
  197. let containsClass = selector.hasClass(moveToCornerClass);
  198. if (move && !containsClass)
  199. selector.addClass(moveToCornerClass);
  200. else if (!move && containsClass)
  201. selector.removeClass(moveToCornerClass);
  202. const {
  203. remoteVideosVisible,
  204. visible
  205. } = APP.store.getState()['features/filmstrip'];
  206. const filmstripWasHidden = selector.hasClass('without-filmstrip');
  207. const filmstipIsOpening = filmstripWasHidden && visible;
  208. selector.toggleClass('opening', filmstipIsOpening);
  209. selector.toggleClass('with-filmstrip', visible);
  210. selector.toggleClass('without-filmstrip', !visible);
  211. selector.toggleClass('with-remote-videos', remoteVideosVisible);
  212. selector.toggleClass('without-remote-videos', !remoteVideosVisible);
  213. }
  214. /**
  215. * The status of the recorder.
  216. * FIXME: Those constants should come from the library.
  217. * @type {{ON: string, OFF: string, AVAILABLE: string,
  218. * UNAVAILABLE: string, PENDING: string}}
  219. */
  220. var Status = {
  221. ON: "on",
  222. OFF: "off",
  223. AVAILABLE: "available",
  224. UNAVAILABLE: "unavailable",
  225. PENDING: "pending",
  226. RETRYING: "retrying",
  227. ERROR: "error",
  228. FAILED: "failed",
  229. BUSY: "busy"
  230. };
  231. /**
  232. * Checks whether if the given status is either PENDING or RETRYING
  233. * @param status {Status} Jibri status to be checked
  234. * @returns {boolean} true if the condition is met or false otherwise.
  235. */
  236. function isStartingStatus(status) {
  237. return status === Status.PENDING || status === Status.RETRYING;
  238. }
  239. /**
  240. * Manages the recording user interface and user experience.
  241. * @type {{init, initRecordingButton, showRecordingButton, updateRecordingState,
  242. * updateRecordingUI, checkAutoRecord}}
  243. */
  244. var Recording = {
  245. /**
  246. * Initializes the recording UI.
  247. */
  248. init(eventEmitter, recordingType) {
  249. this.eventEmitter = eventEmitter;
  250. this.recordingType = recordingType;
  251. this.updateRecordingState(APP.conference.getRecordingState());
  252. if (recordingType === 'jibri') {
  253. this.baseClass = "fa fa-play-circle";
  254. this.recordingTitle = "dialog.liveStreaming";
  255. this.recordingOnKey = "liveStreaming.on";
  256. this.recordingOffKey = "liveStreaming.off";
  257. this.recordingPendingKey = "liveStreaming.pending";
  258. this.failedToStartKey = "liveStreaming.failedToStart";
  259. this.recordingErrorKey = "liveStreaming.error";
  260. this.recordingButtonTooltip = "liveStreaming.buttonTooltip";
  261. this.recordingUnavailable = "liveStreaming.unavailable";
  262. this.recordingBusy = "liveStreaming.busy";
  263. }
  264. else {
  265. this.baseClass = "icon-recEnable";
  266. this.recordingTitle = "dialog.recording";
  267. this.recordingOnKey = "recording.on";
  268. this.recordingOffKey = "recording.off";
  269. this.recordingPendingKey = "recording.pending";
  270. this.failedToStartKey = "recording.failedToStart";
  271. this.recordingErrorKey = "recording.error";
  272. this.recordingButtonTooltip = "recording.buttonTooltip";
  273. this.recordingUnavailable = "recording.unavailable";
  274. this.recordingBusy = "liveStreaming.busy";
  275. }
  276. // XXX Due to the React-ification of Toolbox, the HTMLElement with id
  277. // toolbar_button_record may not exist yet.
  278. $(document).on(
  279. 'click',
  280. '#toolbar_button_record',
  281. ev => this._onToolbarButtonClick(ev));
  282. // If I am a recorder then I publish my recorder custom role to notify
  283. // everyone.
  284. if (config.iAmRecorder) {
  285. VideoLayout.enableDeviceAvailabilityIcons(
  286. APP.conference.getMyUserId(), false);
  287. VideoLayout.setLocalVideoVisible(false);
  288. Feedback.enableFeedback(false);
  289. APP.store.dispatch(setToolboxEnabled(false));
  290. APP.store.dispatch(setNotificationsEnabled(false));
  291. APP.UI.messageHandler.enablePopups(false);
  292. }
  293. this.eventEmitter.addListener(UIEvents.UPDATED_FILMSTRIP_DISPLAY, () =>{
  294. this._updateStatusLabel();
  295. });
  296. },
  297. /**
  298. * Initialise the recording button.
  299. */
  300. initRecordingButton() {
  301. const selector = $('#toolbar_button_record');
  302. UIUtil.setTooltip(selector, 'liveStreaming.buttonTooltip', 'right');
  303. selector.addClass(this.baseClass);
  304. selector.attr("data-i18n", "[content]" + this.recordingButtonTooltip);
  305. APP.translation.translateElement(selector);
  306. },
  307. /**
  308. * Shows or hides the 'recording' button.
  309. * @param show {true} to show the recording button, {false} to hide it
  310. */
  311. showRecordingButton(show) {
  312. let shouldShow = show && _isRecordingButtonEnabled();
  313. let id = 'toolbar_button_record';
  314. UIUtil.setVisible(id, shouldShow);
  315. },
  316. /**
  317. * Updates the recording state UI.
  318. * @param recordingState gives us the current recording state
  319. */
  320. updateRecordingState(recordingState) {
  321. // I'm the recorder, so I don't want to see any UI related to states.
  322. if (config.iAmRecorder)
  323. return;
  324. // If there's no state change, we ignore the update.
  325. if (!recordingState || this.currentState === recordingState)
  326. return;
  327. this.updateRecordingUI(recordingState);
  328. },
  329. /**
  330. * Sets the state of the recording button.
  331. * @param recordingState gives us the current recording state
  332. */
  333. updateRecordingUI(recordingState) {
  334. let oldState = this.currentState;
  335. this.currentState = recordingState;
  336. // TODO: handle recording state=available
  337. if (recordingState === Status.ON ||
  338. recordingState === Status.RETRYING) {
  339. this._setToolbarButtonToggled(true);
  340. this._updateStatusLabel(this.recordingOnKey, false);
  341. }
  342. else if (recordingState === Status.OFF
  343. || recordingState === Status.UNAVAILABLE
  344. || recordingState === Status.BUSY
  345. || recordingState === Status.FAILED) {
  346. // We don't want to do any changes if this is
  347. // an availability change.
  348. if (oldState !== Status.ON
  349. && !isStartingStatus(oldState))
  350. return;
  351. this._setToolbarButtonToggled(false);
  352. let messageKey;
  353. if (isStartingStatus(oldState))
  354. messageKey = this.failedToStartKey;
  355. else
  356. messageKey = this.recordingOffKey;
  357. this._updateStatusLabel(messageKey, true);
  358. setTimeout(function(){
  359. $('#recordingLabel').css({display: "none"});
  360. }, 5000);
  361. }
  362. else if (recordingState === Status.PENDING) {
  363. this._setToolbarButtonToggled(false);
  364. this._updateStatusLabel(this.recordingPendingKey, true);
  365. }
  366. else if (recordingState === Status.ERROR
  367. || recordingState === Status.FAILED) {
  368. this._setToolbarButtonToggled(false);
  369. this._updateStatusLabel(this.recordingErrorKey, true);
  370. }
  371. let labelSelector = $('#recordingLabel');
  372. // We don't show the label for available state.
  373. if (recordingState !== Status.AVAILABLE
  374. && !labelSelector.is(":visible"))
  375. labelSelector.css({display: "inline-block"});
  376. // Recording spinner
  377. let spinnerId = 'recordingSpinner';
  378. UIUtil.setVisible(
  379. spinnerId, recordingState === Status.RETRYING);
  380. },
  381. // checks whether recording is enabled and whether we have params
  382. // to start automatically recording
  383. checkAutoRecord() {
  384. if (_isRecordingButtonEnabled && config.autoRecord) {
  385. this.predefinedToken = UIUtil.escapeHtml(config.autoRecordToken);
  386. this.eventEmitter.emit(UIEvents.RECORDING_TOGGLED,
  387. this.predefinedToken);
  388. }
  389. },
  390. /**
  391. * Updates the status label.
  392. * @param textKey the text to show
  393. * @param isCentered indicates if the label should be centered on the window
  394. * or moved to the top right corner.
  395. */
  396. _updateStatusLabel(textKey, isCentered) {
  397. let labelSelector = $('#recordingLabel');
  398. let labelTextSelector = $('#recordingLabelText');
  399. moveToCorner(labelSelector, !isCentered);
  400. labelTextSelector.attr("data-i18n", textKey);
  401. APP.translation.translateElement(labelSelector);
  402. },
  403. /**
  404. * Handles {@code click} on {@code toolbar_button_record}.
  405. *
  406. * @returns {void}
  407. */
  408. _onToolbarButtonClick() {
  409. if (dialog) {
  410. return;
  411. }
  412. JitsiMeetJS.analytics.sendEvent('recording.clicked');
  413. switch (this.currentState) {
  414. case Status.ON:
  415. case Status.RETRYING:
  416. case Status.PENDING: {
  417. _showStopRecordingPrompt(this.recordingType).then(
  418. () => {
  419. this.eventEmitter.emit(UIEvents.RECORDING_TOGGLED);
  420. JitsiMeetJS.analytics.sendEvent('recording.stopped');
  421. },
  422. () => {});
  423. break;
  424. }
  425. case Status.AVAILABLE:
  426. case Status.OFF: {
  427. if (this.recordingType === 'jibri')
  428. _requestLiveStreamId().then(streamId => {
  429. this.eventEmitter.emit(
  430. UIEvents.RECORDING_TOGGLED,
  431. { streamId });
  432. JitsiMeetJS.analytics.sendEvent('recording.started');
  433. }).catch(reason => {
  434. if (reason !== APP.UI.messageHandler.CANCEL)
  435. logger.error(reason);
  436. else
  437. JitsiMeetJS.analytics.sendEvent('recording.canceled');
  438. });
  439. else {
  440. if (this.predefinedToken) {
  441. this.eventEmitter.emit(
  442. UIEvents.RECORDING_TOGGLED,
  443. { token: this.predefinedToken });
  444. JitsiMeetJS.analytics.sendEvent('recording.started');
  445. return;
  446. }
  447. _requestRecordingToken().then((token) => {
  448. this.eventEmitter.emit(
  449. UIEvents.RECORDING_TOGGLED,
  450. { token });
  451. JitsiMeetJS.analytics.sendEvent('recording.started');
  452. }).catch(reason => {
  453. if (reason !== APP.UI.messageHandler.CANCEL)
  454. logger.error(reason);
  455. else
  456. JitsiMeetJS.analytics.sendEvent('recording.canceled');
  457. });
  458. }
  459. break;
  460. }
  461. case Status.BUSY: {
  462. dialog = APP.UI.messageHandler.openMessageDialog(
  463. this.recordingTitle,
  464. this.recordingBusy,
  465. null,
  466. () => {
  467. dialog = null;
  468. }
  469. );
  470. break;
  471. }
  472. default: {
  473. dialog = APP.UI.messageHandler.openMessageDialog(
  474. this.recordingTitle,
  475. this.recordingUnavailable,
  476. null,
  477. () => {
  478. dialog = null;
  479. }
  480. );
  481. }
  482. }
  483. },
  484. /**
  485. * Sets the toggled state of the recording toolbar button.
  486. *
  487. * @param {boolean} isToggled indicates if the button should be toggled
  488. * or not
  489. */
  490. _setToolbarButtonToggled(isToggled) {
  491. $("#toolbar_button_record").toggleClass("toggled", isToggled);
  492. }
  493. };
  494. export default Recording;