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
29 #include <cstdio>
30 #include <fstream>
31 #include <iostream>
32 #include <memory>
33 #include <string_view>
34 #include <string>
35 #include <thread>
36
37 #include <gtest/gtest.h>
38
39 #include "tinyalsa/pcm.h"
40
41 #include "pcm_test_device.h"
42
43 namespace tinyalsa {
44 namespace testing {
45
TEST(PcmTest,FormatToBits)46 TEST(PcmTest, FormatToBits) {
47 // FIXME: Should we return 16 bits for INVALID?
48 ASSERT_EQ(pcm_format_to_bits(PCM_FORMAT_INVALID), 16);
49
50 ASSERT_EQ(pcm_format_to_bits(PCM_FORMAT_S16_LE), 16);
51 ASSERT_EQ(pcm_format_to_bits(PCM_FORMAT_S32_LE), 32);
52 ASSERT_EQ(pcm_format_to_bits(PCM_FORMAT_S8), 8);
53 ASSERT_EQ(pcm_format_to_bits(PCM_FORMAT_S24_LE), 32);
54 ASSERT_EQ(pcm_format_to_bits(PCM_FORMAT_S24_3LE), 24);
55 ASSERT_EQ(pcm_format_to_bits(PCM_FORMAT_S16_BE), 16);
56 ASSERT_EQ(pcm_format_to_bits(PCM_FORMAT_S24_BE), 32);
57 ASSERT_EQ(pcm_format_to_bits(PCM_FORMAT_S24_3BE), 24);
58 ASSERT_EQ(pcm_format_to_bits(PCM_FORMAT_S32_BE), 32);
59 ASSERT_EQ(pcm_format_to_bits(PCM_FORMAT_FLOAT_LE), 32);
60 ASSERT_EQ(pcm_format_to_bits(PCM_FORMAT_FLOAT_BE), 32);
61 }
62
TEST(PcmTest,OpenAndCloseOutPcm)63 TEST(PcmTest, OpenAndCloseOutPcm) {
64 pcm *pcm_object = pcm_open(1000, 1000, PCM_OUT, &kDefaultConfig);
65 ASSERT_FALSE(pcm_is_ready(pcm_object));
66 ASSERT_EQ(pcm_close(pcm_object), 0);
67
68 // assume card 0, device 0 is always available
69 pcm_object = pcm_open(0, 0, PCM_OUT, &kDefaultConfig);
70 ASSERT_TRUE(pcm_is_ready(pcm_object));
71 ASSERT_EQ(pcm_close(pcm_object), 0);
72
73 pcm_object = pcm_open(0, 0, PCM_OUT | PCM_MMAP, &kDefaultConfig);
74 ASSERT_TRUE(pcm_is_ready(pcm_object));
75 ASSERT_EQ(pcm_close(pcm_object), 0);
76
77 pcm_object = pcm_open(0, 0, PCM_OUT | PCM_MMAP | PCM_NOIRQ, &kDefaultConfig);
78 ASSERT_TRUE(pcm_is_ready(pcm_object));
79 ASSERT_EQ(pcm_close(pcm_object), 0);
80
81 pcm_object = pcm_open(0, 0, PCM_OUT | PCM_NONBLOCK, &kDefaultConfig);
82 ASSERT_TRUE(pcm_is_ready(pcm_object));
83 ASSERT_EQ(pcm_close(pcm_object), 0);
84
85 pcm_object = pcm_open(0, 0, PCM_OUT | PCM_MONOTONIC, &kDefaultConfig);
86 ASSERT_TRUE(pcm_is_ready(pcm_object));
87 ASSERT_EQ(pcm_close(pcm_object), 0);
88
89 std::string name = "hw:0,0";
90 pcm_object = pcm_open_by_name(name.c_str(), PCM_OUT, &kDefaultConfig);
91 ASSERT_TRUE(pcm_is_ready(pcm_object));
92 ASSERT_EQ(pcm_close(pcm_object), 0);
93 }
94
TEST(PcmTest,OpenWithoutBlocking)95 TEST(PcmTest, OpenWithoutBlocking) {
96 char loopback_device_info_path[120] = {};
97 snprintf(loopback_device_info_path, sizeof(loopback_device_info_path),
98 "/proc/asound/card%d/pcm%dp/info", kLoopbackCard, kLoopbackPlaybackDevice);
99
100 std::ifstream info_file_stream{loopback_device_info_path};
101 if (!info_file_stream.is_open()) {
102 GTEST_SKIP();
103 }
104
105 char buffer[256] = {};
106 int32_t subdevice_count = 0;
107 while (info_file_stream.good()) {
108 info_file_stream.getline(buffer, sizeof(buffer));
109 std::cout << buffer << std::endl;
110 std::string_view line{buffer};
111 if (line.find("subdevices_count") != std::string_view::npos) {
112 auto subdevice_count_string = line.substr(line.find(":") + 1);
113 std::cout << subdevice_count_string << std::endl;
114 subdevice_count = std::stoi(std::string{subdevice_count_string});
115 }
116 }
117
118 ASSERT_GT(subdevice_count, 0);
119
120 auto pcm_array = std::make_unique<pcm *[]>(subdevice_count);
121 std::thread *open_thread = new std::thread{[&pcm_array, subdevice_count] {
122 // Occupy all substreams
123 for (int32_t i = 0; i < subdevice_count; i++) {
124 pcm_array[i] = pcm_open(kLoopbackCard, kLoopbackPlaybackDevice, PCM_OUT,
125 &kDefaultConfig);
126 EXPECT_TRUE(pcm_is_ready(pcm_array[i]));
127 }
128
129 // Expect that pcm_open is not blocked in the kernel and return a bad_object pointer.
130 pcm *pcm_object = pcm_open(kLoopbackCard, kLoopbackPlaybackDevice, PCM_OUT,
131 &kDefaultConfig);
132 if (pcm_is_ready(pcm_object)) {
133 // open_thread is blocked in kernel because of the substream is all occupied. pcm_open
134 // returns because the main thread has released all pcm structures in pcm_array. We just
135 // need to close the pcm_object here.
136 pcm_close(pcm_object);
137 return;
138 }
139
140 // Release all substreams
141 for (int32_t i = 0; i < subdevice_count; i++) {
142 pcm_close(pcm_array[i]);
143 pcm_array[i] = nullptr;
144 }
145 }};
146
147 static constexpr int64_t kTimeoutMs = 500;
148 std::this_thread::sleep_for(std::chrono::milliseconds(kTimeoutMs));
149 if (pcm_array[0] == nullptr) {
150 open_thread->join();
151 } else {
152 for (int32_t i = 0; i < subdevice_count; i++) {
153 pcm_close(pcm_array[i]);
154 pcm_array[i] = nullptr;
155 }
156 open_thread->join();
157 FAIL() << "The open_thread is blocked in kernel or the kTimeoutMs(" << kTimeoutMs <<
158 ") is too short to complete";
159 }
160 }
161
162 } // namespace testing
163 } // namespace tinyalsa
164