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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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. let containsClass = selector.hasClass(moveToCornerClass);
  194. if (move && !containsClass)
  195. selector.addClass(moveToCornerClass);
  196. else if (!move && containsClass)
  197. selector.removeClass(moveToCornerClass);
  198. }
  199. /**
  200. * The status of the recorder.
  201. * FIXME: Those constants should come from the library.
  202. * @type {{ON: string, OFF: string, AVAILABLE: string,
  203. * UNAVAILABLE: string, PENDING: string}}
  204. */
  205. var Status = {
  206. ON: "on",
  207. OFF: "off",
  208. AVAILABLE: "available",
  209. UNAVAILABLE: "unavailable",
  210. PENDING: "pending",
  211. ERROR: "error",
  212. FAILED: "failed",
  213. BUSY: "busy"
  214. };
  215. /**
  216. * Manages the recording user interface and user experience.
  217. * @type {{init, initRecordingButton, showRecordingButton, updateRecordingState,
  218. * updateRecordingUI, checkAutoRecord}}
  219. */
  220. var Recording = {
  221. /**
  222. * Initializes the recording UI.
  223. */
  224. init (emitter, recordingType) {
  225. this.eventEmitter = emitter;
  226. this.updateRecordingState(APP.conference.getRecordingState());
  227. this.initRecordingButton(recordingType);
  228. // If I am a recorder then I publish my recorder custom role to notify
  229. // everyone.
  230. if (config.iAmRecorder) {
  231. VideoLayout.enableDeviceAvailabilityIcons(
  232. APP.conference.localId, false);
  233. VideoLayout.setLocalVideoVisible(false);
  234. Feedback.enableFeedback(false);
  235. Toolbar.enable(false);
  236. BottomToolbar.enable(false);
  237. APP.UI.messageHandler.enableNotifications(false);
  238. APP.UI.messageHandler.enablePopups(false);
  239. }
  240. },
  241. /**
  242. * Initialise the recording button.
  243. */
  244. initRecordingButton(recordingType) {
  245. let selector = $('#toolbar_button_record');
  246. if (recordingType === 'jibri') {
  247. this.baseClass = "fa fa-play-circle";
  248. this.recordingTitle = "dialog.liveStreaming";
  249. this.recordingOnKey = "liveStreaming.on";
  250. this.recordingOffKey = "liveStreaming.off";
  251. this.recordingPendingKey = "liveStreaming.pending";
  252. this.failedToStartKey = "liveStreaming.failedToStart";
  253. this.recordingErrorKey = "liveStreaming.error";
  254. this.recordingButtonTooltip = "liveStreaming.buttonTooltip";
  255. this.recordingUnavailable = "liveStreaming.unavailable";
  256. this.recordingBusy = "liveStreaming.busy";
  257. }
  258. else {
  259. this.baseClass = "icon-recEnable";
  260. this.recordingTitle = "dialog.recording";
  261. this.recordingOnKey = "recording.on";
  262. this.recordingOffKey = "recording.off";
  263. this.recordingPendingKey = "recording.pending";
  264. this.failedToStartKey = "recording.failedToStart";
  265. this.recordingErrorKey = "recording.error";
  266. this.recordingButtonTooltip = "recording.buttonTooltip";
  267. this.recordingUnavailable = "recording.unavailable";
  268. this.recordingBusy = "liveStreaming.busy";
  269. }
  270. selector.addClass(this.baseClass);
  271. selector.attr("data-i18n", "[content]" + this.recordingButtonTooltip);
  272. selector.attr("content",
  273. APP.translation.translateString(this.recordingButtonTooltip));
  274. var self = this;
  275. selector.click(function () {
  276. if (dialog)
  277. return;
  278. switch (self.currentState) {
  279. case Status.ON:
  280. case Status.PENDING: {
  281. _showStopRecordingPrompt(recordingType).then(() =>
  282. self.eventEmitter.emit(UIEvents.RECORDING_TOGGLED),
  283. () => {});
  284. break;
  285. }
  286. case Status.AVAILABLE:
  287. case Status.OFF: {
  288. if (recordingType === 'jibri')
  289. _requestLiveStreamId().then((streamId) => {
  290. self.eventEmitter.emit( UIEvents.RECORDING_TOGGLED,
  291. {streamId: streamId});
  292. }).catch(
  293. reason => {
  294. if (reason !== APP.UI.messageHandler.CANCEL)
  295. console.error(reason);
  296. }
  297. );
  298. else {
  299. if (self.predefinedToken) {
  300. self.eventEmitter.emit( UIEvents.RECORDING_TOGGLED,
  301. {token: self.predefinedToken});
  302. return;
  303. }
  304. _requestRecordingToken().then((token) => {
  305. self.eventEmitter.emit( UIEvents.RECORDING_TOGGLED,
  306. {token: token});
  307. }).catch(
  308. reason => {
  309. if (reason !== APP.UI.messageHandler.CANCEL)
  310. console.error(reason);
  311. }
  312. );
  313. }
  314. break;
  315. }
  316. case Status.BUSY: {
  317. dialog = APP.UI.messageHandler.openMessageDialog(
  318. self.recordingTitle,
  319. self.recordingBusy,
  320. null, null,
  321. function () {
  322. dialog = null;
  323. }
  324. );
  325. break;
  326. }
  327. default: {
  328. dialog = APP.UI.messageHandler.openMessageDialog(
  329. self.recordingTitle,
  330. self.recordingUnavailable,
  331. null, null,
  332. function () {
  333. dialog = null;
  334. }
  335. );
  336. }
  337. }
  338. });
  339. },
  340. /**
  341. * Shows or hides the 'recording' button.
  342. * @param show {true} to show the recording button, {false} to hide it
  343. */
  344. showRecordingButton (show) {
  345. if (_isRecordingButtonEnabled() && show) {
  346. $('#toolbar_button_record').css({display: "inline-block"});
  347. } else {
  348. $('#toolbar_button_record').css({display: "none"});
  349. }
  350. },
  351. /**
  352. * Updates the recording state UI.
  353. * @param recordingState gives us the current recording state
  354. */
  355. updateRecordingState(recordingState) {
  356. // I'm the recorder, so I don't want to see any UI related to states.
  357. if (config.iAmRecorder)
  358. return;
  359. // If there's no state change, we ignore the update.
  360. if (!recordingState || this.currentState === recordingState)
  361. return;
  362. this.updateRecordingUI(recordingState);
  363. },
  364. /**
  365. * Sets the state of the recording button.
  366. * @param recordingState gives us the current recording state
  367. */
  368. updateRecordingUI (recordingState) {
  369. let buttonSelector = $('#toolbar_button_record');
  370. let oldState = this.currentState;
  371. this.currentState = recordingState;
  372. // TODO: handle recording state=available
  373. if (recordingState === Status.ON) {
  374. buttonSelector.removeClass(this.baseClass);
  375. buttonSelector.addClass(this.baseClass + " active");
  376. this._updateStatusLabel(this.recordingOnKey, false);
  377. }
  378. else if (recordingState === Status.OFF
  379. || recordingState === Status.UNAVAILABLE
  380. || recordingState === Status.BUSY
  381. || recordingState === Status.FAILED) {
  382. // We don't want to do any changes if this is
  383. // an availability change.
  384. if (oldState !== Status.ON
  385. && oldState !== Status.PENDING)
  386. return;
  387. buttonSelector.removeClass(this.baseClass + " active");
  388. buttonSelector.addClass(this.baseClass);
  389. let messageKey;
  390. if (oldState === Status.PENDING)
  391. messageKey = this.failedToStartKey;
  392. else
  393. messageKey = this.recordingOffKey;
  394. this._updateStatusLabel(messageKey, true);
  395. setTimeout(function(){
  396. $('#recordingLabel').css({display: "none"});
  397. }, 5000);
  398. }
  399. else if (recordingState === Status.PENDING) {
  400. buttonSelector.removeClass(this.baseClass + " active");
  401. buttonSelector.addClass(this.baseClass);
  402. this._updateStatusLabel(this.recordingPendingKey, true);
  403. }
  404. else if (recordingState === Status.ERROR
  405. || recordingState === Status.FAILED) {
  406. buttonSelector.removeClass(this.baseClass + " active");
  407. buttonSelector.addClass(this.baseClass);
  408. this._updateStatusLabel(this.recordingErrorKey, true);
  409. }
  410. let labelSelector = $('#recordingLabel');
  411. // We don't show the label for available state.
  412. if (recordingState !== Status.AVAILABLE
  413. && !labelSelector.is(":visible"))
  414. labelSelector.css({display: "inline-block"});
  415. },
  416. // checks whether recording is enabled and whether we have params
  417. // to start automatically recording
  418. checkAutoRecord () {
  419. if (_isRecordingButtonEnabled && config.autoRecord) {
  420. this.predefinedToken = UIUtil.escapeHtml(config.autoRecordToken);
  421. this.eventEmitter.emit(UIEvents.RECORDING_TOGGLED,
  422. this.predefinedToken);
  423. }
  424. },
  425. /**
  426. * Updates the status label.
  427. * @param textKey the text to show
  428. * @param isCentered indicates if the label should be centered on the window
  429. * or moved to the top right corner.
  430. */
  431. _updateStatusLabel(textKey, isCentered) {
  432. let labelSelector = $('#recordingLabel');
  433. moveToCorner(labelSelector, !isCentered);
  434. labelSelector.attr("data-i18n", textKey);
  435. labelSelector.text(APP.translation.translateString(textKey));
  436. }
  437. };
  438. export default Recording;