Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / media / base / android / media_decoder_job.h
blob329512f2c6b52a93ad7717258ec896cb6c3aea66
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"
12 #include "media/base/android/media_codec_bridge.h"
13 #include "ui/gl/android/scoped_java_surface.h"
15 namespace base {
16 class SingleThreadTaskRunner;
19 namespace media {
21 class MediaDrmBridge;
23 // Class for managing all the decoding tasks. Each decoding task will be posted
24 // onto the same thread. The thread will be stopped once Stop() is called.
25 // Data is stored in 2 chunks. When new data arrives, it is always stored in
26 // an inactive chunk. And when the current active chunk becomes empty, a new
27 // data request will be sent to the renderer.
28 class MediaDecoderJob {
29 public:
30 // Return value when Decode() is called.
31 enum MediaDecoderJobStatus {
32 STATUS_SUCCESS,
33 STATUS_KEY_FRAME_REQUIRED,
34 STATUS_FAILURE,
37 struct Deleter {
38 inline void operator()(MediaDecoderJob* ptr) const { ptr->Release(); }
41 // Callback when a decoder job finishes its work. Args: whether decode
42 // finished successfully, current presentation time, max presentation time.
43 // If the current presentation time is equal to kNoTimestamp(), the decoder
44 // job skipped rendering of the decoded output and the callback target should
45 // ignore the timestamps provided.
46 typedef base::Callback<void(MediaCodecStatus, base::TimeDelta,
47 base::TimeDelta)> DecoderCallback;
48 // Callback when a decoder job finishes releasing the output buffer.
49 // Args: current presentation time, max presentation time.
50 // If the current presentation time is equal to kNoTimestamp(), the callback
51 // target should ignore the timestamps provided.
52 typedef base::Callback<void(base::TimeDelta, base::TimeDelta)>
53 ReleaseOutputCompletionCallback;
55 virtual ~MediaDecoderJob();
57 // Called by MediaSourcePlayer when more data for this object has arrived.
58 void OnDataReceived(const DemuxerData& data);
60 // Prefetch so we know the decoder job has data when we call Decode().
61 // |prefetch_cb| - Run when prefetching has completed.
62 void Prefetch(const base::Closure& prefetch_cb);
64 // Called by MediaSourcePlayer to decode some data.
65 // |callback| - Run when decode operation has completed.
67 // Returns STATUS_SUCCESS on success, or STATUS_FAILURE on failure, or
68 // STATUS_KEY_FRAME_REQUIRED if a browser seek is required. |callback| is
69 // ignored and will not be called for the latter 2 cases.
70 MediaDecoderJobStatus Decode(base::TimeTicks start_time_ticks,
71 base::TimeDelta start_presentation_timestamp,
72 const DecoderCallback& callback);
74 // Called to stop the last Decode() early.
75 // If the decoder is in the process of decoding the next frame, then
76 // this method will just allow the decode to complete as normal. If
77 // this object is waiting for a data request to complete, then this method
78 // will wait for the data to arrive and then call the |callback|
79 // passed to Decode() with a status of MEDIA_CODEC_ABORT. This ensures that
80 // the |callback| passed to Decode() is always called and the status
81 // reflects whether data was actually decoded or the decode terminated early.
82 void StopDecode();
84 // Flushes the decoder and abandons all the data that is being decoded.
85 virtual void Flush();
87 // Enters prerolling state. The job must not currently be decoding.
88 void BeginPrerolling(base::TimeDelta preroll_timestamp);
90 // Releases all the decoder resources as the current tab is going background.
91 virtual void ReleaseDecoderResources();
93 // Sets the demuxer configs.
94 virtual void SetDemuxerConfigs(const DemuxerConfigs& configs) = 0;
96 // Returns whether the decoder has finished decoding all the data.
97 bool OutputEOSReached() const;
99 // Returns true if the audio/video stream is available, implemented by child
100 // classes.
101 virtual bool HasStream() const = 0;
103 void SetDrmBridge(MediaDrmBridge* drm_bridge);
105 bool is_decoding() const { return !decode_cb_.is_null(); }
107 bool is_content_encrypted() const { return is_content_encrypted_; }
109 bool prerolling() const { return prerolling_; }
111 // Returns true if this object has data to decode.
112 bool HasData() const;
114 protected:
115 // Creates a new MediaDecoderJob instance.
116 // |decoder_task_runner| - Thread on which the decoder task will run.
117 // |request_data_cb| - Callback to request more data for the decoder.
118 // |config_changed_cb| - Callback to inform the caller that
119 // demuxer config has changed.
120 MediaDecoderJob(
121 const scoped_refptr<base::SingleThreadTaskRunner>& decoder_task_runner,
122 const base::Closure& request_data_cb,
123 const base::Closure& config_changed_cb);
125 // Release the output buffer at index |output_buffer_index| and render it if
126 // |render_output| is true. Upon completion, |callback| will be called.
127 virtual void ReleaseOutputBuffer(
128 int output_buffer_index,
129 size_t size,
130 bool render_output,
131 base::TimeDelta current_presentation_timestamp,
132 const ReleaseOutputCompletionCallback& callback) = 0;
134 // Returns true if the "time to render" needs to be computed for frames in
135 // this decoder job.
136 virtual bool ComputeTimeToRender() const = 0;
138 // Gets MediaCrypto object from |drm_bridge_|.
139 base::android::ScopedJavaLocalRef<jobject> GetMediaCrypto();
141 // Releases the |media_codec_bridge_|.
142 void ReleaseMediaCodecBridge();
144 // Sets the current frame to a previously cached key frame. Returns true if
145 // a key frame is found, or false otherwise.
146 // TODO(qinmin): add UMA to study the cache hit ratio for key frames.
147 bool SetCurrentFrameToPreviouslyCachedKeyFrame();
149 MediaDrmBridge* drm_bridge() { return drm_bridge_; }
151 void set_is_content_encrypted(bool is_content_encrypted) {
152 is_content_encrypted_ = is_content_encrypted;
155 bool need_to_reconfig_decoder_job_;
157 scoped_ptr<MediaCodecBridge> media_codec_bridge_;
159 private:
160 friend class MediaSourcePlayerTest;
162 // Causes this instance to be deleted on the thread it is bound to.
163 void Release();
165 // Queues an access unit into |media_codec_bridge_|'s input buffer.
166 MediaCodecStatus QueueInputBuffer(const AccessUnit& unit);
168 // Initiates a request for more data.
169 // |done_cb| is called when more data is available in |received_data_|.
170 void RequestData(const base::Closure& done_cb);
172 // Posts a task to start decoding the current access unit in |received_data_|.
173 void DecodeCurrentAccessUnit(
174 base::TimeTicks start_time_ticks,
175 base::TimeDelta start_presentation_timestamp);
177 // Helper function to decode data on |decoder_task_runner_|. |unit| contains
178 // the data to be decoded. |start_time_ticks| and
179 // |start_presentation_timestamp| represent the system time and the
180 // presentation timestamp when the first frame is rendered. We use these
181 // information to estimate when the current frame should be rendered.
182 // If |needs_flush| is true, codec needs to be flushed at the beginning of
183 // this call.
184 // It is possible that |stop_decode_pending_| or |release_resources_pending_|
185 // becomes true while DecodeInternal() is called. However, they should have
186 // no impact on DecodeInternal(). They will be handled after DecoderInternal()
187 // finishes and OnDecodeCompleted() is posted on the UI thread.
188 void DecodeInternal(const AccessUnit& unit,
189 base::TimeTicks start_time_ticks,
190 base::TimeDelta start_presentation_timestamp,
191 bool needs_flush,
192 const DecoderCallback& callback);
194 // Called on the UI thread to indicate that one decode cycle has completed.
195 // Completes any pending job destruction or any pending decode stop. If
196 // destruction was not pending, passes its arguments to |decode_cb_|.
197 void OnDecodeCompleted(MediaCodecStatus status,
198 base::TimeDelta current_presentation_timestamp,
199 base::TimeDelta max_presentation_timestamp);
201 // Helper function to get the current access unit that is being decoded.
202 const AccessUnit& CurrentAccessUnit() const;
204 // Helper function to get the current data chunk index that is being decoded.
205 size_t CurrentReceivedDataChunkIndex() const;
207 // Check whether a chunk has no remaining access units to decode. If
208 // |is_active_chunk| is true, this function returns whether decoder has
209 // consumed all data in |received_data_[current_demuxer_data_index_]|.
210 // Otherwise, it returns whether decoder has consumed all data in the inactive
211 // chunk.
212 bool NoAccessUnitsRemainingInChunk(bool is_active_chunk) const;
214 // Requests new data for the current chunk if it runs out of data.
215 void RequestCurrentChunkIfEmpty();
217 // Initializes |received_data_| and |access_unit_index_|.
218 void InitializeReceivedData();
220 // Called when the decoder is completely drained and is ready to be released.
221 void OnDecoderDrained();
223 // Creates |media_codec_bridge_| for decoding purpose.
224 // Returns STATUS_SUCCESS on success, or STATUS_FAILURE on failure, or
225 // STATUS_KEY_FRAME_REQUIRED if a browser seek is required.
226 MediaDecoderJobStatus CreateMediaCodecBridge();
228 // Implemented by the child class to create |media_codec_bridge_| for a
229 // particular stream.
230 // Returns STATUS_SUCCESS on success, or STATUS_FAILURE on failure, or
231 // STATUS_KEY_FRAME_REQUIRED if a browser seek is required.
232 virtual MediaDecoderJobStatus CreateMediaCodecBridgeInternal() = 0;
234 // Returns true if the |configs| doesn't match the current demuxer configs
235 // the decoder job has.
236 virtual bool AreDemuxerConfigsChanged(
237 const DemuxerConfigs& configs) const = 0;
239 // Returns true if |media_codec_bridge_| needs to be reconfigured for the
240 // new DemuxerConfigs, or false otherwise.
241 virtual bool IsCodecReconfigureNeeded(const DemuxerConfigs& configs) const;
243 // Signals to decoder job that decoder has updated output format. Decoder job
244 // may need to do internal reconfiguration in order to correctly interpret
245 // incoming buffers
246 virtual void OnOutputFormatChanged();
248 // Update the output format from the decoder, returns true if the output
249 // format changes, or false otherwise.
250 virtual bool UpdateOutputFormat();
252 // Return the index to |received_data_| that is not currently being decoded.
253 size_t inactive_demuxer_data_index() const {
254 return 1 - current_demuxer_data_index_;
257 // The UI message loop where callbacks should be dispatched.
258 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_;
260 // The task runner that decoder job runs on.
261 scoped_refptr<base::SingleThreadTaskRunner> decoder_task_runner_;
263 // Whether the decoder needs to be flushed.
264 bool needs_flush_;
266 // Whether input EOS is encountered.
267 // TODO(wolenetz/qinmin): Protect with a lock. See http://crbug.com/320043.
268 bool input_eos_encountered_;
270 // Whether output EOS is encountered.
271 bool output_eos_encountered_;
273 // Tracks whether DecodeInternal() should skip decoding if the first access
274 // unit is EOS or empty, and report |MEDIA_CODEC_OUTPUT_END_OF_STREAM|. This
275 // is to work around some decoders that could crash otherwise. See
276 // http://b/11696552.
277 bool skip_eos_enqueue_;
279 // The timestamp the decoder needs to preroll to. If an access unit's
280 // timestamp is smaller than |preroll_timestamp_|, don't render it.
281 // TODO(qinmin): Comparing access unit's timestamp with |preroll_timestamp_|
282 // is not very accurate.
283 base::TimeDelta preroll_timestamp_;
285 // Indicates prerolling state. If true, this job has not yet decoded output
286 // that it will render, since the most recent of job construction or
287 // BeginPrerolling(). If false, |preroll_timestamp_| has been reached.
288 // TODO(qinmin): Comparing access unit's timestamp with |preroll_timestamp_|
289 // is not very accurate.
290 bool prerolling_;
292 // Callback used to request more data.
293 base::Closure request_data_cb_;
295 // Callback to notify the caller config has changed.
296 base::Closure config_changed_cb_;
298 // Callback to run when new data has been received.
299 base::Closure data_received_cb_;
301 // Callback to run when the current Decode() operation completes.
302 DecoderCallback decode_cb_;
304 // Data received over IPC from last RequestData() operation.
305 // We keep 2 chunks at the same time to reduce the IPC latency between chunks.
306 // If data inside the current chunk are all decoded, we will request a new
307 // chunk from the demuxer and swap the current chunk with the other one.
308 // New data will always be stored in the other chunk since the current
309 // one may be still in use.
310 DemuxerData received_data_[2];
312 // Index to the current data chunk that is being decoded.
313 size_t current_demuxer_data_index_;
315 // Index to the access unit inside each data chunk that is being decoded.
316 size_t access_unit_index_[2];
318 // The index of input buffer that can be used by QueueInputBuffer().
319 // If the index is uninitialized or invalid, it must be -1.
320 int input_buf_index_;
322 // Indicates whether content is encrypted.
323 bool is_content_encrypted_;
325 // Indicates the decoder job should stop after decoding the current access
326 // unit.
327 bool stop_decode_pending_;
329 // Indicates that this object should be destroyed once the current
330 // Decode() has completed. This gets set when Release() gets called
331 // while there is a decode in progress.
332 bool destroy_pending_;
334 // Indicates whether the decoder is in the middle of requesting new data.
335 bool is_requesting_demuxer_data_;
337 // Indicates whether the incoming data should be ignored.
338 bool is_incoming_data_invalid_;
340 // Indicates that |media_codec_bridge_| should be released once the current
341 // Decode() has completed. This gets set when ReleaseDecoderResources() gets
342 // called while there is a decode in progress.
343 bool release_resources_pending_;
345 // Pointer to a DRM object that will be used for encrypted streams.
346 MediaDrmBridge* drm_bridge_;
348 // Indicates whether |media_codec_bridge_| is in the middle of being drained
349 // due to a config change.
350 bool drain_decoder_;
352 // This access unit is passed to the decoder during config changes to drain
353 // the decoder.
354 AccessUnit eos_unit_;
356 DISALLOW_IMPLICIT_CONSTRUCTORS(MediaDecoderJob);
359 } // namespace media
361 #endif // MEDIA_BASE_ANDROID_MEDIA_DECODER_JOB_H_