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