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 ALooper;
39 struct AMessage;
40 struct AString;
41 struct ICrypto;
42 class IGraphicBufferProducer;
43 struct MediaCodec;
44 struct PersistentSurface;
45 class Surface;
46 namespace hardware {
47 class HidlMemory;
48 namespace cas {
49 namespace native {
50 namespace V1_0 {
51 struct IDescrambler;
52 }}}}
53 using hardware::cas::native::V1_0::IDescrambler;
54 
55 struct JMediaCodec : public AHandler {
56     JMediaCodec(
57             JNIEnv *env, jobject thiz,
58             const char *name, bool nameIsType, bool encoder);
59 
60     status_t initCheck() const;
61 
62     void registerSelf();
63     void release();
64     void releaseAsync();
65 
66     status_t enableOnFrameRenderedListener(jboolean enable);
67 
68     status_t setCallback(jobject cb);
69 
70     status_t configure(
71             const sp<AMessage> &format,
72             const sp<IGraphicBufferProducer> &bufferProducer,
73             const sp<ICrypto> &crypto,
74             const sp<IDescrambler> &descrambler,
75             int flags);
76 
77     status_t setSurface(
78             const sp<IGraphicBufferProducer> &surface);
79 
80     status_t createInputSurface(sp<IGraphicBufferProducer>* bufferProducer);
81     status_t setInputSurface(const sp<PersistentSurface> &surface);
82 
83     status_t start();
84     status_t stop();
85     status_t reset();
86 
87     status_t flush();
88 
89     status_t queueInputBuffer(
90             size_t index,
91             size_t offset, size_t size, int64_t timeUs, uint32_t flags,
92             AString *errorDetailMsg);
93 
94     status_t queueSecureInputBuffer(
95             size_t index,
96             size_t offset,
97             const CryptoPlugin::SubSample *subSamples,
98             size_t numSubSamples,
99             const uint8_t key[16],
100             const uint8_t iv[16],
101             CryptoPlugin::Mode mode,
102             const CryptoPlugin::Pattern &pattern,
103             int64_t presentationTimeUs,
104             uint32_t flags,
105             AString *errorDetailMsg);
106 
107     status_t queueBuffer(
108             size_t index, const std::shared_ptr<C2Buffer> &buffer,
109             int64_t timeUs, uint32_t flags, const sp<AMessage> &tunings,
110             AString *errorDetailMsg);
111 
112     status_t queueEncryptedLinearBlock(
113             size_t index,
114             const sp<hardware::HidlMemory> &buffer,
115             size_t offset,
116             const CryptoPlugin::SubSample *subSamples,
117             size_t numSubSamples,
118             const uint8_t key[16],
119             const uint8_t iv[16],
120             CryptoPlugin::Mode mode,
121             const CryptoPlugin::Pattern &pattern,
122             int64_t presentationTimeUs,
123             uint32_t flags,
124             const sp<AMessage> &tunings,
125             AString *errorDetailMsg);
126 
127     status_t dequeueInputBuffer(size_t *index, int64_t timeoutUs);
128 
129     status_t dequeueOutputBuffer(
130             JNIEnv *env, jobject bufferInfo, size_t *index, int64_t timeoutUs);
131 
132     status_t releaseOutputBuffer(
133             size_t index, bool render, bool updatePTS, int64_t timestampNs);
134 
135     status_t signalEndOfInputStream();
136 
137     status_t getFormat(JNIEnv *env, bool input, jobject *format) const;
138 
139     status_t getOutputFormat(JNIEnv *env, size_t index, jobject *format) const;
140 
141     status_t getBuffers(
142             JNIEnv *env, bool input, jobjectArray *bufArray) const;
143 
144     status_t getBuffer(
145             JNIEnv *env, bool input, size_t index, jobject *buf) const;
146 
147     status_t getImage(
148             JNIEnv *env, bool input, size_t index, jobject *image) const;
149 
150     status_t getOutputFrame(
151             JNIEnv *env, jobject frame, size_t index) const;
152 
153     status_t getName(JNIEnv *env, jstring *name) const;
154 
155     status_t getCodecInfo(JNIEnv *env, jobject *codecInfo) const;
156 
157     status_t getMetrics(JNIEnv *env, mediametrics::Item * &reply) const;
158 
159     status_t setParameters(const sp<AMessage> &params);
160 
161     void setVideoScalingMode(int mode);
162 
163     void selectAudioPresentation(const int32_t presentationId, const int32_t programId);
164 
hasCryptoOrDescramblerJMediaCodec165     bool hasCryptoOrDescrambler() { return mHasCryptoOrDescrambler; }
166 
167 protected:
168     virtual ~JMediaCodec();
169 
170     virtual void onMessageReceived(const sp<AMessage> &msg);
171 
172 private:
173     enum {
174         kWhatCallbackNotify,
175         kWhatFrameRendered,
176         kWhatAsyncReleaseComplete,
177     };
178 
179     jclass mClass;
180     jweak mObject;
181     sp<Surface> mSurfaceTextureClient;
182 
183     sp<ALooper> mLooper;
184     sp<MediaCodec> mCodec;
185     AString mNameAtCreation;
186     bool mGraphicOutput{false};
187     bool mHasCryptoOrDescrambler{false};
188     std::once_flag mReleaseFlag;
189     std::once_flag mAsyncReleaseFlag;
190 
191     sp<AMessage> mCallbackNotification;
192     sp<AMessage> mOnFrameRenderedNotification;
193 
194     status_t mInitStatus;
195 
196     template <typename T>
197     status_t createByteBufferFromABuffer(
198             JNIEnv *env, bool readOnly, bool clearBuffer, const sp<T> &buffer,
199             jobject *buf) const;
200 
201     void handleCallback(const sp<AMessage> &msg);
202     void handleFrameRenderedNotification(const sp<AMessage> &msg);
203 
204     DISALLOW_EVIL_CONSTRUCTORS(JMediaCodec);
205 };
206 
207 }  // namespace android
208 
209 #endif  // _ANDROID_MEDIA_MEDIACODEC_H_
210