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