The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 23 | namespace android { |
| 24 | |
Dave Sparks | f6e43bf | 2009-12-08 08:10:42 -0800 | [diff] [blame] | 25 | void SoundPoolThread::write(SoundPoolMsg msg) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | Mutex::Autolock lock(&mLock); |
Dave Sparks | f6e43bf | 2009-12-08 08:10:42 -0800 | [diff] [blame] | 27 | while (mMsgQueue.size() >= maxMessages) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | mCondition.wait(mLock); |
| 29 | } |
Dave Sparks | f6e43bf | 2009-12-08 08:10:42 -0800 | [diff] [blame] | 30 | |
| 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | } |
| 37 | |
Dave Sparks | f6e43bf | 2009-12-08 08:10:42 -0800 | [diff] [blame] | 38 | const SoundPoolMsg SoundPoolThread::read() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | Mutex::Autolock lock(&mLock); |
Dave Sparks | f6e43bf | 2009-12-08 08:10:42 -0800 | [diff] [blame] | 40 | while (mMsgQueue.size() == 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | mCondition.wait(mLock); |
| 42 | } |
Dave Sparks | f6e43bf | 2009-12-08 08:10:42 -0800 | [diff] [blame] | 43 | SoundPoolMsg msg = mMsgQueue[0]; |
| 44 | mMsgQueue.removeAt(0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | mCondition.signal(); |
| 46 | return msg; |
| 47 | } |
| 48 | |
Dave Sparks | f6e43bf | 2009-12-08 08:10:42 -0800 | [diff] [blame] | 49 | void SoundPoolThread::quit() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 50 | Mutex::Autolock lock(&mLock); |
Dave Sparks | f6e43bf | 2009-12-08 08:10:42 -0800 | [diff] [blame] | 51 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 58 | LOGV("return from quit"); |
| 59 | } |
| 60 | |
| 61 | SoundPoolThread::SoundPoolThread(SoundPool* soundPool) : |
| 62 | mSoundPool(soundPool) |
| 63 | { |
Dave Sparks | f6e43bf | 2009-12-08 08:10:42 -0800 | [diff] [blame] | 64 | mMsgQueue.setCapacity(maxMessages); |
Steve Howard | 09468db | 2010-03-11 13:51:52 -0800 | [diff] [blame] | 65 | if (createThreadEtc(beginThread, this, "SoundPoolThread")) { |
Dave Sparks | f6e43bf | 2009-12-08 08:10:42 -0800 | [diff] [blame] | 66 | mRunning = true; |
| 67 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | SoundPoolThread::~SoundPoolThread() |
| 71 | { |
Dave Sparks | f6e43bf | 2009-12-08 08:10:42 -0800 | [diff] [blame] | 72 | quit(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | int SoundPoolThread::beginThread(void* arg) { |
| 76 | LOGV("beginThread"); |
| 77 | SoundPoolThread* soundPoolThread = (SoundPoolThread*)arg; |
| 78 | return soundPoolThread->run(); |
| 79 | } |
| 80 | |
| 81 | int SoundPoolThread::run() { |
| 82 | LOGV("run"); |
| 83 | for (;;) { |
Dave Sparks | f6e43bf | 2009-12-08 08:10:42 -0800 | [diff] [blame] | 84 | SoundPoolMsg msg = read(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 85 | 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 | |
| 101 | void SoundPoolThread::loadSample(int sampleID) { |
Dave Sparks | f6e43bf | 2009-12-08 08:10:42 -0800 | [diff] [blame] | 102 | write(SoundPoolMsg(SoundPoolMsg::LOAD_SAMPLE, sampleID)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | void SoundPoolThread::doLoadSample(int sampleID) { |
| 106 | sp <Sample> sample = mSoundPool->findSample(sampleID); |
Dave Sparks | f6e43bf | 2009-12-08 08:10:42 -0800 | [diff] [blame] | 107 | status_t status = -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 108 | if (sample != 0) { |
Dave Sparks | f6e43bf | 2009-12-08 08:10:42 -0800 | [diff] [blame] | 109 | status = sample->doLoad(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 110 | } |
Dave Sparks | f6e43bf | 2009-12-08 08:10:42 -0800 | [diff] [blame] | 111 | mSoundPool->notify(SoundPoolEvent(SoundPoolEvent::SAMPLE_LOADED, sampleID, status)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | } // end namespace android |