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