NdkMediaCodec.cpp revision 3425fd5a55dd31e261d2f2a9590c762d6d0a6b79
1/*
2 * Copyright (C) 2014 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#define LOG_NDEBUG 0
18#define LOG_TAG "NdkMediaCodec"
19
20#include "NdkMediaCodec.h"
21#include "NdkMediaError.h"
22#include "NdkMediaCryptoPriv.h"
23#include "NdkMediaFormatPriv.h"
24
25#include <utils/Log.h>
26#include <utils/StrongPointer.h>
27#include <gui/Surface.h>
28
29#include <media/stagefright/foundation/ALooper.h>
30#include <media/stagefright/foundation/AMessage.h>
31#include <media/stagefright/foundation/ABuffer.h>
32
33#include <media/stagefright/MediaCodec.h>
34#include <media/stagefright/MediaErrors.h>
35
36using namespace android;
37
38
39static int translate_error(status_t err) {
40    if (err == OK) {
41        return OK;
42    } else if (err == -EAGAIN) {
43        return AMEDIACODEC_INFO_TRY_AGAIN_LATER;
44    }
45    ALOGE("sf error code: %d", err);
46    return AMEDIAERROR_GENERIC;
47}
48
49enum {
50    kWhatActivityNotify,
51    kWhatRequestActivityNotifications,
52    kWhatStopActivityNotifications,
53};
54
55
56class CodecHandler: public AHandler {
57private:
58    AMediaCodec* mCodec;
59public:
60    CodecHandler(AMediaCodec *codec);
61    virtual void onMessageReceived(const sp<AMessage> &msg);
62};
63
64struct AMediaCodec {
65    sp<android::MediaCodec> mCodec;
66    sp<ALooper> mLooper;
67    sp<CodecHandler> mHandler;
68    sp<AMessage> mActivityNotification;
69    int32_t mGeneration;
70    bool mRequestedActivityNotification;
71    OnCodecEvent mCallback;
72    void *mCallbackUserData;
73};
74
75CodecHandler::CodecHandler(AMediaCodec *codec) {
76    mCodec = codec;
77}
78
79void CodecHandler::onMessageReceived(const sp<AMessage> &msg) {
80
81    switch (msg->what()) {
82        case kWhatRequestActivityNotifications:
83        {
84            if (mCodec->mRequestedActivityNotification) {
85                break;
86            }
87
88            mCodec->mCodec->requestActivityNotification(mCodec->mActivityNotification);
89            mCodec->mRequestedActivityNotification = true;
90            break;
91        }
92
93        case kWhatActivityNotify:
94        {
95            {
96                int32_t generation;
97                msg->findInt32("generation", &generation);
98
99                if (generation != mCodec->mGeneration) {
100                    // stale
101                    break;
102                }
103
104                mCodec->mRequestedActivityNotification = false;
105            }
106
107            if (mCodec->mCallback) {
108                mCodec->mCallback(mCodec, mCodec->mCallbackUserData);
109            }
110            break;
111        }
112
113        case kWhatStopActivityNotifications:
114        {
115            uint32_t replyID;
116            msg->senderAwaitsResponse(&replyID);
117
118            mCodec->mGeneration++;
119            mCodec->mRequestedActivityNotification = false;
120
121            sp<AMessage> response = new AMessage;
122            response->postReply(replyID);
123            break;
124        }
125
126        default:
127            ALOGE("shouldn't be here");
128            break;
129    }
130
131}
132
133
134static void requestActivityNotification(AMediaCodec *codec) {
135    (new AMessage(kWhatRequestActivityNotifications, codec->mHandler->id()))->post();
136}
137
138extern "C" {
139
140static AMediaCodec * createAMediaCodec(const char *name, bool name_is_type, bool encoder) {
141    AMediaCodec *mData = new AMediaCodec();
142    mData->mLooper = new ALooper;
143    mData->mLooper->setName("NDK MediaCodec_looper");
144    status_t ret = mData->mLooper->start(
145            false,      // runOnCallingThread
146            true,       // canCallJava XXX
147            PRIORITY_FOREGROUND);
148    if (name_is_type) {
149        mData->mCodec = android::MediaCodec::CreateByType(mData->mLooper, name, encoder);
150    } else {
151        mData->mCodec = android::MediaCodec::CreateByComponentName(mData->mLooper, name);
152    }
153    mData->mHandler = new CodecHandler(mData);
154    mData->mLooper->registerHandler(mData->mHandler);
155    mData->mGeneration = 1;
156    mData->mRequestedActivityNotification = false;
157    mData->mCallback = NULL;
158
159    return mData;
160}
161
162EXPORT
163AMediaCodec* AMediaCodec_createCodecByName(const char *name) {
164    return createAMediaCodec(name, false, false);
165}
166
167EXPORT
168AMediaCodec* AMediaCodec_createDecoderByType(const char *mime_type) {
169    return createAMediaCodec(mime_type, true, false);
170}
171
172EXPORT
173AMediaCodec* AMediaCodec_createEncoderByType(const char *name) {
174    return createAMediaCodec(name, true, true);
175}
176
177EXPORT
178int AMediaCodec_delete(AMediaCodec *mData) {
179    if (mData->mCodec != NULL) {
180        mData->mCodec->release();
181        mData->mCodec.clear();
182    }
183
184    if (mData->mLooper != NULL) {
185        mData->mLooper->unregisterHandler(mData->mHandler->id());
186        mData->mLooper->stop();
187        mData->mLooper.clear();
188    }
189    delete mData;
190    return OK;
191}
192
193EXPORT
194int AMediaCodec_configure(
195        AMediaCodec *mData,
196        const AMediaFormat* format,
197        ANativeWindow* window,
198        AMediaCrypto *crypto,
199        uint32_t flags) {
200    sp<AMessage> nativeFormat;
201    AMediaFormat_getFormat(format, &nativeFormat);
202    ALOGV("configure with format: %s", nativeFormat->debugString(0).c_str());
203    sp<Surface> surface = NULL;
204    if (window != NULL) {
205        surface = (Surface*) window;
206    }
207
208    return translate_error(mData->mCodec->configure(nativeFormat, surface,
209            crypto ? crypto->mCrypto : NULL, flags));
210}
211
212EXPORT
213int AMediaCodec_start(AMediaCodec *mData) {
214    status_t ret =  mData->mCodec->start();
215    if (ret != OK) {
216        return translate_error(ret);
217    }
218    mData->mActivityNotification = new AMessage(kWhatActivityNotify, mData->mHandler->id());
219    mData->mActivityNotification->setInt32("generation", mData->mGeneration);
220    requestActivityNotification(mData);
221    return OK;
222}
223
224EXPORT
225int AMediaCodec_stop(AMediaCodec *mData) {
226    int ret = translate_error(mData->mCodec->stop());
227
228    sp<AMessage> msg = new AMessage(kWhatStopActivityNotifications, mData->mHandler->id());
229    sp<AMessage> response;
230    msg->postAndAwaitResponse(&response);
231    mData->mActivityNotification.clear();
232
233    return ret;
234}
235
236EXPORT
237int AMediaCodec_flush(AMediaCodec *mData) {
238    return translate_error(mData->mCodec->flush());
239}
240
241EXPORT
242ssize_t AMediaCodec_dequeueInputBuffer(AMediaCodec *mData, int64_t timeoutUs) {
243    size_t idx;
244    status_t ret = mData->mCodec->dequeueInputBuffer(&idx, timeoutUs);
245    requestActivityNotification(mData);
246    if (ret == OK) {
247        return idx;
248    }
249    return translate_error(ret);
250}
251
252EXPORT
253uint8_t* AMediaCodec_getInputBuffer(AMediaCodec *mData, size_t idx, size_t *out_size) {
254    android::Vector<android::sp<android::ABuffer> > abufs;
255    if (mData->mCodec->getInputBuffers(&abufs) == 0) {
256        size_t n = abufs.size();
257        if (idx >= n) {
258            ALOGE("buffer index %d out of range", idx);
259            return NULL;
260        }
261        if (out_size != NULL) {
262            *out_size = abufs[idx]->capacity();
263        }
264        return abufs[idx]->data();
265    }
266    ALOGE("couldn't get input buffers");
267    return NULL;
268}
269
270EXPORT
271uint8_t* AMediaCodec_getOutputBuffer(AMediaCodec *mData, size_t idx, size_t *out_size) {
272    android::Vector<android::sp<android::ABuffer> > abufs;
273    if (mData->mCodec->getOutputBuffers(&abufs) == 0) {
274        size_t n = abufs.size();
275        if (idx >= n) {
276            ALOGE("buffer index %d out of range", idx);
277            return NULL;
278        }
279        if (out_size != NULL) {
280            *out_size = abufs[idx]->capacity();
281        }
282        return abufs[idx]->data();
283    }
284    ALOGE("couldn't get output buffers");
285    return NULL;
286}
287
288EXPORT
289int AMediaCodec_queueInputBuffer(AMediaCodec *mData,
290        size_t idx, off_t offset, size_t size, uint64_t time, uint32_t flags) {
291
292    AString errorMsg;
293    status_t ret = mData->mCodec->queueInputBuffer(idx, offset, size, time, flags, &errorMsg);
294    return translate_error(ret);
295}
296
297EXPORT
298ssize_t AMediaCodec_dequeueOutputBuffer(AMediaCodec *mData,
299        AMediaCodecBufferInfo *info, int64_t timeoutUs) {
300    size_t idx;
301    size_t offset;
302    size_t size;
303    uint32_t flags;
304    int64_t presentationTimeUs;
305    status_t ret = mData->mCodec->dequeueOutputBuffer(&idx, &offset, &size, &presentationTimeUs,
306            &flags, timeoutUs);
307    requestActivityNotification(mData);
308    switch (ret) {
309        case OK:
310            info->offset = offset;
311            info->size = size;
312            info->flags = flags;
313            info->presentationTimeUs = presentationTimeUs;
314            return idx;
315        case -EAGAIN:
316            return AMEDIACODEC_INFO_TRY_AGAIN_LATER;
317        case android::INFO_FORMAT_CHANGED:
318            return AMEDIACODEC_INFO_OUTPUT_FORMAT_CHANGED;
319        case INFO_OUTPUT_BUFFERS_CHANGED:
320            return AMEDIACODEC_INFO_OUTPUT_BUFFERS_CHANGED;
321        default:
322            break;
323    }
324    return translate_error(ret);
325}
326
327EXPORT
328AMediaFormat* AMediaCodec_getOutputFormat(AMediaCodec *mData) {
329    sp<AMessage> format;
330    mData->mCodec->getOutputFormat(&format);
331    return AMediaFormat_fromMsg(&format);
332}
333
334EXPORT
335int AMediaCodec_releaseOutputBuffer(AMediaCodec *mData, size_t idx, bool render) {
336    if (render) {
337        return translate_error(mData->mCodec->renderOutputBufferAndRelease(idx));
338    } else {
339        return translate_error(mData->mCodec->releaseOutputBuffer(idx));
340    }
341}
342
343EXPORT
344int AMediaCodec_setNotificationCallback(AMediaCodec *mData, OnCodecEvent callback, void *userdata) {
345    mData->mCallback = callback;
346    mData->mCallbackUserData = userdata;
347    return OK;
348}
349
350typedef struct AMediaCodecCryptoInfo {
351        int numsubsamples;
352        uint8_t key[16];
353        uint8_t iv[16];
354        uint32_t mode;
355        size_t *clearbytes;
356        size_t *encryptedbytes;
357} AMediaCodecCryptoInfo;
358
359EXPORT
360int AMediaCodec_queueSecureInputBuffer(
361        AMediaCodec* codec,
362        size_t idx,
363        off_t offset,
364        AMediaCodecCryptoInfo* crypto,
365        uint64_t time,
366        uint32_t flags) {
367
368    CryptoPlugin::SubSample *subSamples = new CryptoPlugin::SubSample[crypto->numsubsamples];
369    for (int i = 0; i < crypto->numsubsamples; i++) {
370        subSamples[i].mNumBytesOfClearData = crypto->clearbytes[i];
371        subSamples[i].mNumBytesOfEncryptedData = crypto->encryptedbytes[i];
372    }
373
374    AString errormsg;
375    status_t err  = codec->mCodec->queueSecureInputBuffer(idx,
376            offset,
377            subSamples,
378            crypto->numsubsamples,
379            crypto->key,
380            crypto->iv,
381            (CryptoPlugin::Mode) crypto->mode,
382            time,
383            flags,
384            &errormsg);
385    if (err != 0) {
386        ALOGE("queSecureInputBuffer: %s", errormsg.c_str());
387    }
388    delete [] subSamples;
389    return translate_error(err);
390}
391
392
393
394EXPORT
395AMediaCodecCryptoInfo *AMediaCodecCryptoInfo_new(
396        int numsubsamples,
397        uint8_t key[16],
398        uint8_t iv[16],
399        uint32_t mode,
400        size_t *clearbytes,
401        size_t *encryptedbytes) {
402
403    // size needed to store all the crypto data
404    size_t cryptosize = sizeof(AMediaCodecCryptoInfo) + sizeof(size_t) * numsubsamples * 2;
405    AMediaCodecCryptoInfo *ret = (AMediaCodecCryptoInfo*) malloc(cryptosize);
406    if (!ret) {
407        ALOGE("couldn't allocate %d bytes", cryptosize);
408        return NULL;
409    }
410    ret->numsubsamples = numsubsamples;
411    memcpy(ret->key, key, 16);
412    memcpy(ret->iv, iv, 16);
413    ret->mode = mode;
414
415    // clearbytes and encryptedbytes point at the actual data, which follows
416    ret->clearbytes = (size_t*) (ret + 1); // point immediately after the struct
417    ret->encryptedbytes = ret->clearbytes + numsubsamples; // point after the clear sizes
418
419    memcpy(ret->clearbytes, clearbytes, numsubsamples * sizeof(size_t));
420    memcpy(ret->encryptedbytes, encryptedbytes, numsubsamples * sizeof(size_t));
421
422    return ret;
423}
424
425
426EXPORT
427int AMediaCodecCryptoInfo_delete(AMediaCodecCryptoInfo* info) {
428    free(info);
429    return OK;
430}
431
432EXPORT
433size_t AMediaCodecCryptoInfo_getNumSubSamples(AMediaCodecCryptoInfo* ci) {
434    return ci->numsubsamples;
435}
436
437EXPORT
438int AMediaCodecCryptoInfo_getKey(AMediaCodecCryptoInfo* ci, uint8_t *dst) {
439    if (!dst || !ci) {
440        return AMEDIAERROR_UNSUPPORTED;
441    }
442    memcpy(dst, ci->key, 16);
443    return OK;
444}
445
446EXPORT
447int AMediaCodecCryptoInfo_getIV(AMediaCodecCryptoInfo* ci, uint8_t *dst) {
448    if (!dst || !ci) {
449        return AMEDIAERROR_UNSUPPORTED;
450    }
451    memcpy(dst, ci->iv, 16);
452    return OK;
453}
454
455EXPORT
456uint32_t AMediaCodecCryptoInfo_getMode(AMediaCodecCryptoInfo* ci) {
457    if (!ci) {
458        return AMEDIAERROR_UNSUPPORTED;
459    }
460    return ci->mode;
461}
462
463EXPORT
464int AMediaCodecCryptoInfo_getClearBytes(AMediaCodecCryptoInfo* ci, size_t *dst) {
465    if (!dst || !ci) {
466        return AMEDIAERROR_UNSUPPORTED;
467    }
468    memcpy(dst, ci->clearbytes, sizeof(size_t) * ci->numsubsamples);
469    return OK;
470}
471
472EXPORT
473int AMediaCodecCryptoInfo_getEncryptedBytes(AMediaCodecCryptoInfo* ci, size_t *dst) {
474    if (!dst || !ci) {
475        return AMEDIAERROR_UNSUPPORTED;
476    }
477    memcpy(dst, ci->encryptedbytes, sizeof(size_t) * ci->numsubsamples);
478    return OK;
479}
480
481} // extern "C"
482
483