BufferQueueSource.cpp revision 5760ef34bd7f50060fd8fbda10e359f8a830bfbc
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/BufferQueueSource.h"
21
22#include <media/stagefright/MediaDebug.h>
23#include <sys/types.h>
24#include <unistd.h>
25#include <sys/types.h>
26#include <sys/stat.h>
27#include <fcntl.h>
28
29namespace android {
30
31
32const SLuint32 BufferQueueSource::kItemProcessed[NB_BUFFEREVENT_ITEM_FIELDS] = {
33        SL_ANDROID_ITEMKEY_BUFFERQUEUEEVENT, // item key
34        sizeof(SLuint32),                    // item size
35        SL_ANDROIDBUFFERQUEUEEVENT_PROCESSED // item data
36};
37
38
39BufferQueueSource::BufferQueueSource(const void* user, void *context,  const void *caller) :
40          mAndroidBufferQueueSource(NULL),
41          mStreamToBqOffset(0)
42{
43    if (NULL != user) {
44        mAndroidBufferQueueSource = &((CAudioPlayer*)user)->mAndroidBufferQueue;
45    } else {
46        SL_LOGE("Can't create BufferQueueSource with NULL user");
47    }
48
49}
50
51
52BufferQueueSource::~BufferQueueSource() {
53    SL_LOGD("BufferQueueSource::~BufferQueueSource");
54}
55
56
57//--------------------------------------------------------------------------
58status_t BufferQueueSource::initCheck() const {
59    return mAndroidBufferQueueSource != NULL ? OK : NO_INIT;
60}
61
62ssize_t BufferQueueSource::readAt(off64_t offset, void *data, size_t size) {
63    SL_LOGD("BufferQueueSource::readAt(offset=%lld, data=%p, size=%d)", offset, data, size);
64
65    ssize_t readSize = 0;
66    slAndroidBufferQueueCallback callback = NULL;
67    void* pBufferContext, *pBufferData, *callbackPContext = NULL;
68    AdvancedBufferHeader *oldFront = NULL;
69    uint32_t dataSize, dataUsed = 0;
70
71    interface_lock_exclusive(mAndroidBufferQueueSource);
72
73    if (mAndroidBufferQueueSource->mState.count == 0) {
74        readSize = 0;
75    } else {
76        assert(mAndroidBufferQueueSource->mFront != mAndroidBufferQueueSource->mRear);
77
78        oldFront = mAndroidBufferQueueSource->mFront;
79        AdvancedBufferHeader *newFront = &oldFront[1];
80
81        // where to read from
82        char *pSrc = NULL;
83        // can this read operation cause us to call the buffer queue callback
84        // (either because there was a command with no data, or all the data has been consumed)
85        bool queueCallbackCandidate = false;
86
87        // consume events when starting to read data from a buffer for the first time
88        if (oldFront->mDataSizeConsumed == 0) {
89            if (oldFront->mItems.mAdtsCmdData.mAdtsCmdCode & ANDROID_ADTSEVENT_EOS) {
90                // EOS has no associated data
91                queueCallbackCandidate = true;
92            }
93            oldFront->mItems.mAdtsCmdData.mAdtsCmdCode = ANDROID_ADTSEVENT_NONE;
94        }
95
96        //assert(mStreamToBqOffset <= offset);
97        CHECK(mStreamToBqOffset <= offset);
98
99        if (offset + size <= mStreamToBqOffset + oldFront->mDataSize) {
100            pSrc = ((char*)oldFront->mDataBuffer) + (offset - mStreamToBqOffset);
101
102            if (offset - mStreamToBqOffset + size == oldFront->mDataSize) {
103                // consumed buffer entirely
104                oldFront->mDataSizeConsumed = oldFront->mDataSize;
105                mStreamToBqOffset += oldFront->mDataSize;
106                queueCallbackCandidate = true;
107
108                // move queue to next buffer
109                if (newFront == &mAndroidBufferQueueSource->
110                        mBufferArray[mAndroidBufferQueueSource->mNumBuffers + 1]) {
111                    // reached the end, circle back
112                    newFront = mAndroidBufferQueueSource->mBufferArray;
113                }
114                mAndroidBufferQueueSource->mFront = newFront;
115                // update the queue state
116                mAndroidBufferQueueSource->mState.count--;
117                mAndroidBufferQueueSource->mState.index++;
118                SL_LOGV("BufferQueueSource moving to next buffer");
119            }
120        }
121
122        // consume data: copy to given destination
123        if (NULL != pSrc) {
124            memcpy(data, pSrc, size);
125            readSize = size;
126        } else {
127            readSize = 0;
128        }
129
130        if (queueCallbackCandidate) {
131            // data has been consumed, and the buffer queue state has been updated
132            // we will notify the client if applicable
133            if (mAndroidBufferQueueSource->mCallbackEventsMask &
134                    SL_ANDROIDBUFFERQUEUEEVENT_PROCESSED) {
135                callback = mAndroidBufferQueueSource->mCallback;
136                // save callback data while under lock
137                callbackPContext = mAndroidBufferQueueSource->mContext;
138                pBufferContext = (void *)oldFront->mBufferContext;
139                pBufferData    = (void *)oldFront->mDataBuffer;
140                dataSize       = oldFront->mDataSize;
141                dataUsed       = oldFront->mDataSizeConsumed;
142            }
143        }
144    }
145
146    interface_unlock_exclusive(mAndroidBufferQueueSource);
147
148    // notify client
149    if (NULL != callback) {
150        SLresult result = (*callback)(&mAndroidBufferQueueSource->mItf, callbackPContext,
151                pBufferContext, pBufferData, dataSize, dataUsed,
152                // no messages during playback other than marking the buffer as processed
153                (const SLAndroidBufferItem*)(&kItemProcessed) /* pItems */,
154                NB_BUFFEREVENT_ITEM_FIELDS * sizeof(SLuint32) /* itemsLength */ );
155        if (SL_RESULT_SUCCESS != result) {
156            // Reserved for future use
157            SL_LOGW("Unsuccessful result %d returned from AndroidBufferQueueCallback", result);
158        }
159    }
160
161    return readSize;
162}
163
164
165status_t BufferQueueSource::getSize(off64_t *size) {
166    SL_LOGD("BufferQueueSource::getSize()");
167    // we're streaming, we don't know how much there is
168    *size = 0;
169    return OK;
170}
171
172}  // namespace android
173