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

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

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

Powered by Google App Engine
This is Rietveld 408576698