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 // The decoder for MediaCodecPlayer. | |
25 // This class accepts the incoming data into AccessUnitQueue | |
26 // and works with MediaCodecBridge for decoding and rendering | |
27 // the frames. The MediaCodecPlayer has two decoder objects: | |
28 // audio and video. | |
29 // | |
30 // The decoder works on two threads. The data from demuxer comes | |
31 // on Media thread. The commands from MediaCodecPlayer, such as | |
32 // Prefetch, Start, RequestToStop also come on the Media thread. | |
33 // However, the operations with MediaCodec buffers and rendering | |
34 // happen on a separate thread called Decoder thread. | |
35 // This class creates, starts and stops it as necessary. | |
36 class MediaCodecDecoder { | |
37 public: | |
38 // The result of MediaCodec configuration, used by MediaCodecPlayer. | |
39 enum ConfigStatus { | |
40 CONFIG_FAILURE = 0, | |
41 CONFIG_OK, | |
42 CONFIG_KEY_FRAME_REQUIRED, | |
43 }; | |
44 | |
45 // The decoder reports current playback time to the MediaCodecPlayer. | |
46 typedef base::Callback<void(base::TimeDelta, base::TimeDelta)> | |
47 SetTimeCallback; | |
48 | |
49 MediaCodecDecoder( | |
50 const scoped_refptr<base::SingleThreadTaskRunner>& media_task_runner, | |
51 const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner, | |
qinmin
2015/05/26 00:49:19
remove dependence on ui_task_runner in decoders.
Tima Vaisburd
2015/05/28 02:00:34
Done.
| |
52 const base::Closure& request_data_cb, | |
53 const base::Closure& starvation_cb, | |
54 const base::Closure& stop_done_cb, | |
55 const base::Closure& error_cb, | |
56 const char* decoder_thread_name); | |
57 virtual ~MediaCodecDecoder(); | |
58 | |
59 virtual const char* class_name() const { return "Decoder"; } | |
60 | |
61 // MediaCodecDecoder exists through the whole lifetime of the player | |
62 // to support dynamic addition and removal of the streams. | |
63 // This method returns true if the current stream (audio or video) | |
64 // is currently active. | |
65 virtual bool HasStream() const = 0; | |
66 | |
67 // Store configuration for the use of upcoming Configure() | |
68 virtual void SetDemuxerConfigs(const DemuxerConfigs& configs) = 0; | |
69 | |
70 // Stop decoder thread, release the MediaCodecBridge and other resources. | |
71 virtual void ReleaseDecoderResources(); | |
72 | |
73 // Flush the MediaCodec and reset the AccessUnitQueue. | |
74 // Decoder thread should not be running. | |
75 virtual void Flush(); | |
76 | |
77 // Release MediaCodecBridge. | |
78 void ReleaseMediaCodec(); | |
79 | |
80 // Return corresponding conditions. | |
81 bool IsPrefetchingOrPlaying() const; | |
82 bool IsStopped() const; | |
83 bool IsCompleted() const; | |
84 | |
85 base::android::ScopedJavaLocalRef<jobject> GetMediaCrypto(); | |
86 | |
87 // Start prefetching: accumulate enough data in AccessUnitQueue. | |
88 // Decoder thread is not running. | |
89 void Prefetch(const base::Closure& prefetch_done_cb); | |
90 | |
91 // Configure MediaCodec. | |
92 ConfigStatus Configure(); | |
93 | |
94 // Start the decoder thread and resume the playback. | |
95 bool Start(base::TimeDelta current_time); | |
96 | |
97 // Stop the playback process synchronously. | |
98 // This method stops the decoder thread synchronously, | |
99 // and then releases all MediaCodec buffers. | |
100 void SyncStop(); | |
101 | |
102 // Request to stop the playback and return. | |
103 // Decoder will stop asynchronously after all the dequeued | |
104 // buffers are rendered. | |
105 void RequestToStop(); | |
106 | |
107 // Puts the incoming data into AccessUnitQueue. | |
108 void OnDemuxerDataAvailable(const DemuxerData& data); | |
109 | |
110 // Notification posted when asynchronous stop is done | |
111 // or the playback is completed. | |
112 void OnLastFrameRendered(bool completed); | |
113 | |
114 protected: | |
115 // Returns true if the new DemuxerConfids requires MediaCodec | |
116 // reconfiguration. | |
117 virtual bool IsCodecReconfigureNeeded(const DemuxerConfigs& curr, | |
118 const DemuxerConfigs& next) const = 0; | |
119 | |
120 // Does the part of MediaCodecBridge configuration that is specific | |
121 // to audio or video. | |
122 virtual ConfigStatus ConfigureInternal() = 0; | |
123 | |
124 // Associate PTS with device time so we can calculate delays. | |
qinmin
2015/05/26 00:49:19
s/Associate/Associates/ and fix all the verbs belo
Tima Vaisburd
2015/05/28 02:00:34
Done.
| |
125 // We use delays for video decoder only. | |
126 virtual void SynchronizePTSWithTime(base::TimeDelta current_time) {} | |
127 | |
128 // Process the change of the output format, varies by stream. | |
129 virtual void OnOutputFormatChanged() = 0; | |
130 | |
131 // Render the decoded frame and release output buffer, or post | |
132 // a delayed task to do it at a later time, | |
133 virtual void Render(int buffer_index, size_t size, bool render_output, | |
134 base::TimeDelta pts, bool eos_encountered) = 0; | |
135 | |
136 // For video we might have delayed tasks. | |
137 virtual int NumDelayedRenderTasks() const { return 0; } | |
138 | |
139 // Release output buffers that are dequeued and not released yet | |
140 // because their rendering is delayed (video). | |
141 virtual void ReleaseDelayedBuffers() {} | |
142 | |
143 // Helper methods. | |
144 | |
145 // Notify the decoder if the frame is the last one. | |
146 void CheckLastFrame(bool eos_encountered, bool has_delayed_tasks); | |
147 | |
148 // Protected data. | |
149 | |
150 // Object for posting tasks on Media thread. | |
151 scoped_refptr<base::SingleThreadTaskRunner> media_task_runner_; | |
152 | |
153 // Object for posting tasks on UI thread. | |
154 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_; | |
155 | |
156 // Controls Android MediaCodec | |
157 scoped_ptr<MediaCodecBridge> media_codec_bridge_; | |
158 | |
159 // We call MediaCodecBridge on this thread for both | |
160 // input and output buffers. | |
161 base::Thread decoder_thread_; | |
162 | |
163 // The queue of access units. | |
164 AccessUnitQueue au_queue_; | |
165 | |
166 private: | |
167 enum DecoderState { | |
168 STOPPED = 0, | |
169 PREFETCHING, | |
170 RUNNING, | |
171 STOPPING | |
172 }; | |
173 | |
174 // Prefetching callback that is posted to Media thread | |
175 // in the PREFETCHING state. | |
176 void PrefetchNextChunk(); | |
177 | |
178 // The callback to do actual playback. Posted to Decoder thread | |
179 // in the RUNNING mode | |
180 void ProcessNextFrame(); | |
181 | |
182 // Helper method for ProcessNextFrame. | |
183 // Pushes one input buffer to the MediaCodec if the codec ca accept it. | |
184 // Returns false if there was MediaCodec error. | |
185 bool EnqueueInputBuffer(); | |
186 | |
187 // Helper method for ProcessNextFrame. | |
188 // Pulls all currently available output frames and renders them. | |
189 // Returns false if there was MediaCodec error. | |
190 bool DepleteOutputBufferQueue(bool* eos_encountered); | |
191 | |
192 DecoderState GetState() const; | |
193 void SetState(DecoderState state); | |
194 | |
195 // Private Data. | |
196 | |
197 // Callback used to request more data. | |
198 base::Closure request_data_cb_; | |
199 | |
200 // These notifications are called on corresponding conditions. | |
201 base::Closure prefetch_done_cb_; | |
202 base::Closure starvation_cb_; | |
203 base::Closure stop_done_cb_; | |
204 base::Closure error_cb_; | |
205 | |
206 DecoderState state_; | |
207 mutable base::Lock state_lock_; | |
208 | |
209 // Flag is set when the EOS is enqueued into MediaCodec. | |
210 bool eos_enqueued_; | |
211 | |
212 // Flag is set when the EOS is received in MediaCodec output. | |
213 bool completed_; | |
214 | |
215 // Flag to ensure we post last frame notification once. | |
216 bool last_frame_posted_; | |
217 | |
218 base::WeakPtr<MediaCodecDecoder> weak_this_; | |
219 // NOTE: Weak pointers must be invalidated before all other member variables. | |
220 base::WeakPtrFactory<MediaCodecDecoder> weak_factory_; | |
221 | |
222 DISALLOW_COPY_AND_ASSIGN(MediaCodecDecoder); | |
223 }; | |
224 | |
225 } // namespace media | |
226 | |
227 #endif // MEDIA_BASE_ANDROID_MEDIA_CODEC_DECODER_H_ | |
OLD | NEW |