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 "jni.h"
21 
22 #include <media/hardware/CryptoAPI.h>
23 #include <media/stagefright/foundation/ABase.h>
24 #include <media/stagefright/foundation/AHandler.h>
25 #include <utils/Errors.h>
26 
27 namespace android {
28 
29 struct ABuffer;
30 struct ALooper;
31 struct AMessage;
32 struct AString;
33 struct ICrypto;
34 struct IGraphicBufferProducer;
35 struct MediaCodec;
36 struct PersistentSurface;
37 class Surface;
38 
39 struct JMediaCodec : public AHandler {
40     JMediaCodec(
41             JNIEnv *env, jobject thiz,
42             const char *name, bool nameIsType, bool encoder);
43 
44     status_t initCheck() const;
45 
46     void registerSelf();
47     void release();
48 
49     status_t enableOnFrameRenderedListener(jboolean enable);
50 
51     status_t setCallback(jobject cb);
52 
53     status_t configure(
54             const sp<AMessage> &format,
55             const sp<IGraphicBufferProducer> &bufferProducer,
56             const sp<ICrypto> &crypto,
57             int flags);
58 
59     status_t setSurface(
60             const sp<IGraphicBufferProducer> &surface);
61 
62     status_t createInputSurface(sp<IGraphicBufferProducer>* bufferProducer);
63     status_t setInputSurface(const sp<PersistentSurface> &surface);
64 
65     status_t start();
66     status_t stop();
67     status_t reset();
68 
69     status_t flush();
70 
71     status_t queueInputBuffer(
72             size_t index,
73             size_t offset, size_t size, int64_t timeUs, uint32_t flags,
74             AString *errorDetailMsg);
75 
76     status_t queueSecureInputBuffer(
77             size_t index,
78             size_t offset,
79             const CryptoPlugin::SubSample *subSamples,
80             size_t numSubSamples,
81             const uint8_t key[16],
82             const uint8_t iv[16],
83             CryptoPlugin::Mode mode,
84             const CryptoPlugin::Pattern &pattern,
85             int64_t presentationTimeUs,
86             uint32_t flags,
87             AString *errorDetailMsg);
88 
89     status_t dequeueInputBuffer(size_t *index, int64_t timeoutUs);
90 
91     status_t dequeueOutputBuffer(
92             JNIEnv *env, jobject bufferInfo, size_t *index, int64_t timeoutUs);
93 
94     status_t releaseOutputBuffer(
95             size_t index, bool render, bool updatePTS, int64_t timestampNs);
96 
97     status_t signalEndOfInputStream();
98 
99     status_t getFormat(JNIEnv *env, bool input, jobject *format) const;
100 
101     status_t getOutputFormat(JNIEnv *env, size_t index, jobject *format) const;
102 
103     status_t getBuffers(
104             JNIEnv *env, bool input, jobjectArray *bufArray) const;
105 
106     status_t getBuffer(
107             JNIEnv *env, bool input, size_t index, jobject *buf) const;
108 
109     status_t getImage(
110             JNIEnv *env, bool input, size_t index, jobject *image) const;
111 
112     status_t getName(JNIEnv *env, jstring *name) const;
113 
114     status_t setParameters(const sp<AMessage> &params);
115 
116     void setVideoScalingMode(int mode);
117 
118 protected:
119     virtual ~JMediaCodec();
120 
121     virtual void onMessageReceived(const sp<AMessage> &msg);
122 
123 private:
124     enum {
125         kWhatCallbackNotify,
126         kWhatFrameRendered,
127     };
128 
129     jclass mClass;
130     jweak mObject;
131     sp<Surface> mSurfaceTextureClient;
132 
133     // java objects cached
134     jclass mByteBufferClass;
135     jobject mNativeByteOrderObj;
136     jmethodID mByteBufferOrderMethodID;
137     jmethodID mByteBufferPositionMethodID;
138     jmethodID mByteBufferLimitMethodID;
139     jmethodID mByteBufferAsReadOnlyBufferMethodID;
140 
141     sp<ALooper> mLooper;
142     sp<MediaCodec> mCodec;
143 
144     sp<AMessage> mCallbackNotification;
145     sp<AMessage> mOnFrameRenderedNotification;
146 
147     status_t mInitStatus;
148 
149     status_t createByteBufferFromABuffer(
150             JNIEnv *env, bool readOnly, bool clearBuffer, const sp<ABuffer> &buffer,
151             jobject *buf) const;
152 
153     void cacheJavaObjects(JNIEnv *env);
154     void deleteJavaObjects(JNIEnv *env);
155     void handleCallback(const sp<AMessage> &msg);
156     void handleFrameRenderedNotification(const sp<AMessage> &msg);
157 
158     DISALLOW_EVIL_CONSTRUCTORS(JMediaCodec);
159 };
160 
161 }  // namespace android
162 
163 #endif  // _ANDROID_MEDIA_MEDIACODEC_H_
164