1 /*
2  * Copyright (C) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #ifndef SOUNDPOOL_H
16 #define SOUNDPOOL_H
17 
18 #include "isoundpool.h"
19 #include "audio_renderer.h"
20 #include "stream_id_manager.h"
21 #include "sound_id_manager.h"
22 #include "cpp/mutex.h"
23 #include "media_dfx.h"
24 
25 namespace OHOS {
26 namespace Media {
27 class SoundPool : public ISoundPool {
28 public:
29     SoundPool();
30     ~SoundPool();
31     static bool CheckInitParam(int maxStreams, AudioStandard::AudioRendererInfo audioRenderInfo);
32 
33     int32_t Init(int maxStreams, AudioStandard::AudioRendererInfo audioRenderInfo);
34 
35     int32_t Load(const std::string url) override;
36 
37     int32_t Load(int32_t fd, int64_t offset, int64_t length) override;
38 
39     int32_t Play(int32_t soundID, PlayParams playParameters) override;
40 
41     int32_t Stop(int32_t streamID) override;
42 
43     int32_t SetLoop(int32_t streamID, int32_t loop) override;
44 
45     int32_t SetPriority(int32_t streamID, int32_t priority) override;
46 
47     int32_t SetRate(int32_t streamID, AudioStandard::AudioRendererRate renderRate) override;
48 
49     int32_t SetVolume(int32_t streamID, float leftVolume, float rightVolume) override;
50 
51     int32_t Unload(int32_t soundID) override;
52 
53     int32_t Release() override;
54 
55     int32_t SetSoundPoolCallback(const std::shared_ptr<ISoundPoolCallback> &soundPoolCallback) override;
56 
57     int32_t SetSoundPoolFrameWriteCallback(
58         const std::shared_ptr<ISoundPoolFrameWriteCallback> &frameWriteCallback) override;
59 
60 private:
61     bool CheckVolumeVaild(float *leftVol, float *rightVol);
62     int32_t ReleaseInner();
63     std::shared_ptr<SoundIDManager> soundIDManager_;
64     std::shared_ptr<StreamIDManager> streamIdManager_;
65     ffrt::mutex soundPoolLock_;
66     std::shared_ptr<ISoundPoolCallback> callback_ = nullptr;
67     std::shared_ptr<ISoundPoolFrameWriteCallback> frameWriteCallback_ = nullptr;
68     static constexpr int32_t MIN_STREAM_PRIORITY = 0;
69 };
70 } // namespace Media
71 } // namespace OHOS
72 #endif // SOUNDPOOL_H
73