Allow overlapping sync and async startup requests
[chromium-blink-merge.git] / media / base / android / media_decoder_job.h
blob4a3bd87cc89098e1362457f7733ff5d0215d36aa
1 // Copyright 2013 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.
5 #ifndef MEDIA_BASE_ANDROID_MEDIA_DECODER_JOB_H_
6 #define MEDIA_BASE_ANDROID_MEDIA_DECODER_JOB_H_
8 #include "base/callback.h"
9 #include "base/memory/weak_ptr.h"
10 #include "base/time/time.h"
11 #include "media/base/android/demuxer_stream_player_params.h"
13 namespace base {
14 class MessageLoopProxy;
17 namespace media {
19 class MediaCodecBridge;
21 // Class for managing all the decoding tasks. Each decoding task will be posted
22 // onto the same thread. The thread will be stopped once Stop() is called.
23 class MediaDecoderJob {
24 public:
25 enum DecodeStatus {
26 DECODE_SUCCEEDED,
27 DECODE_TRY_ENQUEUE_INPUT_AGAIN_LATER,
28 DECODE_TRY_DEQUEUE_OUTPUT_AGAIN_LATER,
29 DECODE_FORMAT_CHANGED,
30 DECODE_INPUT_END_OF_STREAM,
31 DECODE_OUTPUT_END_OF_STREAM,
32 DECODE_FAILED,
35 struct Deleter {
36 inline void operator()(MediaDecoderJob* ptr) const { ptr->Release(); }
39 virtual ~MediaDecoderJob();
41 // Callback when a decoder job finishes its work. Args: whether decode
42 // finished successfully, presentation time, audio output bytes.
43 typedef base::Callback<void(DecodeStatus, const base::TimeDelta&,
44 size_t)> DecoderCallback;
46 // Called by MediaSourcePlayer to decode some data.
47 void Decode(const AccessUnit& unit,
48 const base::TimeTicks& start_time_ticks,
49 const base::TimeDelta& start_presentation_timestamp,
50 const MediaDecoderJob::DecoderCallback& callback);
52 // Flush the decoder.
53 void Flush();
55 // Called on the UI thread to indicate that one decode cycle has completed.
56 void OnDecodeCompleted();
58 bool is_decoding() const { return is_decoding_; }
60 protected:
61 MediaDecoderJob(const scoped_refptr<base::MessageLoopProxy>& decoder_loop,
62 MediaCodecBridge* media_codec_bridge);
64 // Release the output buffer and render it.
65 virtual void ReleaseOutputBuffer(
66 int outputBufferIndex, size_t size,
67 const base::TimeDelta& presentation_timestamp,
68 const MediaDecoderJob::DecoderCallback& callback,
69 DecodeStatus status) = 0;
71 // Returns true if the "time to render" needs to be computed for frames in
72 // this decoder job.
73 virtual bool ComputeTimeToRender() const = 0;
75 private:
76 // Causes this instance to be deleted on the thread it is bound to.
77 void Release();
79 DecodeStatus QueueInputBuffer(const AccessUnit& unit);
81 // Helper function to decoder data on |thread_|. |unit| contains all the data
82 // to be decoded. |start_time_ticks| and |start_presentation_timestamp|
83 // represent the system time and the presentation timestamp when the first
84 // frame is rendered. We use these information to estimate when the current
85 // frame should be rendered. If |needs_flush| is true, codec needs to be
86 // flushed at the beginning of this call.
87 void DecodeInternal(const AccessUnit& unit,
88 const base::TimeTicks& start_time_ticks,
89 const base::TimeDelta& start_presentation_timestamp,
90 bool needs_flush,
91 const MediaDecoderJob::DecoderCallback& callback);
93 // The UI message loop where callbacks should be dispatched.
94 scoped_refptr<base::MessageLoopProxy> ui_loop_;
96 // The message loop that decoder job runs on.
97 scoped_refptr<base::MessageLoopProxy> decoder_loop_;
99 // The media codec bridge used for decoding. Owned by derived class.
100 // NOTE: This MUST NOT be accessed in the destructor.
101 MediaCodecBridge* media_codec_bridge_;
103 // Whether the decoder needs to be flushed.
104 bool needs_flush_;
106 // Whether input EOS is encountered.
107 bool input_eos_encountered_;
109 // Weak pointer passed to media decoder jobs for callbacks. It is bounded to
110 // the decoder thread.
111 base::WeakPtrFactory<MediaDecoderJob> weak_this_;
113 // Whether the decoder is actively decoding data.
114 bool is_decoding_;
116 DISALLOW_IMPLICIT_CONSTRUCTORS(MediaDecoderJob);
119 } // namespace media
121 #endif // MEDIA_BASE_ANDROID_MEDIA_DECODER_JOB_H_