android_AudioSfDecoder.cpp revision e9236d046fdb5cac0696c42e03443a2439188146
1/*
2 * Copyright (C) 2011 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 USE_LOG SLAndroidLogLevel_Verbose
18
19#include "sles_allinclusive.h"
20
21namespace android {
22
23//--------------------------------------------------------------------------------------------------
24AudioSfDecoder::AudioSfDecoder(const AudioPlayback_Parameters* params) : GenericPlayer(params),
25        mBitrate(-1),
26        mNumChannels(1),
27        mSampleRateHz(0),
28        mDurationUsec(-1),
29        mDecodeBuffer(NULL),
30        mCacheStatus(kStatusEmpty),
31        mCacheFill(0),
32        mLastNotifiedCacheFill(0),
33        mCacheFillNotifThreshold(100),
34        mTimeDelta(-1),
35        mSeekTimeMsec(0),
36        mLastDecodedPositionUs(-1)
37{
38    SL_LOGV("AudioSfDecoder::AudioSfDecoder()");
39
40}
41
42
43AudioSfDecoder::~AudioSfDecoder() {
44    SL_LOGV("AudioSfDecoder::~AudioSfDecoder()");
45
46}
47
48
49//--------------------------------------------------
50void AudioSfDecoder::play() {
51    SL_LOGV("AudioSfDecoder::play");
52
53    GenericPlayer::play();
54    (new AMessage(kWhatDecode, id()))->post();
55}
56
57
58void AudioSfDecoder::startPrefetch_async() {
59    SL_LOGV("AudioSfDecoder::startPrefetch_async()");
60
61    if (wantPrefetch()) {
62        SL_LOGV("AudioSfDecoder::startPrefetch_async(): sending check cache msg");
63
64        mStateFlags |= kFlagPreparing | kFlagBuffering;
65
66        (new AMessage(kWhatCheckCache, id()))->post();
67    }
68}
69
70
71//--------------------------------------------------
72// Event handlers
73void AudioSfDecoder::onPrepare() {
74    SL_LOGD("AudioSfDecoder::onPrepare()");
75
76    sp<DataSource> dataSource;
77
78    switch (mDataLocatorType) {
79
80    case kDataLocatorNone:
81        SL_LOGE("AudioSfDecoder::onPrepare: no data locator set");
82        notifyPrepared(MEDIA_ERROR_BASE);
83        return;
84
85    case kDataLocatorUri:
86        dataSource = DataSource::CreateFromURI(mDataLocator.uriRef);
87        if (dataSource == NULL) {
88            SL_LOGE("AudioSfDecoder::onPrepare(): Error opening %s", mDataLocator.uriRef);
89            notifyPrepared(MEDIA_ERROR_BASE);
90            return;
91        }
92        break;
93
94    case kDataLocatorFd:
95    {
96        dataSource = new FileSource(
97                mDataLocator.fdi.fd, mDataLocator.fdi.offset, mDataLocator.fdi.length);
98        status_t err = dataSource->initCheck();
99        if (err != OK) {
100            notifyPrepared(err);
101            return;
102        }
103        break;
104    }
105
106    default:
107        TRESPASS();
108    }
109
110    sp<MediaExtractor> extractor = MediaExtractor::Create(dataSource);
111    if (extractor == NULL) {
112        SL_LOGE("AudioSfDecoder::onPrepare: Could not instantiate extractor.");
113        notifyPrepared(ERROR_UNSUPPORTED);
114        return;
115    }
116
117    ssize_t audioTrackIndex = -1;
118    bool isRawAudio = false;
119    for (size_t i = 0; i < extractor->countTracks(); ++i) {
120        sp<MetaData> meta = extractor->getTrackMetaData(i);
121
122        const char *mime;
123        CHECK(meta->findCString(kKeyMIMEType, &mime));
124
125        if (!strncasecmp("audio/", mime, 6)) {
126            audioTrackIndex = i;
127
128            if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_RAW, mime)) {
129                isRawAudio = true;
130            }
131            break;
132        }
133    }
134
135    if (audioTrackIndex < 0) {
136        SL_LOGE("AudioSfDecoder::onPrepare: Could not find a supported audio track.");
137        notifyPrepared(ERROR_UNSUPPORTED);
138        return;
139    }
140
141    sp<MediaSource> source = extractor->getTrack(audioTrackIndex);
142    sp<MetaData> meta = source->getFormat();
143
144    off64_t size;
145    int64_t durationUs;
146    if (dataSource->getSize(&size) == OK
147            && meta->findInt64(kKeyDuration, &durationUs)) {
148        mBitrate = size * 8000000ll / durationUs;  // in bits/sec
149        mDurationUsec = durationUs;
150    } else {
151        mBitrate = -1;
152        mDurationUsec = -1;
153    }
154
155    if (!isRawAudio) {
156        OMXClient client;
157        CHECK_EQ(client.connect(), (status_t)OK);
158
159        source = OMXCodec::Create(
160                client.interface(), meta, false /* createEncoder */,
161                source);
162
163        if (source == NULL) {
164            SL_LOGE("AudioSfDecoder::onPrepare: Could not instantiate decoder.");
165            notifyPrepared(ERROR_UNSUPPORTED);
166            return;
167        }
168
169        meta = source->getFormat();
170    }
171
172
173    if (source->start() != OK) {
174        SL_LOGE("AudioSfDecoder::onPrepare: Failed to start source/decoder.");
175        notifyPrepared(MEDIA_ERROR_BASE);
176        return;
177    }
178
179    mDataSource = dataSource;
180    mAudioSource = source;
181
182    CHECK(meta->findInt32(kKeyChannelCount, &mNumChannels));
183    CHECK(meta->findInt32(kKeySampleRate, &mSampleRateHz));
184
185    if (!wantPrefetch()) {
186        SL_LOGV("AudioSfDecoder::onPrepare: no need to prefetch");
187        // doesn't need prefetching, notify good to go
188        mCacheStatus = kStatusHigh;
189        mCacheFill = 1000;
190        notifyStatus();
191        notifyCacheFill();
192    }
193
194    // at this point we have enough information about the source to create the sink that
195    // will consume the data
196    createAudioSink();
197
198    GenericPlayer::onPrepare();
199    SL_LOGD("AudioSfDecoder::onPrepare() done, mStateFlags=0x%x", mStateFlags);
200}
201
202
203void AudioSfDecoder::onPause() {
204    SL_LOGD("AudioSfDecoder::onPause()");
205    GenericPlayer::onPause();
206    pauseAudioSink();
207}
208
209
210void AudioSfDecoder::onPlay() {
211    SL_LOGD("AudioSfDecoder::onPlay()");
212    GenericPlayer::onPlay();
213    startAudioSink();
214}
215
216
217void AudioSfDecoder::onSeek(const sp<AMessage> &msg) {
218    SL_LOGV("AudioSfDecoder::onSeek");
219    int64_t timeMsec;
220    CHECK(msg->findInt64(WHATPARAM_SEEK_SEEKTIME_MS, &timeMsec));
221
222    Mutex::Autolock _l(mSeekLock);
223    mStateFlags |= kFlagSeeking;
224    mSeekTimeMsec = timeMsec;
225    mTimeDelta = -1;
226    mLastDecodedPositionUs = -1;
227}
228
229
230void AudioSfDecoder::onLoop(const sp<AMessage> &msg) {
231    SL_LOGV("AudioSfDecoder::onLoop");
232    int32_t loop;
233    CHECK(msg->findInt32(WHATPARAM_LOOP_LOOPING, &loop));
234
235    if (loop) {
236        //SL_LOGV("AudioSfDecoder::onLoop start looping");
237        mStateFlags |= kFlagLooping;
238    } else {
239        //SL_LOGV("AudioSfDecoder::onLoop stop looping");
240        mStateFlags &= ~kFlagLooping;
241    }
242}
243
244
245void AudioSfDecoder::onCheckCache(const sp<AMessage> &msg) {
246    //SL_LOGV("AudioSfDecoder::onCheckCache");
247    bool eos;
248    CacheStatus status = getCacheRemaining(&eos);
249
250    if (eos || status == kStatusHigh
251            || ((mStateFlags & kFlagPreparing) && (status >= kStatusEnough))) {
252        if (mStateFlags & kFlagPlaying) {
253            startAudioSink();
254        }
255        mStateFlags &= ~kFlagBuffering;
256
257        SL_LOGV("AudioSfDecoder::onCheckCache: buffering done.");
258
259        if (mStateFlags & kFlagPreparing) {
260            //SL_LOGV("AudioSfDecoder::onCheckCache: preparation done.");
261            mStateFlags &= ~kFlagPreparing;
262        }
263
264        mTimeDelta = -1;
265        if (mStateFlags & kFlagPlaying) {
266            (new AMessage(kWhatDecode, id()))->post();
267        }
268        return;
269    }
270
271    msg->post(100000);
272}
273
274
275void AudioSfDecoder::onDecode() {
276    SL_LOGV("AudioSfDecoder::onDecode");
277
278    //-------------------------------- Need to buffer some more before decoding?
279    bool eos;
280    if (mDataSource == 0) {
281        // application set play state to paused which failed, then set play state to playing
282        return;
283    }
284    if (wantPrefetch()
285            && (getCacheRemaining(&eos) == kStatusLow)
286            && !eos) {
287        SL_LOGV("buffering more.");
288
289        if (mStateFlags & kFlagPlaying) {
290            pauseAudioSink();
291        }
292        mStateFlags |= kFlagBuffering;
293        (new AMessage(kWhatCheckCache, id()))->post(100000);
294        return;
295    }
296
297    if (!(mStateFlags & (kFlagPlaying | kFlagBuffering | kFlagPreparing))) {
298        // don't decode if we're not buffering, prefetching or playing
299        //SL_LOGV("don't decode: not buffering, prefetching or playing");
300        return;
301    }
302
303    //-------------------------------- Decode
304    status_t err;
305    MediaSource::ReadOptions readOptions;
306    if (mStateFlags & kFlagSeeking) {
307        readOptions.setSeekTo(mSeekTimeMsec * 1000);
308    }
309
310    {
311        Mutex::Autolock _l(mDecodeBufferLock);
312        if (NULL != mDecodeBuffer) {
313            // the current decoded buffer hasn't been rendered, drop it
314            mDecodeBuffer->release();
315            mDecodeBuffer = NULL;
316        }
317        err = mAudioSource->read(&mDecodeBuffer, &readOptions);
318        if (err == OK) {
319            CHECK(mDecodeBuffer->meta_data()->findInt64(kKeyTime, &mLastDecodedPositionUs));
320        }
321    }
322
323    {
324        Mutex::Autolock _l(mSeekLock);
325        if (mStateFlags & kFlagSeeking) {
326            mStateFlags &= ~kFlagSeeking;
327        }
328    }
329
330    //-------------------------------- Handle return of decode
331    if (err != OK) {
332        bool continueDecoding = false;
333        switch(err) {
334            case ERROR_END_OF_STREAM:
335                if (0 < mDurationUsec) {
336                    mLastDecodedPositionUs = mDurationUsec;
337                }
338                // handle notification and looping at end of stream
339                if (mStateFlags & kFlagPlaying) {
340                    notify(PLAYEREVENT_ENDOFSTREAM, 1, true);
341                }
342                if (mStateFlags & kFlagLooping) {
343                    seek(0);
344                    // kick-off decoding again
345                    continueDecoding = true;
346                }
347                break;
348            case INFO_FORMAT_CHANGED:
349                SL_LOGD("MediaSource::read encountered INFO_FORMAT_CHANGED");
350                // reconfigure output
351                updateAudioSink();
352                continueDecoding = true;
353                break;
354            case INFO_DISCONTINUITY:
355                SL_LOGD("MediaSource::read encountered INFO_DISCONTINUITY");
356                continueDecoding = true;
357                break;
358            default:
359                SL_LOGE("MediaSource::read returned error %d", err);
360                break;
361        }
362        if (continueDecoding) {
363            if (NULL == mDecodeBuffer) {
364                (new AMessage(kWhatDecode, id()))->post();
365                return;
366            }
367        } else {
368            return;
369        }
370    }
371
372    //-------------------------------- Render
373    sp<AMessage> msg = new AMessage(kWhatRender, id());
374    msg->post();
375}
376
377
378void AudioSfDecoder::onRender() {
379    //SL_LOGV("AudioSfDecoder::onRender");
380
381    Mutex::Autolock _l(mDecodeBufferLock);
382
383    if (NULL == mDecodeBuffer) {
384        // nothing to render, move along
385        SL_LOGV("AudioSfDecoder::onRender NULL buffer, exiting");
386        return;
387    }
388
389    mDecodeBuffer->release();
390    mDecodeBuffer = NULL;
391
392}
393
394
395void AudioSfDecoder::onMessageReceived(const sp<AMessage> &msg) {
396    switch (msg->what()) {
397        case kWhatPrepare:
398            onPrepare();
399            break;
400
401        case kWhatDecode:
402            onDecode();
403            break;
404
405        case kWhatRender:
406            onRender();
407            break;
408
409        case kWhatCheckCache:
410            onCheckCache(msg);
411            break;
412
413        case kWhatNotif:
414            onNotify(msg);
415            break;
416
417        case kWhatPlay:
418            onPlay();
419            break;
420
421        case kWhatPause:
422            onPause();
423            break;
424/*
425        case kWhatSeek:
426            onSeek(msg);
427            break;
428
429        case kWhatLoop:
430            onLoop(msg);
431            break;
432*/
433        default:
434            GenericPlayer::onMessageReceived(msg);
435            break;
436    }
437}
438
439//--------------------------------------------------
440// Prepared state, prefetch status notifications
441void AudioSfDecoder::notifyPrepared(status_t prepareRes) {
442    notify(PLAYEREVENT_PREPARED, (int32_t)prepareRes, true);
443
444}
445
446void AudioSfDecoder::notifyStatus() {
447    notify(PLAYEREVENT_PREFETCHSTATUSCHANGE, (int32_t)mCacheStatus, true);
448}
449
450void AudioSfDecoder::notifyCacheFill() {
451    mLastNotifiedCacheFill = mCacheFill;
452    notify(PLAYEREVENT_PREFETCHFILLLEVELUPDATE, (int32_t)mLastNotifiedCacheFill, true);
453}
454
455void AudioSfDecoder::onNotify(const sp<AMessage> &msg) {
456    if (NULL == mNotifyClient) {
457        return;
458    }
459    int32_t val;
460    if (msg->findInt32(PLAYEREVENT_PREFETCHSTATUSCHANGE, &val)) {
461        SL_LOGV("\tASfPlayer notifying %s = %d", PLAYEREVENT_PREFETCHSTATUSCHANGE, val);
462        mNotifyClient(kEventPrefetchStatusChange, val, 0, mNotifyUser);
463    }
464    else if (msg->findInt32(PLAYEREVENT_PREFETCHFILLLEVELUPDATE, &val)) {
465        SL_LOGV("\tASfPlayer notifying %s = %d", PLAYEREVENT_PREFETCHFILLLEVELUPDATE, val);
466        mNotifyClient(kEventPrefetchFillLevelUpdate, val, 0, mNotifyUser);
467    }
468    else if (msg->findInt32(PLAYEREVENT_ENDOFSTREAM, &val)) {
469        SL_LOGV("\tASfPlayer notifying %s = %d", PLAYEREVENT_ENDOFSTREAM, val);
470        mNotifyClient(kEventEndOfStream, val, 0, mNotifyUser);
471    }
472    else {
473        GenericPlayer::onNotify(msg);
474    }
475}
476
477
478//--------------------------------------------------
479// Private utility functions
480
481bool AudioSfDecoder::wantPrefetch() {
482    return (mDataSource->flags() & DataSource::kWantsPrefetching);
483}
484
485
486int64_t AudioSfDecoder::getPositionUsec() {
487    Mutex::Autolock _l(mSeekLock);
488    if (mStateFlags & kFlagSeeking) {
489        return mSeekTimeMsec * 1000;
490    } else {
491        if (mLastDecodedPositionUs < 0) {
492            return 0;
493        } else {
494            return mLastDecodedPositionUs;
495        }
496    }
497}
498
499
500AudioSfDecoder::CacheStatus AudioSfDecoder::getCacheRemaining(bool *eos) {
501    sp<NuCachedSource2> cachedSource =
502        static_cast<NuCachedSource2 *>(mDataSource.get());
503
504    CacheStatus oldStatus = mCacheStatus;
505
506    status_t finalStatus;
507    size_t dataRemaining = cachedSource->approxDataRemaining(&finalStatus);
508    *eos = (finalStatus != OK);
509
510    CHECK_GE(mBitrate, 0);
511
512    int64_t dataRemainingUs = dataRemaining * 8000000ll / mBitrate;
513    //SL_LOGV("AudioSfDecoder::getCacheRemaining: approx %.2f secs remaining (eos=%d)",
514    //       dataRemainingUs / 1E6, *eos);
515
516    if (*eos) {
517        // data is buffered up to the end of the stream, it can't get any better than this
518        mCacheStatus = kStatusHigh;
519        mCacheFill = 1000;
520
521    } else {
522        if (mDurationUsec > 0) {
523            // known duration:
524
525            //   fill level is ratio of how much has been played + how much is
526            //   cached, divided by total duration
527            uint32_t currentPositionUsec = getPositionUsec();
528            mCacheFill = (int16_t) ((1000.0
529                    * (double)(currentPositionUsec + dataRemainingUs) / mDurationUsec));
530            //SL_LOGV("cacheFill = %d", mCacheFill);
531
532            //   cache status is evaluated against duration thresholds
533            if (dataRemainingUs > DURATION_CACHED_HIGH_US) {
534                mCacheStatus = kStatusHigh;
535                //LOGV("high");
536            } else if (dataRemainingUs > DURATION_CACHED_MED_US) {
537                //LOGV("enough");
538                mCacheStatus = kStatusEnough;
539            } else if (dataRemainingUs < DURATION_CACHED_LOW_US) {
540                //LOGV("low");
541                mCacheStatus = kStatusLow;
542            } else {
543                mCacheStatus = kStatusIntermediate;
544            }
545
546        } else {
547            // unknown duration:
548
549            //   cache status is evaluated against cache amount thresholds
550            //   (no duration so we don't have the bitrate either, could be derived from format?)
551            if (dataRemaining > SIZE_CACHED_HIGH_BYTES) {
552                mCacheStatus = kStatusHigh;
553            } else if (dataRemaining > SIZE_CACHED_MED_BYTES) {
554                mCacheStatus = kStatusEnough;
555            } else if (dataRemaining < SIZE_CACHED_LOW_BYTES) {
556                mCacheStatus = kStatusLow;
557            } else {
558                mCacheStatus = kStatusIntermediate;
559            }
560        }
561
562    }
563
564    if (oldStatus != mCacheStatus) {
565        notifyStatus();
566    }
567
568    if (abs(mCacheFill - mLastNotifiedCacheFill) > mCacheFillNotifThreshold) {
569        notifyCacheFill();
570    }
571
572    return mCacheStatus;
573}
574
575} // namespace android
576