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 "AudioLimiterManager"
18 #endif
19
20 #include "audio_errors.h"
21 #include "audio_limiter_manager.h"
22 #include "audio_common_log.h"
23
24 #include "securec.h"
25 namespace OHOS {
26 namespace AudioStandard {
AudioLmtManager()27 AudioLmtManager::AudioLmtManager()
28 {
29 sinkIndexToLimiterMap_.clear();
30 AUDIO_INFO_LOG("AudioLmtManager");
31 }
32
~AudioLmtManager()33 AudioLmtManager::~AudioLmtManager()
34 {
35 AUDIO_INFO_LOG("~AudioLmtManager");
36 }
37
GetInstance()38 AudioLmtManager* AudioLmtManager::GetInstance()
39 {
40 static AudioLmtManager instance;
41 return &instance;
42 }
43
CreateLimiter(int32_t sinkIndex)44 int32_t AudioLmtManager::CreateLimiter(int32_t sinkIndex)
45 {
46 std::lock_guard<std::mutex> lock(limiterMutex_);
47 if (sinkIndexToLimiterMap_.find(sinkIndex) != sinkIndexToLimiterMap_.end() &&
48 sinkIndexToLimiterMap_[sinkIndex] != nullptr) {
49 AUDIO_INFO_LOG("The limiter has been created, sinkIndex = %{public}d", sinkIndex);
50 return SUCCESS;
51 }
52
53 std::shared_ptr<AudioLimiter> limiter = std::make_shared<AudioLimiter>(sinkIndex);
54
55 CHECK_AND_RETURN_RET_LOG(limiter != nullptr, ERROR,
56 "Failed to create limiter, sinkIndex = %{public}d", sinkIndex);
57
58 sinkIndexToLimiterMap_[sinkIndex] = limiter;
59 AUDIO_INFO_LOG("Create limiter success, sinkIndex = %{public}d", sinkIndex);
60 return SUCCESS;
61 }
62
SetLimiterConfig(int32_t sinkIndex,int32_t maxRequest,int32_t biteSize,int32_t sampleRate,int32_t channels)63 int32_t AudioLmtManager::SetLimiterConfig(int32_t sinkIndex, int32_t maxRequest, int32_t biteSize,
64 int32_t sampleRate, int32_t channels)
65 {
66 std::lock_guard<std::mutex> lock(limiterMutex_);
67 auto iter = sinkIndexToLimiterMap_.find(sinkIndex);
68 CHECK_AND_RETURN_RET_LOG(iter != sinkIndexToLimiterMap_.end(), ERROR,
69 "The limiter has not been created, sinkIndex = %{public}d", sinkIndex);
70
71 std::shared_ptr<AudioLimiter> limiter = iter->second;
72 CHECK_AND_RETURN_RET_LOG(limiter != nullptr, ERROR,
73 "The limiter is nullptr, sinkIndex = %{public}d", sinkIndex);
74
75 return limiter->SetConfig(maxRequest, biteSize, sampleRate, channels);
76 }
77
ProcessLimiter(int32_t sinkIndex,int32_t frameLen,float * inBuffer,float * outBuffer)78 int32_t AudioLmtManager::ProcessLimiter(int32_t sinkIndex, int32_t frameLen, float *inBuffer, float *outBuffer)
79 {
80 std::lock_guard<std::mutex> lock(limiterMutex_);
81 CHECK_AND_RETURN_RET_LOG(inBuffer != nullptr && outBuffer != nullptr, ERROR, "inBuffer or outBuffer is nullptr");
82
83 auto iter = sinkIndexToLimiterMap_.find(sinkIndex);
84 if (iter == sinkIndexToLimiterMap_.end()) {
85 AUDIO_INFO_LOG("The limiter has not been created, sinkIndex = %{public}d", sinkIndex);
86 CHECK_AND_RETURN_RET_LOG(memcpy_s(outBuffer, frameLen * sizeof(float), inBuffer, frameLen * sizeof(float)) == 0,
87 ERROR, "memcpy_s failed");
88 return ERROR;
89 }
90
91 std::shared_ptr<AudioLimiter> limiter = iter->second;
92 if (limiter == nullptr) {
93 AUDIO_INFO_LOG("The limiter is nullptr, sinkIndex = %{public}d", sinkIndex);
94 CHECK_AND_RETURN_RET_LOG(memcpy_s(outBuffer, frameLen * sizeof(float), inBuffer, frameLen * sizeof(float)) == 0,
95 ERROR, "memcpy_s failed");
96 return ERROR;
97 }
98
99 int32_t ret = limiter->Process(frameLen, inBuffer, outBuffer);
100 if (ret != SUCCESS) {
101 AUDIO_ERR_LOG("Failed to process limiter, sinkIndex = %{public}d", sinkIndex);
102 CHECK_AND_RETURN_RET_LOG(memcpy_s(outBuffer, frameLen * sizeof(float), inBuffer, frameLen * sizeof(float)) == 0,
103 ERROR, "memcpy_s failed");
104 return ERROR;
105 }
106 return SUCCESS;
107 }
108
ReleaseLimiter(int32_t sinkIndex)109 int32_t AudioLmtManager::ReleaseLimiter(int32_t sinkIndex)
110 {
111 std::lock_guard<std::mutex> lock(limiterMutex_);
112 auto iter = sinkIndexToLimiterMap_.find(sinkIndex);
113 CHECK_AND_RETURN_RET_LOG(iter != sinkIndexToLimiterMap_.end(), ERROR,
114 "The limiter has not been created, sinkIndex = %{public}d", sinkIndex);
115
116 sinkIndexToLimiterMap_.erase(iter);
117 AUDIO_INFO_LOG("Release limiter success, sinkIndex = %{public}d", sinkIndex);
118 return SUCCESS;
119 }
120
GetLatency(int32_t sinkIndex)121 uint32_t AudioLmtManager::GetLatency(int32_t sinkIndex)
122 {
123 std::lock_guard<std::mutex> lock(limiterMutex_);
124 auto iter = sinkIndexToLimiterMap_.find(sinkIndex);
125 CHECK_AND_RETURN_RET_LOG(iter != sinkIndexToLimiterMap_.end(), 0,
126 "The limiter has not been created, sinkIndex = %{public}d", sinkIndex);
127
128 std::shared_ptr<AudioLimiter> limiter = iter->second;
129 CHECK_AND_RETURN_RET_LOG(limiter != nullptr, 0, "The limiter is nullptr, sinkIndex = %{public}d", sinkIndex);
130
131 return limiter->GetLatency();
132 }
133 } // namespace AudioStandard
134 } // namespace OHOS