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 
16 #include "media_errors.h"
17 #include "media_log.h"
18 #include "soundpool_manager.h"
19 
20 namespace {
21     constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_SOUNDPOOL, "SoundPoolManager"};
22 }
23 
24 namespace OHOS {
25 namespace Media {
~SoundPoolManager()26 SoundPoolManager::~SoundPoolManager()
27 {
28     MEDIA_LOGI("Destruction SoundPoolManager.");
29     std::lock_guard<std::mutex> lock(mutex_);
30     soundPools_.clear();
31 };
32 
33 
GetSoundPool(const pid_t pid,std::shared_ptr<SoundPool> & soundPool)34 int32_t SoundPoolManager::GetSoundPool(const pid_t pid, std::shared_ptr<SoundPool>& soundPool)
35 {
36     std::lock_guard<std::mutex> lock(mutex_);
37     auto it = soundPools_.find(pid);
38     it != soundPools_.end() ? soundPool = it->second : soundPool = nullptr;
39     return MSERR_OK;
40 }
41 
SetSoundPool(const pid_t pid,std::shared_ptr<SoundPool> soundPool)42 int32_t SoundPoolManager::SetSoundPool(const pid_t pid, std::shared_ptr<SoundPool> soundPool)
43 {
44     std::lock_guard<std::mutex> lock(mutex_);
45     auto it = soundPools_.find(pid);
46     if (it != soundPools_.end()) {
47         MEDIA_LOGI("SoundPool have setted, use old object.");
48         return MSERR_OK;
49     }
50     soundPool = std::make_shared<SoundPool>();
51     soundPools_.emplace(pid, soundPool);
52 
53     return MSERR_OK;
54 }
55 
Release(const pid_t pid)56 int32_t SoundPoolManager::Release(const pid_t pid)
57 {
58     std::lock_guard<std::mutex> lock(mutex_);
59     auto it = soundPools_.find(pid);
60     if (it != soundPools_.end()) {
61         MEDIA_LOGI("Release soundpool, pid:%{public}d.", pid);
62         soundPools_.erase(it);
63         return MSERR_OK;
64     }
65     return MSERR_OK;
66 }
67 } // namespace Media
68 } // namespace OHOS
69