blob: f195720d6363267bb51e659f5b2f31ebffecbeb1 [file] [log] [blame]
Glenn Kasten0b167262010-05-21 08:47:51 -07001/*
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/* BufferQueue implementation */
18
19#include "sles_allinclusive.h"
20
Glenn Kastened46c292010-07-02 15:58:46 -070021
Glenn Kasten4b65ef92010-07-16 17:37:20 -070022/** Determine the state of the audio player or audio recorder associated with a buffer queue.
23 * Note that PLAYSTATE and RECORDSTATE values are equivalent (where PLAYING == RECORDING).
24 */
25
Glenn Kastenbcc5c722011-01-18 11:44:36 -080026static SLuint32 getAssociatedState(IBufferQueue *thiz)
Glenn Kasten4b65ef92010-07-16 17:37:20 -070027{
28 SLuint32 state;
Glenn Kastenbcc5c722011-01-18 11:44:36 -080029 switch (InterfaceToObjectID(thiz)) {
Glenn Kasten4b65ef92010-07-16 17:37:20 -070030 case SL_OBJECTID_AUDIOPLAYER:
Glenn Kastenbcc5c722011-01-18 11:44:36 -080031 state = ((CAudioPlayer *) thiz->mThis)->mPlay.mState;
Glenn Kasten4b65ef92010-07-16 17:37:20 -070032 break;
33 case SL_OBJECTID_AUDIORECORDER:
Glenn Kastenbcc5c722011-01-18 11:44:36 -080034 state = ((CAudioRecorder *) thiz->mThis)->mRecord.mState;
Glenn Kasten4b65ef92010-07-16 17:37:20 -070035 break;
36 default:
37 // unreachable, but just in case we will assume it is stopped
38 assert(SL_BOOLEAN_FALSE);
39 state = SL_PLAYSTATE_STOPPED;
40 break;
41 }
42 return state;
43}
44
45
Glenn Kastene5d006b2010-08-13 13:57:26 -070046SLresult IBufferQueue_Enqueue(SLBufferQueueItf self, const void *pBuffer, SLuint32 size)
Glenn Kasten0b167262010-05-21 08:47:51 -070047{
Glenn Kastened46c292010-07-02 15:58:46 -070048 SL_ENTER_INTERFACE
Glenn Kastened46c292010-07-02 15:58:46 -070049
Glenn Kasten4b65ef92010-07-16 17:37:20 -070050 // Note that Enqueue while a Clear is pending is equivalent to Enqueue followed by Clear
51
Glenn Kastened46c292010-07-02 15:58:46 -070052 if (NULL == pBuffer || 0 == size) {
53 result = SL_RESULT_PARAMETER_INVALID;
Glenn Kasten0b167262010-05-21 08:47:51 -070054 } else {
Glenn Kastenbcc5c722011-01-18 11:44:36 -080055 IBufferQueue *thiz = (IBufferQueue *) self;
56 interface_lock_exclusive(thiz);
57 BufferHeader *oldRear = thiz->mRear, *newRear;
58 if ((newRear = oldRear + 1) == &thiz->mArray[thiz->mNumBuffers + 1]) {
59 newRear = thiz->mArray;
Glenn Kastena3080da2010-09-21 14:49:48 -070060 }
Glenn Kastenbcc5c722011-01-18 11:44:36 -080061 if (newRear == thiz->mFront) {
Glenn Kastened46c292010-07-02 15:58:46 -070062 result = SL_RESULT_BUFFER_INSUFFICIENT;
63 } else {
64 oldRear->mBuffer = pBuffer;
65 oldRear->mSize = size;
Glenn Kastenbcc5c722011-01-18 11:44:36 -080066 thiz->mRear = newRear;
67 ++thiz->mState.count;
Glenn Kastened46c292010-07-02 15:58:46 -070068 result = SL_RESULT_SUCCESS;
69 }
Glenn Kasten4b65ef92010-07-16 17:37:20 -070070 // set enqueue attribute if state is PLAYING and the first buffer is enqueued
Glenn Kastenbcc5c722011-01-18 11:44:36 -080071 interface_unlock_exclusive_attributes(thiz, ((SL_RESULT_SUCCESS == result) &&
72 (1 == thiz->mState.count) && (SL_PLAYSTATE_PLAYING == getAssociatedState(thiz))) ?
Jean-Michel Trivid158d312011-03-03 16:00:58 -080073 ATTR_BQ_ENQUEUE : ATTR_NONE);
Glenn Kasten0b167262010-05-21 08:47:51 -070074 }
Glenn Kastened46c292010-07-02 15:58:46 -070075 SL_LEAVE_INTERFACE
Glenn Kasten0b167262010-05-21 08:47:51 -070076}
77
Glenn Kastened46c292010-07-02 15:58:46 -070078
Glenn Kastene5d006b2010-08-13 13:57:26 -070079SLresult IBufferQueue_Clear(SLBufferQueueItf self)
Glenn Kasten0b167262010-05-21 08:47:51 -070080{
Glenn Kastened46c292010-07-02 15:58:46 -070081 SL_ENTER_INTERFACE
82
Glenn Kasten4b65ef92010-07-16 17:37:20 -070083 result = SL_RESULT_SUCCESS;
Glenn Kastenbcc5c722011-01-18 11:44:36 -080084 IBufferQueue *thiz = (IBufferQueue *) self;
85 interface_lock_exclusive(thiz);
Glenn Kasten4b65ef92010-07-16 17:37:20 -070086
Jean-Michel Trivief8931a2010-06-21 10:03:06 -070087#ifdef ANDROID
Glenn Kastenbcc5c722011-01-18 11:44:36 -080088 if (SL_OBJECTID_AUDIOPLAYER == InterfaceToObjectID(thiz)) {
89 CAudioPlayer *audioPlayer = (CAudioPlayer *) thiz->mThis;
Glenn Kasten4b65ef92010-07-16 17:37:20 -070090 // flush associated audio player
Jean-Michel Trivi0ac71cb2010-09-29 12:55:40 -070091 result = android_audioPlayer_bufferQueue_onClear(audioPlayer);
Andy Hungd7fd6882015-11-25 12:22:32 -080092 }
93 // flush our buffers
94 if (SL_RESULT_SUCCESS == result) {
95 thiz->mFront = &thiz->mArray[0];
96 thiz->mRear = &thiz->mArray[0];
97 thiz->mState.count = 0;
98 thiz->mState.playIndex = 0;
99 thiz->mSizeConsumed = 0;
100 thiz->mCallbackPending = false;
Glenn Kasten4b65ef92010-07-16 17:37:20 -0700101 }
Jean-Michel Trivi6a7bf772010-05-24 10:16:37 -0700102#endif
Glenn Kasten4b65ef92010-07-16 17:37:20 -0700103
104#ifdef USE_OUTPUTMIXEXT
105 // mixer might be reading from the front buffer, so tread carefully here
106 // NTH asynchronous cancel instead of blocking until mixer acknowledges
Glenn Kastenbcc5c722011-01-18 11:44:36 -0800107 thiz->mClearRequested = SL_BOOLEAN_TRUE;
Glenn Kastena3080da2010-09-21 14:49:48 -0700108 do {
Glenn Kastenbcc5c722011-01-18 11:44:36 -0800109 interface_cond_wait(thiz);
110 } while (thiz->mClearRequested);
Glenn Kasten4b65ef92010-07-16 17:37:20 -0700111#endif
112
Glenn Kastenbcc5c722011-01-18 11:44:36 -0800113 interface_unlock_exclusive(thiz);
Glenn Kastened46c292010-07-02 15:58:46 -0700114
Andy Hungd7fd6882015-11-25 12:22:32 -0800115 // there is a danger that a buffer is still held by the callback thread
116 // after we leave IBufferQueue_Clear(). This buffer will not be written
117 // into anymore, but it is possible that it will be returned via callback.
Glenn Kastened46c292010-07-02 15:58:46 -0700118 SL_LEAVE_INTERFACE
Glenn Kasten0b167262010-05-21 08:47:51 -0700119}
120
Glenn Kastened46c292010-07-02 15:58:46 -0700121
122static SLresult IBufferQueue_GetState(SLBufferQueueItf self, SLBufferQueueState *pState)
Glenn Kasten0b167262010-05-21 08:47:51 -0700123{
Glenn Kastened46c292010-07-02 15:58:46 -0700124 SL_ENTER_INTERFACE
125
Glenn Kasten4b65ef92010-07-16 17:37:20 -0700126 // Note that GetState while a Clear is pending is equivalent to GetState before the Clear
127
Glenn Kastened46c292010-07-02 15:58:46 -0700128 if (NULL == pState) {
129 result = SL_RESULT_PARAMETER_INVALID;
130 } else {
Glenn Kastenbcc5c722011-01-18 11:44:36 -0800131 IBufferQueue *thiz = (IBufferQueue *) self;
Glenn Kastened46c292010-07-02 15:58:46 -0700132 SLBufferQueueState state;
Glenn Kastenbcc5c722011-01-18 11:44:36 -0800133 interface_lock_shared(thiz);
Glenn Kasten0b167262010-05-21 08:47:51 -0700134#ifdef __cplusplus // FIXME Is this a compiler bug?
Glenn Kastenbcc5c722011-01-18 11:44:36 -0800135 state.count = thiz->mState.count;
136 state.playIndex = thiz->mState.playIndex;
Glenn Kasten0b167262010-05-21 08:47:51 -0700137#else
Glenn Kastenbcc5c722011-01-18 11:44:36 -0800138 state = thiz->mState;
Glenn Kasten0b167262010-05-21 08:47:51 -0700139#endif
Glenn Kastenbcc5c722011-01-18 11:44:36 -0800140 interface_unlock_shared(thiz);
Glenn Kastened46c292010-07-02 15:58:46 -0700141 *pState = state;
142 result = SL_RESULT_SUCCESS;
143 }
144
145 SL_LEAVE_INTERFACE
Glenn Kasten0b167262010-05-21 08:47:51 -0700146}
147
Glenn Kastened46c292010-07-02 15:58:46 -0700148
Glenn Kastene5d006b2010-08-13 13:57:26 -0700149SLresult IBufferQueue_RegisterCallback(SLBufferQueueItf self,
Glenn Kasten0b167262010-05-21 08:47:51 -0700150 slBufferQueueCallback callback, void *pContext)
151{
Glenn Kastened46c292010-07-02 15:58:46 -0700152 SL_ENTER_INTERFACE
153
Glenn Kastenbcc5c722011-01-18 11:44:36 -0800154 IBufferQueue *thiz = (IBufferQueue *) self;
155 interface_lock_exclusive(thiz);
Glenn Kasten4b65ef92010-07-16 17:37:20 -0700156 // verify pre-condition that media object is in the SL_PLAYSTATE_STOPPED state
Glenn Kastenbcc5c722011-01-18 11:44:36 -0800157 if (SL_PLAYSTATE_STOPPED == getAssociatedState(thiz)) {
158 thiz->mCallback = callback;
159 thiz->mContext = pContext;
Glenn Kasten4b65ef92010-07-16 17:37:20 -0700160 result = SL_RESULT_SUCCESS;
161 } else {
162 result = SL_RESULT_PRECONDITIONS_VIOLATED;
163 }
Glenn Kastenbcc5c722011-01-18 11:44:36 -0800164 interface_unlock_exclusive(thiz);
Glenn Kastened46c292010-07-02 15:58:46 -0700165
166 SL_LEAVE_INTERFACE
Glenn Kasten0b167262010-05-21 08:47:51 -0700167}
168
Glenn Kastened46c292010-07-02 15:58:46 -0700169
Glenn Kasten0b167262010-05-21 08:47:51 -0700170static const struct SLBufferQueueItf_ IBufferQueue_Itf = {
171 IBufferQueue_Enqueue,
172 IBufferQueue_Clear,
173 IBufferQueue_GetState,
174 IBufferQueue_RegisterCallback
175};
176
177void IBufferQueue_init(void *self)
178{
Glenn Kastenbcc5c722011-01-18 11:44:36 -0800179 IBufferQueue *thiz = (IBufferQueue *) self;
180 thiz->mItf = &IBufferQueue_Itf;
181 thiz->mState.count = 0;
182 thiz->mState.playIndex = 0;
183 thiz->mCallback = NULL;
184 thiz->mContext = NULL;
185 thiz->mNumBuffers = 0;
186 thiz->mClearRequested = SL_BOOLEAN_FALSE;
187 thiz->mArray = NULL;
188 thiz->mFront = NULL;
189 thiz->mRear = NULL;
Glenn Kasten4b65ef92010-07-16 17:37:20 -0700190#ifdef ANDROID
Glenn Kastenbcc5c722011-01-18 11:44:36 -0800191 thiz->mSizeConsumed = 0;
Raph Levien0f6da1a2014-11-14 13:51:32 -0800192 thiz->mCallbackPending = false;
Glenn Kasten4b65ef92010-07-16 17:37:20 -0700193#endif
Glenn Kastenbcc5c722011-01-18 11:44:36 -0800194 BufferHeader *bufferHeader = thiz->mTypical;
Glenn Kasten0b167262010-05-21 08:47:51 -0700195 unsigned i;
196 for (i = 0; i < BUFFER_HEADER_TYPICAL+1; ++i, ++bufferHeader) {
197 bufferHeader->mBuffer = NULL;
198 bufferHeader->mSize = 0;
199 }
Glenn Kasten0b167262010-05-21 08:47:51 -0700200}
Glenn Kastena3080da2010-09-21 14:49:48 -0700201
202
Glenn Kastena9a70a42010-10-05 07:46:26 -0700203/** \brief Interface deinitialization hook called by IObject::Destroy.
204 * Free the buffer queue, if it was larger than typical.
205 */
Glenn Kastena3080da2010-09-21 14:49:48 -0700206
Glenn Kastena9a70a42010-10-05 07:46:26 -0700207void IBufferQueue_deinit(void *self)
Glenn Kastena3080da2010-09-21 14:49:48 -0700208{
Glenn Kastenbcc5c722011-01-18 11:44:36 -0800209 IBufferQueue *thiz = (IBufferQueue *) self;
210 if ((NULL != thiz->mArray) && (thiz->mArray != thiz->mTypical)) {
211 free(thiz->mArray);
212 thiz->mArray = NULL;
Glenn Kastena3080da2010-09-21 14:49:48 -0700213 }
214}