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