• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef LOG_TAG
17 #define LOG_TAG "ChainPoolImpl"
18 #endif
19 
20 #include "chain_pool.h"
21 
22 #include <cinttypes>
23 #include <map>
24 #include <mutex>
25 
26 #include "audio_effect_log.h"
27 #include "audio_errors.h"
28 #include "nocopyable.h"
29 
30 namespace OHOS {
31 namespace AudioStandard {
32 class ChainPoolImpl final : public ChainPool, public NoCopyable {
33 public:
34     int32_t AddChain(const std::shared_ptr<AudioEnhanceChain> chain) override;
35 
36     int32_t DeleteChain(uint64_t chainId) override;
37 
38     std::shared_ptr<AudioEnhanceChain> GetChainById(uint64_t chainId) override;
39 
40     std::vector<std::shared_ptr<AudioEnhanceChain>> GetAllChain(void) override;
41 
42 private:
43     friend class ChainPool;
44     ChainPoolImpl() = default;
45     ~ChainPoolImpl() override = default;
46     std::map<uint64_t, std::shared_ptr<AudioEnhanceChain>> chainMap_;
47     std::mutex mutex_;
48 };
49 
AddChain(const std::shared_ptr<AudioEnhanceChain> chain)50 int32_t ChainPoolImpl::AddChain(const std::shared_ptr<AudioEnhanceChain> chain)
51 {
52     CHECK_AND_RETURN_RET_LOG(chain != nullptr, ERROR, "chain is null");
53 
54     std::lock_guard<std::mutex> lock(mutex_);
55     auto chainId = chain->GetChainId();
56     if (chainMap_.find(chainId) != chainMap_.end()) {
57         AUDIO_INFO_LOG("the chain has been added, Id: %{public}" PRIu64, chainId);
58         return SUCCESS;
59     }
60 
61     chainMap_.emplace(chainId, chain);
62     AUDIO_INFO_LOG("add chain success, chainId: %{public}" PRIu64, chainId);
63 
64     return SUCCESS;
65 }
66 
DeleteChain(uint64_t chainId)67 int32_t ChainPoolImpl::DeleteChain(uint64_t chainId)
68 {
69     std::lock_guard<std::mutex> lock(mutex_);
70     if (chainMap_.erase(chainId) != 0) {
71         AUDIO_INFO_LOG("delete chain success, chainId: %{public}" PRIu64, chainId);
72         return SUCCESS;
73     }
74 
75     AUDIO_ERR_LOG("chain is not found, chainId: %{public}" PRIu64, chainId);
76     return ERROR;
77 }
78 
GetChainById(uint64_t chainId)79 std::shared_ptr<AudioEnhanceChain> ChainPoolImpl::GetChainById(uint64_t chainId)
80 {
81     std::lock_guard<std::mutex> lock(mutex_);
82     auto iter = chainMap_.find(chainId);
83     if (iter == chainMap_.end()) {
84         return nullptr;
85     }
86 
87     return iter->second;
88 }
89 
GetAllChain(void)90 std::vector<std::shared_ptr<AudioEnhanceChain>> ChainPoolImpl::GetAllChain(void)
91 {
92     std::lock_guard<std::mutex> lock(mutex_);
93     std::vector<std::shared_ptr<AudioEnhanceChain>> chainArray;
94     for (const auto &[id, chain] : chainMap_) {
95         chainArray.emplace_back(chain);
96     }
97 
98     return chainArray;
99 }
100 
GetInstance()101 ChainPool &ChainPool::GetInstance()
102 {
103     static ChainPoolImpl impl;
104     return impl;
105 }
106 } // namespace AudioStandard
107 } // namespace OHOS