1 /*
2 * Copyright (C) 2025 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
16 #include "media_errors.h"
17 #include "media_log.h"
18 #include "soundpool_manager_multi.h"
19
20 namespace {
21 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_SOUNDPOOL, "SoundPoolManagerMulti"};
22 static const int32_t SOUNDPOOL_INSTANCE_MAX_NUM = 128;
23 }
24
25 namespace OHOS {
26 namespace Media {
~SoundPoolManagerMulti()27 SoundPoolManagerMulti::~SoundPoolManagerMulti()
28 {
29 MEDIA_LOGI("Destruction SoundPoolManagerMulti");
30 std::lock_guard<std::mutex> lock(mutex_);
31 soundPools_.clear();
32 };
33
GetInstance()34 SoundPoolManagerMulti& SoundPoolManagerMulti::GetInstance()
35 {
36 static SoundPoolManagerMulti instance;
37 return instance;
38 }
39
GetSoundPoolInstance(std::shared_ptr<SoundPool> & soundPool)40 int32_t SoundPoolManagerMulti::GetSoundPoolInstance(std::shared_ptr<SoundPool>& soundPool)
41 {
42 std::lock_guard<std::mutex> lock(mutex_);
43 uint32_t instanceNum = soundPools_.size();
44 if (instanceNum >= SOUNDPOOL_INSTANCE_MAX_NUM) {
45 MEDIA_LOGI("create soundpool fail, instanceNum:%{public}u", instanceNum);
46 return MSERR_INVALID_OPERATION;
47 } else {
48 soundPool = std::make_shared<SoundPool>();
49 soundPools_.push_back(soundPool);
50 MEDIA_LOGI("create soundpool success, instanceNum:%{public}zu", soundPools_.size());
51 }
52
53 return MSERR_OK;
54 }
55
ReleaseInstance(std::shared_ptr<SoundPool> soundPool)56 int32_t SoundPoolManagerMulti::ReleaseInstance(std::shared_ptr<SoundPool> soundPool)
57 {
58 std::lock_guard<std::mutex> lock(mutex_);
59 for (auto it = soundPools_.begin(); it != soundPools_.end();) {
60 if (*it == soundPool) {
61 it = soundPools_.erase(it);
62 break;
63 } else {
64 ++it;
65 }
66 }
67 MEDIA_LOGI("release soundpool after, instanceNum:%{public}zu", soundPools_.size());
68 return MSERR_OK;
69 }
70
71 } // namespace Media
72 } // namespace OHOS
73