blob: dfe30a3f5909d938efb21ca5016046a64e730baf [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
Wonsik Kimeaed9f52019-06-18 17:25:02 -070020#include <mutex>
21
Andreas Huber88572f72012-02-21 11:47:18 -080022#include "jni.h"
23
Ray Essickf2d0e402017-03-09 10:17:51 -080024#include <media/MediaAnalyticsItem.h>
Andreas Huber9e6bcce2012-04-06 12:14:47 -070025#include <media/hardware/CryptoAPI.h>
Andreas Huber88572f72012-02-21 11:47:18 -080026#include <media/stagefright/foundation/ABase.h>
Andreas Huberaba67132013-10-22 12:40:01 -070027#include <media/stagefright/foundation/AHandler.h>
Andreas Huber88572f72012-02-21 11:47:18 -080028#include <utils/Errors.h>
Andreas Huber88572f72012-02-21 11:47:18 -080029
30namespace android {
31
Lajos Molnar7de28d32014-07-25 07:51:02 -070032struct ABuffer;
Andreas Huber88572f72012-02-21 11:47:18 -080033struct ALooper;
34struct AMessage;
Andreas Huberbfc56f42012-04-19 12:47:07 -070035struct AString;
Andreas Huber8240d922012-04-04 14:06:32 -070036struct ICrypto;
Colin Cross08f5f1f2016-09-30 17:36:46 -070037class IGraphicBufferProducer;
Andreas Huber88572f72012-02-21 11:47:18 -080038struct MediaCodec;
Chong Zhang8034d602015-04-28 13:38:48 -070039struct PersistentSurface;
Mathias Agopian52800612013-02-14 17:11:20 -080040class Surface;
Chong Zhang2659c2f2017-04-27 13:18:20 -070041namespace hardware {
42namespace cas {
43namespace native {
44namespace V1_0 {
45struct IDescrambler;
46}}}}
47using hardware::cas::native::V1_0::IDescrambler;
Andreas Huber88572f72012-02-21 11:47:18 -080048
Andreas Huberaba67132013-10-22 12:40:01 -070049struct JMediaCodec : public AHandler {
Andreas Huber88572f72012-02-21 11:47:18 -080050 JMediaCodec(
51 JNIEnv *env, jobject thiz,
52 const char *name, bool nameIsType, bool encoder);
53
54 status_t initCheck() const;
55
Andreas Huberaba67132013-10-22 12:40:01 -070056 void registerSelf();
Chong Zhang128b0122014-03-01 18:04:13 -080057 void release();
Andreas Huberaba67132013-10-22 12:40:01 -070058
Lajos Molnard8578572015-06-05 20:17:33 -070059 status_t enableOnFrameRenderedListener(jboolean enable);
60
Chong Zhang8d5e5562014-07-08 18:49:21 -070061 status_t setCallback(jobject cb);
62
Andreas Huber88572f72012-02-21 11:47:18 -080063 status_t configure(
64 const sp<AMessage> &format,
Andy McFaddend47f7d82012-12-18 09:48:38 -080065 const sp<IGraphicBufferProducer> &bufferProducer,
Andreas Huber8240d922012-04-04 14:06:32 -070066 const sp<ICrypto> &crypto,
Chong Zhangd5927ae2017-01-03 11:07:18 -080067 const sp<IDescrambler> &descrambler,
Andreas Huber88572f72012-02-21 11:47:18 -080068 int flags);
69
Lajos Molnar5e02ba92015-05-01 15:59:35 -070070 status_t setSurface(
71 const sp<IGraphicBufferProducer> &surface);
72
Andy McFadden2621e402013-02-19 07:29:21 -080073 status_t createInputSurface(sp<IGraphicBufferProducer>* bufferProducer);
Chong Zhang9560ddb2015-05-13 10:25:29 -070074 status_t setInputSurface(const sp<PersistentSurface> &surface);
Andy McFadden2621e402013-02-19 07:29:21 -080075
Andreas Huber88572f72012-02-21 11:47:18 -080076 status_t start();
77 status_t stop();
Lajos Molnar1e6e8012014-07-15 16:07:13 -070078 status_t reset();
Andreas Huber88572f72012-02-21 11:47:18 -080079
80 status_t flush();
81
82 status_t queueInputBuffer(
83 size_t index,
Andreas Huberbfc56f42012-04-19 12:47:07 -070084 size_t offset, size_t size, int64_t timeUs, uint32_t flags,
85 AString *errorDetailMsg);
Andreas Huber88572f72012-02-21 11:47:18 -080086
Andreas Huber9e6bcce2012-04-06 12:14:47 -070087 status_t queueSecureInputBuffer(
88 size_t index,
89 size_t offset,
90 const CryptoPlugin::SubSample *subSamples,
91 size_t numSubSamples,
92 const uint8_t key[16],
93 const uint8_t iv[16],
94 CryptoPlugin::Mode mode,
Jeff Tinkerd4ea5d32015-12-18 11:56:22 -080095 const CryptoPlugin::Pattern &pattern,
Andreas Huber9e6bcce2012-04-06 12:14:47 -070096 int64_t presentationTimeUs,
Andreas Huberbfc56f42012-04-19 12:47:07 -070097 uint32_t flags,
98 AString *errorDetailMsg);
Andreas Huber9e6bcce2012-04-06 12:14:47 -070099
Andreas Huber88572f72012-02-21 11:47:18 -0800100 status_t dequeueInputBuffer(size_t *index, int64_t timeoutUs);
101
102 status_t dequeueOutputBuffer(
103 JNIEnv *env, jobject bufferInfo, size_t *index, int64_t timeoutUs);
104
Lajos Molnar7c513b6b2014-05-08 17:16:45 -0700105 status_t releaseOutputBuffer(
106 size_t index, bool render, bool updatePTS, int64_t timestampNs);
Andreas Huber88572f72012-02-21 11:47:18 -0800107
Andy McFadden2621e402013-02-19 07:29:21 -0800108 status_t signalEndOfInputStream();
109
Lajos Molnard4023112014-07-11 15:12:59 -0700110 status_t getFormat(JNIEnv *env, bool input, jobject *format) const;
111
112 status_t getOutputFormat(JNIEnv *env, size_t index, jobject *format) const;
Andreas Huber88572f72012-02-21 11:47:18 -0800113
114 status_t getBuffers(
115 JNIEnv *env, bool input, jobjectArray *bufArray) const;
116
Lajos Molnard4023112014-07-11 15:12:59 -0700117 status_t getBuffer(
118 JNIEnv *env, bool input, size_t index, jobject *buf) const;
119
120 status_t getImage(
121 JNIEnv *env, bool input, size_t index, jobject *image) const;
122
Martin Storsjo056ef2e2012-09-25 11:53:04 +0300123 status_t getName(JNIEnv *env, jstring *name) const;
124
Chong Zhang90d73042018-02-28 18:46:26 -0800125 status_t getCodecInfo(JNIEnv *env, jobject *codecInfo) const;
126
Ray Essickf2d0e402017-03-09 10:17:51 -0800127 status_t getMetrics(JNIEnv *env, MediaAnalyticsItem * &reply) const;
Ray Essick0e0fee12017-01-25 18:01:56 -0800128
Andreas Huber226065b2013-08-12 10:14:11 -0700129 status_t setParameters(const sp<AMessage> &params);
130
Andreas Huberb12a5392012-04-30 14:18:33 -0700131 void setVideoScalingMode(int mode);
132
ybai5e053202018-11-01 13:02:15 +0800133 void selectAudioPresentation(const int32_t presentationId, const int32_t programId);
134
Andreas Huber88572f72012-02-21 11:47:18 -0800135protected:
136 virtual ~JMediaCodec();
137
Andreas Huberaba67132013-10-22 12:40:01 -0700138 virtual void onMessageReceived(const sp<AMessage> &msg);
139
Andreas Huber88572f72012-02-21 11:47:18 -0800140private:
Andreas Huberaba67132013-10-22 12:40:01 -0700141 enum {
Chong Zhang8d5e5562014-07-08 18:49:21 -0700142 kWhatCallbackNotify,
Lajos Molnard8578572015-06-05 20:17:33 -0700143 kWhatFrameRendered,
Andreas Huberaba67132013-10-22 12:40:01 -0700144 };
145
Andreas Huber88572f72012-02-21 11:47:18 -0800146 jclass mClass;
147 jweak mObject;
Mathias Agopian52800612013-02-14 17:11:20 -0800148 sp<Surface> mSurfaceTextureClient;
Andreas Huber88572f72012-02-21 11:47:18 -0800149
Lajos Molnar7de28d32014-07-25 07:51:02 -0700150 // java objects cached
151 jclass mByteBufferClass;
152 jobject mNativeByteOrderObj;
153 jmethodID mByteBufferOrderMethodID;
154 jmethodID mByteBufferPositionMethodID;
155 jmethodID mByteBufferLimitMethodID;
156 jmethodID mByteBufferAsReadOnlyBufferMethodID;
157
Andreas Huber88572f72012-02-21 11:47:18 -0800158 sp<ALooper> mLooper;
159 sp<MediaCodec> mCodec;
Lajos Molnare7473872019-02-05 18:54:27 -0800160 AString mNameAtCreation;
Wonsik Kimeaed9f52019-06-18 17:25:02 -0700161 std::once_flag mReleaseFlag;
Andreas Huber88572f72012-02-21 11:47:18 -0800162
Chong Zhang8d5e5562014-07-08 18:49:21 -0700163 sp<AMessage> mCallbackNotification;
Lajos Molnard8578572015-06-05 20:17:33 -0700164 sp<AMessage> mOnFrameRenderedNotification;
Andreas Huberaba67132013-10-22 12:40:01 -0700165
Andy Hung5f9aa0b2014-07-30 15:48:21 -0700166 status_t mInitStatus;
167
Wonsik Kim4273dd02016-09-27 15:23:35 +0900168 template <typename T>
Lajos Molnar7de28d32014-07-25 07:51:02 -0700169 status_t createByteBufferFromABuffer(
Wonsik Kim4273dd02016-09-27 15:23:35 +0900170 JNIEnv *env, bool readOnly, bool clearBuffer, const sp<T> &buffer,
Lajos Molnar7de28d32014-07-25 07:51:02 -0700171 jobject *buf) const;
172
173 void cacheJavaObjects(JNIEnv *env);
174 void deleteJavaObjects(JNIEnv *env);
Lajos Molnard8578572015-06-05 20:17:33 -0700175 void handleCallback(const sp<AMessage> &msg);
176 void handleFrameRenderedNotification(const sp<AMessage> &msg);
Lajos Molnar7de28d32014-07-25 07:51:02 -0700177
Andreas Huber88572f72012-02-21 11:47:18 -0800178 DISALLOW_EVIL_CONSTRUCTORS(JMediaCodec);
179};
180
181} // namespace android
182
183#endif // _ANDROID_MEDIA_MEDIACODEC_H_