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             int64_t presentationTimeUs,
85             uint32_t flags,
86             AString *errorDetailMsg);
87 
88     status_t dequeueInputBuffer(size_t *index, int64_t timeoutUs);
89 
90     status_t dequeueOutputBuffer(
91             JNIEnv *env, jobject bufferInfo, size_t *index, int64_t timeoutUs);
92 
93     status_t releaseOutputBuffer(
94             size_t index, bool render, bool updatePTS, int64_t timestampNs);
95 
96     status_t signalEndOfInputStream();
97 
98     status_t getFormat(JNIEnv *env, bool input, jobject *format) const;
99 
100     status_t getOutputFormat(JNIEnv *env, size_t index, jobject *format) const;
101 
102     status_t getBuffers(
103             JNIEnv *env, bool input, jobjectArray *bufArray) const;
104 
105     status_t getBuffer(
106             JNIEnv *env, bool input, size_t index, jobject *buf) const;
107 
108     status_t getImage(
109             JNIEnv *env, bool input, size_t index, jobject *image) const;
110 
111     status_t getName(JNIEnv *env, jstring *name) const;
112 
113     status_t setParameters(const sp<AMessage> &params);
114 
115     void setVideoScalingMode(int mode);
116 
117 protected:
118     virtual ~JMediaCodec();
119 
120     virtual void onMessageReceived(const sp<AMessage> &msg);
121 
122 private:
123     enum {
124         kWhatCallbackNotify,
125         kWhatFrameRendered,
126     };
127 
128     jclass mClass;
129     jweak mObject;
130     sp<Surface> mSurfaceTextureClient;
131 
132     // java objects cached
133     jclass mByteBufferClass;
134     jobject mNativeByteOrderObj;
135     jmethodID mByteBufferOrderMethodID;
136     jmethodID mByteBufferPositionMethodID;
137     jmethodID mByteBufferLimitMethodID;
138     jmethodID mByteBufferAsReadOnlyBufferMethodID;
139 
140     sp<ALooper> mLooper;
141     sp<MediaCodec> mCodec;
142 
143     sp<AMessage> mCallbackNotification;
144     sp<AMessage> mOnFrameRenderedNotification;
145 
146     status_t mInitStatus;
147 
148     status_t createByteBufferFromABuffer(
149             JNIEnv *env, bool readOnly, bool clearBuffer, const sp<ABuffer> &buffer,
150             jobject *buf) const;
151 
152     void cacheJavaObjects(JNIEnv *env);
153     void deleteJavaObjects(JNIEnv *env);
154     void handleCallback(const sp<AMessage> &msg);
155     void handleFrameRenderedNotification(const sp<AMessage> &msg);
156 
157     DISALLOW_EVIL_CONSTRUCTORS(JMediaCodec);
158 };
159 
160 }  // namespace android
161 
162 #endif  // _ANDROID_MEDIA_MEDIACODEC_H_
163