Marco Nelissen | 372be89 | 2014-12-04 08:59:22 -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 | |
Marco Nelissen | 7201d9c | 2017-05-31 13:52:21 -0700 | [diff] [blame] | 23 | static const int kMaxWorkers = 3; |
| 24 | |
Marco Nelissen | 372be89 | 2014-12-04 08:59:22 -0800 | [diff] [blame] | 25 | namespace android { |
| 26 | |
| 27 | void SoundPoolThread::write(SoundPoolMsg msg) { |
| 28 | Mutex::Autolock lock(&mLock); |
| 29 | while (mMsgQueue.size() >= maxMessages) { |
| 30 | mCondition.wait(mLock); |
| 31 | } |
| 32 | |
| 33 | // if thread is quitting, don't add to queue |
| 34 | if (mRunning) { |
| 35 | mMsgQueue.push(msg); |
Marco Nelissen | 7201d9c | 2017-05-31 13:52:21 -0700 | [diff] [blame] | 36 | if (mNumWorkers < kMaxWorkers) { |
| 37 | if (createThreadEtc(beginThread, this, "SoundPoolThread")) { |
| 38 | mNumWorkers++; |
| 39 | ALOGV("created worker thread"); |
| 40 | } |
| 41 | } |
Marco Nelissen | 372be89 | 2014-12-04 08:59:22 -0800 | [diff] [blame] | 42 | } |
| 43 | } |
| 44 | |
| 45 | const SoundPoolMsg SoundPoolThread::read() { |
| 46 | Mutex::Autolock lock(&mLock); |
Marco Nelissen | 7201d9c | 2017-05-31 13:52:21 -0700 | [diff] [blame] | 47 | if (mMsgQueue.size() == 0) { |
| 48 | mNumWorkers--; |
| 49 | mCondition.signal(); |
| 50 | return SoundPoolMsg(SoundPoolMsg::KILL, 0); |
Marco Nelissen | 372be89 | 2014-12-04 08:59:22 -0800 | [diff] [blame] | 51 | } |
| 52 | SoundPoolMsg msg = mMsgQueue[0]; |
| 53 | mMsgQueue.removeAt(0); |
| 54 | mCondition.signal(); |
| 55 | return msg; |
| 56 | } |
| 57 | |
| 58 | void SoundPoolThread::quit() { |
| 59 | Mutex::Autolock lock(&mLock); |
| 60 | if (mRunning) { |
| 61 | mRunning = false; |
| 62 | mMsgQueue.clear(); |
Marco Nelissen | 7201d9c | 2017-05-31 13:52:21 -0700 | [diff] [blame] | 63 | mCondition.broadcast(); // wake up any blocked writers |
| 64 | while (mNumWorkers > 0) { |
| 65 | mCondition.wait(mLock); |
| 66 | } |
Marco Nelissen | 372be89 | 2014-12-04 08:59:22 -0800 | [diff] [blame] | 67 | } |
| 68 | ALOGV("return from quit"); |
| 69 | } |
| 70 | |
| 71 | SoundPoolThread::SoundPoolThread(SoundPool* soundPool) : |
Marco Nelissen | 7201d9c | 2017-05-31 13:52:21 -0700 | [diff] [blame] | 72 | mSoundPool(soundPool), |
| 73 | mNumWorkers(0), |
| 74 | mRunning(true) |
Marco Nelissen | 372be89 | 2014-12-04 08:59:22 -0800 | [diff] [blame] | 75 | { |
| 76 | mMsgQueue.setCapacity(maxMessages); |
Marco Nelissen | 372be89 | 2014-12-04 08:59:22 -0800 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | SoundPoolThread::~SoundPoolThread() |
| 80 | { |
| 81 | quit(); |
| 82 | } |
| 83 | |
| 84 | int SoundPoolThread::beginThread(void* arg) { |
| 85 | ALOGV("beginThread"); |
| 86 | SoundPoolThread* soundPoolThread = (SoundPoolThread*)arg; |
| 87 | return soundPoolThread->run(); |
| 88 | } |
| 89 | |
| 90 | int SoundPoolThread::run() { |
| 91 | ALOGV("run"); |
| 92 | for (;;) { |
| 93 | SoundPoolMsg msg = read(); |
| 94 | ALOGV("Got message m=%d, mData=%d", msg.mMessageType, msg.mData); |
| 95 | switch (msg.mMessageType) { |
| 96 | case SoundPoolMsg::KILL: |
| 97 | ALOGV("goodbye"); |
| 98 | return NO_ERROR; |
| 99 | case SoundPoolMsg::LOAD_SAMPLE: |
| 100 | doLoadSample(msg.mData); |
| 101 | break; |
| 102 | default: |
| 103 | ALOGW("run: Unrecognized message %d\n", |
| 104 | msg.mMessageType); |
| 105 | break; |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | void SoundPoolThread::loadSample(int sampleID) { |
| 111 | write(SoundPoolMsg(SoundPoolMsg::LOAD_SAMPLE, sampleID)); |
| 112 | } |
| 113 | |
| 114 | void SoundPoolThread::doLoadSample(int sampleID) { |
| 115 | sp <Sample> sample = mSoundPool->findSample(sampleID); |
| 116 | status_t status = -1; |
| 117 | if (sample != 0) { |
| 118 | status = sample->doLoad(); |
| 119 | } |
| 120 | mSoundPool->notify(SoundPoolEvent(SoundPoolEvent::SAMPLE_LOADED, sampleID, status)); |
| 121 | } |
| 122 | |
| 123 | } // end namespace android |