Allow overlapping sync and async startup requests
[chromium-blink-merge.git] / media / base / android / media_codec_bridge.h
blob3469b1804e7525fb423244162284c617169e7bee
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.
5 #ifndef MEDIA_BASE_ANDROID_MEDIA_CODEC_BRIDGE_H_
6 #define MEDIA_BASE_ANDROID_MEDIA_CODEC_BRIDGE_H_
8 #include <jni.h>
9 #include <string>
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"
17 namespace media {
19 struct SubsampleEntry;
21 // This class serves as a bridge for native code to call java functions inside
22 // Android MediaCodec class. For more information on Android MediaCodec, check
23 // http://developer.android.com/reference/android/media/MediaCodec.html
24 // Note: MediaCodec is only available on JB and greater.
25 // Use AudioCodecBridge or VideoCodecBridge to create an instance of this
26 // object.
27 class MEDIA_EXPORT MediaCodecBridge {
28 public:
29 enum DequeueBufferInfo {
30 INFO_OUTPUT_BUFFERS_CHANGED = -3,
31 INFO_OUTPUT_FORMAT_CHANGED = -2,
32 INFO_TRY_AGAIN_LATER = -1,
33 INFO_MEDIA_CODEC_ERROR = -1000,
36 static const base::TimeDelta kTimeOutInfinity;
37 static const base::TimeDelta kTimeOutNoWait;
39 // Returns true if MediaCodec is available on the device.
40 static bool IsAvailable();
42 virtual ~MediaCodecBridge();
44 // Resets both input and output, all indices previously returned in calls to
45 // DequeueInputBuffer() and DequeueOutputBuffer() become invalid.
46 // Please note that this clears all the inputs in the media codec. In other
47 // words, there will be no outputs until new input is provided.
48 void Reset();
50 // Finishes the decode/encode session. The instance remains active
51 // and ready to be StartAudio/Video()ed again. HOWEVER, due to the buggy
52 // vendor's implementation , b/8125974, Stop() -> StartAudio/Video() may not
53 // work on some devices. For reliability, Stop() -> delete and recreate new
54 // instance -> StartAudio/Video() is recommended.
55 void Stop();
57 // Used for getting output format. This is valid after DequeueInputBuffer()
58 // returns a format change by returning INFO_OUTPUT_FORMAT_CHANGED
59 void GetOutputFormat(int* width, int* height);
61 // Submits a byte array to the given input buffer. Call this after getting an
62 // available buffer from DequeueInputBuffer(). Returns the number of bytes
63 // put to the input buffer.
64 size_t QueueInputBuffer(int index, const uint8* data, int size,
65 const base::TimeDelta& presentation_time);
67 // Similar to the above call, but submits a buffer that is encrypted.
68 size_t QueueSecureInputBuffer(
69 int index, const uint8* data, int data_size,
70 const uint8* key_id, int key_id_size,
71 const uint8* iv, int iv_size,
72 const SubsampleEntry* subsamples, int subsamples_size,
73 const base::TimeDelta& presentation_time);
75 // Submits an empty buffer with a EOS (END OF STREAM) flag.
76 void QueueEOS(int input_buffer_index);
78 // Returns an index (>=0) of an input buffer to be filled with valid data,
79 // INFO_TRY_AGAIN_LATER if no such buffer is currently available, or
80 // INFO_MEDIA_CODEC_ERROR if unexpected error happens.
81 // Use kTimeOutInfinity for infinite timeout.
82 int DequeueInputBuffer(base::TimeDelta timeout);
84 // Dequeues an output buffer, block at most timeout_us microseconds.
85 // Returns the index of an output buffer that has been successfully decoded
86 // or one of DequeueBufferInfo above.
87 // Use kTimeOutInfinity for infinite timeout.
88 int DequeueOutputBuffer(
89 base::TimeDelta timeout, size_t* offset, size_t* size,
90 base::TimeDelta* presentation_time, bool* end_of_stream);
92 // Returns the buffer to the codec. If you previously specified a surface
93 // when configuring this video decoder you can optionally render the buffer.
94 void ReleaseOutputBuffer(int index, bool render);
96 // Gets output buffers from media codec and keeps them inside the java class.
97 // To access them, use DequeueOutputBuffer().
98 void GetOutputBuffers();
100 static bool RegisterMediaCodecBridge(JNIEnv* env);
102 protected:
103 explicit MediaCodecBridge(const char* mime);
105 // Calls start() against the media codec instance. Used in StartXXX() after
106 // configuring media codec.
107 void StartInternal();
109 jobject media_codec() { return j_media_codec_.obj(); }
111 private:
112 // Fills a particular input buffer and returns the size of copied data.
113 size_t FillInputBuffer(int index, const uint8* data, int data_size);
115 // Java MediaCodec instance.
116 base::android::ScopedJavaGlobalRef<jobject> j_media_codec_;
118 DISALLOW_COPY_AND_ASSIGN(MediaCodecBridge);
121 class AudioCodecBridge : public MediaCodecBridge {
122 public:
123 // Returns an AudioCodecBridge instance if |codec| is supported, or a NULL
124 // pointer otherwise.
125 static AudioCodecBridge* Create(const AudioCodec codec);
127 // Start the audio codec bridge.
128 bool Start(const AudioCodec codec, int sample_rate, int channel_count,
129 const uint8* extra_data, size_t extra_data_size,
130 bool play_audio, jobject media_crypto);
132 // Play the output buffer. This call must be called after
133 // DequeueOutputBuffer() and before ReleaseOutputBuffer.
134 void PlayOutputBuffer(int index, size_t size);
136 // Set the volume of the audio output.
137 void SetVolume(double volume);
139 private:
140 explicit AudioCodecBridge(const char* mime);
142 // Configure the java MediaFormat object with the extra codec data passed in.
143 bool ConfigureMediaFormat(jobject j_format, const AudioCodec codec,
144 const uint8* extra_data, size_t extra_data_size);
147 class MEDIA_EXPORT VideoCodecBridge : public MediaCodecBridge {
148 public:
149 // Returns an VideoCodecBridge instance if |codec| is supported, or a NULL
150 // pointer otherwise.
151 static VideoCodecBridge* Create(const VideoCodec codec);
153 // Start the video codec bridge.
154 // TODO(qinmin): Pass codec specific data if available.
155 bool Start(const VideoCodec codec, const gfx::Size& size, jobject surface,
156 jobject media_crypto);
158 private:
159 explicit VideoCodecBridge(const char* mime);
162 } // namespace media
164 #endif // MEDIA_BASE_ANDROID_MEDIA_CODEC_BRIDGE_H_