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_SDK_MEDIA_CODEC_BRIDGE_H_ | 5 #ifndef MEDIA_BASE_ANDROID_MEDIA_CODEC_BRIDGE_IMPL_H_ |
6 #define MEDIA_BASE_ANDROID_SDK_MEDIA_CODEC_BRIDGE_H_ | 6 #define MEDIA_BASE_ANDROID_MEDIA_CODEC_BRIDGE_IMPL_H_ |
7 | 7 |
8 #include <jni.h> | |
9 #include <stddef.h> | 8 #include <stddef.h> |
10 #include <stdint.h> | 9 #include <stdint.h> |
11 | 10 |
12 #include <set> | |
13 #include <string> | 11 #include <string> |
14 | 12 |
15 #include "base/android/scoped_java_ref.h" | 13 #include "base/android/scoped_java_ref.h" |
16 #include "base/macros.h" | 14 #include "base/macros.h" |
17 #include "base/time/time.h" | 15 #include "base/time/time.h" |
18 #include "media/base/android/media_codec_bridge.h" | 16 #include "media/base/android/media_codec_bridge.h" |
| 17 #include "media/base/android/media_codec_direction.h" |
19 #include "media/base/audio_decoder_config.h" | 18 #include "media/base/audio_decoder_config.h" |
20 #include "media/base/media_export.h" | 19 #include "media/base/media_export.h" |
21 #include "media/base/video_decoder_config.h" | 20 #include "media/base/video_decoder_config.h" |
22 #include "ui/gfx/geometry/size.h" | 21 #include "ui/gfx/geometry/size.h" |
23 | 22 |
24 namespace media { | 23 namespace media { |
25 | 24 |
26 // This class implements MediaCodecBridge using android MediaCodec SDK APIs. | 25 // A bridge to a Java MediaCodec. |
27 class MEDIA_EXPORT SdkMediaCodecBridge : public MediaCodecBridge { | 26 class MEDIA_EXPORT MediaCodecBridgeImpl : public MediaCodecBridge { |
28 public: | 27 public: |
29 ~SdkMediaCodecBridge() override; | 28 ~MediaCodecBridgeImpl() override; |
30 | 29 |
31 // MediaCodecBridge implementations. | 30 // Calls start() against the media codec instance. Returns whether media |
| 31 // codec was successfully started. |
32 bool Start() override; | 32 bool Start() override; |
| 33 |
| 34 // Finishes the decode/encode session. The instance remains active |
| 35 // and ready to be StartAudio/Video()ed again. HOWEVER, due to the buggy |
| 36 // vendor's implementation , b/8125974, Stop() -> StartAudio/Video() may not |
| 37 // work on some devices. For reliability, Stop() -> delete and recreate new |
| 38 // instance -> StartAudio/Video() is recommended. |
33 void Stop() override; | 39 void Stop() override; |
| 40 |
| 41 // Calls flush() on the MediaCodec. All indices previously returned in calls |
| 42 // to DequeueInputBuffer() and DequeueOutputBuffer() become invalid. Please |
| 43 // note that this clears all the inputs in the media codec. In other words, |
| 44 // there will be no outputs until new input is provided. Returns |
| 45 // MEDIA_CODEC_ERROR if an unexpected error happens, or MEDIA_CODEC_OK |
| 46 // otherwise. |
34 MediaCodecStatus Flush() override; | 47 MediaCodecStatus Flush() override; |
| 48 |
| 49 // Used for getting the output size. This is valid after DequeueInputBuffer() |
| 50 // returns a format change by returning INFO_OUTPUT_FORMAT_CHANGED. |
| 51 // Returns MEDIA_CODEC_ERROR if an error occurs, or MEDIA_CODEC_OK otherwise. |
35 MediaCodecStatus GetOutputSize(gfx::Size* size) override; | 52 MediaCodecStatus GetOutputSize(gfx::Size* size) override; |
| 53 |
| 54 // Used for checking for new sampling rate after DequeueInputBuffer() returns |
| 55 // INFO_OUTPUT_FORMAT_CHANGED |
| 56 // Returns MEDIA_CODEC_ERROR if an error occurs, or MEDIA_CODEC_OK otherwise. |
36 MediaCodecStatus GetOutputSamplingRate(int* sampling_rate) override; | 57 MediaCodecStatus GetOutputSamplingRate(int* sampling_rate) override; |
| 58 |
| 59 // Fills |channel_count| with the number of audio channels. Useful after |
| 60 // INFO_OUTPUT_FORMAT_CHANGED. |
| 61 // Returns MEDIA_CODEC_ERROR if an error occurs, or MEDIA_CODEC_OK otherwise. |
37 MediaCodecStatus GetOutputChannelCount(int* channel_count) override; | 62 MediaCodecStatus GetOutputChannelCount(int* channel_count) override; |
| 63 |
| 64 // Submits a byte array to the given input buffer. Call this after getting an |
| 65 // available buffer from DequeueInputBuffer(). If |data| is NULL, assume the |
| 66 // input buffer has already been populated (but still obey |size|). |
| 67 // |data_size| must be less than kint32max (because Java). |
38 MediaCodecStatus QueueInputBuffer(int index, | 68 MediaCodecStatus QueueInputBuffer(int index, |
39 const uint8_t* data, | 69 const uint8_t* data, |
40 size_t data_size, | 70 size_t data_size, |
41 base::TimeDelta presentation_time) override; | 71 base::TimeDelta presentation_time) override; |
42 using MediaCodecBridge::QueueSecureInputBuffer; | 72 |
| 73 // As above but for encrypted buffers. NULL |subsamples| indicates the |
| 74 // whole buffer is encrypted. |
43 MediaCodecStatus QueueSecureInputBuffer( | 75 MediaCodecStatus QueueSecureInputBuffer( |
44 int index, | 76 int index, |
45 const uint8_t* data, | 77 const uint8_t* data, |
46 size_t data_size, | 78 size_t data_size, |
47 const std::vector<char>& key_id, | 79 const std::string& key_id, |
48 const std::vector<char>& iv, | 80 const std::string& iv, |
49 const SubsampleEntry* subsamples, | 81 const std::vector<SubsampleEntry>& subsamples, |
50 int subsamples_size, | |
51 const EncryptionScheme& encryption_scheme, | 82 const EncryptionScheme& encryption_scheme, |
52 base::TimeDelta presentation_time) override; | 83 base::TimeDelta presentation_time) override; |
| 84 |
| 85 // Submits an empty buffer with the END_OF_STREAM flag set. |
53 void QueueEOS(int input_buffer_index) override; | 86 void QueueEOS(int input_buffer_index) override; |
| 87 |
| 88 // Returns: |
| 89 // MEDIA_CODEC_OK if an input buffer is ready to be filled with valid data, |
| 90 // MEDIA_CODEC_ENQUEUE_INPUT_AGAIN_LATER if no such buffer is available, or |
| 91 // MEDIA_CODEC_ERROR if unexpected error happens. |
54 MediaCodecStatus DequeueInputBuffer(base::TimeDelta timeout, | 92 MediaCodecStatus DequeueInputBuffer(base::TimeDelta timeout, |
55 int* index) override; | 93 int* index) override; |
| 94 |
| 95 // Dequeues an output buffer, block for up to |timeout|. |
| 96 // Returns the status of this operation. If OK is returned, the output |
| 97 // parameters should be populated. Otherwise, the values of output parameters |
| 98 // should not be used. Output parameters other than index/offset/size are |
| 99 // optional and only set if not NULL. |
56 MediaCodecStatus DequeueOutputBuffer(base::TimeDelta timeout, | 100 MediaCodecStatus DequeueOutputBuffer(base::TimeDelta timeout, |
57 int* index, | 101 int* index, |
58 size_t* offset, | 102 size_t* offset, |
59 size_t* size, | 103 size_t* size, |
60 base::TimeDelta* presentation_time, | 104 base::TimeDelta* presentation_time, |
61 bool* end_of_stream, | 105 bool* end_of_stream, |
62 bool* key_frame) override; | 106 bool* key_frame) override; |
| 107 |
| 108 // Returns the buffer to the codec. If you previously specified a surface when |
| 109 // configuring this video decoder you can optionally render the buffer. |
63 void ReleaseOutputBuffer(int index, bool render) override; | 110 void ReleaseOutputBuffer(int index, bool render) override; |
| 111 |
| 112 // Returns an input buffer's base pointer and capacity. |
64 MediaCodecStatus GetInputBuffer(int input_buffer_index, | 113 MediaCodecStatus GetInputBuffer(int input_buffer_index, |
65 uint8_t** data, | 114 uint8_t** data, |
66 size_t* capacity) override; | 115 size_t* capacity) override; |
| 116 |
| 117 // Copies |num| bytes from output buffer |index|'s |offset| into the memory |
| 118 // region pointed to by |dst|. To avoid overflows, the size of both source |
| 119 // and destination must be at least |num| bytes, and should not overlap. |
| 120 // Returns MEDIA_CODEC_ERROR if an error occurs, or MEDIA_CODEC_OK otherwise. |
| 121 MediaCodecStatus CopyFromOutputBuffer(int index, |
| 122 size_t offset, |
| 123 void* dst, |
| 124 size_t num) override; |
| 125 |
| 126 // Gets the component name. Before API level 18 this returns an empty string. |
| 127 std::string GetName() override; |
| 128 |
| 129 protected: |
| 130 MediaCodecBridgeImpl(const std::string& mime, |
| 131 bool is_secure, |
| 132 MediaCodecDirection direction, |
| 133 bool require_software_codec); |
| 134 |
| 135 jobject media_codec() { return j_media_codec_.obj(); } |
| 136 |
| 137 MediaCodecDirection direction_; |
| 138 |
| 139 private: |
| 140 // Fills the given input buffer. Returns false if |data_size| exceeds the |
| 141 // input buffer's capacity (and doesn't touch the input buffer in that case). |
| 142 bool FillInputBuffer(int index, |
| 143 const uint8_t* data, |
| 144 size_t data_size) WARN_UNUSED_RESULT; |
| 145 |
| 146 // Gets the address of the data in the given output buffer given by |index| |
| 147 // and |offset|. The number of bytes available to read is written to |
| 148 // |*capacity| and the address is written to |*addr|. Returns |
| 149 // MEDIA_CODEC_ERROR if an error occurs, or MEDIA_CODEC_OK otherwise. |
67 MediaCodecStatus GetOutputBufferAddress(int index, | 150 MediaCodecStatus GetOutputBufferAddress(int index, |
68 size_t offset, | 151 size_t offset, |
69 const uint8_t** addr, | 152 const uint8_t** addr, |
70 size_t* capacity) override; | 153 size_t* capacity); |
71 std::string GetName() override; | |
72 | 154 |
73 protected: | 155 // The Java MediaCodecBridge instance. |
74 SdkMediaCodecBridge(const std::string& mime, | |
75 bool is_secure, | |
76 MediaCodecDirection direction, | |
77 bool require_software_codec); | |
78 | |
79 jobject media_codec() { return j_media_codec_.obj(); } | |
80 MediaCodecDirection direction_; | |
81 | |
82 private: | |
83 // Java MediaCodec instance. | |
84 base::android::ScopedJavaGlobalRef<jobject> j_media_codec_; | 156 base::android::ScopedJavaGlobalRef<jobject> j_media_codec_; |
85 | 157 |
86 DISALLOW_COPY_AND_ASSIGN(SdkMediaCodecBridge); | 158 DISALLOW_COPY_AND_ASSIGN(MediaCodecBridgeImpl); |
87 }; | 159 }; |
88 | 160 |
89 // Class for handling audio decoding using android MediaCodec APIs. | 161 // A MediaCodecBridge for audio decoding. |
90 // TODO(qinmin): implement the logic to switch between NDK and SDK | 162 // TODO(watk): Move this into MediaCodecBridgeImpl. |
91 // MediaCodecBridge. | 163 class MEDIA_EXPORT AudioCodecBridge : public MediaCodecBridgeImpl { |
92 class MEDIA_EXPORT AudioCodecBridge : public SdkMediaCodecBridge { | |
93 public: | 164 public: |
| 165 // See MediaCodecUtil::IsKnownUnaccelerated(). |
| 166 static bool IsKnownUnaccelerated(const AudioCodec& codec); |
| 167 |
94 // Returns an AudioCodecBridge instance if |codec| is supported, or a NULL | 168 // Returns an AudioCodecBridge instance if |codec| is supported, or a NULL |
95 // pointer otherwise. | 169 // pointer otherwise. |
96 static AudioCodecBridge* Create(const AudioCodec& codec); | 170 static AudioCodecBridge* Create(const AudioCodec& codec); |
97 | 171 |
98 // See MediaCodecUtil::IsKnownUnaccelerated(). | |
99 static bool IsKnownUnaccelerated(const AudioCodec& codec); | |
100 | |
101 // Starts the audio codec bridge. | 172 // Starts the audio codec bridge. |
102 bool ConfigureAndStart(const AudioDecoderConfig& config, | 173 bool ConfigureAndStart(const AudioDecoderConfig& config, |
103 jobject media_crypto); | 174 jobject media_crypto) WARN_UNUSED_RESULT; |
104 | 175 |
105 // An overloaded variant used by AudioDecoderJob and AudioMediaCodecDecoder. | |
106 // TODO(timav): Modify the above mentioned classes to pass parameters as | |
107 // AudioDecoderConfig and remove this method. | |
108 bool ConfigureAndStart(const AudioCodec& codec, | 176 bool ConfigureAndStart(const AudioCodec& codec, |
109 int sample_rate, | 177 int sample_rate, |
110 int channel_count, | 178 int channel_count, |
111 const uint8_t* extra_data, | 179 const uint8_t* extra_data, |
112 size_t extra_data_size, | 180 size_t extra_data_size, |
113 int64_t codec_delay_ns, | 181 int64_t codec_delay_ns, |
114 int64_t seek_preroll_ns, | 182 int64_t seek_preroll_ns, |
115 jobject media_crypto) WARN_UNUSED_RESULT; | 183 jobject media_crypto) WARN_UNUSED_RESULT; |
116 | 184 |
117 private: | 185 private: |
118 explicit AudioCodecBridge(const std::string& mime); | 186 explicit AudioCodecBridge(const std::string& mime); |
119 | 187 |
120 // Configure the java MediaFormat object with the extra codec data passed in. | 188 // Configure the java MediaFormat object with the extra codec data passed in. |
121 bool ConfigureMediaFormat(jobject j_format, | 189 bool ConfigureMediaFormat(jobject j_format, |
122 const AudioCodec& codec, | 190 const AudioCodec& codec, |
123 const uint8_t* extra_data, | 191 const uint8_t* extra_data, |
124 size_t extra_data_size, | 192 size_t extra_data_size, |
125 int64_t codec_delay_ns, | 193 int64_t codec_delay_ns, |
126 int64_t seek_preroll_ns); | 194 int64_t seek_preroll_ns); |
127 }; | 195 }; |
128 | 196 |
129 // Class for handling video encoding/decoding using android MediaCodec APIs. | 197 // A MediaCodecBridge for video encoding and decoding. |
130 // TODO(qinmin): implement the logic to switch between NDK and SDK | 198 // TODO(watk): Move this into MediaCodecBridgeImpl. |
131 // MediaCodecBridge. | 199 class MEDIA_EXPORT VideoCodecBridge : public MediaCodecBridgeImpl { |
132 class MEDIA_EXPORT VideoCodecBridge : public SdkMediaCodecBridge { | |
133 public: | 200 public: |
134 // See MediaCodecUtil::IsKnownUnaccelerated(). | 201 // See MediaCodecUtil::IsKnownUnaccelerated(). |
135 static bool IsKnownUnaccelerated(const VideoCodec& codec, | 202 static bool IsKnownUnaccelerated(const VideoCodec& codec, |
136 MediaCodecDirection direction); | 203 MediaCodecDirection direction); |
137 | 204 |
138 // Create, start, and return a VideoCodecBridge decoder or NULL on failure. | 205 // Create, start, and return a VideoCodecBridge decoder or NULL on failure. |
139 static VideoCodecBridge* CreateDecoder( | 206 static VideoCodecBridge* CreateDecoder( |
140 const VideoCodec& codec, | 207 const VideoCodec& codec, |
141 bool is_secure, // Will be used with encrypted content. | 208 bool is_secure, // Will be used with encrypted content. |
142 const gfx::Size& size, // Output frame size. | 209 const gfx::Size& size, // Output frame size. |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
183 VideoCodecBridge(const std::string& mime, | 250 VideoCodecBridge(const std::string& mime, |
184 bool is_secure, | 251 bool is_secure, |
185 MediaCodecDirection direction, | 252 MediaCodecDirection direction, |
186 bool require_software_codec); | 253 bool require_software_codec); |
187 | 254 |
188 int adaptive_playback_supported_for_testing_; | 255 int adaptive_playback_supported_for_testing_; |
189 }; | 256 }; |
190 | 257 |
191 } // namespace media | 258 } // namespace media |
192 | 259 |
193 #endif // MEDIA_BASE_ANDROID_SDK_MEDIA_CODEC_BRIDGE_H_ | 260 #endif // MEDIA_BASE_ANDROID_MEDIA_CODEC_BRIDGE_IMPL_H_ |
OLD | NEW |