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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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. 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. import BottomToolbar from '../toolbars/BottomToolbar';
  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 msg = APP.translation.generateTranslationHTML("dialog.liveStreaming");
  43. const token = APP.translation.translateString("dialog.streamKey");
  44. const cancelButton
  45. = APP.translation.generateTranslationHTML("dialog.Cancel");
  46. const backButton = APP.translation.generateTranslationHTML("dialog.Back");
  47. const startStreamingButton
  48. = APP.translation.generateTranslationHTML("dialog.startLiveStreaming");
  49. const streamIdRequired
  50. = APP.translation.generateTranslationHTML(
  51. "liveStreaming.streamIdRequired");
  52. return new Promise(function (resolve, reject) {
  53. dialog = APP.UI.messageHandler.openDialogWithStates({
  54. state0: {
  55. html:
  56. `<h2>${msg}</h2>
  57. <input name="streamId" type="text"
  58. data-i18n="[placeholder]dialog.streamKey"
  59. placeholder="${token}" autofocus>`,
  60. persistent: false,
  61. buttons: [
  62. {title: cancelButton, value: false},
  63. {title: startStreamingButton, value: true}
  64. ],
  65. focus: ':input:first',
  66. defaultButton: 1,
  67. submit: function (e, v, m, f) {
  68. e.preventDefault();
  69. if (v) {
  70. if (f.streamId && f.streamId.length > 0) {
  71. resolve(UIUtil.escapeHtml(f.streamId));
  72. dialog.close();
  73. return;
  74. }
  75. else {
  76. dialog.goToState('state1');
  77. return false;
  78. }
  79. } else {
  80. reject(APP.UI.messageHandler.CANCEL);
  81. dialog.close();
  82. return false;
  83. }
  84. }
  85. },
  86. state1: {
  87. html: `<h2>${msg}</h2> ${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, m, f) {
  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 msg = APP.translation.generateTranslationHTML("dialog.recordingToken");
  118. let token = APP.translation.translateString("dialog.token");
  119. return new Promise(function (resolve, reject) {
  120. dialog = APP.UI.messageHandler.openTwoButtonDialog(
  121. null, null, null,
  122. `<h2>${msg}</h2>
  123. <input name="recordingToken" type="text"
  124. data-i18n="[placeholder]dialog.token"
  125. placeholder="${token}" autofocus>`,
  126. false, "dialog.Save",
  127. function (e, v, m, f) {
  128. if (v && f.recordingToken) {
  129. resolve(UIUtil.escapeHtml(f.recordingToken));
  130. } else {
  131. reject(APP.UI.messageHandler.CANCEL);
  132. }
  133. },
  134. null,
  135. function () {
  136. dialog = null;
  137. },
  138. ':input:first'
  139. );
  140. });
  141. }
  142. /**
  143. * Shows a prompt dialog to the user when they have toggled off the recording.
  144. *
  145. * @param recordingType the recording type
  146. * @returns {Promise}
  147. * @private
  148. */
  149. function _showStopRecordingPrompt (recordingType) {
  150. var title;
  151. var message;
  152. var buttonKey;
  153. if (recordingType === "jibri") {
  154. title = "dialog.liveStreaming";
  155. message = "dialog.stopStreamingWarning";
  156. buttonKey = "dialog.stopLiveStreaming";
  157. }
  158. else {
  159. title = "dialog.recording";
  160. message = "dialog.stopRecordingWarning";
  161. buttonKey = "dialog.stopRecording";
  162. }
  163. return new Promise(function (resolve, reject) {
  164. dialog = APP.UI.messageHandler.openTwoButtonDialog(
  165. title,
  166. null,
  167. message,
  168. null,
  169. false,
  170. buttonKey,
  171. function(e,v,m,f) {
  172. if (v) {
  173. resolve();
  174. } else {
  175. reject();
  176. }
  177. },
  178. null,
  179. function () {
  180. dialog = null;
  181. }
  182. );
  183. });
  184. }
  185. /**
  186. * Moves the element given by {selector} to the top right corner of the screen.
  187. * @param selector the selector for the element to move
  188. * @param move {true} to move the element, {false} to move it back to its intial
  189. * position
  190. */
  191. function moveToCorner(selector, move) {
  192. let moveToCornerClass = "moveToCorner";
  193. if (move && !selector.hasClass(moveToCornerClass))
  194. selector.addClass(moveToCornerClass);
  195. else
  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. ERROR: "error",
  211. FAILED: "failed",
  212. BUSY: "busy"
  213. };
  214. /**
  215. * Manages the recording user interface and user experience.
  216. * @type {{init, initRecordingButton, showRecordingButton, updateRecordingState,
  217. * updateRecordingUI, checkAutoRecord}}
  218. */
  219. var Recording = {
  220. /**
  221. * Initializes the recording UI.
  222. */
  223. init (emitter, recordingType) {
  224. this.eventEmitter = emitter;
  225. this.updateRecordingState(APP.conference.getRecordingState());
  226. this.initRecordingButton(recordingType);
  227. // If I am a recorder then I publish my recorder custom role to notify
  228. // everyone.
  229. if (config.iAmRecorder) {
  230. VideoLayout.enableDeviceAvailabilityIcons(
  231. APP.conference.localId, false);
  232. VideoLayout.setLocalVideoVisible(false);
  233. Feedback.enableFeedback(false);
  234. Toolbar.enable(false);
  235. BottomToolbar.enable(false);
  236. APP.UI.messageHandler.enableNotifications(false);
  237. APP.UI.messageHandler.enablePopups(false);
  238. }
  239. },
  240. /**
  241. * Initialise the recording button.
  242. */
  243. initRecordingButton(recordingType) {
  244. let selector = $('#toolbar_button_record');
  245. if (recordingType === 'jibri') {
  246. this.baseClass = "fa fa-play-circle";
  247. this.recordingTitle = "dialog.liveStreaming";
  248. this.recordingOnKey = "liveStreaming.on";
  249. this.recordingOffKey = "liveStreaming.off";
  250. this.recordingPendingKey = "liveStreaming.pending";
  251. this.failedToStartKey = "liveStreaming.failedToStart";
  252. this.recordingErrorKey = "liveStreaming.error";
  253. this.recordingButtonTooltip = "liveStreaming.buttonTooltip";
  254. this.recordingUnavailable = "liveStreaming.unavailable";
  255. this.recordingBusy = "liveStreaming.busy";
  256. }
  257. else {
  258. this.baseClass = "icon-recEnable";
  259. this.recordingTitle = "dialog.recording";
  260. this.recordingOnKey = "recording.on";
  261. this.recordingOffKey = "recording.off";
  262. this.recordingPendingKey = "recording.pending";
  263. this.failedToStartKey = "recording.failedToStart";
  264. this.recordingErrorKey = "recording.error";
  265. this.recordingButtonTooltip = "recording.buttonTooltip";
  266. this.recordingUnavailable = "recording.unavailable";
  267. this.recordingBusy = "liveStreaming.busy";
  268. }
  269. selector.addClass(this.baseClass);
  270. selector.attr("data-i18n", "[content]" + this.recordingButtonTooltip);
  271. selector.attr("content",
  272. APP.translation.translateString(this.recordingButtonTooltip));
  273. var self = this;
  274. selector.click(function () {
  275. if (dialog)
  276. return;
  277. switch (self.currentState) {
  278. case Status.ON:
  279. case Status.PENDING: {
  280. _showStopRecordingPrompt(recordingType).then(() =>
  281. self.eventEmitter.emit(UIEvents.RECORDING_TOGGLED),
  282. () => {});
  283. break;
  284. }
  285. case Status.AVAILABLE:
  286. case Status.OFF: {
  287. if (recordingType === 'jibri')
  288. _requestLiveStreamId().then((streamId) => {
  289. self.eventEmitter.emit( UIEvents.RECORDING_TOGGLED,
  290. {streamId: streamId});
  291. }).catch(
  292. reason => {
  293. if (reason !== APP.UI.messageHandler.CANCEL)
  294. console.error(reason);
  295. }
  296. );
  297. else {
  298. if (self.predefinedToken) {
  299. self.eventEmitter.emit( UIEvents.RECORDING_TOGGLED,
  300. {token: self.predefinedToken});
  301. return;
  302. }
  303. _requestRecordingToken().then((token) => {
  304. self.eventEmitter.emit( UIEvents.RECORDING_TOGGLED,
  305. {token: token});
  306. }).catch(
  307. reason => {
  308. if (reason !== APP.UI.messageHandler.CANCEL)
  309. console.error(reason);
  310. }
  311. );
  312. }
  313. break;
  314. }
  315. case Status.BUSY: {
  316. dialog = APP.UI.messageHandler.openMessageDialog(
  317. self.recordingTitle,
  318. self.recordingBusy,
  319. null, null,
  320. function () {
  321. dialog = null;
  322. }
  323. );
  324. break;
  325. }
  326. default: {
  327. dialog = APP.UI.messageHandler.openMessageDialog(
  328. self.recordingTitle,
  329. self.recordingUnavailable,
  330. null, null,
  331. function () {
  332. dialog = null;
  333. }
  334. );
  335. }
  336. }
  337. });
  338. },
  339. /**
  340. * Shows or hides the 'recording' button.
  341. * @param show {true} to show the recording button, {false} to hide it
  342. */
  343. showRecordingButton (show) {
  344. if (_isRecordingButtonEnabled() && show) {
  345. $('#toolbar_button_record').css({display: "inline-block"});
  346. } else {
  347. $('#toolbar_button_record').css({display: "none"});
  348. }
  349. },
  350. /**
  351. * Updates the recording state UI.
  352. * @param recordingState gives us the current recording state
  353. */
  354. updateRecordingState(recordingState) {
  355. // I'm the recorder, so I don't want to see any UI related to states.
  356. if (config.iAmRecorder)
  357. return;
  358. // If there's no state change, we ignore the update.
  359. if (!recordingState || this.currentState === recordingState)
  360. return;
  361. this.updateRecordingUI(recordingState);
  362. },
  363. /**
  364. * Sets the state of the recording button.
  365. * @param recordingState gives us the current recording state
  366. */
  367. updateRecordingUI (recordingState) {
  368. let buttonSelector = $('#toolbar_button_record');
  369. let oldState = this.currentState;
  370. this.currentState = recordingState;
  371. // TODO: handle recording state=available
  372. if (recordingState === Status.ON) {
  373. buttonSelector.removeClass(this.baseClass);
  374. buttonSelector.addClass(this.baseClass + " active");
  375. this._updateStatusLabel(this.recordingOnKey, false);
  376. }
  377. else if (recordingState === Status.OFF
  378. || recordingState === Status.UNAVAILABLE
  379. || recordingState === Status.BUSY
  380. || recordingState === Status.FAILED) {
  381. // We don't want to do any changes if this is
  382. // an availability change.
  383. if (oldState !== Status.ON
  384. && oldState !== Status.PENDING)
  385. return;
  386. buttonSelector.removeClass(this.baseClass + " active");
  387. buttonSelector.addClass(this.baseClass);
  388. let messageKey;
  389. if (oldState === Status.PENDING)
  390. messageKey = this.failedToStartKey;
  391. else
  392. messageKey = this.recordingOffKey;
  393. this._updateStatusLabel(messageKey, true);
  394. setTimeout(function(){
  395. $('#recordingLabel').css({display: "none"});
  396. }, 5000);
  397. }
  398. else if (recordingState === Status.PENDING) {
  399. buttonSelector.removeClass(this.baseClass + " active");
  400. buttonSelector.addClass(this.baseClass);
  401. this._updateStatusLabel(this.recordingPendingKey, true);
  402. }
  403. else if (recordingState === Status.ERROR
  404. || recordingState === Status.FAILED) {
  405. buttonSelector.removeClass(this.baseClass + " active");
  406. buttonSelector.addClass(this.baseClass);
  407. this._updateStatusLabel(this.recordingErrorKey, true);
  408. }
  409. let labelSelector = $('#recordingLabel');
  410. // We don't show the label for available state.
  411. if (recordingState !== Status.AVAILABLE
  412. && !labelSelector.is(":visible"))
  413. labelSelector.css({display: "inline-block"});
  414. },
  415. // checks whether recording is enabled and whether we have params
  416. // to start automatically recording
  417. checkAutoRecord () {
  418. if (_isRecordingButtonEnabled && config.autoRecord) {
  419. this.predefinedToken = UIUtil.escapeHtml(config.autoRecordToken);
  420. this.eventEmitter.emit(UIEvents.RECORDING_TOGGLED,
  421. this.predefinedToken);
  422. }
  423. },
  424. /**
  425. * Updates the status label.
  426. * @param textKey the text to show
  427. * @param isCentered indicates if the label should be centered on the window
  428. * or moved to the top right corner.
  429. */
  430. _updateStatusLabel(textKey, isCentered) {
  431. let labelSelector = $('#recordingLabel');
  432. moveToCorner(labelSelector, !isCentered);
  433. labelSelector.attr("data-i18n", textKey);
  434. labelSelector.text(APP.translation.translateString(textKey));
  435. }
  436. };
  437. export default Recording;