blob: fbaf64fda57283195968e675e5ccb2487e0ab353 [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;
Andreas Huber88572f72012-02-21 11:47:18 -080038struct ALooper;
39struct AMessage;
Andreas Huberbfc56f42012-04-19 12:47:07 -070040struct AString;
Andreas Huber8240d922012-04-04 14:06:32 -070041struct ICrypto;
Colin Cross08f5f1f2016-09-30 17:36:46 -070042class IGraphicBufferProducer;
Andreas Huber88572f72012-02-21 11:47:18 -080043struct MediaCodec;
Chong Zhang8034d602015-04-28 13:38:48 -070044struct PersistentSurface;
Mathias Agopian52800612013-02-14 17:11:20 -080045class Surface;
Chong Zhang2659c2f2017-04-27 13:18:20 -070046namespace hardware {
Wonsik Kimccb7ac62019-12-27 17:12:40 -080047class HidlMemory;
Chong Zhang2659c2f2017-04-27 13:18:20 -070048namespace cas {
49namespace native {
50namespace V1_0 {
51struct IDescrambler;
52}}}}
53using hardware::cas::native::V1_0::IDescrambler;
Andreas Huber88572f72012-02-21 11:47:18 -080054
Andreas Huberaba67132013-10-22 12:40:01 -070055struct JMediaCodec : public AHandler {
Andreas Huber88572f72012-02-21 11:47:18 -080056 JMediaCodec(
57 JNIEnv *env, jobject thiz,
Brian Lindahl6ceeed42022-02-01 11:10:30 +010058 const char *name, bool nameIsType, bool encoder, int pid, int uid);
Andreas Huber88572f72012-02-21 11:47:18 -080059
60 status_t initCheck() const;
61
Andreas Huberaba67132013-10-22 12:40:01 -070062 void registerSelf();
Chong Zhang128b0122014-03-01 18:04:13 -080063 void release();
Wonsik Kim89666622020-04-28 10:43:47 -070064 void releaseAsync();
Andreas Huberaba67132013-10-22 12:40:01 -070065
Guillaume Chelfic072caf2021-02-03 16:18:26 +010066 status_t enableOnFirstTunnelFrameReadyListener(jboolean enable);
67
Lajos Molnard8578572015-06-05 20:17:33 -070068 status_t enableOnFrameRenderedListener(jboolean enable);
69
Chong Zhang8d5e5562014-07-08 18:49:21 -070070 status_t setCallback(jobject cb);
71
Andreas Huber88572f72012-02-21 11:47:18 -080072 status_t configure(
73 const sp<AMessage> &format,
Andy McFaddend47f7d82012-12-18 09:48:38 -080074 const sp<IGraphicBufferProducer> &bufferProducer,
Andreas Huber8240d922012-04-04 14:06:32 -070075 const sp<ICrypto> &crypto,
Chong Zhangd5927ae2017-01-03 11:07:18 -080076 const sp<IDescrambler> &descrambler,
Andreas Huber88572f72012-02-21 11:47:18 -080077 int flags);
78
Lajos Molnar5e02ba92015-05-01 15:59:35 -070079 status_t setSurface(
80 const sp<IGraphicBufferProducer> &surface);
81
Andy McFadden2621e402013-02-19 07:29:21 -080082 status_t createInputSurface(sp<IGraphicBufferProducer>* bufferProducer);
Chong Zhang9560ddb2015-05-13 10:25:29 -070083 status_t setInputSurface(const sp<PersistentSurface> &surface);
Andy McFadden2621e402013-02-19 07:29:21 -080084
Andreas Huber88572f72012-02-21 11:47:18 -080085 status_t start();
86 status_t stop();
Lajos Molnar1e6e8012014-07-15 16:07:13 -070087 status_t reset();
Andreas Huber88572f72012-02-21 11:47:18 -080088
89 status_t flush();
90
91 status_t queueInputBuffer(
92 size_t index,
Andreas Huberbfc56f42012-04-19 12:47:07 -070093 size_t offset, size_t size, int64_t timeUs, uint32_t flags,
94 AString *errorDetailMsg);
Andreas Huber88572f72012-02-21 11:47:18 -080095
Andreas Huber9e6bcce2012-04-06 12:14:47 -070096 status_t queueSecureInputBuffer(
97 size_t index,
98 size_t offset,
99 const CryptoPlugin::SubSample *subSamples,
100 size_t numSubSamples,
101 const uint8_t key[16],
102 const uint8_t iv[16],
103 CryptoPlugin::Mode mode,
Jeff Tinkerd4ea5d32015-12-18 11:56:22 -0800104 const CryptoPlugin::Pattern &pattern,
Andreas Huber9e6bcce2012-04-06 12:14:47 -0700105 int64_t presentationTimeUs,
Andreas Huberbfc56f42012-04-19 12:47:07 -0700106 uint32_t flags,
107 AString *errorDetailMsg);
Andreas Huber9e6bcce2012-04-06 12:14:47 -0700108
Wonsik Kimccb7ac62019-12-27 17:12:40 -0800109 status_t queueBuffer(
110 size_t index, const std::shared_ptr<C2Buffer> &buffer,
111 int64_t timeUs, uint32_t flags, const sp<AMessage> &tunings,
112 AString *errorDetailMsg);
113
114 status_t queueEncryptedLinearBlock(
115 size_t index,
116 const sp<hardware::HidlMemory> &buffer,
117 size_t offset,
118 const CryptoPlugin::SubSample *subSamples,
119 size_t numSubSamples,
120 const uint8_t key[16],
121 const uint8_t iv[16],
122 CryptoPlugin::Mode mode,
123 const CryptoPlugin::Pattern &pattern,
124 int64_t presentationTimeUs,
125 uint32_t flags,
126 const sp<AMessage> &tunings,
127 AString *errorDetailMsg);
128
Andreas Huber88572f72012-02-21 11:47:18 -0800129 status_t dequeueInputBuffer(size_t *index, int64_t timeoutUs);
130
131 status_t dequeueOutputBuffer(
132 JNIEnv *env, jobject bufferInfo, size_t *index, int64_t timeoutUs);
133
Lajos Molnar7c513b6b2014-05-08 17:16:45 -0700134 status_t releaseOutputBuffer(
135 size_t index, bool render, bool updatePTS, int64_t timestampNs);
Andreas Huber88572f72012-02-21 11:47:18 -0800136
Andy McFadden2621e402013-02-19 07:29:21 -0800137 status_t signalEndOfInputStream();
138
Lajos Molnard4023112014-07-11 15:12:59 -0700139 status_t getFormat(JNIEnv *env, bool input, jobject *format) const;
140
141 status_t getOutputFormat(JNIEnv *env, size_t index, jobject *format) const;
Andreas Huber88572f72012-02-21 11:47:18 -0800142
143 status_t getBuffers(
144 JNIEnv *env, bool input, jobjectArray *bufArray) const;
145
Lajos Molnard4023112014-07-11 15:12:59 -0700146 status_t getBuffer(
147 JNIEnv *env, bool input, size_t index, jobject *buf) const;
148
149 status_t getImage(
150 JNIEnv *env, bool input, size_t index, jobject *image) const;
151
Wonsik Kimccb7ac62019-12-27 17:12:40 -0800152 status_t getOutputFrame(
153 JNIEnv *env, jobject frame, size_t index) const;
154
Martin Storsjo056ef2e2012-09-25 11:53:04 +0300155 status_t getName(JNIEnv *env, jstring *name) const;
156
Chong Zhang90d73042018-02-28 18:46:26 -0800157 status_t getCodecInfo(JNIEnv *env, jobject *codecInfo) const;
158
Ray Essick81fbc5b2019-12-07 06:24:59 -0800159 status_t getMetrics(JNIEnv *env, mediametrics::Item * &reply) const;
Ray Essick0e0fee12017-01-25 18:01:56 -0800160
Andreas Huber226065b2013-08-12 10:14:11 -0700161 status_t setParameters(const sp<AMessage> &params);
162
Andreas Huberb12a5392012-04-30 14:18:33 -0700163 void setVideoScalingMode(int mode);
164
ybai5e053202018-11-01 13:02:15 +0800165 void selectAudioPresentation(const int32_t presentationId, const int32_t programId);
166
Wonsik Kim8798c8c2021-03-18 21:38:57 -0700167 status_t querySupportedVendorParameters(JNIEnv *env, jobject *names);
168
169 status_t describeParameter(JNIEnv *env, jstring name, jobject *desc);
170
171 status_t subscribeToVendorParameters(JNIEnv *env, jobject names);
172
173 status_t unsubscribeFromVendorParameters(JNIEnv *env, jobject names);
174
Wonsik Kimf7069ce2020-05-13 17:15:47 -0700175 bool hasCryptoOrDescrambler() { return mHasCryptoOrDescrambler; }
176
Robert Shih631a80d2021-02-14 02:23:55 -0800177 const sp<ICrypto> &getCrypto() { return mCrypto; }
178
Wonsik Kimd1a1b4e2023-01-30 10:26:08 -0800179 std::string getExceptionMessage(const char *msg) const;
180
Andreas Huber88572f72012-02-21 11:47:18 -0800181protected:
182 virtual ~JMediaCodec();
183
Andreas Huberaba67132013-10-22 12:40:01 -0700184 virtual void onMessageReceived(const sp<AMessage> &msg);
185
Andreas Huber88572f72012-02-21 11:47:18 -0800186private:
Andreas Huberaba67132013-10-22 12:40:01 -0700187 enum {
Chong Zhang8d5e5562014-07-08 18:49:21 -0700188 kWhatCallbackNotify,
Lajos Molnard8578572015-06-05 20:17:33 -0700189 kWhatFrameRendered,
Wonsik Kimd4ce4e32020-06-08 10:59:48 -0700190 kWhatAsyncReleaseComplete,
Guillaume Chelfic072caf2021-02-03 16:18:26 +0100191 kWhatFirstTunnelFrameReady,
Andreas Huberaba67132013-10-22 12:40:01 -0700192 };
193
Andreas Huber88572f72012-02-21 11:47:18 -0800194 jclass mClass;
195 jweak mObject;
Mathias Agopian52800612013-02-14 17:11:20 -0800196 sp<Surface> mSurfaceTextureClient;
Andreas Huber88572f72012-02-21 11:47:18 -0800197
198 sp<ALooper> mLooper;
199 sp<MediaCodec> mCodec;
Lajos Molnare7473872019-02-05 18:54:27 -0800200 AString mNameAtCreation;
Wonsik Kimccb7ac62019-12-27 17:12:40 -0800201 bool mGraphicOutput{false};
Wonsik Kimf7069ce2020-05-13 17:15:47 -0700202 bool mHasCryptoOrDescrambler{false};
Wonsik Kimeaed9f52019-06-18 17:25:02 -0700203 std::once_flag mReleaseFlag;
Wonsik Kimd4ce4e32020-06-08 10:59:48 -0700204 std::once_flag mAsyncReleaseFlag;
Andreas Huber88572f72012-02-21 11:47:18 -0800205
Chong Zhang8d5e5562014-07-08 18:49:21 -0700206 sp<AMessage> mCallbackNotification;
Guillaume Chelfic072caf2021-02-03 16:18:26 +0100207 sp<AMessage> mOnFirstTunnelFrameReadyNotification;
Lajos Molnard8578572015-06-05 20:17:33 -0700208 sp<AMessage> mOnFrameRenderedNotification;
Andreas Huberaba67132013-10-22 12:40:01 -0700209
Andy Hung5f9aa0b2014-07-30 15:48:21 -0700210 status_t mInitStatus;
211
Robert Shih631a80d2021-02-14 02:23:55 -0800212 sp<ICrypto> mCrypto;
213
Wonsik Kim4273dd02016-09-27 15:23:35 +0900214 template <typename T>
Lajos Molnar7de28d32014-07-25 07:51:02 -0700215 status_t createByteBufferFromABuffer(
Wonsik Kim4273dd02016-09-27 15:23:35 +0900216 JNIEnv *env, bool readOnly, bool clearBuffer, const sp<T> &buffer,
Lajos Molnar7de28d32014-07-25 07:51:02 -0700217 jobject *buf) const;
218
Lajos Molnard8578572015-06-05 20:17:33 -0700219 void handleCallback(const sp<AMessage> &msg);
Guillaume Chelfic072caf2021-02-03 16:18:26 +0100220 void handleFirstTunnelFrameReadyNotification(const sp<AMessage> &msg);
Lajos Molnard8578572015-06-05 20:17:33 -0700221 void handleFrameRenderedNotification(const sp<AMessage> &msg);
Lajos Molnar7de28d32014-07-25 07:51:02 -0700222
Andreas Huber88572f72012-02-21 11:47:18 -0800223 DISALLOW_EVIL_CONSTRUCTORS(JMediaCodec);
224};
225
226} // namespace android
227
228#endif // _ANDROID_MEDIA_MEDIACODEC_H_