Home | History | Annotate | Download | only in libopensles
      1 /*
      2  * Copyright (C) 2010 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 /** \brief libsndfile integration */
     18 
     19 #include "sles_allinclusive.h"
     20 
     21 
     22 /** \brief Called by SndFile.c:audioPlayerTransportUpdate after a play state change or seek,
     23  *  and by IOutputMixExt::FillBuffer after each buffer is consumed.
     24  */
     25 
     26 void SndFile_Callback(SLBufferQueueItf caller, void *pContext)
     27 {
     28     CAudioPlayer *thisAP = (CAudioPlayer *) pContext;
     29     object_lock_peek(&thisAP->mObject);
     30     SLuint32 state = thisAP->mPlay.mState;
     31     object_unlock_peek(&thisAP->mObject);
     32     if (SL_PLAYSTATE_PLAYING != state) {
     33         return;
     34     }
     35     struct SndFile *this = &thisAP->mSndFile;
     36     SLresult result;
     37     pthread_mutex_lock(&this->mMutex);
     38     if (this->mEOF) {
     39         pthread_mutex_unlock(&this->mMutex);
     40         return;
     41     }
     42     short *pBuffer = &this->mBuffer[this->mWhich * SndFile_BUFSIZE];
     43     if (++this->mWhich >= SndFile_NUMBUFS) {
     44         this->mWhich = 0;
     45     }
     46     sf_count_t count;
     47     count = sf_read_short(this->mSNDFILE, pBuffer, (sf_count_t) SndFile_BUFSIZE);
     48     pthread_mutex_unlock(&this->mMutex);
     49     bool headAtNewPos = false;
     50     object_lock_exclusive(&thisAP->mObject);
     51     slPlayCallback callback = thisAP->mPlay.mCallback;
     52     void *context = thisAP->mPlay.mContext;
     53     // make a copy of sample rate so we are absolutely sure we will not divide by zero
     54     SLuint32 sampleRateMilliHz = thisAP->mSampleRateMilliHz;
     55     if (0 != sampleRateMilliHz) {
     56         // this will overflow after 49 days, but no fix possible as it's part of the API
     57         thisAP->mPlay.mPosition = (SLuint32) (((long long) thisAP->mPlay.mFramesSinceLastSeek *
     58             1000000LL) / sampleRateMilliHz) + thisAP->mPlay.mLastSeekPosition;
     59         // make a good faith effort for the mean time between "head at new position" callbacks to
     60         // occur at the requested update period, but there will be jitter
     61         SLuint32 frameUpdatePeriod = thisAP->mPlay.mFrameUpdatePeriod;
     62         if ((0 != frameUpdatePeriod) &&
     63             (thisAP->mPlay.mFramesSincePositionUpdate >= frameUpdatePeriod) &&
     64             (SL_PLAYEVENT_HEADATNEWPOS & thisAP->mPlay.mEventFlags)) {
     65             // if we overrun a requested update period, then reset the clock modulo the
     66             // update period so that it appears to the application as one or more lost callbacks,
     67             // but no additional jitter
     68             if ((thisAP->mPlay.mFramesSincePositionUpdate -= thisAP->mPlay.mFrameUpdatePeriod) >=
     69                     frameUpdatePeriod) {
     70                 thisAP->mPlay.mFramesSincePositionUpdate %= frameUpdatePeriod;
     71             }
     72             headAtNewPos = true;
     73         }
     74     }
     75     if (0 < count) {
     76         object_unlock_exclusive(&thisAP->mObject);
     77         SLuint32 size = (SLuint32) (count * sizeof(short));
     78         result = IBufferQueue_Enqueue(caller, pBuffer, size);
     79         // not much we can do if the Enqueue fails, so we'll just drop the decoded data
     80         if (SL_RESULT_SUCCESS != result) {
     81             SL_LOGE("enqueue failed 0x%lx", result);
     82         }
     83     } else {
     84         thisAP->mPlay.mState = SL_PLAYSTATE_PAUSED;
     85         this->mEOF = SL_BOOLEAN_TRUE;
     86         // this would result in a non-monotonically increasing position, so don't do it
     87         // thisAP->mPlay.mPosition = thisAP->mPlay.mDuration;
     88         object_unlock_exclusive_attributes(&thisAP->mObject, ATTR_TRANSPORT);
     89     }
     90     // callbacks are called with mutex unlocked
     91     if (NULL != callback) {
     92         if (headAtNewPos) {
     93             (*callback)(&thisAP->mPlay.mItf, context, SL_PLAYEVENT_HEADATNEWPOS);
     94         }
     95     }
     96 }
     97 
     98 
     99 /** \brief Check whether the supplied libsndfile format is supported by us */
    100 
    101 SLboolean SndFile_IsSupported(const SF_INFO *sfinfo)
    102 {
    103     switch (sfinfo->format & SF_FORMAT_TYPEMASK) {
    104     case SF_FORMAT_WAV:
    105         break;
    106     default:
    107         return SL_BOOLEAN_FALSE;
    108     }
    109     switch (sfinfo->format & SF_FORMAT_SUBMASK) {
    110     case SF_FORMAT_PCM_U8:
    111     case SF_FORMAT_PCM_16:
    112         break;
    113     default:
    114         return SL_BOOLEAN_FALSE;
    115     }
    116     switch (sfinfo->samplerate) {
    117     case 11025:
    118     case 22050:
    119     case 44100:
    120         break;
    121     default:
    122         return SL_BOOLEAN_FALSE;
    123     }
    124     switch (sfinfo->channels) {
    125     case 1:
    126     case 2:
    127         break;
    128     default:
    129         return SL_BOOLEAN_FALSE;
    130     }
    131     return SL_BOOLEAN_TRUE;
    132 }
    133 
    134 
    135 /** \brief Check whether the partially-constructed AudioPlayer is compatible with libsndfile */
    136 
    137 SLresult SndFile_checkAudioPlayerSourceSink(CAudioPlayer *this)
    138 {
    139     const SLDataSource *pAudioSrc = &this->mDataSource.u.mSource;
    140     SLuint32 locatorType = *(SLuint32 *)pAudioSrc->pLocator;
    141     SLuint32 formatType = *(SLuint32 *)pAudioSrc->pFormat;
    142     switch (locatorType) {
    143     case SL_DATALOCATOR_BUFFERQUEUE:
    144 #ifdef ANDROID
    145     case SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE:
    146 #endif
    147         break;
    148     case SL_DATALOCATOR_URI:
    149         {
    150         SLDataLocator_URI *dl_uri = (SLDataLocator_URI *) pAudioSrc->pLocator;
    151         SLchar *uri = dl_uri->URI;
    152         if (NULL == uri) {
    153             return SL_RESULT_PARAMETER_INVALID;
    154         }
    155         if (!strncmp((const char *) uri, "file:///", 8)) {
    156             uri += 8;
    157         }
    158         switch (formatType) {
    159         case SL_DATAFORMAT_NULL:    // OK to omit the data format
    160         case SL_DATAFORMAT_MIME:    // we ignore a MIME type if specified
    161             break;
    162         default:
    163             return SL_RESULT_CONTENT_UNSUPPORTED;
    164         }
    165         this->mSndFile.mPathname = uri;
    166         this->mBufferQueue.mNumBuffers = SndFile_NUMBUFS;
    167         }
    168         break;
    169     default:
    170         return SL_RESULT_CONTENT_UNSUPPORTED;
    171     }
    172     this->mSndFile.mWhich = 0;
    173     this->mSndFile.mSNDFILE = NULL;
    174     // this->mSndFile.mMutex is initialized only when there is a valid mSNDFILE
    175     this->mSndFile.mEOF = SL_BOOLEAN_FALSE;
    176 
    177     return SL_RESULT_SUCCESS;
    178 }
    179 
    180 
    181 /** \brief Called with mutex unlocked for marker and position updates, and play state change */
    182 
    183 void audioPlayerTransportUpdate(CAudioPlayer *audioPlayer)
    184 {
    185 
    186     if (NULL != audioPlayer->mSndFile.mSNDFILE) {
    187 
    188         object_lock_exclusive(&audioPlayer->mObject);
    189         SLboolean empty = 0 == audioPlayer->mBufferQueue.mState.count;
    190         // FIXME a made-up number that should depend on player state and prefetch status
    191         audioPlayer->mPrefetchStatus.mLevel = 1000;
    192         SLmillisecond pos = audioPlayer->mSeek.mPos;
    193         if (SL_TIME_UNKNOWN != pos) {
    194             audioPlayer->mSeek.mPos = SL_TIME_UNKNOWN;
    195             // trim seek position to the current known duration
    196             if (pos > audioPlayer->mPlay.mDuration) {
    197                 pos = audioPlayer->mPlay.mDuration;
    198             }
    199             audioPlayer->mPlay.mLastSeekPosition = pos;
    200             audioPlayer->mPlay.mFramesSinceLastSeek = 0;
    201             // seek postpones the next head at new position callback
    202             audioPlayer->mPlay.mFramesSincePositionUpdate = 0;
    203         }
    204         object_unlock_exclusive(&audioPlayer->mObject);
    205 
    206         if (SL_TIME_UNKNOWN != pos) {
    207 
    208             // discard any enqueued buffers for the old position
    209             IBufferQueue_Clear(&audioPlayer->mBufferQueue.mItf);
    210             empty = SL_BOOLEAN_TRUE;
    211 
    212             pthread_mutex_lock(&audioPlayer->mSndFile.mMutex);
    213             // FIXME why void?
    214             (void) sf_seek(audioPlayer->mSndFile.mSNDFILE, (sf_count_t) (((long long) pos *
    215                 audioPlayer->mSndFile.mSfInfo.samplerate) / 1000LL), SEEK_SET);
    216             audioPlayer->mSndFile.mEOF = SL_BOOLEAN_FALSE;
    217             audioPlayer->mSndFile.mWhich = 0;
    218             pthread_mutex_unlock(&audioPlayer->mSndFile.mMutex);
    219 
    220         }
    221 
    222         // FIXME only on seek or play state change (STOPPED, PAUSED) -> PLAYING
    223         if (empty) {
    224             SndFile_Callback(&audioPlayer->mBufferQueue.mItf, audioPlayer);
    225         }
    226 
    227     }
    228 
    229 }
    230 
    231 
    232 /** \brief Called by CAudioPlayer_Realize */
    233 
    234 SLresult SndFile_Realize(CAudioPlayer *this)
    235 {
    236     SLresult result = SL_RESULT_SUCCESS;
    237     if (NULL != this->mSndFile.mPathname) {
    238         this->mSndFile.mSfInfo.format = 0;
    239         this->mSndFile.mSNDFILE = sf_open(
    240             (const char *) this->mSndFile.mPathname, SFM_READ, &this->mSndFile.mSfInfo);
    241         if (NULL == this->mSndFile.mSNDFILE) {
    242             result = SL_RESULT_CONTENT_NOT_FOUND;
    243         } else if (!SndFile_IsSupported(&this->mSndFile.mSfInfo)) {
    244             sf_close(this->mSndFile.mSNDFILE);
    245             this->mSndFile.mSNDFILE = NULL;
    246             result = SL_RESULT_CONTENT_UNSUPPORTED;
    247         } else {
    248             int ok;
    249             ok = pthread_mutex_init(&this->mSndFile.mMutex, (const pthread_mutexattr_t *) NULL);
    250             assert(0 == ok);
    251             SLBufferQueueItf bufferQueue = &this->mBufferQueue.mItf;
    252             IBufferQueue *thisBQ = (IBufferQueue *) bufferQueue;
    253             IBufferQueue_RegisterCallback(&thisBQ->mItf, SndFile_Callback, this);
    254             this->mPrefetchStatus.mStatus = SL_PREFETCHSTATUS_SUFFICIENTDATA;
    255             // this is the initial duration; will update when a new maximum position is detected
    256             this->mPlay.mDuration = (SLmillisecond) (((long long) this->mSndFile.mSfInfo.frames *
    257                 1000LL) / this->mSndFile.mSfInfo.samplerate);
    258             this->mNumChannels = this->mSndFile.mSfInfo.channels;
    259             this->mSampleRateMilliHz = this->mSndFile.mSfInfo.samplerate * 1000;
    260 #ifdef USE_OUTPUTMIXEXT
    261             this->mPlay.mFrameUpdatePeriod = ((long long) this->mPlay.mPositionUpdatePeriod *
    262                 (long long) this->mSampleRateMilliHz) / 1000000LL;
    263 #endif
    264         }
    265     }
    266     return result;
    267 }
    268 
    269 
    270 /** \brief Called by CAudioPlayer_Destroy */
    271 
    272 void SndFile_Destroy(CAudioPlayer *this)
    273 {
    274     if (NULL != this->mSndFile.mSNDFILE) {
    275         sf_close(this->mSndFile.mSNDFILE);
    276         this->mSndFile.mSNDFILE = NULL;
    277         int ok;
    278         ok = pthread_mutex_destroy(&this->mSndFile.mMutex);
    279         assert(0 == ok);
    280     }
    281 }
    282