blob: a58f9a74b563529ff511517608640f56aea60dcd [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,
58 const char *name, bool nameIsType, bool encoder);
59
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
Lajos Molnard8578572015-06-05 20:17:33 -070066 status_t enableOnFrameRenderedListener(jboolean enable);
67
Chong Zhang8d5e5562014-07-08 18:49:21 -070068 status_t setCallback(jobject cb);
69
Andreas Huber88572f72012-02-21 11:47:18 -080070 status_t configure(
71 const sp<AMessage> &format,
Andy McFaddend47f7d82012-12-18 09:48:38 -080072 const sp<IGraphicBufferProducer> &bufferProducer,
Andreas Huber8240d922012-04-04 14:06:32 -070073 const sp<ICrypto> &crypto,
Chong Zhangd5927ae2017-01-03 11:07:18 -080074 const sp<IDescrambler> &descrambler,
Andreas Huber88572f72012-02-21 11:47:18 -080075 int flags);
76
Lajos Molnar5e02ba92015-05-01 15:59:35 -070077 status_t setSurface(
78 const sp<IGraphicBufferProducer> &surface);
79
Andy McFadden2621e402013-02-19 07:29:21 -080080 status_t createInputSurface(sp<IGraphicBufferProducer>* bufferProducer);
Chong Zhang9560ddb2015-05-13 10:25:29 -070081 status_t setInputSurface(const sp<PersistentSurface> &surface);
Andy McFadden2621e402013-02-19 07:29:21 -080082
Andreas Huber88572f72012-02-21 11:47:18 -080083 status_t start();
84 status_t stop();
Lajos Molnar1e6e8012014-07-15 16:07:13 -070085 status_t reset();
Andreas Huber88572f72012-02-21 11:47:18 -080086
87 status_t flush();
88
89 status_t queueInputBuffer(
90 size_t index,
Andreas Huberbfc56f42012-04-19 12:47:07 -070091 size_t offset, size_t size, int64_t timeUs, uint32_t flags,
92 AString *errorDetailMsg);
Andreas Huber88572f72012-02-21 11:47:18 -080093
Andreas Huber9e6bcce2012-04-06 12:14:47 -070094 status_t queueSecureInputBuffer(
95 size_t index,
96 size_t offset,
97 const CryptoPlugin::SubSample *subSamples,
98 size_t numSubSamples,
99 const uint8_t key[16],
100 const uint8_t iv[16],
101 CryptoPlugin::Mode mode,
Jeff Tinkerd4ea5d32015-12-18 11:56:22 -0800102 const CryptoPlugin::Pattern &pattern,
Andreas Huber9e6bcce2012-04-06 12:14:47 -0700103 int64_t presentationTimeUs,
Andreas Huberbfc56f42012-04-19 12:47:07 -0700104 uint32_t flags,
105 AString *errorDetailMsg);
Andreas Huber9e6bcce2012-04-06 12:14:47 -0700106
Wonsik Kimccb7ac62019-12-27 17:12:40 -0800107 status_t queueBuffer(
108 size_t index, const std::shared_ptr<C2Buffer> &buffer,
109 int64_t timeUs, uint32_t flags, const sp<AMessage> &tunings,
110 AString *errorDetailMsg);
111
112 status_t queueEncryptedLinearBlock(
113 size_t index,
114 const sp<hardware::HidlMemory> &buffer,
115 size_t offset,
116 const CryptoPlugin::SubSample *subSamples,
117 size_t numSubSamples,
118 const uint8_t key[16],
119 const uint8_t iv[16],
120 CryptoPlugin::Mode mode,
121 const CryptoPlugin::Pattern &pattern,
122 int64_t presentationTimeUs,
123 uint32_t flags,
124 const sp<AMessage> &tunings,
125 AString *errorDetailMsg);
126
Andreas Huber88572f72012-02-21 11:47:18 -0800127 status_t dequeueInputBuffer(size_t *index, int64_t timeoutUs);
128
129 status_t dequeueOutputBuffer(
130 JNIEnv *env, jobject bufferInfo, size_t *index, int64_t timeoutUs);
131
Lajos Molnar7c513b6b2014-05-08 17:16:45 -0700132 status_t releaseOutputBuffer(
133 size_t index, bool render, bool updatePTS, int64_t timestampNs);
Andreas Huber88572f72012-02-21 11:47:18 -0800134
Andy McFadden2621e402013-02-19 07:29:21 -0800135 status_t signalEndOfInputStream();
136
Lajos Molnard4023112014-07-11 15:12:59 -0700137 status_t getFormat(JNIEnv *env, bool input, jobject *format) const;
138
139 status_t getOutputFormat(JNIEnv *env, size_t index, jobject *format) const;
Andreas Huber88572f72012-02-21 11:47:18 -0800140
141 status_t getBuffers(
142 JNIEnv *env, bool input, jobjectArray *bufArray) const;
143
Lajos Molnard4023112014-07-11 15:12:59 -0700144 status_t getBuffer(
145 JNIEnv *env, bool input, size_t index, jobject *buf) const;
146
147 status_t getImage(
148 JNIEnv *env, bool input, size_t index, jobject *image) const;
149
Wonsik Kimccb7ac62019-12-27 17:12:40 -0800150 status_t getOutputFrame(
151 JNIEnv *env, jobject frame, size_t index) const;
152
Martin Storsjo056ef2e2012-09-25 11:53:04 +0300153 status_t getName(JNIEnv *env, jstring *name) const;
154
Chong Zhang90d73042018-02-28 18:46:26 -0800155 status_t getCodecInfo(JNIEnv *env, jobject *codecInfo) const;
156
Ray Essick81fbc5b2019-12-07 06:24:59 -0800157 status_t getMetrics(JNIEnv *env, mediametrics::Item * &reply) const;
Ray Essick0e0fee12017-01-25 18:01:56 -0800158
Andreas Huber226065b2013-08-12 10:14:11 -0700159 status_t setParameters(const sp<AMessage> &params);
160
Andreas Huberb12a5392012-04-30 14:18:33 -0700161 void setVideoScalingMode(int mode);
162
ybai5e053202018-11-01 13:02:15 +0800163 void selectAudioPresentation(const int32_t presentationId, const int32_t programId);
164
Wonsik Kimf7069ce2020-05-13 17:15:47 -0700165 bool hasCryptoOrDescrambler() { return mHasCryptoOrDescrambler; }
166
Andreas Huber88572f72012-02-21 11:47:18 -0800167protected:
168 virtual ~JMediaCodec();
169
Andreas Huberaba67132013-10-22 12:40:01 -0700170 virtual void onMessageReceived(const sp<AMessage> &msg);
171
Andreas Huber88572f72012-02-21 11:47:18 -0800172private:
Andreas Huberaba67132013-10-22 12:40:01 -0700173 enum {
Chong Zhang8d5e5562014-07-08 18:49:21 -0700174 kWhatCallbackNotify,
Lajos Molnard8578572015-06-05 20:17:33 -0700175 kWhatFrameRendered,
Wonsik Kimd4ce4e32020-06-08 10:59:48 -0700176 kWhatAsyncReleaseComplete,
Andreas Huberaba67132013-10-22 12:40:01 -0700177 };
178
Andreas Huber88572f72012-02-21 11:47:18 -0800179 jclass mClass;
180 jweak mObject;
Mathias Agopian52800612013-02-14 17:11:20 -0800181 sp<Surface> mSurfaceTextureClient;
Andreas Huber88572f72012-02-21 11:47:18 -0800182
183 sp<ALooper> mLooper;
184 sp<MediaCodec> mCodec;
Lajos Molnare7473872019-02-05 18:54:27 -0800185 AString mNameAtCreation;
Wonsik Kimccb7ac62019-12-27 17:12:40 -0800186 bool mGraphicOutput{false};
Wonsik Kimf7069ce2020-05-13 17:15:47 -0700187 bool mHasCryptoOrDescrambler{false};
Wonsik Kimeaed9f52019-06-18 17:25:02 -0700188 std::once_flag mReleaseFlag;
Wonsik Kimd4ce4e32020-06-08 10:59:48 -0700189 std::once_flag mAsyncReleaseFlag;
Andreas Huber88572f72012-02-21 11:47:18 -0800190
Chong Zhang8d5e5562014-07-08 18:49:21 -0700191 sp<AMessage> mCallbackNotification;
Lajos Molnard8578572015-06-05 20:17:33 -0700192 sp<AMessage> mOnFrameRenderedNotification;
Andreas Huberaba67132013-10-22 12:40:01 -0700193
Andy Hung5f9aa0b2014-07-30 15:48:21 -0700194 status_t mInitStatus;
195
Wonsik Kim4273dd02016-09-27 15:23:35 +0900196 template <typename T>
Lajos Molnar7de28d32014-07-25 07:51:02 -0700197 status_t createByteBufferFromABuffer(
Wonsik Kim4273dd02016-09-27 15:23:35 +0900198 JNIEnv *env, bool readOnly, bool clearBuffer, const sp<T> &buffer,
Lajos Molnar7de28d32014-07-25 07:51:02 -0700199 jobject *buf) const;
200
Lajos Molnard8578572015-06-05 20:17:33 -0700201 void handleCallback(const sp<AMessage> &msg);
202 void handleFrameRenderedNotification(const sp<AMessage> &msg);
Lajos Molnar7de28d32014-07-25 07:51:02 -0700203
Andreas Huber88572f72012-02-21 11:47:18 -0800204 DISALLOW_EVIL_CONSTRUCTORS(JMediaCodec);
205};
206
207} // namespace android
208
209#endif // _ANDROID_MEDIA_MEDIACODEC_H_