Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 5 #ifndef MEDIA_FILTERS_ANDROID_AUDIO_DECODER_H_ | |
| 6 #define MEDIA_FILTERS_ANDROID_AUDIO_DECODER_H_ | |
| 7 | |
| 8 #include <deque> | |
| 9 #include "base/macros.h" | |
| 10 #include "base/memory/scoped_ptr.h" | |
| 11 #include "base/memory/weak_ptr.h" | |
| 12 #include "base/time/time.h" | |
| 13 #include "base/timer/timer.h" | |
| 14 #include "media/base/android/media_codec_bridge.h" | |
| 15 #include "media/base/android/media_drm_bridge.h" | |
| 16 #include "media/base/audio_decoder.h" | |
| 17 #include "media/base/audio_decoder_config.h" | |
| 18 #include "media/base/cdm_context.h" | |
| 19 #include "media/base/media_export.h" | |
| 20 #include "media/base/media_keys.h" | |
| 21 | |
| 22 namespace base { | |
| 23 class SingleThreadTaskRunner; | |
| 24 } | |
| 25 | |
| 26 namespace media { | |
| 27 | |
| 28 // AudioDecoder based on Android's MediaCopdec API. | |
| 29 // This decoder is a decrypting decoder: it is able to accept an enctypted | |
| 30 // stream and produce the decrypted and decoded output. | |
| 31 class MEDIA_EXPORT AndroidAudioDecoder : public AudioDecoder { | |
| 32 public: | |
| 33 AndroidAudioDecoder(); | |
| 34 ~AndroidAudioDecoder() override; | |
| 35 | |
| 36 // AudioDecoder implementation. | |
| 37 std::string GetDisplayName() const override; | |
| 38 void Initialize(const AudioDecoderConfig& config, | |
| 39 const SetCdmReadyCB& set_cdm_ready_cb, | |
| 40 const InitCB& init_cb, | |
| 41 const OutputCB& output_cb) override; | |
| 42 void Decode(const scoped_refptr<DecoderBuffer>& buffer, | |
| 43 const DecodeCB& decode_cb) override; | |
| 44 void Reset(const base::Closure& closure) override; | |
| 45 | |
| 46 private: | |
| 47 // Possible states. | |
| 48 enum State { | |
| 49 kStateUninitialized, | |
| 50 kStateWaitingForCDM, | |
| 51 kStateWaitingForCrypto, | |
| 52 kStateReady, | |
| 53 kStateWaitingForKey, | |
| 54 kStateDraining, | |
| 55 kStateDrained, | |
| 56 kStateError, | |
| 57 }; | |
| 58 | |
| 59 // Information about the MediaCodec's output buffer. | |
| 60 struct OutputBufferInfo { | |
| 61 int buf_index; | |
| 62 size_t offset; | |
| 63 size_t size; | |
| 64 base::TimeDelta pts; | |
| 65 bool is_eos; | |
| 66 bool is_key_frame; | |
| 67 }; | |
| 68 | |
| 69 // Sets CDM. This method is attached to |set_cdm_ready_cb| and is called when | |
| 70 // CdmContext becomes ready. The |cdm_attached_cb| should be called when the | |
| 71 // decryptor in the CDM has been completely attached to the pipeline. | |
| 72 void SetCdm(CdmContext* cdm_context, const CdmAttachedCB& cdm_attached_cb); | |
|
xhwang
2016/02/01 19:30:55
As you pointed out. Leaving EME part to a separate
Tima Vaisburd
2016/02/04 22:59:12
Done, but I kept the NO_KEY related stuff that I f
| |
| 73 | |
| 74 // Callback called when MediaCrypto object is ready. | |
| 75 void OnMediaCryptoReady(MediaDrmBridge::JavaObjectPtr media_crypto); | |
| 76 | |
| 77 // Callback called when a new key is available after the codec received | |
| 78 // the status MEDIA_CODEC_NO_KEY. | |
| 79 void OnKeyAdded(); | |
| 80 | |
| 81 // Does the MediaCodec processing cycle: enqueues an input buffer, then | |
| 82 // dequeues output buffers. | |
| 83 void DoIOTask(); | |
| 84 | |
| 85 // Enqueue one pending input buffer into MediaCodec if MediaCodec has a room. | |
| 86 // Returns true if any input was processed. | |
| 87 bool QueueInput(); | |
| 88 | |
| 89 // Dequeue all output buffers from MediaCodec that are immediately available. | |
| 90 bool DequeueOutput(); | |
| 91 | |
| 92 // Start or stop our work-polling timer based on whether we did any work, and | |
| 93 // how long it has been since we've done work. Calling this with true will | |
| 94 // start the timer. Calling it with false may stop the timer. | |
| 95 void ManageTimer(bool did_work); | |
| 96 | |
| 97 // Helper method to change the state. | |
| 98 void SetState(State new_state); | |
| 99 | |
| 100 // Configures |media_codec_| with the given codec parameters from the client. | |
| 101 bool ConfigureMediaCodec(); | |
| 102 | |
| 103 // Processes the output buffer after it comes from MediaCodec. | |
| 104 void OnDecodedFrame(const OutputBufferInfo* out); | |
| 105 | |
| 106 // Processed the output format change. | |
| 107 void OnOutputFormatChanged(); | |
| 108 | |
| 109 // A helper function for logging. | |
| 110 static const char* AsString(State state); | |
| 111 | |
| 112 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | |
| 113 | |
| 114 // The current state of the object. | |
| 115 State state_; | |
| 116 | |
| 117 // The queue of encoded (and maybe encrypted) buffers. | |
| 118 using BufferCBPair = std::pair<scoped_refptr<DecoderBuffer>, DecodeCB>; | |
| 119 using InputQueue = std::deque<BufferCBPair>; | |
| 120 InputQueue input_queue_; | |
| 121 | |
| 122 // Decoder configuration cache. | |
| 123 AudioDecoderConfig config_; | |
| 124 | |
| 125 // Frequently used constants derived from |config_|. | |
| 126 int channel_count_; | |
| 127 int bytes_per_frame_; | |
| 128 | |
| 129 // Callback that reports the end of initialization and its status. | |
| 130 InitCB init_cb_; | |
| 131 | |
| 132 // Callback that delivers output frames. | |
| 133 OutputCB output_cb_; | |
| 134 | |
| 135 // DecodeCB that should be fired after the decoder has been flashed in case | |
| 136 // of EOS. | |
| 137 DecodeCB eos_decode_cb_; | |
| 138 | |
| 139 // The bridge to Android's MediaCodec API. | |
| 140 scoped_ptr<MediaCodecBridge> media_codec_; | |
| 141 | |
| 142 // Repeating timer that kicks MediaCodec operation. | |
| 143 base::RepeatingTimer io_timer_; | |
| 144 | |
| 145 // Time at which we last did useful work on |io_timer_|. | |
| 146 base::TimeTicks most_recent_work_; | |
| 147 | |
| 148 // CDM related stuff. | |
| 149 | |
| 150 // Callback to request/cancel CDM ready notification. | |
| 151 SetCdmReadyCB set_cdm_ready_cb_; | |
| 152 | |
| 153 // Callback to notify that CDM is attached to the pipeine. | |
| 154 CdmAttachedCB cdm_attached_cb_; | |
| 155 | |
| 156 // Holds a ref-count to the CDM. | |
| 157 scoped_refptr<MediaKeys> cdm_; | |
| 158 | |
| 159 // MediaDrmBridge requires registration/unregistration of the player, this | |
| 160 // registration id is used for this. | |
| 161 int cdm_registration_id_; | |
| 162 | |
| 163 // The MediaCrypto object is used in the MediaCodec.configure() in case of | |
| 164 // an encrypted stream. | |
| 165 MediaDrmBridge::JavaObjectPtr media_crypto_; | |
| 166 | |
| 167 // Index of the dequeued and filled buffer that we keep trying to enqueue. | |
| 168 // Such buffer appears in MEDIA_CODEC_NO_KEY processing. | |
| 169 int pending_input_buf_index_; | |
| 170 | |
| 171 // Weak pointer factory must be the last member variable. | |
| 172 base::WeakPtrFactory<AndroidAudioDecoder> weak_factory_; | |
| 173 | |
| 174 DISALLOW_COPY_AND_ASSIGN(AndroidAudioDecoder); | |
| 175 }; | |
| 176 | |
| 177 } // namespace media | |
| 178 | |
| 179 #endif // MEDIA_FILTERS_ANDROID_AUDIO_DECODER_H_ | |
| OLD | NEW |