Chromium Code Reviews| 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_IMPL_H_ | 5 #ifndef MEDIA_BASE_ANDROID_MEDIA_CODEC_BRIDGE_IMPL_H_ |
| 6 #define MEDIA_BASE_ANDROID_MEDIA_CODEC_BRIDGE_IMPL_H_ | 6 #define MEDIA_BASE_ANDROID_MEDIA_CODEC_BRIDGE_IMPL_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 #include <stdint.h> | 9 #include <stdint.h> |
| 10 | 10 |
| 11 #include <string> | 11 #include <string> |
| 12 | 12 |
| 13 #include "base/android/scoped_java_ref.h" | 13 #include "base/android/scoped_java_ref.h" |
| 14 #include "base/macros.h" | 14 #include "base/macros.h" |
| 15 #include "base/time/time.h" | 15 #include "base/time/time.h" |
| 16 #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" | 17 #include "media/base/android/media_codec_direction.h" |
| 18 #include "media/base/audio_decoder_config.h" | 18 #include "media/base/audio_decoder_config.h" |
| 19 #include "media/base/media_export.h" | 19 #include "media/base/media_export.h" |
| 20 #include "media/base/video_decoder_config.h" | 20 #include "media/base/video_decoder_config.h" |
| 21 #include "ui/gfx/geometry/size.h" | 21 #include "ui/gfx/geometry/size.h" |
| 22 | 22 |
| 23 namespace media { | 23 namespace media { |
| 24 | 24 |
| 25 // A bridge to a Java MediaCodec. | 25 // A bridge to a Java MediaCodec. |
| 26 class MEDIA_EXPORT MediaCodecBridgeImpl : public MediaCodecBridge { | 26 class MEDIA_EXPORT MediaCodecBridgeImpl : public MediaCodecBridge { |
| 27 public: | 27 public: |
| 28 // Creates and starts a new MediaCodec configured for decoding. Returns | |
| 29 // nullptr on failure. | |
| 30 static std::unique_ptr<MediaCodecBridge> CreateVideoDecoder( | |
| 31 VideoCodec codec, | |
| 32 bool is_secure, // Will be used with encrypted content. | |
| 33 const gfx::Size& size, // Output frame size. | |
| 34 jobject surface, // Output surface, optional. | |
| 35 jobject media_crypto, // MediaCrypto object, optional. | |
| 36 // Codec specific data. See MediaCodec docs. | |
| 37 const std::vector<uint8_t>& csd0, | |
| 38 const std::vector<uint8_t>& csd1, | |
| 39 // Should adaptive playback be allowed if supported. | |
| 40 bool allow_adaptive_playback = true, | |
| 41 bool require_software_codec = false); | |
| 42 | |
| 43 // Creates and starts a new MediaCodec configured for encoding. Returns | |
| 44 // nullptr on failure. | |
| 45 static std::unique_ptr<MediaCodecBridge> CreateVideoEncoder( | |
| 46 VideoCodec codec, // e.g. media::kCodecVP8 | |
| 47 const gfx::Size& size, // input frame size | |
| 48 int bit_rate, // bits/second | |
| 49 int frame_rate, // frames/second | |
| 50 int i_frame_interval, // count | |
| 51 int color_format); // MediaCodecInfo.CodecCapabilities. | |
| 52 | |
| 53 // Creates and starts a new MediaCodec configured for decoding. Returns | |
| 54 // nullptr on failure. | |
| 55 static std::unique_ptr<MediaCodecBridge> CreateAudioDecoder( | |
| 56 const AudioDecoderConfig& config, | |
| 57 jobject media_crypto); | |
| 58 | |
| 28 ~MediaCodecBridgeImpl() override; | 59 ~MediaCodecBridgeImpl() override; |
| 29 | |
| 30 // Calls start() against the media codec instance. Returns whether media | |
|
watk
2017/02/14 01:40:51
These comments were duplicating the interface comm
| |
| 31 // codec was successfully started. | |
| 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. | |
| 39 void Stop() override; | 60 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. | |
| 47 MediaCodecStatus Flush() override; | 61 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. | |
| 52 MediaCodecStatus GetOutputSize(gfx::Size* size) override; | 62 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. | |
| 57 MediaCodecStatus GetOutputSamplingRate(int* sampling_rate) override; | 63 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. | |
| 62 MediaCodecStatus GetOutputChannelCount(int* channel_count) override; | 64 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). | |
| 68 MediaCodecStatus QueueInputBuffer(int index, | 65 MediaCodecStatus QueueInputBuffer(int index, |
| 69 const uint8_t* data, | 66 const uint8_t* data, |
| 70 size_t data_size, | 67 size_t data_size, |
| 71 base::TimeDelta presentation_time) override; | 68 base::TimeDelta presentation_time) override; |
| 72 | |
| 73 // As above but for encrypted buffers. NULL |subsamples| indicates the | |
| 74 // whole buffer is encrypted. | |
| 75 MediaCodecStatus QueueSecureInputBuffer( | 69 MediaCodecStatus QueueSecureInputBuffer( |
| 76 int index, | 70 int index, |
| 77 const uint8_t* data, | 71 const uint8_t* data, |
| 78 size_t data_size, | 72 size_t data_size, |
| 79 const std::string& key_id, | 73 const std::string& key_id, |
| 80 const std::string& iv, | 74 const std::string& iv, |
| 81 const std::vector<SubsampleEntry>& subsamples, | 75 const std::vector<SubsampleEntry>& subsamples, |
| 82 const EncryptionScheme& encryption_scheme, | 76 const EncryptionScheme& encryption_scheme, |
| 83 base::TimeDelta presentation_time) override; | 77 base::TimeDelta presentation_time) override; |
| 84 | |
| 85 // Submits an empty buffer with the END_OF_STREAM flag set. | |
| 86 void QueueEOS(int input_buffer_index) override; | 78 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. | |
| 92 MediaCodecStatus DequeueInputBuffer(base::TimeDelta timeout, | 79 MediaCodecStatus DequeueInputBuffer(base::TimeDelta timeout, |
| 93 int* index) override; | 80 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. | |
| 100 MediaCodecStatus DequeueOutputBuffer(base::TimeDelta timeout, | 81 MediaCodecStatus DequeueOutputBuffer(base::TimeDelta timeout, |
| 101 int* index, | 82 int* index, |
| 102 size_t* offset, | 83 size_t* offset, |
| 103 size_t* size, | 84 size_t* size, |
| 104 base::TimeDelta* presentation_time, | 85 base::TimeDelta* presentation_time, |
| 105 bool* end_of_stream, | 86 bool* end_of_stream, |
| 106 bool* key_frame) override; | 87 bool* key_frame) override; |
| 107 | 88 |
| 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. | |
| 110 void ReleaseOutputBuffer(int index, bool render) override; | 89 void ReleaseOutputBuffer(int index, bool render) override; |
| 111 | |
| 112 // Returns an input buffer's base pointer and capacity. | |
| 113 MediaCodecStatus GetInputBuffer(int input_buffer_index, | 90 MediaCodecStatus GetInputBuffer(int input_buffer_index, |
| 114 uint8_t** data, | 91 uint8_t** data, |
| 115 size_t* capacity) override; | 92 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, | 93 MediaCodecStatus CopyFromOutputBuffer(int index, |
| 122 size_t offset, | 94 size_t offset, |
| 123 void* dst, | 95 void* dst, |
| 124 size_t num) override; | 96 size_t num) override; |
| 97 std::string GetName() override; | |
| 98 bool SetSurface(jobject surface) override; | |
| 99 void SetVideoBitrate(int bps, int frame_rate) override; | |
| 100 void RequestKeyFrameSoon() override; | |
| 101 bool IsAdaptivePlaybackSupported() override; | |
| 125 | 102 |
| 126 // Gets the component name. Before API level 18 this returns an empty string. | 103 private: |
| 127 std::string GetName() override; | |
| 128 | |
| 129 protected: | |
| 130 MediaCodecBridgeImpl(const std::string& mime, | 104 MediaCodecBridgeImpl(const std::string& mime, |
| 131 bool is_secure, | 105 bool is_secure, |
| 132 MediaCodecDirection direction, | 106 MediaCodecDirection direction, |
| 133 bool require_software_codec); | 107 bool require_software_codec); |
| 134 | 108 |
| 135 jobject media_codec() { return j_media_codec_.obj(); } | 109 // Calls MediaCodec#start(). Returns whether it was successful. |
| 110 bool Start(); | |
| 136 | 111 |
| 137 MediaCodecDirection direction_; | |
| 138 | |
| 139 private: | |
| 140 // Fills the given input buffer. Returns false if |data_size| exceeds the | 112 // 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). | 113 // input buffer's capacity (and doesn't touch the input buffer in that case). |
| 142 bool FillInputBuffer(int index, | 114 bool FillInputBuffer(int index, |
| 143 const uint8_t* data, | 115 const uint8_t* data, |
| 144 size_t data_size) WARN_UNUSED_RESULT; | 116 size_t data_size) WARN_UNUSED_RESULT; |
| 145 | 117 |
| 146 // Gets the address of the data in the given output buffer given by |index| | 118 // 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 | 119 // and |offset|. The number of bytes available to read is written to |
| 148 // |*capacity| and the address is written to |*addr|. Returns | 120 // |*capacity| and the address is written to |*addr|. Returns |
| 149 // MEDIA_CODEC_ERROR if an error occurs, or MEDIA_CODEC_OK otherwise. | 121 // MEDIA_CODEC_ERROR if an error occurs, or MEDIA_CODEC_OK otherwise. |
| 150 MediaCodecStatus GetOutputBufferAddress(int index, | 122 MediaCodecStatus GetOutputBufferAddress(int index, |
| 151 size_t offset, | 123 size_t offset, |
| 152 const uint8_t** addr, | 124 const uint8_t** addr, |
| 153 size_t* capacity); | 125 size_t* capacity); |
| 154 | 126 |
| 155 // The Java MediaCodecBridge instance. | 127 // The Java MediaCodecBridge instance. |
| 156 base::android::ScopedJavaGlobalRef<jobject> j_media_codec_; | 128 base::android::ScopedJavaGlobalRef<jobject> j_bridge_; |
| 129 | |
| 130 MediaCodecDirection direction_; | |
| 157 | 131 |
| 158 DISALLOW_COPY_AND_ASSIGN(MediaCodecBridgeImpl); | 132 DISALLOW_COPY_AND_ASSIGN(MediaCodecBridgeImpl); |
| 159 }; | 133 }; |
| 160 | 134 |
| 161 // A MediaCodecBridge for audio decoding. | |
| 162 // TODO(watk): Move this into MediaCodecBridgeImpl. | |
| 163 class MEDIA_EXPORT AudioCodecBridge : public MediaCodecBridgeImpl { | |
| 164 public: | |
| 165 // See MediaCodecUtil::IsKnownUnaccelerated(). | |
| 166 static bool IsKnownUnaccelerated(const AudioCodec& codec); | |
| 167 | |
| 168 // Returns an AudioCodecBridge instance if |codec| is supported, or a NULL | |
| 169 // pointer otherwise. | |
| 170 static AudioCodecBridge* Create(const AudioCodec& codec); | |
| 171 | |
| 172 // Starts the audio codec bridge. | |
| 173 bool ConfigureAndStart(const AudioDecoderConfig& config, | |
| 174 jobject media_crypto) WARN_UNUSED_RESULT; | |
| 175 | |
| 176 bool ConfigureAndStart(const AudioCodec& codec, | |
| 177 int sample_rate, | |
| 178 int channel_count, | |
| 179 const uint8_t* extra_data, | |
| 180 size_t extra_data_size, | |
| 181 int64_t codec_delay_ns, | |
| 182 int64_t seek_preroll_ns, | |
| 183 jobject media_crypto) WARN_UNUSED_RESULT; | |
| 184 | |
| 185 private: | |
| 186 explicit AudioCodecBridge(const std::string& mime); | |
| 187 | |
| 188 // Configure the java MediaFormat object with the extra codec data passed in. | |
| 189 bool ConfigureMediaFormat(jobject j_format, | |
| 190 const AudioCodec& codec, | |
| 191 const uint8_t* extra_data, | |
| 192 size_t extra_data_size, | |
| 193 int64_t codec_delay_ns, | |
| 194 int64_t seek_preroll_ns); | |
| 195 }; | |
| 196 | |
| 197 // A MediaCodecBridge for video encoding and decoding. | |
| 198 // TODO(watk): Move this into MediaCodecBridgeImpl. | |
| 199 class MEDIA_EXPORT VideoCodecBridge : public MediaCodecBridgeImpl { | |
| 200 public: | |
| 201 // See MediaCodecUtil::IsKnownUnaccelerated(). | |
| 202 static bool IsKnownUnaccelerated(const VideoCodec& codec, | |
| 203 MediaCodecDirection direction); | |
| 204 | |
| 205 // Create, start, and return a VideoCodecBridge decoder or NULL on failure. | |
| 206 static VideoCodecBridge* CreateDecoder( | |
| 207 const VideoCodec& codec, | |
| 208 bool is_secure, // Will be used with encrypted content. | |
| 209 const gfx::Size& size, // Output frame size. | |
| 210 jobject surface, // Output surface, optional. | |
| 211 jobject media_crypto, // MediaCrypto object, optional. | |
| 212 // Codec specific data. See MediaCodec docs. | |
| 213 const std::vector<uint8_t>& csd0, | |
| 214 const std::vector<uint8_t>& csd1, | |
| 215 // Should adaptive playback be allowed if supported. | |
| 216 bool allow_adaptive_playback = true, | |
| 217 bool require_software_codec = false); | |
| 218 | |
| 219 // Create, start, and return a VideoCodecBridge encoder or NULL on failure. | |
| 220 static VideoCodecBridge* CreateEncoder( | |
| 221 const VideoCodec& codec, // e.g. media::kCodecVP8 | |
| 222 const gfx::Size& size, // input frame size | |
| 223 int bit_rate, // bits/second | |
| 224 int frame_rate, // frames/second | |
| 225 int i_frame_interval, // count | |
| 226 int color_format); // MediaCodecInfo.CodecCapabilities. | |
| 227 | |
| 228 void SetVideoBitrate(int bps, int frame_rate); | |
| 229 void RequestKeyFrameSoon(); | |
| 230 | |
| 231 // Returns whether adaptive playback is supported for this object given | |
| 232 // the new size. | |
| 233 bool IsAdaptivePlaybackSupported(int width, int height); | |
| 234 | |
| 235 // Changes the output surface for the MediaCodec. May only be used on API | |
| 236 // level 23 and higher (Marshmallow). | |
| 237 bool SetSurface(jobject surface); | |
| 238 | |
| 239 // Test-only method to set the return value of IsAdaptivePlaybackSupported(). | |
| 240 // Without this function, the return value of that function will be device | |
| 241 // dependent. If |adaptive_playback_supported| is equal to 0, the return value | |
| 242 // will be false. If |adaptive_playback_supported| is larger than 0, the | |
| 243 // return value will be true. | |
| 244 void set_adaptive_playback_supported_for_testing( | |
| 245 int adaptive_playback_supported) { | |
| 246 adaptive_playback_supported_for_testing_ = adaptive_playback_supported; | |
| 247 } | |
| 248 | |
| 249 private: | |
| 250 VideoCodecBridge(const std::string& mime, | |
| 251 bool is_secure, | |
| 252 MediaCodecDirection direction, | |
| 253 bool require_software_codec); | |
| 254 | |
| 255 int adaptive_playback_supported_for_testing_; | |
| 256 }; | |
| 257 | |
| 258 } // namespace media | 135 } // namespace media |
| 259 | 136 |
| 260 #endif // MEDIA_BASE_ANDROID_MEDIA_CODEC_BRIDGE_IMPL_H_ | 137 #endif // MEDIA_BASE_ANDROID_MEDIA_CODEC_BRIDGE_IMPL_H_ |
| OLD | NEW |