188572f7a3e9d7ef85c26865a0150f3c2041561c2Andreas Huber/*
288572f7a3e9d7ef85c26865a0150f3c2041561c2Andreas Huber * Copyright 2012, The Android Open Source Project
388572f7a3e9d7ef85c26865a0150f3c2041561c2Andreas Huber *
488572f7a3e9d7ef85c26865a0150f3c2041561c2Andreas Huber * Licensed under the Apache License, Version 2.0 (the "License");
588572f7a3e9d7ef85c26865a0150f3c2041561c2Andreas Huber * you may not use this file except in compliance with the License.
688572f7a3e9d7ef85c26865a0150f3c2041561c2Andreas Huber * You may obtain a copy of the License at
788572f7a3e9d7ef85c26865a0150f3c2041561c2Andreas Huber *
888572f7a3e9d7ef85c26865a0150f3c2041561c2Andreas Huber *     http://www.apache.org/licenses/LICENSE-2.0
988572f7a3e9d7ef85c26865a0150f3c2041561c2Andreas Huber *
1088572f7a3e9d7ef85c26865a0150f3c2041561c2Andreas Huber * Unless required by applicable law or agreed to in writing, software
1188572f7a3e9d7ef85c26865a0150f3c2041561c2Andreas Huber * distributed under the License is distributed on an "AS IS" BASIS,
1288572f7a3e9d7ef85c26865a0150f3c2041561c2Andreas Huber * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1388572f7a3e9d7ef85c26865a0150f3c2041561c2Andreas Huber * See the License for the specific language governing permissions and
1488572f7a3e9d7ef85c26865a0150f3c2041561c2Andreas Huber * limitations under the License.
1588572f7a3e9d7ef85c26865a0150f3c2041561c2Andreas Huber */
1688572f7a3e9d7ef85c26865a0150f3c2041561c2Andreas Huber
1788572f7a3e9d7ef85c26865a0150f3c2041561c2Andreas Huber#ifndef _ANDROID_MEDIA_MEDIACODEC_H_
1888572f7a3e9d7ef85c26865a0150f3c2041561c2Andreas Huber#define _ANDROID_MEDIA_MEDIACODEC_H_
1988572f7a3e9d7ef85c26865a0150f3c2041561c2Andreas Huber
2088572f7a3e9d7ef85c26865a0150f3c2041561c2Andreas Huber#include "jni.h"
2188572f7a3e9d7ef85c26865a0150f3c2041561c2Andreas Huber
22f2d0e40bf01fa133dd6b36f3716005d53fa9776cRay Essick#include <media/MediaAnalyticsItem.h>
239e6bcce17d13bd4f7bba5f8fbcc2e6a0d695274bAndreas Huber#include <media/hardware/CryptoAPI.h>
2488572f7a3e9d7ef85c26865a0150f3c2041561c2Andreas Huber#include <media/stagefright/foundation/ABase.h>
25aba671392d6606e35726c350a28d0c9b36ebfe16Andreas Huber#include <media/stagefright/foundation/AHandler.h>
2688572f7a3e9d7ef85c26865a0150f3c2041561c2Andreas Huber#include <utils/Errors.h>
2788572f7a3e9d7ef85c26865a0150f3c2041561c2Andreas Huber
2888572f7a3e9d7ef85c26865a0150f3c2041561c2Andreas Hubernamespace android {
2988572f7a3e9d7ef85c26865a0150f3c2041561c2Andreas Huber
307de28d34f1ca3a727a8325cf3304f2fe03d2ac59Lajos Molnarstruct ABuffer;
3188572f7a3e9d7ef85c26865a0150f3c2041561c2Andreas Huberstruct ALooper;
3288572f7a3e9d7ef85c26865a0150f3c2041561c2Andreas Huberstruct AMessage;
33bfc56f497de0d18125366a02a93e61cdc0891be4Andreas Huberstruct AString;
348240d9239d9aabed75c49f9d4d69fd8a5fe4c899Andreas Huberstruct ICrypto;
3508f5f1f03a4607c67058cb764a0a85be11a3d33dColin Crossclass IGraphicBufferProducer;
3688572f7a3e9d7ef85c26865a0150f3c2041561c2Andreas Huberstruct MediaCodec;
378034d60f6e044deafe48501dfbfe17c31284bc17Chong Zhangstruct PersistentSurface;
3852800617946c456e78ed010c82d0ec4358368164Mathias Agopianclass Surface;
39d5927ae8833780395f8fc5070d0a0a4f8f668292Chong Zhangnamespace media {
40d5927ae8833780395f8fc5070d0a0a4f8f668292Chong Zhangclass IDescrambler;
41d5927ae8833780395f8fc5070d0a0a4f8f668292Chong Zhang};
42d5927ae8833780395f8fc5070d0a0a4f8f668292Chong Zhangusing namespace media;
4388572f7a3e9d7ef85c26865a0150f3c2041561c2Andreas Huber
44aba671392d6606e35726c350a28d0c9b36ebfe16Andreas Huberstruct JMediaCodec : public AHandler {
4588572f7a3e9d7ef85c26865a0150f3c2041561c2Andreas Huber    JMediaCodec(
4688572f7a3e9d7ef85c26865a0150f3c2041561c2Andreas Huber            JNIEnv *env, jobject thiz,
4788572f7a3e9d7ef85c26865a0150f3c2041561c2Andreas Huber            const char *name, bool nameIsType, bool encoder);
4888572f7a3e9d7ef85c26865a0150f3c2041561c2Andreas Huber
4988572f7a3e9d7ef85c26865a0150f3c2041561c2Andreas Huber    status_t initCheck() const;
5088572f7a3e9d7ef85c26865a0150f3c2041561c2Andreas Huber
51aba671392d6606e35726c350a28d0c9b36ebfe16Andreas Huber    void registerSelf();
52128b012cc8c2369136bb8450bc91c81aebe18506Chong Zhang    void release();
53aba671392d6606e35726c350a28d0c9b36ebfe16Andreas Huber
54d8578577b02cf6360402eb8726e964d18b46434dLajos Molnar    status_t enableOnFrameRenderedListener(jboolean enable);
55d8578577b02cf6360402eb8726e964d18b46434dLajos Molnar
568d5e556b2b76278000a506382fdf594c071e1fbdChong Zhang    status_t setCallback(jobject cb);
578d5e556b2b76278000a506382fdf594c071e1fbdChong Zhang
5888572f7a3e9d7ef85c26865a0150f3c2041561c2Andreas Huber    status_t configure(
5988572f7a3e9d7ef85c26865a0150f3c2041561c2Andreas Huber            const sp<AMessage> &format,
60d47f7d8b5fe3a3861d7cbdc5f912235407823c8eAndy McFadden            const sp<IGraphicBufferProducer> &bufferProducer,
618240d9239d9aabed75c49f9d4d69fd8a5fe4c899Andreas Huber            const sp<ICrypto> &crypto,
62d5927ae8833780395f8fc5070d0a0a4f8f668292Chong Zhang            const sp<IDescrambler> &descrambler,
6388572f7a3e9d7ef85c26865a0150f3c2041561c2Andreas Huber            int flags);
6488572f7a3e9d7ef85c26865a0150f3c2041561c2Andreas Huber
655e02ba977ede1e38c9f206057ec3f0f1e89ba943Lajos Molnar    status_t setSurface(
665e02ba977ede1e38c9f206057ec3f0f1e89ba943Lajos Molnar            const sp<IGraphicBufferProducer> &surface);
675e02ba977ede1e38c9f206057ec3f0f1e89ba943Lajos Molnar
682621e40d0e0a496a96575768b7e2b70e3b3be640Andy McFadden    status_t createInputSurface(sp<IGraphicBufferProducer>* bufferProducer);
699560ddb48af0e2da7743452f8d9d6d9cd34d8438Chong Zhang    status_t setInputSurface(const sp<PersistentSurface> &surface);
702621e40d0e0a496a96575768b7e2b70e3b3be640Andy McFadden
7188572f7a3e9d7ef85c26865a0150f3c2041561c2Andreas Huber    status_t start();
7288572f7a3e9d7ef85c26865a0150f3c2041561c2Andreas Huber    status_t stop();
731e6e8018a4f914210b615bfca0f818fd13574228Lajos Molnar    status_t reset();
7488572f7a3e9d7ef85c26865a0150f3c2041561c2Andreas Huber
7588572f7a3e9d7ef85c26865a0150f3c2041561c2Andreas Huber    status_t flush();
7688572f7a3e9d7ef85c26865a0150f3c2041561c2Andreas Huber
7788572f7a3e9d7ef85c26865a0150f3c2041561c2Andreas Huber    status_t queueInputBuffer(
7888572f7a3e9d7ef85c26865a0150f3c2041561c2Andreas Huber            size_t index,
79bfc56f497de0d18125366a02a93e61cdc0891be4Andreas Huber            size_t offset, size_t size, int64_t timeUs, uint32_t flags,
80bfc56f497de0d18125366a02a93e61cdc0891be4Andreas Huber            AString *errorDetailMsg);
8188572f7a3e9d7ef85c26865a0150f3c2041561c2Andreas Huber
829e6bcce17d13bd4f7bba5f8fbcc2e6a0d695274bAndreas Huber    status_t queueSecureInputBuffer(
839e6bcce17d13bd4f7bba5f8fbcc2e6a0d695274bAndreas Huber            size_t index,
849e6bcce17d13bd4f7bba5f8fbcc2e6a0d695274bAndreas Huber            size_t offset,
859e6bcce17d13bd4f7bba5f8fbcc2e6a0d695274bAndreas Huber            const CryptoPlugin::SubSample *subSamples,
869e6bcce17d13bd4f7bba5f8fbcc2e6a0d695274bAndreas Huber            size_t numSubSamples,
879e6bcce17d13bd4f7bba5f8fbcc2e6a0d695274bAndreas Huber            const uint8_t key[16],
889e6bcce17d13bd4f7bba5f8fbcc2e6a0d695274bAndreas Huber            const uint8_t iv[16],
899e6bcce17d13bd4f7bba5f8fbcc2e6a0d695274bAndreas Huber            CryptoPlugin::Mode mode,
90d4ea5d3eb630476a43da2adb4ac518360bd64758Jeff Tinker            const CryptoPlugin::Pattern &pattern,
919e6bcce17d13bd4f7bba5f8fbcc2e6a0d695274bAndreas Huber            int64_t presentationTimeUs,
92bfc56f497de0d18125366a02a93e61cdc0891be4Andreas Huber            uint32_t flags,
93bfc56f497de0d18125366a02a93e61cdc0891be4Andreas Huber            AString *errorDetailMsg);
949e6bcce17d13bd4f7bba5f8fbcc2e6a0d695274bAndreas Huber
9588572f7a3e9d7ef85c26865a0150f3c2041561c2Andreas Huber    status_t dequeueInputBuffer(size_t *index, int64_t timeoutUs);
9688572f7a3e9d7ef85c26865a0150f3c2041561c2Andreas Huber
9788572f7a3e9d7ef85c26865a0150f3c2041561c2Andreas Huber    status_t dequeueOutputBuffer(
9888572f7a3e9d7ef85c26865a0150f3c2041561c2Andreas Huber            JNIEnv *env, jobject bufferInfo, size_t *index, int64_t timeoutUs);
9988572f7a3e9d7ef85c26865a0150f3c2041561c2Andreas Huber
1007c513b6bef8ed4dfc28e0af6c8594563fdb9f436Lajos Molnar    status_t releaseOutputBuffer(
1017c513b6bef8ed4dfc28e0af6c8594563fdb9f436Lajos Molnar            size_t index, bool render, bool updatePTS, int64_t timestampNs);
10288572f7a3e9d7ef85c26865a0150f3c2041561c2Andreas Huber
1032621e40d0e0a496a96575768b7e2b70e3b3be640Andy McFadden    status_t signalEndOfInputStream();
1042621e40d0e0a496a96575768b7e2b70e3b3be640Andy McFadden
105d4023114e8cf7ec7db4d07958a303699b658f2c0Lajos Molnar    status_t getFormat(JNIEnv *env, bool input, jobject *format) const;
106d4023114e8cf7ec7db4d07958a303699b658f2c0Lajos Molnar
107d4023114e8cf7ec7db4d07958a303699b658f2c0Lajos Molnar    status_t getOutputFormat(JNIEnv *env, size_t index, jobject *format) const;
10888572f7a3e9d7ef85c26865a0150f3c2041561c2Andreas Huber
10988572f7a3e9d7ef85c26865a0150f3c2041561c2Andreas Huber    status_t getBuffers(
11088572f7a3e9d7ef85c26865a0150f3c2041561c2Andreas Huber            JNIEnv *env, bool input, jobjectArray *bufArray) const;
11188572f7a3e9d7ef85c26865a0150f3c2041561c2Andreas Huber
112d4023114e8cf7ec7db4d07958a303699b658f2c0Lajos Molnar    status_t getBuffer(
113d4023114e8cf7ec7db4d07958a303699b658f2c0Lajos Molnar            JNIEnv *env, bool input, size_t index, jobject *buf) const;
114d4023114e8cf7ec7db4d07958a303699b658f2c0Lajos Molnar
115d4023114e8cf7ec7db4d07958a303699b658f2c0Lajos Molnar    status_t getImage(
116d4023114e8cf7ec7db4d07958a303699b658f2c0Lajos Molnar            JNIEnv *env, bool input, size_t index, jobject *image) const;
117d4023114e8cf7ec7db4d07958a303699b658f2c0Lajos Molnar
118056ef2ed5ff92b742fe53f2498c596ec1f6bed3cMartin Storsjo    status_t getName(JNIEnv *env, jstring *name) const;
119056ef2ed5ff92b742fe53f2498c596ec1f6bed3cMartin Storsjo
120f2d0e40bf01fa133dd6b36f3716005d53fa9776cRay Essick    status_t getMetrics(JNIEnv *env, MediaAnalyticsItem * &reply) const;
1210e0fee1e3a04023c9ec122d3bd7da54e9565c3e7Ray Essick
122226065bbe60cf32b33a5f86d27e2db88138e4486Andreas Huber    status_t setParameters(const sp<AMessage> &params);
123226065bbe60cf32b33a5f86d27e2db88138e4486Andreas Huber
124b12a5390694e8525ac61d7377e266e31d124c815Andreas Huber    void setVideoScalingMode(int mode);
125b12a5390694e8525ac61d7377e266e31d124c815Andreas Huber
12688572f7a3e9d7ef85c26865a0150f3c2041561c2Andreas Huberprotected:
12788572f7a3e9d7ef85c26865a0150f3c2041561c2Andreas Huber    virtual ~JMediaCodec();
12888572f7a3e9d7ef85c26865a0150f3c2041561c2Andreas Huber
129aba671392d6606e35726c350a28d0c9b36ebfe16Andreas Huber    virtual void onMessageReceived(const sp<AMessage> &msg);
130aba671392d6606e35726c350a28d0c9b36ebfe16Andreas Huber
13188572f7a3e9d7ef85c26865a0150f3c2041561c2Andreas Huberprivate:
132aba671392d6606e35726c350a28d0c9b36ebfe16Andreas Huber    enum {
1338d5e556b2b76278000a506382fdf594c071e1fbdChong Zhang        kWhatCallbackNotify,
134d8578577b02cf6360402eb8726e964d18b46434dLajos Molnar        kWhatFrameRendered,
135aba671392d6606e35726c350a28d0c9b36ebfe16Andreas Huber    };
136aba671392d6606e35726c350a28d0c9b36ebfe16Andreas Huber
13788572f7a3e9d7ef85c26865a0150f3c2041561c2Andreas Huber    jclass mClass;
13888572f7a3e9d7ef85c26865a0150f3c2041561c2Andreas Huber    jweak mObject;
13952800617946c456e78ed010c82d0ec4358368164Mathias Agopian    sp<Surface> mSurfaceTextureClient;
14088572f7a3e9d7ef85c26865a0150f3c2041561c2Andreas Huber
1417de28d34f1ca3a727a8325cf3304f2fe03d2ac59Lajos Molnar    // java objects cached
1427de28d34f1ca3a727a8325cf3304f2fe03d2ac59Lajos Molnar    jclass mByteBufferClass;
1437de28d34f1ca3a727a8325cf3304f2fe03d2ac59Lajos Molnar    jobject mNativeByteOrderObj;
1447de28d34f1ca3a727a8325cf3304f2fe03d2ac59Lajos Molnar    jmethodID mByteBufferOrderMethodID;
1457de28d34f1ca3a727a8325cf3304f2fe03d2ac59Lajos Molnar    jmethodID mByteBufferPositionMethodID;
1467de28d34f1ca3a727a8325cf3304f2fe03d2ac59Lajos Molnar    jmethodID mByteBufferLimitMethodID;
1477de28d34f1ca3a727a8325cf3304f2fe03d2ac59Lajos Molnar    jmethodID mByteBufferAsReadOnlyBufferMethodID;
1487de28d34f1ca3a727a8325cf3304f2fe03d2ac59Lajos Molnar
14988572f7a3e9d7ef85c26865a0150f3c2041561c2Andreas Huber    sp<ALooper> mLooper;
15088572f7a3e9d7ef85c26865a0150f3c2041561c2Andreas Huber    sp<MediaCodec> mCodec;
15188572f7a3e9d7ef85c26865a0150f3c2041561c2Andreas Huber
1528d5e556b2b76278000a506382fdf594c071e1fbdChong Zhang    sp<AMessage> mCallbackNotification;
153d8578577b02cf6360402eb8726e964d18b46434dLajos Molnar    sp<AMessage> mOnFrameRenderedNotification;
154aba671392d6606e35726c350a28d0c9b36ebfe16Andreas Huber
1555f9aa0bcea8be860fdf5a35476435616cb5f44f3Andy Hung    status_t mInitStatus;
1565f9aa0bcea8be860fdf5a35476435616cb5f44f3Andy Hung
1574273dd03a83fd5f9ba25f3b7c3a4add7bce7206cWonsik Kim    template <typename T>
1587de28d34f1ca3a727a8325cf3304f2fe03d2ac59Lajos Molnar    status_t createByteBufferFromABuffer(
1594273dd03a83fd5f9ba25f3b7c3a4add7bce7206cWonsik Kim            JNIEnv *env, bool readOnly, bool clearBuffer, const sp<T> &buffer,
1607de28d34f1ca3a727a8325cf3304f2fe03d2ac59Lajos Molnar            jobject *buf) const;
1617de28d34f1ca3a727a8325cf3304f2fe03d2ac59Lajos Molnar
1627de28d34f1ca3a727a8325cf3304f2fe03d2ac59Lajos Molnar    void cacheJavaObjects(JNIEnv *env);
1637de28d34f1ca3a727a8325cf3304f2fe03d2ac59Lajos Molnar    void deleteJavaObjects(JNIEnv *env);
164d8578577b02cf6360402eb8726e964d18b46434dLajos Molnar    void handleCallback(const sp<AMessage> &msg);
165d8578577b02cf6360402eb8726e964d18b46434dLajos Molnar    void handleFrameRenderedNotification(const sp<AMessage> &msg);
1667de28d34f1ca3a727a8325cf3304f2fe03d2ac59Lajos Molnar
16788572f7a3e9d7ef85c26865a0150f3c2041561c2Andreas Huber    DISALLOW_EVIL_CONSTRUCTORS(JMediaCodec);
16888572f7a3e9d7ef85c26865a0150f3c2041561c2Andreas Huber};
16988572f7a3e9d7ef85c26865a0150f3c2041561c2Andreas Huber
17088572f7a3e9d7ef85c26865a0150f3c2041561c2Andreas Huber}  // namespace android
17188572f7a3e9d7ef85c26865a0150f3c2041561c2Andreas Huber
17288572f7a3e9d7ef85c26865a0150f3c2041561c2Andreas Huber#endif  // _ANDROID_MEDIA_MEDIACODEC_H_
173