blob: 3d988773ff04e379ea5c835d647d1c07129064b0 [file] [log] [blame]
Marco Nelissen372be892014-12-04 08:59:22 -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
Marco Nelissen7201d9c2017-05-31 13:52:21 -070023static const int kMaxWorkers = 3;
24
Marco Nelissen372be892014-12-04 08:59:22 -080025namespace android {
26
27void 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 Nelissen7201d9c2017-05-31 13:52:21 -070036 if (mNumWorkers < kMaxWorkers) {
37 if (createThreadEtc(beginThread, this, "SoundPoolThread")) {
38 mNumWorkers++;
39 ALOGV("created worker thread");
40 }
41 }
Marco Nelissen372be892014-12-04 08:59:22 -080042 }
43}
44
45const SoundPoolMsg SoundPoolThread::read() {
46 Mutex::Autolock lock(&mLock);
Marco Nelissen7201d9c2017-05-31 13:52:21 -070047 if (mMsgQueue.size() == 0) {
48 mNumWorkers--;
49 mCondition.signal();
50 return SoundPoolMsg(SoundPoolMsg::KILL, 0);
Marco Nelissen372be892014-12-04 08:59:22 -080051 }
52 SoundPoolMsg msg = mMsgQueue[0];
53 mMsgQueue.removeAt(0);
54 mCondition.signal();
55 return msg;
56}
57
58void SoundPoolThread::quit() {
59 Mutex::Autolock lock(&mLock);
60 if (mRunning) {
61 mRunning = false;
62 mMsgQueue.clear();
Marco Nelissen7201d9c2017-05-31 13:52:21 -070063 mCondition.broadcast(); // wake up any blocked writers
64 while (mNumWorkers > 0) {
65 mCondition.wait(mLock);
66 }
Marco Nelissen372be892014-12-04 08:59:22 -080067 }
68 ALOGV("return from quit");
69}
70
71SoundPoolThread::SoundPoolThread(SoundPool* soundPool) :
Marco Nelissen7201d9c2017-05-31 13:52:21 -070072 mSoundPool(soundPool),
73 mNumWorkers(0),
74 mRunning(true)
Marco Nelissen372be892014-12-04 08:59:22 -080075{
76 mMsgQueue.setCapacity(maxMessages);
Marco Nelissen372be892014-12-04 08:59:22 -080077}
78
79SoundPoolThread::~SoundPoolThread()
80{
81 quit();
82}
83
84int SoundPoolThread::beginThread(void* arg) {
85 ALOGV("beginThread");
86 SoundPoolThread* soundPoolThread = (SoundPoolThread*)arg;
87 return soundPoolThread->run();
88}
89
90int 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
110void SoundPoolThread::loadSample(int sampleID) {
111 write(SoundPoolMsg(SoundPoolMsg::LOAD_SAMPLE, sampleID));
112}
113
114void 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