1 /*
2  * Copyright 2017, 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 NDK_WRAPPER_H_
18 
19 #define NDK_WRAPPER_H_
20 
21 #include <media/DataSource.h>
22 #include <media/MediaSource.h>
23 #include <media/NdkMediaDataSource.h>
24 #include <media/NdkMediaError.h>
25 #include <media/NdkMediaExtractor.h>
26 #include <media/hardware/CryptoAPI.h>
27 #include <media/stagefright/foundation/ABase.h>
28 #include <media/stagefright/foundation/ABuffer.h>
29 #include <utils/Errors.h>
30 #include <utils/RefBase.h>
31 
32 struct AMediaCodec;
33 struct AMediaCodecBufferInfo;
34 struct AMediaCodecCryptoInfo;
35 struct AMediaCrypto;
36 struct AMediaDrm;
37 struct AMediaFormat;
38 struct AMediaExtractor;
39 struct ANativeWindow;
40 struct PsshInfo;
41 
42 namespace android {
43 
44 struct AMessage;
45 class MetaData;
46 
47 struct AMediaFormatWrapper : public RefBase {
48 
49     static sp<AMediaFormatWrapper> Create(const sp<AMessage> &message);
50 
51     AMediaFormatWrapper();
52     AMediaFormatWrapper(AMediaFormat *aMediaFormat);
53 
54     // the returned AMediaFormat is still owned by this wrapper.
55     AMediaFormat *getAMediaFormat() const;
56 
57     sp<AMessage> toAMessage() const ;
58     void writeToAMessage(sp<AMessage>&) const ;
59     const char* toString() const ;
60 
61     status_t release();
62 
63     bool getInt32(const char *name, int32_t *out) const;
64     bool getInt64(const char *name, int64_t *out) const;
65     bool getFloat(const char *name, float *out) const;
66     bool getDouble(const char *name, double *out) const;
67     bool getSize(const char *name, size_t *out) const;
68     bool getRect(const char *name,
69                  int32_t *left, int32_t *top, int32_t *right, int32_t *bottom) const;
70     bool getBuffer(const char *name, void** data, size_t *outsize) const;
71     bool getString(const char *name, AString *out) const;
72 
73     void setInt32(const char* name, int32_t value);
74     void setInt64(const char* name, int64_t value);
75     void setFloat(const char* name, float value);
76     void setDouble(const char *name, double value);
77     void setSize(const char* name, size_t value);
78     void setRect(const char* name,
79                  int32_t left, int32_t top, int32_t right, int32_t bottom);
80     void setString(const char* name, const AString &value);
81     void setBuffer(const char* name, void* data, size_t size);
82 
83 protected:
84     virtual ~AMediaFormatWrapper();
85 
86 private:
87     AMediaFormat *mAMediaFormat;
88 
89     DISALLOW_EVIL_CONSTRUCTORS(AMediaFormatWrapper);
90 };
91 
92 struct ANativeWindowWrapper : public RefBase {
93     ANativeWindowWrapper(ANativeWindow *aNativeWindow);
94 
95     // the returned ANativeWindow is still owned by this wrapper.
96     ANativeWindow *getANativeWindow() const;
97 
98     status_t release();
99 
100 protected:
101     virtual ~ANativeWindowWrapper();
102 
103 private:
104     ANativeWindow *mANativeWindow;
105 
106     DISALLOW_EVIL_CONSTRUCTORS(ANativeWindowWrapper);
107 };
108 
109 struct AMediaDrmWrapper : public RefBase {
110     AMediaDrmWrapper(const uint8_t uuid[16]);
111     AMediaDrmWrapper(AMediaDrm *aMediaDrm);
112 
113     // the returned AMediaDrm is still owned by this wrapper.
114     AMediaDrm *getAMediaDrm() const;
115 
116     status_t release();
117 
118     static bool isCryptoSchemeSupported(const uint8_t uuid[16], const char *mimeType);
119 
120 protected:
121     virtual ~AMediaDrmWrapper();
122 
123 private:
124     AMediaDrm *mAMediaDrm;
125 
126     DISALLOW_EVIL_CONSTRUCTORS(AMediaDrmWrapper);
127 };
128 
129 struct AMediaCryptoWrapper : public RefBase {
130     AMediaCryptoWrapper(const uint8_t uuid[16],
131                         const void *initData,
132                         size_t initDataSize);
133     AMediaCryptoWrapper(AMediaCrypto *aMediaCrypto);
134 
135     // the returned AMediaCrypto is still owned by this wrapper.
136     AMediaCrypto *getAMediaCrypto() const;
137 
138     status_t release();
139 
140     bool isCryptoSchemeSupported(const uint8_t uuid[16]);
141 
142     bool requiresSecureDecoderComponent(const char *mime);
143 
144 protected:
145     virtual ~AMediaCryptoWrapper();
146 
147 private:
148     AMediaCrypto *mAMediaCrypto;
149 
150     DISALLOW_EVIL_CONSTRUCTORS(AMediaCryptoWrapper);
151 };
152 
153 struct AMediaCodecCryptoInfoWrapper : public RefBase {
154     static sp<AMediaCodecCryptoInfoWrapper> Create(MetaDataBase &meta);
155 
156     AMediaCodecCryptoInfoWrapper(int numsubsamples,
157                                  uint8_t key[16],
158                                  uint8_t iv[16],
159                                  CryptoPlugin::Mode mode,
160                                  size_t *clearbytes,
161                                  size_t *encryptedbytes);
162     AMediaCodecCryptoInfoWrapper(AMediaCodecCryptoInfo *aMediaCodecCryptoInfo);
163 
164     // the returned AMediaCryptoInfo is still owned by this wrapper.
165     AMediaCodecCryptoInfo *getAMediaCodecCryptoInfo() const;
166 
167     status_t release();
168 
169     void setPattern(CryptoPlugin::Pattern *pattern);
170 
171     size_t getNumSubSamples();
172 
173     status_t getKey(uint8_t *dst);
174 
175     status_t getIV(uint8_t *dst);
176 
177     CryptoPlugin::Mode getMode();
178 
179     status_t getClearBytes(size_t *dst);
180 
181     status_t getEncryptedBytes(size_t *dst);
182 
183 protected:
184     virtual ~AMediaCodecCryptoInfoWrapper();
185 
186 private:
187     AMediaCodecCryptoInfo *mAMediaCodecCryptoInfo;
188 
189     DISALLOW_EVIL_CONSTRUCTORS(AMediaCodecCryptoInfoWrapper);
190 };
191 
192 struct AMediaCodecWrapper : public RefBase {
193     enum {
194         CB_INPUT_AVAILABLE = 1,
195         CB_OUTPUT_AVAILABLE = 2,
196         CB_ERROR = 3,
197         CB_OUTPUT_FORMAT_CHANGED = 4,
198     };
199 
200     static sp<AMediaCodecWrapper> CreateCodecByName(const AString &name);
201     static sp<AMediaCodecWrapper> CreateDecoderByType(const AString &mimeType);
202 
203     static void OnInputAvailableCB(AMediaCodec *codec,
204                                    void *userdata,
205                                    int32_t index);
206     static void OnOutputAvailableCB(AMediaCodec *codec,
207                                     void *userdata,
208                                     int32_t index,
209                                     AMediaCodecBufferInfo *bufferInfo);
210     static void OnFormatChangedCB(AMediaCodec *codec,
211                                   void *userdata,
212                                   AMediaFormat *format);
213     static void OnErrorCB(AMediaCodec *codec,
214                           void *userdata,
215                           media_status_t err,
216                           int32_t actionCode,
217                           const char *detail);
218 
219     AMediaCodecWrapper(AMediaCodec *aMediaCodec);
220 
221     // the returned AMediaCodec is still owned by this wrapper.
222     AMediaCodec *getAMediaCodec() const;
223 
224     status_t release();
225 
226     status_t getName(AString* outComponentName) const;
227 
228     status_t configure(
229             const sp<AMediaFormatWrapper> &format,
230             const sp<ANativeWindowWrapper> &nww,
231             const sp<AMediaCryptoWrapper> &crypto,
232             uint32_t flags);
233 
234     status_t setCallback(const sp<AMessage> &callback);
235 
236     status_t releaseCrypto();
237 
238     status_t start();
239     status_t stop();
240     status_t flush();
241 
242     uint8_t* getInputBuffer(size_t idx, size_t *out_size);
243     uint8_t* getOutputBuffer(size_t idx, size_t *out_size);
244 
245     status_t queueInputBuffer(
246             size_t idx,
247             size_t offset,
248             size_t size,
249             uint64_t time,
250             uint32_t flags);
251 
252     status_t queueSecureInputBuffer(
253             size_t idx,
254             size_t offset,
255             sp<AMediaCodecCryptoInfoWrapper> &codecCryptoInfo,
256             uint64_t time,
257             uint32_t flags);
258 
259     sp<AMediaFormatWrapper> getOutputFormat();
260     sp<AMediaFormatWrapper> getInputFormat();
261 
262     status_t releaseOutputBuffer(size_t idx, bool render);
263 
264     status_t setOutputSurface(const sp<ANativeWindowWrapper> &nww);
265 
266     status_t releaseOutputBufferAtTime(size_t idx, int64_t timestampNs);
267 
268     status_t setParameters(const sp<AMediaFormatWrapper> &params);
269 
270 protected:
271     virtual ~AMediaCodecWrapper();
272 
273 private:
274     AMediaCodec *mAMediaCodec;
275 
276     sp<AMessage> mCallback;
277 
278     DISALLOW_EVIL_CONSTRUCTORS(AMediaCodecWrapper);
279 };
280 
281 struct AMediaDataSourceWrapper : public RefBase {
282 
283     AMediaDataSourceWrapper(const sp<DataSource>&);
284     AMediaDataSourceWrapper(AMediaDataSource*);
285 
286     AMediaDataSource *getAMediaDataSource();
287 
288     void close();
289 
290 protected:
291     virtual ~AMediaDataSourceWrapper();
292 
293 private:
294     sp<DataSource> mDataSource;
295     AMediaDataSource *mAMediaDataSource;
296 
297     DISALLOW_EVIL_CONSTRUCTORS(AMediaDataSourceWrapper);
298 };
299 
300 struct AMediaExtractorWrapper : public RefBase {
301 
302     AMediaExtractorWrapper(AMediaExtractor *aMediaExtractor);
303 
304     // the returned AMediaExtractor is still owned by this wrapper.
305     AMediaExtractor *getAMediaExtractor() const;
306 
307     status_t release();
308 
309     status_t setDataSource(int fd, off64_t offset, off64_t length);
310 
311     status_t setDataSource(const char *location);
312 
313     status_t setDataSource(AMediaDataSource *);
314 
315     size_t getTrackCount();
316 
317     sp<AMediaFormatWrapper> getFormat();
318 
319     sp<AMediaFormatWrapper> getTrackFormat(size_t idx);
320 
321     status_t selectTrack(size_t idx);
322 
323     status_t unselectTrack(size_t idx);
324 
325     status_t selectSingleTrack(size_t idx);
326 
327     ssize_t readSampleData(const sp<ABuffer> &buffer);
328 
329     ssize_t getSampleSize();
330 
331     uint32_t getSampleFlags();
332 
333     int getSampleTrackIndex();
334 
335     int64_t getSampleTime();
336 
337     status_t getSampleFormat(sp<AMediaFormatWrapper> &formatWrapper);
338 
339     int64_t getCachedDuration();
340 
341     bool advance();
342 
343     status_t seekTo(int64_t seekPosUs, MediaSource::ReadOptions::SeekMode mode);
344 
345     // the returned PsshInfo is still owned by this wrapper.
346     PsshInfo* getPsshInfo();
347 
348     sp<AMediaCodecCryptoInfoWrapper> getSampleCryptoInfo();
349 
350 protected:
351     virtual ~AMediaExtractorWrapper();
352 
353 private:
354     AMediaExtractor *mAMediaExtractor;
355 
356     DISALLOW_EVIL_CONSTRUCTORS(AMediaExtractorWrapper);
357 };
358 
359 }  // namespace android
360 
361 #endif  // NDK_WRAPPER_H_
362