blob: 02708efdea3ac1a3eb95b17f9194e9641b579c85 [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
Wonsik Kimccb7ac62019-12-27 17:12:40 -0800117 status_t queueBuffer(
118 size_t index, const std::shared_ptr<C2Buffer> &buffer,
Arun Johnson8d6a41a2024-01-10 21:14:14 +0000119 const sp<RefBase> &infos, const sp<AMessage> &tunings,
Wonsik Kimccb7ac62019-12-27 17:12:40 -0800120 AString *errorDetailMsg);
121
122 status_t queueEncryptedLinearBlock(
123 size_t index,
124 const sp<hardware::HidlMemory> &buffer,
125 size_t offset,
126 const CryptoPlugin::SubSample *subSamples,
127 size_t numSubSamples,
128 const uint8_t key[16],
129 const uint8_t iv[16],
130 CryptoPlugin::Mode mode,
131 const CryptoPlugin::Pattern &pattern,
Arun Johnson8d6a41a2024-01-10 21:14:14 +0000132 const sp<RefBase> &infos,
Wonsik Kimccb7ac62019-12-27 17:12:40 -0800133 const sp<AMessage> &tunings,
134 AString *errorDetailMsg);
135
Andreas Huber88572f72012-02-21 11:47:18 -0800136 status_t dequeueInputBuffer(size_t *index, int64_t timeoutUs);
137
138 status_t dequeueOutputBuffer(
139 JNIEnv *env, jobject bufferInfo, size_t *index, int64_t timeoutUs);
140
Lajos Molnar7c513b6b2014-05-08 17:16:45 -0700141 status_t releaseOutputBuffer(
142 size_t index, bool render, bool updatePTS, int64_t timestampNs);
Andreas Huber88572f72012-02-21 11:47:18 -0800143
Andy McFadden2621e402013-02-19 07:29:21 -0800144 status_t signalEndOfInputStream();
145
Lajos Molnard4023112014-07-11 15:12:59 -0700146 status_t getFormat(JNIEnv *env, bool input, jobject *format) const;
147
148 status_t getOutputFormat(JNIEnv *env, size_t index, jobject *format) const;
Andreas Huber88572f72012-02-21 11:47:18 -0800149
150 status_t getBuffers(
151 JNIEnv *env, bool input, jobjectArray *bufArray) const;
152
Lajos Molnard4023112014-07-11 15:12:59 -0700153 status_t getBuffer(
154 JNIEnv *env, bool input, size_t index, jobject *buf) const;
155
156 status_t getImage(
157 JNIEnv *env, bool input, size_t index, jobject *image) const;
158
Wonsik Kimccb7ac62019-12-27 17:12:40 -0800159 status_t getOutputFrame(
160 JNIEnv *env, jobject frame, size_t index) const;
161
Martin Storsjo056ef2e2012-09-25 11:53:04 +0300162 status_t getName(JNIEnv *env, jstring *name) const;
163
Chong Zhang90d73042018-02-28 18:46:26 -0800164 status_t getCodecInfo(JNIEnv *env, jobject *codecInfo) const;
165
Ray Essick81fbc5b2019-12-07 06:24:59 -0800166 status_t getMetrics(JNIEnv *env, mediametrics::Item * &reply) const;
Ray Essick0e0fee12017-01-25 18:01:56 -0800167
Andreas Huber226065b2013-08-12 10:14:11 -0700168 status_t setParameters(const sp<AMessage> &params);
169
Andreas Huberb12a5392012-04-30 14:18:33 -0700170 void setVideoScalingMode(int mode);
171
ybai5e053202018-11-01 13:02:15 +0800172 void selectAudioPresentation(const int32_t presentationId, const int32_t programId);
173
Wonsik Kim8798c8c2021-03-18 21:38:57 -0700174 status_t querySupportedVendorParameters(JNIEnv *env, jobject *names);
175
176 status_t describeParameter(JNIEnv *env, jstring name, jobject *desc);
177
178 status_t subscribeToVendorParameters(JNIEnv *env, jobject names);
179
180 status_t unsubscribeFromVendorParameters(JNIEnv *env, jobject names);
181
Wonsik Kimf7069ce2020-05-13 17:15:47 -0700182 bool hasCryptoOrDescrambler() { return mHasCryptoOrDescrambler; }
183
Robert Shih631a80d2021-02-14 02:23:55 -0800184 const sp<ICrypto> &getCrypto() { return mCrypto; }
185
Wonsik Kimd1a1b4e2023-01-30 10:26:08 -0800186 std::string getExceptionMessage(const char *msg) const;
187
Andreas Huber88572f72012-02-21 11:47:18 -0800188protected:
189 virtual ~JMediaCodec();
190
Andreas Huberaba67132013-10-22 12:40:01 -0700191 virtual void onMessageReceived(const sp<AMessage> &msg);
192
Andreas Huber88572f72012-02-21 11:47:18 -0800193private:
Andreas Huberaba67132013-10-22 12:40:01 -0700194 enum {
Chong Zhang8d5e5562014-07-08 18:49:21 -0700195 kWhatCallbackNotify,
Lajos Molnard8578572015-06-05 20:17:33 -0700196 kWhatFrameRendered,
Wonsik Kimd4ce4e32020-06-08 10:59:48 -0700197 kWhatAsyncReleaseComplete,
Guillaume Chelfic072caf2021-02-03 16:18:26 +0100198 kWhatFirstTunnelFrameReady,
Andreas Huberaba67132013-10-22 12:40:01 -0700199 };
200
Andreas Huber88572f72012-02-21 11:47:18 -0800201 jclass mClass;
202 jweak mObject;
Mathias Agopian52800612013-02-14 17:11:20 -0800203 sp<Surface> mSurfaceTextureClient;
Andreas Huber88572f72012-02-21 11:47:18 -0800204
205 sp<ALooper> mLooper;
206 sp<MediaCodec> mCodec;
Lajos Molnare7473872019-02-05 18:54:27 -0800207 AString mNameAtCreation;
Wonsik Kimccb7ac62019-12-27 17:12:40 -0800208 bool mGraphicOutput{false};
Wonsik Kimf7069ce2020-05-13 17:15:47 -0700209 bool mHasCryptoOrDescrambler{false};
Wonsik Kimeaed9f52019-06-18 17:25:02 -0700210 std::once_flag mReleaseFlag;
Wonsik Kimd4ce4e32020-06-08 10:59:48 -0700211 std::once_flag mAsyncReleaseFlag;
Andreas Huber88572f72012-02-21 11:47:18 -0800212
Chong Zhang8d5e5562014-07-08 18:49:21 -0700213 sp<AMessage> mCallbackNotification;
Guillaume Chelfic072caf2021-02-03 16:18:26 +0100214 sp<AMessage> mOnFirstTunnelFrameReadyNotification;
Lajos Molnard8578572015-06-05 20:17:33 -0700215 sp<AMessage> mOnFrameRenderedNotification;
Andreas Huberaba67132013-10-22 12:40:01 -0700216
Andy Hung5f9aa0b2014-07-30 15:48:21 -0700217 status_t mInitStatus;
218
Robert Shih631a80d2021-02-14 02:23:55 -0800219 sp<ICrypto> mCrypto;
220
Wonsik Kim4273dd02016-09-27 15:23:35 +0900221 template <typename T>
Lajos Molnar7de28d32014-07-25 07:51:02 -0700222 status_t createByteBufferFromABuffer(
Wonsik Kim4273dd02016-09-27 15:23:35 +0900223 JNIEnv *env, bool readOnly, bool clearBuffer, const sp<T> &buffer,
Lajos Molnar7de28d32014-07-25 07:51:02 -0700224 jobject *buf) const;
225
Lajos Molnard8578572015-06-05 20:17:33 -0700226 void handleCallback(const sp<AMessage> &msg);
Guillaume Chelfic072caf2021-02-03 16:18:26 +0100227 void handleFirstTunnelFrameReadyNotification(const sp<AMessage> &msg);
Lajos Molnard8578572015-06-05 20:17:33 -0700228 void handleFrameRenderedNotification(const sp<AMessage> &msg);
Lajos Molnar7de28d32014-07-25 07:51:02 -0700229
Andreas Huber88572f72012-02-21 11:47:18 -0800230 DISALLOW_EVIL_CONSTRUCTORS(JMediaCodec);
231};
232
233} // namespace android
234
235#endif // _ANDROID_MEDIA_MEDIACODEC_H_