NuPlayerDecoderPassThrough.cpp revision c8f7c350bec6534b4c5a9cc66fbb049a159311c3
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 "NuPlayerDecoderPassThrough"
19#include <utils/Log.h>
20#include <inttypes.h>
21
22#include "NuPlayerDecoderPassThrough.h"
23
24#include "NuPlayerRenderer.h"
25#include "NuPlayerSource.h"
26
27#include <media/ICrypto.h>
28#include <media/MediaCodecBuffer.h>
29#include <media/stagefright/foundation/ABuffer.h>
30#include <media/stagefright/foundation/ADebug.h>
31#include <media/stagefright/foundation/AMessage.h>
32#include <media/stagefright/MediaErrors.h>
33
34#include "ATSParser.h"
35
36namespace android {
37
38// TODO optimize buffer size for power consumption
39// The offload read buffer size is 32 KB but 24 KB uses less power.
40static const size_t kAggregateBufferSizeBytes = 24 * 1024;
41static const size_t kMaxCachedBytes = 200000;
42
43NuPlayer::DecoderPassThrough::DecoderPassThrough(
44        const sp<AMessage> &notify,
45        const sp<Source> &source,
46        const sp<Renderer> &renderer)
47    : DecoderBase(notify),
48      mSource(source),
49      mRenderer(renderer),
50      mSkipRenderingUntilMediaTimeUs(-1ll),
51      mReachedEOS(true),
52      mPendingAudioErr(OK),
53      mPendingBuffersToDrain(0),
54      mCachedBytes(0),
55      mComponentName("pass through decoder") {
56    ALOGW_IF(renderer == NULL, "expect a non-NULL renderer");
57}
58
59NuPlayer::DecoderPassThrough::~DecoderPassThrough() {
60}
61
62void NuPlayer::DecoderPassThrough::onConfigure(const sp<AMessage> &format) {
63    ALOGV("[%s] onConfigure", mComponentName.c_str());
64    mCachedBytes = 0;
65    mPendingBuffersToDrain = 0;
66    mReachedEOS = false;
67    ++mBufferGeneration;
68
69    onRequestInputBuffers();
70
71    int32_t hasVideo = 0;
72    format->findInt32("has-video", &hasVideo);
73
74    // The audio sink is already opened before the PassThrough decoder is created.
75    // Opening again might be relevant if decoder is instantiated after shutdown and
76    // format is different.
77    status_t err = mRenderer->openAudioSink(
78            format, true /* offloadOnly */, hasVideo,
79            AUDIO_OUTPUT_FLAG_NONE /* flags */, NULL /* isOffloaded */);
80    if (err != OK) {
81        handleError(err);
82    }
83}
84
85void NuPlayer::DecoderPassThrough::onSetParameters(const sp<AMessage> &/*params*/) {
86    ALOGW("onSetParameters() called unexpectedly");
87}
88
89void NuPlayer::DecoderPassThrough::onSetRenderer(
90        const sp<Renderer> &renderer) {
91    // renderer can't be changed during offloading
92    ALOGW_IF(renderer != mRenderer,
93            "ignoring request to change renderer");
94}
95
96void NuPlayer::DecoderPassThrough::onGetInputBuffers(
97        Vector<sp<MediaCodecBuffer> > * /* dstBuffers */) {
98    ALOGE("onGetInputBuffers() called unexpectedly");
99}
100
101bool NuPlayer::DecoderPassThrough::isStaleReply(const sp<AMessage> &msg) {
102    int32_t generation;
103    CHECK(msg->findInt32("generation", &generation));
104    return generation != mBufferGeneration;
105}
106
107bool NuPlayer::DecoderPassThrough::isDoneFetching() const {
108    ALOGV("[%s] mCachedBytes = %zu, mReachedEOS = %d mPaused = %d",
109            mComponentName.c_str(), mCachedBytes, mReachedEOS, mPaused);
110
111    return mCachedBytes >= kMaxCachedBytes || mReachedEOS || mPaused;
112}
113
114/*
115 * returns true if we should request more data
116 */
117bool NuPlayer::DecoderPassThrough::doRequestBuffers() {
118    status_t err = OK;
119    while (!isDoneFetching()) {
120        sp<AMessage> msg = new AMessage();
121
122        err = fetchInputData(msg);
123        if (err != OK) {
124            break;
125        }
126
127        onInputBufferFetched(msg);
128    }
129
130    return err == -EWOULDBLOCK
131            && mSource->feedMoreTSData() == OK;
132}
133
134status_t NuPlayer::DecoderPassThrough::dequeueAccessUnit(sp<ABuffer> *accessUnit) {
135    status_t err;
136
137    // Did we save an accessUnit earlier because of a discontinuity?
138    if (mPendingAudioAccessUnit != NULL) {
139        *accessUnit = mPendingAudioAccessUnit;
140        mPendingAudioAccessUnit.clear();
141        err = mPendingAudioErr;
142        ALOGV("feedDecoderInputData() use mPendingAudioAccessUnit");
143    } else {
144        err = mSource->dequeueAccessUnit(true /* audio */, accessUnit);
145    }
146
147    if (err == INFO_DISCONTINUITY || err == ERROR_END_OF_STREAM) {
148        if (mAggregateBuffer != NULL) {
149            // We already have some data so save this for later.
150            mPendingAudioErr = err;
151            mPendingAudioAccessUnit = *accessUnit;
152            (*accessUnit).clear();
153            ALOGD("return aggregated buffer and save err(=%d) for later", err);
154            err = OK;
155        }
156    }
157
158    return err;
159}
160
161sp<ABuffer> NuPlayer::DecoderPassThrough::aggregateBuffer(
162        const sp<ABuffer> &accessUnit) {
163    sp<ABuffer> aggregate;
164
165    if (accessUnit == NULL) {
166        // accessUnit is saved to mPendingAudioAccessUnit
167        // return current mAggregateBuffer
168        aggregate = mAggregateBuffer;
169        mAggregateBuffer.clear();
170        return aggregate;
171    }
172
173    size_t smallSize = accessUnit->size();
174    if ((mAggregateBuffer == NULL)
175            // Don't bother if only room for a few small buffers.
176            && (smallSize < (kAggregateBufferSizeBytes / 3))) {
177        // Create a larger buffer for combining smaller buffers from the extractor.
178        mAggregateBuffer = new ABuffer(kAggregateBufferSizeBytes);
179        mAggregateBuffer->setRange(0, 0); // start empty
180    }
181
182    if (mAggregateBuffer != NULL) {
183        int64_t timeUs;
184        int64_t dummy;
185        bool smallTimestampValid = accessUnit->meta()->findInt64("timeUs", &timeUs);
186        bool bigTimestampValid = mAggregateBuffer->meta()->findInt64("timeUs", &dummy);
187        // Will the smaller buffer fit?
188        size_t bigSize = mAggregateBuffer->size();
189        size_t roomLeft = mAggregateBuffer->capacity() - bigSize;
190        // Should we save this small buffer for the next big buffer?
191        // If the first small buffer did not have a timestamp then save
192        // any buffer that does have a timestamp until the next big buffer.
193        if ((smallSize > roomLeft)
194            || (!bigTimestampValid && (bigSize > 0) && smallTimestampValid)) {
195            mPendingAudioErr = OK;
196            mPendingAudioAccessUnit = accessUnit;
197            aggregate = mAggregateBuffer;
198            mAggregateBuffer.clear();
199        } else {
200            // Grab time from first small buffer if available.
201            if ((bigSize == 0) && smallTimestampValid) {
202                mAggregateBuffer->meta()->setInt64("timeUs", timeUs);
203            }
204            // Append small buffer to the bigger buffer.
205            memcpy(mAggregateBuffer->base() + bigSize, accessUnit->data(), smallSize);
206            bigSize += smallSize;
207            mAggregateBuffer->setRange(0, bigSize);
208
209            ALOGV("feedDecoderInputData() smallSize = %zu, bigSize = %zu, capacity = %zu",
210                    smallSize, bigSize, mAggregateBuffer->capacity());
211        }
212    } else {
213        // decided not to aggregate
214        aggregate = accessUnit;
215    }
216
217    return aggregate;
218}
219
220status_t NuPlayer::DecoderPassThrough::fetchInputData(sp<AMessage> &reply) {
221    sp<ABuffer> accessUnit;
222
223    do {
224        status_t err = dequeueAccessUnit(&accessUnit);
225
226        if (err == -EWOULDBLOCK) {
227            // Flush out the aggregate buffer to try to avoid underrun.
228            accessUnit = aggregateBuffer(NULL /* accessUnit */);
229            if (accessUnit != NULL) {
230                break;
231            }
232            return err;
233        } else if (err != OK) {
234            if (err == INFO_DISCONTINUITY) {
235                int32_t type;
236                CHECK(accessUnit->meta()->findInt32("discontinuity", &type));
237
238                bool formatChange =
239                        (type & ATSParser::DISCONTINUITY_AUDIO_FORMAT) != 0;
240
241                bool timeChange =
242                        (type & ATSParser::DISCONTINUITY_TIME) != 0;
243
244                ALOGI("audio discontinuity (formatChange=%d, time=%d)",
245                        formatChange, timeChange);
246
247                if (formatChange || timeChange) {
248                    sp<AMessage> msg = mNotify->dup();
249                    msg->setInt32("what", kWhatInputDiscontinuity);
250                    // will perform seamless format change,
251                    // only notify NuPlayer to scan sources
252                    msg->setInt32("formatChange", false);
253                    msg->post();
254                }
255
256                if (timeChange) {
257                    doFlush(false /* notifyComplete */);
258                    err = OK;
259                } else if (formatChange) {
260                    // do seamless format change
261                    err = OK;
262                } else {
263                    // This stream is unaffected by the discontinuity
264                    return -EWOULDBLOCK;
265                }
266            }
267
268            reply->setInt32("err", err);
269            return OK;
270        }
271
272        accessUnit = aggregateBuffer(accessUnit);
273    } while (accessUnit == NULL);
274
275#if 0
276    int64_t mediaTimeUs;
277    CHECK(accessUnit->meta()->findInt64("timeUs", &mediaTimeUs));
278    ALOGV("feeding audio input buffer at media time %.2f secs",
279         mediaTimeUs / 1E6);
280#endif
281
282    reply->setBuffer("buffer", accessUnit);
283
284    return OK;
285}
286
287void NuPlayer::DecoderPassThrough::onInputBufferFetched(
288        const sp<AMessage> &msg) {
289    if (mReachedEOS) {
290        return;
291    }
292
293    sp<ABuffer> buffer;
294    bool hasBuffer = msg->findBuffer("buffer", &buffer);
295    if (buffer == NULL) {
296        int32_t streamErr = ERROR_END_OF_STREAM;
297        CHECK(msg->findInt32("err", &streamErr) || !hasBuffer);
298        if (streamErr == OK) {
299            return;
300        }
301
302        mReachedEOS = true;
303        if (mRenderer != NULL) {
304            mRenderer->queueEOS(true /* audio */, ERROR_END_OF_STREAM);
305        }
306        return;
307    }
308
309    sp<AMessage> extra;
310    if (buffer->meta()->findMessage("extra", &extra) && extra != NULL) {
311        int64_t resumeAtMediaTimeUs;
312        if (extra->findInt64(
313                    "resume-at-mediatimeUs", &resumeAtMediaTimeUs)) {
314            ALOGI("[%s] suppressing rendering until %lld us",
315                    mComponentName.c_str(), (long long)resumeAtMediaTimeUs);
316            mSkipRenderingUntilMediaTimeUs = resumeAtMediaTimeUs;
317        }
318    }
319
320    int32_t bufferSize = buffer->size();
321    mCachedBytes += bufferSize;
322
323    int64_t timeUs = 0;
324    CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
325    if (mSkipRenderingUntilMediaTimeUs >= 0) {
326        if (timeUs < mSkipRenderingUntilMediaTimeUs) {
327            ALOGV("[%s] dropping buffer at time %lld as requested.",
328                     mComponentName.c_str(), (long long)timeUs);
329
330            onBufferConsumed(bufferSize);
331            return;
332        }
333
334        mSkipRenderingUntilMediaTimeUs = -1;
335    }
336
337    if (mRenderer == NULL) {
338        onBufferConsumed(bufferSize);
339        return;
340    }
341
342    sp<AMessage> reply = new AMessage(kWhatBufferConsumed, this);
343    reply->setInt32("generation", mBufferGeneration);
344    reply->setInt32("size", bufferSize);
345
346    sp<MediaCodecBuffer> mcBuffer = new MediaCodecBuffer(nullptr, buffer);
347    mcBuffer->meta()->setInt64("timeUs", timeUs);
348
349    mRenderer->queueBuffer(true /* audio */, mcBuffer, reply);
350
351    ++mPendingBuffersToDrain;
352    ALOGV("onInputBufferFilled: #ToDrain = %zu, cachedBytes = %zu",
353            mPendingBuffersToDrain, mCachedBytes);
354}
355
356void NuPlayer::DecoderPassThrough::onBufferConsumed(int32_t size) {
357    --mPendingBuffersToDrain;
358    mCachedBytes -= size;
359    ALOGV("onBufferConsumed: #ToDrain = %zu, cachedBytes = %zu",
360            mPendingBuffersToDrain, mCachedBytes);
361    onRequestInputBuffers();
362}
363
364void NuPlayer::DecoderPassThrough::onResume(bool notifyComplete) {
365    mPaused = false;
366
367    onRequestInputBuffers();
368
369    if (notifyComplete) {
370        sp<AMessage> notify = mNotify->dup();
371        notify->setInt32("what", kWhatResumeCompleted);
372        notify->post();
373    }
374}
375
376void NuPlayer::DecoderPassThrough::doFlush(bool notifyComplete) {
377    ++mBufferGeneration;
378    mSkipRenderingUntilMediaTimeUs = -1;
379    mPendingAudioAccessUnit.clear();
380    mPendingAudioErr = OK;
381    mAggregateBuffer.clear();
382
383    if (mRenderer != NULL) {
384        mRenderer->flush(true /* audio */, notifyComplete);
385        mRenderer->signalTimeDiscontinuity();
386    }
387
388    mPendingBuffersToDrain = 0;
389    mCachedBytes = 0;
390    mReachedEOS = false;
391}
392
393void NuPlayer::DecoderPassThrough::onFlush() {
394    doFlush(true /* notifyComplete */);
395
396    mPaused = true;
397    sp<AMessage> notify = mNotify->dup();
398    notify->setInt32("what", kWhatFlushCompleted);
399    notify->post();
400
401}
402
403void NuPlayer::DecoderPassThrough::onShutdown(bool notifyComplete) {
404    ++mBufferGeneration;
405    mSkipRenderingUntilMediaTimeUs = -1;
406
407    if (notifyComplete) {
408        sp<AMessage> notify = mNotify->dup();
409        notify->setInt32("what", kWhatShutdownCompleted);
410        notify->post();
411    }
412
413    mReachedEOS = true;
414}
415
416void NuPlayer::DecoderPassThrough::onMessageReceived(const sp<AMessage> &msg) {
417    ALOGV("[%s] onMessage: %s", mComponentName.c_str(),
418            msg->debugString().c_str());
419
420    switch (msg->what()) {
421        case kWhatBufferConsumed:
422        {
423            if (!isStaleReply(msg)) {
424                int32_t size;
425                CHECK(msg->findInt32("size", &size));
426                onBufferConsumed(size);
427            }
428            break;
429        }
430
431        default:
432            DecoderBase::onMessageReceived(msg);
433            break;
434    }
435}
436
437}  // namespace android
438