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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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. ERROR: "error"
  197. };
  198. /**
  199. * Manages the recording user interface and user experience.
  200. * @type {{init, initRecordingButton, showRecordingButton, updateRecordingState,
  201. * updateRecordingUI, checkAutoRecord}}
  202. */
  203. var Recording = {
  204. /**
  205. * Initializes the recording UI.
  206. */
  207. init (emitter, recordingType) {
  208. this.eventEmitter = emitter;
  209. this.updateRecordingState(APP.conference.getRecordingState());
  210. this.initRecordingButton(recordingType);
  211. // If I am a recorder then I publish my recorder custom role to notify
  212. // everyone.
  213. if (config.iAmRecorder) {
  214. VideoLayout.enableDeviceAvailabilityIcons(
  215. APP.conference.localId, false);
  216. VideoLayout.setLocalVideoVisible(false);
  217. Feedback.enableFeedback(false);
  218. Toolbar.enable(false);
  219. BottomToolbar.enable(false);
  220. }
  221. },
  222. /**
  223. * Initialise the recording button.
  224. */
  225. initRecordingButton(recordingType) {
  226. let selector = $('#toolbar_button_record');
  227. if (recordingType === 'jibri') {
  228. this.baseClass = "fa fa-play-circle";
  229. this.recordingOnKey = "liveStreaming.on";
  230. this.recordingOffKey = "liveStreaming.off";
  231. this.recordingPendingKey = "liveStreaming.pending";
  232. this.failedToStartKey = "liveStreaming.failedToStart";
  233. this.recordingErrorKey = "liveStreaming.error";
  234. this.recordingButtonTooltip = "liveStreaming.buttonTooltip";
  235. }
  236. else {
  237. this.baseClass = "icon-recEnable";
  238. this.recordingOnKey = "recording.on";
  239. this.recordingOffKey = "recording.off";
  240. this.recordingPendingKey = "recording.pending";
  241. this.failedToStartKey = "recording.failedToStart";
  242. this.recordingErrorKey = "recording.error";
  243. this.recordingButtonTooltip = "recording.buttonTooltip";
  244. }
  245. selector.addClass(this.baseClass);
  246. selector.attr("data-i18n", "[content]" + this.recordingButtonTooltip);
  247. selector.attr("content",
  248. APP.translation.translateString(this.recordingButtonTooltip));
  249. var self = this;
  250. selector.click(function () {
  251. switch (self.currentState) {
  252. case Status.ON:
  253. case Status.PENDING: {
  254. _showStopRecordingPrompt(recordingType).then(() =>
  255. self.eventEmitter.emit(UIEvents.RECORDING_TOGGLED));
  256. break;
  257. }
  258. case Status.AVAILABLE:
  259. case Status.OFF: {
  260. if (recordingType === 'jibri')
  261. _requestLiveStreamId().then((streamId) => {
  262. self.eventEmitter.emit( UIEvents.RECORDING_TOGGLED,
  263. {streamId: streamId});
  264. });
  265. else {
  266. if (self.predefinedToken) {
  267. self.eventEmitter.emit( UIEvents.RECORDING_TOGGLED,
  268. {token: self.predefinedToken});
  269. return;
  270. }
  271. _requestRecordingToken().then((token) => {
  272. self.eventEmitter.emit( UIEvents.RECORDING_TOGGLED,
  273. {token: token});
  274. });
  275. }
  276. break;
  277. }
  278. default: {
  279. APP.UI.messageHandler.openMessageDialog(
  280. "dialog.liveStreaming",
  281. "liveStreaming.unavailable"
  282. );
  283. }
  284. }
  285. });
  286. },
  287. /**
  288. * Shows or hides the 'recording' button.
  289. * @param show {true} to show the recording button, {false} to hide it
  290. */
  291. showRecordingButton (show) {
  292. if (_isRecordingButtonEnabled() && show) {
  293. $('#toolbar_button_record').css({display: "inline-block"});
  294. } else {
  295. $('#toolbar_button_record').css({display: "none"});
  296. }
  297. },
  298. /**
  299. * Updates the recording state UI.
  300. * @param recordingState gives us the current recording state
  301. */
  302. updateRecordingState(recordingState) {
  303. // I'm the recorder, so I don't want to see any UI related to states.
  304. if (config.iAmRecorder)
  305. return;
  306. // If there's no state change, we ignore the update.
  307. if (!recordingState || this.currentState === recordingState)
  308. return;
  309. this.updateRecordingUI(recordingState);
  310. },
  311. /**
  312. * Sets the state of the recording button.
  313. * @param recordingState gives us the current recording state
  314. */
  315. updateRecordingUI (recordingState) {
  316. let buttonSelector = $('#toolbar_button_record');
  317. // TODO: handle recording state=available
  318. if (recordingState === Status.ON) {
  319. buttonSelector.removeClass(this.baseClass);
  320. buttonSelector.addClass(this.baseClass + " active");
  321. this._updateStatusLabel(this.recordingOnKey, false);
  322. }
  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. let messageKey;
  333. if (this.currentState === Status.PENDING)
  334. messageKey = this.failedToStartKey;
  335. else
  336. messageKey = this.recordingOffKey;
  337. this._updateStatusLabel(messageKey, true);
  338. setTimeout(function(){
  339. $('#recordingLabel').css({display: "none"});
  340. }, 5000);
  341. }
  342. else if (recordingState === Status.PENDING) {
  343. buttonSelector.removeClass(this.baseClass + " active");
  344. buttonSelector.addClass(this.baseClass);
  345. this._updateStatusLabel(this.recordingPendingKey, true);
  346. }
  347. else if (recordingState === Status.ERROR) {
  348. buttonSelector.removeClass(this.baseClass + " active");
  349. buttonSelector.addClass(this.baseClass);
  350. this._updateStatusLabel(this.recordingErrorKey, true);
  351. }
  352. this.currentState = recordingState;
  353. let labelSelector = $('#recordingLabel');
  354. // We don't show the label for available state.
  355. if (recordingState !== Status.AVAILABLE
  356. && !labelSelector.is(":visible"))
  357. labelSelector.css({display: "inline-block"});
  358. },
  359. // checks whether recording is enabled and whether we have params
  360. // to start automatically recording
  361. checkAutoRecord () {
  362. if (_isRecordingButtonEnabled && config.autoRecord) {
  363. this.predefinedToken = UIUtil.escapeHtml(config.autoRecordToken);
  364. this.eventEmitter.emit(UIEvents.RECORDING_TOGGLED,
  365. this.predefinedToken);
  366. }
  367. },
  368. /**
  369. * Updates the status label.
  370. * @param textKey the text to show
  371. * @param isCentered indicates if the label should be centered on the window
  372. * or moved to the top right corner.
  373. */
  374. _updateStatusLabel(textKey, isCentered) {
  375. let labelSelector = $('#recordingLabel');
  376. moveToCorner(labelSelector, !isCentered);
  377. labelSelector.attr("data-i18n", textKey);
  378. labelSelector.text(APP.translation.translateString(textKey));
  379. }
  380. };
  381. export default Recording;