OLD | NEW |
---|---|
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 <stddef.h> | 8 #include <stddef.h> |
9 #include <stdint.h> | 9 #include <stdint.h> |
10 | 10 |
11 #include <set> | |
12 #include <string> | 11 #include <string> |
13 #include <vector> | |
14 | 12 |
13 #include "base/android/scoped_java_ref.h" | |
15 #include "base/compiler_specific.h" | 14 #include "base/compiler_specific.h" |
16 #include "base/macros.h" | 15 #include "base/macros.h" |
17 #include "base/time/time.h" | 16 #include "base/time/time.h" |
17 #include "media/base/android/media_codec_bridge.h" | |
18 #include "media/base/android/media_codec_direction.h" | 18 #include "media/base/android/media_codec_direction.h" |
19 #include "media/base/audio_decoder_config.h" | |
19 #include "media/base/media_export.h" | 20 #include "media/base/media_export.h" |
21 #include "media/base/video_decoder_config.h" | |
20 #include "ui/gfx/geometry/size.h" | 22 #include "ui/gfx/geometry/size.h" |
21 | 23 |
22 namespace media { | 24 namespace media { |
23 | 25 |
24 class EncryptionScheme; | 26 class EncryptionScheme; |
25 struct SubsampleEntry; | 27 struct SubsampleEntry; |
26 | 28 |
27 // These must be in sync with MediaCodecBridge.MEDIA_CODEC_XXX constants in | 29 // These must be in sync with MediaCodecBridge.MEDIA_CODEC_XXX constants in |
28 // MediaCodecBridge.java. | 30 // MediaCodecBridge.java. |
29 enum MediaCodecStatus { | 31 enum MediaCodecStatus { |
30 MEDIA_CODEC_OK, | 32 MEDIA_CODEC_OK, |
31 MEDIA_CODEC_DEQUEUE_INPUT_AGAIN_LATER, | 33 MEDIA_CODEC_DEQUEUE_INPUT_AGAIN_LATER, |
32 MEDIA_CODEC_DEQUEUE_OUTPUT_AGAIN_LATER, | 34 MEDIA_CODEC_DEQUEUE_OUTPUT_AGAIN_LATER, |
33 MEDIA_CODEC_OUTPUT_BUFFERS_CHANGED, | 35 MEDIA_CODEC_OUTPUT_BUFFERS_CHANGED, |
34 MEDIA_CODEC_OUTPUT_FORMAT_CHANGED, | 36 MEDIA_CODEC_OUTPUT_FORMAT_CHANGED, |
35 MEDIA_CODEC_INPUT_END_OF_STREAM, | |
36 MEDIA_CODEC_OUTPUT_END_OF_STREAM, | |
37 MEDIA_CODEC_NO_KEY, | 37 MEDIA_CODEC_NO_KEY, |
38 MEDIA_CODEC_ABORT, | |
39 MEDIA_CODEC_ERROR | 38 MEDIA_CODEC_ERROR |
40 }; | 39 }; |
41 | 40 |
42 // Interface for wrapping different Android MediaCodec implementations. For | 41 // A wrapper around a Java MediaCodecBridge. |
43 // more information on Android MediaCodec, check | |
44 // http://developer.android.com/reference/android/media/MediaCodec.html | |
45 // Note: MediaCodec is only available on JB and greater. | |
46 class MEDIA_EXPORT MediaCodecBridge { | 42 class MEDIA_EXPORT MediaCodecBridge { |
47 public: | 43 public: |
48 virtual ~MediaCodecBridge(); | 44 virtual ~MediaCodecBridge(); |
49 | 45 |
50 // Calls start() against the media codec instance. Returns whether media | 46 // Calls start() against the media codec instance. Returns whether media |
51 // codec was successfully started. | 47 // codec was successfully started. |
52 virtual bool Start() = 0; | 48 virtual bool Start(); |
53 | 49 |
54 // Finishes the decode/encode session. The instance remains active | 50 // Finishes the decode/encode session. The instance remains active |
55 // and ready to be StartAudio/Video()ed again. HOWEVER, due to the buggy | 51 // and ready to be StartAudio/Video()ed again. HOWEVER, due to the buggy |
56 // vendor's implementation , b/8125974, Stop() -> StartAudio/Video() may not | 52 // vendor's implementation , b/8125974, Stop() -> StartAudio/Video() may not |
57 // work on some devices. For reliability, Stop() -> delete and recreate new | 53 // work on some devices. For reliability, Stop() -> delete and recreate new |
58 // instance -> StartAudio/Video() is recommended. | 54 // instance -> StartAudio/Video() is recommended. |
59 virtual void Stop() = 0; | 55 virtual void Stop(); |
60 | 56 |
61 // Calls flush() on the MediaCodec. All indices previously returned in calls | 57 // Calls flush() on the MediaCodec. All indices previously returned in calls |
62 // to DequeueInputBuffer() and DequeueOutputBuffer() become invalid. Please | 58 // to DequeueInputBuffer() and DequeueOutputBuffer() become invalid. Please |
63 // note that this clears all the inputs in the media codec. In other words, | 59 // note that this clears all the inputs in the media codec. In other words, |
64 // there will be no outputs until new input is provided. Returns | 60 // there will be no outputs until new input is provided. Returns |
65 // MEDIA_CODEC_ERROR if an unexpected error happens, or MEDIA_CODEC_OK | 61 // MEDIA_CODEC_ERROR if an unexpected error happens, or MEDIA_CODEC_OK |
66 // otherwise. | 62 // otherwise. |
67 virtual MediaCodecStatus Flush() = 0; | 63 virtual MediaCodecStatus Flush(); |
68 | 64 |
69 // Used for getting the output size. This is valid after DequeueInputBuffer() | 65 // Used for getting the output size. This is valid after DequeueInputBuffer() |
70 // returns a format change by returning INFO_OUTPUT_FORMAT_CHANGED. | 66 // returns a format change by returning INFO_OUTPUT_FORMAT_CHANGED. |
71 // Returns MEDIA_CODEC_ERROR if an error occurs, or MEDIA_CODEC_OK otherwise. | 67 // Returns MEDIA_CODEC_ERROR if an error occurs, or MEDIA_CODEC_OK otherwise. |
72 virtual MediaCodecStatus GetOutputSize(gfx::Size* size) = 0; | 68 virtual MediaCodecStatus GetOutputSize(gfx::Size* size); |
73 | 69 |
74 // Used for checking for new sampling rate after DequeueInputBuffer() returns | 70 // Used for checking for new sampling rate after DequeueInputBuffer() returns |
75 // INFO_OUTPUT_FORMAT_CHANGED | 71 // INFO_OUTPUT_FORMAT_CHANGED |
76 // Returns MEDIA_CODEC_ERROR if an error occurs, or MEDIA_CODEC_OK otherwise. | 72 // Returns MEDIA_CODEC_ERROR if an error occurs, or MEDIA_CODEC_OK otherwise. |
77 virtual MediaCodecStatus GetOutputSamplingRate(int* sampling_rate) = 0; | 73 virtual MediaCodecStatus GetOutputSamplingRate(int* sampling_rate); |
78 | 74 |
79 // Fills |channel_count| with the number of audio channels. Useful after | 75 // Fills |channel_count| with the number of audio channels. Useful after |
80 // INFO_OUTPUT_FORMAT_CHANGED. | 76 // INFO_OUTPUT_FORMAT_CHANGED. |
81 // Returns MEDIA_CODEC_ERROR if an error occurs, or MEDIA_CODEC_OK otherwise. | 77 // Returns MEDIA_CODEC_ERROR if an error occurs, or MEDIA_CODEC_OK otherwise. |
82 virtual MediaCodecStatus GetOutputChannelCount(int* channel_count) = 0; | 78 virtual MediaCodecStatus GetOutputChannelCount(int* channel_count); |
83 | 79 |
84 // Submits a byte array to the given input buffer. Call this after getting an | 80 // Submits a byte array to the given input buffer. Call this after getting an |
85 // available buffer from DequeueInputBuffer(). If |data| is NULL, assume the | 81 // available buffer from DequeueInputBuffer(). If |data| is NULL, assume the |
86 // input buffer has already been populated (but still obey |size|). | 82 // input buffer has already been populated (but still obey |size|). |
87 // |data_size| must be less than kint32max (because Java). | 83 // |data_size| must be less than kint32max (because Java). |
88 virtual MediaCodecStatus QueueInputBuffer( | 84 virtual MediaCodecStatus QueueInputBuffer(int index, |
85 const uint8_t* data, | |
86 size_t data_size, | |
87 base::TimeDelta presentation_time); | |
88 | |
89 // As above but for encrypted buffers. NULL |subsamples| indicates the | |
90 // whole buffer is encrypted. | |
91 virtual MediaCodecStatus QueueSecureInputBuffer( | |
89 int index, | 92 int index, |
90 const uint8_t* data, | 93 const uint8_t* data, |
91 size_t data_size, | 94 size_t data_size, |
92 base::TimeDelta presentation_time) = 0; | |
93 | |
94 // Similar to the above call, but submits a buffer that is encrypted. Note: | |
95 // NULL |subsamples| indicates the whole buffer is encrypted. If |data| is | |
96 // NULL, assume the input buffer has already been populated (but still obey | |
97 // |data_size|). |data_size| must be less than kint32max (because Java). | |
98 MediaCodecStatus QueueSecureInputBuffer( | |
99 int index, | |
100 const uint8_t* data, | |
101 size_t data_size, | |
102 const std::string& key_id, | 95 const std::string& key_id, |
103 const std::string& iv, | 96 const std::string& iv, |
104 const std::vector<SubsampleEntry>& subsamples, | 97 const std::vector<SubsampleEntry>& subsamples, |
105 const EncryptionScheme& encryption_scheme, | 98 const EncryptionScheme& encryption_scheme, |
106 base::TimeDelta presentation_time); | 99 base::TimeDelta presentation_time); |
107 | 100 |
108 // Same QueueSecureInputBuffer overriden for the use with | 101 // Submits an empty buffer with the END_OF_STREAM flag set. |
109 // AndroidVideoDecodeAccelerator and MediaCodecAudioDecoder. TODO(timav): | 102 virtual void QueueEOS(int input_buffer_index); |
110 // remove this method and keep only the one above after we switch to the | |
111 // Spitzer pipeline. | |
112 virtual MediaCodecStatus QueueSecureInputBuffer( | |
113 int index, | |
114 const uint8_t* data, | |
115 size_t data_size, | |
116 const std::vector<char>& key_id, | |
117 const std::vector<char>& iv, | |
118 const SubsampleEntry* subsamples, | |
119 int subsamples_size, | |
120 const EncryptionScheme& encryption_scheme, | |
121 base::TimeDelta presentation_time) = 0; | |
122 | |
123 // Submits an empty buffer with a EOS (END OF STREAM) flag. | |
124 virtual void QueueEOS(int input_buffer_index) = 0; | |
125 | 103 |
126 // Returns: | 104 // Returns: |
127 // MEDIA_CODEC_OK if an input buffer is ready to be filled with valid data, | 105 // MEDIA_CODEC_OK if an input buffer is ready to be filled with valid data, |
128 // MEDIA_CODEC_ENQUEUE_INPUT_AGAIN_LATER if no such buffer is available, or | 106 // MEDIA_CODEC_ENQUEUE_INPUT_AGAIN_LATER if no such buffer is available, or |
129 // MEDIA_CODEC_ERROR if unexpected error happens. | 107 // MEDIA_CODEC_ERROR if unexpected error happens. |
130 // Note: Never use infinite timeout as this would block the decoder thread and | |
131 // prevent the decoder job from being released. | |
132 virtual MediaCodecStatus DequeueInputBuffer(base::TimeDelta timeout, | 108 virtual MediaCodecStatus DequeueInputBuffer(base::TimeDelta timeout, |
133 int* index) = 0; | 109 int* index); |
134 | 110 |
135 // Dequeues an output buffer, block at most timeout_us microseconds. | 111 // Dequeues an output buffer, block for up to |timeout|. |
136 // Returns the status of this operation. If OK is returned, the output | 112 // Returns the status of this operation. If OK is returned, the output |
137 // parameters should be populated. Otherwise, the values of output parameters | 113 // parameters should be populated. Otherwise, the values of output parameters |
138 // should not be used. Output parameters other than index/offset/size are | 114 // should not be used. Output parameters other than index/offset/size are |
139 // optional and only set if not NULL. | 115 // optional and only set if not NULL. |
140 // Note: Never use infinite timeout as this would block the decoder thread and | |
141 // prevent the decoder job from being released. | |
142 // TODO(xhwang): Can we drop |end_of_stream| and return | |
143 // MEDIA_CODEC_OUTPUT_END_OF_STREAM? | |
144 virtual MediaCodecStatus DequeueOutputBuffer( | 116 virtual MediaCodecStatus DequeueOutputBuffer( |
145 base::TimeDelta timeout, | 117 base::TimeDelta timeout, |
146 int* index, | 118 int* index, |
147 size_t* offset, | 119 size_t* offset, |
148 size_t* size, | 120 size_t* size, |
149 base::TimeDelta* presentation_time, | 121 base::TimeDelta* presentation_time, |
150 bool* end_of_stream, | 122 bool* end_of_stream, |
151 bool* key_frame) = 0; | 123 bool* key_frame); |
152 | 124 |
153 // Returns the buffer to the codec. If you previously specified a surface when | 125 // Returns the buffer to the codec. If you previously specified a surface when |
154 // configuring this video decoder you can optionally render the buffer. | 126 // configuring this video decoder you can optionally render the buffer. |
155 virtual void ReleaseOutputBuffer(int index, bool render) = 0; | 127 virtual void ReleaseOutputBuffer(int index, bool render); |
156 | 128 |
157 // Returns an input buffer's base pointer and capacity. | 129 // Returns an input buffer's base pointer and capacity. |
158 virtual MediaCodecStatus GetInputBuffer(int input_buffer_index, | 130 virtual MediaCodecStatus GetInputBuffer(int input_buffer_index, |
159 uint8_t** data, | 131 uint8_t** data, |
160 size_t* capacity) = 0; | 132 size_t* capacity); |
161 | |
162 // Gives the access to buffer's data which is referenced by |index| and | |
163 // |offset|. The size of available data for reading is written to |*capacity| | |
164 // and the address is written to |*addr|. | |
165 // Returns MEDIA_CODEC_ERROR if a error occurs, or MEDIA_CODEC_OK otherwise. | |
166 virtual MediaCodecStatus GetOutputBufferAddress(int index, | |
167 size_t offset, | |
168 const uint8_t** addr, | |
169 size_t* capacity) = 0; | |
170 | 133 |
171 // Copies |num| bytes from output buffer |index|'s |offset| into the memory | 134 // Copies |num| bytes from output buffer |index|'s |offset| into the memory |
172 // region pointed to by |dst|. To avoid overflows, the size of both source | 135 // region pointed to by |dst|. To avoid overflows, the size of both source |
173 // and destination must be at least |num| bytes, and should not overlap. | 136 // and destination must be at least |num| bytes, and should not overlap. |
174 // Returns MEDIA_CODEC_ERROR if an error occurs, or MEDIA_CODEC_OK otherwise. | 137 // Returns MEDIA_CODEC_ERROR if an error occurs, or MEDIA_CODEC_OK otherwise. |
175 MediaCodecStatus CopyFromOutputBuffer(int index, | 138 MediaCodecStatus CopyFromOutputBuffer(int index, |
176 size_t offset, | 139 size_t offset, |
177 void* dst, | 140 void* dst, |
178 size_t num); | 141 size_t num); |
179 | 142 |
180 // Gets the component name. Before API level 18 this returns an empty string. | 143 // Gets the component name. Before API level 18 this returns an empty string. |
181 virtual std::string GetName() = 0; | 144 virtual std::string GetName(); |
182 | 145 |
183 protected: | 146 protected: |
147 // This constructor is only for making this class mockable. | |
liberato (no reviews please)
2017/02/07 06:19:43
personally, i think that this should still be an i
DaleCurtis
2017/02/07 20:08:01
Chrome is taking an active stance to reduce layers
liberato (no reviews please)
2017/02/07 20:37:30
won't we need virtual functions anyway to mock thi
watk
2017/02/08 02:53:57
I was intending to make it mockable either way (th
| |
184 MediaCodecBridge(); | 148 MediaCodecBridge(); |
149 MediaCodecBridge(const std::string& mime, | |
150 bool is_secure, | |
151 MediaCodecDirection direction, | |
152 bool require_software_codec); | |
185 | 153 |
186 // Fills a particular input buffer; returns false if |data_size| exceeds the | 154 jobject media_codec() { return j_media_codec_.obj(); } |
155 | |
156 MediaCodecDirection direction_; | |
157 | |
158 private: | |
159 // Fills the given input buffer. Returns false if |data_size| exceeds the | |
187 // input buffer's capacity (and doesn't touch the input buffer in that case). | 160 // input buffer's capacity (and doesn't touch the input buffer in that case). |
188 bool FillInputBuffer(int index, | 161 bool FillInputBuffer(int index, |
189 const uint8_t* data, | 162 const uint8_t* data, |
190 size_t data_size) WARN_UNUSED_RESULT; | 163 size_t data_size) WARN_UNUSED_RESULT; |
191 | 164 |
165 // Gets the address of the data in the given output buffer given by |index| | |
166 // and |offset|. The number of bytes available to read is written to | |
167 // |*capacity| and the address is written to |*addr|. Returns | |
168 // MEDIA_CODEC_ERROR if an error occurs, or MEDIA_CODEC_OK otherwise. | |
169 MediaCodecStatus GetOutputBufferAddress(int index, | |
170 size_t offset, | |
171 const uint8_t** addr, | |
172 size_t* capacity); | |
173 | |
174 // The Java MediaCodecBridge instance. | |
175 base::android::ScopedJavaGlobalRef<jobject> j_media_codec_; | |
176 | |
192 DISALLOW_COPY_AND_ASSIGN(MediaCodecBridge); | 177 DISALLOW_COPY_AND_ASSIGN(MediaCodecBridge); |
193 }; | 178 }; |
194 | 179 |
180 // A MediaCodec wrapper for audio decoding. | |
181 class MEDIA_EXPORT AudioCodecBridge : public MediaCodecBridge { | |
liberato (no reviews please)
2017/02/07 06:19:43
this and VideoCodecBridge should be in their own h
watk
2017/02/08 02:53:58
Will do in a follow up.
| |
182 public: | |
183 // See MediaCodecUtil::IsKnownUnaccelerated(). | |
184 static bool IsKnownUnaccelerated(const AudioCodec& codec); | |
185 | |
186 // Returns an AudioCodecBridge instance if |codec| is supported, or a NULL | |
187 // pointer otherwise. | |
188 static AudioCodecBridge* Create(const AudioCodec& codec); | |
189 | |
190 // Starts the audio codec bridge. | |
191 bool ConfigureAndStart(const AudioDecoderConfig& config, | |
192 jobject media_crypto) WARN_UNUSED_RESULT; | |
193 | |
194 bool ConfigureAndStart(const AudioCodec& codec, | |
195 int sample_rate, | |
196 int channel_count, | |
197 const uint8_t* extra_data, | |
198 size_t extra_data_size, | |
199 int64_t codec_delay_ns, | |
200 int64_t seek_preroll_ns, | |
201 jobject media_crypto) WARN_UNUSED_RESULT; | |
202 | |
203 private: | |
204 explicit AudioCodecBridge(const std::string& mime); | |
205 | |
206 // Configure the java MediaFormat object with the extra codec data passed in. | |
207 bool ConfigureMediaFormat(jobject j_format, | |
208 const AudioCodec& codec, | |
209 const uint8_t* extra_data, | |
210 size_t extra_data_size, | |
211 int64_t codec_delay_ns, | |
212 int64_t seek_preroll_ns); | |
213 }; | |
214 | |
215 // A MediaCodecBridge with for video encoding and decoding. | |
216 class MEDIA_EXPORT VideoCodecBridge : public MediaCodecBridge { | |
liberato (no reviews please)
2017/02/07 06:19:43
i keep going back and forth on these. on one hand
watk
2017/02/08 02:53:58
Yeah I don't like these. I have long felt that the
| |
217 public: | |
218 // See MediaCodecUtil::IsKnownUnaccelerated(). | |
219 static bool IsKnownUnaccelerated(const VideoCodec& codec, | |
220 MediaCodecDirection direction); | |
221 | |
222 // Create, start, and return a VideoCodecBridge decoder or NULL on failure. | |
223 static VideoCodecBridge* CreateDecoder( | |
liberato (no reviews please)
2017/02/07 06:19:43
if you're aiming to make these testable, then thes
watk
2017/02/08 02:53:57
Sounds good. For my immediate testing needs, I thi
| |
224 const VideoCodec& codec, | |
225 bool is_secure, // Will be used with encrypted content. | |
226 const gfx::Size& size, // Output frame size. | |
227 jobject surface, // Output surface, optional. | |
228 jobject media_crypto, // MediaCrypto object, optional. | |
229 // Codec specific data. See MediaCodec docs. | |
230 const std::vector<uint8_t>& csd0, | |
231 const std::vector<uint8_t>& csd1, | |
232 // Should adaptive playback be allowed if supported. | |
233 bool allow_adaptive_playback = true, | |
234 bool require_software_codec = false); | |
235 | |
236 // Create, start, and return a VideoCodecBridge encoder or NULL on failure. | |
237 static VideoCodecBridge* CreateEncoder( | |
238 const VideoCodec& codec, // e.g. media::kCodecVP8 | |
239 const gfx::Size& size, // input frame size | |
240 int bit_rate, // bits/second | |
241 int frame_rate, // frames/second | |
242 int i_frame_interval, // count | |
243 int color_format); // MediaCodecInfo.CodecCapabilities. | |
244 | |
245 void SetVideoBitrate(int bps, int frame_rate); | |
246 void RequestKeyFrameSoon(); | |
247 | |
248 // Returns whether adaptive playback is supported for this object given | |
249 // the new size. | |
250 bool IsAdaptivePlaybackSupported(int width, int height); | |
251 | |
252 // Changes the output surface for the MediaCodec. May only be used on API | |
253 // level 23 and higher (Marshmallow). | |
254 bool SetSurface(jobject surface); | |
255 | |
256 // Test-only method to set the return value of IsAdaptivePlaybackSupported(). | |
257 // Without this function, the return value of that function will be device | |
258 // dependent. If |adaptive_playback_supported| is equal to 0, the return value | |
259 // will be false. If |adaptive_playback_supported| is larger than 0, the | |
260 // return value will be true. | |
261 void set_adaptive_playback_supported_for_testing( | |
262 int adaptive_playback_supported) { | |
263 adaptive_playback_supported_for_testing_ = adaptive_playback_supported; | |
264 } | |
265 | |
266 private: | |
267 VideoCodecBridge(const std::string& mime, | |
268 bool is_secure, | |
269 MediaCodecDirection direction, | |
270 bool require_software_codec); | |
271 | |
272 int adaptive_playback_supported_for_testing_; | |
273 }; | |
274 | |
195 } // namespace media | 275 } // namespace media |
196 | 276 |
197 #endif // MEDIA_BASE_ANDROID_MEDIA_CODEC_BRIDGE_H_ | 277 #endif // MEDIA_BASE_ANDROID_MEDIA_CODEC_BRIDGE_H_ |
OLD | NEW |