aboutsummaryrefslogtreecommitdiff
path: root/tests/src/pcm_params_test.cc
blob: c8151e1194de03ae275305bf29d5715c377cec76 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/* pcm_params_test.c
**
** Copyright 2020, The Android Open Source Project
**
** Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are met:
**     * Redistributions of source code must retain the above copyright
**       notice, this list of conditions and the following disclaimer.
**     * Redistributions in binary form must reproduce the above copyright
**       notice, this list of conditions and the following disclaimer in the
**       documentation and/or other materials provided with the distribution.
**     * Neither the name of The Android Open Source Project nor the names of
**       its contributors may be used to endorse or promote products derived
**       from this software without specific prior written permission.
**
** THIS SOFTWARE IS PROVIDED BY The Android Open Source Project ``AS IS'' AND
** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
** ARE DISCLAIMED. IN NO EVENT SHALL The Android Open Source Project BE LIABLE
** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
** SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
** CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
** DAMAGE.
*/

#include "pcm_test_device.h"

#include <cstring>
#include <iostream>
#include <memory>

#include <gtest/gtest.h>

#include "tinyalsa/pcm.h"

namespace tinyalsa {
namespace testing {

static inline unsigned int OrAllBits(const pcm_mask *mask) {
    static constexpr size_t kTotalMaskBytes = 32;
    unsigned int res = 0;
    for (uint32_t i = 0; i < kTotalMaskBytes / sizeof(pcm_mask::bits[0]); ++i) {
        res |= mask->bits[i];
    }
    return res;
}

TEST(PcmParamsTest, GetAndFreeParams) {
    pcm_params *params = nullptr;

    // test to get nonexistent card and device.
    params = pcm_params_get(1000, 1000, PCM_IN);
    ASSERT_EQ(params, nullptr);

    // test free null params.
    pcm_params_free(params);

    // assume that card 0, device 0 is always available.
    params = pcm_params_get(0, 0, PCM_OUT);
    ASSERT_NE(params, nullptr);
    pcm_params_free(params);
}

TEST(PcmParamsTest, GetParamsBitMask) {
    // test to get mask with null params
    ASSERT_EQ(pcm_params_get_mask(nullptr, PCM_PARAM_ACCESS), nullptr);

    // assume that card 0, device 0 is always available.
    pcm_params *params = pcm_params_get(0, 0, PCM_OUT);
    ASSERT_NE(params, nullptr);

    // test to get param which is not described in bit mask format
    ASSERT_EQ(pcm_params_get_mask(params, PCM_PARAM_SAMPLE_BITS), nullptr);

    // test to get mask out of pcm_param enum
    ASSERT_EQ(pcm_params_get_mask(params, static_cast<pcm_param>(100)), nullptr);

    const pcm_mask *mask = pcm_params_get_mask(params, PCM_PARAM_ACCESS);
    ASSERT_NE(mask, nullptr);

    pcm_params_free(params);
}

TEST(PcmParamsTest, GetParamsInterval) {
    // test to get interval with null params
    ASSERT_EQ(pcm_params_get_min(nullptr, PCM_PARAM_SAMPLE_BITS), 0);
    ASSERT_EQ(pcm_params_get_max(nullptr, PCM_PARAM_SAMPLE_BITS), 0);

    // assume that card 0, device 0 is always available.
    pcm_params *params = pcm_params_get(0, 0, PCM_OUT);
    ASSERT_NE(params, nullptr);

    // test to get param which is not described in interval format
    ASSERT_EQ(pcm_params_get_min(params, PCM_PARAM_ACCESS), 0);
    ASSERT_EQ(pcm_params_get_max(params, PCM_PARAM_ACCESS), 0);

    // test to get interval out of pcm_param enum
    ASSERT_EQ(pcm_params_get_min(params, static_cast<pcm_param>(100)), 0);
    ASSERT_EQ(pcm_params_get_max(params, static_cast<pcm_param>(100)), 0);

    pcm_params_free(params);
}

TEST(PcmParamsTest, ParamsToString) {
    // assume that card 0, device 0 is always available.
    pcm_params *params = pcm_params_get(0, 0, PCM_OUT);
    ASSERT_NE(params, nullptr);

    char long_string[1024] = { 0 };
    int count = pcm_params_to_string(params, long_string, sizeof(long_string));
    ASSERT_LE(static_cast<size_t>(count), sizeof(long_string));
    ASSERT_GT(static_cast<size_t>(count), 0);

    char short_string[1] = { 0 };
    count = pcm_params_to_string(params, short_string, sizeof(short_string));
    ASSERT_GT(static_cast<size_t>(count), sizeof(short_string));

    int proper_string_len = count;
    int proper_string_size = proper_string_len + 1;
    auto proper_string = std::make_unique<char[]>(proper_string_size);
    count = pcm_params_to_string(params, proper_string.get(), proper_string_size);
    ASSERT_GT(static_cast<size_t>(count), 0);
    ASSERT_EQ(static_cast<size_t>(count), proper_string_len);
    ASSERT_EQ(std::strlen(proper_string.get()), proper_string_len);
    pcm_params_free(params);
}

TEST(PcmParamsTest, GetPlaybackDeviceParams) {
    pcm_params *params = pcm_params_get(kLoopbackCard, kLoopbackPlaybackDevice, PCM_OUT);
    ASSERT_NE(params, nullptr);

    const pcm_mask *access_mask = pcm_params_get_mask(params, PCM_PARAM_ACCESS);
    ASSERT_NE(access_mask, nullptr);
    ASSERT_NE(OrAllBits(access_mask), 0);

    const pcm_mask *format_mask = pcm_params_get_mask(params, PCM_PARAM_FORMAT);
    ASSERT_NE(format_mask, nullptr);
    ASSERT_NE(OrAllBits(format_mask), 0);

    const pcm_mask *subformat_mask = pcm_params_get_mask(params, PCM_PARAM_SUBFORMAT);
    ASSERT_NE(subformat_mask, nullptr);
    ASSERT_NE(OrAllBits(subformat_mask), 0);

    unsigned int sample_bits_min = pcm_params_get_min(params, PCM_PARAM_SAMPLE_BITS);
    unsigned int sample_bits_max = pcm_params_get_max(params, PCM_PARAM_SAMPLE_BITS);
    std::cout << "sample_bits: " << sample_bits_min << " - " << sample_bits_max << std::endl;
    ASSERT_GT(sample_bits_min, 0);
    ASSERT_GT(sample_bits_max, 0);

    unsigned int frame_bits_min = pcm_params_get_min(params, PCM_PARAM_FRAME_BITS);
    unsigned int frame_bits_max = pcm_params_get_max(params, PCM_PARAM_FRAME_BITS);
    std::cout << "frame_bits: " << frame_bits_min << " - " << frame_bits_max << std::endl;
    ASSERT_GT(frame_bits_min, 0);
    ASSERT_GT(frame_bits_max, 0);

    unsigned int channels_min = pcm_params_get_min(params, PCM_PARAM_CHANNELS);
    unsigned int channels_max = pcm_params_get_max(params, PCM_PARAM_CHANNELS);
    std::cout << "channels: " << channels_min << " - " << channels_max << std::endl;
    ASSERT_GT(channels_min, 0);
    ASSERT_GT(channels_max, 0);

    unsigned int sampling_rate_min = pcm_params_get_min(params, PCM_PARAM_RATE);
    unsigned int sampling_rate_max = pcm_params_get_max(params, PCM_PARAM_RATE);
    std::cout << "sampling_rate: " << sampling_rate_min << " - " << sampling_rate_max << std::endl;
    ASSERT_GT(sampling_rate_min, 0);
    ASSERT_GT(sampling_rate_max, 0);

    unsigned int period_time_min = pcm_params_get_min(params, PCM_PARAM_PERIOD_TIME);
    unsigned int period_time_max = pcm_params_get_max(params, PCM_PARAM_PERIOD_TIME);
    std::cout << "period_time: " << period_time_min << " - " << period_time_max << std::endl;
    ASSERT_GT(period_time_min, 0);
    ASSERT_GT(period_time_max, 0);

    unsigned int period_size_min = pcm_params_get_min(params, PCM_PARAM_PERIOD_SIZE);
    unsigned int period_size_max = pcm_params_get_max(params, PCM_PARAM_PERIOD_SIZE);
    std::cout << "period_size: " << period_size_min << " - " << period_size_max << std::endl;
    ASSERT_GT(period_size_min, 0);
    ASSERT_GT(period_size_max, 0);

    unsigned int period_bytes_min = pcm_params_get_min(params, PCM_PARAM_PERIOD_BYTES);
    unsigned int period_bytes_max = pcm_params_get_max(params, PCM_PARAM_PERIOD_BYTES);
    std::cout << "period_bytes: " << period_bytes_min << " - " << period_bytes_max << std::endl;
    ASSERT_GT(period_bytes_min, 0);
    ASSERT_GT(period_bytes_max, 0);

    unsigned int period_count_min = pcm_params_get_min(params, PCM_PARAM_PERIODS);
    unsigned int period_count_max = pcm_params_get_max(params, PCM_PARAM_PERIODS);
    std::cout << "period_count: " << period_count_min << " - " << period_count_max << std::endl;
    ASSERT_GT(period_count_min, 0);
    ASSERT_GT(period_count_max, 0);

    unsigned int buffer_time_min = pcm_params_get_min(params, PCM_PARAM_BUFFER_TIME);
    unsigned int buffer_time_max = pcm_params_get_max(params, PCM_PARAM_BUFFER_TIME);
    std::cout << "buffer_time: " << buffer_time_min << " - " << buffer_time_max << std::endl;
    ASSERT_GT(buffer_time_min, 0);
    ASSERT_GT(buffer_time_max, 0);

    unsigned int buffer_size_min = pcm_params_get_min(params, PCM_PARAM_BUFFER_SIZE);
    unsigned int buffer_size_max = pcm_params_get_max(params, PCM_PARAM_BUFFER_SIZE);
    std::cout << "buffer_size: " << buffer_size_min << " - " << buffer_size_max << std::endl;
    ASSERT_GT(buffer_size_min, 0);
    ASSERT_GT(buffer_size_max, 0);

    unsigned int buffer_bytes_min = pcm_params_get_min(params, PCM_PARAM_BUFFER_BYTES);
    unsigned int buffer_bytes_max = pcm_params_get_max(params, PCM_PARAM_BUFFER_BYTES);
    std::cout << "buffer_bytes: " << buffer_bytes_min << " - " << buffer_bytes_max << std::endl;
    ASSERT_GT(buffer_bytes_min, 0);
    ASSERT_GT(buffer_bytes_max, 0);

    unsigned int tick_in_us_min = pcm_params_get_min(params, PCM_PARAM_TICK_TIME);
    unsigned int tick_in_us_max = pcm_params_get_max(params, PCM_PARAM_TICK_TIME);
    ASSERT_GT(tick_in_us_max, 0);
    std::cout << "tick_in_us: " << tick_in_us_min << " - " << tick_in_us_max << std::endl;

    pcm_params_free(params);
}

} // namespace testing
} // namespace tinyalsa