blob: c9b6b7f6f3372cddfdbfce52751813a71288febb [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
shubangd49681e2020-02-17 21:32:30 -080024#include <C2Buffer.h>
25#include <binder/MemoryHeapBase.h>
26#include <media/MediaCodecBuffer.h>
Ray Essick81fbc5b2019-12-07 06:24:59 -080027#include <media/MediaMetricsItem.h>
Andreas Huber9e6bcce2012-04-06 12:14:47 -070028#include <media/hardware/CryptoAPI.h>
Andreas Huber88572f72012-02-21 11:47:18 -080029#include <media/stagefright/foundation/ABase.h>
Andreas Huberaba67132013-10-22 12:40:01 -070030#include <media/stagefright/foundation/AHandler.h>
Andreas Huber88572f72012-02-21 11:47:18 -080031#include <utils/Errors.h>
Andreas Huber88572f72012-02-21 11:47:18 -080032
Wonsik Kimccb7ac62019-12-27 17:12:40 -080033class C2Buffer;
34
Andreas Huber88572f72012-02-21 11:47:18 -080035namespace android {
36
Lajos Molnar7de28d32014-07-25 07:51:02 -070037struct ABuffer;
Arun Johnson206270e2023-10-31 20:40:12 +000038struct AccessUnitInfo;
Andreas Huber88572f72012-02-21 11:47:18 -080039struct ALooper;
40struct AMessage;
Andreas Huberbfc56f42012-04-19 12:47:07 -070041struct AString;
Andreas Huber8240d922012-04-04 14:06:32 -070042struct ICrypto;
Colin Cross08f5f1f2016-09-30 17:36:46 -070043class IGraphicBufferProducer;
Andreas Huber88572f72012-02-21 11:47:18 -080044struct MediaCodec;
Chong Zhang8034d602015-04-28 13:38:48 -070045struct PersistentSurface;
Mathias Agopian52800612013-02-14 17:11:20 -080046class Surface;
Chong Zhang2659c2f2017-04-27 13:18:20 -070047namespace hardware {
Wonsik Kimccb7ac62019-12-27 17:12:40 -080048class HidlMemory;
Chong Zhang2659c2f2017-04-27 13:18:20 -070049namespace cas {
50namespace native {
51namespace V1_0 {
52struct IDescrambler;
53}}}}
54using hardware::cas::native::V1_0::IDescrambler;
Andreas Huber88572f72012-02-21 11:47:18 -080055
Andreas Huberaba67132013-10-22 12:40:01 -070056struct JMediaCodec : public AHandler {
Andreas Huber88572f72012-02-21 11:47:18 -080057 JMediaCodec(
58 JNIEnv *env, jobject thiz,
Brian Lindahl6ceeed42022-02-01 11:10:30 +010059 const char *name, bool nameIsType, bool encoder, int pid, int uid);
Andreas Huber88572f72012-02-21 11:47:18 -080060
61 status_t initCheck() const;
62
Andreas Huberaba67132013-10-22 12:40:01 -070063 void registerSelf();
Chong Zhang128b0122014-03-01 18:04:13 -080064 void release();
Wonsik Kim89666622020-04-28 10:43:47 -070065 void releaseAsync();
Andreas Huberaba67132013-10-22 12:40:01 -070066
Guillaume Chelfic072caf2021-02-03 16:18:26 +010067 status_t enableOnFirstTunnelFrameReadyListener(jboolean enable);
68
Lajos Molnard8578572015-06-05 20:17:33 -070069 status_t enableOnFrameRenderedListener(jboolean enable);
70
Chong Zhang8d5e5562014-07-08 18:49:21 -070071 status_t setCallback(jobject cb);
72
Andreas Huber88572f72012-02-21 11:47:18 -080073 status_t configure(
74 const sp<AMessage> &format,
Andy McFaddend47f7d82012-12-18 09:48:38 -080075 const sp<IGraphicBufferProducer> &bufferProducer,
Andreas Huber8240d922012-04-04 14:06:32 -070076 const sp<ICrypto> &crypto,
Chong Zhangd5927ae2017-01-03 11:07:18 -080077 const sp<IDescrambler> &descrambler,
Andreas Huber88572f72012-02-21 11:47:18 -080078 int flags);
79
Lajos Molnar5e02ba92015-05-01 15:59:35 -070080 status_t setSurface(
81 const sp<IGraphicBufferProducer> &surface);
82
Lajos Molnar3a7c83a2024-03-29 08:15:59 -070083 status_t detachOutputSurface();
84
Andy McFadden2621e402013-02-19 07:29:21 -080085 status_t createInputSurface(sp<IGraphicBufferProducer>* bufferProducer);
Chong Zhang9560ddb2015-05-13 10:25:29 -070086 status_t setInputSurface(const sp<PersistentSurface> &surface);
Andy McFadden2621e402013-02-19 07:29:21 -080087
Andreas Huber88572f72012-02-21 11:47:18 -080088 status_t start();
89 status_t stop();
Lajos Molnar1e6e8012014-07-15 16:07:13 -070090 status_t reset();
Andreas Huber88572f72012-02-21 11:47:18 -080091
92 status_t flush();
93
94 status_t queueInputBuffer(
95 size_t index,
Andreas Huberbfc56f42012-04-19 12:47:07 -070096 size_t offset, size_t size, int64_t timeUs, uint32_t flags,
97 AString *errorDetailMsg);
Andreas Huber88572f72012-02-21 11:47:18 -080098
Arun Johnson206270e2023-10-31 20:40:12 +000099 status_t queueInputBuffers(
100 size_t index,
101 size_t offset,
102 size_t size,
103 const sp<RefBase> &auInfo,
104 AString *errorDetailMsg = NULL);
105
Andreas Huber9e6bcce2012-04-06 12:14:47 -0700106 status_t queueSecureInputBuffer(
107 size_t index,
108 size_t offset,
109 const CryptoPlugin::SubSample *subSamples,
110 size_t numSubSamples,
111 const uint8_t key[16],
112 const uint8_t iv[16],
113 CryptoPlugin::Mode mode,
Jeff Tinkerd4ea5d32015-12-18 11:56:22 -0800114 const CryptoPlugin::Pattern &pattern,
Andreas Huber9e6bcce2012-04-06 12:14:47 -0700115 int64_t presentationTimeUs,
Andreas Huberbfc56f42012-04-19 12:47:07 -0700116 uint32_t flags,
117 AString *errorDetailMsg);
Andreas Huber9e6bcce2012-04-06 12:14:47 -0700118
Arun Johnson4ca49092024-02-01 19:07:15 +0000119 status_t queueSecureInputBuffers(
120 size_t index,
121 size_t offset,
122 size_t size,
123 const sp<RefBase> &auInfos,
124 const sp<RefBase> &cryptoInfos,
125 AString *errorDetailMsg);
126
Wonsik Kimccb7ac62019-12-27 17:12:40 -0800127 status_t queueBuffer(
128 size_t index, const std::shared_ptr<C2Buffer> &buffer,
Arun Johnson8d6a41a2024-01-10 21:14:14 +0000129 const sp<RefBase> &infos, const sp<AMessage> &tunings,
Wonsik Kimccb7ac62019-12-27 17:12:40 -0800130 AString *errorDetailMsg);
131
132 status_t queueEncryptedLinearBlock(
133 size_t index,
134 const sp<hardware::HidlMemory> &buffer,
135 size_t offset,
Arun Johnson677a8812024-02-06 12:32:54 +0000136 size_t size,
Arun Johnson8d6a41a2024-01-10 21:14:14 +0000137 const sp<RefBase> &infos,
Arun Johnson677a8812024-02-06 12:32:54 +0000138 const sp<RefBase> &cryptoInfos,
Wonsik Kimccb7ac62019-12-27 17:12:40 -0800139 const sp<AMessage> &tunings,
140 AString *errorDetailMsg);
141
Andreas Huber88572f72012-02-21 11:47:18 -0800142 status_t dequeueInputBuffer(size_t *index, int64_t timeoutUs);
143
144 status_t dequeueOutputBuffer(
145 JNIEnv *env, jobject bufferInfo, size_t *index, int64_t timeoutUs);
146
Lajos Molnar7c513b6b2014-05-08 17:16:45 -0700147 status_t releaseOutputBuffer(
148 size_t index, bool render, bool updatePTS, int64_t timestampNs);
Andreas Huber88572f72012-02-21 11:47:18 -0800149
Andy McFadden2621e402013-02-19 07:29:21 -0800150 status_t signalEndOfInputStream();
151
Lajos Molnard4023112014-07-11 15:12:59 -0700152 status_t getFormat(JNIEnv *env, bool input, jobject *format) const;
153
154 status_t getOutputFormat(JNIEnv *env, size_t index, jobject *format) const;
Andreas Huber88572f72012-02-21 11:47:18 -0800155
156 status_t getBuffers(
157 JNIEnv *env, bool input, jobjectArray *bufArray) const;
158
Lajos Molnard4023112014-07-11 15:12:59 -0700159 status_t getBuffer(
160 JNIEnv *env, bool input, size_t index, jobject *buf) const;
161
162 status_t getImage(
163 JNIEnv *env, bool input, size_t index, jobject *image) const;
164
Wonsik Kimccb7ac62019-12-27 17:12:40 -0800165 status_t getOutputFrame(
166 JNIEnv *env, jobject frame, size_t index) const;
167
Martin Storsjo056ef2e2012-09-25 11:53:04 +0300168 status_t getName(JNIEnv *env, jstring *name) const;
169
Chong Zhang90d73042018-02-28 18:46:26 -0800170 status_t getCodecInfo(JNIEnv *env, jobject *codecInfo) const;
171
Ray Essick81fbc5b2019-12-07 06:24:59 -0800172 status_t getMetrics(JNIEnv *env, mediametrics::Item * &reply) const;
Ray Essick0e0fee12017-01-25 18:01:56 -0800173
Andreas Huber226065b2013-08-12 10:14:11 -0700174 status_t setParameters(const sp<AMessage> &params);
175
Andreas Huberb12a5392012-04-30 14:18:33 -0700176 void setVideoScalingMode(int mode);
177
ybai5e053202018-11-01 13:02:15 +0800178 void selectAudioPresentation(const int32_t presentationId, const int32_t programId);
179
Wonsik Kim8798c8c2021-03-18 21:38:57 -0700180 status_t querySupportedVendorParameters(JNIEnv *env, jobject *names);
181
182 status_t describeParameter(JNIEnv *env, jstring name, jobject *desc);
183
184 status_t subscribeToVendorParameters(JNIEnv *env, jobject names);
185
186 status_t unsubscribeFromVendorParameters(JNIEnv *env, jobject names);
187
Wonsik Kimf7069ce2020-05-13 17:15:47 -0700188 bool hasCryptoOrDescrambler() { return mHasCryptoOrDescrambler; }
189
Robert Shih631a80d2021-02-14 02:23:55 -0800190 const sp<ICrypto> &getCrypto() { return mCrypto; }
191
Wonsik Kimd1a1b4e2023-01-30 10:26:08 -0800192 std::string getExceptionMessage(const char *msg) const;
193
Andreas Huber88572f72012-02-21 11:47:18 -0800194protected:
195 virtual ~JMediaCodec();
196
Andreas Huberaba67132013-10-22 12:40:01 -0700197 virtual void onMessageReceived(const sp<AMessage> &msg);
198
Andreas Huber88572f72012-02-21 11:47:18 -0800199private:
Andreas Huberaba67132013-10-22 12:40:01 -0700200 enum {
Chong Zhang8d5e5562014-07-08 18:49:21 -0700201 kWhatCallbackNotify,
Lajos Molnard8578572015-06-05 20:17:33 -0700202 kWhatFrameRendered,
Wonsik Kimd4ce4e32020-06-08 10:59:48 -0700203 kWhatAsyncReleaseComplete,
Guillaume Chelfic072caf2021-02-03 16:18:26 +0100204 kWhatFirstTunnelFrameReady,
Andreas Huberaba67132013-10-22 12:40:01 -0700205 };
206
Andreas Huber88572f72012-02-21 11:47:18 -0800207 jclass mClass;
208 jweak mObject;
Mathias Agopian52800612013-02-14 17:11:20 -0800209 sp<Surface> mSurfaceTextureClient;
Andreas Huber88572f72012-02-21 11:47:18 -0800210
211 sp<ALooper> mLooper;
212 sp<MediaCodec> mCodec;
Lajos Molnare7473872019-02-05 18:54:27 -0800213 AString mNameAtCreation;
Wonsik Kimccb7ac62019-12-27 17:12:40 -0800214 bool mGraphicOutput{false};
Wonsik Kimf7069ce2020-05-13 17:15:47 -0700215 bool mHasCryptoOrDescrambler{false};
Wonsik Kimeaed9f52019-06-18 17:25:02 -0700216 std::once_flag mReleaseFlag;
Wonsik Kimd4ce4e32020-06-08 10:59:48 -0700217 std::once_flag mAsyncReleaseFlag;
Andreas Huber88572f72012-02-21 11:47:18 -0800218
Chong Zhang8d5e5562014-07-08 18:49:21 -0700219 sp<AMessage> mCallbackNotification;
Guillaume Chelfic072caf2021-02-03 16:18:26 +0100220 sp<AMessage> mOnFirstTunnelFrameReadyNotification;
Lajos Molnard8578572015-06-05 20:17:33 -0700221 sp<AMessage> mOnFrameRenderedNotification;
Andreas Huberaba67132013-10-22 12:40:01 -0700222
Andy Hung5f9aa0b2014-07-30 15:48:21 -0700223 status_t mInitStatus;
224
Robert Shih631a80d2021-02-14 02:23:55 -0800225 sp<ICrypto> mCrypto;
226
Wonsik Kim4273dd02016-09-27 15:23:35 +0900227 template <typename T>
Lajos Molnar7de28d32014-07-25 07:51:02 -0700228 status_t createByteBufferFromABuffer(
Wonsik Kim4273dd02016-09-27 15:23:35 +0900229 JNIEnv *env, bool readOnly, bool clearBuffer, const sp<T> &buffer,
Lajos Molnar7de28d32014-07-25 07:51:02 -0700230 jobject *buf) const;
231
Lajos Molnard8578572015-06-05 20:17:33 -0700232 void handleCallback(const sp<AMessage> &msg);
Guillaume Chelfic072caf2021-02-03 16:18:26 +0100233 void handleFirstTunnelFrameReadyNotification(const sp<AMessage> &msg);
Lajos Molnard8578572015-06-05 20:17:33 -0700234 void handleFrameRenderedNotification(const sp<AMessage> &msg);
Lajos Molnar7de28d32014-07-25 07:51:02 -0700235
Andreas Huber88572f72012-02-21 11:47:18 -0800236 DISALLOW_EVIL_CONSTRUCTORS(JMediaCodec);
237};
238
239} // namespace android
240
241#endif // _ANDROID_MEDIA_MEDIACODEC_H_