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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. /* global APP, $, config, interfaceConfig */
  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 {
  22. JitsiRecordingStatus
  23. } from '../../../react/features/base/lib-jitsi-meet';
  24. import {
  25. sendEvent
  26. } from '../../../react/features/analytics';
  27. import { setToolboxEnabled } from '../../../react/features/toolbox';
  28. import { setNotificationsEnabled } from '../../../react/features/notifications';
  29. import {
  30. hideRecordingLabel,
  31. updateRecordingState
  32. } from '../../../react/features/recording';
  33. /**
  34. * Translation keys to use for display in the UI when recording the conference
  35. * but not streaming live.
  36. *
  37. * @private
  38. * @type {Object}
  39. */
  40. export const RECORDING_TRANSLATION_KEYS = {
  41. failedToStartKey: 'recording.failedToStart',
  42. recordingBusy: 'liveStreaming.busy',
  43. recordingButtonTooltip: 'recording.buttonTooltip',
  44. recordingErrorKey: 'recording.error',
  45. recordingOffKey: 'recording.off',
  46. recordingOnKey: 'recording.on',
  47. recordingPendingKey: 'recording.pending',
  48. recordingTitle: 'dialog.recording',
  49. recordingUnavailable: 'recording.unavailable'
  50. };
  51. /**
  52. * Translation keys to use for display in the UI when the recording mode is
  53. * currently streaming live.
  54. *
  55. * @private
  56. * @type {Object}
  57. */
  58. export const STREAMING_TRANSLATION_KEYS = {
  59. failedToStartKey: 'liveStreaming.failedToStart',
  60. recordingBusy: 'liveStreaming.busy',
  61. recordingButtonTooltip: 'liveStreaming.buttonTooltip',
  62. recordingErrorKey: 'liveStreaming.error',
  63. recordingOffKey: 'liveStreaming.off',
  64. recordingOnKey: 'liveStreaming.on',
  65. recordingPendingKey: 'liveStreaming.pending',
  66. recordingTitle: 'dialog.liveStreaming',
  67. recordingUnavailable: 'liveStreaming.unavailable'
  68. };
  69. /**
  70. * The dialog for user input.
  71. */
  72. let dialog = null;
  73. /**
  74. * Indicates if the recording button should be enabled.
  75. *
  76. * @returns {boolean} {true} if the
  77. * @private
  78. */
  79. function _isRecordingButtonEnabled() {
  80. return (
  81. interfaceConfig.TOOLBAR_BUTTONS.indexOf("recording") !== -1
  82. && config.enableRecording
  83. && APP.conference.isRecordingSupported());
  84. }
  85. /**
  86. * Request live stream token from the user.
  87. * @returns {Promise}
  88. */
  89. function _requestLiveStreamId() {
  90. const cancelButton
  91. = APP.translation.generateTranslationHTML("dialog.Cancel");
  92. const backButton = APP.translation.generateTranslationHTML("dialog.Back");
  93. const startStreamingButton
  94. = APP.translation.generateTranslationHTML("dialog.startLiveStreaming");
  95. const streamIdRequired
  96. = APP.translation.generateTranslationHTML(
  97. "liveStreaming.streamIdRequired");
  98. const streamIdHelp
  99. = APP.translation.generateTranslationHTML(
  100. "liveStreaming.streamIdHelp");
  101. return new Promise(function (resolve, reject) {
  102. dialog = APP.UI.messageHandler.openDialogWithStates({
  103. state0: {
  104. titleKey: "dialog.liveStreaming",
  105. html:
  106. `<input class="input-control"
  107. name="streamId" type="text"
  108. data-i18n="[placeholder]dialog.streamKey"
  109. autofocus><div style="text-align: right">
  110. <a class="helper-link" target="_new"
  111. href="${interfaceConfig.LIVE_STREAMING_HELP_LINK}">`
  112. + streamIdHelp
  113. + `</a></div>`,
  114. persistent: false,
  115. buttons: [
  116. {title: cancelButton, value: false},
  117. {title: startStreamingButton, value: true}
  118. ],
  119. focus: ':input:first',
  120. defaultButton: 1,
  121. submit: function (e, v, m, f) {
  122. e.preventDefault();
  123. if (v) {
  124. if (f.streamId && f.streamId.length > 0) {
  125. resolve(UIUtil.escapeHtml(f.streamId));
  126. dialog.close();
  127. return;
  128. }
  129. else {
  130. dialog.goToState('state1');
  131. return false;
  132. }
  133. } else {
  134. reject(APP.UI.messageHandler.CANCEL);
  135. dialog.close();
  136. return false;
  137. }
  138. }
  139. },
  140. state1: {
  141. titleKey: "dialog.liveStreaming",
  142. html: streamIdRequired,
  143. persistent: false,
  144. buttons: [
  145. {title: cancelButton, value: false},
  146. {title: backButton, value: true}
  147. ],
  148. focus: ':input:first',
  149. defaultButton: 1,
  150. submit: function (e, v) {
  151. e.preventDefault();
  152. if (v === 0) {
  153. reject(APP.UI.messageHandler.CANCEL);
  154. dialog.close();
  155. } else {
  156. dialog.goToState('state0');
  157. }
  158. }
  159. }
  160. }, {
  161. close: function () {
  162. dialog = null;
  163. }
  164. });
  165. });
  166. }
  167. /**
  168. * Request recording token from the user.
  169. * @returns {Promise}
  170. */
  171. function _requestRecordingToken() {
  172. let titleKey = "dialog.recordingToken";
  173. let msgString = (
  174. `<input name="recordingToken" type="text"
  175. data-i18n="[placeholder]dialog.token"
  176. class="input-control"
  177. autofocus>`
  178. );
  179. return new Promise(function (resolve, reject) {
  180. dialog = APP.UI.messageHandler.openTwoButtonDialog({
  181. titleKey,
  182. msgString,
  183. leftButtonKey: 'dialog.Save',
  184. submitFunction: function (e, v, m, f) {
  185. if (v && f.recordingToken) {
  186. resolve(UIUtil.escapeHtml(f.recordingToken));
  187. } else {
  188. reject(APP.UI.messageHandler.CANCEL);
  189. }
  190. },
  191. closeFunction: function () {
  192. dialog = null;
  193. },
  194. focus: ':input:first'
  195. });
  196. });
  197. }
  198. /**
  199. * Shows a prompt dialog to the user when they have toggled off the recording.
  200. *
  201. * @param recordingType the recording type
  202. * @returns {Promise}
  203. * @private
  204. */
  205. function _showStopRecordingPrompt(recordingType) {
  206. var title;
  207. var message;
  208. var buttonKey;
  209. if (recordingType === "jibri") {
  210. title = "dialog.liveStreaming";
  211. message = "dialog.stopStreamingWarning";
  212. buttonKey = "dialog.stopLiveStreaming";
  213. }
  214. else {
  215. title = "dialog.recording";
  216. message = "dialog.stopRecordingWarning";
  217. buttonKey = "dialog.stopRecording";
  218. }
  219. return new Promise((resolve, reject) => {
  220. dialog = APP.UI.messageHandler.openTwoButtonDialog({
  221. titleKey: title,
  222. msgKey: message,
  223. leftButtonKey: buttonKey,
  224. submitFunction: (e, v) => (v ? resolve : reject)(),
  225. closeFunction: () => {
  226. dialog = null;
  227. }
  228. });
  229. });
  230. }
  231. /**
  232. * Checks whether if the given status is either PENDING or RETRYING
  233. * @param status {JitsiRecordingStatus} 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 === JitsiRecordingStatus.PENDING || status === JitsiRecordingStatus.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. Object.assign(this, STREAMING_TRANSLATION_KEYS);
  255. }
  256. else {
  257. this.baseClass = "icon-recEnable";
  258. Object.assign(this, RECORDING_TRANSLATION_KEYS);
  259. }
  260. // XXX Due to the React-ification of Toolbox, the HTMLElement with id
  261. // toolbar_button_record may not exist yet.
  262. $(document).on(
  263. 'click',
  264. '#toolbar_button_record',
  265. ev => this._onToolbarButtonClick(ev));
  266. // If I am a recorder then I publish my recorder custom role to notify
  267. // everyone.
  268. if (config.iAmRecorder) {
  269. VideoLayout.enableDeviceAvailabilityIcons(
  270. APP.conference.getMyUserId(), false);
  271. VideoLayout.setLocalVideoVisible(false);
  272. APP.store.dispatch(setToolboxEnabled(false));
  273. APP.store.dispatch(setNotificationsEnabled(false));
  274. APP.UI.messageHandler.enablePopups(false);
  275. }
  276. },
  277. /**
  278. * Initialise the recording button.
  279. */
  280. initRecordingButton() {
  281. const selector = $('#toolbar_button_record');
  282. selector.addClass(this.baseClass);
  283. selector.attr("data-i18n", "[content]" + this.recordingButtonTooltip);
  284. APP.translation.translateElement(selector);
  285. },
  286. /**
  287. * Shows or hides the 'recording' button.
  288. * @param show {true} to show the recording button, {false} to hide it
  289. */
  290. showRecordingButton(show) {
  291. let shouldShow = show && _isRecordingButtonEnabled();
  292. let id = 'toolbar_button_record';
  293. UIUtil.setVisible(id, shouldShow);
  294. },
  295. /**
  296. * Updates the recording state UI.
  297. * @param recordingState gives us the current recording state
  298. */
  299. updateRecordingState(recordingState) {
  300. // I'm the recorder, so I don't want to see any UI related to states.
  301. if (config.iAmRecorder)
  302. return;
  303. // If there's no state change, we ignore the update.
  304. if (!recordingState || this.currentState === recordingState)
  305. return;
  306. this.updateRecordingUI(recordingState);
  307. },
  308. /**
  309. * Sets the state of the recording button.
  310. * @param recordingState gives us the current recording state
  311. */
  312. updateRecordingUI(recordingState) {
  313. let oldState = this.currentState;
  314. this.currentState = recordingState;
  315. let labelDisplayConfiguration;
  316. switch (recordingState) {
  317. case JitsiRecordingStatus.ON:
  318. case JitsiRecordingStatus.RETRYING: {
  319. labelDisplayConfiguration = {
  320. centered: false,
  321. key: this.recordingOnKey,
  322. showSpinner: recordingState === JitsiRecordingStatus.RETRYING
  323. };
  324. this._setToolbarButtonToggled(true);
  325. break;
  326. }
  327. case JitsiRecordingStatus.OFF:
  328. case JitsiRecordingStatus.BUSY:
  329. case JitsiRecordingStatus.FAILED:
  330. case JitsiRecordingStatus.UNAVAILABLE: {
  331. const wasInStartingStatus = isStartingStatus(oldState);
  332. // We don't want UI changes if this is an availability change.
  333. if (oldState !== JitsiRecordingStatus.ON && !wasInStartingStatus) {
  334. APP.store.dispatch(updateRecordingState({ recordingState }));
  335. return;
  336. }
  337. labelDisplayConfiguration = {
  338. centered: true,
  339. key: wasInStartingStatus
  340. ? this.failedToStartKey
  341. : this.recordingOffKey
  342. };
  343. this._setToolbarButtonToggled(false);
  344. setTimeout(function(){
  345. APP.store.dispatch(hideRecordingLabel());
  346. }, 5000);
  347. break;
  348. }
  349. case JitsiRecordingStatus.PENDING: {
  350. labelDisplayConfiguration = {
  351. centered: true,
  352. key: this.recordingPendingKey
  353. };
  354. this._setToolbarButtonToggled(false);
  355. break;
  356. }
  357. case JitsiRecordingStatus.ERROR: {
  358. labelDisplayConfiguration = {
  359. centered: true,
  360. key: this.recordingErrorKey
  361. };
  362. this._setToolbarButtonToggled(false);
  363. break;
  364. }
  365. // Return an empty label display configuration to indicate no label
  366. // should be displayed. The JitsiRecordingStatus.AVAIABLE case is handled here.
  367. default: {
  368. labelDisplayConfiguration = null;
  369. }
  370. }
  371. APP.store.dispatch(updateRecordingState({
  372. labelDisplayConfiguration,
  373. recordingState
  374. }));
  375. },
  376. // checks whether recording is enabled and whether we have params
  377. // to start automatically recording
  378. checkAutoRecord() {
  379. if (_isRecordingButtonEnabled && config.autoRecord) {
  380. this.predefinedToken = UIUtil.escapeHtml(config.autoRecordToken);
  381. this.eventEmitter.emit(UIEvents.RECORDING_TOGGLED,
  382. this.predefinedToken);
  383. }
  384. },
  385. /**
  386. * Handles {@code click} on {@code toolbar_button_record}.
  387. *
  388. * @returns {void}
  389. */
  390. _onToolbarButtonClick() {
  391. if (dialog) {
  392. return;
  393. }
  394. sendEvent('recording.clicked');
  395. switch (this.currentState) {
  396. case JitsiRecordingStatus.ON:
  397. case JitsiRecordingStatus.RETRYING:
  398. case JitsiRecordingStatus.PENDING: {
  399. _showStopRecordingPrompt(this.recordingType).then(
  400. () => {
  401. this.eventEmitter.emit(UIEvents.RECORDING_TOGGLED);
  402. sendEvent('recording.stopped');
  403. },
  404. () => {});
  405. break;
  406. }
  407. case JitsiRecordingStatus.AVAILABLE:
  408. case JitsiRecordingStatus.OFF: {
  409. if (this.recordingType === 'jibri')
  410. _requestLiveStreamId().then(streamId => {
  411. this.eventEmitter.emit(
  412. UIEvents.RECORDING_TOGGLED,
  413. { streamId });
  414. sendEvent('recording.started');
  415. }).catch(reason => {
  416. if (reason !== APP.UI.messageHandler.CANCEL)
  417. logger.error(reason);
  418. else
  419. sendEvent('recording.canceled');
  420. });
  421. else {
  422. if (this.predefinedToken) {
  423. this.eventEmitter.emit(
  424. UIEvents.RECORDING_TOGGLED,
  425. { token: this.predefinedToken });
  426. sendEvent('recording.started');
  427. return;
  428. }
  429. _requestRecordingToken().then((token) => {
  430. this.eventEmitter.emit(
  431. UIEvents.RECORDING_TOGGLED,
  432. { token });
  433. sendEvent('recording.started');
  434. }).catch(reason => {
  435. if (reason !== APP.UI.messageHandler.CANCEL)
  436. logger.error(reason);
  437. else
  438. sendEvent('recording.canceled');
  439. });
  440. }
  441. break;
  442. }
  443. case JitsiRecordingStatus.BUSY: {
  444. dialog = APP.UI.messageHandler.openMessageDialog(
  445. this.recordingTitle,
  446. this.recordingBusy,
  447. null,
  448. () => {
  449. dialog = null;
  450. }
  451. );
  452. break;
  453. }
  454. default: {
  455. dialog = APP.UI.messageHandler.openMessageDialog(
  456. this.recordingTitle,
  457. this.recordingUnavailable,
  458. null,
  459. () => {
  460. dialog = null;
  461. }
  462. );
  463. }
  464. }
  465. },
  466. /**
  467. * Sets the toggled state of the recording toolbar button.
  468. *
  469. * @param {boolean} isToggled indicates if the button should be toggled
  470. * or not
  471. */
  472. _setToolbarButtonToggled(isToggled) {
  473. $("#toolbar_button_record").toggleClass("toggled", isToggled);
  474. }
  475. };
  476. export default Recording;