blob: de08550fa3db2169325c7da9132bc8d854649f5a [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
Ray Essickf2d0e402017-03-09 10:17:51 -080022#include <media/MediaAnalyticsItem.h>
Andreas Huber9e6bcce2012-04-06 12:14:47 -070023#include <media/hardware/CryptoAPI.h>
Andreas Huber88572f72012-02-21 11:47:18 -080024#include <media/stagefright/foundation/ABase.h>
Andreas Huberaba67132013-10-22 12:40:01 -070025#include <media/stagefright/foundation/AHandler.h>
Andreas Huber88572f72012-02-21 11:47:18 -080026#include <utils/Errors.h>
Andreas Huber88572f72012-02-21 11:47:18 -080027
28namespace android {
29
Lajos Molnar7de28d32014-07-25 07:51:02 -070030struct ABuffer;
Andreas Huber88572f72012-02-21 11:47:18 -080031struct ALooper;
32struct AMessage;
Andreas Huberbfc56f42012-04-19 12:47:07 -070033struct AString;
Andreas Huber8240d922012-04-04 14:06:32 -070034struct ICrypto;
Colin Cross08f5f1f2016-09-30 17:36:46 -070035class IGraphicBufferProducer;
Andreas Huber88572f72012-02-21 11:47:18 -080036struct MediaCodec;
Chong Zhang8034d602015-04-28 13:38:48 -070037struct PersistentSurface;
Mathias Agopian52800612013-02-14 17:11:20 -080038class Surface;
Chong Zhang2659c2f2017-04-27 13:18:20 -070039namespace hardware {
40namespace cas {
41namespace native {
42namespace V1_0 {
43struct IDescrambler;
44}}}}
45using hardware::cas::native::V1_0::IDescrambler;
Andreas Huber88572f72012-02-21 11:47:18 -080046
Andreas Huberaba67132013-10-22 12:40:01 -070047struct JMediaCodec : public AHandler {
Andreas Huber88572f72012-02-21 11:47:18 -080048 JMediaCodec(
49 JNIEnv *env, jobject thiz,
50 const char *name, bool nameIsType, bool encoder);
51
52 status_t initCheck() const;
53
Andreas Huberaba67132013-10-22 12:40:01 -070054 void registerSelf();
Chong Zhang128b0122014-03-01 18:04:13 -080055 void release();
Andreas Huberaba67132013-10-22 12:40:01 -070056
Lajos Molnard8578572015-06-05 20:17:33 -070057 status_t enableOnFrameRenderedListener(jboolean enable);
58
Chong Zhang8d5e5562014-07-08 18:49:21 -070059 status_t setCallback(jobject cb);
60
Andreas Huber88572f72012-02-21 11:47:18 -080061 status_t configure(
62 const sp<AMessage> &format,
Andy McFaddend47f7d82012-12-18 09:48:38 -080063 const sp<IGraphicBufferProducer> &bufferProducer,
Andreas Huber8240d922012-04-04 14:06:32 -070064 const sp<ICrypto> &crypto,
Chong Zhangd5927ae2017-01-03 11:07:18 -080065 const sp<IDescrambler> &descrambler,
Andreas Huber88572f72012-02-21 11:47:18 -080066 int flags);
67
Lajos Molnar5e02ba92015-05-01 15:59:35 -070068 status_t setSurface(
69 const sp<IGraphicBufferProducer> &surface);
70
Andy McFadden2621e402013-02-19 07:29:21 -080071 status_t createInputSurface(sp<IGraphicBufferProducer>* bufferProducer);
Chong Zhang9560ddb2015-05-13 10:25:29 -070072 status_t setInputSurface(const sp<PersistentSurface> &surface);
Andy McFadden2621e402013-02-19 07:29:21 -080073
Andreas Huber88572f72012-02-21 11:47:18 -080074 status_t start();
75 status_t stop();
Lajos Molnar1e6e8012014-07-15 16:07:13 -070076 status_t reset();
Andreas Huber88572f72012-02-21 11:47:18 -080077
78 status_t flush();
79
80 status_t queueInputBuffer(
81 size_t index,
Andreas Huberbfc56f42012-04-19 12:47:07 -070082 size_t offset, size_t size, int64_t timeUs, uint32_t flags,
83 AString *errorDetailMsg);
Andreas Huber88572f72012-02-21 11:47:18 -080084
Andreas Huber9e6bcce2012-04-06 12:14:47 -070085 status_t queueSecureInputBuffer(
86 size_t index,
87 size_t offset,
88 const CryptoPlugin::SubSample *subSamples,
89 size_t numSubSamples,
90 const uint8_t key[16],
91 const uint8_t iv[16],
92 CryptoPlugin::Mode mode,
Jeff Tinkerd4ea5d32015-12-18 11:56:22 -080093 const CryptoPlugin::Pattern &pattern,
Andreas Huber9e6bcce2012-04-06 12:14:47 -070094 int64_t presentationTimeUs,
Andreas Huberbfc56f42012-04-19 12:47:07 -070095 uint32_t flags,
96 AString *errorDetailMsg);
Andreas Huber9e6bcce2012-04-06 12:14:47 -070097
Andreas Huber88572f72012-02-21 11:47:18 -080098 status_t dequeueInputBuffer(size_t *index, int64_t timeoutUs);
99
100 status_t dequeueOutputBuffer(
101 JNIEnv *env, jobject bufferInfo, size_t *index, int64_t timeoutUs);
102
Lajos Molnar7c513b6b2014-05-08 17:16:45 -0700103 status_t releaseOutputBuffer(
104 size_t index, bool render, bool updatePTS, int64_t timestampNs);
Andreas Huber88572f72012-02-21 11:47:18 -0800105
Andy McFadden2621e402013-02-19 07:29:21 -0800106 status_t signalEndOfInputStream();
107
Lajos Molnard4023112014-07-11 15:12:59 -0700108 status_t getFormat(JNIEnv *env, bool input, jobject *format) const;
109
110 status_t getOutputFormat(JNIEnv *env, size_t index, jobject *format) const;
Andreas Huber88572f72012-02-21 11:47:18 -0800111
112 status_t getBuffers(
113 JNIEnv *env, bool input, jobjectArray *bufArray) const;
114
Lajos Molnard4023112014-07-11 15:12:59 -0700115 status_t getBuffer(
116 JNIEnv *env, bool input, size_t index, jobject *buf) const;
117
118 status_t getImage(
119 JNIEnv *env, bool input, size_t index, jobject *image) const;
120
Martin Storsjo056ef2e2012-09-25 11:53:04 +0300121 status_t getName(JNIEnv *env, jstring *name) const;
122
Chong Zhang90d73042018-02-28 18:46:26 -0800123 status_t getCodecInfo(JNIEnv *env, jobject *codecInfo) const;
124
Ray Essickf2d0e402017-03-09 10:17:51 -0800125 status_t getMetrics(JNIEnv *env, MediaAnalyticsItem * &reply) const;
Ray Essick0e0fee12017-01-25 18:01:56 -0800126
Andreas Huber226065b2013-08-12 10:14:11 -0700127 status_t setParameters(const sp<AMessage> &params);
128
Andreas Huberb12a5392012-04-30 14:18:33 -0700129 void setVideoScalingMode(int mode);
130
ybai5e053202018-11-01 13:02:15 +0800131 void selectAudioPresentation(const int32_t presentationId, const int32_t programId);
132
Andreas Huber88572f72012-02-21 11:47:18 -0800133protected:
134 virtual ~JMediaCodec();
135
Andreas Huberaba67132013-10-22 12:40:01 -0700136 virtual void onMessageReceived(const sp<AMessage> &msg);
137
Andreas Huber88572f72012-02-21 11:47:18 -0800138private:
Andreas Huberaba67132013-10-22 12:40:01 -0700139 enum {
Chong Zhang8d5e5562014-07-08 18:49:21 -0700140 kWhatCallbackNotify,
Lajos Molnard8578572015-06-05 20:17:33 -0700141 kWhatFrameRendered,
Andreas Huberaba67132013-10-22 12:40:01 -0700142 };
143
Andreas Huber88572f72012-02-21 11:47:18 -0800144 jclass mClass;
145 jweak mObject;
Mathias Agopian52800612013-02-14 17:11:20 -0800146 sp<Surface> mSurfaceTextureClient;
Andreas Huber88572f72012-02-21 11:47:18 -0800147
Lajos Molnar7de28d32014-07-25 07:51:02 -0700148 // java objects cached
149 jclass mByteBufferClass;
150 jobject mNativeByteOrderObj;
151 jmethodID mByteBufferOrderMethodID;
152 jmethodID mByteBufferPositionMethodID;
153 jmethodID mByteBufferLimitMethodID;
154 jmethodID mByteBufferAsReadOnlyBufferMethodID;
155
Andreas Huber88572f72012-02-21 11:47:18 -0800156 sp<ALooper> mLooper;
157 sp<MediaCodec> mCodec;
Lajos Molnare7473872019-02-05 18:54:27 -0800158 AString mNameAtCreation;
Andreas Huber88572f72012-02-21 11:47:18 -0800159
Chong Zhang8d5e5562014-07-08 18:49:21 -0700160 sp<AMessage> mCallbackNotification;
Lajos Molnard8578572015-06-05 20:17:33 -0700161 sp<AMessage> mOnFrameRenderedNotification;
Andreas Huberaba67132013-10-22 12:40:01 -0700162
Andy Hung5f9aa0b2014-07-30 15:48:21 -0700163 status_t mInitStatus;
164
Wonsik Kim4273dd02016-09-27 15:23:35 +0900165 template <typename T>
Lajos Molnar7de28d32014-07-25 07:51:02 -0700166 status_t createByteBufferFromABuffer(
Wonsik Kim4273dd02016-09-27 15:23:35 +0900167 JNIEnv *env, bool readOnly, bool clearBuffer, const sp<T> &buffer,
Lajos Molnar7de28d32014-07-25 07:51:02 -0700168 jobject *buf) const;
169
170 void cacheJavaObjects(JNIEnv *env);
171 void deleteJavaObjects(JNIEnv *env);
Lajos Molnard8578572015-06-05 20:17:33 -0700172 void handleCallback(const sp<AMessage> &msg);
173 void handleFrameRenderedNotification(const sp<AMessage> &msg);
Lajos Molnar7de28d32014-07-25 07:51:02 -0700174
Andreas Huber88572f72012-02-21 11:47:18 -0800175 DISALLOW_EVIL_CONSTRUCTORS(JMediaCodec);
176};
177
178} // namespace android
179
180#endif // _ANDROID_MEDIA_MEDIACODEC_H_