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

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