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