• 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 "AudioPrimarySourceClock"
18 #endif
19 
20 #include "audio_primary_source_clock.h"
21 #include <limits>
22 #include <cinttypes>
23 #include "audio_hdi_log.h"
24 #include "audio_utils.h"
25 
26 namespace OHOS {
27 namespace AudioStandard {
28 
29 static constexpr uint32_t USING_FIRST_TS_FOR_TS_COUNTTING_MAX = 20;
30 static constexpr uint32_t REGULAR_DETLA_RATIO = 2;
31 
32 /*
33  * The timestamp for the first 20 frames can be obtained by accumulating the firstTimeStamp of the HDI layer.
34  * After that,
35  * the systemClock is used to obtain the timestamp when "the packet interval is normal" or "after first 20 frames".
36  */
CheckAndResetTimestamp(uint64_t & timestamp,uint32_t positionInc)37 void AudioCapturerSourceClock::CheckAndResetTimestamp(uint64_t &timestamp, uint32_t positionInc)
38 {
39     CHECK_AND_RETURN_LOG(sampleRate_ != 0, "sampleRate_ is zero!");
40     if (isGetTimeStampFromSystemClock_) {
41         return;
42     }
43 
44     frameCnt_++;
45 
46     if (frameCnt_ == 1) {
47         AUDIO_INFO_LOG("GetFirstTimeStampFromAlgo:%{public}" PRIu64, firstTimeStamp_);
48     }
49 
50     if (frameCnt_ > USING_FIRST_TS_FOR_TS_COUNTTING_MAX || firstTimeStamp_ == 0) {
51         AUDIO_ERR_LOG("frameCnt_ > MAX or ts is 0! Get timestamp from system Clock");
52         isGetTimeStampFromSystemClock_ = true;
53         return;
54     }
55 
56     uint64_t tsDetla = timestamp - lastTs_;
57     lastTs_ = timestamp;
58     uint64_t regularTsDetla = positionInc * AUDIO_NS_PER_SECOND / sampleRate_;
59     AUDIO_INFO_LOG("tsDetla:%{public}" PRIu64 " regularTsDetla:%{public}" PRIu64, tsDetla, regularTsDetla);
60     if (tsDetla > (regularTsDetla / REGULAR_DETLA_RATIO) && tsDetla < (regularTsDetla * REGULAR_DETLA_RATIO)) {
61         AUDIO_INFO_LOG("tsDetla good! Get timestamp from system Clock");
62         isGetTimeStampFromSystemClock_ = true;
63         return;
64     }
65 
66     if (frameCnt_ != 1) {
67         firstTimeStamp_ += regularTsDetla;
68     }
69     timestamp = firstTimeStamp_;
70 
71     AUDIO_INFO_LOG("frameCnt_:%{public}u timestamp:%{public}" PRIu64, frameCnt_, timestamp);
72 }
73 
GetTimestamp(uint32_t positionInc)74 uint64_t AudioCapturerSourceClock::GetTimestamp(uint32_t positionInc)
75 {
76     int64_t timestamp = ClockTime::GetCurNano();
77     CHECK_AND_RETURN_RET_LOG(timestamp > 0, 0, "GetCurNano fail!");
78     uint64_t unsignedTimestamp = static_cast<uint64_t>(timestamp);
79 
80     CheckAndResetTimestamp(unsignedTimestamp, positionInc);
81     return unsignedTimestamp;
82 }
83 
GetFrameCnt() const84 uint32_t AudioCapturerSourceClock::GetFrameCnt() const
85 {
86     return frameCnt_;
87 }
88 
Reset()89 void AudioCapturerSourceClock::Reset()
90 {
91     std::lock_guard<std::mutex> lock(clockMtx_);
92     frameCnt_ = 0;
93     firstTimeStamp_ = 0;
94     lastTs_ = 0;
95     isGetTimeStampFromSystemClock_ = false;
96 }
97 
SetFirstTimestampFromHdi(uint64_t hdiTimestamp)98 void AudioCapturerSourceClock::SetFirstTimestampFromHdi(uint64_t hdiTimestamp)
99 {
100     AUDIO_INFO_LOG("hdiTimestamp:%{public}" PRIu64, hdiTimestamp);
101 
102     std::lock_guard<std::mutex> lock(clockMtx_);
103     firstTimeStamp_ = hdiTimestamp;
104 }
105 
106 } // namespace AudioStandard
107 } // namespace OHOS
108