blob: a8c76c5e61f7661ddffe1b42505c16d23ceed10e [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;
Colin Cross08f5f1f2016-09-30 17:36:46 -070034class IGraphicBufferProducer;
Andreas Huber88572f72012-02-21 11:47:18 -080035struct MediaCodec;
Chong Zhang8034d602015-04-28 13:38:48 -070036struct PersistentSurface;
Mathias Agopian52800612013-02-14 17:11:20 -080037class Surface;
Chong Zhangd5927ae2017-01-03 11:07:18 -080038namespace media {
39class IDescrambler;
40};
41using namespace media;
Andreas Huber88572f72012-02-21 11:47:18 -080042
Andreas Huberaba67132013-10-22 12:40:01 -070043struct JMediaCodec : public AHandler {
Andreas Huber88572f72012-02-21 11:47:18 -080044 JMediaCodec(
45 JNIEnv *env, jobject thiz,
46 const char *name, bool nameIsType, bool encoder);
47
48 status_t initCheck() const;
49
Andreas Huberaba67132013-10-22 12:40:01 -070050 void registerSelf();
Chong Zhang128b0122014-03-01 18:04:13 -080051 void release();
Andreas Huberaba67132013-10-22 12:40:01 -070052
Lajos Molnard8578572015-06-05 20:17:33 -070053 status_t enableOnFrameRenderedListener(jboolean enable);
54
Chong Zhang8d5e5562014-07-08 18:49:21 -070055 status_t setCallback(jobject cb);
56
Andreas Huber88572f72012-02-21 11:47:18 -080057 status_t configure(
58 const sp<AMessage> &format,
Andy McFaddend47f7d82012-12-18 09:48:38 -080059 const sp<IGraphicBufferProducer> &bufferProducer,
Andreas Huber8240d922012-04-04 14:06:32 -070060 const sp<ICrypto> &crypto,
Chong Zhangd5927ae2017-01-03 11:07:18 -080061 const sp<IDescrambler> &descrambler,
Andreas Huber88572f72012-02-21 11:47:18 -080062 int flags);
63
Lajos Molnar5e02ba92015-05-01 15:59:35 -070064 status_t setSurface(
65 const sp<IGraphicBufferProducer> &surface);
66
Andy McFadden2621e402013-02-19 07:29:21 -080067 status_t createInputSurface(sp<IGraphicBufferProducer>* bufferProducer);
Chong Zhang9560ddb2015-05-13 10:25:29 -070068 status_t setInputSurface(const sp<PersistentSurface> &surface);
Andy McFadden2621e402013-02-19 07:29:21 -080069
Andreas Huber88572f72012-02-21 11:47:18 -080070 status_t start();
71 status_t stop();
Lajos Molnar1e6e8012014-07-15 16:07:13 -070072 status_t reset();
Andreas Huber88572f72012-02-21 11:47:18 -080073
74 status_t flush();
75
76 status_t queueInputBuffer(
77 size_t index,
Andreas Huberbfc56f42012-04-19 12:47:07 -070078 size_t offset, size_t size, int64_t timeUs, uint32_t flags,
79 AString *errorDetailMsg);
Andreas Huber88572f72012-02-21 11:47:18 -080080
Andreas Huber9e6bcce2012-04-06 12:14:47 -070081 status_t queueSecureInputBuffer(
82 size_t index,
83 size_t offset,
84 const CryptoPlugin::SubSample *subSamples,
85 size_t numSubSamples,
86 const uint8_t key[16],
87 const uint8_t iv[16],
88 CryptoPlugin::Mode mode,
Jeff Tinkerd4ea5d32015-12-18 11:56:22 -080089 const CryptoPlugin::Pattern &pattern,
Andreas Huber9e6bcce2012-04-06 12:14:47 -070090 int64_t presentationTimeUs,
Andreas Huberbfc56f42012-04-19 12:47:07 -070091 uint32_t flags,
92 AString *errorDetailMsg);
Andreas Huber9e6bcce2012-04-06 12:14:47 -070093
Andreas Huber88572f72012-02-21 11:47:18 -080094 status_t dequeueInputBuffer(size_t *index, int64_t timeoutUs);
95
96 status_t dequeueOutputBuffer(
97 JNIEnv *env, jobject bufferInfo, size_t *index, int64_t timeoutUs);
98
Lajos Molnar7c513b6b2014-05-08 17:16:45 -070099 status_t releaseOutputBuffer(
100 size_t index, bool render, bool updatePTS, int64_t timestampNs);
Andreas Huber88572f72012-02-21 11:47:18 -0800101
Andy McFadden2621e402013-02-19 07:29:21 -0800102 status_t signalEndOfInputStream();
103
Lajos Molnard4023112014-07-11 15:12:59 -0700104 status_t getFormat(JNIEnv *env, bool input, jobject *format) const;
105
106 status_t getOutputFormat(JNIEnv *env, size_t index, jobject *format) const;
Andreas Huber88572f72012-02-21 11:47:18 -0800107
108 status_t getBuffers(
109 JNIEnv *env, bool input, jobjectArray *bufArray) const;
110
Lajos Molnard4023112014-07-11 15:12:59 -0700111 status_t getBuffer(
112 JNIEnv *env, bool input, size_t index, jobject *buf) const;
113
114 status_t getImage(
115 JNIEnv *env, bool input, size_t index, jobject *image) const;
116
Martin Storsjo056ef2e2012-09-25 11:53:04 +0300117 status_t getName(JNIEnv *env, jstring *name) const;
118
Ray Essick0e0fee12017-01-25 18:01:56 -0800119 status_t getMetrics(JNIEnv *env, Parcel *reply) const;
120
Andreas Huber226065b2013-08-12 10:14:11 -0700121 status_t setParameters(const sp<AMessage> &params);
122
Andreas Huberb12a5392012-04-30 14:18:33 -0700123 void setVideoScalingMode(int mode);
124
Andreas Huber88572f72012-02-21 11:47:18 -0800125protected:
126 virtual ~JMediaCodec();
127
Andreas Huberaba67132013-10-22 12:40:01 -0700128 virtual void onMessageReceived(const sp<AMessage> &msg);
129
Andreas Huber88572f72012-02-21 11:47:18 -0800130private:
Andreas Huberaba67132013-10-22 12:40:01 -0700131 enum {
Chong Zhang8d5e5562014-07-08 18:49:21 -0700132 kWhatCallbackNotify,
Lajos Molnard8578572015-06-05 20:17:33 -0700133 kWhatFrameRendered,
Andreas Huberaba67132013-10-22 12:40:01 -0700134 };
135
Andreas Huber88572f72012-02-21 11:47:18 -0800136 jclass mClass;
137 jweak mObject;
Mathias Agopian52800612013-02-14 17:11:20 -0800138 sp<Surface> mSurfaceTextureClient;
Andreas Huber88572f72012-02-21 11:47:18 -0800139
Lajos Molnar7de28d32014-07-25 07:51:02 -0700140 // java objects cached
141 jclass mByteBufferClass;
142 jobject mNativeByteOrderObj;
143 jmethodID mByteBufferOrderMethodID;
144 jmethodID mByteBufferPositionMethodID;
145 jmethodID mByteBufferLimitMethodID;
146 jmethodID mByteBufferAsReadOnlyBufferMethodID;
147
Andreas Huber88572f72012-02-21 11:47:18 -0800148 sp<ALooper> mLooper;
149 sp<MediaCodec> mCodec;
150
Chong Zhang8d5e5562014-07-08 18:49:21 -0700151 sp<AMessage> mCallbackNotification;
Lajos Molnard8578572015-06-05 20:17:33 -0700152 sp<AMessage> mOnFrameRenderedNotification;
Andreas Huberaba67132013-10-22 12:40:01 -0700153
Andy Hung5f9aa0b2014-07-30 15:48:21 -0700154 status_t mInitStatus;
155
Wonsik Kim4273dd02016-09-27 15:23:35 +0900156 template <typename T>
Lajos Molnar7de28d32014-07-25 07:51:02 -0700157 status_t createByteBufferFromABuffer(
Wonsik Kim4273dd02016-09-27 15:23:35 +0900158 JNIEnv *env, bool readOnly, bool clearBuffer, const sp<T> &buffer,
Lajos Molnar7de28d32014-07-25 07:51:02 -0700159 jobject *buf) const;
160
161 void cacheJavaObjects(JNIEnv *env);
162 void deleteJavaObjects(JNIEnv *env);
Lajos Molnard8578572015-06-05 20:17:33 -0700163 void handleCallback(const sp<AMessage> &msg);
164 void handleFrameRenderedNotification(const sp<AMessage> &msg);
Lajos Molnar7de28d32014-07-25 07:51:02 -0700165
Andreas Huber88572f72012-02-21 11:47:18 -0800166 DISALLOW_EVIL_CONSTRUCTORS(JMediaCodec);
167};
168
169} // namespace android
170
171#endif // _ANDROID_MEDIA_MEDIACODEC_H_