1 /*
2  * Copyright 2012, The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef _ANDROID_MEDIA_MEDIACODEC_H_
18 #define _ANDROID_MEDIA_MEDIACODEC_H_
19 
20 #include <mutex>
21 
22 #include "jni.h"
23 
24 #include <C2Buffer.h>
25 #include <binder/MemoryHeapBase.h>
26 #include <media/MediaCodecBuffer.h>
27 #include <media/MediaMetricsItem.h>
28 #include <media/hardware/CryptoAPI.h>
29 #include <media/stagefright/foundation/ABase.h>
30 #include <media/stagefright/foundation/AHandler.h>
31 #include <utils/Errors.h>
32 
33 class C2Buffer;
34 
35 namespace android {
36 
37 struct ABuffer;
38 struct AccessUnitInfo;
39 struct ALooper;
40 struct AMessage;
41 struct AString;
42 struct ICrypto;
43 class IGraphicBufferProducer;
44 struct MediaCodec;
45 struct PersistentSurface;
46 class Surface;
47 namespace hardware {
48 class HidlMemory;
49 namespace cas {
50 namespace native {
51 namespace V1_0 {
52 struct IDescrambler;
53 }}}}
54 using hardware::cas::native::V1_0::IDescrambler;
55 
56 struct JMediaCodec : public AHandler {
57     JMediaCodec(
58             JNIEnv *env, jobject thiz,
59             const char *name, bool nameIsType, bool encoder, int pid, int uid);
60 
61     status_t initCheck() const;
62 
63     void registerSelf();
64     void release();
65     void releaseAsync();
66 
67     status_t enableOnFirstTunnelFrameReadyListener(jboolean enable);
68 
69     status_t enableOnFrameRenderedListener(jboolean enable);
70 
71     status_t setCallback(jobject cb);
72 
73     status_t configure(
74             const sp<AMessage> &format,
75             const sp<IGraphicBufferProducer> &bufferProducer,
76             const sp<ICrypto> &crypto,
77             const sp<IDescrambler> &descrambler,
78             int flags);
79 
80     status_t setSurface(
81             const sp<IGraphicBufferProducer> &surface);
82 
83     status_t detachOutputSurface();
84 
85     status_t createInputSurface(sp<IGraphicBufferProducer>* bufferProducer);
86     status_t setInputSurface(const sp<PersistentSurface> &surface);
87 
88     status_t start();
89     status_t stop();
90     status_t reset();
91 
92     status_t flush();
93 
94     status_t queueInputBuffer(
95             size_t index,
96             size_t offset, size_t size, int64_t timeUs, uint32_t flags,
97             AString *errorDetailMsg);
98 
99     status_t queueInputBuffers(
100             size_t index,
101             size_t offset,
102             size_t size,
103             const sp<RefBase> &auInfo,
104             AString *errorDetailMsg = NULL);
105 
106     status_t queueSecureInputBuffer(
107             size_t index,
108             size_t offset,
109             const CryptoPlugin::SubSample *subSamples,
110             size_t numSubSamples,
111             const uint8_t key[16],
112             const uint8_t iv[16],
113             CryptoPlugin::Mode mode,
114             const CryptoPlugin::Pattern &pattern,
115             int64_t presentationTimeUs,
116             uint32_t flags,
117             AString *errorDetailMsg);
118 
119     status_t queueSecureInputBuffers(
120             size_t index,
121             size_t offset,
122             size_t size,
123             const sp<RefBase> &auInfos,
124             const sp<RefBase> &cryptoInfos,
125             AString *errorDetailMsg);
126 
127     status_t queueBuffer(
128             size_t index, const std::shared_ptr<C2Buffer> &buffer,
129             const sp<RefBase> &infos, const sp<AMessage> &tunings,
130             AString *errorDetailMsg);
131 
132     status_t queueEncryptedLinearBlock(
133             size_t index,
134             const sp<hardware::HidlMemory> &buffer,
135             size_t offset,
136             size_t size,
137             const sp<RefBase> &infos,
138             const sp<RefBase> &cryptoInfos,
139             const sp<AMessage> &tunings,
140             AString *errorDetailMsg);
141 
142     status_t dequeueInputBuffer(size_t *index, int64_t timeoutUs);
143 
144     status_t dequeueOutputBuffer(
145             JNIEnv *env, jobject bufferInfo, size_t *index, int64_t timeoutUs);
146 
147     status_t releaseOutputBuffer(
148             size_t index, bool render, bool updatePTS, int64_t timestampNs);
149 
150     status_t signalEndOfInputStream();
151 
152     status_t getFormat(JNIEnv *env, bool input, jobject *format) const;
153 
154     status_t getOutputFormat(JNIEnv *env, size_t index, jobject *format) const;
155 
156     status_t getBuffers(
157             JNIEnv *env, bool input, jobjectArray *bufArray) const;
158 
159     status_t getBuffer(
160             JNIEnv *env, bool input, size_t index, jobject *buf) const;
161 
162     status_t getImage(
163             JNIEnv *env, bool input, size_t index, jobject *image) const;
164 
165     status_t getOutputFrame(
166             JNIEnv *env, jobject frame, size_t index) const;
167 
168     status_t getName(JNIEnv *env, jstring *name) const;
169 
170     status_t getCodecInfo(JNIEnv *env, jobject *codecInfo) const;
171 
172     status_t getMetrics(JNIEnv *env, mediametrics::Item * &reply) const;
173 
174     status_t setParameters(const sp<AMessage> &params);
175 
176     void setVideoScalingMode(int mode);
177 
178     void selectAudioPresentation(const int32_t presentationId, const int32_t programId);
179 
180     status_t querySupportedVendorParameters(JNIEnv *env, jobject *names);
181 
182     status_t describeParameter(JNIEnv *env, jstring name, jobject *desc);
183 
184     status_t subscribeToVendorParameters(JNIEnv *env, jobject names);
185 
186     status_t unsubscribeFromVendorParameters(JNIEnv *env, jobject names);
187 
hasCryptoOrDescramblerJMediaCodec188     bool hasCryptoOrDescrambler() { return mHasCryptoOrDescrambler; }
189 
getCryptoJMediaCodec190     const sp<ICrypto> &getCrypto() { return mCrypto; }
191 
192     std::string getExceptionMessage(const char *msg) const;
193 
194 protected:
195     virtual ~JMediaCodec();
196 
197     virtual void onMessageReceived(const sp<AMessage> &msg);
198 
199 private:
200     enum {
201         kWhatCallbackNotify,
202         kWhatFrameRendered,
203         kWhatAsyncReleaseComplete,
204         kWhatFirstTunnelFrameReady,
205     };
206 
207     jclass mClass;
208     jweak mObject;
209     sp<Surface> mSurfaceTextureClient;
210 
211     sp<ALooper> mLooper;
212     sp<MediaCodec> mCodec;
213     AString mNameAtCreation;
214     bool mGraphicOutput{false};
215     bool mHasCryptoOrDescrambler{false};
216     std::once_flag mReleaseFlag;
217     std::once_flag mAsyncReleaseFlag;
218 
219     sp<AMessage> mCallbackNotification;
220     sp<AMessage> mOnFirstTunnelFrameReadyNotification;
221     sp<AMessage> mOnFrameRenderedNotification;
222 
223     status_t mInitStatus;
224 
225     sp<ICrypto> mCrypto;
226 
227     template <typename T>
228     status_t createByteBufferFromABuffer(
229             JNIEnv *env, bool readOnly, bool clearBuffer, const sp<T> &buffer,
230             jobject *buf) const;
231 
232     void handleCallback(const sp<AMessage> &msg);
233     void handleFirstTunnelFrameReadyNotification(const sp<AMessage> &msg);
234     void handleFrameRenderedNotification(const sp<AMessage> &msg);
235 
236     DISALLOW_EVIL_CONSTRUCTORS(JMediaCodec);
237 };
238 
239 }  // namespace android
240 
241 #endif  // _ANDROID_MEDIA_MEDIACODEC_H_
242