xref: /aosp_15_r20/frameworks/wilhelm/src/itf/IBufferQueue.cpp (revision bebae9c0e76121f8312ccb50385c080b3a0b023c)
1*bebae9c0SAndroid Build Coastguard Worker /*
2*bebae9c0SAndroid Build Coastguard Worker  * Copyright (C) 2010 The Android Open Source Project
3*bebae9c0SAndroid Build Coastguard Worker  *
4*bebae9c0SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*bebae9c0SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*bebae9c0SAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*bebae9c0SAndroid Build Coastguard Worker  *
8*bebae9c0SAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*bebae9c0SAndroid Build Coastguard Worker  *
10*bebae9c0SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*bebae9c0SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*bebae9c0SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*bebae9c0SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*bebae9c0SAndroid Build Coastguard Worker  * limitations under the License.
15*bebae9c0SAndroid Build Coastguard Worker  */
16*bebae9c0SAndroid Build Coastguard Worker 
17*bebae9c0SAndroid Build Coastguard Worker /* BufferQueue implementation */
18*bebae9c0SAndroid Build Coastguard Worker 
19*bebae9c0SAndroid Build Coastguard Worker #include "sles_allinclusive.h"
20*bebae9c0SAndroid Build Coastguard Worker 
21*bebae9c0SAndroid Build Coastguard Worker 
22*bebae9c0SAndroid Build Coastguard Worker /** Determine the state of the audio player or audio recorder associated with a buffer queue.
23*bebae9c0SAndroid Build Coastguard Worker  *  Note that PLAYSTATE and RECORDSTATE values are equivalent (where PLAYING == RECORDING).
24*bebae9c0SAndroid Build Coastguard Worker  */
25*bebae9c0SAndroid Build Coastguard Worker 
getAssociatedState(IBufferQueue * thiz)26*bebae9c0SAndroid Build Coastguard Worker static SLuint32 getAssociatedState(IBufferQueue *thiz)
27*bebae9c0SAndroid Build Coastguard Worker {
28*bebae9c0SAndroid Build Coastguard Worker     SLuint32 state;
29*bebae9c0SAndroid Build Coastguard Worker     switch (InterfaceToObjectID(thiz)) {
30*bebae9c0SAndroid Build Coastguard Worker     case SL_OBJECTID_AUDIOPLAYER:
31*bebae9c0SAndroid Build Coastguard Worker         state = ((CAudioPlayer *) thiz->mThis)->mPlay.mState;
32*bebae9c0SAndroid Build Coastguard Worker         break;
33*bebae9c0SAndroid Build Coastguard Worker     case SL_OBJECTID_AUDIORECORDER:
34*bebae9c0SAndroid Build Coastguard Worker         state = ((CAudioRecorder *) thiz->mThis)->mRecord.mState;
35*bebae9c0SAndroid Build Coastguard Worker         break;
36*bebae9c0SAndroid Build Coastguard Worker     default:
37*bebae9c0SAndroid Build Coastguard Worker         // unreachable, but just in case we will assume it is stopped
38*bebae9c0SAndroid Build Coastguard Worker         assert(SL_BOOLEAN_FALSE);
39*bebae9c0SAndroid Build Coastguard Worker         state = SL_PLAYSTATE_STOPPED;
40*bebae9c0SAndroid Build Coastguard Worker         break;
41*bebae9c0SAndroid Build Coastguard Worker     }
42*bebae9c0SAndroid Build Coastguard Worker     return state;
43*bebae9c0SAndroid Build Coastguard Worker }
44*bebae9c0SAndroid Build Coastguard Worker 
45*bebae9c0SAndroid Build Coastguard Worker 
IBufferQueue_Enqueue(SLBufferQueueItf self,const void * pBuffer,SLuint32 size)46*bebae9c0SAndroid Build Coastguard Worker SLresult IBufferQueue_Enqueue(SLBufferQueueItf self, const void *pBuffer, SLuint32 size)
47*bebae9c0SAndroid Build Coastguard Worker {
48*bebae9c0SAndroid Build Coastguard Worker     SL_ENTER_INTERFACE
49*bebae9c0SAndroid Build Coastguard Worker 
50*bebae9c0SAndroid Build Coastguard Worker     // Note that Enqueue while a Clear is pending is equivalent to Enqueue followed by Clear
51*bebae9c0SAndroid Build Coastguard Worker 
52*bebae9c0SAndroid Build Coastguard Worker     if (NULL == pBuffer || 0 == size) {
53*bebae9c0SAndroid Build Coastguard Worker         result = SL_RESULT_PARAMETER_INVALID;
54*bebae9c0SAndroid Build Coastguard Worker     } else {
55*bebae9c0SAndroid Build Coastguard Worker         IBufferQueue *thiz = (IBufferQueue *) self;
56*bebae9c0SAndroid Build Coastguard Worker         interface_lock_exclusive(thiz);
57*bebae9c0SAndroid Build Coastguard Worker         BufferHeader *oldRear = thiz->mRear, *newRear;
58*bebae9c0SAndroid Build Coastguard Worker         if ((newRear = oldRear + 1) == &thiz->mArray[thiz->mNumBuffers + 1]) {
59*bebae9c0SAndroid Build Coastguard Worker             newRear = thiz->mArray;
60*bebae9c0SAndroid Build Coastguard Worker         }
61*bebae9c0SAndroid Build Coastguard Worker         if (newRear == thiz->mFront) {
62*bebae9c0SAndroid Build Coastguard Worker             result = SL_RESULT_BUFFER_INSUFFICIENT;
63*bebae9c0SAndroid Build Coastguard Worker         } else {
64*bebae9c0SAndroid Build Coastguard Worker             oldRear->mBuffer = pBuffer;
65*bebae9c0SAndroid Build Coastguard Worker             oldRear->mSize = size;
66*bebae9c0SAndroid Build Coastguard Worker             thiz->mRear = newRear;
67*bebae9c0SAndroid Build Coastguard Worker             ++thiz->mState.count;
68*bebae9c0SAndroid Build Coastguard Worker             result = SL_RESULT_SUCCESS;
69*bebae9c0SAndroid Build Coastguard Worker         }
70*bebae9c0SAndroid Build Coastguard Worker         // set enqueue attribute if state is PLAYING and the first buffer is enqueued
71*bebae9c0SAndroid Build Coastguard Worker         interface_unlock_exclusive_attributes(thiz, ((SL_RESULT_SUCCESS == result) &&
72*bebae9c0SAndroid Build Coastguard Worker             (1 == thiz->mState.count) && (SL_PLAYSTATE_PLAYING == getAssociatedState(thiz))) ?
73*bebae9c0SAndroid Build Coastguard Worker             ATTR_BQ_ENQUEUE : ATTR_NONE);
74*bebae9c0SAndroid Build Coastguard Worker     }
75*bebae9c0SAndroid Build Coastguard Worker     SL_LEAVE_INTERFACE
76*bebae9c0SAndroid Build Coastguard Worker }
77*bebae9c0SAndroid Build Coastguard Worker 
78*bebae9c0SAndroid Build Coastguard Worker 
IBufferQueue_Clear(SLBufferQueueItf self)79*bebae9c0SAndroid Build Coastguard Worker SLresult IBufferQueue_Clear(SLBufferQueueItf self)
80*bebae9c0SAndroid Build Coastguard Worker {
81*bebae9c0SAndroid Build Coastguard Worker     SL_ENTER_INTERFACE
82*bebae9c0SAndroid Build Coastguard Worker 
83*bebae9c0SAndroid Build Coastguard Worker     result = SL_RESULT_SUCCESS;
84*bebae9c0SAndroid Build Coastguard Worker     IBufferQueue *thiz = (IBufferQueue *) self;
85*bebae9c0SAndroid Build Coastguard Worker     interface_lock_exclusive(thiz);
86*bebae9c0SAndroid Build Coastguard Worker 
87*bebae9c0SAndroid Build Coastguard Worker #ifdef ANDROID
88*bebae9c0SAndroid Build Coastguard Worker     if (SL_OBJECTID_AUDIOPLAYER == InterfaceToObjectID(thiz)) {
89*bebae9c0SAndroid Build Coastguard Worker         CAudioPlayer *audioPlayer = (CAudioPlayer *) thiz->mThis;
90*bebae9c0SAndroid Build Coastguard Worker         // flush associated audio player
91*bebae9c0SAndroid Build Coastguard Worker         result = android_audioPlayer_bufferQueue_onClear(audioPlayer);
92*bebae9c0SAndroid Build Coastguard Worker     }
93*bebae9c0SAndroid Build Coastguard Worker     // flush our buffers
94*bebae9c0SAndroid Build Coastguard Worker     if (SL_RESULT_SUCCESS == result) {
95*bebae9c0SAndroid Build Coastguard Worker         thiz->mFront = &thiz->mArray[0];
96*bebae9c0SAndroid Build Coastguard Worker         thiz->mRear = &thiz->mArray[0];
97*bebae9c0SAndroid Build Coastguard Worker         thiz->mState.count = 0;
98*bebae9c0SAndroid Build Coastguard Worker         thiz->mState.playIndex = 0;
99*bebae9c0SAndroid Build Coastguard Worker         thiz->mSizeConsumed = 0;
100*bebae9c0SAndroid Build Coastguard Worker         thiz->mCallbackPending = false;
101*bebae9c0SAndroid Build Coastguard Worker     }
102*bebae9c0SAndroid Build Coastguard Worker #endif
103*bebae9c0SAndroid Build Coastguard Worker 
104*bebae9c0SAndroid Build Coastguard Worker #ifdef USE_OUTPUTMIXEXT
105*bebae9c0SAndroid Build Coastguard Worker     // mixer might be reading from the front buffer, so tread carefully here
106*bebae9c0SAndroid Build Coastguard Worker     // NTH asynchronous cancel instead of blocking until mixer acknowledges
107*bebae9c0SAndroid Build Coastguard Worker     thiz->mClearRequested = SL_BOOLEAN_TRUE;
108*bebae9c0SAndroid Build Coastguard Worker     do {
109*bebae9c0SAndroid Build Coastguard Worker         interface_cond_wait(thiz);
110*bebae9c0SAndroid Build Coastguard Worker     } while (thiz->mClearRequested);
111*bebae9c0SAndroid Build Coastguard Worker #endif
112*bebae9c0SAndroid Build Coastguard Worker 
113*bebae9c0SAndroid Build Coastguard Worker     interface_unlock_exclusive(thiz);
114*bebae9c0SAndroid Build Coastguard Worker 
115*bebae9c0SAndroid Build Coastguard Worker     // there is a danger that a buffer is still held by the callback thread
116*bebae9c0SAndroid Build Coastguard Worker     // after we leave IBufferQueue_Clear().  This buffer will not be written
117*bebae9c0SAndroid Build Coastguard Worker     // into anymore, but it is possible that it will be returned via callback.
118*bebae9c0SAndroid Build Coastguard Worker     SL_LEAVE_INTERFACE
119*bebae9c0SAndroid Build Coastguard Worker }
120*bebae9c0SAndroid Build Coastguard Worker 
121*bebae9c0SAndroid Build Coastguard Worker 
IBufferQueue_GetState(SLBufferQueueItf self,SLBufferQueueState * pState)122*bebae9c0SAndroid Build Coastguard Worker static SLresult IBufferQueue_GetState(SLBufferQueueItf self, SLBufferQueueState *pState)
123*bebae9c0SAndroid Build Coastguard Worker {
124*bebae9c0SAndroid Build Coastguard Worker     SL_ENTER_INTERFACE
125*bebae9c0SAndroid Build Coastguard Worker 
126*bebae9c0SAndroid Build Coastguard Worker     // Note that GetState while a Clear is pending is equivalent to GetState before the Clear
127*bebae9c0SAndroid Build Coastguard Worker 
128*bebae9c0SAndroid Build Coastguard Worker     if (NULL == pState) {
129*bebae9c0SAndroid Build Coastguard Worker         result = SL_RESULT_PARAMETER_INVALID;
130*bebae9c0SAndroid Build Coastguard Worker     } else {
131*bebae9c0SAndroid Build Coastguard Worker         IBufferQueue *thiz = (IBufferQueue *) self;
132*bebae9c0SAndroid Build Coastguard Worker         SLBufferQueueState state;
133*bebae9c0SAndroid Build Coastguard Worker         interface_lock_shared(thiz);
134*bebae9c0SAndroid Build Coastguard Worker #ifdef __cplusplus // FIXME Is this a compiler bug?
135*bebae9c0SAndroid Build Coastguard Worker         state.count = thiz->mState.count;
136*bebae9c0SAndroid Build Coastguard Worker         state.playIndex = thiz->mState.playIndex;
137*bebae9c0SAndroid Build Coastguard Worker #else
138*bebae9c0SAndroid Build Coastguard Worker         state = thiz->mState;
139*bebae9c0SAndroid Build Coastguard Worker #endif
140*bebae9c0SAndroid Build Coastguard Worker         interface_unlock_shared(thiz);
141*bebae9c0SAndroid Build Coastguard Worker         *pState = state;
142*bebae9c0SAndroid Build Coastguard Worker         result = SL_RESULT_SUCCESS;
143*bebae9c0SAndroid Build Coastguard Worker     }
144*bebae9c0SAndroid Build Coastguard Worker 
145*bebae9c0SAndroid Build Coastguard Worker     SL_LEAVE_INTERFACE
146*bebae9c0SAndroid Build Coastguard Worker }
147*bebae9c0SAndroid Build Coastguard Worker 
148*bebae9c0SAndroid Build Coastguard Worker 
IBufferQueue_RegisterCallback(SLBufferQueueItf self,slBufferQueueCallback callback,void * pContext)149*bebae9c0SAndroid Build Coastguard Worker SLresult IBufferQueue_RegisterCallback(SLBufferQueueItf self,
150*bebae9c0SAndroid Build Coastguard Worker     slBufferQueueCallback callback, void *pContext)
151*bebae9c0SAndroid Build Coastguard Worker {
152*bebae9c0SAndroid Build Coastguard Worker     SL_ENTER_INTERFACE
153*bebae9c0SAndroid Build Coastguard Worker 
154*bebae9c0SAndroid Build Coastguard Worker     IBufferQueue *thiz = (IBufferQueue *) self;
155*bebae9c0SAndroid Build Coastguard Worker     interface_lock_exclusive(thiz);
156*bebae9c0SAndroid Build Coastguard Worker     // verify pre-condition that media object is in the SL_PLAYSTATE_STOPPED state
157*bebae9c0SAndroid Build Coastguard Worker     if (SL_PLAYSTATE_STOPPED == getAssociatedState(thiz)) {
158*bebae9c0SAndroid Build Coastguard Worker         thiz->mCallback = callback;
159*bebae9c0SAndroid Build Coastguard Worker         thiz->mContext = pContext;
160*bebae9c0SAndroid Build Coastguard Worker         result = SL_RESULT_SUCCESS;
161*bebae9c0SAndroid Build Coastguard Worker     } else {
162*bebae9c0SAndroid Build Coastguard Worker         result = SL_RESULT_PRECONDITIONS_VIOLATED;
163*bebae9c0SAndroid Build Coastguard Worker     }
164*bebae9c0SAndroid Build Coastguard Worker     interface_unlock_exclusive(thiz);
165*bebae9c0SAndroid Build Coastguard Worker 
166*bebae9c0SAndroid Build Coastguard Worker     SL_LEAVE_INTERFACE
167*bebae9c0SAndroid Build Coastguard Worker }
168*bebae9c0SAndroid Build Coastguard Worker 
169*bebae9c0SAndroid Build Coastguard Worker 
170*bebae9c0SAndroid Build Coastguard Worker static const struct SLBufferQueueItf_ IBufferQueue_Itf = {
171*bebae9c0SAndroid Build Coastguard Worker     IBufferQueue_Enqueue,
172*bebae9c0SAndroid Build Coastguard Worker     IBufferQueue_Clear,
173*bebae9c0SAndroid Build Coastguard Worker     IBufferQueue_GetState,
174*bebae9c0SAndroid Build Coastguard Worker     IBufferQueue_RegisterCallback
175*bebae9c0SAndroid Build Coastguard Worker };
176*bebae9c0SAndroid Build Coastguard Worker 
IBufferQueue_init(void * self)177*bebae9c0SAndroid Build Coastguard Worker void IBufferQueue_init(void *self)
178*bebae9c0SAndroid Build Coastguard Worker {
179*bebae9c0SAndroid Build Coastguard Worker     IBufferQueue *thiz = (IBufferQueue *) self;
180*bebae9c0SAndroid Build Coastguard Worker     thiz->mItf = &IBufferQueue_Itf;
181*bebae9c0SAndroid Build Coastguard Worker     thiz->mState.count = 0;
182*bebae9c0SAndroid Build Coastguard Worker     thiz->mState.playIndex = 0;
183*bebae9c0SAndroid Build Coastguard Worker     thiz->mCallback = NULL;
184*bebae9c0SAndroid Build Coastguard Worker     thiz->mContext = NULL;
185*bebae9c0SAndroid Build Coastguard Worker     thiz->mNumBuffers = 0;
186*bebae9c0SAndroid Build Coastguard Worker     thiz->mClearRequested = SL_BOOLEAN_FALSE;
187*bebae9c0SAndroid Build Coastguard Worker     thiz->mArray = NULL;
188*bebae9c0SAndroid Build Coastguard Worker     thiz->mFront = NULL;
189*bebae9c0SAndroid Build Coastguard Worker     thiz->mRear = NULL;
190*bebae9c0SAndroid Build Coastguard Worker #ifdef ANDROID
191*bebae9c0SAndroid Build Coastguard Worker     thiz->mSizeConsumed = 0;
192*bebae9c0SAndroid Build Coastguard Worker     thiz->mCallbackPending = false;
193*bebae9c0SAndroid Build Coastguard Worker #endif
194*bebae9c0SAndroid Build Coastguard Worker     BufferHeader *bufferHeader = thiz->mTypical;
195*bebae9c0SAndroid Build Coastguard Worker     unsigned i;
196*bebae9c0SAndroid Build Coastguard Worker     for (i = 0; i < BUFFER_HEADER_TYPICAL+1; ++i, ++bufferHeader) {
197*bebae9c0SAndroid Build Coastguard Worker         bufferHeader->mBuffer = NULL;
198*bebae9c0SAndroid Build Coastguard Worker         bufferHeader->mSize = 0;
199*bebae9c0SAndroid Build Coastguard Worker     }
200*bebae9c0SAndroid Build Coastguard Worker }
201*bebae9c0SAndroid Build Coastguard Worker 
202*bebae9c0SAndroid Build Coastguard Worker 
203*bebae9c0SAndroid Build Coastguard Worker /** \brief Interface deinitialization hook called by IObject::Destroy.
204*bebae9c0SAndroid Build Coastguard Worker  *  Free the buffer queue, if it was larger than typical.
205*bebae9c0SAndroid Build Coastguard Worker  */
206*bebae9c0SAndroid Build Coastguard Worker 
IBufferQueue_deinit(void * self)207*bebae9c0SAndroid Build Coastguard Worker void IBufferQueue_deinit(void *self)
208*bebae9c0SAndroid Build Coastguard Worker {
209*bebae9c0SAndroid Build Coastguard Worker     IBufferQueue *thiz = (IBufferQueue *) self;
210*bebae9c0SAndroid Build Coastguard Worker     if ((NULL != thiz->mArray) && (thiz->mArray != thiz->mTypical)) {
211*bebae9c0SAndroid Build Coastguard Worker         free(thiz->mArray);
212*bebae9c0SAndroid Build Coastguard Worker         thiz->mArray = NULL;
213*bebae9c0SAndroid Build Coastguard Worker     }
214*bebae9c0SAndroid Build Coastguard Worker }
215