Chromium Code Reviews| Index: media/filters/android/android_audio_decoder.h |
| diff --git a/media/filters/android/android_audio_decoder.h b/media/filters/android/android_audio_decoder.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..6f2362ea755ac3d105029f193d92b4bd1791dea8 |
| --- /dev/null |
| +++ b/media/filters/android/android_audio_decoder.h |
| @@ -0,0 +1,172 @@ |
| +// 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_ANDROID_AUDIO_DECODER_H_ |
| +#define MEDIA_FILTERS_ANDROID_ANDROID_AUDIO_DECODER_H_ |
| + |
| +#include <deque> |
| +#include <utility> |
| + |
| +#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" |
|
qinmin
2016/02/09 00:31:56
do we need this here?
Tima Vaisburd
2016/02/11 20:23:23
Removed.
|
| +#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" |
|
qinmin
2016/02/09 00:31:56
is this going to be exported?
Tima Vaisburd
2016/02/11 20:23:23
I think not, removed.
|
| +#include "media/base/media_keys.h" |
| + |
| +namespace base { |
| +class SingleThreadTaskRunner; |
| +} |
| + |
| +namespace media { |
| + |
| +// AudioDecoder based on Android's MediaCodec API. |
| +// |
| +// It is intended for EME encrypted audio streams which on Android require |
| +// the MediaCodec API for the playback and coordination with MediaDRM API. |
| +// Therefore, this decoder is a decrypting decoder: it is able to accept an |
| +// encrypted as well as non-encrypted stream. The output will be decrypted, |
| +// if necessary, and decoded. |
| + |
| +class MEDIA_EXPORT AndroidAudioDecoder : public AudioDecoder { |
| + public: |
| + explicit AndroidAudioDecoder( |
| + const scoped_refptr<base::SingleThreadTaskRunner>& task_runner); |
| + ~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, |
| + 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; |
| + }; |
| + |
| + // 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(); |
| + |
| + // Enqueues one pending input buffer into MediaCodec if MediaCodec has a room. |
| + // Returns true if any input was processed. |
| + bool QueueInput(); |
| + |
| + // Dequeues all output buffers from MediaCodec that are immediately available. |
| + // Returns true if any output buffer has been received from MediaCodec. |
| + 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. Should be called within the |
| + // timer callback. Calling it with true will start the timer if the timer is |
| + // not yet started. The first call with false sets the beginning of idle time, |
| + // and subsequent calls with false will eventually stop the timer when the |
| + // idle time is large enough. |
| + void ManageTimer(bool did_work); |
| + |
| + // Helper method to change the state. |
| + void SetState(State new_state); |
| + |
| + // The following helper methods ConfigureMediaCodec(), OnDecodedFrame(), |
| + // OnOutputFormatChanged() are specific to the stream (audio/video), but |
| + // others seem to apply to any decoder which would be based on MediaCodec. |
| + // TODO(timav): refactor the common part out and use it here and in AVDA |
| + // (http://crbug.com/583082). |
| + |
| + // Configures |media_codec_| with |config_|. Returns true if the configuration |
| + // succeeded. |
| + bool ConfigureMediaCodec(); |
| + |
| + // Processes the output buffer after it comes from MediaCodec. |
| + void OnDecodedFrame(const OutputBufferInfo& out); |
| + |
| + // Processes the output format change. |
| + void OnOutputFormatChanged(); |
| + |
| + // A helper function for logging. |
| + static const char* AsString(State state); |
| + |
| + // Every method is supposed to run on this |task_runner_|. |
| + scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| + |
| + // The current state of the object. |
| + State state_; |
| + |
| + // The queue of encoded (and maybe encrypted) buffers. The MediaCodec might |
| + // not be able to accept the input at the time of Dequeue(), thus all |
| + // DecoderBuffers first go to |input_queue_|. |
| + 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_; |
|
xhwang
2016/02/08 20:03:34
Is it possible to replace this with a (null, Decod
Tima Vaisburd
2016/02/11 20:23:23
It seems there is no need to remove the pair from
|
| + |
| + // 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 idle_time_begin_; |
|
liberato (no reviews please)
2016/02/08 16:04:36
i apologize, i didn't press send on friday about t
|
| + |
| + // 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_ANDROID_AUDIO_DECODER_H_ |