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