Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

StartLiveStreamDialog.web.js 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. // @flow
  2. import Spinner from '@atlaskit/spinner';
  3. import React, { Component } from 'react';
  4. import { connect } from 'react-redux';
  5. import {
  6. createRecordingDialogEvent,
  7. sendAnalytics
  8. } from '../../../analytics';
  9. import { Dialog } from '../../../base/dialog';
  10. import { translate } from '../../../base/i18n';
  11. import { JitsiRecordingConstants } from '../../../base/lib-jitsi-meet';
  12. import googleApi from '../../googleApi';
  13. import BroadcastsDropdown from './BroadcastsDropdown';
  14. import GoogleSignInButton from './GoogleSignInButton';
  15. import StreamKeyForm from './StreamKeyForm';
  16. declare var interfaceConfig: Object;
  17. /**
  18. * An enumeration of the different states the Google API can be in while
  19. * interacting with {@code StartLiveStreamDialog}.
  20. *
  21. * @private
  22. * @type {Object}
  23. */
  24. const GOOGLE_API_STATES = {
  25. /**
  26. * The state in which the Google API still needs to be loaded.
  27. */
  28. NEEDS_LOADING: 0,
  29. /**
  30. * The state in which the Google API is loaded and ready for use.
  31. */
  32. LOADED: 1,
  33. /**
  34. * The state in which a user has been logged in through the Google API.
  35. */
  36. SIGNED_IN: 2,
  37. /**
  38. * The state in which the Google API encountered an error either loading
  39. * or with an API request.
  40. */
  41. ERROR: 3
  42. };
  43. /**
  44. * The type of the React {@code Component} props of
  45. * {@link StartLiveStreamDialog}.
  46. */
  47. type Props = {
  48. /**
  49. * The {@code JitsiConference} for the current conference.
  50. */
  51. _conference: Object,
  52. /**
  53. * The ID for the Google web client application used for making stream key
  54. * related requests.
  55. */
  56. _googleApiApplicationClientID: string,
  57. /**
  58. * Invoked to obtain translated strings.
  59. */
  60. t: Function
  61. };
  62. /**
  63. * The type of the React {@code Component} state of
  64. * {@link StartLiveStreamDialog}.
  65. */
  66. type State = {
  67. /**
  68. * Details about the broadcasts available for use for the logged in Google
  69. * user's YouTube account.
  70. */
  71. broadcasts: ?Array<Object>,
  72. /**
  73. * The current state of interactions with the Google API. Determines what
  74. * Google related UI should display.
  75. */
  76. googleAPIState: number,
  77. /**
  78. * The email of the user currently logged in to the Google web client
  79. * application.
  80. */
  81. googleProfileEmail: string,
  82. /**
  83. * The boundStreamID of the broadcast currently selected in the broadcast
  84. * dropdown.
  85. */
  86. selectedBoundStreamID: ?string,
  87. /**
  88. * The selected or entered stream key to use for YouTube live streaming.
  89. */
  90. streamKey: string
  91. };
  92. /**
  93. * A React Component for requesting a YouTube stream key to use for live
  94. * streaming of the current conference.
  95. *
  96. * @extends Component
  97. */
  98. class StartLiveStreamDialog extends Component<Props, State> {
  99. _isMounted: boolean;
  100. /**
  101. * Initializes a new {@code StartLiveStreamDialog} instance.
  102. *
  103. * @param {Props} props - The React {@code Component} props to initialize
  104. * the new {@code StartLiveStreamDialog} instance with.
  105. */
  106. constructor(props: Props) {
  107. super(props);
  108. this.state = {
  109. broadcasts: undefined,
  110. googleAPIState: GOOGLE_API_STATES.NEEDS_LOADING,
  111. googleProfileEmail: '',
  112. selectedBoundStreamID: undefined,
  113. streamKey: ''
  114. };
  115. /**
  116. * Instance variable used to flag whether the component is or is not
  117. * mounted. Used as a hack to avoid setting state on an unmounted
  118. * component.
  119. *
  120. * @private
  121. * @type {boolean}
  122. */
  123. this._isMounted = false;
  124. // Bind event handlers so they are only bound once per instance.
  125. this._onCancel = this._onCancel.bind(this);
  126. this._onGetYouTubeBroadcasts = this._onGetYouTubeBroadcasts.bind(this);
  127. this._onInitializeGoogleApi = this._onInitializeGoogleApi.bind(this);
  128. this._onRequestGoogleSignIn = this._onRequestGoogleSignIn.bind(this);
  129. this._onStreamKeyChange = this._onStreamKeyChange.bind(this);
  130. this._onSubmit = this._onSubmit.bind(this);
  131. this._onYouTubeBroadcastIDSelected
  132. = this._onYouTubeBroadcastIDSelected.bind(this);
  133. }
  134. /**
  135. * Implements {@link Component#componentDidMount()}. Invoked immediately
  136. * after this component is mounted.
  137. *
  138. * @inheritdoc
  139. * @returns {void}
  140. */
  141. componentDidMount() {
  142. this._isMounted = true;
  143. if (this.props._googleApiApplicationClientID) {
  144. this._onInitializeGoogleApi();
  145. }
  146. }
  147. /**
  148. * Implements React's {@link Component#componentWillUnmount()}. Invoked
  149. * immediately before this component is unmounted and destroyed.
  150. *
  151. * @inheritdoc
  152. */
  153. componentWillUnmount() {
  154. this._isMounted = false;
  155. }
  156. /**
  157. * Implements React's {@link Component#render()}.
  158. *
  159. * @inheritdoc
  160. * @returns {ReactElement}
  161. */
  162. render() {
  163. const { _googleApiApplicationClientID } = this.props;
  164. return (
  165. <Dialog
  166. cancelTitleKey = 'dialog.Cancel'
  167. okTitleKey = 'dialog.startLiveStreaming'
  168. onCancel = { this._onCancel }
  169. onSubmit = { this._onSubmit }
  170. titleKey = 'liveStreaming.start'
  171. width = { 'small' }>
  172. <div className = 'live-stream-dialog'>
  173. { _googleApiApplicationClientID
  174. ? this._renderYouTubePanel() : null }
  175. <StreamKeyForm
  176. helpURL = { interfaceConfig.LIVE_STREAMING_HELP_LINK }
  177. onChange = { this._onStreamKeyChange }
  178. value = { this.state.streamKey } />
  179. </div>
  180. </Dialog>
  181. );
  182. }
  183. _onInitializeGoogleApi: () => Object;
  184. /**
  185. * Loads the Google web client application used for fetching stream keys.
  186. * If the user is already logged in, then a request for available YouTube
  187. * broadcasts is also made.
  188. *
  189. * @private
  190. * @returns {Promise}
  191. */
  192. _onInitializeGoogleApi() {
  193. return googleApi.get()
  194. .then(() => googleApi.initializeClient(
  195. this.props._googleApiApplicationClientID))
  196. .then(() => this._setStateIfMounted({
  197. googleAPIState: GOOGLE_API_STATES.LOADED
  198. }))
  199. .then(() => googleApi.isSignedIn())
  200. .then(isSignedIn => {
  201. if (isSignedIn) {
  202. return this._onGetYouTubeBroadcasts();
  203. }
  204. })
  205. .catch(() => {
  206. this._setStateIfMounted({
  207. googleAPIState: GOOGLE_API_STATES.ERROR
  208. });
  209. });
  210. }
  211. _onCancel: () => boolean;
  212. /**
  213. * Invokes the passed in {@link onCancel} callback and closes
  214. * {@code StartLiveStreamDialog}.
  215. *
  216. * @private
  217. * @returns {boolean} True is returned to close the modal.
  218. */
  219. _onCancel() {
  220. sendAnalytics(createRecordingDialogEvent('start', 'cancel.button'));
  221. return true;
  222. }
  223. _onGetYouTubeBroadcasts: () => Object;
  224. /**
  225. * Asks the user to sign in, if not already signed in, and then requests a
  226. * list of the user's YouTube broadcasts.
  227. *
  228. * @private
  229. * @returns {Promise}
  230. */
  231. _onGetYouTubeBroadcasts() {
  232. return googleApi.get()
  233. .then(() => googleApi.signInIfNotSignedIn())
  234. .then(() => googleApi.getCurrentUserProfile())
  235. .then(profile => {
  236. this._setStateIfMounted({
  237. googleProfileEmail: profile.getEmail(),
  238. googleAPIState: GOOGLE_API_STATES.SIGNED_IN
  239. });
  240. })
  241. .then(() => googleApi.requestAvailableYouTubeBroadcasts())
  242. .then(response => {
  243. const broadcasts = this._parseBroadcasts(response.result.items);
  244. this._setStateIfMounted({
  245. broadcasts
  246. });
  247. if (broadcasts.length === 1 && !this.state.streamKey) {
  248. const broadcast = broadcasts[0];
  249. this._onYouTubeBroadcastIDSelected(broadcast.boundStreamID);
  250. }
  251. })
  252. .catch(response => {
  253. // Only show an error if an external request was made with the
  254. // Google api. Do not error if the login in canceled.
  255. if (response && response.result) {
  256. this._setStateIfMounted({
  257. googleAPIState: GOOGLE_API_STATES.ERROR
  258. });
  259. }
  260. });
  261. }
  262. _onRequestGoogleSignIn: () => Object;
  263. /**
  264. * Forces the Google web client application to prompt for a sign in, such as
  265. * when changing account, and will then fetch available YouTube broadcasts.
  266. *
  267. * @private
  268. * @returns {Promise}
  269. */
  270. _onRequestGoogleSignIn() {
  271. return googleApi.showAccountSelection()
  272. .then(() => this._setStateIfMounted({ broadcasts: undefined }))
  273. .then(() => this._onGetYouTubeBroadcasts());
  274. }
  275. _onStreamKeyChange: () => void;
  276. /**
  277. * Callback invoked to update the {@code StartLiveStreamDialog} component's
  278. * display of the entered YouTube stream key.
  279. *
  280. * @param {Object} event - DOM Event for value change.
  281. * @private
  282. * @returns {void}
  283. */
  284. _onStreamKeyChange(event) {
  285. this._setStateIfMounted({
  286. streamKey: event.target.value,
  287. selectedBoundStreamID: undefined
  288. });
  289. }
  290. _onSubmit: () => boolean;
  291. /**
  292. * Invokes the passed in {@link onSubmit} callback with the entered stream
  293. * key, and then closes {@code StartLiveStreamDialog}.
  294. *
  295. * @private
  296. * @returns {boolean} False if no stream key is entered to preventing
  297. * closing, true to close the modal.
  298. */
  299. _onSubmit() {
  300. const { broadcasts, streamKey, selectedBoundStreamID } = this.state;
  301. if (!streamKey) {
  302. return false;
  303. }
  304. let selectedBroadcastID = null;
  305. if (selectedBoundStreamID) {
  306. const selectedBroadcast = broadcasts && broadcasts.find(
  307. broadcast => broadcast.boundStreamID === selectedBoundStreamID);
  308. selectedBroadcastID = selectedBroadcast && selectedBroadcast.id;
  309. }
  310. sendAnalytics(createRecordingDialogEvent('start', 'confirm.button'));
  311. this.props._conference.startRecording({
  312. broadcastId: selectedBroadcastID,
  313. mode: JitsiRecordingConstants.mode.STREAM,
  314. streamId: streamKey
  315. });
  316. return true;
  317. }
  318. _onYouTubeBroadcastIDSelected: (string) => Object;
  319. /**
  320. * Fetches the stream key for a YouTube broadcast and updates the internal
  321. * state to display the associated stream key as being entered.
  322. *
  323. * @param {string} boundStreamID - The bound stream ID associated with the
  324. * broadcast from which to get the stream key.
  325. * @private
  326. * @returns {Promise}
  327. */
  328. _onYouTubeBroadcastIDSelected(boundStreamID) {
  329. return googleApi.requestLiveStreamsForYouTubeBroadcast(boundStreamID)
  330. .then(response => {
  331. const broadcasts = response.result.items;
  332. const streamName = broadcasts
  333. && broadcasts[0]
  334. && broadcasts[0].cdn.ingestionInfo.streamName;
  335. const streamKey = streamName || '';
  336. this._setStateIfMounted({
  337. streamKey,
  338. selectedBoundStreamID: boundStreamID
  339. });
  340. });
  341. }
  342. _parseBroadcasts: (Array<Object>) => Array<Object>;
  343. /**
  344. * Takes in a list of broadcasts from the YouTube API, removes dupes,
  345. * removes broadcasts that cannot get a stream key, and parses the
  346. * broadcasts into flat objects.
  347. *
  348. * @param {Array} broadcasts - Broadcast descriptions as obtained from
  349. * calling the YouTube API.
  350. * @private
  351. * @returns {Array} An array of objects describing each unique broadcast.
  352. */
  353. _parseBroadcasts(broadcasts) {
  354. const parsedBroadcasts = {};
  355. for (let i = 0; i < broadcasts.length; i++) {
  356. const broadcast = broadcasts[i];
  357. const boundStreamID = broadcast.contentDetails.boundStreamId;
  358. if (boundStreamID && !parsedBroadcasts[boundStreamID]) {
  359. parsedBroadcasts[boundStreamID] = {
  360. boundStreamID,
  361. id: broadcast.id,
  362. status: broadcast.status.lifeCycleStatus,
  363. title: broadcast.snippet.title
  364. };
  365. }
  366. }
  367. return Object.values(parsedBroadcasts);
  368. }
  369. /**
  370. * Renders a React Element for authenticating with the Google web client.
  371. *
  372. * @private
  373. * @returns {ReactElement}
  374. */
  375. _renderYouTubePanel() {
  376. const { t } = this.props;
  377. const {
  378. broadcasts,
  379. googleProfileEmail,
  380. selectedBoundStreamID
  381. } = this.state;
  382. let googleContent, helpText;
  383. switch (this.state.googleAPIState) {
  384. case GOOGLE_API_STATES.LOADED:
  385. googleContent = ( // eslint-disable-line no-extra-parens
  386. <GoogleSignInButton
  387. onClick = { this._onGetYouTubeBroadcasts }
  388. text = { t('liveStreaming.signIn') } />
  389. );
  390. helpText = t('liveStreaming.signInCTA');
  391. break;
  392. case GOOGLE_API_STATES.SIGNED_IN:
  393. googleContent = ( // eslint-disable-line no-extra-parens
  394. <BroadcastsDropdown
  395. broadcasts = { broadcasts }
  396. onBroadcastSelected = { this._onYouTubeBroadcastIDSelected }
  397. selectedBoundStreamID = { selectedBoundStreamID } />
  398. );
  399. /**
  400. * FIXME: Ideally this help text would be one translation string
  401. * that also accepts the anchor. This can be done using the Trans
  402. * component of react-i18next but I couldn't get it working...
  403. */
  404. helpText = ( // eslint-disable-line no-extra-parens
  405. <div>
  406. { `${t('liveStreaming.chooseCTA',
  407. { email: googleProfileEmail })} ` }
  408. <a onClick = { this._onRequestGoogleSignIn }>
  409. { t('liveStreaming.changeSignIn') }
  410. </a>
  411. </div>
  412. );
  413. break;
  414. case GOOGLE_API_STATES.ERROR:
  415. googleContent = ( // eslint-disable-line no-extra-parens
  416. <GoogleSignInButton
  417. onClick = { this._onRequestGoogleSignIn }
  418. text = { t('liveStreaming.signIn') } />
  419. );
  420. helpText = t('liveStreaming.errorAPI');
  421. break;
  422. case GOOGLE_API_STATES.NEEDS_LOADING:
  423. default:
  424. googleContent = ( // eslint-disable-line no-extra-parens
  425. <Spinner
  426. isCompleting = { false }
  427. size = 'medium' />
  428. );
  429. break;
  430. }
  431. return (
  432. <div className = 'google-panel'>
  433. <div className = 'live-stream-cta'>
  434. { helpText }
  435. </div>
  436. <div className = 'google-api'>
  437. { googleContent }
  438. </div>
  439. </div>
  440. );
  441. }
  442. /**
  443. * Updates the internal state if the component is still mounted. This is a
  444. * workaround for all the state setting that occurs after ajax.
  445. *
  446. * @param {Object} newState - The new state to merge into the existing
  447. * state.
  448. * @private
  449. * @returns {void}
  450. */
  451. _setStateIfMounted(newState) {
  452. if (this._isMounted) {
  453. this.setState(newState);
  454. }
  455. }
  456. }
  457. /**
  458. * Maps (parts of) the redux state to the React {@code Component} props of
  459. * {@code StartLiveStreamDialog}.
  460. *
  461. * @param {Object} state - The redux state.
  462. * @private
  463. * @returns {{
  464. * _conference: Object,
  465. * _googleApiApplicationClientID: string
  466. * }}
  467. */
  468. function _mapStateToProps(state) {
  469. return {
  470. _conference: state['features/base/conference'].conference,
  471. _googleApiApplicationClientID:
  472. state['features/base/config'].googleApiApplicationClientID
  473. };
  474. }
  475. export default translate(connect(_mapStateToProps)(StartLiveStreamDialog));