SoundPoolThread.h revision 54b6cfa9a9e5b861a9930af873580d6dc20f773c
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#ifndef SOUNDPOOLTHREAD_H_
18#define SOUNDPOOLTHREAD_H_
19
20#include <utils/threads.h>
21#include <utils/Vector.h>
22#include <media/AudioTrack.h>
23
24#include "SoundPool.h"
25
26namespace android {
27
28class SoundPoolMsg {
29public:
30    enum MessageType { INVALID, KILL, LOAD_SAMPLE, PLAY_SAMPLE, SAMPLE_DONE };
31    SoundPoolMsg() : mMessageType(INVALID), mData(0) {}
32    SoundPoolMsg(MessageType MessageType, int data) :
33        mMessageType(MessageType), mData(data) {}
34    uint8_t         mMessageType;
35    uint8_t         mData;
36    uint8_t         mData2;
37    uint8_t         mData3;
38};
39
40/*
41 * This class handles background requests from the SoundPool
42 */
43class SoundPoolThread {
44public:
45    SoundPoolThread(SoundPool* SoundPool);
46    ~SoundPoolThread();
47    void loadSample(int sampleID);
48    void quit() { mMessages.quit(); }
49
50private:
51    static const size_t maxMessages = 5;
52
53    class MessageQueue {
54    public:
55        void write(SoundPoolMsg msg);
56        const SoundPoolMsg read();
57        void setCapacity(size_t size) { mQueue.setCapacity(size); }
58        void quit();
59    private:
60        Vector<SoundPoolMsg>    mQueue;
61        Mutex                   mLock;
62        Condition               mCondition;
63    };
64
65    static int beginThread(void* arg);
66    int run();
67    void doLoadSample(int sampleID);
68
69    SoundPool*                  mSoundPool;
70    MessageQueue                mMessages;
71};
72
73} // end namespace android
74
75#endif /*SOUNDPOOLTHREAD_H_*/
76