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

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: addressing comments 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>
Tima Vaisburd 2015/12/04 02:04:48 <jni.h> is not needed any more?
qinmin 2015/12/04 20:26:04 Done.
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/android/media_codec_util.h"
15 #include "media/base/video_decoder_config.h" 16 #include "media/base/media_export.h"
16 #include "ui/gfx/geometry/size.h"
17 17
18 namespace media { 18 namespace media {
19 19
20 struct SubsampleEntry; 20 struct SubsampleEntry;
21 21
22 // These must be in sync with MediaCodecBridge.MEDIA_CODEC_XXX constants in 22 // These must be in sync with MediaCodecBridge.MEDIA_CODEC_XXX constants in
23 // MediaCodecBridge.java. 23 // MediaCodecBridge.java.
24 enum MediaCodecStatus { 24 enum MediaCodecStatus {
25 MEDIA_CODEC_OK, 25 MEDIA_CODEC_OK,
26 MEDIA_CODEC_DEQUEUE_INPUT_AGAIN_LATER, 26 MEDIA_CODEC_DEQUEUE_INPUT_AGAIN_LATER,
27 MEDIA_CODEC_DEQUEUE_OUTPUT_AGAIN_LATER, 27 MEDIA_CODEC_DEQUEUE_OUTPUT_AGAIN_LATER,
28 MEDIA_CODEC_OUTPUT_BUFFERS_CHANGED, 28 MEDIA_CODEC_OUTPUT_BUFFERS_CHANGED,
29 MEDIA_CODEC_OUTPUT_FORMAT_CHANGED, 29 MEDIA_CODEC_OUTPUT_FORMAT_CHANGED,
30 MEDIA_CODEC_INPUT_END_OF_STREAM, 30 MEDIA_CODEC_INPUT_END_OF_STREAM,
31 MEDIA_CODEC_OUTPUT_END_OF_STREAM, 31 MEDIA_CODEC_OUTPUT_END_OF_STREAM,
32 MEDIA_CODEC_NO_KEY, 32 MEDIA_CODEC_NO_KEY,
33 MEDIA_CODEC_ABORT, 33 MEDIA_CODEC_ABORT,
34 MEDIA_CODEC_ERROR 34 MEDIA_CODEC_ERROR
35 }; 35 };
36 36
37 // Codec direction. Keep this in sync with MediaCodecBridge.java. 37 // Interface for wrapping different Android MediaCodec implementations. For
38 enum MediaCodecDirection { 38 // more information on Android MediaCodec, check
39 MEDIA_CODEC_DECODER,
40 MEDIA_CODEC_ENCODER,
41 };
42
43 // This class serves as a bridge for native code to call java functions inside
44 // Android MediaCodec class. For more information on Android MediaCodec, check
45 // http://developer.android.com/reference/android/media/MediaCodec.html 39 // http://developer.android.com/reference/android/media/MediaCodec.html
46 // Note: MediaCodec is only available on JB and greater. 40 // 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 { 41 class MEDIA_EXPORT MediaCodecBridge {
53 public: 42 public:
54 // Returns true if MediaCodec is available on the device.
55 // All other static methods check IsAvailable() internally. There's no need
56 // to check IsAvailable() explicitly before calling them.
57 static bool IsAvailable();
58
59 // Returns true if MediaCodec.setParameters() is available on the device.
60 static bool SupportsSetParameters();
61
62 // Returns true if MediaCodec.getName() is available on the device.
63 static bool SupportsGetName();
64
65 // Returns whether MediaCodecBridge has a decoder that |is_secure| and can
66 // decode |codec| type.
67 static bool CanDecode(const std::string& codec, bool is_secure);
68
69 // Represents supported codecs on android.
70 // TODO(qinmin): Currently the codecs string only contains one codec. Do we
71 // need to support codecs separated by comma. (e.g. "vp8" -> "vp8, vp8.0")?
72 struct CodecsInfo {
73 std::string codecs; // E.g. "vp8" or "avc1".
74 std::string name; // E.g. "OMX.google.vp8.decoder".
75 MediaCodecDirection direction;
76 };
77
78 // Get a list of supported codecs.
79 static std::vector<CodecsInfo> GetCodecsInfo();
80
81 // Get default codec name for |mime_type|.
82 static std::string GetDefaultCodecName(const std::string& mime_type,
83 MediaCodecDirection direction);
84
85 // Get a list of encoder supported color formats for |mime_type|.
86 // The mapping of color format name and its value refers to
87 // MediaCodecInfo.CodecCapabilities.
88 static std::set<int> GetEncoderColorFormats(const std::string& mime_type);
89
90 virtual ~MediaCodecBridge(); 43 virtual ~MediaCodecBridge();
91 44
92 // Resets both input and output, all indices previously returned in calls to 45 // Resets both input and output, all indices previously returned in calls to
93 // DequeueInputBuffer() and DequeueOutputBuffer() become invalid. 46 // DequeueInputBuffer() and DequeueOutputBuffer() become invalid.
94 // Please note that this clears all the inputs in the media codec. In other 47 // 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. 48 // 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 49 // Returns MEDIA_CODEC_ERROR if an unexpected error happens, or Media_CODEC_OK
97 // otherwise. 50 // otherwise.
98 MediaCodecStatus Reset(); 51 virtual MediaCodecStatus Reset() = 0;
52
53 // Calls start() against the media codec instance. Returns whether media
54 // codec was successfully started.
55 virtual bool Start() WARN_UNUSED_RESULT = 0;
Tima Vaisburd 2015/12/04 02:04:48 Can we remove WARN_UNUSED_RESULT here?
qinmin 2015/12/04 20:26:04 Done.
99 56
100 // Finishes the decode/encode session. The instance remains active 57 // Finishes the decode/encode session. The instance remains active
101 // and ready to be StartAudio/Video()ed again. HOWEVER, due to the buggy 58 // and ready to be StartAudio/Video()ed again. HOWEVER, due to the buggy
102 // vendor's implementation , b/8125974, Stop() -> StartAudio/Video() may not 59 // vendor's implementation , b/8125974, Stop() -> StartAudio/Video() may not
103 // work on some devices. For reliability, Stop() -> delete and recreate new 60 // work on some devices. For reliability, Stop() -> delete and recreate new
104 // instance -> StartAudio/Video() is recommended. 61 // instance -> StartAudio/Video() is recommended.
105 void Stop(); 62 virtual void Stop() = 0;
106 63
107 // Used for getting output format. This is valid after DequeueInputBuffer() 64 // Used for getting output format. This is valid after DequeueInputBuffer()
108 // returns a format change by returning INFO_OUTPUT_FORMAT_CHANGED 65 // returns a format change by returning INFO_OUTPUT_FORMAT_CHANGED
109 void GetOutputFormat(int* width, int* height); 66 virtual void GetOutputFormat(int* width, int* height) = 0;
110 67
111 // Used for checking for new sampling rate after DequeueInputBuffer() returns 68 // Used for checking for new sampling rate after DequeueInputBuffer() returns
112 // INFO_OUTPUT_FORMAT_CHANGED 69 // INFO_OUTPUT_FORMAT_CHANGED
113 int GetOutputSamplingRate(); 70 virtual int GetOutputSamplingRate() = 0;
114 71
115 // Submits a byte array to the given input buffer. Call this after getting an 72 // 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 73 // available buffer from DequeueInputBuffer(). If |data| is NULL, assume the
117 // input buffer has already been populated (but still obey |size|). 74 // input buffer has already been populated (but still obey |size|).
118 // |data_size| must be less than kint32max (because Java). 75 // |data_size| must be less than kint32max (because Java).
119 MediaCodecStatus QueueInputBuffer(int index, 76 virtual MediaCodecStatus QueueInputBuffer(
120 const uint8* data, 77 int index,
121 size_t data_size, 78 const uint8* data,
122 const base::TimeDelta& presentation_time); 79 size_t data_size,
80 const base::TimeDelta& presentation_time) = 0;
123 81
124 // Similar to the above call, but submits a buffer that is encrypted. Note: 82 // 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 83 // NULL |subsamples| indicates the whole buffer is encrypted. If |data| is
126 // NULL, assume the input buffer has already been populated (but still obey 84 // NULL, assume the input buffer has already been populated (but still obey
127 // |data_size|). |data_size| must be less than kint32max (because Java). 85 // |data_size|). |data_size| must be less than kint32max (because Java).
128 MediaCodecStatus QueueSecureInputBuffer( 86 MediaCodecStatus QueueSecureInputBuffer(
129 int index, 87 int index,
130 const uint8* data, 88 const uint8* data,
131 size_t data_size, 89 size_t data_size,
132 const std::string& key_id, 90 const std::string& key_id,
133 const std::string& iv, 91 const std::string& iv,
134 const std::vector<SubsampleEntry>& subsamples, 92 const std::vector<SubsampleEntry>& subsamples,
135 const base::TimeDelta& presentation_time); 93 const base::TimeDelta& presentation_time);
136 94
137 // Same QueueSecureInputBuffer overriden for the use with MediaSourcePlayer 95 // Same QueueSecureInputBuffer overriden for the use with MediaSourcePlayer
138 // and MediaCodecPlayer. 96 // and MediaCodecPlayer.
139 // TODO(timav): remove this method and keep only the one above after we 97 // TODO(timav): remove this method and keep only the one above after we
140 // switch to the Spitzer pipeline. 98 // switch to the Spitzer pipeline.
141 MediaCodecStatus QueueSecureInputBuffer( 99 virtual MediaCodecStatus QueueSecureInputBuffer(
142 int index, 100 int index,
143 const uint8* data, 101 const uint8* data,
144 size_t data_size, 102 size_t data_size,
145 const uint8* key_id, 103 std::vector<char> key_id,
146 int key_id_size, 104 std::vector<char> iv,
xhwang 2015/12/04 18:07:58 pass by const ref also, it seems these two are pa
qinmin 2015/12/04 20:26:04 We already have the string version defined above.
147 const uint8* iv,
148 int iv_size,
149 const SubsampleEntry* subsamples, 105 const SubsampleEntry* subsamples,
150 int subsamples_size, 106 int subsamples_size,
151 const base::TimeDelta& presentation_time); 107 const base::TimeDelta& presentation_time) = 0;
152 108
153 // Submits an empty buffer with a EOS (END OF STREAM) flag. 109 // Submits an empty buffer with a EOS (END OF STREAM) flag.
154 void QueueEOS(int input_buffer_index); 110 virtual void QueueEOS(int input_buffer_index) = 0;
155 111
156 // Returns: 112 // Returns:
157 // MEDIA_CODEC_OK if an input buffer is ready to be filled with valid data, 113 // 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 114 // MEDIA_CODEC_ENQUEUE_INPUT_AGAIN_LATER if no such buffer is available, or
159 // MEDIA_CODEC_ERROR if unexpected error happens. 115 // MEDIA_CODEC_ERROR if unexpected error happens.
160 // Note: Never use infinite timeout as this would block the decoder thread and 116 // Note: Never use infinite timeout as this would block the decoder thread and
161 // prevent the decoder job from being released. 117 // prevent the decoder job from being released.
162 MediaCodecStatus DequeueInputBuffer(const base::TimeDelta& timeout, 118 virtual MediaCodecStatus DequeueInputBuffer(const base::TimeDelta& timeout,
163 int* index); 119 int* index) = 0;
164 120
165 // Dequeues an output buffer, block at most timeout_us microseconds. 121 // Dequeues an output buffer, block at most timeout_us microseconds.
166 // Returns the status of this operation. If OK is returned, the output 122 // Returns the status of this operation. If OK is returned, the output
167 // parameters should be populated. Otherwise, the values of output parameters 123 // parameters should be populated. Otherwise, the values of output parameters
168 // should not be used. Output parameters other than index/offset/size are 124 // should not be used. Output parameters other than index/offset/size are
169 // optional and only set if not NULL. 125 // optional and only set if not NULL.
170 // Note: Never use infinite timeout as this would block the decoder thread and 126 // Note: Never use infinite timeout as this would block the decoder thread and
171 // prevent the decoder job from being released. 127 // prevent the decoder job from being released.
172 // TODO(xhwang): Can we drop |end_of_stream| and return 128 // TODO(xhwang): Can we drop |end_of_stream| and return
173 // MEDIA_CODEC_OUTPUT_END_OF_STREAM? 129 // MEDIA_CODEC_OUTPUT_END_OF_STREAM?
174 MediaCodecStatus DequeueOutputBuffer(const base::TimeDelta& timeout, 130 virtual MediaCodecStatus DequeueOutputBuffer(
175 int* index, 131 const base::TimeDelta& timeout,
176 size_t* offset, 132 int* index,
177 size_t* size, 133 size_t* offset,
178 base::TimeDelta* presentation_time, 134 size_t* size,
179 bool* end_of_stream, 135 base::TimeDelta* presentation_time,
180 bool* key_frame); 136 bool* end_of_stream,
137 bool* key_frame) = 0;
181 138
182 // Returns the buffer to the codec. If you previously specified a surface when 139 // Returns the buffer to the codec. If you previously specified a surface when
183 // configuring this video decoder you can optionally render the buffer. 140 // configuring this video decoder you can optionally render the buffer.
184 void ReleaseOutputBuffer(int index, bool render); 141 virtual void ReleaseOutputBuffer(int index, bool render) = 0;
185 142
186 // Returns the number of output buffers used by the codec. 143 // Returns the number of output buffers used by the codec.
187 // TODO(qinmin): this call is deprecated in Lollipop. 144 // TODO(qinmin): this call is deprecated in Lollipop.
188 int GetOutputBuffersCount(); 145 virtual int GetOutputBuffersCount();
189 146
190 // Returns the capacity of each output buffer used by the codec. 147 // Returns the capacity of each output buffer used by the codec.
191 // TODO(qinmin): this call is deprecated in Lollipop. 148 // TODO(qinmin): this call is deprecated in Lollipop.
192 size_t GetOutputBuffersCapacity(); 149 virtual size_t GetOutputBuffersCapacity();
193 150
194 // Returns an input buffer's base pointer and capacity. 151 // Returns an input buffer's base pointer and capacity.
195 void GetInputBuffer(int input_buffer_index, uint8** data, size_t* capacity); 152 virtual void GetInputBuffer(int input_buffer_index,
153 uint8** data,
154 size_t* capacity) = 0;
196 155
197 // Copy |dst_size| bytes from output buffer |index|'s |offset| onwards into 156 // Copy |dst_size| bytes from output buffer |index|'s |offset| onwards into
198 // |*dst|. 157 // |*dst|.
199 bool CopyFromOutputBuffer(int index, size_t offset, void* dst, int dst_size); 158 virtual bool CopyFromOutputBuffer(int index,
200 159 size_t offset,
201 static bool RegisterMediaCodecBridge(JNIEnv* env); 160 void* dst,
161 int dst_size) = 0;
202 162
203 protected: 163 protected:
204 // Returns true if |mime_type| is known to be unaccelerated (i.e. backed by a 164 MediaCodecBridge();
205 // software codec instead of a hardware one).
206 static bool IsKnownUnaccelerated(const std::string& mime_type,
207 MediaCodecDirection direction);
208 165
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 166 // 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). 167 // input buffer's capacity (and doesn't touch the input buffer in that case).
229 bool FillInputBuffer(int index, 168 bool FillInputBuffer(int index,
230 const uint8* data, 169 const uint8* data,
231 size_t data_size) WARN_UNUSED_RESULT; 170 size_t data_size) WARN_UNUSED_RESULT;
232 171
233 // Java MediaCodec instance.
234 base::android::ScopedJavaGlobalRef<jobject> j_media_codec_;
235
236 DISALLOW_COPY_AND_ASSIGN(MediaCodecBridge); 172 DISALLOW_COPY_AND_ASSIGN(MediaCodecBridge);
237 }; 173 };
238 174
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 175 } // namespace media
328 176
329 #endif // MEDIA_BASE_ANDROID_MEDIA_CODEC_BRIDGE_H_ 177 #endif // MEDIA_BASE_ANDROID_MEDIA_CODEC_BRIDGE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698