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.

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