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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  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.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. html:
  55. `<h2>${msg}</h2>
  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. html: `<h2>${msg}</h2> ${streamIdRequired}`,
  87. persistent: false,
  88. buttons: [
  89. {title: cancelButton, value: false},
  90. {title: backButton, value: true}
  91. ],
  92. focus: ':input:first',
  93. defaultButton: 1,
  94. submit: function (e, v, m, f) {
  95. e.preventDefault();
  96. if (v === 0) {
  97. reject(APP.UI.messageHandler.CANCEL);
  98. dialog.close();
  99. } else {
  100. dialog.goToState('state0');
  101. }
  102. }
  103. }
  104. }, {
  105. close: function () {
  106. dialog = null;
  107. }
  108. });
  109. });
  110. }
  111. /**
  112. * Request recording token from the user.
  113. * @returns {Promise}
  114. */
  115. function _requestRecordingToken () {
  116. let msg = APP.translation.generateTranslationHTML("dialog.recordingToken");
  117. let token = APP.translation.translateString("dialog.token");
  118. return new Promise(function (resolve, reject) {
  119. dialog = APP.UI.messageHandler.openTwoButtonDialog(
  120. null, null, null,
  121. `<h2>${msg}</h2>
  122. <input name="recordingToken" type="text"
  123. data-i18n="[placeholder]dialog.token"
  124. placeholder="${token}" autofocus>`,
  125. false, "dialog.Save",
  126. function (e, v, m, f) {
  127. if (v && f.recordingToken) {
  128. resolve(UIUtil.escapeHtml(f.recordingToken));
  129. } else {
  130. reject(APP.UI.messageHandler.CANCEL);
  131. }
  132. },
  133. null,
  134. function () {
  135. dialog = null;
  136. },
  137. ':input:first'
  138. );
  139. });
  140. }
  141. /**
  142. * Shows a prompt dialog to the user when they have toggled off the recording.
  143. *
  144. * @param recordingType the recording type
  145. * @returns {Promise}
  146. * @private
  147. */
  148. function _showStopRecordingPrompt (recordingType) {
  149. var title;
  150. var message;
  151. var buttonKey;
  152. if (recordingType === "jibri") {
  153. title = "dialog.liveStreaming";
  154. message = "dialog.stopStreamingWarning";
  155. buttonKey = "dialog.stopLiveStreaming";
  156. }
  157. else {
  158. title = "dialog.recording";
  159. message = "dialog.stopRecordingWarning";
  160. buttonKey = "dialog.stopRecording";
  161. }
  162. return new Promise(function (resolve, reject) {
  163. dialog = APP.UI.messageHandler.openTwoButtonDialog(
  164. title,
  165. null,
  166. message,
  167. null,
  168. false,
  169. buttonKey,
  170. function(e,v,m,f) {
  171. if (v) {
  172. resolve();
  173. } else {
  174. reject();
  175. }
  176. },
  177. null,
  178. function () {
  179. dialog = null;
  180. }
  181. );
  182. });
  183. }
  184. /**
  185. * Moves the element given by {selector} to the top right corner of the screen.
  186. * @param selector the selector for the element to move
  187. * @param move {true} to move the element, {false} to move it back to its intial
  188. * position
  189. */
  190. function moveToCorner(selector, move) {
  191. let moveToCornerClass = "moveToCorner";
  192. let containsClass = selector.hasClass(moveToCornerClass);
  193. if (move && !containsClass)
  194. selector.addClass(moveToCornerClass);
  195. else if (!move && containsClass)
  196. selector.removeClass(moveToCornerClass);
  197. }
  198. /**
  199. * The status of the recorder.
  200. * FIXME: Those constants should come from the library.
  201. * @type {{ON: string, OFF: string, AVAILABLE: string,
  202. * UNAVAILABLE: string, PENDING: string}}
  203. */
  204. var Status = {
  205. ON: "on",
  206. OFF: "off",
  207. AVAILABLE: "available",
  208. UNAVAILABLE: "unavailable",
  209. PENDING: "pending",
  210. RETRYING: "retrying",
  211. ERROR: "error",
  212. FAILED: "failed",
  213. BUSY: "busy"
  214. };
  215. /**
  216. * Checks whether if the given status is either PENDING or RETRYING
  217. * @param status {Status} Jibri status to be checked
  218. * @returns {boolean} true if the condition is met or false otherwise.
  219. */
  220. function isStartingStatus(status) {
  221. return status === Status.PENDING || status === Status.RETRYING;
  222. }
  223. /**
  224. * Manages the recording user interface and user experience.
  225. * @type {{init, initRecordingButton, showRecordingButton, updateRecordingState,
  226. * updateRecordingUI, checkAutoRecord}}
  227. */
  228. var Recording = {
  229. /**
  230. * Initializes the recording UI.
  231. */
  232. init (emitter, recordingType) {
  233. this.eventEmitter = emitter;
  234. this.updateRecordingState(APP.conference.getRecordingState());
  235. this.initRecordingButton(recordingType);
  236. // If I am a recorder then I publish my recorder custom role to notify
  237. // everyone.
  238. if (config.iAmRecorder) {
  239. VideoLayout.enableDeviceAvailabilityIcons(
  240. APP.conference.getMyUserId(), false);
  241. VideoLayout.setLocalVideoVisible(false);
  242. Feedback.enableFeedback(false);
  243. Toolbar.enable(false);
  244. APP.UI.messageHandler.enableNotifications(false);
  245. APP.UI.messageHandler.enablePopups(false);
  246. }
  247. },
  248. /**
  249. * Initialise the recording button.
  250. */
  251. initRecordingButton(recordingType) {
  252. let selector = $('#toolbar_button_record');
  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 buttonSelector = $('#toolbar_button_record');
  395. let oldState = this.currentState;
  396. this.currentState = recordingState;
  397. // TODO: handle recording state=available
  398. if (recordingState === Status.ON ||
  399. recordingState === Status.RETRYING) {
  400. buttonSelector.removeClass(this.baseClass);
  401. buttonSelector.addClass(this.baseClass + " active");
  402. this._updateStatusLabel(this.recordingOnKey, false);
  403. }
  404. else if (recordingState === Status.OFF
  405. || recordingState === Status.UNAVAILABLE
  406. || recordingState === Status.BUSY
  407. || recordingState === Status.FAILED) {
  408. // We don't want to do any changes if this is
  409. // an availability change.
  410. if (oldState !== Status.ON
  411. && !isStartingStatus(oldState))
  412. return;
  413. buttonSelector.removeClass(this.baseClass + " active");
  414. buttonSelector.addClass(this.baseClass);
  415. let messageKey;
  416. if (isStartingStatus(oldState))
  417. messageKey = this.failedToStartKey;
  418. else
  419. messageKey = this.recordingOffKey;
  420. this._updateStatusLabel(messageKey, true);
  421. setTimeout(function(){
  422. $('#recordingLabel').css({display: "none"});
  423. }, 5000);
  424. }
  425. else if (recordingState === Status.PENDING) {
  426. buttonSelector.removeClass(this.baseClass + " active");
  427. buttonSelector.addClass(this.baseClass);
  428. this._updateStatusLabel(this.recordingPendingKey, true);
  429. }
  430. else if (recordingState === Status.ERROR
  431. || recordingState === Status.FAILED) {
  432. buttonSelector.removeClass(this.baseClass + " active");
  433. buttonSelector.addClass(this.baseClass);
  434. this._updateStatusLabel(this.recordingErrorKey, true);
  435. }
  436. let labelSelector = $('#recordingLabel');
  437. // We don't show the label for available state.
  438. if (recordingState !== Status.AVAILABLE
  439. && !labelSelector.is(":visible"))
  440. labelSelector.css({display: "inline-block"});
  441. // Recording spinner
  442. if (recordingState === Status.RETRYING)
  443. $("#recordingSpinner").show();
  444. else
  445. $("#recordingSpinner").hide();
  446. },
  447. // checks whether recording is enabled and whether we have params
  448. // to start automatically recording
  449. checkAutoRecord () {
  450. if (_isRecordingButtonEnabled && config.autoRecord) {
  451. this.predefinedToken = UIUtil.escapeHtml(config.autoRecordToken);
  452. this.eventEmitter.emit(UIEvents.RECORDING_TOGGLED,
  453. this.predefinedToken);
  454. }
  455. },
  456. /**
  457. * Updates the status label.
  458. * @param textKey the text to show
  459. * @param isCentered indicates if the label should be centered on the window
  460. * or moved to the top right corner.
  461. */
  462. _updateStatusLabel(textKey, isCentered) {
  463. let labelSelector = $('#recordingLabel');
  464. let labelTextSelector = $('#recordingLabelText');
  465. moveToCorner(labelSelector, !isCentered);
  466. labelTextSelector.attr("data-i18n", textKey);
  467. labelTextSelector.text(APP.translation.translateString(textKey));
  468. }
  469. };
  470. export default Recording;