blob: 2ec8703adc8311ee08f8f1dd136074cc7c882efd [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
20#include "jni.h"
21
Ray Essickf2d0e402017-03-09 10:17:51 -080022#include <media/MediaAnalyticsItem.h>
Andreas Huber9e6bcce2012-04-06 12:14:47 -070023#include <media/hardware/CryptoAPI.h>
Andreas Huber88572f72012-02-21 11:47:18 -080024#include <media/stagefright/foundation/ABase.h>
Andreas Huberaba67132013-10-22 12:40:01 -070025#include <media/stagefright/foundation/AHandler.h>
Andreas Huber88572f72012-02-21 11:47:18 -080026#include <utils/Errors.h>
Andreas Huber88572f72012-02-21 11:47:18 -080027
28namespace android {
29
Lajos Molnar7de28d32014-07-25 07:51:02 -070030struct ABuffer;
Andreas Huber88572f72012-02-21 11:47:18 -080031struct ALooper;
32struct AMessage;
Andreas Huberbfc56f42012-04-19 12:47:07 -070033struct AString;
Andreas Huber8240d922012-04-04 14:06:32 -070034struct ICrypto;
Colin Cross08f5f1f2016-09-30 17:36:46 -070035class IGraphicBufferProducer;
Andreas Huber88572f72012-02-21 11:47:18 -080036struct MediaCodec;
Chong Zhang8034d602015-04-28 13:38:48 -070037struct PersistentSurface;
Mathias Agopian52800612013-02-14 17:11:20 -080038class Surface;
Chong Zhang2659c2f2017-04-27 13:18:20 -070039namespace hardware {
40namespace cas {
41namespace native {
42namespace V1_0 {
43struct IDescrambler;
44}}}}
45using hardware::cas::native::V1_0::IDescrambler;
Andreas Huber88572f72012-02-21 11:47:18 -080046
Andreas Huberaba67132013-10-22 12:40:01 -070047struct JMediaCodec : public AHandler {
Andreas Huber88572f72012-02-21 11:47:18 -080048 JMediaCodec(
49 JNIEnv *env, jobject thiz,
50 const char *name, bool nameIsType, bool encoder);
51
52 status_t initCheck() const;
53
Andreas Huberaba67132013-10-22 12:40:01 -070054 void registerSelf();
Chong Zhang128b0122014-03-01 18:04:13 -080055 void release();
Andreas Huberaba67132013-10-22 12:40:01 -070056
Lajos Molnard8578572015-06-05 20:17:33 -070057 status_t enableOnFrameRenderedListener(jboolean enable);
58
Chong Zhang8d5e5562014-07-08 18:49:21 -070059 status_t setCallback(jobject cb);
60
Andreas Huber88572f72012-02-21 11:47:18 -080061 status_t configure(
62 const sp<AMessage> &format,
Andy McFaddend47f7d82012-12-18 09:48:38 -080063 const sp<IGraphicBufferProducer> &bufferProducer,
Andreas Huber8240d922012-04-04 14:06:32 -070064 const sp<ICrypto> &crypto,
Chong Zhangd5927ae2017-01-03 11:07:18 -080065 const sp<IDescrambler> &descrambler,
Andreas Huber88572f72012-02-21 11:47:18 -080066 int flags);
67
Lajos Molnar5e02ba92015-05-01 15:59:35 -070068 status_t setSurface(
69 const sp<IGraphicBufferProducer> &surface);
70
Andy McFadden2621e402013-02-19 07:29:21 -080071 status_t createInputSurface(sp<IGraphicBufferProducer>* bufferProducer);
Chong Zhang9560ddb2015-05-13 10:25:29 -070072 status_t setInputSurface(const sp<PersistentSurface> &surface);
Andy McFadden2621e402013-02-19 07:29:21 -080073
Andreas Huber88572f72012-02-21 11:47:18 -080074 status_t start();
75 status_t stop();
Lajos Molnar1e6e8012014-07-15 16:07:13 -070076 status_t reset();
Andreas Huber88572f72012-02-21 11:47:18 -080077
78 status_t flush();
79
80 status_t queueInputBuffer(
81 size_t index,
Andreas Huberbfc56f42012-04-19 12:47:07 -070082 size_t offset, size_t size, int64_t timeUs, uint32_t flags,
83 AString *errorDetailMsg);
Andreas Huber88572f72012-02-21 11:47:18 -080084
Andreas Huber9e6bcce2012-04-06 12:14:47 -070085 status_t queueSecureInputBuffer(
86 size_t index,
87 size_t offset,
88 const CryptoPlugin::SubSample *subSamples,
89 size_t numSubSamples,
90 const uint8_t key[16],
91 const uint8_t iv[16],
92 CryptoPlugin::Mode mode,
Jeff Tinkerd4ea5d32015-12-18 11:56:22 -080093 const CryptoPlugin::Pattern &pattern,
Andreas Huber9e6bcce2012-04-06 12:14:47 -070094 int64_t presentationTimeUs,
Andreas Huberbfc56f42012-04-19 12:47:07 -070095 uint32_t flags,
96 AString *errorDetailMsg);
Andreas Huber9e6bcce2012-04-06 12:14:47 -070097
Andreas Huber88572f72012-02-21 11:47:18 -080098 status_t dequeueInputBuffer(size_t *index, int64_t timeoutUs);
99
100 status_t dequeueOutputBuffer(
101 JNIEnv *env, jobject bufferInfo, size_t *index, int64_t timeoutUs);
102
Lajos Molnar7c513b6b2014-05-08 17:16:45 -0700103 status_t releaseOutputBuffer(
104 size_t index, bool render, bool updatePTS, int64_t timestampNs);
Andreas Huber88572f72012-02-21 11:47:18 -0800105
Andy McFadden2621e402013-02-19 07:29:21 -0800106 status_t signalEndOfInputStream();
107
Lajos Molnard4023112014-07-11 15:12:59 -0700108 status_t getFormat(JNIEnv *env, bool input, jobject *format) const;
109
110 status_t getOutputFormat(JNIEnv *env, size_t index, jobject *format) const;
Andreas Huber88572f72012-02-21 11:47:18 -0800111
112 status_t getBuffers(
113 JNIEnv *env, bool input, jobjectArray *bufArray) const;
114
Lajos Molnard4023112014-07-11 15:12:59 -0700115 status_t getBuffer(
116 JNIEnv *env, bool input, size_t index, jobject *buf) const;
117
118 status_t getImage(
119 JNIEnv *env, bool input, size_t index, jobject *image) const;
120
Martin Storsjo056ef2e2012-09-25 11:53:04 +0300121 status_t getName(JNIEnv *env, jstring *name) const;
122
Ray Essickf2d0e402017-03-09 10:17:51 -0800123 status_t getMetrics(JNIEnv *env, MediaAnalyticsItem * &reply) const;
Ray Essick0e0fee12017-01-25 18:01:56 -0800124
Andreas Huber226065b2013-08-12 10:14:11 -0700125 status_t setParameters(const sp<AMessage> &params);
126
Andreas Huberb12a5392012-04-30 14:18:33 -0700127 void setVideoScalingMode(int mode);
128
Andreas Huber88572f72012-02-21 11:47:18 -0800129protected:
130 virtual ~JMediaCodec();
131
Andreas Huberaba67132013-10-22 12:40:01 -0700132 virtual void onMessageReceived(const sp<AMessage> &msg);
133
Andreas Huber88572f72012-02-21 11:47:18 -0800134private:
Andreas Huberaba67132013-10-22 12:40:01 -0700135 enum {
Chong Zhang8d5e5562014-07-08 18:49:21 -0700136 kWhatCallbackNotify,
Lajos Molnard8578572015-06-05 20:17:33 -0700137 kWhatFrameRendered,
Andreas Huberaba67132013-10-22 12:40:01 -0700138 };
139
Andreas Huber88572f72012-02-21 11:47:18 -0800140 jclass mClass;
141 jweak mObject;
Mathias Agopian52800612013-02-14 17:11:20 -0800142 sp<Surface> mSurfaceTextureClient;
Andreas Huber88572f72012-02-21 11:47:18 -0800143
Lajos Molnar7de28d32014-07-25 07:51:02 -0700144 // java objects cached
145 jclass mByteBufferClass;
146 jobject mNativeByteOrderObj;
147 jmethodID mByteBufferOrderMethodID;
148 jmethodID mByteBufferPositionMethodID;
149 jmethodID mByteBufferLimitMethodID;
150 jmethodID mByteBufferAsReadOnlyBufferMethodID;
151
Andreas Huber88572f72012-02-21 11:47:18 -0800152 sp<ALooper> mLooper;
153 sp<MediaCodec> mCodec;
154
Chong Zhang8d5e5562014-07-08 18:49:21 -0700155 sp<AMessage> mCallbackNotification;
Lajos Molnard8578572015-06-05 20:17:33 -0700156 sp<AMessage> mOnFrameRenderedNotification;
Andreas Huberaba67132013-10-22 12:40:01 -0700157
Andy Hung5f9aa0b2014-07-30 15:48:21 -0700158 status_t mInitStatus;
159
Wonsik Kim4273dd02016-09-27 15:23:35 +0900160 template <typename T>
Lajos Molnar7de28d32014-07-25 07:51:02 -0700161 status_t createByteBufferFromABuffer(
Wonsik Kim4273dd02016-09-27 15:23:35 +0900162 JNIEnv *env, bool readOnly, bool clearBuffer, const sp<T> &buffer,
Lajos Molnar7de28d32014-07-25 07:51:02 -0700163 jobject *buf) const;
164
165 void cacheJavaObjects(JNIEnv *env);
166 void deleteJavaObjects(JNIEnv *env);
Lajos Molnard8578572015-06-05 20:17:33 -0700167 void handleCallback(const sp<AMessage> &msg);
168 void handleFrameRenderedNotification(const sp<AMessage> &msg);
Lajos Molnar7de28d32014-07-25 07:51:02 -0700169
Andreas Huber88572f72012-02-21 11:47:18 -0800170 DISALLOW_EVIL_CONSTRUCTORS(JMediaCodec);
171};
172
173} // namespace android
174
175#endif // _ANDROID_MEDIA_MEDIACODEC_H_