Revert "Reland c91b178b07b0d - Delete dead signin code (SigninGlobalError)"
[chromium-blink-merge.git] / media / base / android / media_codec_decoder.h
blob2cc38b6ff98967f3b971d740b69ce48aa074aa53
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.
5 #ifndef MEDIA_BASE_ANDROID_MEDIA_CODEC_DECODER_H_
6 #define MEDIA_BASE_ANDROID_MEDIA_CODEC_DECODER_H_
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"
20 namespace media {
22 class MediaCodecBridge;
24 // The decoder for MediaCodecPlayer.
25 // This class accepts the incoming data into AccessUnitQueue and works with
26 // MediaCodecBridge for decoding and rendering the frames. The MediaCodecPlayer
27 // has two decoder objects: audio and video.
29 // The decoder works on two threads. The data from demuxer comes on Media
30 // thread. The commands from MediaCodecPlayer, such as Prefetch, Start,
31 // RequestToStop also come on the Media thread. The operations with MediaCodec
32 // buffers and rendering happen on a separate thread called Decoder thread.
33 // This class creates, starts and stops it as necessary.
35 // Decoder's internal state machine goes through the following states:
37 // [ Stopped ] <------------------- (any state except Error)
38 // | | |
39 // | Prefetch |--- internal ------|
40 // v | transition v
41 // [ Prefetching ] | [ Error ]
42 // | |
43 // | internal transition |
44 // v | Error recovery:
45 // [ Prefetched ] |
46 // | | (any state including Error)
47 // | Configure and Start | |
48 // v | | ReleaseDecoderResources
49 // [ Running ] | v
50 // | | [ InEmergencyStop ]
51 // | RequestToStop | |
52 // v | |(decoder thread stopped)
53 // [ Stopping ] ------------------- v
54 // [ Stopped ]
56 // [ Stopped ] --------------------
57 // ^ |
58 // | Flush |
59 // ---------------------------
61 // (any state except Error)
62 // |
63 // | SyncStop
64 // v
65 // [ InEmergencyStop ]
66 // |
67 // |(decoder thread stopped)
68 // v
69 // [ Stopped ]
71 // Here is the workflow that is expected to be maintained by a caller, which is
72 // MediaCodecPlayer currently.
74 // [ Stopped ]
75 // |
76 // | Prefetch
77 // v
78 // [ Prefetching ]
79 // |
80 // | (Enough data received)
81 // v
82 // [ Prefetched ]
83 // |
84 // | <---------- SetDemuxerConfigs (*)
85 // |
86 // | <---------- SetVideoSurface (**)
87 // |
88 // | Configure --------------------------------------------+
89 // | |
90 // v v
91 // ( Config Succeeded ) ( Key frame required )
92 // | |
93 // | Start |
94 // v |
95 // [ Running ] ------------------------------+ |
96 // | | |
97 // | | |
98 // | RequestToStop | SyncStop | SyncStop
99 // | | |
100 // [ Stopping ] | |
101 // | | |
102 // | ( Last frame rendered ) | |
103 // | | |
104 // | | |
105 // v | |
106 // [ Stopped ] <-----------------------------+-----------------+
109 // (*) Demuxer configs is a precondition to Configure(), but MediaCodecPlayer
110 // has stricter requirements and they are set before Prefetch().
112 // (**) VideoSurface is a precondition to video decoder Configure(), can be set
113 // any time before Configure().
115 class MediaCodecDecoder {
116 public:
117 // The result of MediaCodec configuration, used by MediaCodecPlayer.
118 enum ConfigStatus {
119 kConfigFailure = 0,
120 kConfigOk,
121 kConfigKeyFrameRequired,
124 // The decoder reports current playback time to the MediaCodecPlayer.
125 // For audio, the parameters designate the beginning and end of a time
126 // interval. The beginning is the estimated time that is playing right now.
127 // The end is the playback time of the last buffered data. During normal
128 // playback the subsequent intervals overlap.
129 // For video both values are PTS of the corresponding frame, i.e. the interval
130 // has zero width.
131 typedef base::Callback<void(base::TimeDelta, base::TimeDelta)>
132 SetTimeCallback;
134 // MediaCodecDecoder constructor.
135 // Parameters:
136 // media_task_runner:
137 // A task runner for the controlling thread. All public methods should be
138 // called on this thread, and callbacks are delivered on this thread.
139 // The MediaCodecPlayer uses a dedicated (Media) thread for this.
140 // external_request_data_cb:
141 // Called periodically as the amount of internally stored data decreases.
142 // The receiver should call OnDemuxerDataAvailable() with more data.
143 // starvation_cb:
144 // Called when starvation is detected. The decoder state does not change.
145 // The player is supposed to stop and then prefetch the decoder.
146 // stop_done_cb:
147 // Called when async stop request is completed.
148 // error_cb:
149 // Called when a MediaCodec error occurred. If this happens, a player has
150 // to either call ReleaseDecoderResources() or destroy the decoder object.
151 // decoder_thread_name:
152 // The thread name to be passed to decoder thread constructor.
153 MediaCodecDecoder(
154 const scoped_refptr<base::SingleThreadTaskRunner>& media_task_runner,
155 const base::Closure& external_request_data_cb,
156 const base::Closure& starvation_cb,
157 const base::Closure& stop_done_cb,
158 const base::Closure& error_cb,
159 const char* decoder_thread_name);
160 virtual ~MediaCodecDecoder();
162 virtual const char* class_name() const;
164 // MediaCodecDecoder exists through the whole lifetime of the player
165 // to support dynamic addition and removal of the streams.
166 // This method returns true if the current stream (audio or video)
167 // is currently active.
168 virtual bool HasStream() const = 0;
170 // Stores configuration for the use of upcoming Configure()
171 virtual void SetDemuxerConfigs(const DemuxerConfigs& configs) = 0;
173 // Stops decoder thread, releases the MediaCodecBridge and other resources.
174 virtual void ReleaseDecoderResources();
176 // Flushes the MediaCodec, after that resets the AccessUnitQueue and blocks
177 // the input. Decoder thread should not be running.
178 virtual void Flush();
180 // Releases MediaCodecBridge.
181 void ReleaseMediaCodec();
183 // Returns corresponding conditions.
184 bool IsPrefetchingOrPlaying() const;
185 bool IsStopped() const;
186 bool IsCompleted() const;
188 base::android::ScopedJavaLocalRef<jobject> GetMediaCrypto();
190 // Starts prefetching: accumulates enough data in AccessUnitQueue.
191 // Decoder thread is not running.
192 void Prefetch(const base::Closure& prefetch_done_cb);
194 // Configures MediaCodec.
195 ConfigStatus Configure();
197 // Starts the decoder thread and resumes the playback.
198 bool Start(base::TimeDelta current_time);
200 // Stops the playback process synchronously. This method stops the decoder
201 // thread synchronously, and then releases all MediaCodec buffers.
202 void SyncStop();
204 // Requests to stop the playback and returns.
205 // Decoder will stop asynchronously after all the dequeued output buffers
206 // are rendered.
207 void RequestToStop();
209 // Notification posted when asynchronous stop is done or playback completed.
210 void OnLastFrameRendered(bool completed);
212 // Puts the incoming data into AccessUnitQueue.
213 void OnDemuxerDataAvailable(const DemuxerData& data);
215 protected:
216 // Returns true if the new DemuxerConfigs requires MediaCodec
217 // reconfiguration.
218 virtual bool IsCodecReconfigureNeeded(const DemuxerConfigs& curr,
219 const DemuxerConfigs& next) const = 0;
221 // Does the part of MediaCodecBridge configuration that is specific
222 // to audio or video.
223 virtual ConfigStatus ConfigureInternal() = 0;
225 // Associates PTS with device time so we can calculate delays.
226 // We use delays for video decoder only.
227 virtual void SynchronizePTSWithTime(base::TimeDelta current_time) {}
229 // Processes the change of the output format, varies by stream.
230 virtual void OnOutputFormatChanged() = 0;
232 // Renders the decoded frame and releases output buffer, or posts
233 // a delayed task to do it at a later time,
234 virtual void Render(int buffer_index,
235 size_t size,
236 bool render_output,
237 base::TimeDelta pts,
238 bool eos_encountered) = 0;
240 // Returns the number of delayed task (we might have them for video).
241 virtual int NumDelayedRenderTasks() const;
243 // 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.
245 virtual void ClearDelayedBuffers(bool release) {}
247 #ifndef NDEBUG
248 // For video, checks that access unit is the key frame or stand-alone EOS.
249 virtual void VerifyUnitIsKeyFrame(const AccessUnit* unit) const {}
250 #endif
252 // Helper methods.
254 // Notifies the decoder if the frame is the last one.
255 void CheckLastFrame(bool eos_encountered, bool has_delayed_tasks);
257 // Returns true is we are in the process of sync stop.
258 bool InEmergencyStop() const { return GetState() == kInEmergencyStop; }
260 // Protected data.
262 // Object for posting tasks on Media thread.
263 scoped_refptr<base::SingleThreadTaskRunner> media_task_runner_;
265 // Controls Android MediaCodec
266 scoped_ptr<MediaCodecBridge> media_codec_bridge_;
268 // We call MediaCodecBridge on this thread for both
269 // input and output buffers.
270 base::Thread decoder_thread_;
272 // The queue of access units.
273 AccessUnitQueue au_queue_;
275 // Flag forces reconfiguration even if |media_codec_bridge_| exists. Currently
276 // is set by video decoder when the video surface changes.
277 bool needs_reconfigure_;
279 private:
280 enum DecoderState {
281 kStopped = 0,
282 kPrefetching,
283 kPrefetched,
284 kRunning,
285 kStopping,
286 kInEmergencyStop,
287 kError,
290 // Helper method that processes an error from MediaCodec.
291 void OnCodecError();
293 // Requests data. Ensures there is no more than one request at a time.
294 void RequestData();
296 // Prefetching callback that is posted to Media thread
297 // in the kPrefetching state.
298 void PrefetchNextChunk();
300 // The callback to do actual playback. Posted to Decoder thread
301 // in the kRunning state.
302 void ProcessNextFrame();
304 // Helper method for ProcessNextFrame.
305 // Pushes one input buffer to the MediaCodec if the codec can accept it.
306 // Returns false if there was MediaCodec error.
307 bool EnqueueInputBuffer();
309 // Helper method for ProcessNextFrame.
310 // Pulls all currently available output frames and renders them.
311 // Returns true if we need to continue decoding process, i.e post next
312 // ProcessNextFrame method, and false if we need to stop decoding.
313 bool DepleteOutputBufferQueue();
315 DecoderState GetState() const;
316 void SetState(DecoderState state);
317 const char* AsString(DecoderState state);
319 // Private Data.
321 // External data request callback that is passed to decoder.
322 base::Closure external_request_data_cb_;
324 // These notifications are called on corresponding conditions.
325 base::Closure prefetch_done_cb_;
326 base::Closure starvation_cb_;
327 base::Closure stop_done_cb_;
328 base::Closure error_cb_;
330 // Data request callback that is posted by decoder internally.
331 base::Closure request_data_cb_;
333 // Callback used to post OnCodecError method.
334 base::Closure internal_error_cb_;
336 // Internal state.
337 DecoderState state_;
338 mutable base::Lock state_lock_;
340 // Flag is set when the EOS is enqueued into MediaCodec. Reset by Flush.
341 bool eos_enqueued_;
343 // Flag is set when the EOS is received in MediaCodec output. Reset by Flush.
344 bool completed_;
346 // Flag to ensure we post last frame notification once.
347 bool last_frame_posted_;
349 // Indicates whether the data request is in progress.
350 bool is_data_request_in_progress_;
352 // Indicates whether the incoming data should be ignored.
353 bool is_incoming_data_invalid_;
355 #ifndef NDEBUG
356 // When set, we check that the following video frame is the key frame.
357 bool verify_next_frame_is_key_;
358 #endif
360 // NOTE: Weak pointers must be invalidated before all other member variables.
361 base::WeakPtrFactory<MediaCodecDecoder> weak_factory_;
363 DISALLOW_COPY_AND_ASSIGN(MediaCodecDecoder);
366 } // namespace media
368 #endif // MEDIA_BASE_ANDROID_MEDIA_CODEC_DECODER_H_