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