android_StreamPlayer.cpp revision 18ddf4c7a41e6c9752fb2dc39c52f41916723255
13f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian/* 23f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian * Copyright (C) 2010 The Android Open Source Project 33f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian * 43f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian * Licensed under the Apache License, Version 2.0 (the "License"); 53f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian * you may not use this file except in compliance with the License. 63f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian * You may obtain a copy of the License at 73f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian * 83f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian * http://www.apache.org/licenses/LICENSE-2.0 93f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian * 103f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian * Unless required by applicable law or agreed to in writing, software 113f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian * distributed under the License is distributed on an "AS IS" BASIS, 123f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 133f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian * See the License for the specific language governing permissions and 143f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian * limitations under the License. 153f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian */ 163f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian 173f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian//#define USE_LOG SLAndroidLogLevel_Verbose 183f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian 193f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian#include "sles_allinclusive.h" 203f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian#include "android_StreamPlayer.h" 213f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian 223f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian#include <media/IStreamSource.h> 233f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian#include <media/IMediaPlayerService.h> 243f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian#include <media/stagefright/foundation/ADebug.h> 253f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian#include <binder/IPCThreadState.h> 263f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian 273f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian#include <ATSParser.h> 283f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian 293f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian//-------------------------------------------------------------------------------------------------- 3088d37ddac876c75dd5d2e39d33787dbcbf833641Romain Guynamespace android { 313f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian 323f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias AgopianStreamSourceAppProxy::StreamSourceAppProxy( 333f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian IAndroidBufferQueue *androidBufferQueue, 3488d37ddac876c75dd5d2e39d33787dbcbf833641Romain Guy const sp<CallbackProtector> &callbackProtector, 353f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian // sp<StreamPlayer> would cause StreamPlayer's destructor to run during it's own 363f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian // construction. If you pass in a sp<> to 'this' inside a constructor, then first the 3749457ac092071a8f964f7f69156093657ccdc3d0Mathias Agopian // refcount is increased from 0 to 1, then decreased from 1 to 0, which causes the object's 3849457ac092071a8f964f7f69156093657ccdc3d0Mathias Agopian // destructor to run from inside it's own constructor. 3949457ac092071a8f964f7f69156093657ccdc3d0Mathias Agopian StreamPlayer * /* const sp<StreamPlayer> & */ player) : 403f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian mBuffersHasBeenSet(false), 413f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian mAndroidBufferQueue(androidBufferQueue), 423f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian mCallbackProtector(callbackProtector), 4349457ac092071a8f964f7f69156093657ccdc3d0Mathias Agopian mPlayer(player) 443f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian{ 453f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian SL_LOGV("StreamSourceAppProxy::StreamSourceAppProxy()"); 4613fdc49516d17f41e64e62e73c313b0928bf13ccchaviw} 4713fdc49516d17f41e64e62e73c313b0928bf13ccchaviw 483f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias AgopianStreamSourceAppProxy::~StreamSourceAppProxy() { 493f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian SL_LOGV("StreamSourceAppProxy::~StreamSourceAppProxy()"); 50a8c386f1c36e916c1df18d41a22104d655a89817Mathias Agopian disconnect(); 51a8c386f1c36e916c1df18d41a22104d655a89817Mathias Agopian} 523f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian 533f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopianconst SLuint32 StreamSourceAppProxy::kItemProcessed[NB_BUFFEREVENT_ITEM_FIELDS] = { 5476dd77a6f5298bf0e7cb89ec570ab1578e3947fbPeiyong Lin SL_ANDROID_ITEMKEY_BUFFERQUEUEEVENT, // item key 5576dd77a6f5298bf0e7cb89ec570ab1578e3947fbPeiyong Lin sizeof(SLuint32), // item size 5676dd77a6f5298bf0e7cb89ec570ab1578e3947fbPeiyong Lin SL_ANDROIDBUFFERQUEUEEVENT_PROCESSED // item data 5776dd77a6f5298bf0e7cb89ec570ab1578e3947fbPeiyong Lin}; 58ff2ed70fa30f04b90dd1a2c06ec2319e157152d7Mathias Agopian 59ff2ed70fa30f04b90dd1a2c06ec2319e157152d7Mathias Agopian//-------------------------------------------------- 60a296b0c232483ff2d80a814a1a39d02397ba559ePeiyong Lin// IStreamSource implementation 61a296b0c232483ff2d80a814a1a39d02397ba559ePeiyong Linvoid StreamSourceAppProxy::setListener(const sp<IStreamListener> &listener) { 62a296b0c232483ff2d80a814a1a39d02397ba559ePeiyong Lin assert(listener != NULL); 63a296b0c232483ff2d80a814a1a39d02397ba559ePeiyong Lin Mutex::Autolock _l(mLock); 64a296b0c232483ff2d80a814a1a39d02397ba559ePeiyong Lin assert(mListener == NULL); 65a296b0c232483ff2d80a814a1a39d02397ba559ePeiyong Lin mListener = listener; 66a296b0c232483ff2d80a814a1a39d02397ba559ePeiyong Lin} 67a296b0c232483ff2d80a814a1a39d02397ba559ePeiyong Lin 68a296b0c232483ff2d80a814a1a39d02397ba559ePeiyong Linvoid StreamSourceAppProxy::setBuffers(const Vector<sp<IMemory> > &buffers) { 69a296b0c232483ff2d80a814a1a39d02397ba559ePeiyong Lin Mutex::Autolock _l(mLock); 70a296b0c232483ff2d80a814a1a39d02397ba559ePeiyong Lin assert(!mBuffersHasBeenSet); 71a296b0c232483ff2d80a814a1a39d02397ba559ePeiyong Lin mBuffers = buffers; 72a296b0c232483ff2d80a814a1a39d02397ba559ePeiyong Lin mBuffersHasBeenSet = true; 73a296b0c232483ff2d80a814a1a39d02397ba559ePeiyong Lin} 74a296b0c232483ff2d80a814a1a39d02397ba559ePeiyong Lin 75a296b0c232483ff2d80a814a1a39d02397ba559ePeiyong Linvoid StreamSourceAppProxy::onBufferAvailable(size_t index) { 76a296b0c232483ff2d80a814a1a39d02397ba559ePeiyong Lin //SL_LOGD("StreamSourceAppProxy::onBufferAvailable(%d)", index); 77a296b0c232483ff2d80a814a1a39d02397ba559ePeiyong Lin 78a296b0c232483ff2d80a814a1a39d02397ba559ePeiyong Lin { 79a296b0c232483ff2d80a814a1a39d02397ba559ePeiyong Lin Mutex::Autolock _l(mLock); 80a296b0c232483ff2d80a814a1a39d02397ba559ePeiyong Lin if (!mBuffersHasBeenSet) { 81a296b0c232483ff2d80a814a1a39d02397ba559ePeiyong Lin // no buffers available to push data to from the buffer queue, bail 82a296b0c232483ff2d80a814a1a39d02397ba559ePeiyong Lin return; 83ff2ed70fa30f04b90dd1a2c06ec2319e157152d7Mathias Agopian } 84ff2ed70fa30f04b90dd1a2c06ec2319e157152d7Mathias Agopian CHECK_LT(index, mBuffers.size()); 8576dd77a6f5298bf0e7cb89ec570ab1578e3947fbPeiyong Lin#if 0 // enable if needed for debugging 8676dd77a6f5298bf0e7cb89ec570ab1578e3947fbPeiyong Lin sp<IMemory> mem = mBuffers.itemAt(index); 8776dd77a6f5298bf0e7cb89ec570ab1578e3947fbPeiyong Lin SLAint64 length = (SLAint64) mem->size(); 8876dd77a6f5298bf0e7cb89ec570ab1578e3947fbPeiyong Lin#endif 8976dd77a6f5298bf0e7cb89ec570ab1578e3947fbPeiyong Lin mAvailableBuffers.push_back(index); 90f008799d3753e52c10849824ff8146985ea66284Dan Stoza //SL_LOGD("onBufferAvailable() now %d buffers available in queue", 91f008799d3753e52c10849824ff8146985ea66284Dan Stoza // mAvailableBuffers.size()); 92f008799d3753e52c10849824ff8146985ea66284Dan Stoza } 93f008799d3753e52c10849824ff8146985ea66284Dan Stoza 94131d3760a0437fac08ff4cc384640b7ca1802d17Chia-I Wu // a new shared mem buffer is available: let's try to fill immediately 95131d3760a0437fac08ff4cc384640b7ca1802d17Chia-I Wu pullFromBuffQueue(); 96131d3760a0437fac08ff4cc384640b7ca1802d17Chia-I Wu} 97131d3760a0437fac08ff4cc384640b7ca1802d17Chia-I Wu 987e65bc00e46fbae2820fa52c997d72ab69ecd916Chia-I Wuvoid StreamSourceAppProxy::receivedCmd_l(IStreamListener::Command cmd, const sp<AMessage> &msg) { 997e65bc00e46fbae2820fa52c997d72ab69ecd916Chia-I Wu if (mListener != 0) { 1007e65bc00e46fbae2820fa52c997d72ab69ecd916Chia-I Wu mListener->issueCommand(cmd, false /* synchronous */, msg); 1017e65bc00e46fbae2820fa52c997d72ab69ecd916Chia-I Wu } 1027e65bc00e46fbae2820fa52c997d72ab69ecd916Chia-I Wu} 1037e65bc00e46fbae2820fa52c997d72ab69ecd916Chia-I Wu 10488d37ddac876c75dd5d2e39d33787dbcbf833641Romain Guyvoid StreamSourceAppProxy::receivedBuffer_l(size_t buffIndex, size_t buffLength) { 105ff2ed70fa30f04b90dd1a2c06ec2319e157152d7Mathias Agopian if (mListener != 0) { 106fb069305e90947aeb76b72527f23aa24564f3c87Peiyong Lin mListener->queueBuffer(buffIndex, buffLength); 107fb069305e90947aeb76b72527f23aa24564f3c87Peiyong Lin } 108fb069305e90947aeb76b72527f23aa24564f3c87Peiyong Lin} 109fb069305e90947aeb76b72527f23aa24564f3c87Peiyong Lin 1103f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopianvoid StreamSourceAppProxy::disconnect() { 111 Mutex::Autolock _l(mLock); 112 mListener.clear(); 113 // Force binder to push the decremented reference count for sp<IStreamListener>. 114 // mediaserver and client both have sp<> to the other. When you decrement an sp<> 115 // reference count, binder doesn't push that to the other process immediately. 116 IPCThreadState::self()->flushCommands(); 117 mBuffers.clear(); 118 mBuffersHasBeenSet = false; 119 mAvailableBuffers.clear(); 120} 121 122//-------------------------------------------------- 123// consumption from ABQ: pull from the ABQ, and push to shared memory (media server) 124void StreamSourceAppProxy::pullFromBuffQueue() { 125 126 if (android::CallbackProtector::enterCbIfOk(mCallbackProtector)) { 127 128 size_t bufferId; 129 void* bufferLoc; 130 size_t buffSize; 131 132 slAndroidBufferQueueCallback callback = NULL; 133 void* pBufferContext, *pBufferData, *callbackPContext = NULL; 134 AdvancedBufferHeader *oldFront = NULL; 135 uint32_t dataSize /* , dataUsed */; 136 137 // retrieve data from the buffer queue 138 interface_lock_exclusive(mAndroidBufferQueue); 139 140 // can this read operation cause us to call the buffer queue callback 141 // (either because there was a command with no data, or all the data has been consumed) 142 bool queueCallbackCandidate = false; 143 144 if (mAndroidBufferQueue->mState.count != 0) { 145 // SL_LOGD("nbBuffers in ABQ = %u, buffSize=%u",abq->mState.count, buffSize); 146 assert(mAndroidBufferQueue->mFront != mAndroidBufferQueue->mRear); 147 148 oldFront = mAndroidBufferQueue->mFront; 149 AdvancedBufferHeader *newFront = &oldFront[1]; 150 151 // consume events when starting to read data from a buffer for the first time 152 if (oldFront->mDataSizeConsumed == 0) { 153 // note this code assumes at most one event per buffer; see IAndroidBufferQueue_Enqueue 154 if (oldFront->mItems.mTsCmdData.mTsCmdCode & ANDROID_MP2TSEVENT_EOS) { 155 receivedCmd_l(IStreamListener::EOS); 156 // EOS has no associated data 157 queueCallbackCandidate = true; 158 } else if (oldFront->mItems.mTsCmdData.mTsCmdCode & ANDROID_MP2TSEVENT_DISCONTINUITY) { 159 receivedCmd_l(IStreamListener::DISCONTINUITY); 160 } else if (oldFront->mItems.mTsCmdData.mTsCmdCode & ANDROID_MP2TSEVENT_DISCON_NEWPTS) { 161 sp<AMessage> msg = new AMessage(); 162 msg->setInt64(IStreamListener::kKeyResumeAtPTS, 163 (int64_t)oldFront->mItems.mTsCmdData.mPts); 164 receivedCmd_l(IStreamListener::DISCONTINUITY, msg /*msg*/); 165 } else if (oldFront->mItems.mTsCmdData.mTsCmdCode 166 & ANDROID_MP2TSEVENT_FORMAT_CHANGE_FULL) { 167 sp<AMessage> msg = new AMessage(); 168 msg->setInt32( 169 IStreamListener::kKeyDiscontinuityMask, 170 ATSParser::DISCONTINUITY_FORMATCHANGE); 171 receivedCmd_l(IStreamListener::DISCONTINUITY, msg /*msg*/); 172 } else if (oldFront->mItems.mTsCmdData.mTsCmdCode 173 & ANDROID_MP2TSEVENT_FORMAT_CHANGE_VIDEO) { 174 sp<AMessage> msg = new AMessage(); 175 msg->setInt32( 176 IStreamListener::kKeyDiscontinuityMask, 177 ATSParser::DISCONTINUITY_VIDEO_FORMAT); 178 receivedCmd_l(IStreamListener::DISCONTINUITY, msg /*msg*/); 179 } 180 // note that here we are intentionally only supporting 181 // ANDROID_MP2TSEVENT_FORMAT_CHANGE_VIDEO, see IAndroidBufferQueue.c 182 183 // some commands may introduce a time discontinuity, reevaluate position if needed 184 if (oldFront->mItems.mTsCmdData.mTsCmdCode & (ANDROID_MP2TSEVENT_DISCONTINUITY | 185 ANDROID_MP2TSEVENT_DISCON_NEWPTS | ANDROID_MP2TSEVENT_FORMAT_CHANGE_FULL)) { 186 const sp<StreamPlayer> player(mPlayer.promote()); 187 if (player != NULL) { 188 // FIXME see note at onSeek 189 player->seek(ANDROID_UNKNOWN_TIME); 190 } 191 } 192 oldFront->mItems.mTsCmdData.mTsCmdCode = ANDROID_MP2TSEVENT_NONE; 193 } 194 195 { 196 // we're going to change the shared mem buffer queue, so lock it 197 Mutex::Autolock _l(mLock); 198 if (!mAvailableBuffers.empty()) { 199 bufferId = *mAvailableBuffers.begin(); 200 CHECK_LT(bufferId, mBuffers.size()); 201 sp<IMemory> mem = mBuffers.itemAt(bufferId); 202 bufferLoc = mem->pointer(); 203 buffSize = mem->size(); 204 205 char *pSrc = ((char*)oldFront->mDataBuffer) + oldFront->mDataSizeConsumed; 206 if (oldFront->mDataSizeConsumed + buffSize < oldFront->mDataSize) { 207 // more available than requested, copy as much as requested 208 // consume data: 1/ copy to given destination 209 memcpy(bufferLoc, pSrc, buffSize); 210 // 2/ keep track of how much has been consumed 211 oldFront->mDataSizeConsumed += buffSize; 212 // 3/ notify shared mem listener that new data is available 213 receivedBuffer_l(bufferId, buffSize); 214 mAvailableBuffers.erase(mAvailableBuffers.begin()); 215 } else { 216 // requested as much available or more: consume the whole of the current 217 // buffer and move to the next 218 size_t consumed = oldFront->mDataSize - oldFront->mDataSizeConsumed; 219 //SL_LOGD("consuming rest of buffer: enqueueing=%u", consumed); 220 oldFront->mDataSizeConsumed = oldFront->mDataSize; 221 222 // move queue to next 223 if (newFront == &mAndroidBufferQueue-> 224 mBufferArray[mAndroidBufferQueue->mNumBuffers + 1]) { 225 // reached the end, circle back 226 newFront = mAndroidBufferQueue->mBufferArray; 227 } 228 mAndroidBufferQueue->mFront = newFront; 229 mAndroidBufferQueue->mState.count--; 230 mAndroidBufferQueue->mState.index++; 231 232 if (consumed > 0) { 233 // consume data: 1/ copy to given destination 234 memcpy(bufferLoc, pSrc, consumed); 235 // 2/ keep track of how much has been consumed 236 // here nothing to do because we are done with this buffer 237 // 3/ notify StreamPlayer that new data is available 238 receivedBuffer_l(bufferId, consumed); 239 mAvailableBuffers.erase(mAvailableBuffers.begin()); 240 } 241 242 // data has been consumed, and the buffer queue state has been updated 243 // we will notify the client if applicable 244 queueCallbackCandidate = true; 245 } 246 } 247 248 if (queueCallbackCandidate) { 249 if (mAndroidBufferQueue->mCallbackEventsMask & 250 SL_ANDROIDBUFFERQUEUEEVENT_PROCESSED) { 251 callback = mAndroidBufferQueue->mCallback; 252 // save callback data while under lock 253 callbackPContext = mAndroidBufferQueue->mContext; 254 pBufferContext = (void *)oldFront->mBufferContext; 255 pBufferData = (void *)oldFront->mDataBuffer; 256 dataSize = oldFront->mDataSize; 257 // here a buffer is only dequeued when fully consumed 258 //dataUsed = oldFront->mDataSizeConsumed; 259 } 260 } 261 //SL_LOGD("%d buffers available after reading from queue", mAvailableBuffers.size()); 262 if (!mAvailableBuffers.empty()) { 263 // there is still room in the shared memory, recheck later if we can pull 264 // data from the buffer queue and write it to shared memory 265 const sp<StreamPlayer> player(mPlayer.promote()); 266 if (player != NULL) { 267 player->queueRefilled(); 268 } 269 } 270 } 271 272 } else { // empty queue 273 SL_LOGD("ABQ empty, starving!"); 274 } 275 276 interface_unlock_exclusive(mAndroidBufferQueue); 277 278 // notify client of buffer processed 279 if (NULL != callback) { 280 SLresult result = (*callback)(&mAndroidBufferQueue->mItf, callbackPContext, 281 pBufferContext, pBufferData, dataSize, 282 dataSize, /* dataUsed */ 283 // no messages during playback other than marking the buffer as processed 284 (const SLAndroidBufferItem*)(&kItemProcessed) /* pItems */, 285 NB_BUFFEREVENT_ITEM_FIELDS *sizeof(SLuint32) /* itemsLength */ ); 286 if (SL_RESULT_SUCCESS != result) { 287 // Reserved for future use 288 SL_LOGW("Unsuccessful result %d returned from AndroidBufferQueueCallback", result); 289 } 290 } 291 292 mCallbackProtector->exitCb(); 293 } // enterCbIfOk 294} 295 296 297//-------------------------------------------------------------------------------------------------- 298StreamPlayer::StreamPlayer(const AudioPlayback_Parameters* params, bool hasVideo, 299 IAndroidBufferQueue *androidBufferQueue, const sp<CallbackProtector> &callbackProtector) : 300 GenericMediaPlayer(params, hasVideo), 301 mAppProxy(new StreamSourceAppProxy(androidBufferQueue, callbackProtector, this)), 302 mStopForDestroyCompleted(false) 303{ 304 SL_LOGD("StreamPlayer::StreamPlayer()"); 305} 306 307StreamPlayer::~StreamPlayer() { 308 SL_LOGD("StreamPlayer::~StreamPlayer()"); 309 mAppProxy->disconnect(); 310} 311 312 313void StreamPlayer::onMessageReceived(const sp<AMessage> &msg) { 314 switch (msg->what()) { 315 case kWhatPullFromAbq: 316 onPullFromAndroidBufferQueue(); 317 break; 318 319 case kWhatStopForDestroy: 320 onStopForDestroy(); 321 break; 322 323 default: 324 GenericMediaPlayer::onMessageReceived(msg); 325 break; 326 } 327} 328 329 330void StreamPlayer::preDestroy() { 331 // FIXME NuPlayerDriver is currently not thread-safe, so stop() must be called by looper 332 (new AMessage(kWhatStopForDestroy, this))->post(); 333 { 334 Mutex::Autolock _l(mStopForDestroyLock); 335 while (!mStopForDestroyCompleted) { 336 mStopForDestroyCondition.wait(mStopForDestroyLock); 337 } 338 } 339 // GenericMediaPlayer::preDestroy will repeat some of what we've done, but that's benign 340 GenericMediaPlayer::preDestroy(); 341} 342 343 344void StreamPlayer::onStopForDestroy() { 345 if (mPlayer != 0) { 346 mPlayer->stop(); 347 // causes CHECK failure in Nuplayer 348 //mPlayer->setDataSource(NULL); 349 mPlayer->setVideoSurfaceTexture(NULL); 350 mPlayer->disconnect(); 351 mPlayer.clear(); 352 { 353 // FIXME ugh make this a method 354 Mutex::Autolock _l(mPreparedPlayerLock); 355 mPreparedPlayer.clear(); 356 } 357 } 358 { 359 Mutex::Autolock _l(mStopForDestroyLock); 360 mStopForDestroyCompleted = true; 361 } 362 mStopForDestroyCondition.signal(); 363} 364 365 366/** 367 * Asynchronously notify the player that the queue is ready to be pulled from. 368 */ 369void StreamPlayer::queueRefilled() { 370 // async notification that the ABQ was refilled: the player should pull from the ABQ, and 371 // and push to shared memory (to the media server) 372 (new AMessage(kWhatPullFromAbq, this))->post(); 373} 374 375 376void StreamPlayer::appClear_l() { 377 // the user of StreamPlayer has cleared its AndroidBufferQueue: 378 // there's no clear() for the shared memory queue, so this is a no-op 379} 380 381 382//-------------------------------------------------- 383// Event handlers 384void StreamPlayer::onPrepare() { 385 SL_LOGD("StreamPlayer::onPrepare()"); 386 sp<IMediaPlayerService> mediaPlayerService(getMediaPlayerService()); 387 if (mediaPlayerService != NULL) { 388 mPlayer = mediaPlayerService->create(mPlayerClient /*IMediaPlayerClient*/, 389 mPlaybackParams.sessionId); 390 if (mPlayer == NULL) { 391 SL_LOGE("media player service failed to create player by app proxy"); 392 } else if (mPlayer->setDataSource(static_cast<sp<IStreamSource>>(mAppProxy)) != 393 NO_ERROR) { 394 SL_LOGE("setDataSource failed"); 395 mPlayer.clear(); 396 } 397 } 398 if (mPlayer == NULL) { 399 mStateFlags |= kFlagPreparedUnsuccessfully; 400 } 401 GenericMediaPlayer::onPrepare(); 402 SL_LOGD("StreamPlayer::onPrepare() done"); 403} 404 405 406void StreamPlayer::onPlay() { 407 SL_LOGD("StreamPlayer::onPlay()"); 408 // enqueue a message that will cause StreamAppProxy to consume from the queue (again if the 409 // player had starved the shared memory) 410 queueRefilled(); 411 412 GenericMediaPlayer::onPlay(); 413} 414 415 416void StreamPlayer::onPullFromAndroidBufferQueue() { 417 SL_LOGD("StreamPlayer::onPullFromAndroidBufferQueue()"); 418 mAppProxy->pullFromBuffQueue(); 419} 420 421} // namespace android 422