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 "TestCaseCommon"
18 #endif
19
20 #include "test_case_common.h"
21 #include <iostream>
22 #include "hpae_info.h"
23 #include "audio_engine_log.h"
24
25 namespace OHOS {
26 namespace AudioStandard {
27 static std::string g_rootCapturerPath = "/data/source_file_io_48000_2_s16le.pcm";
28
OnStreamData(AudioCallBackStreamInfo & callBackStremInfo)29 int32_t WriteFixedDataCb::OnStreamData(AudioCallBackStreamInfo& callBackStremInfo)
30 {
31 size_t sampleSize = HPAE::GetSizeFromFormat(format_);
32 CHECK_AND_RETURN_RET_LOG(sampleSize != 0, SUCCESS, "sampleSize is zero, invalid format");
33 for (size_t i = 0; i < callBackStremInfo.requestDataLen / sampleSize; i++) {
34 switch (format_) {
35 case AudioSampleFormat::SAMPLE_U8: {
36 *(callBackStremInfo.inputData + i) = writeNum_;
37 break;
38 }
39 case SAMPLE_S16LE: {
40 *((int16_t*)callBackStremInfo.inputData + i) = writeNum_;
41 break;
42 }
43 case SAMPLE_S24LE: {
44 uint8_t *p = (uint8_t *)(callBackStremInfo.inputData + OFFSET_BIT_24 * i);
45 p[BIT_DEPTH_TWO] = (uint8_t) (writeNum_ >> BIT_16);
46 p[1] = (uint8_t) (writeNum_ >> BIT_8);
47 p[0] = (uint8_t) writeNum_;
48 break;
49 }
50 case SAMPLE_S32LE: {
51 *((int32_t*)callBackStremInfo.inputData + i) = writeNum_;
52 break;
53 }
54 case SAMPLE_F32LE: {
55 *((float*)callBackStremInfo.inputData + i) = writeNum_;
56 break;
57 }
58 default:
59 break;
60 }
61 }
62 writeNum_++;
63 return 0;
64 }
65
OnStreamData(AudioCallBackStreamInfo & callBackStremInfo)66 int32_t WriteFixedValueCb::OnStreamData(AudioCallBackStreamInfo& callBackStremInfo)
67 {
68 size_t sampleSize = HPAE::GetSizeFromFormat(format_);
69 CHECK_AND_RETURN_RET_LOG(sampleSize != 0, SUCCESS, "sampleSize is zero, invalid format");
70 for (size_t i = 0; i < callBackStremInfo.requestDataLen / sampleSize; i++) {
71 switch (format_) {
72 case AudioSampleFormat::SAMPLE_U8: {
73 *(callBackStremInfo.inputData + i) = fixValue_;
74 break;
75 }
76 case SAMPLE_S16LE: {
77 *((int16_t*)callBackStremInfo.inputData + i) = fixValue_;
78 break;
79 }
80 case SAMPLE_S24LE: {
81 uint8_t *p = (uint8_t *)(callBackStremInfo.inputData + OFFSET_BIT_24 * i);
82 p[BIT_DEPTH_TWO] = (uint8_t) (fixValue_ >> BIT_16);
83 p[1] = (uint8_t) (fixValue_ >> BIT_8);
84 p[0] = (uint8_t) fixValue_;
85 break;
86 }
87 case SAMPLE_S32LE: {
88 *((int32_t*)callBackStremInfo.inputData + i) = fixValue_;
89 break;
90 }
91 case SAMPLE_F32LE: {
92 *((float*)callBackStremInfo.inputData + i) = fixValue_;
93 break;
94 }
95 default:
96 break;
97 }
98 }
99 return 0;
100 }
101
OnStreamData(AudioCallBackStreamInfo & callBackStremInfo)102 int32_t WriteIncDataCb::OnStreamData(AudioCallBackStreamInfo& callBackStremInfo)
103 {
104 for (size_t i = 0; i < callBackStremInfo.requestDataLen /
105 HPAE::GetSizeFromFormat(format_); i++) {
106 switch (format_) {
107 case AudioSampleFormat::SAMPLE_U8: {
108 *(callBackStremInfo.inputData + i) = i;
109 break;
110 }
111 case SAMPLE_S16LE: {
112 *((int16_t*)callBackStremInfo.inputData + i) = i;
113 break;
114 }
115 case SAMPLE_S24LE: {
116 uint8_t *p = (uint8_t *)(callBackStremInfo.inputData + OFFSET_BIT_24 * i);
117 p[BIT_DEPTH_TWO] = (uint8_t) (i >> BIT_16);
118 p[1] = (uint8_t) (i >> BIT_8);
119 p[0] = (uint8_t) i;
120 break;
121 }
122 case SAMPLE_S32LE: {
123 *((int32_t*)callBackStremInfo.inputData + i) = i;
124 break;
125 }
126 case SAMPLE_F32LE: {
127 *((float*)callBackStremInfo.inputData + i) = i;
128 break;
129 }
130 default:
131 break;
132 }
133 }
134 writeNum_++;
135 return 0;
136 }
137
OnStatusUpdate(IOperation operation,uint32_t streamIndex)138 void StatusChangeCb::OnStatusUpdate(IOperation operation, uint32_t streamIndex)
139 {
140 switch (operation) {
141 case OPERATION_STARTED:
142 status_ = I_STATUS_STARTED;
143 break;
144 case OPERATION_PAUSED:
145 status_ = I_STATUS_PAUSED;
146 break;
147 case OPERATION_STOPPED:
148 status_ = I_STATUS_STOPPED;
149 break;
150 default:
151 status_ = I_STATUS_INVALID;
152 }
153 }
154
GetStatus()155 IStatus StatusChangeCb::GetStatus()
156 {
157 return status_;
158 }
159
ReadDataCb(const std::string & fileName)160 ReadDataCb::ReadDataCb(const std::string &fileName)
161 {
162 testFile_ = fopen(fileName.c_str(), "ab");
163 if (testFile_ == nullptr) {
164 AUDIO_ERR_LOG("Open file failed");
165 }
166 }
167
~ReadDataCb()168 ReadDataCb::~ReadDataCb()
169 {
170 if (testFile_) {
171 fclose(testFile_);
172 testFile_ = nullptr;
173 }
174 }
175
OnStreamData(AudioCallBackCapturerStreamInfo & callBackStreamInfo)176 int32_t ReadDataCb::OnStreamData(AudioCallBackCapturerStreamInfo &callBackStreamInfo)
177 {
178 return SUCCESS;
179 }
180
TestCapturerSourceFrame(char * frame,uint64_t requestBytes,uint64_t & replyBytes)181 void TestCapturerSourceFrame(char *frame, uint64_t requestBytes, uint64_t &replyBytes)
182 {
183 std::string filePath = g_rootCapturerPath;
184 std::string dirPath;
185 std::string fileName;
186
187 auto pos = filePath.rfind("/");
188 if (pos != std::string::npos) {
189 dirPath = filePath.substr(0, pos);
190 fileName = filePath.substr(pos);
191 }
192
193 char realPath[PATH_MAX + 1] = { 0x00 };
194 CHECK_AND_RETURN_LOG((filePath.length() < PATH_MAX) && (realpath(dirPath.c_str(), realPath) != nullptr),
195 "invalid path, errno: %{public}d", errno);
196
197 std::string realPathStr(realPath);
198 FILE *file = fopen(realPathStr.append(fileName).c_str(), "rb");
199 CHECK_AND_RETURN_LOG(file != nullptr, "open file fail, errno: %{public}d", errno);
200
201 if (feof(file)) {
202 AUDIO_INFO_LOG("reach end of the file, start reading from beginning");
203 rewind(file);
204 }
205 replyBytes = fread(frame, 1, requestBytes, file);
206 if (file != nullptr) {
207 fclose(file);
208 file = nullptr;
209 }
210 }
211 } // namespace AudioStandard
212 } // namespace OHOS