blob: f84a16aad7a3d4787288f56db4fa8ff8f934ad1d [file] [log] [blame]
Andreas Huber88572f72012-02-21 11:47:18 -08001/*
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
Andreas Huber9e6bcce2012-04-06 12:14:47 -070022#include <media/hardware/CryptoAPI.h>
Andreas Huber88572f72012-02-21 11:47:18 -080023#include <media/stagefright/foundation/ABase.h>
Andreas Huberaba67132013-10-22 12:40:01 -070024#include <media/stagefright/foundation/AHandler.h>
Andreas Huber88572f72012-02-21 11:47:18 -080025#include <utils/Errors.h>
Andreas Huber88572f72012-02-21 11:47:18 -080026
27namespace android {
28
Lajos Molnar7de28d32014-07-25 07:51:02 -070029struct ABuffer;
Andreas Huber88572f72012-02-21 11:47:18 -080030struct ALooper;
31struct AMessage;
Andreas Huberbfc56f42012-04-19 12:47:07 -070032struct AString;
Andreas Huber8240d922012-04-04 14:06:32 -070033struct ICrypto;
Andy McFaddend47f7d82012-12-18 09:48:38 -080034struct IGraphicBufferProducer;
Andreas Huber88572f72012-02-21 11:47:18 -080035struct MediaCodec;
Mathias Agopian52800612013-02-14 17:11:20 -080036class Surface;
Andreas Huber88572f72012-02-21 11:47:18 -080037
Andreas Huberaba67132013-10-22 12:40:01 -070038struct JMediaCodec : public AHandler {
Andreas Huber88572f72012-02-21 11:47:18 -080039 JMediaCodec(
40 JNIEnv *env, jobject thiz,
41 const char *name, bool nameIsType, bool encoder);
42
43 status_t initCheck() const;
44
Andreas Huberaba67132013-10-22 12:40:01 -070045 void registerSelf();
Chong Zhang128b0122014-03-01 18:04:13 -080046 void release();
Andreas Huberaba67132013-10-22 12:40:01 -070047
Chong Zhang8d5e5562014-07-08 18:49:21 -070048 status_t setCallback(jobject cb);
49
Andreas Huber88572f72012-02-21 11:47:18 -080050 status_t configure(
51 const sp<AMessage> &format,
Andy McFaddend47f7d82012-12-18 09:48:38 -080052 const sp<IGraphicBufferProducer> &bufferProducer,
Andreas Huber8240d922012-04-04 14:06:32 -070053 const sp<ICrypto> &crypto,
Andreas Huber88572f72012-02-21 11:47:18 -080054 int flags);
55
Andy McFadden2621e402013-02-19 07:29:21 -080056 status_t createInputSurface(sp<IGraphicBufferProducer>* bufferProducer);
57
Andreas Huber88572f72012-02-21 11:47:18 -080058 status_t start();
59 status_t stop();
Lajos Molnar1e6e8012014-07-15 16:07:13 -070060 status_t reset();
Andreas Huber88572f72012-02-21 11:47:18 -080061
62 status_t flush();
63
64 status_t queueInputBuffer(
65 size_t index,
Andreas Huberbfc56f42012-04-19 12:47:07 -070066 size_t offset, size_t size, int64_t timeUs, uint32_t flags,
67 AString *errorDetailMsg);
Andreas Huber88572f72012-02-21 11:47:18 -080068
Andreas Huber9e6bcce2012-04-06 12:14:47 -070069 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,
Andreas Huberbfc56f42012-04-19 12:47:07 -070078 uint32_t flags,
79 AString *errorDetailMsg);
Andreas Huber9e6bcce2012-04-06 12:14:47 -070080
Andreas Huber88572f72012-02-21 11:47:18 -080081 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
Lajos Molnar7c513b6b2014-05-08 17:16:45 -070086 status_t releaseOutputBuffer(
87 size_t index, bool render, bool updatePTS, int64_t timestampNs);
Andreas Huber88572f72012-02-21 11:47:18 -080088
Andy McFadden2621e402013-02-19 07:29:21 -080089 status_t signalEndOfInputStream();
90
Lajos Molnard4023112014-07-11 15:12:59 -070091 status_t getFormat(JNIEnv *env, bool input, jobject *format) const;
92
93 status_t getOutputFormat(JNIEnv *env, size_t index, jobject *format) const;
Andreas Huber88572f72012-02-21 11:47:18 -080094
95 status_t getBuffers(
96 JNIEnv *env, bool input, jobjectArray *bufArray) const;
97
Lajos Molnard4023112014-07-11 15:12:59 -070098 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
Martin Storsjo056ef2e2012-09-25 11:53:04 +0300104 status_t getName(JNIEnv *env, jstring *name) const;
105
Andreas Huber226065b2013-08-12 10:14:11 -0700106 status_t setParameters(const sp<AMessage> &params);
107
Andreas Huberb12a5392012-04-30 14:18:33 -0700108 void setVideoScalingMode(int mode);
109
Andreas Huber88572f72012-02-21 11:47:18 -0800110protected:
111 virtual ~JMediaCodec();
112
Andreas Huberaba67132013-10-22 12:40:01 -0700113 virtual void onMessageReceived(const sp<AMessage> &msg);
Chong Zhang8d5e5562014-07-08 18:49:21 -0700114 void handleCallback(const sp<AMessage> &msg);
Andreas Huberaba67132013-10-22 12:40:01 -0700115
Andreas Huber88572f72012-02-21 11:47:18 -0800116private:
Andreas Huberaba67132013-10-22 12:40:01 -0700117 enum {
Chong Zhang8d5e5562014-07-08 18:49:21 -0700118 kWhatCallbackNotify,
Andreas Huberaba67132013-10-22 12:40:01 -0700119 };
120
Andreas Huber88572f72012-02-21 11:47:18 -0800121 jclass mClass;
122 jweak mObject;
Mathias Agopian52800612013-02-14 17:11:20 -0800123 sp<Surface> mSurfaceTextureClient;
Andreas Huber88572f72012-02-21 11:47:18 -0800124
Lajos Molnar7de28d32014-07-25 07:51:02 -0700125 // java objects cached
126 jclass mByteBufferClass;
127 jobject mNativeByteOrderObj;
128 jmethodID mByteBufferOrderMethodID;
129 jmethodID mByteBufferPositionMethodID;
130 jmethodID mByteBufferLimitMethodID;
131 jmethodID mByteBufferAsReadOnlyBufferMethodID;
132
Andreas Huber88572f72012-02-21 11:47:18 -0800133 sp<ALooper> mLooper;
134 sp<MediaCodec> mCodec;
135
Chong Zhang8d5e5562014-07-08 18:49:21 -0700136 sp<AMessage> mCallbackNotification;
Andreas Huberaba67132013-10-22 12:40:01 -0700137
Lajos Molnar7de28d32014-07-25 07:51:02 -0700138 status_t createByteBufferFromABuffer(
139 JNIEnv *env, bool readOnly, bool clearBuffer, const sp<ABuffer> &buffer,
140 jobject *buf) const;
141
142 void cacheJavaObjects(JNIEnv *env);
143 void deleteJavaObjects(JNIEnv *env);
144
Andreas Huber88572f72012-02-21 11:47:18 -0800145 DISALLOW_EVIL_CONSTRUCTORS(JMediaCodec);
146};
147
148} // namespace android
149
150#endif // _ANDROID_MEDIA_MEDIACODEC_H_