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

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

Issue 74563002: AndroidVideoEncodeAccelerator is born! (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 7 years, 1 month 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 | Annotate | Revision Log
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 <string> 9 #include <string>
10 10
(...skipping 26 matching lines...) Expand all
37 // Android MediaCodec class. For more information on Android MediaCodec, check 37 // Android MediaCodec class. For more information on Android MediaCodec, check
38 // http://developer.android.com/reference/android/media/MediaCodec.html 38 // http://developer.android.com/reference/android/media/MediaCodec.html
39 // Note: MediaCodec is only available on JB and greater. 39 // Note: MediaCodec is only available on JB and greater.
40 // Use AudioCodecBridge or VideoCodecBridge to create an instance of this 40 // Use AudioCodecBridge or VideoCodecBridge to create an instance of this
41 // object. 41 // object.
42 class MEDIA_EXPORT MediaCodecBridge { 42 class MEDIA_EXPORT MediaCodecBridge {
43 public: 43 public:
44 // Returns true if MediaCodec is available on the device. 44 // Returns true if MediaCodec is available on the device.
45 static bool IsAvailable(); 45 static bool IsAvailable();
46 46
47 // Returns true if MediaCodec.setParameters() is available on the device.
48 static bool SupportsSetParameters();
49
47 // Returns whether MediaCodecBridge has a decoder that |is_secure| and can 50 // Returns whether MediaCodecBridge has a decoder that |is_secure| and can
48 // decode |codec| type. 51 // decode |codec| type.
49 static bool CanDecode(const std::string& codec, bool is_secure); 52 static bool CanDecode(const std::string& codec, bool is_secure);
50 53
51 // Represents supported codecs on android. 54 // Represents supported codecs on android.
52 // TODO(qinmin): Curretly the codecs string only contains one codec, do we 55 // TODO(qinmin): Curretly the codecs string only contains one codec, do we
53 // need more specific codecs separated by comma. (e.g. "vp8" -> "vp8, vp8.0") 56 // need more specific codecs separated by comma. (e.g. "vp8" -> "vp8, vp8.0")
54 struct CodecsInfo { 57 struct CodecsInfo {
55 std::string codecs; 58 std::string codecs; // E.g. "vp8" or "avc1".
56 std::string name; 59 std::string name; // E.g. "OMX.google.vp8.decoder".
60 bool is_encoder;
57 }; 61 };
58 62
59 // Get a list of supported codecs. 63 // Get a list of supported codecs.
60 static void GetCodecsInfo(std::vector<CodecsInfo>* codecs_info); 64 static std::vector<CodecsInfo> GetCodecsInfo();
61 65
62 virtual ~MediaCodecBridge(); 66 virtual ~MediaCodecBridge();
63 67
64 // Resets both input and output, all indices previously returned in calls to 68 // Resets both input and output, all indices previously returned in calls to
65 // DequeueInputBuffer() and DequeueOutputBuffer() become invalid. 69 // DequeueInputBuffer() and DequeueOutputBuffer() become invalid.
66 // Please note that this clears all the inputs in the media codec. In other 70 // Please note that this clears all the inputs in the media codec. In other
67 // words, there will be no outputs until new input is provided. 71 // words, there will be no outputs until new input is provided.
68 // Returns MEDIA_CODEC_ERROR if an unexpected error happens, or Media_CODEC_OK 72 // Returns MEDIA_CODEC_ERROR if an unexpected error happens, or Media_CODEC_OK
69 // otherwise. 73 // otherwise.
70 MediaCodecStatus Reset(); 74 MediaCodecStatus Reset();
71 75
72 // Finishes the decode/encode session. The instance remains active 76 // Finishes the decode/encode session. The instance remains active
73 // and ready to be StartAudio/Video()ed again. HOWEVER, due to the buggy 77 // and ready to be StartAudio/Video()ed again. HOWEVER, due to the buggy
74 // vendor's implementation , b/8125974, Stop() -> StartAudio/Video() may not 78 // vendor's implementation , b/8125974, Stop() -> StartAudio/Video() may not
75 // work on some devices. For reliability, Stop() -> delete and recreate new 79 // work on some devices. For reliability, Stop() -> delete and recreate new
76 // instance -> StartAudio/Video() is recommended. 80 // instance -> StartAudio/Video() is recommended.
77 void Stop(); 81 void Stop();
78 82
79 // Used for getting output format. This is valid after DequeueInputBuffer() 83 // Used for getting output format. This is valid after DequeueInputBuffer()
80 // returns a format change by returning INFO_OUTPUT_FORMAT_CHANGED 84 // returns a format change by returning INFO_OUTPUT_FORMAT_CHANGED
81 void GetOutputFormat(int* width, int* height); 85 void GetOutputFormat(int* width, int* height);
82 86
87 // Returns the number of input buffers used by the codec.
88 int GetInputBuffersCount();
89
83 // Submits a byte array to the given input buffer. Call this after getting an 90 // Submits a byte array to the given input buffer. Call this after getting an
84 // available buffer from DequeueInputBuffer(). 91 // available buffer from DequeueInputBuffer(). If |data| is NULL, assume the
92 // input buffer has already been populated (but still obey |size|).
85 MediaCodecStatus QueueInputBuffer(int index, 93 MediaCodecStatus QueueInputBuffer(int index,
86 const uint8* data, 94 const uint8* data,
87 int size, 95 int size,
88 const base::TimeDelta& presentation_time); 96 const base::TimeDelta& presentation_time);
89 97
90 // Similar to the above call, but submits a buffer that is encrypted. 98 // Similar to the above call, but submits a buffer that is encrypted.
91 // Note: NULL |subsamples| indicates the whole buffer is encrypted. 99 // Note: NULL |subsamples| indicates the whole buffer is encrypted. If |data|
100 // is NULL, assume the input buffer has already been populated (but still obey
101 // |data_size|).
92 MediaCodecStatus QueueSecureInputBuffer( 102 MediaCodecStatus QueueSecureInputBuffer(
93 int index, 103 int index,
94 const uint8* data, int data_size, 104 const uint8* data, int data_size,
95 const uint8* key_id, int key_id_size, 105 const uint8* key_id, int key_id_size,
96 const uint8* iv, int iv_size, 106 const uint8* iv, int iv_size,
97 const SubsampleEntry* subsamples, int subsamples_size, 107 const SubsampleEntry* subsamples, int subsamples_size,
98 const base::TimeDelta& presentation_time); 108 const base::TimeDelta& presentation_time);
99 109
100 // Submits an empty buffer with a EOS (END OF STREAM) flag. 110 // Submits an empty buffer with a EOS (END OF STREAM) flag.
101 void QueueEOS(int input_buffer_index); 111 void QueueEOS(int input_buffer_index);
102 112
103 // Returns: 113 // Returns:
104 // MEDIA_CODEC_OK if an input buffer is ready to be filled with valid data, 114 // MEDIA_CODEC_OK if an input buffer is ready to be filled with valid data,
105 // MEDIA_CODEC_ENQUEUE_INPUT_AGAIN_LATER if no such buffer is available, or 115 // MEDIA_CODEC_ENQUEUE_INPUT_AGAIN_LATER if no such buffer is available, or
106 // MEDIA_CODEC_ERROR if unexpected error happens. 116 // MEDIA_CODEC_ERROR if unexpected error happens.
107 // Note: Never use infinite timeout as this would block the decoder thread and 117 // Note: Never use infinite timeout as this would block the decoder thread and
108 // prevent the decoder job from being released. 118 // prevent the decoder job from being released.
109 MediaCodecStatus DequeueInputBuffer(const base::TimeDelta& timeout, 119 MediaCodecStatus DequeueInputBuffer(const base::TimeDelta& timeout,
110 int* index); 120 int* index);
111 121
112 // Dequeues an output buffer, block at most timeout_us microseconds. 122 // Dequeues an output buffer, block at most timeout_us microseconds.
113 // Returns the status of this operation. If OK is returned, the output 123 // Returns the status of this operation. If OK is returned, the output
114 // parameters should be populated. Otherwise, the values of output parameters 124 // parameters should be populated. Otherwise, the values of output parameters
115 // should not be used. 125 // should not be used. Output parameters other than index/offset/size are
126 // optional and only set if not NULL.
116 // Note: Never use infinite timeout as this would block the decoder thread and 127 // Note: Never use infinite timeout as this would block the decoder thread and
117 // prevent the decoder job from being released. 128 // prevent the decoder job from being released.
118 // TODO(xhwang): Can we drop |end_of_stream| and return 129 // TODO(xhwang): Can we drop |end_of_stream| and return
119 // MEDIA_CODEC_OUTPUT_END_OF_STREAM? 130 // MEDIA_CODEC_OUTPUT_END_OF_STREAM?
120 MediaCodecStatus DequeueOutputBuffer(const base::TimeDelta& timeout, 131 MediaCodecStatus DequeueOutputBuffer(const base::TimeDelta& timeout,
121 int* index, 132 int* index, size_t* offset, size_t* size,
122 size_t* offset,
123 size_t* size,
124 base::TimeDelta* presentation_time, 133 base::TimeDelta* presentation_time,
125 bool* end_of_stream); 134 bool* end_of_stream, bool* key_frame);
126 135
127 // Returns the buffer to the codec. If you previously specified a surface 136 // Returns the buffer to the codec. If you previously specified a surface when
128 // when configuring this video decoder you can optionally render the buffer. 137 // configuring this video decoder you can
138 // optionally render the buffer.
xhwang 2013/11/19 00:11:25 nit: merge with the previous line?
Ami GONE FROM CHROMIUM 2013/11/21 22:59:07 Done.
129 void ReleaseOutputBuffer(int index, bool render); 139 void ReleaseOutputBuffer(int index, bool render);
130 140
141 // Returns the number of output buffers used by the codec.
142 int GetOutputBuffersCount();
143
144 // Returns the capacity of each output buffer used by the codec.
145 size_t GetOutputBuffersCapacity();
146
131 // Gets output buffers from media codec and keeps them inside the java class. 147 // Gets output buffers from media codec and keeps them inside the java class.
132 // To access them, use DequeueOutputBuffer(). Returns whether output buffers 148 // To access them, use DequeueOutputBuffer(). Returns whether output buffers
133 // were successfully obtained. 149 // were successfully obtained.
134 bool GetOutputBuffers() WARN_UNUSED_RESULT; 150 bool GetOutputBuffers() WARN_UNUSED_RESULT;
135 151
152 // Returns an input buffers' base pointer and capacity.
xhwang 2013/11/19 00:11:25 s/buffers'/buffer's
Ami GONE FROM CHROMIUM 2013/11/21 22:59:07 Done.
153 void GetInputBuffer(int input_buffer_index, uint8** data, size_t* capacity);
154
155 // Copy |dst_size| bytes from output buffer |index|'s |offset| onwards into
156 // |*dst|.
157 bool CopyFromOutputBuffer(int index, size_t offset, void* dst, int dst_size);
158
136 static bool RegisterMediaCodecBridge(JNIEnv* env); 159 static bool RegisterMediaCodecBridge(JNIEnv* env);
137 160
138 protected: 161 protected:
139 // Returns true if |mime_type| is known to be unaccelerated (i.e. backed by a 162 // Returns true if |mime_type| is known to be unaccelerated (i.e. backed by a
140 // software codec instead of a hardware one). 163 // software codec instead of a hardware one).
141 static bool IsKnownUnaccelerated(const std::string& mime_type); 164 static bool IsKnownUnaccelerated(const std::string& mime_type,
165 bool is_encoder);
142 166
143 MediaCodecBridge(const std::string& mime, bool is_secure); 167 MediaCodecBridge(const std::string& mime, bool is_secure, bool is_encoder);
144 168
145 // Calls start() against the media codec instance. Used in StartXXX() after 169 // Calls start() against the media codec instance. Used in StartXXX() after
146 // configuring media codec. Returns whether media codec was successfully 170 // configuring media codec. Returns whether media codec was successfully
147 // started. 171 // started.
148 bool StartInternal() WARN_UNUSED_RESULT; 172 bool StartInternal() WARN_UNUSED_RESULT;
149 173
150 jobject media_codec() { return j_media_codec_.obj(); } 174 jobject media_codec() { return j_media_codec_.obj(); }
175 bool is_encoder_;
151 176
152 private: 177 private:
153 // Fills a particular input buffer; returns false if |data_size| exceeds the 178 // Fills a particular input buffer; returns false if |data_size| exceeds the
154 // input buffer's capacity (and doesn't touch the input buffer in that case). 179 // input buffer's capacity (and doesn't touch the input buffer in that case).
155 bool FillInputBuffer(int index, 180 bool FillInputBuffer(int index, const uint8* data,
156 const uint8* data, 181 size_t data_size) WARN_UNUSED_RESULT;
157 int data_size) WARN_UNUSED_RESULT;
158 182
159 // Java MediaCodec instance. 183 // Java MediaCodec instance.
160 base::android::ScopedJavaGlobalRef<jobject> j_media_codec_; 184 base::android::ScopedJavaGlobalRef<jobject> j_media_codec_;
161 185
162 DISALLOW_COPY_AND_ASSIGN(MediaCodecBridge); 186 DISALLOW_COPY_AND_ASSIGN(MediaCodecBridge);
163 }; 187 };
164 188
165 class AudioCodecBridge : public MediaCodecBridge { 189 class AudioCodecBridge : public MediaCodecBridge {
166 public: 190 public:
167 // Returns an AudioCodecBridge instance if |codec| is supported, or a NULL 191 // Returns an AudioCodecBridge instance if |codec| is supported, or a NULL
(...skipping 18 matching lines...) Expand all
186 private: 210 private:
187 explicit AudioCodecBridge(const std::string& mime); 211 explicit AudioCodecBridge(const std::string& mime);
188 212
189 // Configure the java MediaFormat object with the extra codec data passed in. 213 // Configure the java MediaFormat object with the extra codec data passed in.
190 bool ConfigureMediaFormat(jobject j_format, const AudioCodec& codec, 214 bool ConfigureMediaFormat(jobject j_format, const AudioCodec& codec,
191 const uint8* extra_data, size_t extra_data_size); 215 const uint8* extra_data, size_t extra_data_size);
192 }; 216 };
193 217
194 class MEDIA_EXPORT VideoCodecBridge : public MediaCodecBridge { 218 class MEDIA_EXPORT VideoCodecBridge : public MediaCodecBridge {
195 public: 219 public:
196 // Returns an VideoCodecBridge instance if |codec| is supported, or a NULL 220 // See MediaCodecBridge::IsKnownUnaccelerated().
197 // pointer otherwise. 221 static bool IsKnownUnaccelerated(const VideoCodec& codec, bool is_encoder);
198 static VideoCodecBridge* Create(const VideoCodec& codec, bool is_secure);
199 222
200 // See MediaCodecBridge::IsKnownUnaccelerated(). 223 // Create, start, and return a VideoCodecBridge decoder or NULL on failure.
201 static bool IsKnownUnaccelerated(const VideoCodec& codec); 224 static VideoCodecBridge* CreateDecoder(
225 const VideoCodec& codec, // e.g. media::kCodecVP8
226 bool is_secure, const gfx::Size& size, // Output frame size.
227 jobject surface, // Output surface, optional.
228 jobject media_crypto); // MediaCrypto object, optional.
202 229
203 // Start the video codec bridge. 230 // Create, start, and return a VideoCodecBridge encoder or NULL on failure.
204 // TODO(qinmin): Pass codec specific data if available. 231 static VideoCodecBridge* CreateEncoder(
205 bool Start(const VideoCodec& codec, const gfx::Size& size, jobject surface, 232 const VideoCodec& codec, // e.g. media::kCodecVP8
206 jobject media_crypto); 233 const gfx::Size& size, // input frame size
234 int bit_rate, // bits/second
235 int frame_rate, // frames/second
236 int i_frame_interval, // count
237 int color_format); // MediaCodecInfo.CodecCapabilities.
238
239 void SetVideoBitrate(int bps);
240 void RequestKeyFrameSoon();
207 241
208 private: 242 private:
209 VideoCodecBridge(const std::string& mime, bool is_secure); 243 VideoCodecBridge(const std::string& mime, bool is_secure, bool is_encoder);
210 }; 244 };
211 245
212 } // namespace media 246 } // namespace media
213 247
214 #endif // MEDIA_BASE_ANDROID_MEDIA_CODEC_BRIDGE_H_ 248 #endif // MEDIA_BASE_ANDROID_MEDIA_CODEC_BRIDGE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698