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