blob: 0663e4126b8a71f4254f2fad961833e6be97e506 [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
Andy McFadden2621e402013-02-19 07:29:21 -080083 status_t createInputSurface(sp<IGraphicBufferProducer>* bufferProducer);
Chong Zhang9560ddb2015-05-13 10:25:29 -070084 status_t setInputSurface(const sp<PersistentSurface> &surface);
Andy McFadden2621e402013-02-19 07:29:21 -080085
Andreas Huber88572f72012-02-21 11:47:18 -080086 status_t start();
87 status_t stop();
Lajos Molnar1e6e8012014-07-15 16:07:13 -070088 status_t reset();
Andreas Huber88572f72012-02-21 11:47:18 -080089
90 status_t flush();
91
92 status_t queueInputBuffer(
93 size_t index,
Andreas Huberbfc56f42012-04-19 12:47:07 -070094 size_t offset, size_t size, int64_t timeUs, uint32_t flags,
95 AString *errorDetailMsg);
Andreas Huber88572f72012-02-21 11:47:18 -080096
Arun Johnson206270e2023-10-31 20:40:12 +000097 status_t queueInputBuffers(
98 size_t index,
99 size_t offset,
100 size_t size,
101 const sp<RefBase> &auInfo,
102 AString *errorDetailMsg = NULL);
103
Andreas Huber9e6bcce2012-04-06 12:14:47 -0700104 status_t queueSecureInputBuffer(
105 size_t index,
106 size_t offset,
107 const CryptoPlugin::SubSample *subSamples,
108 size_t numSubSamples,
109 const uint8_t key[16],
110 const uint8_t iv[16],
111 CryptoPlugin::Mode mode,
Jeff Tinkerd4ea5d32015-12-18 11:56:22 -0800112 const CryptoPlugin::Pattern &pattern,
Andreas Huber9e6bcce2012-04-06 12:14:47 -0700113 int64_t presentationTimeUs,
Andreas Huberbfc56f42012-04-19 12:47:07 -0700114 uint32_t flags,
115 AString *errorDetailMsg);
Andreas Huber9e6bcce2012-04-06 12:14:47 -0700116
Arun Johnson4ca49092024-02-01 19:07:15 +0000117 status_t queueSecureInputBuffers(
118 size_t index,
119 size_t offset,
120 size_t size,
121 const sp<RefBase> &auInfos,
122 const sp<RefBase> &cryptoInfos,
123 AString *errorDetailMsg);
124
Wonsik Kimccb7ac62019-12-27 17:12:40 -0800125 status_t queueBuffer(
126 size_t index, const std::shared_ptr<C2Buffer> &buffer,
Arun Johnson8d6a41a2024-01-10 21:14:14 +0000127 const sp<RefBase> &infos, const sp<AMessage> &tunings,
Wonsik Kimccb7ac62019-12-27 17:12:40 -0800128 AString *errorDetailMsg);
129
130 status_t queueEncryptedLinearBlock(
131 size_t index,
132 const sp<hardware::HidlMemory> &buffer,
133 size_t offset,
134 const CryptoPlugin::SubSample *subSamples,
135 size_t numSubSamples,
136 const uint8_t key[16],
137 const uint8_t iv[16],
138 CryptoPlugin::Mode mode,
139 const CryptoPlugin::Pattern &pattern,
Arun Johnson8d6a41a2024-01-10 21:14:14 +0000140 const sp<RefBase> &infos,
Wonsik Kimccb7ac62019-12-27 17:12:40 -0800141 const sp<AMessage> &tunings,
142 AString *errorDetailMsg);
143
Andreas Huber88572f72012-02-21 11:47:18 -0800144 status_t dequeueInputBuffer(size_t *index, int64_t timeoutUs);
145
146 status_t dequeueOutputBuffer(
147 JNIEnv *env, jobject bufferInfo, size_t *index, int64_t timeoutUs);
148
Lajos Molnar7c513b6b2014-05-08 17:16:45 -0700149 status_t releaseOutputBuffer(
150 size_t index, bool render, bool updatePTS, int64_t timestampNs);
Andreas Huber88572f72012-02-21 11:47:18 -0800151
Andy McFadden2621e402013-02-19 07:29:21 -0800152 status_t signalEndOfInputStream();
153
Lajos Molnard4023112014-07-11 15:12:59 -0700154 status_t getFormat(JNIEnv *env, bool input, jobject *format) const;
155
156 status_t getOutputFormat(JNIEnv *env, size_t index, jobject *format) const;
Andreas Huber88572f72012-02-21 11:47:18 -0800157
158 status_t getBuffers(
159 JNIEnv *env, bool input, jobjectArray *bufArray) const;
160
Lajos Molnard4023112014-07-11 15:12:59 -0700161 status_t getBuffer(
162 JNIEnv *env, bool input, size_t index, jobject *buf) const;
163
164 status_t getImage(
165 JNIEnv *env, bool input, size_t index, jobject *image) const;
166
Wonsik Kimccb7ac62019-12-27 17:12:40 -0800167 status_t getOutputFrame(
168 JNIEnv *env, jobject frame, size_t index) const;
169
Martin Storsjo056ef2e2012-09-25 11:53:04 +0300170 status_t getName(JNIEnv *env, jstring *name) const;
171
Chong Zhang90d73042018-02-28 18:46:26 -0800172 status_t getCodecInfo(JNIEnv *env, jobject *codecInfo) const;
173
Ray Essick81fbc5b2019-12-07 06:24:59 -0800174 status_t getMetrics(JNIEnv *env, mediametrics::Item * &reply) const;
Ray Essick0e0fee12017-01-25 18:01:56 -0800175
Andreas Huber226065b2013-08-12 10:14:11 -0700176 status_t setParameters(const sp<AMessage> &params);
177
Andreas Huberb12a5392012-04-30 14:18:33 -0700178 void setVideoScalingMode(int mode);
179
ybai5e053202018-11-01 13:02:15 +0800180 void selectAudioPresentation(const int32_t presentationId, const int32_t programId);
181
Wonsik Kim8798c8c2021-03-18 21:38:57 -0700182 status_t querySupportedVendorParameters(JNIEnv *env, jobject *names);
183
184 status_t describeParameter(JNIEnv *env, jstring name, jobject *desc);
185
186 status_t subscribeToVendorParameters(JNIEnv *env, jobject names);
187
188 status_t unsubscribeFromVendorParameters(JNIEnv *env, jobject names);
189
Wonsik Kimf7069ce2020-05-13 17:15:47 -0700190 bool hasCryptoOrDescrambler() { return mHasCryptoOrDescrambler; }
191
Robert Shih631a80d2021-02-14 02:23:55 -0800192 const sp<ICrypto> &getCrypto() { return mCrypto; }
193
Wonsik Kimd1a1b4e2023-01-30 10:26:08 -0800194 std::string getExceptionMessage(const char *msg) const;
195
Andreas Huber88572f72012-02-21 11:47:18 -0800196protected:
197 virtual ~JMediaCodec();
198
Andreas Huberaba67132013-10-22 12:40:01 -0700199 virtual void onMessageReceived(const sp<AMessage> &msg);
200
Andreas Huber88572f72012-02-21 11:47:18 -0800201private:
Andreas Huberaba67132013-10-22 12:40:01 -0700202 enum {
Chong Zhang8d5e5562014-07-08 18:49:21 -0700203 kWhatCallbackNotify,
Lajos Molnard8578572015-06-05 20:17:33 -0700204 kWhatFrameRendered,
Wonsik Kimd4ce4e32020-06-08 10:59:48 -0700205 kWhatAsyncReleaseComplete,
Guillaume Chelfic072caf2021-02-03 16:18:26 +0100206 kWhatFirstTunnelFrameReady,
Andreas Huberaba67132013-10-22 12:40:01 -0700207 };
208
Andreas Huber88572f72012-02-21 11:47:18 -0800209 jclass mClass;
210 jweak mObject;
Mathias Agopian52800612013-02-14 17:11:20 -0800211 sp<Surface> mSurfaceTextureClient;
Andreas Huber88572f72012-02-21 11:47:18 -0800212
213 sp<ALooper> mLooper;
214 sp<MediaCodec> mCodec;
Lajos Molnare7473872019-02-05 18:54:27 -0800215 AString mNameAtCreation;
Wonsik Kimccb7ac62019-12-27 17:12:40 -0800216 bool mGraphicOutput{false};
Wonsik Kimf7069ce2020-05-13 17:15:47 -0700217 bool mHasCryptoOrDescrambler{false};
Wonsik Kimeaed9f52019-06-18 17:25:02 -0700218 std::once_flag mReleaseFlag;
Wonsik Kimd4ce4e32020-06-08 10:59:48 -0700219 std::once_flag mAsyncReleaseFlag;
Andreas Huber88572f72012-02-21 11:47:18 -0800220
Chong Zhang8d5e5562014-07-08 18:49:21 -0700221 sp<AMessage> mCallbackNotification;
Guillaume Chelfic072caf2021-02-03 16:18:26 +0100222 sp<AMessage> mOnFirstTunnelFrameReadyNotification;
Lajos Molnard8578572015-06-05 20:17:33 -0700223 sp<AMessage> mOnFrameRenderedNotification;
Andreas Huberaba67132013-10-22 12:40:01 -0700224
Andy Hung5f9aa0b2014-07-30 15:48:21 -0700225 status_t mInitStatus;
226
Robert Shih631a80d2021-02-14 02:23:55 -0800227 sp<ICrypto> mCrypto;
228
Wonsik Kim4273dd02016-09-27 15:23:35 +0900229 template <typename T>
Lajos Molnar7de28d32014-07-25 07:51:02 -0700230 status_t createByteBufferFromABuffer(
Wonsik Kim4273dd02016-09-27 15:23:35 +0900231 JNIEnv *env, bool readOnly, bool clearBuffer, const sp<T> &buffer,
Lajos Molnar7de28d32014-07-25 07:51:02 -0700232 jobject *buf) const;
233
Lajos Molnard8578572015-06-05 20:17:33 -0700234 void handleCallback(const sp<AMessage> &msg);
Guillaume Chelfic072caf2021-02-03 16:18:26 +0100235 void handleFirstTunnelFrameReadyNotification(const sp<AMessage> &msg);
Lajos Molnard8578572015-06-05 20:17:33 -0700236 void handleFrameRenderedNotification(const sp<AMessage> &msg);
Lajos Molnar7de28d32014-07-25 07:51:02 -0700237
Andreas Huber88572f72012-02-21 11:47:18 -0800238 DISALLOW_EVIL_CONSTRUCTORS(JMediaCodec);
239};
240
241} // namespace android
242
243#endif // _ANDROID_MEDIA_MEDIACODEC_H_