| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
#pragma once |
| 18 |
|
| 19 |
#include "Sound.h" |
| 20 |
|
| 21 |
#include <mutex> |
| 22 |
#include <unordered_map> |
| 23 |
|
| 24 |
#include <android-base/thread_annotations.h> |
| 25 |
|
| 26 |
namespace android { |
| 27 |
|
| 28 |
class SoundPool; |
| 29 |
|
| 30 |
|
| 31 |
class SoundPoolEvent { |
| 32 |
public: |
| 33 |
explicit SoundPoolEvent(int msg, int arg1 = 0, int arg2 = 0) : |
| 34 |
mMsg(msg), mArg1(arg1), mArg2(arg2) {} |
| 35 |
const int mMsg; |
| 36 |
const int mArg1; |
| 37 |
const int mArg2; |
| 38 |
enum MessageType { INVALID, SOUND_LOADED }; |
| 39 |
}; |
| 40 |
|
| 41 |
|
| 42 |
typedef void SoundPoolCallback(SoundPoolEvent event, SoundPool* soundPool, void* user); |
| 43 |
|
| 44 |
} |
| 45 |
|
| 46 |
namespace android::soundpool { |
| 47 |
|
| 48 |
|
| 49 |
class SoundManager { |
| 50 |
public: |
| 51 |
SoundManager(); |
| 52 |
~SoundManager(); |
| 53 |
|
| 54 |
|
| 55 |
int32_t load(int fd, int64_t offset, int64_t length, int32_t priority); |
| 56 |
bool unload(int32_t soundID); |
| 57 |
void setCallback(SoundPool* soundPool, SoundPoolCallback* callback, void* user); |
| 58 |
void* getUserData() const; |
| 59 |
|
| 60 |
|
| 61 |
std::shared_ptr<Sound> findSound(int32_t soundID) const; |
| 62 |
|
| 63 |
|
| 64 |
void notify(SoundPoolEvent event); |
| 65 |
|
| 66 |
private: |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
class CallbackHandler { |
| 72 |
public: |
| 73 |
void setCallback(SoundPool *soundPool, SoundPoolCallback* callback, void* userData) |
| 74 |
{ |
| 75 |
std::lock_guard<std::recursive_mutex> lock(mCallbackLock); |
| 76 |
mSoundPool = soundPool; |
| 77 |
mCallback = callback; |
| 78 |
mUserData = userData; |
| 79 |
} |
| 80 |
void notify(SoundPoolEvent event) const |
| 81 |
{ |
| 82 |
std::lock_guard<std::recursive_mutex> lock(mCallbackLock); |
| 83 |
if (mCallback != nullptr) { |
| 84 |
mCallback(event, mSoundPool, mUserData); |
| 85 |
|
| 86 |
|
| 87 |
} |
| 88 |
} |
| 89 |
void* getUserData() const |
| 90 |
{ |
| 91 |
std::lock_guard<std::recursive_mutex> lock(mCallbackLock); |
| 92 |
return mUserData; |
| 93 |
} |
| 94 |
private: |
| 95 |
mutable std::recursive_mutex mCallbackLock; |
| 96 |
|
| 97 |
SoundPool* mSoundPool = nullptr; |
| 98 |
SoundPoolCallback* mCallback = nullptr; |
| 99 |
void* mUserData = nullptr; |
| 100 |
}; |
| 101 |
|
| 102 |
std::shared_ptr<Sound> findSound_l(int32_t soundID) const REQUIRES(mSoundManagerLock); |
| 103 |
|
| 104 |
|
| 105 |
CallbackHandler mCallbackHandler; |
| 106 |
const std::unique_ptr<SoundDecoder> mDecoder; |
| 107 |
|
| 108 |
mutable std::mutex mSoundManagerLock; |
| 109 |
std::unordered_map<int, std::shared_ptr<Sound>> mSounds GUARDED_BY(mSoundManagerLock); |
| 110 |
int32_t mNextSoundID GUARDED_BY(mSoundManagerLock) = 0; |
| 111 |
}; |
| 112 |
|
| 113 |
} |