blob: 275c51940a5859ea514c034f9a44adf5a0a977ca [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 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 LOG_NDEBUG 0
18#define LOG_TAG "SoundPoolThread"
19#include "utils/Log.h"
20
21#include "SoundPoolThread.h"
22
23namespace android {
24
Dave Sparksf6e43bf2009-12-08 08:10:42 -080025void SoundPoolThread::write(SoundPoolMsg msg) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026 Mutex::Autolock lock(&mLock);
Dave Sparksf6e43bf2009-12-08 08:10:42 -080027 while (mMsgQueue.size() >= maxMessages) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028 mCondition.wait(mLock);
29 }
Dave Sparksf6e43bf2009-12-08 08:10:42 -080030
31 // if thread is quitting, don't add to queue
32 if (mRunning) {
33 mMsgQueue.push(msg);
34 mCondition.signal();
35 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036}
37
Dave Sparksf6e43bf2009-12-08 08:10:42 -080038const SoundPoolMsg SoundPoolThread::read() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039 Mutex::Autolock lock(&mLock);
Dave Sparksf6e43bf2009-12-08 08:10:42 -080040 while (mMsgQueue.size() == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041 mCondition.wait(mLock);
42 }
Dave Sparksf6e43bf2009-12-08 08:10:42 -080043 SoundPoolMsg msg = mMsgQueue[0];
44 mMsgQueue.removeAt(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045 mCondition.signal();
46 return msg;
47}
48
Dave Sparksf6e43bf2009-12-08 08:10:42 -080049void SoundPoolThread::quit() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050 Mutex::Autolock lock(&mLock);
Dave Sparksf6e43bf2009-12-08 08:10:42 -080051 if (mRunning) {
52 mRunning = false;
53 mMsgQueue.clear();
54 mMsgQueue.push(SoundPoolMsg(SoundPoolMsg::KILL, 0));
55 mCondition.signal();
56 mCondition.wait(mLock);
57 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058 LOGV("return from quit");
59}
60
61SoundPoolThread::SoundPoolThread(SoundPool* soundPool) :
62 mSoundPool(soundPool)
63{
Dave Sparksf6e43bf2009-12-08 08:10:42 -080064 mMsgQueue.setCapacity(maxMessages);
Steve Howard09468db2010-03-11 13:51:52 -080065 if (createThreadEtc(beginThread, this, "SoundPoolThread")) {
Dave Sparksf6e43bf2009-12-08 08:10:42 -080066 mRunning = true;
67 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068}
69
70SoundPoolThread::~SoundPoolThread()
71{
Dave Sparksf6e43bf2009-12-08 08:10:42 -080072 quit();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073}
74
75int SoundPoolThread::beginThread(void* arg) {
76 LOGV("beginThread");
77 SoundPoolThread* soundPoolThread = (SoundPoolThread*)arg;
78 return soundPoolThread->run();
79}
80
81int SoundPoolThread::run() {
82 LOGV("run");
83 for (;;) {
Dave Sparksf6e43bf2009-12-08 08:10:42 -080084 SoundPoolMsg msg = read();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085 LOGV("Got message m=%d, mData=%d", msg.mMessageType, msg.mData);
86 switch (msg.mMessageType) {
87 case SoundPoolMsg::KILL:
88 LOGV("goodbye");
89 return NO_ERROR;
90 case SoundPoolMsg::LOAD_SAMPLE:
91 doLoadSample(msg.mData);
92 break;
93 default:
94 LOGW("run: Unrecognized message %d\n",
95 msg.mMessageType);
96 break;
97 }
98 }
99}
100
101void SoundPoolThread::loadSample(int sampleID) {
Dave Sparksf6e43bf2009-12-08 08:10:42 -0800102 write(SoundPoolMsg(SoundPoolMsg::LOAD_SAMPLE, sampleID));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103}
104
105void SoundPoolThread::doLoadSample(int sampleID) {
106 sp <Sample> sample = mSoundPool->findSample(sampleID);
Dave Sparksf6e43bf2009-12-08 08:10:42 -0800107 status_t status = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108 if (sample != 0) {
Dave Sparksf6e43bf2009-12-08 08:10:42 -0800109 status = sample->doLoad();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110 }
Dave Sparksf6e43bf2009-12-08 08:10:42 -0800111 mSoundPool->notify(SoundPoolEvent(SoundPoolEvent::SAMPLE_LOADED, sampleID, status));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112}
113
114} // end namespace android