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

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

Issue 1176993005: Audio and video decoders for MediaCodecPlayer (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Added tests for video decoder, video decoder now reports current time. Created 5 years, 6 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 base::Closure& request_data_cb,
52 const base::Closure& starvation_cb,
53 const base::Closure& stop_done_cb,
54 const base::Closure& error_cb,
55 const char* decoder_thread_name);
56 virtual ~MediaCodecDecoder();
57
58 virtual const char* class_name() const { return "Decoder"; }
59
60 // MediaCodecDecoder exists through the whole lifetime of the player
61 // to support dynamic addition and removal of the streams.
62 // This method returns true if the current stream (audio or video)
63 // is currently active.
64 virtual bool HasStream() const = 0;
65
66 // Stores configuration for the use of upcoming Configure()
67 virtual void SetDemuxerConfigs(const DemuxerConfigs& configs) = 0;
68
69 // Stops decoder thread, releases the MediaCodecBridge and other resources.
70 virtual void ReleaseDecoderResources();
71
72 // Flushes the MediaCodec and resets the AccessUnitQueue.
73 // Decoder thread should not be running.
74 virtual void Flush();
75
76 // Releases MediaCodecBridge.
77 void ReleaseMediaCodec();
78
79 // Returns corresponding conditions.
80 bool IsPrefetchingOrPlaying() const;
81 bool IsStopped() const;
82 bool IsCompleted() const;
83
84 base::android::ScopedJavaLocalRef<jobject> GetMediaCrypto();
85
86 // Starts prefetching: accumulates enough data in AccessUnitQueue.
87 // Decoder thread is not running.
88 void Prefetch(const base::Closure& prefetch_done_cb);
89
90 // Configures MediaCodec.
91 ConfigStatus Configure();
92
93 // Starts the decoder thread and resumes the playback.
94 bool Start(base::TimeDelta current_time);
95
96 // Stops the playback process synchronously.
97 // This method stops the decoder thread synchronously,
98 // and then releases all MediaCodec buffers.
99 void SyncStop();
100
101 // Requests to stop the playback and returns.
102 // Decoder will stop asynchronously after all the dequeued output buffers
103 // are rendered.
104 void RequestToStop();
105
106 // Notification posted when asynchronous stop is done
107 // or the playback is completed.
108 void OnLastFrameRendered(bool completed);
109
110 // Puts the incoming data into AccessUnitQueue.
111 void OnDemuxerDataAvailable(const DemuxerData& data);
112
113 protected:
114 // Returns true if the new DemuxerConfigs requires MediaCodec
115 // reconfiguration.
116 virtual bool IsCodecReconfigureNeeded(const DemuxerConfigs& curr,
117 const DemuxerConfigs& next) const = 0;
118
119 // Does the part of MediaCodecBridge configuration that is specific
120 // to audio or video.
121 virtual ConfigStatus ConfigureInternal() = 0;
122
123 // Associates PTS with device time so we can calculate delays.
124 // We use delays for video decoder only.
125 virtual void SynchronizePTSWithTime(base::TimeDelta current_time) {}
126
127 // Processes the change of the output format, varies by stream.
128 virtual void OnOutputFormatChanged() = 0;
129
130 // Renders the decoded frame and releases output buffer, or posts
131 // a delayed task to do it at a later time,
132 virtual void Render(int buffer_index,
133 size_t size,
134 bool render_output,
135 base::TimeDelta pts,
136 bool eos_encountered) = 0;
137
138 // Returns the number of delayed task (we might have them for video).
139 virtual int NumDelayedRenderTasks() const { return 0; }
140
141 // Releases output buffers that are dequeued and not released yet
142 // because their rendering is delayed (video).
143 virtual void ReleaseDelayedBuffers() {}
144
145 // Helper methods.
146
147 // Notifies the decoder if the frame is the last one.
148 void CheckLastFrame(bool eos_encountered, bool has_delayed_tasks);
149
150 // Protected data.
151
152 // Object for posting tasks on Media thread.
153 scoped_refptr<base::SingleThreadTaskRunner> media_task_runner_;
154
155 // Controls Android MediaCodec
156 scoped_ptr<MediaCodecBridge> media_codec_bridge_;
157
158 // We call MediaCodecBridge on this thread for both
159 // input and output buffers.
160 base::Thread decoder_thread_;
161
162 // The queue of access units.
163 AccessUnitQueue au_queue_;
164
165 private:
166 enum DecoderState { STOPPED = 0, PREFETCHING, RUNNING, STOPPING };
167
168 // Prefetching callback that is posted to Media thread
169 // in the PREFETCHING state.
170 void PrefetchNextChunk();
171
172 // The callback to do actual playback. Posted to Decoder thread
173 // in the RUNNING mode
174 void ProcessNextFrame();
175
176 // Helper method for ProcessNextFrame.
177 // Pushes one input buffer to the MediaCodec if the codec can accept it.
178 // Returns false if there was MediaCodec error.
179 bool EnqueueInputBuffer();
180
181 // Helper method for ProcessNextFrame.
182 // Pulls all currently available output frames and renders them.
183 // Returns false if there was MediaCodec error.
184 bool DepleteOutputBufferQueue(bool* eos_encountered);
185
186 DecoderState GetState() const;
187 void SetState(DecoderState state);
188
189 // Private Data.
190
191 // Callback used to request more data.
192 base::Closure request_data_cb_;
193
194 // These notifications are called on corresponding conditions.
195 base::Closure prefetch_done_cb_;
196 base::Closure starvation_cb_;
197 base::Closure stop_done_cb_;
198 base::Closure error_cb_;
199
200 DecoderState state_;
201 mutable base::Lock state_lock_;
202
203 // Flag is set when the EOS is enqueued into MediaCodec.
204 bool eos_enqueued_;
205
206 // Flag is set when the EOS is received in MediaCodec output.
207 bool completed_;
208
209 // Flag to ensure we post last frame notification once.
210 bool last_frame_posted_;
211
212 base::WeakPtr<MediaCodecDecoder> weak_this_;
213 // NOTE: Weak pointers must be invalidated before all other member variables.
214 base::WeakPtrFactory<MediaCodecDecoder> weak_factory_;
215
216 DISALLOW_COPY_AND_ASSIGN(MediaCodecDecoder);
217 };
218
219 } // namespace media
220
221 #endif // MEDIA_BASE_ANDROID_MEDIA_CODEC_DECODER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698