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.

AudioTrack.js 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. // @flow
  2. import React, { Component } from 'react';
  3. import { connect } from 'react-redux';
  4. import { createAudioPlayErrorEvent, createAudioPlaySuccessEvent, sendAnalytics } from '../../../../analytics';
  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._errorHandler = this._errorHandler.bind(this);
  67. this._setRef = this._setRef.bind(this);
  68. this._play = this._play.bind(this);
  69. }
  70. /**
  71. * Attaches the audio track to the audio element and plays it.
  72. *
  73. * @inheritdoc
  74. * @returns {void}
  75. */
  76. componentDidMount() {
  77. this._attachTrack(this.props.audioTrack);
  78. if (this._ref) {
  79. const { _muted, _volume } = this.props;
  80. if (typeof _volume === 'number') {
  81. this._ref.volume = _volume;
  82. }
  83. if (typeof _muted === 'boolean') {
  84. this._ref.muted = _muted;
  85. }
  86. this._ref.addEventListener('error', this._errorHandler);
  87. }
  88. }
  89. /**
  90. * Remove any existing associations between the current audio track and the
  91. * component's audio element.
  92. *
  93. * @inheritdoc
  94. * @returns {void}
  95. */
  96. componentWillUnmount() {
  97. this._detachTrack(this.props.audioTrack);
  98. this._ref.removeEventListener('error', this._errorHandler);
  99. }
  100. /**
  101. * This component's updating is blackboxed from React to prevent re-rendering of the audio
  102. * element, as we set all the properties manually.
  103. *
  104. * @inheritdoc
  105. * @returns {boolean} - False is always returned to blackbox this component
  106. * from React.
  107. */
  108. shouldComponentUpdate(nextProps: Props) {
  109. const currentJitsiTrack = this.props.audioTrack?.jitsiTrack;
  110. const nextJitsiTrack = nextProps.audioTrack?.jitsiTrack;
  111. if (currentJitsiTrack !== nextJitsiTrack) {
  112. this._detachTrack(this.props.audioTrack);
  113. this._attachTrack(nextProps.audioTrack);
  114. }
  115. if (this._ref) {
  116. const currentVolume = this._ref.volume;
  117. const nextVolume = nextProps._volume;
  118. if (typeof nextVolume === 'number' && !isNaN(nextVolume) && currentVolume !== nextVolume) {
  119. this._ref.volume = nextVolume;
  120. }
  121. const currentMuted = this._ref.muted;
  122. const nextMuted = nextProps._muted;
  123. if (typeof nextMuted === 'boolean' && currentMuted !== nextVolume) {
  124. this._ref.muted = nextMuted;
  125. }
  126. }
  127. return false;
  128. }
  129. /**
  130. * Implements React's {@link Component#render()}.
  131. *
  132. * @inheritdoc
  133. * @returns {ReactElement}
  134. */
  135. render() {
  136. const { autoPlay, id } = this.props;
  137. return (
  138. <audio
  139. autoPlay = { autoPlay }
  140. id = { id }
  141. ref = { this._setRef } />
  142. );
  143. }
  144. /**
  145. * Calls into the passed in track to associate the track with the component's audio element.
  146. *
  147. * @param {Object} track - The redux representation of the {@code JitsiLocalTrack}.
  148. * @private
  149. * @returns {void}
  150. */
  151. _attachTrack(track) {
  152. if (!track || !track.jitsiTrack) {
  153. return;
  154. }
  155. track.jitsiTrack.attach(this._ref);
  156. this._play();
  157. }
  158. /**
  159. * Removes the association to the component's audio element from the passed
  160. * in redux representation of jitsi audio track.
  161. *
  162. * @param {Object} track - The redux representation of the {@code JitsiLocalTrack}.
  163. * @private
  164. * @returns {void}
  165. */
  166. _detachTrack(track) {
  167. if (this._ref && track && track.jitsiTrack) {
  168. clearTimeout(this._playTimeout);
  169. this._playTimeout = undefined;
  170. track.jitsiTrack.detach(this._ref);
  171. }
  172. }
  173. _errorHandler: (?Error) => void;
  174. /**
  175. * Reattaches the audio track to the underlying HTMLAudioElement when an 'error' event is fired.
  176. *
  177. * @param {Error} error - The error event fired on the HTMLAudioElement.
  178. * @returns {void}
  179. */
  180. _errorHandler(error) {
  181. logger.error(`Error ${error?.message} called on audio track ${this.props.audioTrack?.jitsiTrack}. `
  182. + 'Attempting to reattach the audio track to the element and execute play on it');
  183. this._detachTrack(this.props.audioTrack);
  184. this._attachTrack(this.props.audioTrack);
  185. }
  186. _play: ?number => void;
  187. /**
  188. * Plays the underlying HTMLAudioElement.
  189. *
  190. * @param {number} retries - The number of previously failed retries.
  191. * @returns {void}
  192. */
  193. _play(retries = 0) {
  194. if (!this._ref) {
  195. // nothing to play.
  196. return;
  197. }
  198. const { autoPlay, id } = this.props;
  199. if (autoPlay) {
  200. // Ensure the audio gets play() called on it. This may be necessary in the
  201. // case where the local video container was moved and re-attached, in which
  202. // case the audio may not autoplay.
  203. this._ref.play()
  204. .then(() => {
  205. if (retries !== 0) {
  206. // success after some failures
  207. this._playTimeout = undefined;
  208. sendAnalytics(createAudioPlaySuccessEvent(id));
  209. logger.info(`Successfully played audio track! retries: ${retries}`);
  210. }
  211. }, e => {
  212. logger.error(`Failed to play audio track! retry: ${retries} ; Error: ${e}`);
  213. if (retries < 3) {
  214. this._playTimeout = setTimeout(() => this._play(retries + 1), 1000);
  215. if (retries === 0) {
  216. // send only 1 error event.
  217. sendAnalytics(createAudioPlayErrorEvent(id));
  218. }
  219. } else {
  220. this._playTimeout = undefined;
  221. }
  222. });
  223. }
  224. }
  225. _setRef: (?HTMLAudioElement) => void;
  226. /**
  227. * Sets the reference to the HTML audio element.
  228. *
  229. * @param {HTMLAudioElement} audioElement - The HTML audio element instance.
  230. * @private
  231. * @returns {void}
  232. */
  233. _setRef(audioElement: ?HTMLAudioElement) {
  234. this._ref = audioElement;
  235. }
  236. }
  237. /**
  238. * Maps (parts of) the Redux state to the associated {@code AudioTrack}'s props.
  239. *
  240. * @param {Object} state - The Redux state.
  241. * @param {Object} ownProps - The props passed to the component.
  242. * @private
  243. * @returns {Props}
  244. */
  245. function _mapStateToProps(state, ownProps) {
  246. const { participantsVolume } = state['features/filmstrip'];
  247. return {
  248. _muted: state['features/base/config'].startSilent,
  249. _volume: participantsVolume[ownProps.participantId]
  250. };
  251. }
  252. export default connect(_mapStateToProps)(AudioTrack);