您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

StartLiveStreamDialog.web.js 14KB

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