media_codec_audio_decoder.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. // Copyright 2016 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef MEDIA_FILTERS_ANDROID_MEDIA_CODEC_AUDIO_DECODER_H_
  5. #define MEDIA_FILTERS_ANDROID_MEDIA_CODEC_AUDIO_DECODER_H_
  6. #include <memory>
  7. #include <utility>
  8. #include <vector>
  9. #include "base/containers/circular_deque.h"
  10. #include "base/memory/raw_ptr.h"
  11. #include "base/memory/weak_ptr.h"
  12. #include "base/timer/timer.h"
  13. #include "media/base/android/media_codec_loop.h"
  14. #include "media/base/android/media_crypto_context.h"
  15. #include "media/base/audio_buffer.h"
  16. #include "media/base/audio_decoder.h"
  17. #include "media/base/audio_decoder_config.h"
  18. #include "media/base/callback_registry.h"
  19. #include "media/base/cdm_context.h"
  20. #include "media/base/media_export.h"
  21. // MediaCodecAudioDecoder is based on Android's MediaCodec API.
  22. // The MediaCodec API is required to play encrypted (as in EME) content on
  23. // Android. It is also a way to employ hardware-accelerated decoding.
  24. // Implementation notes.
  25. //
  26. // This class provides audio decoding via MediaCodec. It allocates the
  27. // MediaCodecBridge instance, and hands ownership to MediaCodecLoop to drive I/O
  28. // with the codec. For encrypted streams, we also talk to the DRM bridge.
  29. //
  30. // Because both dequeuing and enqueuing of an input buffer can fail, the
  31. // implementation puts the input |DecoderBuffer|s and the corresponding decode
  32. // callbacks into an input queue. The decoder has a timer that periodically
  33. // fires the decoding cycle that has two steps. The first step tries to send the
  34. // front buffer from the input queue to MediaCodecLoop. In the case of success
  35. // the element is removed from the queue, the decode callback is fired and the
  36. // decoding process advances. The second step tries to dequeue an output buffer,
  37. // and uses it in the case of success.
  38. //
  39. // An EOS buffer is handled differently. Success is not signalled to the decode
  40. // callback until the EOS is received at the output. So, for EOS, the decode
  41. // callback indicates that all previous decodes have completed.
  42. //
  43. // The failures in both steps are normal and they happen periodically since
  44. // both input and output buffers become available at unpredictable moments. The
  45. // timer is here to repeat the dequeueing attempts.
  46. //
  47. // State diagram.
  48. //
  49. // [Uninitialized] <-> (init failed)
  50. // | |
  51. // (no enc.) (encrypted)
  52. // | |
  53. // | [WaitingForMediaCrypto] -- (OMCR failure) --> [Uninitialized]
  54. // | | (OnMediaCryptoReady success)
  55. // v v
  56. // (Create Codec and MediaCodecLoop)
  57. // |
  58. // \--> [Ready] -(any error)-> [Error]
  59. //
  60. // -[Any state]-
  61. // | |
  62. // (Reset ok) (Reset fails)
  63. // | |
  64. // [Ready] [Error]
  65. namespace base {
  66. class SingleThreadTaskRunner;
  67. }
  68. namespace media {
  69. class AudioTimestampHelper;
  70. class MEDIA_EXPORT MediaCodecAudioDecoder : public AudioDecoder,
  71. public MediaCodecLoop::Client {
  72. public:
  73. explicit MediaCodecAudioDecoder(
  74. scoped_refptr<base::SingleThreadTaskRunner> task_runner);
  75. MediaCodecAudioDecoder(const MediaCodecAudioDecoder&) = delete;
  76. MediaCodecAudioDecoder& operator=(const MediaCodecAudioDecoder&) = delete;
  77. ~MediaCodecAudioDecoder() override;
  78. // AudioDecoder implementation.
  79. AudioDecoderType GetDecoderType() const override;
  80. void Initialize(const AudioDecoderConfig& config,
  81. CdmContext* cdm_context,
  82. InitCB init_cb,
  83. const OutputCB& output_cb,
  84. const WaitingCB& waiting_cb) override;
  85. void Decode(scoped_refptr<DecoderBuffer> buffer, DecodeCB decode_cb) override;
  86. void Reset(base::OnceClosure closure) override;
  87. bool NeedsBitstreamConversion() const override;
  88. // MediaCodecLoop::Client implementation
  89. bool IsAnyInputPending() const override;
  90. MediaCodecLoop::InputData ProvideInputData() override;
  91. void OnInputDataQueued(bool) override;
  92. bool OnDecodedEos(const MediaCodecLoop::OutputBuffer& out) override;
  93. bool OnDecodedFrame(const MediaCodecLoop::OutputBuffer& out) override;
  94. void OnWaiting(WaitingReason reason) override;
  95. bool OnOutputFormatChanged() override;
  96. void OnCodecLoopError() override;
  97. private:
  98. // Possible states.
  99. enum State {
  100. // A successful call to Initialize() is required.
  101. STATE_UNINITIALIZED,
  102. // Codec is initialized, but a call to SetCdm() is required.
  103. STATE_WAITING_FOR_MEDIA_CRYPTO,
  104. // Normal state. May decode, etc.
  105. STATE_READY,
  106. // Codec must be reset.
  107. STATE_ERROR,
  108. };
  109. // Allocate a new MediaCodec and MediaCodecLoop, and replace |codec_loop_|.
  110. // Returns true on success.
  111. bool CreateMediaCodecLoop();
  112. // A helper method to start CDM initialization. This must be called if and
  113. // only if we were constructed with |is_encrypted| set to true.
  114. void SetCdm(CdmContext* cdm_context, InitCB init_cb);
  115. // This callback is called after CDM obtained a MediaCrypto object.
  116. void OnMediaCryptoReady(InitCB init_cb,
  117. JavaObjectPtr media_crypto,
  118. bool requires_secure_video_codec);
  119. // Callback for the CDM to notify |this|.
  120. void OnCdmContextEvent(CdmContext::Event event);
  121. // Calls DecodeCB with |decode_status| for every frame in |input_queue| and
  122. // then clears it.
  123. void ClearInputQueue(DecoderStatus decode_status);
  124. // Helper method to change the state.
  125. void SetState(State new_state);
  126. // Helper method to set sample rate, channel count and |timestamp_helper_|
  127. // from |config_|.
  128. void SetInitialConfiguration();
  129. void PumpMediaCodecLoop();
  130. // TODO(timav): refactor the common part out and use it here and in AVDA
  131. // (http://crbug.com/583082).
  132. // A helper function for logging.
  133. static const char* AsString(State state);
  134. // Used to post tasks. This class is single threaded and every method should
  135. // run on this task runner.
  136. scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
  137. State state_;
  138. // The queue of encoded (and maybe encrypted) buffers. The MediaCodec might
  139. // not be able to accept the input at the time of Decode(), thus all
  140. // DecoderBuffers first go to |input_queue_|.
  141. using BufferCBPair = std::pair<scoped_refptr<DecoderBuffer>, DecodeCB>;
  142. using InputQueue = base::circular_deque<BufferCBPair>;
  143. InputQueue input_queue_;
  144. // Cached decoder config.
  145. AudioDecoderConfig config_;
  146. // Indication to use passthrough decoder or not.
  147. bool is_passthrough_;
  148. // The audio sample format of the audio decoder output.
  149. SampleFormat sample_format_;
  150. // Actual channel count that comes from decoder may be different than config.
  151. int channel_count_;
  152. ChannelLayout channel_layout_;
  153. // Actual sample rate that comes from the decoder, may be different than
  154. // config.
  155. int sample_rate_;
  156. // Callback that delivers output frames.
  157. OutputCB output_cb_;
  158. WaitingCB waiting_cb_;
  159. std::unique_ptr<MediaCodecLoop> codec_loop_;
  160. std::unique_ptr<AudioTimestampHelper> timestamp_helper_;
  161. // CDM related stuff.
  162. // Owned by CDM which is external to this decoder.
  163. raw_ptr<MediaCryptoContext> media_crypto_context_;
  164. // To keep the CdmContext event callback registered.
  165. std::unique_ptr<CallbackRegistration> event_cb_registration_;
  166. // Pool which helps avoid thrashing memory when returning audio buffers.
  167. scoped_refptr<AudioBufferMemoryPool> pool_;
  168. // The MediaCrypto object is used in the MediaCodec.configure() in case of
  169. // an encrypted stream.
  170. JavaObjectPtr media_crypto_;
  171. base::WeakPtrFactory<MediaCodecAudioDecoder> weak_factory_{this};
  172. };
  173. } // namespace media
  174. #endif // MEDIA_FILTERS_ANDROID_MEDIA_CODEC_AUDIO_DECODER_H_