• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* pcm_out_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 #include "pcm_test_device.h"
29 
30 #include <chrono>
31 #include <cstring>
32 #include <iostream>
33 
34 #include <gtest/gtest.h>
35 
36 #include "tinyalsa/pcm.h"
37 
38 namespace tinyalsa {
39 namespace testing {
40 
41 class PcmOutTest : public ::testing::Test {
42   protected:
PcmOutTest()43     PcmOutTest() : pcm_object(nullptr) {}
44     virtual ~PcmOutTest() = default;
45 
SetUp()46     virtual void SetUp() override {
47         pcm_object = pcm_open(kLoopbackCard, kLoopbackPlaybackDevice, PCM_OUT, &kDefaultConfig);
48         ASSERT_NE(pcm_object, nullptr);
49         ASSERT_TRUE(pcm_is_ready(pcm_object));
50     }
51 
TearDown()52     virtual void TearDown() override {
53         ASSERT_EQ(pcm_close(pcm_object), 0);
54     }
55 
56     static constexpr unsigned int kDefaultChannels = 2;
57     static constexpr unsigned int kDefaultSamplingRate = 48000;
58     static constexpr unsigned int kDefaultPeriodSize = 1024;
59     static constexpr unsigned int kDefaultPeriodCount = 3;
60     static constexpr pcm_config kDefaultConfig = {
61         .channels = kDefaultChannels,
62         .rate = kDefaultSamplingRate,
63         .period_size = kDefaultPeriodSize,
64         .period_count = kDefaultPeriodCount,
65         .format = PCM_FORMAT_S16_LE,
66         .start_threshold = kDefaultPeriodSize,
67         .stop_threshold = kDefaultPeriodSize * kDefaultPeriodCount,
68         .silence_threshold = 0,
69         .silence_size = 0,
70     };
71 
72     pcm* pcm_object;
73 };
74 
TEST_F(PcmOutTest,GetFileDescriptor)75 TEST_F(PcmOutTest, GetFileDescriptor) {
76     ASSERT_GT(pcm_get_file_descriptor(pcm_object), 0);
77 }
78 
TEST_F(PcmOutTest,GetChannels)79 TEST_F(PcmOutTest, GetChannels) {
80     ASSERT_EQ(pcm_get_channels(pcm_object), kDefaultConfig.channels);
81 }
82 
TEST_F(PcmOutTest,GetSamplingRate)83 TEST_F(PcmOutTest, GetSamplingRate) {
84     ASSERT_EQ(pcm_get_rate(pcm_object), kDefaultConfig.rate);
85 }
86 
TEST_F(PcmOutTest,GetFormat)87 TEST_F(PcmOutTest, GetFormat) {
88     ASSERT_EQ(pcm_get_format(pcm_object), kDefaultConfig.format);
89 
90 }
91 
TEST_F(PcmOutTest,GetErrorMessage)92 TEST_F(PcmOutTest, GetErrorMessage) {
93     ASSERT_STREQ(pcm_get_error(pcm_object), "");
94 }
95 
TEST_F(PcmOutTest,GetConfig)96 TEST_F(PcmOutTest, GetConfig) {
97     ASSERT_EQ(pcm_get_config(nullptr), nullptr);
98     ASSERT_EQ(std::memcmp(pcm_get_config(pcm_object), &kDefaultConfig, sizeof(pcm_config)), 0);
99 }
100 
TEST_F(PcmOutTest,SetConfig)101 TEST_F(PcmOutTest, SetConfig) {
102     ASSERT_EQ(pcm_set_config(nullptr, nullptr), -EFAULT);
103     ASSERT_EQ(pcm_set_config(pcm_object, nullptr), 0);
104 }
105 
TEST_F(PcmOutTest,GetBufferSize)106 TEST_F(PcmOutTest, GetBufferSize) {
107     unsigned int buffer_size = pcm_get_buffer_size(pcm_object);
108     ASSERT_EQ(buffer_size, kDefaultConfig.period_count * kDefaultConfig.period_size);
109 }
110 
TEST_F(PcmOutTest,FramesBytesConvert)111 TEST_F(PcmOutTest, FramesBytesConvert) {
112     unsigned int bytes = pcm_frames_to_bytes(pcm_object, 1);
113     ASSERT_EQ(bytes, pcm_format_to_bits(kDefaultConfig.format) / 8 * kDefaultConfig.channels);
114 
115     unsigned int frames = pcm_bytes_to_frames(pcm_object, bytes + 1);
116     ASSERT_EQ(frames, 1);
117 }
118 
TEST_F(PcmOutTest,GetAvailableAndTimestamp)119 TEST_F(PcmOutTest, GetAvailableAndTimestamp) {
120     unsigned int available = 0;
121     timespec time = { 0 };
122 
123     ASSERT_LT(pcm_get_htimestamp(nullptr, nullptr, nullptr), 0);
124 
125     ASSERT_EQ(pcm_get_htimestamp(pcm_object, &available, &time), 0);
126     ASSERT_NE(available, 0);
127     // ASSERT_NE(time.tv_nsec | time.tv_sec, 0);
128 }
129 
TEST_F(PcmOutTest,GetSubdevice)130 TEST_F(PcmOutTest, GetSubdevice) {
131     ASSERT_EQ(pcm_get_subdevice(pcm_object), 0);
132 }
133 
TEST_F(PcmOutTest,Readi)134 TEST_F(PcmOutTest, Readi) {
135     size_t buffer_size = pcm_frames_to_bytes(pcm_object, kDefaultConfig.period_size);
136     auto buffer = std::make_unique<char[]>(buffer_size);
137 
138     unsigned int frames = pcm_bytes_to_frames(pcm_object, buffer_size);
139     ASSERT_EQ(pcm_readi(pcm_object, buffer.get(), frames), -EINVAL);
140 }
141 
TEST_F(PcmOutTest,Writei)142 TEST_F(PcmOutTest, Writei) {
143     constexpr uint32_t write_count = 20;
144 
145     size_t buffer_size = pcm_frames_to_bytes(pcm_object, kDefaultConfig.period_size);
146     auto buffer = std::make_unique<char[]>(buffer_size);
147     for (uint32_t i = 0; i < buffer_size; ++i) {
148         buffer[i] = static_cast<char>(i);
149     }
150 
151     int written_frames = 0;
152     unsigned int frames = pcm_bytes_to_frames(pcm_object, buffer_size);
153     auto start = std::chrono::steady_clock::now();
154     for (uint32_t i = 0; i < write_count; ++i) {
155         written_frames = pcm_writei(pcm_object, buffer.get(), frames);
156         ASSERT_EQ(written_frames, frames);
157     }
158 
159     std::chrono::duration<double> difference = std::chrono::steady_clock::now() - start;
160     std::chrono::milliseconds expected_elapsed_time_ms(frames *
161             (write_count - kDefaultConfig.period_count) / (kDefaultConfig.rate / 1000));
162 
163     std::cout << difference.count() << std::endl;
164     std::cout << expected_elapsed_time_ms.count() << std::endl;
165 
166     ASSERT_NEAR(difference.count() * 1000, expected_elapsed_time_ms.count(), 100);
167 }
168 
169 class PcmOutMmapTest : public PcmOutTest {
170   protected:
171     PcmOutMmapTest() = default;
172     ~PcmOutMmapTest() = default;
173 
SetUp()174     virtual void SetUp() override {
175         pcm_object = pcm_open(kLoopbackCard, kLoopbackPlaybackDevice, PCM_OUT | PCM_MMAP,
176                 &kDefaultConfig);
177         ASSERT_NE(pcm_object, nullptr);
178         ASSERT_TRUE(pcm_is_ready(pcm_object));
179     }
180 
TearDown()181     virtual void TearDown() override {
182         ASSERT_EQ(pcm_close(pcm_object), 0);
183     }
184 };
185 
TEST_F(PcmOutMmapTest,Write)186 TEST_F(PcmOutMmapTest, Write) {
187     constexpr uint32_t write_count = 20;
188 
189     size_t buffer_size = pcm_frames_to_bytes(pcm_object, kDefaultConfig.period_size);
190     auto buffer = std::make_unique<char[]>(buffer_size);
191     for (uint32_t i = 0; i < buffer_size; ++i) {
192         buffer[i] = static_cast<char>(i);
193     }
194 
195     int res = 0;
196     unsigned int frames = pcm_bytes_to_frames(pcm_object, buffer_size);
197     pcm_start(pcm_object);
198     auto start = std::chrono::steady_clock::now();
199     for (uint32_t i = 0; i < write_count; ++i) {
200         res = pcm_mmap_write(pcm_object, buffer.get(), buffer_size);
201         ASSERT_EQ(res, 0);
202     }
203     pcm_stop(pcm_object);
204 
205     std::chrono::duration<double> difference = std::chrono::steady_clock::now() - start;
206     std::chrono::milliseconds expected_elapsed_time_ms(frames *
207             (write_count - kDefaultConfig.period_count) / (kDefaultConfig.rate / 1000));
208 
209     std::cout << difference.count() << std::endl;
210     std::cout << expected_elapsed_time_ms.count() << std::endl;
211 
212     ASSERT_NEAR(difference.count() * 1000, expected_elapsed_time_ms.count(), 100);
213 }
214 
215 } // namespace testing
216 } // namespace tinyalsa
217