xref: /aosp_15_r20/external/tinyalsa_new/tests/src/pcm_in_test.cc (revision 02e95f1a335b55495d41ca67eaf42361f13704fa)
1 /* pcm_in_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 PcmInTest : public ::testing::Test {
42   protected:
PcmInTest()43     PcmInTest() : pcm_object(nullptr) {}
44     virtual ~PcmInTest() = default;
45 
SetUp()46     virtual void SetUp() override {
47         pcm_object = pcm_open(kLoopbackCard, kLoopbackCaptureDevice, PCM_IN, &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 = 0,
67         .stop_threshold = 0,
68         .silence_threshold = 0,
69         .silence_size = 0,
70     };
71 
72     pcm* pcm_object;
73 };
74 
TEST_F(PcmInTest,GetDelay)75 TEST_F(PcmInTest, GetDelay) {
76     pcm_prepare(pcm_object);
77     long delay = pcm_get_delay(pcm_object);
78     std::cout << delay << std::endl;
79     ASSERT_GE(delay, 0);
80 }
81 
TEST_F(PcmInTest,Readi)82 TEST_F(PcmInTest, Readi) {
83     constexpr uint32_t read_count = 20;
84 
85     size_t buffer_size = pcm_frames_to_bytes(pcm_object, kDefaultConfig.period_size);
86     auto buffer = std::make_unique<char[]>(buffer_size);
87 
88     int read_frames = 0;
89     unsigned int frames = pcm_bytes_to_frames(pcm_object, buffer_size);
90     auto start = std::chrono::steady_clock::now();
91     for (uint32_t i = 0; i < read_count; ++i) {
92         read_frames = pcm_readi(pcm_object, buffer.get(), frames);
93         ASSERT_EQ(read_frames, frames);
94     }
95 
96     std::chrono::duration<double> difference = std::chrono::steady_clock::now() - start;
97     std::chrono::milliseconds expected_elapsed_time_ms(frames * read_count /
98             (kDefaultConfig.rate / 1000));
99 
100     std::cout << difference.count() << std::endl;
101     std::cout << expected_elapsed_time_ms.count() << std::endl;
102 
103     ASSERT_NEAR(difference.count() * 1000, expected_elapsed_time_ms.count(), 100);
104 }
105 
TEST_F(PcmInTest,Writei)106 TEST_F(PcmInTest, Writei) {
107     size_t buffer_size = pcm_frames_to_bytes(pcm_object, kDefaultConfig.period_size);
108     auto buffer = std::make_unique<char[]>(buffer_size);
109 
110     unsigned int frames = pcm_bytes_to_frames(pcm_object, buffer_size);
111     ASSERT_EQ(pcm_writei(pcm_object, buffer.get(), frames), -EINVAL);
112 }
113 
114 } // namespace testing
115 } // namespace tinyalsa
116