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
32struct AMediaCodec;
33struct AMediaCodecBufferInfo;
34struct AMediaCodecCryptoInfo;
35struct AMediaCrypto;
36struct AMediaDrm;
37struct AMediaFormat;
38struct AMediaExtractor;
39struct ANativeWindow;
40struct PsshInfo;
41
42namespace android {
43
44struct AMessage;
45class MetaData;
46
47struct 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
83protected:
84    virtual ~AMediaFormatWrapper();
85
86private:
87    AMediaFormat *mAMediaFormat;
88
89    DISALLOW_EVIL_CONSTRUCTORS(AMediaFormatWrapper);
90};
91
92struct 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
100protected:
101    virtual ~ANativeWindowWrapper();
102
103private:
104    ANativeWindow *mANativeWindow;
105
106    DISALLOW_EVIL_CONSTRUCTORS(ANativeWindowWrapper);
107};
108
109struct 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
120protected:
121    virtual ~AMediaDrmWrapper();
122
123private:
124    AMediaDrm *mAMediaDrm;
125
126    DISALLOW_EVIL_CONSTRUCTORS(AMediaDrmWrapper);
127};
128
129struct 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
144protected:
145    virtual ~AMediaCryptoWrapper();
146
147private:
148    AMediaCrypto *mAMediaCrypto;
149
150    DISALLOW_EVIL_CONSTRUCTORS(AMediaCryptoWrapper);
151};
152
153struct 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
183protected:
184    virtual ~AMediaCodecCryptoInfoWrapper();
185
186private:
187    AMediaCodecCryptoInfo *mAMediaCodecCryptoInfo;
188
189    DISALLOW_EVIL_CONSTRUCTORS(AMediaCodecCryptoInfoWrapper);
190};
191
192struct 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
270protected:
271    virtual ~AMediaCodecWrapper();
272
273private:
274    AMediaCodec *mAMediaCodec;
275
276    sp<AMessage> mCallback;
277
278    DISALLOW_EVIL_CONSTRUCTORS(AMediaCodecWrapper);
279};
280
281struct AMediaExtractorWrapper : public RefBase {
282
283    AMediaExtractorWrapper(AMediaExtractor *aMediaExtractor);
284
285    // the returned AMediaExtractor is still owned by this wrapper.
286    AMediaExtractor *getAMediaExtractor() const;
287
288    status_t release();
289
290    status_t disconnect();
291
292    status_t setDataSource(int fd, off64_t offset, off64_t length);
293
294    status_t setDataSource(const char *location);
295
296    status_t setDataSource(AMediaDataSource *);
297
298    size_t getTrackCount();
299
300    sp<AMediaFormatWrapper> getFormat();
301
302    sp<AMediaFormatWrapper> getTrackFormat(size_t idx);
303
304    status_t selectTrack(size_t idx);
305
306    status_t unselectTrack(size_t idx);
307
308    status_t selectSingleTrack(size_t idx);
309
310    ssize_t readSampleData(const sp<ABuffer> &buffer);
311
312    ssize_t getSampleSize();
313
314    uint32_t getSampleFlags();
315
316    int getSampleTrackIndex();
317
318    int64_t getSampleTime();
319
320    status_t getSampleFormat(sp<AMediaFormatWrapper> &formatWrapper);
321
322    int64_t getCachedDuration();
323
324    bool advance();
325
326    status_t seekTo(int64_t seekPosUs, MediaSource::ReadOptions::SeekMode mode);
327
328    // the returned PsshInfo is still owned by this wrapper.
329    PsshInfo* getPsshInfo();
330
331    sp<AMediaCodecCryptoInfoWrapper> getSampleCryptoInfo();
332
333protected:
334    virtual ~AMediaExtractorWrapper();
335
336private:
337    AMediaExtractor *mAMediaExtractor;
338
339    DISALLOW_EVIL_CONSTRUCTORS(AMediaExtractorWrapper);
340};
341
342struct AMediaDataSourceWrapper : public RefBase {
343
344    static status_t translate_error(media_status_t err);
345
346    static ssize_t AMediaDataSourceWrapper_getSize(void *userdata);
347
348    static ssize_t AMediaDataSourceWrapper_readAt(void *userdata, off64_t offset, void * buf, size_t size);
349
350    static void AMediaDataSourceWrapper_close(void *userdata);
351
352    AMediaDataSourceWrapper(const sp<DataSource> &dataSource);
353
354    AMediaDataSource *getAMediaDataSource();
355
356protected:
357    virtual ~AMediaDataSourceWrapper();
358
359private:
360    sp<DataSource> mDataSource;
361
362    AMediaDataSource *mAMediaDataSource;
363
364    DISALLOW_EVIL_CONSTRUCTORS(AMediaDataSourceWrapper);
365};
366
367}  // namespace android
368
369#endif  // NDK_WRAPPER_H_
370