• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_MEDIA_CODEC_BRIDGE_H_
6 #define MEDIA_BASE_ANDROID_MEDIA_CODEC_BRIDGE_H_
7 
8 #include <jni.h>
9 #include <string>
10 
11 #include "base/android/scoped_java_ref.h"
12 #include "base/time/time.h"
13 #include "media/base/audio_decoder_config.h"
14 #include "media/base/video_decoder_config.h"
15 #include "ui/gfx/size.h"
16 
17 namespace media {
18 
19 struct SubsampleEntry;
20 
21 // These must be in sync with MediaCodecBridge.MEDIA_CODEC_XXX constants in
22 // MediaCodecBridge.java.
23 enum MediaCodecStatus {
24   MEDIA_CODEC_OK,
25   MEDIA_CODEC_DEQUEUE_INPUT_AGAIN_LATER,
26   MEDIA_CODEC_DEQUEUE_OUTPUT_AGAIN_LATER,
27   MEDIA_CODEC_OUTPUT_BUFFERS_CHANGED,
28   MEDIA_CODEC_OUTPUT_FORMAT_CHANGED,
29   MEDIA_CODEC_INPUT_END_OF_STREAM,
30   MEDIA_CODEC_OUTPUT_END_OF_STREAM,
31   MEDIA_CODEC_NO_KEY,
32   MEDIA_CODEC_STOPPED,
33   MEDIA_CODEC_ERROR
34 };
35 
36 // Codec direction.  Keep this in sync with MediaCodecBridge.java.
37 enum MediaCodecDirection {
38   MEDIA_CODEC_DECODER,
39   MEDIA_CODEC_ENCODER,
40 };
41 
42 // This class serves as a bridge for native code to call java functions inside
43 // Android MediaCodec class. For more information on Android MediaCodec, check
44 // http://developer.android.com/reference/android/media/MediaCodec.html
45 // Note: MediaCodec is only available on JB and greater.
46 // Use AudioCodecBridge or VideoCodecBridge to create an instance of this
47 // object.
48 //
49 // TODO(fischman,xhwang): replace this (and the enums that go with it) with
50 // chromium's JNI auto-generation hotness.
51 class MEDIA_EXPORT MediaCodecBridge {
52  public:
53   // Returns true if MediaCodec is available on the device.
54   static bool IsAvailable();
55 
56   // Returns true if MediaCodec.setParameters() is available on the device.
57   static bool SupportsSetParameters();
58 
59   // Returns whether MediaCodecBridge has a decoder that |is_secure| and can
60   // decode |codec| type.
61   static bool CanDecode(const std::string& codec, bool is_secure);
62 
63   // Represents supported codecs on android.
64   // TODO(qinmin): Curretly the codecs string only contains one codec, do we
65   // need more specific codecs separated by comma. (e.g. "vp8" -> "vp8, vp8.0")
66   struct CodecsInfo {
67     std::string codecs;  // E.g. "vp8" or "avc1".
68     std::string name;    // E.g. "OMX.google.vp8.decoder".
69     MediaCodecDirection direction;
70   };
71 
72   // Get a list of supported codecs.
73   static std::vector<CodecsInfo> GetCodecsInfo();
74 
75   virtual ~MediaCodecBridge();
76 
77   // Resets both input and output, all indices previously returned in calls to
78   // DequeueInputBuffer() and DequeueOutputBuffer() become invalid.
79   // Please note that this clears all the inputs in the media codec. In other
80   // words, there will be no outputs until new input is provided.
81   // Returns MEDIA_CODEC_ERROR if an unexpected error happens, or Media_CODEC_OK
82   // otherwise.
83   MediaCodecStatus Reset();
84 
85   // Finishes the decode/encode session. The instance remains active
86   // and ready to be StartAudio/Video()ed again. HOWEVER, due to the buggy
87   // vendor's implementation , b/8125974, Stop() -> StartAudio/Video() may not
88   // work on some devices. For reliability, Stop() -> delete and recreate new
89   // instance -> StartAudio/Video() is recommended.
90   void Stop();
91 
92   // Used for getting output format. This is valid after DequeueInputBuffer()
93   // returns a format change by returning INFO_OUTPUT_FORMAT_CHANGED
94   void GetOutputFormat(int* width, int* height);
95 
96   // Returns the number of input buffers used by the codec.
97   int GetInputBuffersCount();
98 
99   // Submits a byte array to the given input buffer. Call this after getting an
100   // available buffer from DequeueInputBuffer().  If |data| is NULL, assume the
101   // input buffer has already been populated (but still obey |size|).
102   // |data_size| must be less than kint32max (because Java).
103   MediaCodecStatus QueueInputBuffer(int index,
104                                     const uint8* data,
105                                     size_t data_size,
106                                     const base::TimeDelta& presentation_time);
107 
108   // Similar to the above call, but submits a buffer that is encrypted.  Note:
109   // NULL |subsamples| indicates the whole buffer is encrypted.  If |data| is
110   // NULL, assume the input buffer has already been populated (but still obey
111   // |data_size|).  |data_size| must be less than kint32max (because Java).
112   MediaCodecStatus QueueSecureInputBuffer(
113       int index,
114       const uint8* data,
115       size_t data_size,
116       const uint8* key_id,
117       int key_id_size,
118       const uint8* iv,
119       int iv_size,
120       const SubsampleEntry* subsamples,
121       int subsamples_size,
122       const base::TimeDelta& presentation_time);
123 
124   // Submits an empty buffer with a EOS (END OF STREAM) flag.
125   void QueueEOS(int input_buffer_index);
126 
127   // Returns:
128   // MEDIA_CODEC_OK if an input buffer is ready to be filled with valid data,
129   // MEDIA_CODEC_ENQUEUE_INPUT_AGAIN_LATER if no such buffer is available, or
130   // MEDIA_CODEC_ERROR if unexpected error happens.
131   // Note: Never use infinite timeout as this would block the decoder thread and
132   // prevent the decoder job from being released.
133   MediaCodecStatus DequeueInputBuffer(const base::TimeDelta& timeout,
134                                       int* index);
135 
136   // Dequeues an output buffer, block at most timeout_us microseconds.
137   // Returns the status of this operation. If OK is returned, the output
138   // parameters should be populated. Otherwise, the values of output parameters
139   // should not be used.  Output parameters other than index/offset/size are
140   // optional and only set if not NULL.
141   // Note: Never use infinite timeout as this would block the decoder thread and
142   // prevent the decoder job from being released.
143   // TODO(xhwang): Can we drop |end_of_stream| and return
144   // MEDIA_CODEC_OUTPUT_END_OF_STREAM?
145   MediaCodecStatus DequeueOutputBuffer(const base::TimeDelta& timeout,
146                                        int* index,
147                                        size_t* offset,
148                                        size_t* size,
149                                        base::TimeDelta* presentation_time,
150                                        bool* end_of_stream,
151                                        bool* key_frame);
152 
153   // Returns the buffer to the codec. If you previously specified a surface when
154   // configuring this video decoder you can optionally render the buffer.
155   void ReleaseOutputBuffer(int index, bool render);
156 
157   // Returns the number of output buffers used by the codec.
158   int GetOutputBuffersCount();
159 
160   // Returns the capacity of each output buffer used by the codec.
161   size_t GetOutputBuffersCapacity();
162 
163   // Gets output buffers from media codec and keeps them inside the java class.
164   // To access them, use DequeueOutputBuffer(). Returns whether output buffers
165   // were successfully obtained.
166   bool GetOutputBuffers() WARN_UNUSED_RESULT;
167 
168   // Returns an input buffer's base pointer and capacity.
169   void GetInputBuffer(int input_buffer_index, uint8** data, size_t* capacity);
170 
171   // Copy |dst_size| bytes from output buffer |index|'s |offset| onwards into
172   // |*dst|.
173   bool CopyFromOutputBuffer(int index, size_t offset, void* dst, int dst_size);
174 
175   static bool RegisterMediaCodecBridge(JNIEnv* env);
176 
177  protected:
178   // Returns true if |mime_type| is known to be unaccelerated (i.e. backed by a
179   // software codec instead of a hardware one).
180   static bool IsKnownUnaccelerated(const std::string& mime_type,
181                                    MediaCodecDirection direction);
182 
183   MediaCodecBridge(const std::string& mime,
184                    bool is_secure,
185                    MediaCodecDirection direction);
186 
187   // Calls start() against the media codec instance. Used in StartXXX() after
188   // configuring media codec. Returns whether media codec was successfully
189   // started.
190   bool StartInternal() WARN_UNUSED_RESULT;
191 
media_codec()192   jobject media_codec() { return j_media_codec_.obj(); }
193   MediaCodecDirection direction_;
194 
195  private:
196   // Fills a particular input buffer; returns false if |data_size| exceeds the
197   // input buffer's capacity (and doesn't touch the input buffer in that case).
198   bool FillInputBuffer(int index,
199                        const uint8* data,
200                        size_t data_size) WARN_UNUSED_RESULT;
201 
202   // Java MediaCodec instance.
203   base::android::ScopedJavaGlobalRef<jobject> j_media_codec_;
204 
205   DISALLOW_COPY_AND_ASSIGN(MediaCodecBridge);
206 };
207 
208 class AudioCodecBridge : public MediaCodecBridge {
209  public:
210   // Returns an AudioCodecBridge instance if |codec| is supported, or a NULL
211   // pointer otherwise.
212   static AudioCodecBridge* Create(const AudioCodec& codec);
213 
214   // See MediaCodecBridge::IsKnownUnaccelerated().
215   static bool IsKnownUnaccelerated(const AudioCodec& codec);
216 
217   // Start the audio codec bridge.
218   bool Start(const AudioCodec& codec, int sample_rate, int channel_count,
219              const uint8* extra_data, size_t extra_data_size,
220              bool play_audio, jobject media_crypto) WARN_UNUSED_RESULT;
221 
222   // Play the output buffer. This call must be called after
223   // DequeueOutputBuffer() and before ReleaseOutputBuffer.
224   void PlayOutputBuffer(int index, size_t size);
225 
226   // Set the volume of the audio output.
227   void SetVolume(double volume);
228 
229  private:
230   explicit AudioCodecBridge(const std::string& mime);
231 
232   // Configure the java MediaFormat object with the extra codec data passed in.
233   bool ConfigureMediaFormat(jobject j_format, const AudioCodec& codec,
234                             const uint8* extra_data, size_t extra_data_size);
235 };
236 
237 class MEDIA_EXPORT VideoCodecBridge : public MediaCodecBridge {
238  public:
239   // See MediaCodecBridge::IsKnownUnaccelerated().
240   static bool IsKnownUnaccelerated(const VideoCodec& codec,
241                                    MediaCodecDirection direction);
242 
243   // Create, start, and return a VideoCodecBridge decoder or NULL on failure.
244   static VideoCodecBridge* CreateDecoder(
245       const VideoCodec& codec,  // e.g. media::kCodecVP8
246       bool is_secure,
247       const gfx::Size& size,  // Output frame size.
248       jobject surface,        // Output surface, optional.
249       jobject media_crypto);  // MediaCrypto object, optional.
250 
251   // Create, start, and return a VideoCodecBridge encoder or NULL on failure.
252   static VideoCodecBridge* CreateEncoder(
253       const VideoCodec& codec,  // e.g. media::kCodecVP8
254       const gfx::Size& size,    // input frame size
255       int bit_rate,             // bits/second
256       int frame_rate,           // frames/second
257       int i_frame_interval,     // count
258       int color_format);        // MediaCodecInfo.CodecCapabilities.
259 
260   void SetVideoBitrate(int bps);
261   void RequestKeyFrameSoon();
262 
263  private:
264   VideoCodecBridge(const std::string& mime,
265                    bool is_secure,
266                    MediaCodecDirection direction);
267 };
268 
269 }  // namespace media
270 
271 #endif  // MEDIA_BASE_ANDROID_MEDIA_CODEC_BRIDGE_H_
272