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 "CapturerClockManager"
18 #endif
19
20 #include "capturer_clock_manager.h"
21 #include <cinttypes>
22 #include "audio_hdi_log.h"
23
24 namespace OHOS {
25 namespace AudioStandard {
26
27 static CapturerClockManager g_captureClockMgrSingleton;
28
GetInstance(void)29 CapturerClockManager &CapturerClockManager::GetInstance(void)
30 {
31 return g_captureClockMgrSingleton;
32 }
33
CreateCapturerClock(uint32_t sessionId,uint32_t sampleRate)34 std::shared_ptr<CapturerClock> CapturerClockManager::CreateCapturerClock(
35 uint32_t sessionId, uint32_t sampleRate)
36 {
37 std::lock_guard<std::mutex> lock(clockPoolMtx_);
38 CHECK_AND_RETURN_RET_LOG(capturerClockPool_.find(sessionId) == capturerClockPool_.end(), nullptr,
39 "fail, [%{public}u] is existed!", sessionId);
40
41 std::shared_ptr<CapturerClock> clock = std::make_shared<CapturerClock>(sampleRate);
42 capturerClockPool_[sessionId] = clock;
43 return clock;
44 }
45
DeleteCapturerClock(uint32_t sessionId)46 void CapturerClockManager::DeleteCapturerClock(uint32_t sessionId)
47 {
48 std::lock_guard<std::mutex> lock(clockPoolMtx_);
49 capturerClockPool_.erase(sessionId);
50 }
51
GetCapturerClock(uint32_t sessionId)52 std::shared_ptr<CapturerClock> CapturerClockManager::GetCapturerClock(uint32_t sessionId)
53 {
54 std::lock_guard<std::mutex> lock(clockPoolMtx_);
55 CHECK_AND_RETURN_RET_LOG(capturerClockPool_.find(sessionId) != capturerClockPool_.end(), nullptr,
56 "fail, [%{public}u] is not existed!", sessionId);
57
58 return capturerClockPool_[sessionId];
59 }
60
RegisterAudioSourceClock(uint32_t captureId,std::shared_ptr<AudioSourceClock> clock)61 bool CapturerClockManager::RegisterAudioSourceClock(uint32_t captureId, std::shared_ptr<AudioSourceClock> clock)
62 {
63 std::lock_guard<std::mutex> lock(clockPoolMtx_);
64 CHECK_AND_RETURN_RET_LOG(audioSrcClockPool_.find(captureId) == audioSrcClockPool_.end(), false,
65 "fail, [%{public}u] is existed!", captureId);
66
67 audioSrcClockPool_[captureId] = clock;
68 return true;
69 }
70
DeleteAudioSourceClock(uint32_t captureId)71 void CapturerClockManager::DeleteAudioSourceClock(uint32_t captureId)
72 {
73 std::lock_guard<std::mutex> lock(clockPoolMtx_);
74 audioSrcClockPool_.erase(captureId);
75 }
76
GetAudioSourceClock(uint32_t captureId)77 std::shared_ptr<AudioSourceClock> CapturerClockManager::GetAudioSourceClock(uint32_t captureId)
78 {
79 std::lock_guard<std::mutex> lock(clockPoolMtx_);
80 CHECK_AND_RETURN_RET_LOG(audioSrcClockPool_.find(captureId) != audioSrcClockPool_.end(), nullptr,
81 "fail, [%{public}u] is not existed!", captureId);
82
83 return audioSrcClockPool_[captureId];
84 }
85
86 } // namespace AudioStandard
87 } // namespace OHOS
88