| 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 | #pragma once |
| 18 | |
| 19 | #include "SoundManager.h" |
| 20 | #include "StreamManager.h" |
| 21 | |
| 22 | #include <string> |
| 23 | |
| 24 | namespace android { |
| 25 | |
| 26 | /** |
| 27 | * Native class for Java SoundPool, manages a pool of sounds. |
| 28 | * |
| 29 | * See the Android SoundPool Java documentation for description of valid values. |
| 30 | * https://developer.android.com/reference/android/media/SoundPool |
| 31 | */ |
| 32 | class SoundPool { |
| 33 | public: |
| 34 | SoundPool(int32_t maxStreams, const audio_attributes_t& attributes, |
| 35 | const std::string& opPackageName = {}); |
| 36 | ~SoundPool(); |
| 37 | |
| 38 | // SoundPool Java API support |
| 39 | int32_t load(int fd, int64_t offset, int64_t length, int32_t priority); |
| 40 | bool unload(int32_t soundID); |
| 41 | int32_t play(int32_t soundID, float leftVolume, float rightVolume, int32_t priority, |
| 42 | int32_t loop, float rate); |
| 43 | void pause(int32_t streamID); |
| 44 | void autoPause(); |
| 45 | void resume(int32_t streamID); |
| 46 | void autoResume(); |
| 47 | void stop(int32_t streamID); |
| 48 | void setVolume(int32_t streamID, float leftVolume, float rightVolume); |
| 49 | void setPriority(int32_t streamID, int32_t priority); |
| 50 | void setLoop(int32_t streamID, int32_t loop); |
| 51 | void setRate(int32_t streamID, float rate); |
| 52 | void setCallback(SoundPoolCallback* callback, void* user); |
| 53 | void* getUserData() const; |
| 54 | |
| 55 | // not exposed in the public Java API, used for internal playerSetVolume() muting. |
| 56 | void mute(bool muting); |
| 57 | |
| 58 | private: |
| 59 | |
| 60 | // Constructor initialized variables |
| 61 | // Can access without lock as they are internally locked, |
| 62 | // though care needs to be taken that the final result composed of |
| 63 | // individually consistent actions are consistent. |
| 64 | soundpool::SoundManager mSoundManager; |
| 65 | soundpool::StreamManager mStreamManager; |
| 66 | |
| 67 | // mApiLock serializes SoundPool application calls (configurable by kUseApiLock). |
| 68 | // It only locks at the SoundPool layer and not below. At this level, |
| 69 | // mApiLock is only required for autoPause() and autoResume() to prevent zippering |
| 70 | // of the individual pauses and resumes, and mute() for self-interaction with itself. |
| 71 | // It is optional for all other apis. |
| 72 | mutable std::mutex mApiLock; |
| 73 | }; |
| 74 | |
| 75 | } // end namespace android |