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

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