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

AudioTrack.js 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. // @flow
  2. import React, { Component } from 'react';
  3. import { createAudioPlayErrorEvent, createAudioPlaySuccessEvent, sendAnalytics } from '../../../../analytics';
  4. import { connect } from '../../../redux';
  5. import logger from '../../logger';
  6. /**
  7. * The type of the React {@code Component} props of {@link AudioTrack}.
  8. */
  9. type Props = {
  10. /**
  11. * Represents muted property of the underlying audio element.
  12. */
  13. _muted: ?Boolean,
  14. /**
  15. * Represents volume property of the underlying audio element.
  16. */
  17. _volume: ?number,
  18. /**
  19. * The value of the id attribute of the audio element.
  20. */
  21. id: string,
  22. /**
  23. * The audio track.
  24. */
  25. audioTrack: ?Object,
  26. /**
  27. * Used to determine the value of the autoplay attribute of the underlying
  28. * audio element.
  29. */
  30. autoPlay: boolean,
  31. /**
  32. * The ID of the participant associated with the audio element.
  33. */
  34. participantId: string
  35. };
  36. /**
  37. * The React/Web {@link Component} which is similar to and wraps around {@code HTMLAudioElement}.
  38. */
  39. class AudioTrack extends Component<Props> {
  40. /**
  41. * Reference to the HTML audio element, stored until the file is ready.
  42. */
  43. _ref: ?HTMLAudioElement;
  44. /**
  45. * The current timeout ID for play() retries.
  46. */
  47. _playTimeout: ?TimeoutID;
  48. /**
  49. * Default values for {@code AudioTrack} component's properties.
  50. *
  51. * @static
  52. */
  53. static defaultProps = {
  54. autoPlay: true,
  55. id: ''
  56. };
  57. /**
  58. * Creates new <code>Audio</code> element instance with given props.
  59. *
  60. * @param {Object} props - The read-only properties with which the new
  61. * instance is to be initialized.
  62. */
  63. constructor(props: Props) {
  64. super(props);
  65. // Bind event handlers so they are only bound once for every instance.
  66. this._setRef = this._setRef.bind(this);
  67. this._play = this._play.bind(this);
  68. }
  69. /**
  70. * Attaches the audio track to the audio element and plays it.
  71. *
  72. * @inheritdoc
  73. * @returns {void}
  74. */
  75. componentDidMount() {
  76. this._attachTrack(this.props.audioTrack);
  77. if (this._ref) {
  78. const { _muted, _volume } = this.props;
  79. if (typeof _volume === 'number') {
  80. this._ref.volume = _volume;
  81. }
  82. if (typeof _muted === 'boolean') {
  83. this._ref.muted = _muted;
  84. }
  85. }
  86. }
  87. /**
  88. * Remove any existing associations between the current audio track and the
  89. * component's audio element.
  90. *
  91. * @inheritdoc
  92. * @returns {void}
  93. */
  94. componentWillUnmount() {
  95. this._detachTrack(this.props.audioTrack);
  96. }
  97. /**
  98. * This component's updating is blackboxed from React to prevent re-rendering of the audio
  99. * element, as we set all the properties manually.
  100. *
  101. * @inheritdoc
  102. * @returns {boolean} - False is always returned to blackbox this component
  103. * from React.
  104. */
  105. shouldComponentUpdate(nextProps: Props) {
  106. const currentJitsiTrack = this.props.audioTrack?.jitsiTrack;
  107. const nextJitsiTrack = nextProps.audioTrack?.jitsiTrack;
  108. if (currentJitsiTrack !== nextJitsiTrack) {
  109. this._detachTrack(this.props.audioTrack);
  110. this._attachTrack(nextProps.audioTrack);
  111. }
  112. if (this._ref) {
  113. const currentVolume = this._ref.volume;
  114. const nextVolume = nextProps._volume;
  115. if (typeof nextVolume === 'number' && !isNaN(nextVolume) && currentVolume !== nextVolume) {
  116. this._ref.volume = nextVolume;
  117. }
  118. const currentMuted = this._ref.muted;
  119. const nextMuted = nextProps._muted;
  120. if (typeof nextMuted === 'boolean' && currentMuted !== nextVolume) {
  121. this._ref.muted = nextMuted;
  122. }
  123. }
  124. return false;
  125. }
  126. /**
  127. * Implements React's {@link Component#render()}.
  128. *
  129. * @inheritdoc
  130. * @returns {ReactElement}
  131. */
  132. render() {
  133. const { autoPlay, id } = this.props;
  134. return (
  135. <audio
  136. autoPlay = { autoPlay }
  137. id = { id }
  138. ref = { this._setRef } />
  139. );
  140. }
  141. /**
  142. * Calls into the passed in track to associate the track with the component's audio element.
  143. *
  144. * @param {Object} track - The redux representation of the {@code JitsiLocalTrack}.
  145. * @private
  146. * @returns {void}
  147. */
  148. _attachTrack(track) {
  149. if (!track || !track.jitsiTrack) {
  150. return;
  151. }
  152. track.jitsiTrack.attach(this._ref);
  153. this._play();
  154. }
  155. /**
  156. * Removes the association to the component's audio element from the passed
  157. * in redux representation of jitsi audio track.
  158. *
  159. * @param {Object} track - The redux representation of the {@code JitsiLocalTrack}.
  160. * @private
  161. * @returns {void}
  162. */
  163. _detachTrack(track) {
  164. if (this._ref && track && track.jitsiTrack) {
  165. clearTimeout(this._playTimeout);
  166. this._playTimeout = undefined;
  167. track.jitsiTrack.detach(this._ref);
  168. }
  169. }
  170. _play: ?number => void;
  171. /**
  172. * Plays the uderlying HTMLAudioElement.
  173. *
  174. * @param {number} retries - The number of previously failed retries.
  175. * @returns {void}
  176. */
  177. _play(retries = 0) {
  178. if (!this._ref) {
  179. // nothing to play.
  180. return;
  181. }
  182. const { autoPlay, id } = this.props;
  183. if (autoPlay) {
  184. // Ensure the audio gets play() called on it. This may be necessary in the
  185. // case where the local video container was moved and re-attached, in which
  186. // case the audio may not autoplay.
  187. this._ref.play()
  188. .then(() => {
  189. if (retries !== 0) {
  190. // success after some failures
  191. this._playTimeout = undefined;
  192. sendAnalytics(createAudioPlaySuccessEvent(id));
  193. logger.info(`Successfully played audio track! retries: ${retries}`);
  194. }
  195. }, e => {
  196. logger.error(`Failed to play audio track! retry: ${retries} ; Error: ${e}`);
  197. if (retries < 3) {
  198. this._playTimeout = setTimeout(() => this._play(retries + 1), 1000);
  199. if (retries === 0) {
  200. // send only 1 error event.
  201. sendAnalytics(createAudioPlayErrorEvent(id));
  202. }
  203. } else {
  204. this._playTimeout = undefined;
  205. }
  206. });
  207. }
  208. }
  209. _setRef: (?HTMLAudioElement) => void;
  210. /**
  211. * Sets the reference to the HTML audio element.
  212. *
  213. * @param {HTMLAudioElement} audioElement - The HTML audio element instance.
  214. * @private
  215. * @returns {void}
  216. */
  217. _setRef(audioElement: ?HTMLAudioElement) {
  218. this._ref = audioElement;
  219. }
  220. }
  221. /**
  222. * Maps (parts of) the Redux state to the associated {@code AudioTrack}'s props.
  223. *
  224. * @param {Object} state - The Redux state.
  225. * @param {Object} ownProps - The props passed to the component.
  226. * @private
  227. * @returns {Props}
  228. */
  229. function _mapStateToProps(state, ownProps) {
  230. const { participantsVolume } = state['features/filmstrip'];
  231. return {
  232. _muted: state['features/base/config'].startSilent,
  233. _volume: participantsVolume[ownProps.participantId]
  234. };
  235. }
  236. export default connect(_mapStateToProps)(AudioTrack);