xref: /aosp_15_r20/external/tinyalsa_new/tests/src/pcm_params_test.cc (revision 02e95f1a335b55495d41ca67eaf42361f13704fa)
1 /* pcm_params_test.c
2 **
3 ** Copyright 2020, The Android Open Source Project
4 **
5 ** Redistribution and use in source and binary forms, with or without
6 ** modification, are permitted provided that the following conditions are met:
7 **     * Redistributions of source code must retain the above copyright
8 **       notice, this list of conditions and the following disclaimer.
9 **     * Redistributions in binary form must reproduce the above copyright
10 **       notice, this list of conditions and the following disclaimer in the
11 **       documentation and/or other materials provided with the distribution.
12 **     * Neither the name of The Android Open Source Project nor the names of
13 **       its contributors may be used to endorse or promote products derived
14 **       from this software without specific prior written permission.
15 **
16 ** THIS SOFTWARE IS PROVIDED BY The Android Open Source Project ``AS IS'' AND
17 ** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 ** ARE DISCLAIMED. IN NO EVENT SHALL The Android Open Source Project BE LIABLE
20 ** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 ** SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 ** CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 ** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 ** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
26 ** DAMAGE.
27 */
28 
29 #include "pcm_test_device.h"
30 
31 #include <cstring>
32 #include <iostream>
33 #include <memory>
34 
35 #include <gtest/gtest.h>
36 
37 #include "tinyalsa/pcm.h"
38 
39 namespace tinyalsa {
40 namespace testing {
41 
OrAllBits(const pcm_mask * mask)42 static inline unsigned int OrAllBits(const pcm_mask *mask) {
43     static constexpr size_t kTotalMaskBytes = 32;
44     unsigned int res = 0;
45     for (uint32_t i = 0; i < kTotalMaskBytes / sizeof(pcm_mask::bits[0]); ++i) {
46         res |= mask->bits[i];
47     }
48     return res;
49 }
50 
TEST(PcmParamsTest,GetAndFreeParams)51 TEST(PcmParamsTest, GetAndFreeParams) {
52     pcm_params *params = nullptr;
53 
54     // test to get nonexistent card and device.
55     params = pcm_params_get(1000, 1000, PCM_IN);
56     ASSERT_EQ(params, nullptr);
57 
58     // test free null params.
59     pcm_params_free(params);
60 
61     // assume that card 0, device 0 is always available.
62     params = pcm_params_get(0, 0, PCM_OUT);
63     ASSERT_NE(params, nullptr);
64     pcm_params_free(params);
65 }
66 
TEST(PcmParamsTest,GetParamsBitMask)67 TEST(PcmParamsTest, GetParamsBitMask) {
68     // test to get mask with null params
69     ASSERT_EQ(pcm_params_get_mask(nullptr, PCM_PARAM_ACCESS), nullptr);
70 
71     // assume that card 0, device 0 is always available.
72     pcm_params *params = pcm_params_get(0, 0, PCM_OUT);
73     ASSERT_NE(params, nullptr);
74 
75     // test to get param which is not described in bit mask format
76     ASSERT_EQ(pcm_params_get_mask(params, PCM_PARAM_SAMPLE_BITS), nullptr);
77 
78     // test to get mask out of pcm_param enum
79     ASSERT_EQ(pcm_params_get_mask(params, static_cast<pcm_param>(100)), nullptr);
80 
81     const pcm_mask *mask = pcm_params_get_mask(params, PCM_PARAM_ACCESS);
82     ASSERT_NE(mask, nullptr);
83 
84     pcm_params_free(params);
85 }
86 
TEST(PcmParamsTest,GetParamsInterval)87 TEST(PcmParamsTest, GetParamsInterval) {
88     // test to get interval with null params
89     ASSERT_EQ(pcm_params_get_min(nullptr, PCM_PARAM_SAMPLE_BITS), 0);
90     ASSERT_EQ(pcm_params_get_max(nullptr, PCM_PARAM_SAMPLE_BITS), 0);
91 
92     // assume that card 0, device 0 is always available.
93     pcm_params *params = pcm_params_get(0, 0, PCM_OUT);
94     ASSERT_NE(params, nullptr);
95 
96     // test to get param which is not described in interval format
97     ASSERT_EQ(pcm_params_get_min(params, PCM_PARAM_ACCESS), 0);
98     ASSERT_EQ(pcm_params_get_max(params, PCM_PARAM_ACCESS), 0);
99 
100     // test to get interval out of pcm_param enum
101     ASSERT_EQ(pcm_params_get_min(params, static_cast<pcm_param>(100)), 0);
102     ASSERT_EQ(pcm_params_get_max(params, static_cast<pcm_param>(100)), 0);
103 
104     pcm_params_free(params);
105 }
106 
TEST(PcmParamsTest,ParamsToString)107 TEST(PcmParamsTest, ParamsToString) {
108     // assume that card 0, device 0 is always available.
109     pcm_params *params = pcm_params_get(0, 0, PCM_OUT);
110     ASSERT_NE(params, nullptr);
111 
112     char long_string[1024] = { 0 };
113     int count = pcm_params_to_string(params, long_string, sizeof(long_string));
114     ASSERT_LE(static_cast<size_t>(count), sizeof(long_string));
115     ASSERT_GT(static_cast<size_t>(count), 0);
116 
117     char short_string[1] = { 0 };
118     count = pcm_params_to_string(params, short_string, sizeof(short_string));
119     ASSERT_GT(static_cast<size_t>(count), sizeof(short_string));
120 
121     int proper_string_len = count;
122     int proper_string_size = proper_string_len + 1;
123     auto proper_string = std::make_unique<char[]>(proper_string_size);
124     count = pcm_params_to_string(params, proper_string.get(), proper_string_size);
125     ASSERT_GT(static_cast<size_t>(count), 0);
126     ASSERT_EQ(static_cast<size_t>(count), proper_string_len);
127     ASSERT_EQ(std::strlen(proper_string.get()), proper_string_len);
128     pcm_params_free(params);
129 }
130 
TEST(PcmParamsTest,GetPlaybackDeviceParams)131 TEST(PcmParamsTest, GetPlaybackDeviceParams) {
132     pcm_params *params = pcm_params_get(kLoopbackCard, kLoopbackPlaybackDevice, PCM_OUT);
133     ASSERT_NE(params, nullptr);
134 
135     const pcm_mask *access_mask = pcm_params_get_mask(params, PCM_PARAM_ACCESS);
136     ASSERT_NE(access_mask, nullptr);
137     ASSERT_NE(OrAllBits(access_mask), 0);
138 
139     const pcm_mask *format_mask = pcm_params_get_mask(params, PCM_PARAM_FORMAT);
140     ASSERT_NE(format_mask, nullptr);
141     ASSERT_NE(OrAllBits(format_mask), 0);
142 
143     const pcm_mask *subformat_mask = pcm_params_get_mask(params, PCM_PARAM_SUBFORMAT);
144     ASSERT_NE(subformat_mask, nullptr);
145     ASSERT_NE(OrAllBits(subformat_mask), 0);
146 
147     unsigned int sample_bits_min = pcm_params_get_min(params, PCM_PARAM_SAMPLE_BITS);
148     unsigned int sample_bits_max = pcm_params_get_max(params, PCM_PARAM_SAMPLE_BITS);
149     std::cout << "sample_bits: " << sample_bits_min << " - " << sample_bits_max << std::endl;
150     ASSERT_GT(sample_bits_min, 0);
151     ASSERT_GT(sample_bits_max, 0);
152 
153     unsigned int frame_bits_min = pcm_params_get_min(params, PCM_PARAM_FRAME_BITS);
154     unsigned int frame_bits_max = pcm_params_get_max(params, PCM_PARAM_FRAME_BITS);
155     std::cout << "frame_bits: " << frame_bits_min << " - " << frame_bits_max << std::endl;
156     ASSERT_GT(frame_bits_min, 0);
157     ASSERT_GT(frame_bits_max, 0);
158 
159     unsigned int channels_min = pcm_params_get_min(params, PCM_PARAM_CHANNELS);
160     unsigned int channels_max = pcm_params_get_max(params, PCM_PARAM_CHANNELS);
161     std::cout << "channels: " << channels_min << " - " << channels_max << std::endl;
162     ASSERT_GT(channels_min, 0);
163     ASSERT_GT(channels_max, 0);
164 
165     unsigned int sampling_rate_min = pcm_params_get_min(params, PCM_PARAM_RATE);
166     unsigned int sampling_rate_max = pcm_params_get_max(params, PCM_PARAM_RATE);
167     std::cout << "sampling_rate: " << sampling_rate_min << " - " << sampling_rate_max << std::endl;
168     ASSERT_GT(sampling_rate_min, 0);
169     ASSERT_GT(sampling_rate_max, 0);
170 
171     unsigned int period_time_min = pcm_params_get_min(params, PCM_PARAM_PERIOD_TIME);
172     unsigned int period_time_max = pcm_params_get_max(params, PCM_PARAM_PERIOD_TIME);
173     std::cout << "period_time: " << period_time_min << " - " << period_time_max << std::endl;
174     ASSERT_GT(period_time_min, 0);
175     ASSERT_GT(period_time_max, 0);
176 
177     unsigned int period_size_min = pcm_params_get_min(params, PCM_PARAM_PERIOD_SIZE);
178     unsigned int period_size_max = pcm_params_get_max(params, PCM_PARAM_PERIOD_SIZE);
179     std::cout << "period_size: " << period_size_min << " - " << period_size_max << std::endl;
180     ASSERT_GT(period_size_min, 0);
181     ASSERT_GT(period_size_max, 0);
182 
183     unsigned int period_bytes_min = pcm_params_get_min(params, PCM_PARAM_PERIOD_BYTES);
184     unsigned int period_bytes_max = pcm_params_get_max(params, PCM_PARAM_PERIOD_BYTES);
185     std::cout << "period_bytes: " << period_bytes_min << " - " << period_bytes_max << std::endl;
186     ASSERT_GT(period_bytes_min, 0);
187     ASSERT_GT(period_bytes_max, 0);
188 
189     unsigned int period_count_min = pcm_params_get_min(params, PCM_PARAM_PERIODS);
190     unsigned int period_count_max = pcm_params_get_max(params, PCM_PARAM_PERIODS);
191     std::cout << "period_count: " << period_count_min << " - " << period_count_max << std::endl;
192     ASSERT_GT(period_count_min, 0);
193     ASSERT_GT(period_count_max, 0);
194 
195     unsigned int buffer_time_min = pcm_params_get_min(params, PCM_PARAM_BUFFER_TIME);
196     unsigned int buffer_time_max = pcm_params_get_max(params, PCM_PARAM_BUFFER_TIME);
197     std::cout << "buffer_time: " << buffer_time_min << " - " << buffer_time_max << std::endl;
198     ASSERT_GT(buffer_time_min, 0);
199     ASSERT_GT(buffer_time_max, 0);
200 
201     unsigned int buffer_size_min = pcm_params_get_min(params, PCM_PARAM_BUFFER_SIZE);
202     unsigned int buffer_size_max = pcm_params_get_max(params, PCM_PARAM_BUFFER_SIZE);
203     std::cout << "buffer_size: " << buffer_size_min << " - " << buffer_size_max << std::endl;
204     ASSERT_GT(buffer_size_min, 0);
205     ASSERT_GT(buffer_size_max, 0);
206 
207     unsigned int buffer_bytes_min = pcm_params_get_min(params, PCM_PARAM_BUFFER_BYTES);
208     unsigned int buffer_bytes_max = pcm_params_get_max(params, PCM_PARAM_BUFFER_BYTES);
209     std::cout << "buffer_bytes: " << buffer_bytes_min << " - " << buffer_bytes_max << std::endl;
210     ASSERT_GT(buffer_bytes_min, 0);
211     ASSERT_GT(buffer_bytes_max, 0);
212 
213     unsigned int tick_in_us_min = pcm_params_get_min(params, PCM_PARAM_TICK_TIME);
214     unsigned int tick_in_us_max = pcm_params_get_max(params, PCM_PARAM_TICK_TIME);
215     ASSERT_GT(tick_in_us_max, 0);
216     std::cout << "tick_in_us: " << tick_in_us_min << " - " << tick_in_us_max << std::endl;
217 
218     pcm_params_free(params);
219 }
220 
221 } // namespace testing
222 } // namespace tinyalsa
223