blob: c9a1700215a47d0751060bf4cb7d85a6fcc9cafb [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 Zhangd5927ae2017-01-03 11:07:18 -080039namespace media {
40class IDescrambler;
41};
42using namespace media;
Andreas Huber88572f72012-02-21 11:47:18 -080043
Andreas Huberaba67132013-10-22 12:40:01 -070044struct JMediaCodec : public AHandler {
Andreas Huber88572f72012-02-21 11:47:18 -080045 JMediaCodec(
46 JNIEnv *env, jobject thiz,
47 const char *name, bool nameIsType, bool encoder);
48
49 status_t initCheck() const;
50
Andreas Huberaba67132013-10-22 12:40:01 -070051 void registerSelf();
Chong Zhang128b0122014-03-01 18:04:13 -080052 void release();
Andreas Huberaba67132013-10-22 12:40:01 -070053
Lajos Molnard8578572015-06-05 20:17:33 -070054 status_t enableOnFrameRenderedListener(jboolean enable);
55
Chong Zhang8d5e5562014-07-08 18:49:21 -070056 status_t setCallback(jobject cb);
57
Andreas Huber88572f72012-02-21 11:47:18 -080058 status_t configure(
59 const sp<AMessage> &format,
Andy McFaddend47f7d82012-12-18 09:48:38 -080060 const sp<IGraphicBufferProducer> &bufferProducer,
Andreas Huber8240d922012-04-04 14:06:32 -070061 const sp<ICrypto> &crypto,
Chong Zhangd5927ae2017-01-03 11:07:18 -080062 const sp<IDescrambler> &descrambler,
Andreas Huber88572f72012-02-21 11:47:18 -080063 int flags);
64
Lajos Molnar5e02ba92015-05-01 15:59:35 -070065 status_t setSurface(
66 const sp<IGraphicBufferProducer> &surface);
67
Andy McFadden2621e402013-02-19 07:29:21 -080068 status_t createInputSurface(sp<IGraphicBufferProducer>* bufferProducer);
Chong Zhang9560ddb2015-05-13 10:25:29 -070069 status_t setInputSurface(const sp<PersistentSurface> &surface);
Andy McFadden2621e402013-02-19 07:29:21 -080070
Andreas Huber88572f72012-02-21 11:47:18 -080071 status_t start();
72 status_t stop();
Lajos Molnar1e6e8012014-07-15 16:07:13 -070073 status_t reset();
Andreas Huber88572f72012-02-21 11:47:18 -080074
75 status_t flush();
76
77 status_t queueInputBuffer(
78 size_t index,
Andreas Huberbfc56f42012-04-19 12:47:07 -070079 size_t offset, size_t size, int64_t timeUs, uint32_t flags,
80 AString *errorDetailMsg);
Andreas Huber88572f72012-02-21 11:47:18 -080081
Andreas Huber9e6bcce2012-04-06 12:14:47 -070082 status_t queueSecureInputBuffer(
83 size_t index,
84 size_t offset,
85 const CryptoPlugin::SubSample *subSamples,
86 size_t numSubSamples,
87 const uint8_t key[16],
88 const uint8_t iv[16],
89 CryptoPlugin::Mode mode,
Jeff Tinkerd4ea5d32015-12-18 11:56:22 -080090 const CryptoPlugin::Pattern &pattern,
Andreas Huber9e6bcce2012-04-06 12:14:47 -070091 int64_t presentationTimeUs,
Andreas Huberbfc56f42012-04-19 12:47:07 -070092 uint32_t flags,
93 AString *errorDetailMsg);
Andreas Huber9e6bcce2012-04-06 12:14:47 -070094
Andreas Huber88572f72012-02-21 11:47:18 -080095 status_t dequeueInputBuffer(size_t *index, int64_t timeoutUs);
96
97 status_t dequeueOutputBuffer(
98 JNIEnv *env, jobject bufferInfo, size_t *index, int64_t timeoutUs);
99
Lajos Molnar7c513b6b2014-05-08 17:16:45 -0700100 status_t releaseOutputBuffer(
101 size_t index, bool render, bool updatePTS, int64_t timestampNs);
Andreas Huber88572f72012-02-21 11:47:18 -0800102
Andy McFadden2621e402013-02-19 07:29:21 -0800103 status_t signalEndOfInputStream();
104
Lajos Molnard4023112014-07-11 15:12:59 -0700105 status_t getFormat(JNIEnv *env, bool input, jobject *format) const;
106
107 status_t getOutputFormat(JNIEnv *env, size_t index, jobject *format) const;
Andreas Huber88572f72012-02-21 11:47:18 -0800108
109 status_t getBuffers(
110 JNIEnv *env, bool input, jobjectArray *bufArray) const;
111
Lajos Molnard4023112014-07-11 15:12:59 -0700112 status_t getBuffer(
113 JNIEnv *env, bool input, size_t index, jobject *buf) const;
114
115 status_t getImage(
116 JNIEnv *env, bool input, size_t index, jobject *image) const;
117
Martin Storsjo056ef2e2012-09-25 11:53:04 +0300118 status_t getName(JNIEnv *env, jstring *name) const;
119
Ray Essickf2d0e402017-03-09 10:17:51 -0800120 status_t getMetrics(JNIEnv *env, MediaAnalyticsItem * &reply) const;
Ray Essick0e0fee12017-01-25 18:01:56 -0800121
Andreas Huber226065b2013-08-12 10:14:11 -0700122 status_t setParameters(const sp<AMessage> &params);
123
Andreas Huberb12a5392012-04-30 14:18:33 -0700124 void setVideoScalingMode(int mode);
125
Andreas Huber88572f72012-02-21 11:47:18 -0800126protected:
127 virtual ~JMediaCodec();
128
Andreas Huberaba67132013-10-22 12:40:01 -0700129 virtual void onMessageReceived(const sp<AMessage> &msg);
130
Andreas Huber88572f72012-02-21 11:47:18 -0800131private:
Andreas Huberaba67132013-10-22 12:40:01 -0700132 enum {
Chong Zhang8d5e5562014-07-08 18:49:21 -0700133 kWhatCallbackNotify,
Lajos Molnard8578572015-06-05 20:17:33 -0700134 kWhatFrameRendered,
Andreas Huberaba67132013-10-22 12:40:01 -0700135 };
136
Andreas Huber88572f72012-02-21 11:47:18 -0800137 jclass mClass;
138 jweak mObject;
Mathias Agopian52800612013-02-14 17:11:20 -0800139 sp<Surface> mSurfaceTextureClient;
Andreas Huber88572f72012-02-21 11:47:18 -0800140
Lajos Molnar7de28d32014-07-25 07:51:02 -0700141 // java objects cached
142 jclass mByteBufferClass;
143 jobject mNativeByteOrderObj;
144 jmethodID mByteBufferOrderMethodID;
145 jmethodID mByteBufferPositionMethodID;
146 jmethodID mByteBufferLimitMethodID;
147 jmethodID mByteBufferAsReadOnlyBufferMethodID;
148
Andreas Huber88572f72012-02-21 11:47:18 -0800149 sp<ALooper> mLooper;
150 sp<MediaCodec> mCodec;
151
Chong Zhang8d5e5562014-07-08 18:49:21 -0700152 sp<AMessage> mCallbackNotification;
Lajos Molnard8578572015-06-05 20:17:33 -0700153 sp<AMessage> mOnFrameRenderedNotification;
Andreas Huberaba67132013-10-22 12:40:01 -0700154
Andy Hung5f9aa0b2014-07-30 15:48:21 -0700155 status_t mInitStatus;
156
Wonsik Kim4273dd02016-09-27 15:23:35 +0900157 template <typename T>
Lajos Molnar7de28d32014-07-25 07:51:02 -0700158 status_t createByteBufferFromABuffer(
Wonsik Kim4273dd02016-09-27 15:23:35 +0900159 JNIEnv *env, bool readOnly, bool clearBuffer, const sp<T> &buffer,
Lajos Molnar7de28d32014-07-25 07:51:02 -0700160 jobject *buf) const;
161
162 void cacheJavaObjects(JNIEnv *env);
163 void deleteJavaObjects(JNIEnv *env);
Lajos Molnard8578572015-06-05 20:17:33 -0700164 void handleCallback(const sp<AMessage> &msg);
165 void handleFrameRenderedNotification(const sp<AMessage> &msg);
Lajos Molnar7de28d32014-07-25 07:51:02 -0700166
Andreas Huber88572f72012-02-21 11:47:18 -0800167 DISALLOW_EVIL_CONSTRUCTORS(JMediaCodec);
168};
169
170} // namespace android
171
172#endif // _ANDROID_MEDIA_MEDIACODEC_H_