android_AudioSfDecoder.cpp revision 3ac5dcc05fe321e4f01918aef2e3e54e22c9a5c1
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#include "android/android_AudioSfDecoder.h"
21
22#include <media/stagefright/foundation/ADebug.h>
23
24
25#define SIZE_CACHED_HIGH_BYTES 1000000
26#define SIZE_CACHED_MED_BYTES   700000
27#define SIZE_CACHED_LOW_BYTES   400000
28
29namespace android {
30
31//--------------------------------------------------------------------------------------------------
32AudioSfDecoder::AudioSfDecoder(const AudioPlayback_Parameters* params) : GenericPlayer(params),
33        mDataSource(0),
34        mAudioSource(0),
35        mAudioSourceStarted(false),
36        mBitrate(-1),
37        mChannelMask(UNKNOWN_CHANNELMASK),
38        mDurationUsec(ANDROID_UNKNOWN_TIME),
39        mDecodeBuffer(NULL),
40        mSeekTimeMsec(0),
41        // play event logic depends on the initial time being zero not ANDROID_UNKNOWN_TIME
42        mLastDecodedPositionUs(0)
43{
44    SL_LOGD("AudioSfDecoder::AudioSfDecoder()");
45}
46
47
48AudioSfDecoder::~AudioSfDecoder() {
49    SL_LOGD("AudioSfDecoder::~AudioSfDecoder()");
50}
51
52
53void AudioSfDecoder::preDestroy() {
54    GenericPlayer::preDestroy();
55    SL_LOGD("AudioSfDecoder::preDestroy()");
56    {
57        Mutex::Autolock _l(mBufferSourceLock);
58
59        if (NULL != mDecodeBuffer) {
60            mDecodeBuffer->release();
61            mDecodeBuffer = NULL;
62        }
63
64        if ((mAudioSource != 0) && mAudioSourceStarted) {
65            mAudioSource->stop();
66            mAudioSourceStarted = false;
67        }
68    }
69}
70
71
72//--------------------------------------------------
73void AudioSfDecoder::play() {
74    SL_LOGD("AudioSfDecoder::play");
75
76    GenericPlayer::play();
77    (new AMessage(kWhatDecode, id()))->post();
78}
79
80
81void AudioSfDecoder::getPositionMsec(int* msec) {
82    int64_t timeUsec = getPositionUsec();
83    if (timeUsec == ANDROID_UNKNOWN_TIME) {
84        *msec = ANDROID_UNKNOWN_TIME;
85    } else {
86        *msec = timeUsec / 1000;
87    }
88}
89
90
91//--------------------------------------------------
92uint32_t AudioSfDecoder::getPcmFormatKeyCount() const {
93    return NB_PCMMETADATA_KEYS;
94}
95
96
97//--------------------------------------------------
98bool AudioSfDecoder::getPcmFormatKeySize(uint32_t index, uint32_t* pKeySize) {
99    if (index >= NB_PCMMETADATA_KEYS) {
100        return false;
101    } else {
102        *pKeySize = strlen(kPcmDecodeMetadataKeys[index]) +1;
103        return true;
104    }
105}
106
107
108//--------------------------------------------------
109bool AudioSfDecoder::getPcmFormatKeyName(uint32_t index, uint32_t keySize, char* keyName) {
110    uint32_t actualKeySize;
111    if (!getPcmFormatKeySize(index, &actualKeySize)) {
112        return false;
113    }
114    if (keySize < actualKeySize) {
115        return false;
116    }
117    strncpy(keyName, kPcmDecodeMetadataKeys[index], actualKeySize);
118    return true;
119}
120
121
122//--------------------------------------------------
123bool AudioSfDecoder::getPcmFormatValueSize(uint32_t index, uint32_t* pValueSize) {
124    if (index >= NB_PCMMETADATA_KEYS) {
125        *pValueSize = 0;
126        return false;
127    } else {
128        *pValueSize = sizeof(uint32_t);
129        return true;
130    }
131}
132
133
134//--------------------------------------------------
135bool AudioSfDecoder::getPcmFormatKeyValue(uint32_t index, uint32_t size, uint32_t* pValue) {
136    uint32_t valueSize = 0;
137    if (!getPcmFormatValueSize(index, &valueSize)) {
138        return false;
139    } else if (size != valueSize) {
140        // this ensures we are accessing mPcmFormatValues with a valid size for that index
141        SL_LOGE("Error retrieving metadata value at index %d: using size of %d, should be %d",
142                index, size, valueSize);
143        return false;
144    } else {
145        android::Mutex::Autolock autoLock(mPcmFormatLock);
146        *pValue = mPcmFormatValues[index];
147        return true;
148    }
149}
150
151
152//--------------------------------------------------
153// Event handlers
154//  it is strictly verboten to call those methods outside of the event loop
155
156// Initializes the data and audio sources, and update the PCM format info
157// post-condition: upon successful initialization based on the player data locator
158//    GenericPlayer::onPrepare() was called
159//    mDataSource != 0
160//    mAudioSource != 0
161//    mAudioSourceStarted == true
162// All error returns from this method are via notifyPrepared(status) followed by "return".
163void AudioSfDecoder::onPrepare() {
164    SL_LOGD("AudioSfDecoder::onPrepare()");
165    Mutex::Autolock _l(mBufferSourceLock);
166
167    {
168    android::Mutex::Autolock autoLock(mPcmFormatLock);
169    // Initialize the PCM format info with the known parameters before the start of the decode
170    mPcmFormatValues[ANDROID_KEY_INDEX_PCMFORMAT_BITSPERSAMPLE] = SL_PCMSAMPLEFORMAT_FIXED_16;
171    mPcmFormatValues[ANDROID_KEY_INDEX_PCMFORMAT_CONTAINERSIZE] = 16;
172    mPcmFormatValues[ANDROID_KEY_INDEX_PCMFORMAT_ENDIANNESS] = SL_BYTEORDER_LITTLEENDIAN;
173    //    initialization with the default values: they will be replaced by the actual values
174    //      once the decoder has figured them out
175    mPcmFormatValues[ANDROID_KEY_INDEX_PCMFORMAT_NUMCHANNELS] = mChannelCount;
176    mPcmFormatValues[ANDROID_KEY_INDEX_PCMFORMAT_SAMPLERATE] = mSampleRateHz;
177    mPcmFormatValues[ANDROID_KEY_INDEX_PCMFORMAT_CHANNELMASK] = mChannelMask;
178    }
179
180    //---------------------------------
181    // Instantiate and initialize the data source for the decoder
182    sp<DataSource> dataSource;
183
184    switch (mDataLocatorType) {
185
186    case kDataLocatorNone:
187        SL_LOGE("AudioSfDecoder::onPrepare: no data locator set");
188        notifyPrepared(MEDIA_ERROR_BASE);
189        return;
190
191    case kDataLocatorUri:
192        dataSource = DataSource::CreateFromURI(mDataLocator.uriRef);
193        if (dataSource == NULL) {
194            SL_LOGE("AudioSfDecoder::onPrepare(): Error opening %s", mDataLocator.uriRef);
195            notifyPrepared(MEDIA_ERROR_BASE);
196            return;
197        }
198        break;
199
200    case kDataLocatorFd:
201    {
202        // As FileSource unconditionally takes ownership of the fd and closes it, then
203        // we have to make a dup for FileSource if the app wants to keep ownership itself
204        int fd = mDataLocator.fdi.fd;
205        if (mDataLocator.fdi.mCloseAfterUse) {
206            mDataLocator.fdi.mCloseAfterUse = false;
207        } else {
208            fd = ::dup(fd);
209        }
210        dataSource = new FileSource(fd, mDataLocator.fdi.offset, mDataLocator.fdi.length);
211        status_t err = dataSource->initCheck();
212        if (err != OK) {
213            notifyPrepared(err);
214            return;
215        }
216        break;
217    }
218
219    default:
220        TRESPASS();
221    }
222
223    //---------------------------------
224    // Instanciate and initialize the decoder attached to the data source
225    sp<MediaExtractor> extractor = MediaExtractor::Create(dataSource);
226    if (extractor == NULL) {
227        SL_LOGE("AudioSfDecoder::onPrepare: Could not instantiate extractor.");
228        notifyPrepared(ERROR_UNSUPPORTED);
229        return;
230    }
231
232    ssize_t audioTrackIndex = -1;
233    bool isRawAudio = false;
234    for (size_t i = 0; i < extractor->countTracks(); ++i) {
235        sp<MetaData> meta = extractor->getTrackMetaData(i);
236
237        const char *mime;
238        CHECK(meta->findCString(kKeyMIMEType, &mime));
239
240        if (!strncasecmp("audio/", mime, 6)) {
241            audioTrackIndex = i;
242
243            if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_RAW, mime)) {
244                isRawAudio = true;
245            }
246            break;
247        }
248    }
249
250    if (audioTrackIndex < 0) {
251        SL_LOGE("AudioSfDecoder::onPrepare: Could not find a supported audio track.");
252        notifyPrepared(ERROR_UNSUPPORTED);
253        return;
254    }
255
256    sp<MediaSource> source = extractor->getTrack(audioTrackIndex);
257    sp<MetaData> meta = source->getFormat();
258
259    // we can't trust the OMXCodec (if there is one) to issue a INFO_FORMAT_CHANGED so we want
260    // to have some meaningful values as soon as possible.
261    bool hasChannelCount = meta->findInt32(kKeyChannelCount, &mChannelCount);
262    int32_t sr;
263    bool hasSampleRate = meta->findInt32(kKeySampleRate, &sr);
264    if (hasSampleRate) {
265        mSampleRateHz = (uint32_t) sr;
266    }
267
268    off64_t size;
269    int64_t durationUs;
270    if (dataSource->getSize(&size) == OK
271            && meta->findInt64(kKeyDuration, &durationUs)) {
272        if (durationUs != 0) {
273            mBitrate = size * 8000000ll / durationUs;  // in bits/sec
274        } else {
275            mBitrate = -1;
276        }
277        mDurationUsec = durationUs;
278        mDurationMsec = durationUs / 1000;
279    } else {
280        mBitrate = -1;
281        mDurationUsec = ANDROID_UNKNOWN_TIME;
282        mDurationMsec = ANDROID_UNKNOWN_TIME;
283    }
284
285    // the audio content is not raw PCM, so we need a decoder
286    if (!isRawAudio) {
287        OMXClient client;
288        CHECK_EQ(client.connect(), (status_t)OK);
289
290        source = OMXCodec::Create(
291                client.interface(), meta, false /* createEncoder */,
292                source);
293
294        if (source == NULL) {
295            SL_LOGE("AudioSfDecoder::onPrepare: Could not instantiate decoder.");
296            notifyPrepared(ERROR_UNSUPPORTED);
297            return;
298        }
299
300        meta = source->getFormat();
301    }
302
303
304    if (source->start() != OK) {
305        SL_LOGE("AudioSfDecoder::onPrepare: Failed to start source/decoder.");
306        notifyPrepared(MEDIA_ERROR_BASE);
307        return;
308    }
309
310    //---------------------------------
311    // The data source, and audio source (a decoder if required) are ready to be used
312    mDataSource = dataSource;
313    mAudioSource = source;
314    mAudioSourceStarted = true;
315
316    if (!hasChannelCount) {
317        CHECK(meta->findInt32(kKeyChannelCount, &mChannelCount));
318    }
319
320    if (!hasSampleRate) {
321        CHECK(meta->findInt32(kKeySampleRate, &sr));
322        mSampleRateHz = (uint32_t) sr;
323    }
324    // FIXME add code below once channel mask support is in, currently initialized to default
325    //       value computed from the channel count
326    //    if (!hasChannelMask) {
327    //        CHECK(meta->findInt32(kKeyChannelMask, &mChannelMask));
328    //    }
329    mChannelMask = channelCountToMask(mChannelCount);
330
331    if (!wantPrefetch()) {
332        SL_LOGV("AudioSfDecoder::onPrepare: no need to prefetch");
333        // doesn't need prefetching, notify good to go
334        mCacheStatus = kStatusHigh;
335        mCacheFill = 1000;
336        notifyStatus();
337        notifyCacheFill();
338    }
339
340    {
341        android::Mutex::Autolock autoLock(mPcmFormatLock);
342        mPcmFormatValues[ANDROID_KEY_INDEX_PCMFORMAT_SAMPLERATE] = mSampleRateHz;
343        mPcmFormatValues[ANDROID_KEY_INDEX_PCMFORMAT_NUMCHANNELS] = mChannelCount;
344        mPcmFormatValues[ANDROID_KEY_INDEX_PCMFORMAT_CHANNELMASK] = mChannelMask;
345    }
346
347    // at this point we have enough information about the source to create the sink that
348    // will consume the data
349    createAudioSink();
350
351    // signal successful completion of prepare
352    mStateFlags |= kFlagPrepared;
353
354    GenericPlayer::onPrepare();
355    SL_LOGD("AudioSfDecoder::onPrepare() done, mStateFlags=0x%x", mStateFlags);
356}
357
358
359void AudioSfDecoder::onPause() {
360    SL_LOGV("AudioSfDecoder::onPause()");
361    GenericPlayer::onPause();
362    pauseAudioSink();
363}
364
365
366void AudioSfDecoder::onPlay() {
367    SL_LOGV("AudioSfDecoder::onPlay()");
368    GenericPlayer::onPlay();
369    startAudioSink();
370}
371
372
373void AudioSfDecoder::onSeek(const sp<AMessage> &msg) {
374    SL_LOGV("AudioSfDecoder::onSeek");
375    int64_t timeMsec;
376    CHECK(msg->findInt64(WHATPARAM_SEEK_SEEKTIME_MS, &timeMsec));
377
378    Mutex::Autolock _l(mTimeLock);
379    mStateFlags |= kFlagSeeking;
380    mSeekTimeMsec = timeMsec;
381    // don't set mLastDecodedPositionUs to ANDROID_UNKNOWN_TIME; getPositionUsec
382    // ignores mLastDecodedPositionUs while seeking, and substitutes the seek goal instead
383
384    // nop for now
385    GenericPlayer::onSeek(msg);
386}
387
388
389void AudioSfDecoder::onLoop(const sp<AMessage> &msg) {
390    SL_LOGV("AudioSfDecoder::onLoop");
391    int32_t loop;
392    CHECK(msg->findInt32(WHATPARAM_LOOP_LOOPING, &loop));
393
394    if (loop) {
395        //SL_LOGV("AudioSfDecoder::onLoop start looping");
396        mStateFlags |= kFlagLooping;
397    } else {
398        //SL_LOGV("AudioSfDecoder::onLoop stop looping");
399        mStateFlags &= ~kFlagLooping;
400    }
401
402    // nop for now
403    GenericPlayer::onLoop(msg);
404}
405
406
407void AudioSfDecoder::onCheckCache(const sp<AMessage> &msg) {
408    //SL_LOGV("AudioSfDecoder::onCheckCache");
409    bool eos;
410    CacheStatus_t status = getCacheRemaining(&eos);
411
412    if (eos || status == kStatusHigh
413            || ((mStateFlags & kFlagPreparing) && (status >= kStatusEnough))) {
414        if (mStateFlags & kFlagPlaying) {
415            startAudioSink();
416        }
417        mStateFlags &= ~kFlagBuffering;
418
419        SL_LOGV("AudioSfDecoder::onCheckCache: buffering done.");
420
421        if (mStateFlags & kFlagPreparing) {
422            //SL_LOGV("AudioSfDecoder::onCheckCache: preparation done.");
423            mStateFlags &= ~kFlagPreparing;
424        }
425
426        if (mStateFlags & kFlagPlaying) {
427            (new AMessage(kWhatDecode, id()))->post();
428        }
429        return;
430    }
431
432    msg->post(100000);
433}
434
435
436void AudioSfDecoder::onDecode() {
437    SL_LOGV("AudioSfDecoder::onDecode");
438
439    //-------------------------------- Need to buffer some more before decoding?
440    bool eos;
441    if (mDataSource == 0) {
442        // application set play state to paused which failed, then set play state to playing
443        return;
444    }
445
446    if (wantPrefetch()
447            && (getCacheRemaining(&eos) == kStatusLow)
448            && !eos) {
449        SL_LOGV("buffering more.");
450
451        if (mStateFlags & kFlagPlaying) {
452            pauseAudioSink();
453        }
454        mStateFlags |= kFlagBuffering;
455        (new AMessage(kWhatCheckCache, id()))->post(100000);
456        return;
457    }
458
459    if (!(mStateFlags & (kFlagPlaying | kFlagBuffering | kFlagPreparing))) {
460        // don't decode if we're not buffering, prefetching or playing
461        //SL_LOGV("don't decode: not buffering, prefetching or playing");
462        return;
463    }
464
465    //-------------------------------- Decode
466    status_t err;
467    MediaSource::ReadOptions readOptions;
468    if (mStateFlags & kFlagSeeking) {
469        assert(mSeekTimeMsec != ANDROID_UNKNOWN_TIME);
470        readOptions.setSeekTo(mSeekTimeMsec * 1000);
471    }
472
473    int64_t timeUsec = ANDROID_UNKNOWN_TIME;
474    {
475        Mutex::Autolock _l(mBufferSourceLock);
476
477        if (NULL != mDecodeBuffer) {
478            // the current decoded buffer hasn't been rendered, drop it
479            mDecodeBuffer->release();
480            mDecodeBuffer = NULL;
481        }
482        if(!mAudioSourceStarted) {
483            return;
484        }
485        err = mAudioSource->read(&mDecodeBuffer, &readOptions);
486        if (err == OK) {
487            CHECK(mDecodeBuffer->meta_data()->findInt64(kKeyTime, &timeUsec));
488        }
489    }
490
491    {
492        Mutex::Autolock _l(mTimeLock);
493        if (mStateFlags & kFlagSeeking) {
494            mStateFlags &= ~kFlagSeeking;
495            mSeekTimeMsec = ANDROID_UNKNOWN_TIME;
496        }
497        if (timeUsec != ANDROID_UNKNOWN_TIME) {
498            // Note that though we've decoded this position, we haven't rendered it yet.
499            // So a GetPosition called after this point will observe the advanced position,
500            // even though the PCM may not have been supplied to the sink.  That's OK as
501            // we don't claim to provide frame-accurate (let alone sample-accurate) GetPosition.
502            mLastDecodedPositionUs = timeUsec;
503        }
504    }
505
506    //-------------------------------- Handle return of decode
507    if (err != OK) {
508        bool continueDecoding = false;
509        switch(err) {
510            case ERROR_END_OF_STREAM:
511                if (0 < mDurationUsec) {
512                    Mutex::Autolock _l(mTimeLock);
513                    mLastDecodedPositionUs = mDurationUsec;
514                }
515                // handle notification and looping at end of stream
516                if (mStateFlags & kFlagPlaying) {
517                    notify(PLAYEREVENT_ENDOFSTREAM, 1, true);
518                }
519                if (mStateFlags & kFlagLooping) {
520                    seek(0);
521                    // kick-off decoding again
522                    continueDecoding = true;
523                }
524                break;
525            case INFO_FORMAT_CHANGED:
526                SL_LOGD("MediaSource::read encountered INFO_FORMAT_CHANGED");
527                // reconfigure output
528                {
529                    Mutex::Autolock _l(mBufferSourceLock);
530                    hasNewDecodeParams();
531                }
532                continueDecoding = true;
533                break;
534            case INFO_DISCONTINUITY:
535                SL_LOGD("MediaSource::read encountered INFO_DISCONTINUITY");
536                continueDecoding = true;
537                break;
538            default:
539                SL_LOGE("MediaSource::read returned error %d", err);
540                break;
541        }
542        if (continueDecoding) {
543            if (NULL == mDecodeBuffer) {
544                (new AMessage(kWhatDecode, id()))->post();
545                return;
546            }
547        } else {
548            return;
549        }
550    }
551
552    //-------------------------------- Render
553    sp<AMessage> msg = new AMessage(kWhatRender, id());
554    msg->post();
555
556}
557
558
559void AudioSfDecoder::onMessageReceived(const sp<AMessage> &msg) {
560    switch (msg->what()) {
561        case kWhatDecode:
562            onDecode();
563            break;
564
565        case kWhatRender:
566            onRender();
567            break;
568
569        case kWhatCheckCache:
570            onCheckCache(msg);
571            break;
572
573        default:
574            GenericPlayer::onMessageReceived(msg);
575            break;
576    }
577}
578
579//--------------------------------------------------
580// Prepared state, prefetch status notifications
581void AudioSfDecoder::notifyPrepared(status_t prepareRes) {
582    assert(!(mStateFlags & (kFlagPrepared | kFlagPreparedUnsuccessfully)));
583    if (NO_ERROR == prepareRes) {
584        // The "then" fork is not currently used, but is kept here to make it easier
585        // to replace by a new signalPrepareCompletion(status) if we re-visit this later.
586        mStateFlags |= kFlagPrepared;
587    } else {
588        mStateFlags |= kFlagPreparedUnsuccessfully;
589    }
590    // Do not call the superclass onPrepare to notify, because it uses a default error
591    // status code but we can provide a more specific one.
592    // GenericPlayer::onPrepare();
593    notify(PLAYEREVENT_PREPARED, (int32_t)prepareRes, true);
594    SL_LOGD("AudioSfDecoder::onPrepare() done, mStateFlags=0x%x", mStateFlags);
595}
596
597
598void AudioSfDecoder::onNotify(const sp<AMessage> &msg) {
599    notif_cbf_t notifyClient;
600    void*       notifyUser;
601    {
602        android::Mutex::Autolock autoLock(mNotifyClientLock);
603        if (NULL == mNotifyClient) {
604            return;
605        } else {
606            notifyClient = mNotifyClient;
607            notifyUser   = mNotifyUser;
608        }
609    }
610    int32_t val;
611    if (msg->findInt32(PLAYEREVENT_PREFETCHSTATUSCHANGE, &val)) {
612        SL_LOGV("\tASfPlayer notifying %s = %d", PLAYEREVENT_PREFETCHSTATUSCHANGE, val);
613        notifyClient(kEventPrefetchStatusChange, val, 0, notifyUser);
614    }
615    else if (msg->findInt32(PLAYEREVENT_PREFETCHFILLLEVELUPDATE, &val)) {
616        SL_LOGV("\tASfPlayer notifying %s = %d", PLAYEREVENT_PREFETCHFILLLEVELUPDATE, val);
617        notifyClient(kEventPrefetchFillLevelUpdate, val, 0, notifyUser);
618    }
619    else if (msg->findInt32(PLAYEREVENT_ENDOFSTREAM, &val)) {
620        SL_LOGV("\tASfPlayer notifying %s = %d", PLAYEREVENT_ENDOFSTREAM, val);
621        notifyClient(kEventEndOfStream, val, 0, notifyUser);
622    }
623    else {
624        GenericPlayer::onNotify(msg);
625    }
626}
627
628
629//--------------------------------------------------
630// Private utility functions
631
632bool AudioSfDecoder::wantPrefetch() {
633    if (mDataSource != 0) {
634        return (mDataSource->flags() & DataSource::kWantsPrefetching);
635    } else {
636        // happens if an improper data locator was passed, if the media extractor couldn't be
637        //  initialized, if there is no audio track in the media, if the OMX decoder couldn't be
638        //  instantiated, if the source couldn't be opened, or if the MediaSource
639        //  couldn't be started
640        SL_LOGV("AudioSfDecoder::wantPrefetch() tries to access NULL mDataSource");
641        return false;
642    }
643}
644
645
646int64_t AudioSfDecoder::getPositionUsec() {
647    Mutex::Autolock _l(mTimeLock);
648    if (mStateFlags & kFlagSeeking) {
649        return mSeekTimeMsec * 1000;
650    } else {
651        return mLastDecodedPositionUs;
652    }
653}
654
655
656CacheStatus_t AudioSfDecoder::getCacheRemaining(bool *eos) {
657    sp<NuCachedSource2> cachedSource =
658        static_cast<NuCachedSource2 *>(mDataSource.get());
659
660    CacheStatus_t oldStatus = mCacheStatus;
661
662    status_t finalStatus;
663    size_t dataRemaining = cachedSource->approxDataRemaining(&finalStatus);
664    *eos = (finalStatus != OK);
665
666    CHECK_GE(mBitrate, 0);
667
668    int64_t dataRemainingUs = dataRemaining * 8000000ll / mBitrate;
669    //SL_LOGV("AudioSfDecoder::getCacheRemaining: approx %.2f secs remaining (eos=%d)",
670    //       dataRemainingUs / 1E6, *eos);
671
672    if (*eos) {
673        // data is buffered up to the end of the stream, it can't get any better than this
674        mCacheStatus = kStatusHigh;
675        mCacheFill = 1000;
676
677    } else {
678        if (mDurationUsec > 0) {
679            // known duration:
680
681            //   fill level is ratio of how much has been played + how much is
682            //   cached, divided by total duration
683            uint32_t currentPositionUsec = getPositionUsec();
684            if (currentPositionUsec == ANDROID_UNKNOWN_TIME) {
685                // if we don't know where we are, assume the worst for the fill ratio
686                currentPositionUsec = 0;
687            }
688            if (mDurationUsec > 0) {
689                mCacheFill = (int16_t) ((1000.0
690                        * (double)(currentPositionUsec + dataRemainingUs) / mDurationUsec));
691            } else {
692                mCacheFill = 0;
693            }
694            //SL_LOGV("cacheFill = %d", mCacheFill);
695
696            //   cache status is evaluated against duration thresholds
697            if (dataRemainingUs > DURATION_CACHED_HIGH_MS*1000) {
698                mCacheStatus = kStatusHigh;
699                //LOGV("high");
700            } else if (dataRemainingUs > DURATION_CACHED_MED_MS*1000) {
701                //LOGV("enough");
702                mCacheStatus = kStatusEnough;
703            } else if (dataRemainingUs < DURATION_CACHED_LOW_MS*1000) {
704                //LOGV("low");
705                mCacheStatus = kStatusLow;
706            } else {
707                mCacheStatus = kStatusIntermediate;
708            }
709
710        } else {
711            // unknown duration:
712
713            //   cache status is evaluated against cache amount thresholds
714            //   (no duration so we don't have the bitrate either, could be derived from format?)
715            if (dataRemaining > SIZE_CACHED_HIGH_BYTES) {
716                mCacheStatus = kStatusHigh;
717            } else if (dataRemaining > SIZE_CACHED_MED_BYTES) {
718                mCacheStatus = kStatusEnough;
719            } else if (dataRemaining < SIZE_CACHED_LOW_BYTES) {
720                mCacheStatus = kStatusLow;
721            } else {
722                mCacheStatus = kStatusIntermediate;
723            }
724        }
725
726    }
727
728    if (oldStatus != mCacheStatus) {
729        notifyStatus();
730    }
731
732    if (abs(mCacheFill - mLastNotifiedCacheFill) > mCacheFillNotifThreshold) {
733        notifyCacheFill();
734    }
735
736    return mCacheStatus;
737}
738
739
740void AudioSfDecoder::hasNewDecodeParams() {
741
742    if ((mAudioSource != 0) && mAudioSourceStarted) {
743        sp<MetaData> meta = mAudioSource->getFormat();
744
745        SL_LOGV("old sample rate = %d, channel count = %d", mSampleRateHz, mChannelCount);
746
747        CHECK(meta->findInt32(kKeyChannelCount, &mChannelCount));
748        int32_t sr;
749        CHECK(meta->findInt32(kKeySampleRate, &sr));
750        mSampleRateHz = (uint32_t) sr;
751        SL_LOGV("format changed: new sample rate = %d, channel count = %d",
752                mSampleRateHz, mChannelCount);
753
754        // FIXME similar to onPrepare()
755        mChannelMask = channelCountToMask(mChannelCount);
756
757        {
758            android::Mutex::Autolock autoLock(mPcmFormatLock);
759            mPcmFormatValues[ANDROID_KEY_INDEX_PCMFORMAT_NUMCHANNELS] = mChannelCount;
760            mPcmFormatValues[ANDROID_KEY_INDEX_PCMFORMAT_SAMPLERATE] = mSampleRateHz;
761            mPcmFormatValues[ANDROID_KEY_INDEX_PCMFORMAT_CHANNELMASK] = mChannelMask;
762        }
763    }
764
765    // alert users of those params
766    updateAudioSink();
767}
768
769} // namespace android
770