OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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_BASE_ANDROID_MEDIA_CODEC_DECODER_H_ |
| 6 #define MEDIA_BASE_ANDROID_MEDIA_CODEC_DECODER_H_ |
| 7 |
| 8 #include "base/android/scoped_java_ref.h" |
| 9 #include "base/callback.h" |
| 10 #include "base/macros.h" |
| 11 #include "base/memory/ref_counted.h" |
| 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/single_thread_task_runner.h" |
| 14 #include "base/synchronization/lock.h" |
| 15 #include "base/threading/thread.h" |
| 16 #include "base/time/time.h" |
| 17 #include "media/base/android/access_unit_queue.h" |
| 18 #include "media/base/android/demuxer_stream_player_params.h" |
| 19 |
| 20 namespace media { |
| 21 |
| 22 class MediaCodecBridge; |
| 23 |
| 24 enum DecoderState { |
| 25 STOPPED = 0, |
| 26 PREFETCHING, |
| 27 RUNNING, |
| 28 STOPPING |
| 29 }; |
| 30 |
| 31 class MediaCodecDecoder { |
| 32 public: |
| 33 |
| 34 enum ConfigStatus { |
| 35 CONFIG_FAILURE = 0, |
| 36 CONFIG_OK, |
| 37 CONFIG_KEY_FRAME_REQUIRED, |
| 38 }; |
| 39 |
| 40 typedef base::Callback<void(base::TimeDelta, base::TimeDelta)> |
| 41 SetTimeCallback; |
| 42 |
| 43 MediaCodecDecoder( |
| 44 const scoped_refptr<base::SingleThreadTaskRunner>& media_task_runner, |
| 45 const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner, |
| 46 const base::Closure& request_data_cb, |
| 47 const base::Closure& starvation_cb, |
| 48 const base::Closure& stop_done_cb, |
| 49 const base::Closure& error_cb, |
| 50 const char* decoder_thread_name); |
| 51 virtual ~MediaCodecDecoder(); |
| 52 |
| 53 virtual const char* class_name() const { return "Decoder"; } |
| 54 |
| 55 virtual bool HasStream() const = 0; |
| 56 virtual void ReleaseDecoderResources(); |
| 57 virtual void Flush(); |
| 58 |
| 59 void ReleaseMediaCodec(); |
| 60 |
| 61 bool IsPrefetchingOrPlaying() const; |
| 62 bool IsStopped() const; |
| 63 bool IsCompleted() const; |
| 64 |
| 65 void SetDemuxerConfigs(const DemuxerConfigs& configs); |
| 66 |
| 67 base::android::ScopedJavaLocalRef<jobject> GetMediaCrypto(); |
| 68 |
| 69 void Prefetch(const base::Closure& prefetch_done_cb); |
| 70 ConfigStatus Configure(); |
| 71 bool Start(base::TimeDelta current_time); |
| 72 void SyncStop(); |
| 73 void RequestToStop(); |
| 74 |
| 75 void OnDemuxerDataAvailable(const DemuxerData& data); |
| 76 |
| 77 void OnLastFrameRendered(bool completed); |
| 78 |
| 79 protected: |
| 80 virtual bool IsCodecReconfigureNeeded(const DemuxerConfigs& curr, |
| 81 const DemuxerConfigs& next) const = 0; |
| 82 virtual ConfigStatus ConfigureInternal() = 0; |
| 83 virtual void SynchronizePTSWithTime(base::TimeDelta current_time) {} |
| 84 |
| 85 virtual void Render(int buffer_index, size_t size, bool render_output, |
| 86 base::TimeDelta pts, bool eos_encountered) = 0; |
| 87 virtual int NumDelayedRenderTasks() const { return 0; } |
| 88 virtual void ReleaseDelayedBuffers() {} |
| 89 |
| 90 void ProcessLastFrame(bool eos_encountered, bool has_delayed_tasks); |
| 91 |
| 92 // Protected data. |
| 93 |
| 94 // Object for posting tasks on Media thread. |
| 95 scoped_refptr<base::SingleThreadTaskRunner> media_task_runner_; |
| 96 |
| 97 // Object for posting tasks on UI thread. |
| 98 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_; |
| 99 |
| 100 // Controls Android MediaCodec |
| 101 scoped_ptr<MediaCodecBridge> media_codec_bridge_; |
| 102 |
| 103 // We call MediaCodecBridge on this thread for both |
| 104 // input and output buffers. |
| 105 base::Thread decoder_thread_; |
| 106 |
| 107 // Configuration received from demuxer |
| 108 DemuxerConfigs configs_; |
| 109 mutable base::Lock configs_lock_; |
| 110 |
| 111 // The queue of access units. |
| 112 AccessUnitQueue au_queue_; |
| 113 |
| 114 private: |
| 115 void PrefetchNextChunk(); |
| 116 void ProcessNextFrame(); |
| 117 |
| 118 // Returns false if there was MediaCodec error. |
| 119 bool EnqueueInputBuffer(DecoderState state); |
| 120 |
| 121 // Returns false if there was MediaCodec error. |
| 122 bool DepleteOutputBufferQueue(DecoderState state, bool* eos_encountered); |
| 123 |
| 124 DecoderState GetState() const; |
| 125 void SetState(DecoderState state); |
| 126 |
| 127 // Private Data. |
| 128 |
| 129 // Callback used to request more data. |
| 130 base::Closure request_data_cb_; |
| 131 |
| 132 // These notifications are called on corresponding conditions . |
| 133 base::Closure prefetch_done_cb_; |
| 134 base::Closure starvation_cb_; |
| 135 base::Closure stop_done_cb_; |
| 136 base::Closure error_cb_; |
| 137 |
| 138 DecoderState state_; |
| 139 mutable base::Lock state_lock_; |
| 140 |
| 141 // Flag is set when the EOS is enqueued into MediaCodec. |
| 142 bool eos_enqueued_; |
| 143 |
| 144 // Flag is set when the EOS is received in MediaCodec output. |
| 145 bool completed_; |
| 146 |
| 147 // Flag to ensure we post last frame notification once. |
| 148 bool last_frame_posted_; |
| 149 |
| 150 base::WeakPtr<MediaCodecDecoder> weak_this_; |
| 151 // NOTE: Weak pointers must be invalidated before all other member variables. |
| 152 base::WeakPtrFactory<MediaCodecDecoder> weak_factory_; |
| 153 |
| 154 DISALLOW_COPY_AND_ASSIGN(MediaCodecDecoder); |
| 155 }; |
| 156 |
| 157 } // namespace media |
| 158 |
| 159 #endif // MEDIA_BASE_ANDROID_MEDIA_CODEC_DECODER_H_ |
OLD | NEW |