• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 <mutex>
21 
22 #include "jni.h"
23 
24 #include <media/MediaAnalyticsItem.h>
25 #include <media/hardware/CryptoAPI.h>
26 #include <media/stagefright/foundation/ABase.h>
27 #include <media/stagefright/foundation/AHandler.h>
28 #include <utils/Errors.h>
29 
30 namespace android {
31 
32 struct ABuffer;
33 struct ALooper;
34 struct AMessage;
35 struct AString;
36 struct ICrypto;
37 class IGraphicBufferProducer;
38 struct MediaCodec;
39 struct PersistentSurface;
40 class Surface;
41 namespace hardware {
42 namespace cas {
43 namespace native {
44 namespace V1_0 {
45 struct IDescrambler;
46 }}}}
47 using hardware::cas::native::V1_0::IDescrambler;
48 
49 struct JMediaCodec : public AHandler {
50     JMediaCodec(
51             JNIEnv *env, jobject thiz,
52             const char *name, bool nameIsType, bool encoder);
53 
54     status_t initCheck() const;
55 
56     void registerSelf();
57     void release();
58 
59     status_t enableOnFrameRenderedListener(jboolean enable);
60 
61     status_t setCallback(jobject cb);
62 
63     status_t configure(
64             const sp<AMessage> &format,
65             const sp<IGraphicBufferProducer> &bufferProducer,
66             const sp<ICrypto> &crypto,
67             const sp<IDescrambler> &descrambler,
68             int flags);
69 
70     status_t setSurface(
71             const sp<IGraphicBufferProducer> &surface);
72 
73     status_t createInputSurface(sp<IGraphicBufferProducer>* bufferProducer);
74     status_t setInputSurface(const sp<PersistentSurface> &surface);
75 
76     status_t start();
77     status_t stop();
78     status_t reset();
79 
80     status_t flush();
81 
82     status_t queueInputBuffer(
83             size_t index,
84             size_t offset, size_t size, int64_t timeUs, uint32_t flags,
85             AString *errorDetailMsg);
86 
87     status_t queueSecureInputBuffer(
88             size_t index,
89             size_t offset,
90             const CryptoPlugin::SubSample *subSamples,
91             size_t numSubSamples,
92             const uint8_t key[16],
93             const uint8_t iv[16],
94             CryptoPlugin::Mode mode,
95             const CryptoPlugin::Pattern &pattern,
96             int64_t presentationTimeUs,
97             uint32_t flags,
98             AString *errorDetailMsg);
99 
100     status_t dequeueInputBuffer(size_t *index, int64_t timeoutUs);
101 
102     status_t dequeueOutputBuffer(
103             JNIEnv *env, jobject bufferInfo, size_t *index, int64_t timeoutUs);
104 
105     status_t releaseOutputBuffer(
106             size_t index, bool render, bool updatePTS, int64_t timestampNs);
107 
108     status_t signalEndOfInputStream();
109 
110     status_t getFormat(JNIEnv *env, bool input, jobject *format) const;
111 
112     status_t getOutputFormat(JNIEnv *env, size_t index, jobject *format) const;
113 
114     status_t getBuffers(
115             JNIEnv *env, bool input, jobjectArray *bufArray) const;
116 
117     status_t getBuffer(
118             JNIEnv *env, bool input, size_t index, jobject *buf) const;
119 
120     status_t getImage(
121             JNIEnv *env, bool input, size_t index, jobject *image) const;
122 
123     status_t getName(JNIEnv *env, jstring *name) const;
124 
125     status_t getCodecInfo(JNIEnv *env, jobject *codecInfo) const;
126 
127     status_t getMetrics(JNIEnv *env, MediaAnalyticsItem * &reply) const;
128 
129     status_t setParameters(const sp<AMessage> &params);
130 
131     void setVideoScalingMode(int mode);
132 
133     void selectAudioPresentation(const int32_t presentationId, const int32_t programId);
134 
135 protected:
136     virtual ~JMediaCodec();
137 
138     virtual void onMessageReceived(const sp<AMessage> &msg);
139 
140 private:
141     enum {
142         kWhatCallbackNotify,
143         kWhatFrameRendered,
144     };
145 
146     jclass mClass;
147     jweak mObject;
148     sp<Surface> mSurfaceTextureClient;
149 
150     // java objects cached
151     jclass mByteBufferClass;
152     jobject mNativeByteOrderObj;
153     jmethodID mByteBufferOrderMethodID;
154     jmethodID mByteBufferPositionMethodID;
155     jmethodID mByteBufferLimitMethodID;
156     jmethodID mByteBufferAsReadOnlyBufferMethodID;
157 
158     sp<ALooper> mLooper;
159     sp<MediaCodec> mCodec;
160     AString mNameAtCreation;
161     std::once_flag mReleaseFlag;
162 
163     sp<AMessage> mCallbackNotification;
164     sp<AMessage> mOnFrameRenderedNotification;
165 
166     status_t mInitStatus;
167 
168     template <typename T>
169     status_t createByteBufferFromABuffer(
170             JNIEnv *env, bool readOnly, bool clearBuffer, const sp<T> &buffer,
171             jobject *buf) const;
172 
173     void cacheJavaObjects(JNIEnv *env);
174     void deleteJavaObjects(JNIEnv *env);
175     void handleCallback(const sp<AMessage> &msg);
176     void handleFrameRenderedNotification(const sp<AMessage> &msg);
177 
178     DISALLOW_EVIL_CONSTRUCTORS(JMediaCodec);
179 };
180 
181 }  // namespace android
182 
183 #endif  // _ANDROID_MEDIA_MEDIACODEC_H_
184