| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef MEDIA_BASE_ANDROID_SDK_MEDIA_CODEC_BRIDGE_H_ | |
| 6 #define MEDIA_BASE_ANDROID_SDK_MEDIA_CODEC_BRIDGE_H_ | |
| 7 | |
| 8 #include <jni.h> | |
| 9 #include <stdint.h> | |
| 10 | |
| 11 #include <set> | |
| 12 #include <string> | |
| 13 | |
| 14 #include "base/android/scoped_java_ref.h" | |
| 15 #include "base/macros.h" | |
| 16 #include "base/time/time.h" | |
| 17 #include "media/base/android/media_codec_bridge.h" | |
| 18 #include "media/base/audio_decoder_config.h" | |
| 19 #include "media/base/media_export.h" | |
| 20 #include "media/base/video_decoder_config.h" | |
| 21 #include "ui/gfx/geometry/size.h" | |
| 22 | |
| 23 namespace media { | |
| 24 | |
| 25 // This class implements MediaCodecBridge using android MediaCodec SDK APIs. | |
| 26 class MEDIA_EXPORT SdkMediaCodecBridge : public MediaCodecBridge { | |
| 27 public: | |
| 28 ~SdkMediaCodecBridge() override; | |
| 29 | |
| 30 // MediaCodecBridge implementations. | |
| 31 MediaCodecStatus Reset() override; | |
| 32 bool Start() override; | |
| 33 void Stop() override; | |
| 34 void GetOutputFormat(int* width, int* height) override; | |
| 35 int GetOutputSamplingRate() override; | |
| 36 MediaCodecStatus QueueInputBuffer( | |
| 37 int index, | |
| 38 const uint8_t* data, | |
| 39 size_t data_size, | |
| 40 const base::TimeDelta& presentation_time) override; | |
| 41 using MediaCodecBridge::QueueSecureInputBuffer; | |
| 42 MediaCodecStatus QueueSecureInputBuffer( | |
| 43 int index, | |
| 44 const uint8_t* data, | |
| 45 size_t data_size, | |
| 46 const std::vector<char>& key_id, | |
| 47 const std::vector<char>& iv, | |
| 48 const SubsampleEntry* subsamples, | |
| 49 int subsamples_size, | |
| 50 const base::TimeDelta& presentation_time) override; | |
| 51 void QueueEOS(int input_buffer_index) override; | |
| 52 MediaCodecStatus DequeueInputBuffer(const base::TimeDelta& timeout, | |
| 53 int* index) override; | |
| 54 MediaCodecStatus DequeueOutputBuffer(const base::TimeDelta& timeout, | |
| 55 int* index, | |
| 56 size_t* offset, | |
| 57 size_t* size, | |
| 58 base::TimeDelta* presentation_time, | |
| 59 bool* end_of_stream, | |
| 60 bool* key_frame) override; | |
| 61 void ReleaseOutputBuffer(int index, bool render) override; | |
| 62 int GetOutputBuffersCount() override; | |
| 63 size_t GetOutputBuffersCapacity() override; | |
| 64 void GetInputBuffer(int input_buffer_index, | |
| 65 uint8_t** data, | |
| 66 size_t* capacity) override; | |
| 67 bool CopyFromOutputBuffer(int index, | |
| 68 size_t offset, | |
| 69 void* dst, | |
| 70 int dst_size) override; | |
| 71 | |
| 72 static bool RegisterSdkMediaCodecBridge(JNIEnv* env); | |
| 73 | |
| 74 protected: | |
| 75 SdkMediaCodecBridge(const std::string& mime, | |
| 76 bool is_secure, | |
| 77 MediaCodecDirection direction); | |
| 78 | |
| 79 // Called to get the buffer address given the output buffer index and offset. | |
| 80 // This function returns the size of the output and |addr| is the pointer to | |
| 81 // the address to read. | |
| 82 int GetOutputBufferAddress(int index, size_t offset, void** addr); | |
| 83 | |
| 84 jobject media_codec() { return j_media_codec_.obj(); } | |
| 85 MediaCodecDirection direction_; | |
| 86 | |
| 87 private: | |
| 88 // Java MediaCodec instance. | |
| 89 base::android::ScopedJavaGlobalRef<jobject> j_media_codec_; | |
| 90 | |
| 91 DISALLOW_COPY_AND_ASSIGN(SdkMediaCodecBridge); | |
| 92 }; | |
| 93 | |
| 94 // Class for handling audio decoding using android MediaCodec APIs. | |
| 95 // TODO(qinmin): implement the logic to switch between NDK and SDK | |
| 96 // MediaCodecBridge. | |
| 97 class MEDIA_EXPORT AudioCodecBridge : public SdkMediaCodecBridge { | |
| 98 public: | |
| 99 // Returns an AudioCodecBridge instance if |codec| is supported, or a NULL | |
| 100 // pointer otherwise. | |
| 101 static AudioCodecBridge* Create(const AudioCodec& codec); | |
| 102 | |
| 103 // See MediaCodecUtil::IsKnownUnaccelerated(). | |
| 104 static bool IsKnownUnaccelerated(const AudioCodec& codec); | |
| 105 | |
| 106 // Start the audio codec bridge. | |
| 107 bool ConfigureAndStart(const AudioCodec& codec, | |
| 108 int sample_rate, | |
| 109 int channel_count, | |
| 110 const uint8_t* extra_data, | |
| 111 size_t extra_data_size, | |
| 112 int64_t codec_delay_ns, | |
| 113 int64_t seek_preroll_ns, | |
| 114 bool play_audio, | |
| 115 jobject media_crypto) WARN_UNUSED_RESULT; | |
| 116 | |
| 117 // Plays the output buffer right away or save for later playback if |postpone| | |
| 118 // is set to true. This call must be called after DequeueOutputBuffer() and | |
| 119 // before ReleaseOutputBuffer. The data is extracted from the output buffers | |
| 120 // using |index|, |size| and |offset|. Returns the playback head position | |
| 121 // expressed in frames. | |
| 122 // When |postpone| is set to true, the next PlayOutputBuffer() should have | |
| 123 // postpone == false, and it will play two buffers: the postponed one and | |
| 124 // the one identified by |index|. | |
| 125 int64_t PlayOutputBuffer(int index, | |
| 126 size_t size, | |
| 127 size_t offset, | |
| 128 bool postpone = false); | |
| 129 | |
| 130 // Set the volume of the audio output. | |
| 131 void SetVolume(double volume); | |
| 132 | |
| 133 private: | |
| 134 explicit AudioCodecBridge(const std::string& mime); | |
| 135 | |
| 136 // Configure the java MediaFormat object with the extra codec data passed in. | |
| 137 bool ConfigureMediaFormat(jobject j_format, | |
| 138 const AudioCodec& codec, | |
| 139 const uint8_t* extra_data, | |
| 140 size_t extra_data_size, | |
| 141 int64_t codec_delay_ns, | |
| 142 int64_t seek_preroll_ns); | |
| 143 }; | |
| 144 | |
| 145 // Class for handling video encoding/decoding using android MediaCodec APIs. | |
| 146 // TODO(qinmin): implement the logic to switch between NDK and SDK | |
| 147 // MediaCodecBridge. | |
| 148 class MEDIA_EXPORT VideoCodecBridge : public SdkMediaCodecBridge { | |
| 149 public: | |
| 150 // See MediaCodecUtil::IsKnownUnaccelerated(). | |
| 151 static bool IsKnownUnaccelerated(const VideoCodec& codec, | |
| 152 MediaCodecDirection direction); | |
| 153 | |
| 154 // Create, start, and return a VideoCodecBridge decoder or NULL on failure. | |
| 155 static VideoCodecBridge* CreateDecoder( | |
| 156 const VideoCodec& codec, // e.g. media::kCodecVP8 | |
| 157 bool is_secure, | |
| 158 const gfx::Size& size, // Output frame size. | |
| 159 jobject surface, // Output surface, optional. | |
| 160 jobject media_crypto); // MediaCrypto object, optional. | |
| 161 | |
| 162 // Create, start, and return a VideoCodecBridge encoder or NULL on failure. | |
| 163 static VideoCodecBridge* CreateEncoder( | |
| 164 const VideoCodec& codec, // e.g. media::kCodecVP8 | |
| 165 const gfx::Size& size, // input frame size | |
| 166 int bit_rate, // bits/second | |
| 167 int frame_rate, // frames/second | |
| 168 int i_frame_interval, // count | |
| 169 int color_format); // MediaCodecInfo.CodecCapabilities. | |
| 170 | |
| 171 void SetVideoBitrate(int bps); | |
| 172 void RequestKeyFrameSoon(); | |
| 173 | |
| 174 // Returns whether adaptive playback is supported for this object given | |
| 175 // the new size. | |
| 176 bool IsAdaptivePlaybackSupported(int width, int height); | |
| 177 | |
| 178 // Test-only method to set the return value of IsAdaptivePlaybackSupported(). | |
| 179 // Without this function, the return value of that function will be device | |
| 180 // dependent. If |adaptive_playback_supported| is equal to 0, the return value | |
| 181 // will be false. If |adaptive_playback_supported| is larger than 0, the | |
| 182 // return value will be true. | |
| 183 void set_adaptive_playback_supported_for_testing( | |
| 184 int adaptive_playback_supported) { | |
| 185 adaptive_playback_supported_for_testing_ = adaptive_playback_supported; | |
| 186 } | |
| 187 | |
| 188 private: | |
| 189 VideoCodecBridge(const std::string& mime, | |
| 190 bool is_secure, | |
| 191 MediaCodecDirection direction); | |
| 192 | |
| 193 int adaptive_playback_supported_for_testing_; | |
| 194 }; | |
| 195 | |
| 196 } // namespace media | |
| 197 | |
| 198 #endif // MEDIA_BASE_ANDROID_SDK_MEDIA_CODEC_BRIDGE_H_ | |
| OLD | NEW |