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

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

Issue 1472943002: Refactor MediaCodecBridge to add support for NDK APIs (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years 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
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef MEDIA_BASE_ANDROID_MEDIA_CODEC_BRIDGE_H_ 5 #ifndef MEDIA_BASE_ANDROID_MEDIA_CODEC_BRIDGE_H_
6 #define MEDIA_BASE_ANDROID_MEDIA_CODEC_BRIDGE_H_ 6 #define MEDIA_BASE_ANDROID_MEDIA_CODEC_BRIDGE_H_
7 7
8 #include <jni.h> 8 #include <jni.h>
9 #include <set> 9 #include <set>
10 #include <string> 10 #include <string>
11 #include <vector>
11 12
12 #include "base/android/scoped_java_ref.h" 13 #include "base/compiler_specific.h"
13 #include "base/time/time.h" 14 #include "base/time/time.h"
14 #include "media/base/audio_decoder_config.h" 15 #include "media/base/media_export.h"
15 #include "media/base/video_decoder_config.h"
16 #include "ui/gfx/geometry/size.h"
17 16
18 namespace media { 17 namespace media {
19 18
20 struct SubsampleEntry; 19 struct SubsampleEntry;
21 20
22 // These must be in sync with MediaCodecBridge.MEDIA_CODEC_XXX constants in 21 // These must be in sync with MediaCodecBridge.MEDIA_CODEC_XXX constants in
23 // MediaCodecBridge.java. 22 // MediaCodecBridge.java.
24 enum MediaCodecStatus { 23 enum MediaCodecStatus {
25 MEDIA_CODEC_OK, 24 MEDIA_CODEC_OK,
26 MEDIA_CODEC_DEQUEUE_INPUT_AGAIN_LATER, 25 MEDIA_CODEC_DEQUEUE_INPUT_AGAIN_LATER,
27 MEDIA_CODEC_DEQUEUE_OUTPUT_AGAIN_LATER, 26 MEDIA_CODEC_DEQUEUE_OUTPUT_AGAIN_LATER,
28 MEDIA_CODEC_OUTPUT_BUFFERS_CHANGED, 27 MEDIA_CODEC_OUTPUT_BUFFERS_CHANGED,
29 MEDIA_CODEC_OUTPUT_FORMAT_CHANGED, 28 MEDIA_CODEC_OUTPUT_FORMAT_CHANGED,
30 MEDIA_CODEC_INPUT_END_OF_STREAM, 29 MEDIA_CODEC_INPUT_END_OF_STREAM,
31 MEDIA_CODEC_OUTPUT_END_OF_STREAM, 30 MEDIA_CODEC_OUTPUT_END_OF_STREAM,
32 MEDIA_CODEC_NO_KEY, 31 MEDIA_CODEC_NO_KEY,
33 MEDIA_CODEC_ABORT, 32 MEDIA_CODEC_ABORT,
34 MEDIA_CODEC_ERROR 33 MEDIA_CODEC_ERROR
35 }; 34 };
36 35
37 // Codec direction. Keep this in sync with MediaCodecBridge.java. 36 // Codec direction. Keep this in sync with MediaCodecBridge.java.
Tima Vaisburd 2015/11/24 02:24:19 Now MediaCodecUtil.java?
qinmin 2015/11/24 20:44:06 Done.
38 enum MediaCodecDirection { 37 enum MediaCodecDirection {
39 MEDIA_CODEC_DECODER, 38 MEDIA_CODEC_DECODER,
40 MEDIA_CODEC_ENCODER, 39 MEDIA_CODEC_ENCODER,
41 }; 40 };
42 41
43 // This class serves as a bridge for native code to call java functions inside 42 // Interface for wrapping different Android MediaCodec implementations. For
44 // Android MediaCodec class. For more information on Android MediaCodec, check 43 // more information on Android MediaCodec, check
45 // http://developer.android.com/reference/android/media/MediaCodec.html 44 // http://developer.android.com/reference/android/media/MediaCodec.html
46 // Note: MediaCodec is only available on JB and greater. 45 // Note: MediaCodec is only available on JB and greater.
47 // Use AudioCodecBridge or VideoCodecBridge to create an instance of this
48 // object.
49 //
50 // TODO(fischman,xhwang): replace this (and the enums that go with it) with
51 // chromium's JNI auto-generation hotness.
52 class MEDIA_EXPORT MediaCodecBridge { 46 class MEDIA_EXPORT MediaCodecBridge {
53 public: 47 public:
54 // Returns true if MediaCodec is available on the device. 48 // Returns true if MediaCodec is available on the device.
55 // All other static methods check IsAvailable() internally. There's no need 49 // All other static methods check IsAvailable() internally. There's no need
56 // to check IsAvailable() explicitly before calling them. 50 // to check IsAvailable() explicitly before calling them.
57 static bool IsAvailable(); 51 static bool IsAvailable();
58 52
59 // Returns true if MediaCodec.setParameters() is available on the device. 53 // Returns true if MediaCodec.setParameters() is available on the device.
60 static bool SupportsSetParameters(); 54 static bool SupportsSetParameters();
61 55
(...skipping 26 matching lines...) Expand all
88 static std::set<int> GetEncoderColorFormats(const std::string& mime_type); 82 static std::set<int> GetEncoderColorFormats(const std::string& mime_type);
89 83
90 virtual ~MediaCodecBridge(); 84 virtual ~MediaCodecBridge();
91 85
92 // Resets both input and output, all indices previously returned in calls to 86 // Resets both input and output, all indices previously returned in calls to
93 // DequeueInputBuffer() and DequeueOutputBuffer() become invalid. 87 // DequeueInputBuffer() and DequeueOutputBuffer() become invalid.
94 // Please note that this clears all the inputs in the media codec. In other 88 // Please note that this clears all the inputs in the media codec. In other
95 // words, there will be no outputs until new input is provided. 89 // words, there will be no outputs until new input is provided.
96 // Returns MEDIA_CODEC_ERROR if an unexpected error happens, or Media_CODEC_OK 90 // Returns MEDIA_CODEC_ERROR if an unexpected error happens, or Media_CODEC_OK
97 // otherwise. 91 // otherwise.
98 MediaCodecStatus Reset(); 92 virtual MediaCodecStatus Reset() = 0;
99 93
100 // Finishes the decode/encode session. The instance remains active 94 // Finishes the decode/encode session. The instance remains active
101 // and ready to be StartAudio/Video()ed again. HOWEVER, due to the buggy 95 // and ready to be StartAudio/Video()ed again. HOWEVER, due to the buggy
102 // vendor's implementation , b/8125974, Stop() -> StartAudio/Video() may not 96 // vendor's implementation , b/8125974, Stop() -> StartAudio/Video() may not
103 // work on some devices. For reliability, Stop() -> delete and recreate new 97 // work on some devices. For reliability, Stop() -> delete and recreate new
104 // instance -> StartAudio/Video() is recommended. 98 // instance -> StartAudio/Video() is recommended.
105 void Stop(); 99 virtual void Stop() = 0;
106 100
107 // Used for getting output format. This is valid after DequeueInputBuffer() 101 // Used for getting output format. This is valid after DequeueInputBuffer()
108 // returns a format change by returning INFO_OUTPUT_FORMAT_CHANGED 102 // returns a format change by returning INFO_OUTPUT_FORMAT_CHANGED
109 void GetOutputFormat(int* width, int* height); 103 virtual void GetOutputFormat(int* width, int* height) = 0;
110 104
111 // Used for checking for new sampling rate after DequeueInputBuffer() returns 105 // Used for checking for new sampling rate after DequeueInputBuffer() returns
112 // INFO_OUTPUT_FORMAT_CHANGED 106 // INFO_OUTPUT_FORMAT_CHANGED
113 int GetOutputSamplingRate(); 107 virtual int GetOutputSamplingRate() = 0;
114 108
115 // Submits a byte array to the given input buffer. Call this after getting an 109 // Submits a byte array to the given input buffer. Call this after getting an
116 // available buffer from DequeueInputBuffer(). If |data| is NULL, assume the 110 // available buffer from DequeueInputBuffer(). If |data| is NULL, assume the
117 // input buffer has already been populated (but still obey |size|). 111 // input buffer has already been populated (but still obey |size|).
118 // |data_size| must be less than kint32max (because Java). 112 // |data_size| must be less than kint32max (because Java).
119 MediaCodecStatus QueueInputBuffer(int index, 113 virtual MediaCodecStatus QueueInputBuffer(
120 const uint8* data, 114 int index,
121 size_t data_size, 115 const uint8* data,
122 const base::TimeDelta& presentation_time); 116 size_t data_size,
117 const base::TimeDelta& presentation_time) = 0;
123 118
124 // Similar to the above call, but submits a buffer that is encrypted. Note: 119 // Similar to the above call, but submits a buffer that is encrypted. Note:
125 // NULL |subsamples| indicates the whole buffer is encrypted. If |data| is 120 // NULL |subsamples| indicates the whole buffer is encrypted. If |data| is
126 // NULL, assume the input buffer has already been populated (but still obey 121 // NULL, assume the input buffer has already been populated (but still obey
127 // |data_size|). |data_size| must be less than kint32max (because Java). 122 // |data_size|). |data_size| must be less than kint32max (because Java).
128 MediaCodecStatus QueueSecureInputBuffer( 123 MediaCodecStatus QueueSecureInputBuffer(
129 int index, 124 int index,
130 const uint8* data, 125 const uint8* data,
131 size_t data_size, 126 size_t data_size,
132 const std::string& key_id, 127 const std::string& key_id,
133 const std::string& iv, 128 const std::string& iv,
134 const std::vector<SubsampleEntry>& subsamples, 129 const std::vector<SubsampleEntry>& subsamples,
135 const base::TimeDelta& presentation_time); 130 const base::TimeDelta& presentation_time);
136 131
137 // Same QueueSecureInputBuffer overriden for the use with MediaSourcePlayer 132 // Same QueueSecureInputBuffer overriden for the use with MediaSourcePlayer
138 // and MediaCodecPlayer. 133 // and MediaCodecPlayer.
139 // TODO(timav): remove this method and keep only the one above after we 134 // TODO(timav): remove this method and keep only the one above after we
140 // switch to the Spitzer pipeline. 135 // switch to the Spitzer pipeline.
141 MediaCodecStatus QueueSecureInputBuffer( 136 virtual MediaCodecStatus QueueSecureInputBuffer(
142 int index, 137 int index,
143 const uint8* data, 138 const uint8* data,
144 size_t data_size, 139 size_t data_size,
145 const uint8* key_id, 140 const uint8* key_id,
146 int key_id_size, 141 int key_id_size,
147 const uint8* iv, 142 const uint8* iv,
148 int iv_size, 143 int iv_size,
149 const SubsampleEntry* subsamples, 144 const SubsampleEntry* subsamples,
150 int subsamples_size, 145 int subsamples_size,
151 const base::TimeDelta& presentation_time); 146 const base::TimeDelta& presentation_time) = 0;
152 147
153 // Submits an empty buffer with a EOS (END OF STREAM) flag. 148 // Submits an empty buffer with a EOS (END OF STREAM) flag.
154 void QueueEOS(int input_buffer_index); 149 virtual void QueueEOS(int input_buffer_index) = 0;
155 150
156 // Returns: 151 // Returns:
157 // MEDIA_CODEC_OK if an input buffer is ready to be filled with valid data, 152 // MEDIA_CODEC_OK if an input buffer is ready to be filled with valid data,
158 // MEDIA_CODEC_ENQUEUE_INPUT_AGAIN_LATER if no such buffer is available, or 153 // MEDIA_CODEC_ENQUEUE_INPUT_AGAIN_LATER if no such buffer is available, or
159 // MEDIA_CODEC_ERROR if unexpected error happens. 154 // MEDIA_CODEC_ERROR if unexpected error happens.
160 // Note: Never use infinite timeout as this would block the decoder thread and 155 // Note: Never use infinite timeout as this would block the decoder thread and
161 // prevent the decoder job from being released. 156 // prevent the decoder job from being released.
162 MediaCodecStatus DequeueInputBuffer(const base::TimeDelta& timeout, 157 virtual MediaCodecStatus DequeueInputBuffer(const base::TimeDelta& timeout,
163 int* index); 158 int* index) = 0;
164 159
165 // Dequeues an output buffer, block at most timeout_us microseconds. 160 // Dequeues an output buffer, block at most timeout_us microseconds.
166 // Returns the status of this operation. If OK is returned, the output 161 // Returns the status of this operation. If OK is returned, the output
167 // parameters should be populated. Otherwise, the values of output parameters 162 // parameters should be populated. Otherwise, the values of output parameters
168 // should not be used. Output parameters other than index/offset/size are 163 // should not be used. Output parameters other than index/offset/size are
169 // optional and only set if not NULL. 164 // optional and only set if not NULL.
170 // Note: Never use infinite timeout as this would block the decoder thread and 165 // Note: Never use infinite timeout as this would block the decoder thread and
171 // prevent the decoder job from being released. 166 // prevent the decoder job from being released.
172 // TODO(xhwang): Can we drop |end_of_stream| and return 167 // TODO(xhwang): Can we drop |end_of_stream| and return
173 // MEDIA_CODEC_OUTPUT_END_OF_STREAM? 168 // MEDIA_CODEC_OUTPUT_END_OF_STREAM?
174 MediaCodecStatus DequeueOutputBuffer(const base::TimeDelta& timeout, 169 virtual MediaCodecStatus DequeueOutputBuffer(
175 int* index, 170 const base::TimeDelta& timeout,
176 size_t* offset, 171 int* index,
177 size_t* size, 172 size_t* offset,
178 base::TimeDelta* presentation_time, 173 size_t* size,
179 bool* end_of_stream, 174 base::TimeDelta* presentation_time,
180 bool* key_frame); 175 bool* end_of_stream,
176 bool* key_frame) = 0;
181 177
182 // Returns the buffer to the codec. If you previously specified a surface when 178 // Returns the buffer to the codec. If you previously specified a surface when
183 // configuring this video decoder you can optionally render the buffer. 179 // configuring this video decoder you can optionally render the buffer.
184 void ReleaseOutputBuffer(int index, bool render); 180 virtual void ReleaseOutputBuffer(int index, bool render) = 0;
185 181
186 // Returns the number of output buffers used by the codec. 182 // Returns the number of output buffers used by the codec.
187 // TODO(qinmin): this call is deprecated in Lollipop. 183 // TODO(qinmin): this call is deprecated in Lollipop.
188 int GetOutputBuffersCount(); 184 virtual int GetOutputBuffersCount();
189 185
190 // Returns the capacity of each output buffer used by the codec. 186 // Returns the capacity of each output buffer used by the codec.
191 // TODO(qinmin): this call is deprecated in Lollipop. 187 // TODO(qinmin): this call is deprecated in Lollipop.
192 size_t GetOutputBuffersCapacity(); 188 virtual size_t GetOutputBuffersCapacity();
193 189
194 // Returns an input buffer's base pointer and capacity. 190 // Returns an input buffer's base pointer and capacity.
195 void GetInputBuffer(int input_buffer_index, uint8** data, size_t* capacity); 191 virtual void GetInputBuffer(int input_buffer_index,
192 uint8** data,
193 size_t* capacity) = 0;
196 194
197 // Copy |dst_size| bytes from output buffer |index|'s |offset| onwards into 195 // Copy |dst_size| bytes from output buffer |index|'s |offset| onwards into
198 // |*dst|. 196 // |*dst|.
199 bool CopyFromOutputBuffer(int index, size_t offset, void* dst, int dst_size); 197 virtual bool CopyFromOutputBuffer(int index,
198 size_t offset,
199 void* dst,
200 int dst_size) = 0;
200 201
201 static bool RegisterMediaCodecBridge(JNIEnv* env); 202 static bool RegisterMediaCodecUtil(JNIEnv* env);
Tima Vaisburd 2015/11/24 02:24:19 Would it be cleaner to create a new C++ class Medi
qinmin 2015/11/24 20:44:06 Done.
202 203
203 protected: 204 protected:
205 MediaCodecBridge();
206
204 // Returns true if |mime_type| is known to be unaccelerated (i.e. backed by a 207 // Returns true if |mime_type| is known to be unaccelerated (i.e. backed by a
205 // software codec instead of a hardware one). 208 // software codec instead of a hardware one).
206 static bool IsKnownUnaccelerated(const std::string& mime_type, 209 static bool IsKnownUnaccelerated(const std::string& mime_type,
207 MediaCodecDirection direction); 210 MediaCodecDirection direction);
208 211
209 MediaCodecBridge(const std::string& mime,
210 bool is_secure,
211 MediaCodecDirection direction);
212
213 // Calls start() against the media codec instance. Used in StartXXX() after
214 // configuring media codec. Returns whether media codec was successfully
215 // started.
216 bool StartInternal() WARN_UNUSED_RESULT;
217
218 // Called to get the buffer address given the output buffer index and offset.
219 // This function returns the size of the output and |addr| is the pointer to
220 // the address to read.
221 int GetOutputBufferAddress(int index, size_t offset, void** addr);
222
223 jobject media_codec() { return j_media_codec_.obj(); }
224 MediaCodecDirection direction_;
225
226 private:
227 // Fills a particular input buffer; returns false if |data_size| exceeds the 212 // Fills a particular input buffer; returns false if |data_size| exceeds the
228 // input buffer's capacity (and doesn't touch the input buffer in that case). 213 // input buffer's capacity (and doesn't touch the input buffer in that case).
229 bool FillInputBuffer(int index, 214 bool FillInputBuffer(int index,
230 const uint8* data, 215 const uint8* data,
231 size_t data_size) WARN_UNUSED_RESULT; 216 size_t data_size) WARN_UNUSED_RESULT;
232 217
233 // Java MediaCodec instance. 218 // Calls start() against the media codec instance. Used in StartXXX() after
234 base::android::ScopedJavaGlobalRef<jobject> j_media_codec_; 219 // configuring media codec. Returns whether media codec was successfully
220 // started.
221 virtual bool StartInternal() WARN_UNUSED_RESULT = 0;
Tima Vaisburd 2015/11/24 02:24:19 I would be nice to have a public method Start() in
qinmin 2015/11/24 20:44:06 Done.
235 222
236 DISALLOW_COPY_AND_ASSIGN(MediaCodecBridge); 223 DISALLOW_COPY_AND_ASSIGN(MediaCodecBridge);
237 }; 224 };
238 225
239 class AudioCodecBridge : public MediaCodecBridge {
240 public:
241 // Returns an AudioCodecBridge instance if |codec| is supported, or a NULL
242 // pointer otherwise.
243 static AudioCodecBridge* Create(const AudioCodec& codec);
244
245 // See MediaCodecBridge::IsKnownUnaccelerated().
246 static bool IsKnownUnaccelerated(const AudioCodec& codec);
247
248 // Start the audio codec bridge.
249 bool Start(const AudioCodec& codec, int sample_rate, int channel_count,
250 const uint8* extra_data, size_t extra_data_size,
251 int64 codec_delay_ns, int64 seek_preroll_ns,
252 bool play_audio, jobject media_crypto) WARN_UNUSED_RESULT;
253
254 // Plays the output buffer right away or save for later playback if |postpone|
255 // is set to true. This call must be called after DequeueOutputBuffer() and
256 // before ReleaseOutputBuffer. The data is extracted from the output buffers
257 // using |index|, |size| and |offset|. Returns the playback head position
258 // expressed in frames.
259 // When |postpone| is set to true, the next PlayOutputBuffer() should have
260 // postpone == false, and it will play two buffers: the postponed one and
261 // the one identified by |index|.
262 int64 PlayOutputBuffer(int index,
263 size_t size,
264 size_t offset,
265 bool postpone = false);
266
267 // Set the volume of the audio output.
268 void SetVolume(double volume);
269
270 private:
271 explicit AudioCodecBridge(const std::string& mime);
272
273 // Configure the java MediaFormat object with the extra codec data passed in.
274 bool ConfigureMediaFormat(jobject j_format, const AudioCodec& codec,
275 const uint8* extra_data, size_t extra_data_size,
276 int64 codec_delay_ns, int64 seek_preroll_ns);
277 };
278
279 class MEDIA_EXPORT VideoCodecBridge : public MediaCodecBridge {
280 public:
281 // See MediaCodecBridge::IsKnownUnaccelerated().
282 static bool IsKnownUnaccelerated(const VideoCodec& codec,
283 MediaCodecDirection direction);
284
285 // Create, start, and return a VideoCodecBridge decoder or NULL on failure.
286 static VideoCodecBridge* CreateDecoder(
287 const VideoCodec& codec, // e.g. media::kCodecVP8
288 bool is_secure,
289 const gfx::Size& size, // Output frame size.
290 jobject surface, // Output surface, optional.
291 jobject media_crypto); // MediaCrypto object, optional.
292
293 // Create, start, and return a VideoCodecBridge encoder or NULL on failure.
294 static VideoCodecBridge* CreateEncoder(
295 const VideoCodec& codec, // e.g. media::kCodecVP8
296 const gfx::Size& size, // input frame size
297 int bit_rate, // bits/second
298 int frame_rate, // frames/second
299 int i_frame_interval, // count
300 int color_format); // MediaCodecInfo.CodecCapabilities.
301
302 void SetVideoBitrate(int bps);
303 void RequestKeyFrameSoon();
304
305 // Returns whether adaptive playback is supported for this object given
306 // the new size.
307 bool IsAdaptivePlaybackSupported(int width, int height);
308
309 // Test-only method to set the return value of IsAdaptivePlaybackSupported().
310 // Without this function, the return value of that function will be device
311 // dependent. If |adaptive_playback_supported| is equal to 0, the return value
312 // will be false. If |adaptive_playback_supported| is larger than 0, the
313 // return value will be true.
314 void set_adaptive_playback_supported_for_testing(
315 int adaptive_playback_supported) {
316 adaptive_playback_supported_for_testing_ = adaptive_playback_supported;
317 }
318
319 private:
320 VideoCodecBridge(const std::string& mime,
321 bool is_secure,
322 MediaCodecDirection direction);
323
324 int adaptive_playback_supported_for_testing_;
325 };
326
327 } // namespace media 226 } // namespace media
328 227
329 #endif // MEDIA_BASE_ANDROID_MEDIA_CODEC_BRIDGE_H_ 228 #endif // MEDIA_BASE_ANDROID_MEDIA_CODEC_BRIDGE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698