Chromium Code Reviews| Index: media/filters/android_audio_decoder.h |
| diff --git a/media/filters/android_audio_decoder.h b/media/filters/android_audio_decoder.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..74fedf523458d42ba8bc2c2a87f1b40953d6765e |
| --- /dev/null |
| +++ b/media/filters/android_audio_decoder.h |
| @@ -0,0 +1,179 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef MEDIA_FILTERS_ANDROID_AUDIO_DECODER_H_ |
| +#define MEDIA_FILTERS_ANDROID_AUDIO_DECODER_H_ |
| + |
| +#include <deque> |
| +#include "base/macros.h" |
| +#include "base/memory/scoped_ptr.h" |
| +#include "base/memory/weak_ptr.h" |
| +#include "base/time/time.h" |
| +#include "base/timer/timer.h" |
| +#include "media/base/android/media_codec_bridge.h" |
| +#include "media/base/android/media_drm_bridge.h" |
| +#include "media/base/audio_decoder.h" |
| +#include "media/base/audio_decoder_config.h" |
| +#include "media/base/cdm_context.h" |
| +#include "media/base/media_export.h" |
| +#include "media/base/media_keys.h" |
| + |
| +namespace base { |
| +class SingleThreadTaskRunner; |
| +} |
| + |
| +namespace media { |
| + |
| +// AudioDecoder based on Android's MediaCopdec API. |
| +// This decoder is a decrypting decoder: it is able to accept an enctypted |
| +// stream and produce the decrypted and decoded output. |
| +class MEDIA_EXPORT AndroidAudioDecoder : public AudioDecoder { |
| + public: |
| + AndroidAudioDecoder(); |
| + ~AndroidAudioDecoder() override; |
| + |
| + // AudioDecoder implementation. |
| + std::string GetDisplayName() const override; |
| + void Initialize(const AudioDecoderConfig& config, |
| + const SetCdmReadyCB& set_cdm_ready_cb, |
| + const InitCB& init_cb, |
| + const OutputCB& output_cb) override; |
| + void Decode(const scoped_refptr<DecoderBuffer>& buffer, |
| + const DecodeCB& decode_cb) override; |
| + void Reset(const base::Closure& closure) override; |
| + |
| + private: |
| + // Possible states. |
| + enum State { |
| + kStateUninitialized, |
| + kStateWaitingForCDM, |
| + kStateWaitingForCrypto, |
| + kStateReady, |
| + kStateWaitingForKey, |
| + kStateDraining, |
| + kStateDrained, |
| + kStateError, |
| + }; |
| + |
| + // Information about the MediaCodec's output buffer. |
| + struct OutputBufferInfo { |
| + int buf_index; |
| + size_t offset; |
| + size_t size; |
| + base::TimeDelta pts; |
| + bool is_eos; |
| + bool is_key_frame; |
| + }; |
| + |
| + // Sets CDM. This method is attached to |set_cdm_ready_cb| and is called when |
| + // CdmContext becomes ready. The |cdm_attached_cb| should be called when the |
| + // decryptor in the CDM has been completely attached to the pipeline. |
| + 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
|
| + |
| + // Callback called when MediaCrypto object is ready. |
| + void OnMediaCryptoReady(MediaDrmBridge::JavaObjectPtr media_crypto); |
| + |
| + // Callback called when a new key is available after the codec received |
| + // the status MEDIA_CODEC_NO_KEY. |
| + void OnKeyAdded(); |
| + |
| + // Does the MediaCodec processing cycle: enqueues an input buffer, then |
| + // dequeues output buffers. |
| + void DoIOTask(); |
| + |
| + // Enqueue one pending input buffer into MediaCodec if MediaCodec has a room. |
| + // Returns true if any input was processed. |
| + bool QueueInput(); |
| + |
| + // Dequeue all output buffers from MediaCodec that are immediately available. |
| + bool DequeueOutput(); |
| + |
| + // Start or stop our work-polling timer based on whether we did any work, and |
| + // how long it has been since we've done work. Calling this with true will |
| + // start the timer. Calling it with false may stop the timer. |
| + void ManageTimer(bool did_work); |
| + |
| + // Helper method to change the state. |
| + void SetState(State new_state); |
| + |
| + // Configures |media_codec_| with the given codec parameters from the client. |
| + bool ConfigureMediaCodec(); |
| + |
| + // Processes the output buffer after it comes from MediaCodec. |
| + void OnDecodedFrame(const OutputBufferInfo* out); |
| + |
| + // Processed the output format change. |
| + void OnOutputFormatChanged(); |
| + |
| + // A helper function for logging. |
| + static const char* AsString(State state); |
| + |
| + scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| + |
| + // The current state of the object. |
| + State state_; |
| + |
| + // The queue of encoded (and maybe encrypted) buffers. |
| + using BufferCBPair = std::pair<scoped_refptr<DecoderBuffer>, DecodeCB>; |
| + using InputQueue = std::deque<BufferCBPair>; |
| + InputQueue input_queue_; |
| + |
| + // Decoder configuration cache. |
| + AudioDecoderConfig config_; |
| + |
| + // Frequently used constants derived from |config_|. |
| + int channel_count_; |
| + int bytes_per_frame_; |
| + |
| + // Callback that reports the end of initialization and its status. |
| + InitCB init_cb_; |
| + |
| + // Callback that delivers output frames. |
| + OutputCB output_cb_; |
| + |
| + // DecodeCB that should be fired after the decoder has been flashed in case |
| + // of EOS. |
| + DecodeCB eos_decode_cb_; |
| + |
| + // The bridge to Android's MediaCodec API. |
| + scoped_ptr<MediaCodecBridge> media_codec_; |
| + |
| + // Repeating timer that kicks MediaCodec operation. |
| + base::RepeatingTimer io_timer_; |
| + |
| + // Time at which we last did useful work on |io_timer_|. |
| + base::TimeTicks most_recent_work_; |
| + |
| + // CDM related stuff. |
| + |
| + // Callback to request/cancel CDM ready notification. |
| + SetCdmReadyCB set_cdm_ready_cb_; |
| + |
| + // Callback to notify that CDM is attached to the pipeine. |
| + CdmAttachedCB cdm_attached_cb_; |
| + |
| + // Holds a ref-count to the CDM. |
| + scoped_refptr<MediaKeys> cdm_; |
| + |
| + // MediaDrmBridge requires registration/unregistration of the player, this |
| + // registration id is used for this. |
| + int cdm_registration_id_; |
| + |
| + // The MediaCrypto object is used in the MediaCodec.configure() in case of |
| + // an encrypted stream. |
| + MediaDrmBridge::JavaObjectPtr media_crypto_; |
| + |
| + // Index of the dequeued and filled buffer that we keep trying to enqueue. |
| + // Such buffer appears in MEDIA_CODEC_NO_KEY processing. |
| + int pending_input_buf_index_; |
| + |
| + // Weak pointer factory must be the last member variable. |
| + base::WeakPtrFactory<AndroidAudioDecoder> weak_factory_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(AndroidAudioDecoder); |
| +}; |
| + |
| +} // namespace media |
| + |
| +#endif // MEDIA_FILTERS_ANDROID_AUDIO_DECODER_H_ |