Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(681)

Side by Side Diff: media/base/android/media_codec_decoder.h

Issue 1128383003: Implementation of MediaCodecPlayer stage 1 (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Comments, better Listener callbacks, removed unused includes Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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,
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 // Stop decoder thread, release the MediaCodecBridge and other resources.
qinmin 2015/05/26 00:49:18 s/Stop/Stops/, s/release/releases/
68 virtual void ReleaseDecoderResources();
69
70 // Flush the MediaCodec and reset the AccessUnitQueue.
qinmin 2015/05/26 00:49:19 s/Flush/Flushes/, s/reset/resets/
71 // Decoder thread should not be running.
72 virtual void Flush();
73
74 // Release MediaCodecBridge.
qinmin 2015/05/26 00:49:19 s/Release/Releases/
75 void ReleaseMediaCodec();
76
77 // Return corresponding conditions.
qinmin 2015/05/26 00:49:18 s/Return/Returns/
78 bool IsPrefetchingOrPlaying() const;
79 bool IsStopped() const;
80 bool IsCompleted() const;
81
82 // Store configuration for the use of upcoming Configure()
qinmin 2015/05/26 00:49:18 s/Store/Stores/
83 void SetDemuxerConfigs(const DemuxerConfigs& configs);
84
85 base::android::ScopedJavaLocalRef<jobject> GetMediaCrypto();
86
87 // Start prefetching: accumulate enough data in AccessUnitQueue.
qinmin 2015/05/26 00:49:19 s/Start/Starts/
88 // Decoder thread is not running.
89 void Prefetch(const base::Closure& prefetch_done_cb);
90
91 // Configure MediaCodec.
qinmin 2015/05/26 00:49:19 s/Configure/Configures/
92 ConfigStatus Configure();
93
94 // Start the decoder thread and resume the playback.
qinmin 2015/05/26 00:49:18 s/Start/Starts/
95 bool Start(base::TimeDelta current_time);
96
97 // Stop the playback process synchronously.
qinmin 2015/05/26 00:49:18 s/Stop/Stops/
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.
125 // We use delays for video decoder only.
126 virtual void SynchronizePTSWithTime(base::TimeDelta current_time) {}
127
128 // Render the decoded frame and release output buffer, or post
qinmin 2015/05/26 00:49:19 s/Render/Renders/, s/post/posts/
129 // a delayed task to do it at a later time,
130 virtual void Render(int buffer_index, size_t size, bool render_output,
131 base::TimeDelta pts, bool eos_encountered) = 0;
132
133 // For video we might have delayed tasks.
134 virtual int NumDelayedRenderTasks() const { return 0; }
135
136 // Release output buffers that are dequeued and not released yet
qinmin 2015/05/26 00:49:19 s/Release/Releases/
137 // because their rendering is delayed (video).
138 virtual void ReleaseDelayedBuffers() {}
139
140 // Helper methods.
141
142 // Notify the decoder if the frame is the last one.
143 void CheckLastFrame(bool eos_encountered, bool has_delayed_tasks);
144
145 // Protected data.
146
147 // Object for posting tasks on Media thread.
148 scoped_refptr<base::SingleThreadTaskRunner> media_task_runner_;
149
150 // Object for posting tasks on UI thread.
151 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_;
152
153 // Controls Android MediaCodec
154 scoped_ptr<MediaCodecBridge> media_codec_bridge_;
155
156 // We call MediaCodecBridge on this thread for both
157 // input and output buffers.
158 base::Thread decoder_thread_;
159
160 // Configuration received from demuxer
161 DemuxerConfigs configs_;
162 mutable base::Lock configs_lock_;
163
164 // The queue of access units.
165 AccessUnitQueue au_queue_;
166
167 private:
168 enum DecoderState {
169 STOPPED = 0,
170 PREFETCHING,
171 RUNNING,
172 STOPPING
173 };
174
175 // Prefetching callback that is posted to Media thread
176 // in the PREFETCHING state.
177 void PrefetchNextChunk();
178
179 // The callback to do actual playback. Posted to Decoder thread
180 // in the RUNNING mode
181 void ProcessNextFrame();
182
183 // Helper method for ProcessNextFrame.
184 // Pushes one input buffer to the MediaCodec if the codec ca accept it.
185 // Returns false if there was MediaCodec error.
186 bool EnqueueInputBuffer(DecoderState state);
187
188 // Helper method for ProcessNextFrame.
189 // Pulls all currently available output frames and renders them.
190 // Returns false if there was MediaCodec error.
191 bool DepleteOutputBufferQueue(DecoderState state, bool* eos_encountered);
192
193 DecoderState GetState() const;
194 void SetState(DecoderState state);
195
196 // Private Data.
197
198 // Callback used to request more data.
199 base::Closure request_data_cb_;
200
201 // These notifications are called on corresponding conditions.
202 base::Closure prefetch_done_cb_;
203 base::Closure starvation_cb_;
204 base::Closure stop_done_cb_;
205 base::Closure error_cb_;
206
207 DecoderState state_;
208 mutable base::Lock state_lock_;
209
210 // Flag is set when the EOS is enqueued into MediaCodec.
211 bool eos_enqueued_;
212
213 // Flag is set when the EOS is received in MediaCodec output.
214 bool completed_;
215
216 // Flag to ensure we post last frame notification once.
217 bool last_frame_posted_;
218
219 base::WeakPtr<MediaCodecDecoder> weak_this_;
220 // NOTE: Weak pointers must be invalidated before all other member variables.
221 base::WeakPtrFactory<MediaCodecDecoder> weak_factory_;
222
223 DISALLOW_COPY_AND_ASSIGN(MediaCodecDecoder);
224 };
225
226 } // namespace media
227
228 #endif // MEDIA_BASE_ANDROID_MEDIA_CODEC_DECODER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698