android_media_MediaCodec.h revision 4273dd03a83fd5f9ba25f3b7c3a4add7bce7206c
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 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
118protected:
119    virtual ~JMediaCodec();
120
121    virtual void onMessageReceived(const sp<AMessage> &msg);
122
123private:
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    template <typename T>
150    status_t createByteBufferFromABuffer(
151            JNIEnv *env, bool readOnly, bool clearBuffer, const sp<T> &buffer,
152            jobject *buf) const;
153
154    void cacheJavaObjects(JNIEnv *env);
155    void deleteJavaObjects(JNIEnv *env);
156    void handleCallback(const sp<AMessage> &msg);
157    void handleFrameRenderedNotification(const sp<AMessage> &msg);
158
159    DISALLOW_EVIL_CONSTRUCTORS(JMediaCodec);
160};
161
162}  // namespace android
163
164#endif  // _ANDROID_MEDIA_MEDIACODEC_H_
165