OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef MEDIA_BASE_ANDROID_MEDIA_CODEC_DECODER_H_ | 5 #ifndef MEDIA_BASE_ANDROID_MEDIA_CODEC_DECODER_H_ |
6 #define MEDIA_BASE_ANDROID_MEDIA_CODEC_DECODER_H_ | 6 #define MEDIA_BASE_ANDROID_MEDIA_CODEC_DECODER_H_ |
7 | 7 |
8 #include "base/android/scoped_java_ref.h" | 8 #include "base/android/scoped_java_ref.h" |
9 #include "base/callback.h" | 9 #include "base/callback.h" |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
147 // Called when async stop request is completed. | 147 // Called when async stop request is completed. |
148 // error_cb: | 148 // error_cb: |
149 // Called when a MediaCodec error occurred. If this happens, a player has | 149 // Called when a MediaCodec error occurred. If this happens, a player has |
150 // to either call ReleaseDecoderResources() or destroy the decoder object. | 150 // to either call ReleaseDecoderResources() or destroy the decoder object. |
151 // decoder_thread_name: | 151 // decoder_thread_name: |
152 // The thread name to be passed to decoder thread constructor. | 152 // The thread name to be passed to decoder thread constructor. |
153 MediaCodecDecoder( | 153 MediaCodecDecoder( |
154 const scoped_refptr<base::SingleThreadTaskRunner>& media_task_runner, | 154 const scoped_refptr<base::SingleThreadTaskRunner>& media_task_runner, |
155 const base::Closure& external_request_data_cb, | 155 const base::Closure& external_request_data_cb, |
156 const base::Closure& starvation_cb, | 156 const base::Closure& starvation_cb, |
| 157 const base::Closure& preroll_done_cb, |
157 const base::Closure& stop_done_cb, | 158 const base::Closure& stop_done_cb, |
158 const base::Closure& error_cb, | 159 const base::Closure& error_cb, |
159 const char* decoder_thread_name); | 160 const char* decoder_thread_name); |
160 virtual ~MediaCodecDecoder(); | 161 virtual ~MediaCodecDecoder(); |
161 | 162 |
162 virtual const char* class_name() const; | 163 virtual const char* class_name() const; |
163 | 164 |
164 // MediaCodecDecoder exists through the whole lifetime of the player | 165 // MediaCodecDecoder exists through the whole lifetime of the player |
165 // to support dynamic addition and removal of the streams. | 166 // to support dynamic addition and removal of the streams. |
166 // This method returns true if the current stream (audio or video) | 167 // This method returns true if the current stream (audio or video) |
167 // is currently active. | 168 // is currently active. |
168 virtual bool HasStream() const = 0; | 169 virtual bool HasStream() const = 0; |
169 | 170 |
170 // Stores configuration for the use of upcoming Configure() | 171 // Stores configuration for the use of upcoming Configure() |
171 virtual void SetDemuxerConfigs(const DemuxerConfigs& configs) = 0; | 172 virtual void SetDemuxerConfigs(const DemuxerConfigs& configs) = 0; |
172 | 173 |
173 // Stops decoder thread, releases the MediaCodecBridge and other resources. | 174 // Stops decoder thread, releases the MediaCodecBridge and other resources. |
174 virtual void ReleaseDecoderResources(); | 175 virtual void ReleaseDecoderResources(); |
175 | 176 |
176 // Flushes the MediaCodec, after that resets the AccessUnitQueue and blocks | 177 // Flushes the MediaCodec, after that resets the AccessUnitQueue and blocks |
177 // the input. Decoder thread should not be running. | 178 // the input. Decoder thread should not be running. |
178 virtual void Flush(); | 179 virtual void Flush(); |
179 | 180 |
180 // Releases MediaCodecBridge. | 181 // Releases MediaCodecBridge and any related buffers or references. |
181 void ReleaseMediaCodec(); | 182 virtual void ReleaseMediaCodec(); |
182 | 183 |
183 // Returns corresponding conditions. | 184 // Returns corresponding conditions. |
184 bool IsPrefetchingOrPlaying() const; | 185 bool IsPrefetchingOrPlaying() const; |
| 186 bool IsPrerollDone() const; |
185 bool IsStopped() const; | 187 bool IsStopped() const; |
186 bool IsCompleted() const; | 188 bool IsCompleted() const; |
187 | 189 |
188 base::android::ScopedJavaLocalRef<jobject> GetMediaCrypto(); | 190 base::android::ScopedJavaLocalRef<jobject> GetMediaCrypto(); |
189 | 191 |
190 // Starts prefetching: accumulates enough data in AccessUnitQueue. | 192 // Starts prefetching: accumulates enough data in AccessUnitQueue. |
191 // Decoder thread is not running. | 193 // Decoder thread is not running. |
192 void Prefetch(const base::Closure& prefetch_done_cb); | 194 void Prefetch(const base::Closure& prefetch_done_cb); |
193 | 195 |
194 // Configures MediaCodec. | 196 // Configures MediaCodec. |
195 ConfigStatus Configure(); | 197 ConfigStatus Configure(); |
196 | 198 |
| 199 // Sets preroll timestamp. The rendering starts after we reached it. |
| 200 void SetPrerollTimestamp(base::TimeDelta preroll_timestamp); |
| 201 |
197 // Starts the decoder thread and resumes the playback. | 202 // Starts the decoder thread and resumes the playback. |
198 bool Start(base::TimeDelta current_time); | 203 bool Start(base::TimeDelta start_timestamp); |
| 204 |
| 205 void ResumeAfterPreroll(); |
199 | 206 |
200 // Stops the playback process synchronously. This method stops the decoder | 207 // Stops the playback process synchronously. This method stops the decoder |
201 // thread synchronously, and then releases all MediaCodec buffers. | 208 // thread synchronously, and then releases all MediaCodec buffers. |
202 void SyncStop(); | 209 void SyncStop(); |
203 | 210 |
204 // Requests to stop the playback and returns. | 211 // Requests to stop the playback and returns. |
205 // Decoder will stop asynchronously after all the dequeued output buffers | 212 // Decoder will stop asynchronously after all the dequeued output buffers |
206 // are rendered. | 213 // are rendered. |
207 void RequestToStop(); | 214 void RequestToStop(); |
208 | 215 |
209 // Notification posted when asynchronous stop is done or playback completed. | 216 // Notification posted when asynchronous stop is done or playback completed. |
210 void OnLastFrameRendered(bool completed); | 217 void OnLastFrameRendered(bool completed); |
211 | 218 |
| 219 // Notification posted when last prerolled frame has been returned to codec. |
| 220 void OnPrerollDone(); |
| 221 |
212 // Puts the incoming data into AccessUnitQueue. | 222 // Puts the incoming data into AccessUnitQueue. |
213 void OnDemuxerDataAvailable(const DemuxerData& data); | 223 void OnDemuxerDataAvailable(const DemuxerData& data); |
214 | 224 |
215 protected: | 225 protected: |
| 226 enum RenderMode { |
| 227 kRenderSkip = 0, |
| 228 kRenderAfterPreroll, |
| 229 kRenderNow, |
| 230 }; |
| 231 |
216 // Returns true if the new DemuxerConfigs requires MediaCodec | 232 // Returns true if the new DemuxerConfigs requires MediaCodec |
217 // reconfiguration. | 233 // reconfiguration. |
218 virtual bool IsCodecReconfigureNeeded(const DemuxerConfigs& curr, | 234 virtual bool IsCodecReconfigureNeeded(const DemuxerConfigs& curr, |
219 const DemuxerConfigs& next) const = 0; | 235 const DemuxerConfigs& next) const = 0; |
220 | 236 |
221 // Does the part of MediaCodecBridge configuration that is specific | 237 // Does the part of MediaCodecBridge configuration that is specific |
222 // to audio or video. | 238 // to audio or video. |
223 virtual ConfigStatus ConfigureInternal() = 0; | 239 virtual ConfigStatus ConfigureInternal() = 0; |
224 | 240 |
225 // Associates PTS with device time so we can calculate delays. | 241 // Associates PTS with device time so we can calculate delays. |
226 // We use delays for video decoder only. | 242 // We use delays for video decoder only. |
227 virtual void SynchronizePTSWithTime(base::TimeDelta current_time) {} | 243 virtual void AssociateCurrentTimeWithPTS(base::TimeDelta current_time) {} |
| 244 |
| 245 // Invalidate delay calculation. We use delays for video decoder only. |
| 246 virtual void DissociatePTSFromTime() {} |
228 | 247 |
229 // Processes the change of the output format, varies by stream. | 248 // Processes the change of the output format, varies by stream. |
230 virtual void OnOutputFormatChanged() = 0; | 249 virtual void OnOutputFormatChanged() = 0; |
231 | 250 |
232 // Renders the decoded frame and releases output buffer, or posts | 251 // Renders the decoded frame and releases output buffer, or posts |
233 // a delayed task to do it at a later time, | 252 // a delayed task to do it at a later time, |
234 virtual void Render(int buffer_index, | 253 virtual void Render(int buffer_index, |
235 size_t size, | 254 size_t size, |
236 bool render_output, | 255 RenderMode render_mode, |
237 base::TimeDelta pts, | 256 base::TimeDelta pts, |
238 bool eos_encountered) = 0; | 257 bool eos_encountered) = 0; |
239 | 258 |
240 // Returns the number of delayed task (we might have them for video). | 259 // Returns the number of delayed task (we might have them for video). |
241 virtual int NumDelayedRenderTasks() const; | 260 virtual int NumDelayedRenderTasks() const; |
242 | 261 |
243 // Releases output buffers that are dequeued and not released yet (video) | 262 // Releases output buffers that are dequeued and not released yet (video). |
244 // if the |release| parameter is set and then remove the references to them. | 263 virtual void ReleaseDelayedBuffers() {} |
245 virtual void ClearDelayedBuffers(bool release) {} | |
246 | 264 |
247 #ifndef NDEBUG | 265 #ifndef NDEBUG |
248 // For video, checks that access unit is the key frame or stand-alone EOS. | 266 // For video, checks that access unit is the key frame or stand-alone EOS. |
249 virtual void VerifyUnitIsKeyFrame(const AccessUnit* unit) const {} | 267 virtual void VerifyUnitIsKeyFrame(const AccessUnit* unit) const {} |
250 #endif | 268 #endif |
251 | 269 |
252 // Helper methods. | 270 // Helper methods. |
253 | 271 |
254 // Notifies the decoder if the frame is the last one. | 272 // Notifies the decoder if the frame is the last one. |
255 void CheckLastFrame(bool eos_encountered, bool has_delayed_tasks); | 273 void CheckLastFrame(bool eos_encountered, bool has_delayed_tasks); |
256 | 274 |
| 275 const char* AsString(RenderMode render_mode); |
| 276 |
257 // Protected data. | 277 // Protected data. |
258 | 278 |
259 // Object for posting tasks on Media thread. | 279 // Object for posting tasks on Media thread. |
260 scoped_refptr<base::SingleThreadTaskRunner> media_task_runner_; | 280 scoped_refptr<base::SingleThreadTaskRunner> media_task_runner_; |
261 | 281 |
262 // Controls Android MediaCodec | 282 // Controls Android MediaCodec |
263 scoped_ptr<MediaCodecBridge> media_codec_bridge_; | 283 scoped_ptr<MediaCodecBridge> media_codec_bridge_; |
264 | 284 |
265 // We call MediaCodecBridge on this thread for both | 285 // We call MediaCodecBridge on this thread for both |
266 // input and output buffers. | 286 // input and output buffers. |
267 base::Thread decoder_thread_; | 287 base::Thread decoder_thread_; |
268 | 288 |
269 // The queue of access units. | 289 // The queue of access units. |
270 AccessUnitQueue au_queue_; | 290 AccessUnitQueue au_queue_; |
271 | 291 |
272 // Flag forces reconfiguration even if |media_codec_bridge_| exists. Currently | 292 // Flag forces reconfiguration even if |media_codec_bridge_| exists. Currently |
273 // is set by video decoder when the video surface changes. | 293 // is set by video decoder when the video surface changes. |
274 bool needs_reconfigure_; | 294 bool needs_reconfigure_; |
275 | 295 |
276 private: | 296 private: |
277 enum DecoderState { | 297 enum DecoderState { |
278 kStopped = 0, | 298 kStopped = 0, |
279 kPrefetching, | 299 kPrefetching, |
280 kPrefetched, | 300 kPrefetched, |
| 301 kPrerolling, |
| 302 kPrerolled, |
281 kRunning, | 303 kRunning, |
282 kStopping, | 304 kStopping, |
283 kInEmergencyStop, | 305 kInEmergencyStop, |
284 kError, | 306 kError, |
285 }; | 307 }; |
286 | 308 |
287 // Helper method that processes an error from MediaCodec. | 309 // Helper method that processes an error from MediaCodec. |
288 void OnCodecError(); | 310 void OnCodecError(); |
289 | 311 |
290 // Requests data. Ensures there is no more than one request at a time. | 312 // Requests data. Ensures there is no more than one request at a time. |
(...skipping 23 matching lines...) Expand all Loading... |
314 const char* AsString(DecoderState state); | 336 const char* AsString(DecoderState state); |
315 | 337 |
316 // Private Data. | 338 // Private Data. |
317 | 339 |
318 // External data request callback that is passed to decoder. | 340 // External data request callback that is passed to decoder. |
319 base::Closure external_request_data_cb_; | 341 base::Closure external_request_data_cb_; |
320 | 342 |
321 // These notifications are called on corresponding conditions. | 343 // These notifications are called on corresponding conditions. |
322 base::Closure prefetch_done_cb_; | 344 base::Closure prefetch_done_cb_; |
323 base::Closure starvation_cb_; | 345 base::Closure starvation_cb_; |
| 346 base::Closure preroll_done_cb_; |
324 base::Closure stop_done_cb_; | 347 base::Closure stop_done_cb_; |
325 base::Closure error_cb_; | 348 base::Closure error_cb_; |
326 | 349 |
327 // Data request callback that is posted by decoder internally. | 350 // Data request callback that is posted by decoder internally. |
328 base::Closure request_data_cb_; | 351 base::Closure request_data_cb_; |
329 | 352 |
330 // Callback used to post OnCodecError method. | 353 // Callback used to post OnCodecError method. |
331 base::Closure internal_error_cb_; | 354 base::Closure internal_error_cb_; |
332 | 355 |
| 356 // Callback for posting OnPrerollDone method. |
| 357 base::Closure internal_preroll_done_cb_; |
| 358 |
333 // Internal state. | 359 // Internal state. |
334 DecoderState state_; | 360 DecoderState state_; |
335 mutable base::Lock state_lock_; | 361 mutable base::Lock state_lock_; |
336 | 362 |
| 363 // Preroll timestamp is set if we need preroll and cleared after we done it. |
| 364 base::TimeDelta preroll_timestamp_; |
| 365 |
337 // Flag is set when the EOS is enqueued into MediaCodec. Reset by Flush. | 366 // Flag is set when the EOS is enqueued into MediaCodec. Reset by Flush. |
338 bool eos_enqueued_; | 367 bool eos_enqueued_; |
339 | 368 |
340 // Flag is set when the EOS is received in MediaCodec output. Reset by Flush. | 369 // Flag is set when the EOS is received in MediaCodec output. Reset by Flush. |
341 bool completed_; | 370 bool completed_; |
342 | 371 |
343 // Flag to ensure we post last frame notification once. | 372 // Flag to ensure we post last frame notification once. |
344 bool last_frame_posted_; | 373 bool last_frame_posted_; |
345 | 374 |
346 // Indicates whether the data request is in progress. | 375 // Indicates whether the data request is in progress. |
347 bool is_data_request_in_progress_; | 376 bool is_data_request_in_progress_; |
348 | 377 |
349 // Indicates whether the incoming data should be ignored. | 378 // Indicates whether the incoming data should be ignored. |
350 bool is_incoming_data_invalid_; | 379 bool is_incoming_data_invalid_; |
351 | 380 |
352 #ifndef NDEBUG | 381 #ifndef NDEBUG |
353 // When set, we check that the following video frame is the key frame. | 382 // When set, we check that the following video frame is the key frame. |
354 bool verify_next_frame_is_key_; | 383 bool verify_next_frame_is_key_; |
355 #endif | 384 #endif |
356 | 385 |
357 // NOTE: Weak pointers must be invalidated before all other member variables. | 386 // NOTE: Weak pointers must be invalidated before all other member variables. |
358 base::WeakPtrFactory<MediaCodecDecoder> weak_factory_; | 387 base::WeakPtrFactory<MediaCodecDecoder> weak_factory_; |
359 | 388 |
360 DISALLOW_COPY_AND_ASSIGN(MediaCodecDecoder); | 389 DISALLOW_COPY_AND_ASSIGN(MediaCodecDecoder); |
361 }; | 390 }; |
362 | 391 |
363 } // namespace media | 392 } // namespace media |
364 | 393 |
365 #endif // MEDIA_BASE_ANDROID_MEDIA_CODEC_DECODER_H_ | 394 #endif // MEDIA_BASE_ANDROID_MEDIA_CODEC_DECODER_H_ |
OLD | NEW |