1 /*
2 * Copyright (c) 2022 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 "hdf_audio_events.h"
17 #include "hdf_io_service_if.h"
18 #include "hdf_remote_adapter_if.h"
19 #include "hdf_service_status.h"
20 #include "ioservstat_listener.h"
21 #include "servstat_listener_hdi.h"
22 #include "svcmgr_ioservice.h"
23 #include "audio_events.h"
24 #include "audio_hdi_common.h"
25
26 using namespace std;
27 using namespace testing::ext;
28 using namespace OHOS::Audio;
29
30 namespace {
31 const string CODEC_SERVICE_NAME = "hdf_audio_codec_primary_dev0";
32 constexpr int BUFFER_SIZE_BIT = 16 * 1024;
33 constexpr uint64_t FILE_SIZE_BYTE = 64;
34 constexpr uint64_t FILE_SIZE_BIT = FILE_SIZE_BYTE * 1024;
35 uint32_t g_reportCount = 0;
36 static void AudioThresholdReportReceived(struct ServiceStatusListener *listener, struct ServiceStatus *svcStatus);
37 class AudioThresholdReportTest : public testing::Test {
38 public:
39 static void SetUpTestCase(void);
40 static void TearDownTestCase(void);
41 void SetUp();
42 void TearDown();
43 static struct ISvcMgrIoservice *servmgr;
44 static struct ServiceStatusListener *listener;
45 static TestAudioManager *manager;
46 };
47
48 TestAudioManager *AudioThresholdReportTest::manager = nullptr;
49 struct ISvcMgrIoservice *AudioThresholdReportTest::servmgr = nullptr;
50 struct ServiceStatusListener *AudioThresholdReportTest::listener = nullptr;
51 using THREAD_FUNC = void *(*)(void *);
SetUpTestCase(void)52 void AudioThresholdReportTest::SetUpTestCase(void)
53 {
54 (void)HdfRemoteGetCallingPid();
55 ASSERT_NE(nullptr, GetAudioManagerFuncs());
56 manager = getAudioManager();
57 ASSERT_NE(nullptr, manager);
58 servmgr = SvcMgrIoserviceGet();
59 ASSERT_NE(nullptr, servmgr);
60 listener = IoServiceStatusListenerNewInstance();
61 ASSERT_NE(nullptr, listener);
62
63 listener->callback = AudioThresholdReportReceived;
64 int status = servmgr->RegisterServiceStatusListener(servmgr, listener, DEVICE_CLASS_AUDIO);
65 ASSERT_EQ(HDF_SUCCESS, status);
66 }
67
TearDownTestCase(void)68 void AudioThresholdReportTest::TearDownTestCase(void)
69 {
70 if (manager != nullptr) {
71 (void)manager->ReleaseAudioManagerObject(manager);
72 manager = nullptr;
73 }
74 (void)servmgr->UnregisterServiceStatusListener(servmgr, listener);
75 (void)HdiServiceStatusListenerFree(listener);
76 listener = nullptr;
77 (void)SvcMgrIoserviceRelease(servmgr);
78 servmgr = nullptr;
79 }
80
SetUp(void)81 void AudioThresholdReportTest::SetUp(void) {}
82
TearDown(void)83 void AudioThresholdReportTest::TearDown(void) {}
84
AudioThresholdReportReceived(struct ServiceStatusListener * listener,struct ServiceStatus * svcStatus)85 void AudioThresholdReportReceived(struct ServiceStatusListener *listener, struct ServiceStatus *svcStatus)
86 {
87 if (listener == nullptr || svcStatus == nullptr) {
88 return;
89 }
90 struct AudioEvent thresholdReportEvent = {};
91 if (!strcmp(svcStatus->serviceName, CODEC_SERVICE_NAME.c_str())) {
92 AudioPnpMsgReadValue(svcStatus->info, "EVENT_TYPE", &(thresholdReportEvent.eventType));
93 AudioPnpMsgReadValue(svcStatus->info, "DEVICE_TYPE", &(thresholdReportEvent.deviceType));
94 }
95 if (thresholdReportEvent.eventType == HDF_AUDIO_CAPTURE_THRESHOLD &&
96 thresholdReportEvent.deviceType == HDF_AUDIO_PRIMARY_DEVICE) {
97 g_reportCount++;
98 }
99 }
100
101 /**
102 * @tc.name AudioThresholdCaptureReport_001
103 * @tc.desc test Threshold Reporting function ,Start recording can be reported.
104 * @tc.type: FUNC
105 */
106 HWTEST_F(AudioThresholdReportTest, AudioThresholdCaptureReport_001, TestSize.Level1)
107 {
108 int32_t ret = -1;
109 uint64_t replyBytes = 0;
110 uint64_t requestBytes = BUFFER_SIZE_BIT;
111 struct AudioAdapter *adapter = nullptr;
112 struct AudioCapture *capture = nullptr;
113 g_reportCount = 0;
114 uint32_t expectReportCount = 1;
115 ASSERT_NE(nullptr, manager);
116 ret = AudioCreateCapture(manager, PIN_IN_MIC, ADAPTER_NAME, &adapter, &capture);
117 ASSERT_EQ(AUDIO_HAL_SUCCESS, ret);
118 ret = capture->control.Start((AudioHandle)capture);
119 EXPECT_EQ(AUDIO_HAL_SUCCESS, ret);
120 char *frame = (char *)calloc(1, BUFFER_SIZE_BIT);
121 EXPECT_NE(nullptr, frame);
122 ret = capture->CaptureFrame(capture, frame, requestBytes, &replyBytes);
123 EXPECT_EQ(AUDIO_HAL_SUCCESS, ret);
124 EXPECT_EQ(expectReportCount, g_reportCount);
125 capture->control.Stop((AudioHandle)capture);
126 adapter->DestroyCapture(adapter, capture);
127 manager->UnloadAdapter(manager, adapter);
128 if (frame != nullptr) {
129 free(frame);
130 frame = nullptr;
131 }
132 }
133 /**
134 * @tc.name AudioThresholdCaptureReport_002
135 * @tc.desc test Threshold Reporting function is normal,when Recording.
136 * @tc.type: FUNC
137 */
138 HWTEST_F(AudioThresholdReportTest, AudioThresholdCaptureReport_002, TestSize.Level1)
139 {
140 int32_t ret = -1;
141 g_reportCount =0;
142 ASSERT_NE(nullptr, manager);
143 uint32_t expectReportCount = FILE_SIZE_BIT / BUFFER_SIZE_BIT + 1;
144 struct PrepareAudioPara audiopara = {
145 .manager = manager, .adapterName = ADAPTER_NAME.c_str(), .pins = PIN_IN_MIC,
146 .path = AUDIO_CAPTURE_FILE.c_str(), .fileSize = FILE_SIZE_BYTE
147 };
148
149 ret = pthread_create(&audiopara.tids, NULL, (THREAD_FUNC)RecordAudio, &audiopara);
150 ASSERT_EQ(AUDIO_HAL_SUCCESS, ret);
151 ret = ThreadRelease(audiopara);
152 EXPECT_EQ(AUDIO_HAL_SUCCESS, ret);
153 EXPECT_EQ(expectReportCount, g_reportCount);
154 }
155 /**
156 * @tc.name AudioThresholdCaptureReport_003
157 * @tc.desc test Threshold Reporting function,No threshold reporting when no recording.
158 * @tc.type: FUNC
159 */
160 HWTEST_F(AudioThresholdReportTest, AudioThresholdCaptureReport_003, TestSize.Level1)
161 {
162 g_reportCount = 0;
163 int32_t ret = -1;
164 uint64_t requestBytes = 0;
165 uint64_t replyBytes = 0;
166 uint32_t expectReportCount = 0;
167 AudioPortPin pins = PIN_OUT_SPEAKER;
168 struct AudioAdapter *adapter = nullptr;
169 struct AudioRender *render = nullptr;
170 char *frame = nullptr;
171 ASSERT_NE(nullptr, manager);
172 ret = AudioCreateRender(manager, pins, ADAPTER_NAME, &adapter, &render);
173 ASSERT_EQ(AUDIO_HAL_SUCCESS, ret);
174 ret = render->control.Start((AudioHandle)render);
175 EXPECT_EQ(AUDIO_HAL_SUCCESS, ret);
176
177 ret = RenderFramePrepare(AUDIO_FILE, frame, requestBytes);
178 EXPECT_EQ(AUDIO_HAL_SUCCESS, ret);
179 ret = render->RenderFrame(render, frame, requestBytes, &replyBytes);
180 EXPECT_EQ(AUDIO_HAL_SUCCESS, ret);
181
182 render->control.Stop((AudioHandle)render);
183 EXPECT_EQ(expectReportCount, g_reportCount);
184 adapter->DestroyRender(adapter, render);
185 manager->UnloadAdapter(manager, adapter);
186 if (frame != nullptr) {
187 free(frame);
188 frame = nullptr;
189 }
190 }
191 }
192