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 "AudioSourceClock"
18 #endif
19
20 #include "audio_source_clock.h"
21 #include <limits>
22 #include <cinttypes>
23 #include "audio_hdi_log.h"
24 #include "audio_utils.h"
25 #include "capturer_clock_manager.h"
26
27 namespace OHOS {
28 namespace AudioStandard {
29
30 enum AudioByteSize : int32_t {
31 BYTE_SIZE_SAMPLE_U8 = 1,
32 BYTE_SIZE_SAMPLE_S16 = 2,
33 BYTE_SIZE_SAMPLE_S24 = 3,
34 BYTE_SIZE_SAMPLE_S32 = 4,
35 };
36
GetByteSizeByFormat(AudioSampleFormat format)37 static int32_t GetByteSizeByFormat(AudioSampleFormat format)
38 {
39 int32_t byteSize = 0;
40 switch (format) {
41 case SAMPLE_U8:
42 byteSize = BYTE_SIZE_SAMPLE_U8;
43 break;
44 case SAMPLE_S16LE:
45 byteSize = BYTE_SIZE_SAMPLE_S16;
46 break;
47 case SAMPLE_S24LE:
48 byteSize = BYTE_SIZE_SAMPLE_S24;
49 break;
50 case SAMPLE_S32LE:
51 byteSize = BYTE_SIZE_SAMPLE_S32;
52 break;
53 default:
54 byteSize = BYTE_SIZE_SAMPLE_S16;
55 break;
56 }
57
58 return byteSize;
59 }
60
Init(uint32_t sampleRate,AudioSampleFormat format,uint32_t channel)61 void AudioSourceClock::Init(uint32_t sampleRate, AudioSampleFormat format, uint32_t channel)
62 {
63 AUDIO_INFO_LOG("sampleRate:%{public}u format:%{public}d channel:%{public}u",
64 sampleRate, static_cast<int32_t>(format), channel);
65 std::lock_guard<std::mutex> lock(clockMtx_);
66 sampleRate_ = sampleRate;
67 sizePerPos_ = static_cast<uint32_t>(GetByteSizeByFormat(format)) * channel;
68 }
69
70 // can be override for differ method
GetTimestamp(uint32_t positionInc)71 uint64_t AudioSourceClock::GetTimestamp(uint32_t positionInc)
72 {
73 int64_t timestamp = ClockTime::GetCurNano();
74 CHECK_AND_RETURN_RET_LOG(timestamp > 0, 0, "GetCurNano fail!");
75 return static_cast<uint64_t>(timestamp);
76 }
77
Renew(uint32_t posIncSize)78 void AudioSourceClock::Renew(uint32_t posIncSize)
79 {
80 CHECK_AND_RETURN_LOG(posIncSize != 0, "posIncSize is 0!");
81 CHECK_AND_RETURN_LOG(sizePerPos_ != 0, "sizePerPos_ is 0!");
82
83 std::lock_guard<std::mutex> lock(clockMtx_);
84
85 uint32_t positionInc = posIncSize / sizePerPos_;
86 uint64_t timestamp = GetTimestamp(positionInc);
87
88 AUDIO_DEBUG_LOG("dataSize:%{public}u positionInc:%{public}u", posIncSize, positionInc);
89
90 for (size_t i = 0; i < sessionIdList_.size(); i++) {
91 if (sessionIdList_[i] == 0) {
92 break;
93 }
94 AUDIO_DEBUG_LOG("SessionId %{public}u", sessionIdList_[i]);
95 std::shared_ptr<CapturerClock> clock =
96 CapturerClockManager::GetInstance().GetCapturerClock(sessionIdList_[i]);
97 if (clock == nullptr) {
98 AUDIO_INFO_LOG("SessionId[%{public}u] capturer clock == nullptr!", sessionIdList_[i]);
99 continue;
100 }
101 clock->SetTimeStampByPosition(timestamp, sampleRate_, positionInc);
102 }
103 }
104
UpdateSessionId(const std::vector<int32_t> & sessionIdList)105 void AudioSourceClock::UpdateSessionId(const std::vector<int32_t> &sessionIdList)
106 {
107 std::lock_guard<std::mutex> lock(clockMtx_);
108 sessionIdList_ = sessionIdList;
109 }
110
~AudioCapturerSourceTsRecorder()111 AudioCapturerSourceTsRecorder::~AudioCapturerSourceTsRecorder()
112 {
113 CHECK_AND_RETURN(clock_ != nullptr, "clock_ is nullptr! fail to renew timestamp!");
114 clock_->Renew(replyBytes_);
115 }
116
117 } // namespace AudioStandard
118 } // namespace OHOS
119