summaryrefslogtreecommitdiffstats
path: root/media/jni/soundpool/SoundPoolThread.cpp
diff options
context:
space:
mode:
authorDave Sparks <davidsparks@android.com>2009-12-08 08:10:42 -0800
committerDave Sparks <davidsparks@android.com>2009-12-23 09:11:29 -0800
commitf6e43bf29084760b39257b2273e0f04c2815fdc5 (patch)
treef9690ab4d0caeb527bf13392f6a88e4002a1a1cb /media/jni/soundpool/SoundPoolThread.cpp
parentfddd8f96f34b622cbdd0d891f8a7f5b7e18fdbf8 (diff)
downloadframeworks_base-f6e43bf29084760b39257b2273e0f04c2815fdc5.zip
frameworks_base-f6e43bf29084760b39257b2273e0f04c2815fdc5.tar.gz
frameworks_base-f6e43bf29084760b39257b2273e0f04c2815fdc5.tar.bz2
Add OnLoadCompleteListener to SoundPool.
Diffstat (limited to 'media/jni/soundpool/SoundPoolThread.cpp')
-rw-r--r--media/jni/soundpool/SoundPoolThread.cpp54
1 files changed, 30 insertions, 24 deletions
diff --git a/media/jni/soundpool/SoundPoolThread.cpp b/media/jni/soundpool/SoundPoolThread.cpp
index 4e6798d..e32c794 100644
--- a/media/jni/soundpool/SoundPoolThread.cpp
+++ b/media/jni/soundpool/SoundPoolThread.cpp
@@ -22,50 +22,54 @@
namespace android {
-void SoundPoolThread::MessageQueue::write(SoundPoolMsg msg) {
- LOGV("MessageQueue::write - acquiring lock\n");
+void SoundPoolThread::write(SoundPoolMsg msg) {
Mutex::Autolock lock(&mLock);
- while (mQueue.size() >= maxMessages) {
- LOGV("MessageQueue::write - wait\n");
+ while (mMsgQueue.size() >= maxMessages) {
mCondition.wait(mLock);
}
- LOGV("MessageQueue::write - push message\n");
- mQueue.push(msg);
- mCondition.signal();
+
+ // if thread is quitting, don't add to queue
+ if (mRunning) {
+ mMsgQueue.push(msg);
+ mCondition.signal();
+ }
}
-const SoundPoolMsg SoundPoolThread::MessageQueue::read() {
- LOGV("MessageQueue::read - acquiring lock\n");
+const SoundPoolMsg SoundPoolThread::read() {
Mutex::Autolock lock(&mLock);
- while (mQueue.size() == 0) {
- LOGV("MessageQueue::read - wait\n");
+ while (mMsgQueue.size() == 0) {
mCondition.wait(mLock);
}
- SoundPoolMsg msg = mQueue[0];
- LOGV("MessageQueue::read - retrieve message\n");
- mQueue.removeAt(0);
+ SoundPoolMsg msg = mMsgQueue[0];
+ mMsgQueue.removeAt(0);
mCondition.signal();
return msg;
}
-void SoundPoolThread::MessageQueue::quit() {
+void SoundPoolThread::quit() {
Mutex::Autolock lock(&mLock);
- mQueue.clear();
- mQueue.push(SoundPoolMsg(SoundPoolMsg::KILL, 0));
- mCondition.signal();
- mCondition.wait(mLock);
+ if (mRunning) {
+ mRunning = false;
+ mMsgQueue.clear();
+ mMsgQueue.push(SoundPoolMsg(SoundPoolMsg::KILL, 0));
+ mCondition.signal();
+ mCondition.wait(mLock);
+ }
LOGV("return from quit");
}
SoundPoolThread::SoundPoolThread(SoundPool* soundPool) :
mSoundPool(soundPool)
{
- mMessages.setCapacity(maxMessages);
- createThread(beginThread, this);
+ mMsgQueue.setCapacity(maxMessages);
+ if (createThread(beginThread, this)) {
+ mRunning = true;
+ }
}
SoundPoolThread::~SoundPoolThread()
{
+ quit();
}
int SoundPoolThread::beginThread(void* arg) {
@@ -77,7 +81,7 @@ int SoundPoolThread::beginThread(void* arg) {
int SoundPoolThread::run() {
LOGV("run");
for (;;) {
- SoundPoolMsg msg = mMessages.read();
+ SoundPoolMsg msg = read();
LOGV("Got message m=%d, mData=%d", msg.mMessageType, msg.mData);
switch (msg.mMessageType) {
case SoundPoolMsg::KILL:
@@ -95,14 +99,16 @@ int SoundPoolThread::run() {
}
void SoundPoolThread::loadSample(int sampleID) {
- mMessages.write(SoundPoolMsg(SoundPoolMsg::LOAD_SAMPLE, sampleID));
+ write(SoundPoolMsg(SoundPoolMsg::LOAD_SAMPLE, sampleID));
}
void SoundPoolThread::doLoadSample(int sampleID) {
sp <Sample> sample = mSoundPool->findSample(sampleID);
+ status_t status = -1;
if (sample != 0) {
- sample->doLoad();
+ status = sample->doLoad();
}
+ mSoundPool->notify(SoundPoolEvent(SoundPoolEvent::SAMPLE_LOADED, sampleID, status));
}
} // end namespace android