1 /*
2  * Copyright (c) 2023 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 #include "daudio_latency_test.h"
17 
18 #include <ctime>
19 #include <string>
20 
21 #include "daudio_errorcode.h"
22 #include "daudio_log.h"
23 #include "daudio_util.h"
24 
25 #undef DH_LOG_TAG
26 #define DH_LOG_TAG "DAudioLatencyTest"
27 namespace OHOS {
28 namespace DistributedHardware {
29 IMPLEMENT_SINGLE_INSTANCE(DAudioLatencyTest);
30 constexpr int32_t MAXSIZE = 8192;
31 
DAudioLatencyTest()32 DAudioLatencyTest::DAudioLatencyTest()
33 {
34     DHLOGI("DAudioLatencyTest constructed.");
35 }
36 
~DAudioLatencyTest()37 DAudioLatencyTest::~DAudioLatencyTest()
38 {
39     DHLOGI("DAudioLatencyTest deconstructed.");
40 }
41 
AddPlayTime(const int64_t playBeepTime)42 int32_t DAudioLatencyTest::AddPlayTime(const int64_t playBeepTime)
43 {
44     if (GetNowTimeUs() - lastPlayTime_ <= TWO_BEEP_TIME_INTERVAL) {
45         DHLOGE("Catch play high frame, but not in %{public}d ms.", TWO_BEEP_TIME_INTERVAL);
46         return ERR_DH_AUDIO_FAILED;
47     }
48     DHLOGI("Catch play high frame, playTime: %{public}" PRId64, playBeepTime);
49     playBeepTime_.push_back(playBeepTime);
50     lastPlayTime_ = GetNowTimeUs();
51     return DH_SUCCESS;
52 }
53 
AddRecordTime(const int64_t recordBeepTime)54 int32_t DAudioLatencyTest::AddRecordTime(const int64_t recordBeepTime)
55 {
56     if (captureBeepTime_.size() >= playBeepTime_.size()) {
57         DHLOGE("Catch record high frame size error, capturesize %{public}zu, playsize %{public}zu.",
58             captureBeepTime_.size(), playBeepTime_.size());
59         return ERR_DH_AUDIO_BAD_VALUE;
60     }
61     if (GetNowTimeUs() - lastRecordTime_ <= TWO_BEEP_TIME_INTERVAL) {
62         DHLOGE("Catch record high frame, but not in %{public}d ms.", TWO_BEEP_TIME_INTERVAL);
63         return ERR_DH_AUDIO_FAILED;
64     }
65     DHLOGI("Catch record high frame, recordTime: %{public}" PRId64, recordBeepTime);
66     captureBeepTime_.push_back(recordBeepTime);
67     lastRecordTime_ = GetNowTimeUs();
68     return DH_SUCCESS;
69 }
70 
IsFrameHigh(const int16_t * audioData,const int32_t size,int32_t threshhold)71 bool DAudioLatencyTest::IsFrameHigh(const int16_t *audioData, const int32_t size, int32_t threshhold)
72 {
73     if (size > MAXSIZE) {
74         DHLOGI("size=%{public}d is over range", size);
75         return false;
76     }
77     int32_t max = 0;
78     for (int32_t i = 0; i < size; i++) {
79         int16_t f = abs(audioData[i]);
80         if (f > max) {
81             max = f;
82         }
83     }
84     return (max >= threshhold) ? true : false;
85 }
86 
RecordBeepTime(const uint8_t * base,const int32_t & sizePerFrame,bool & status)87 int64_t DAudioLatencyTest::RecordBeepTime(const uint8_t *base, const int32_t &sizePerFrame, bool &status)
88 {
89     int32_t threshhold = BEEP_THRESHHOLD;
90     bool isHigh = IsFrameHigh(reinterpret_cast<int16_t *>(const_cast<uint8_t *>(base)),
91         sizePerFrame / sizeof(int16_t), threshhold);
92     if (isHigh && status) {
93         status = false;
94         return GetNowTimeUs();
95     } else if (!isHigh) {
96         status = true;
97     }
98     return 0;
99 }
100 
ComputeLatency()101 int32_t DAudioLatencyTest::ComputeLatency()
102 {
103     DHLOGD("Compute latency time.");
104     int32_t playSize = static_cast<int32_t>(playBeepTime_.size());
105     int32_t captureSize = static_cast<int32_t>(captureBeepTime_.size());
106     if (playSize == 0 || playBeepTime_.size() != captureBeepTime_.size()) {
107         DHLOGE("Record num is not equal %{public}d: %{public}d", playSize, captureSize);
108         return -1;
109     }
110     DHLOGI("Record %{public}d times frame high.", playSize);
111     int32_t sum = 0;
112     for (int32_t i = 0; i < playSize; i++) {
113         DHLOGI("Send: %{public}" PRId64", Received: %{public}" PRId64, playBeepTime_[i], captureBeepTime_[i]);
114         DHLOGI("Time is: %{public}" PRId64" ms.", (captureBeepTime_[i] - playBeepTime_[i]) / US_PER_MS);
115         sum += captureBeepTime_[i] - playBeepTime_[i];
116     }
117     DHLOGI("Audio latency in average is: %{public}d us.", sum / playSize);
118     playBeepTime_.clear();
119     captureBeepTime_.clear();
120     return sum / playSize;
121 }
122 } // namespace DistributedHardware
123 } // namespace OHOS
124