SoundPoolThread.cpp revision 9066cfe9886ac131c34d59ed0e2d287b0e3c0087
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
23namespace android {
24
25void SoundPoolThread::MessageQueue::write(SoundPoolMsg msg) {
26    LOGV("MessageQueue::write - acquiring lock\n");
27    Mutex::Autolock lock(&mLock);
28    while (mQueue.size() >= maxMessages) {
29        LOGV("MessageQueue::write - wait\n");
30        mCondition.wait(mLock);
31    }
32    LOGV("MessageQueue::write - push message\n");
33    mQueue.push(msg);
34    mCondition.signal();
35}
36
37const SoundPoolMsg SoundPoolThread::MessageQueue::read() {
38    LOGV("MessageQueue::read - acquiring lock\n");
39    Mutex::Autolock lock(&mLock);
40    while (mQueue.size() == 0) {
41        LOGV("MessageQueue::read - wait\n");
42        mCondition.wait(mLock);
43    }
44    SoundPoolMsg msg = mQueue[0];
45    LOGV("MessageQueue::read - retrieve message\n");
46    mQueue.removeAt(0);
47    mCondition.signal();
48    return msg;
49}
50
51void SoundPoolThread::MessageQueue::quit() {
52    Mutex::Autolock lock(&mLock);
53    mQueue.clear();
54    mQueue.push(SoundPoolMsg(SoundPoolMsg::KILL, 0));
55    mCondition.signal();
56    mCondition.wait(mLock);
57    LOGV("return from quit");
58}
59
60SoundPoolThread::SoundPoolThread(SoundPool* soundPool) :
61    mSoundPool(soundPool)
62{
63    mMessages.setCapacity(maxMessages);
64    createThread(beginThread, this);
65}
66
67SoundPoolThread::~SoundPoolThread()
68{
69}
70
71int SoundPoolThread::beginThread(void* arg) {
72    LOGV("beginThread");
73    SoundPoolThread* soundPoolThread = (SoundPoolThread*)arg;
74    return soundPoolThread->run();
75}
76
77int SoundPoolThread::run() {
78    LOGV("run");
79    for (;;) {
80        SoundPoolMsg msg = mMessages.read();
81        LOGV("Got message m=%d, mData=%d", msg.mMessageType, msg.mData);
82        switch (msg.mMessageType) {
83        case SoundPoolMsg::KILL:
84            LOGV("goodbye");
85            return NO_ERROR;
86        case SoundPoolMsg::LOAD_SAMPLE:
87            doLoadSample(msg.mData);
88            break;
89        default:
90            LOGW("run: Unrecognized message %d\n",
91                    msg.mMessageType);
92            break;
93        }
94    }
95}
96
97void SoundPoolThread::loadSample(int sampleID) {
98    mMessages.write(SoundPoolMsg(SoundPoolMsg::LOAD_SAMPLE, sampleID));
99}
100
101void SoundPoolThread::doLoadSample(int sampleID) {
102    sp <Sample> sample = mSoundPool->findSample(sampleID);
103    if (sample != 0) {
104        sample->doLoad();
105    }
106}
107
108} // end namespace android
109