Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

Recording.js 17KB

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