From 0eafdbba49227560652325d551b05fff0f374cbc Mon Sep 17 00:00:00 2001 From: dvdli Date: Mon, 16 Nov 2020 17:07:04 +0800 Subject: add unit tests for pcm_* functions --- .gitignore | 1 + BUILD | 57 +++++++++++ WORKSPACE | 7 ++ tests/include/pcm_test_device.h | 49 +++++++++ tests/src/pcm_out_test.cc | 216 +++++++++++++++++++++++++++++++++++++++ tests/src/pcm_params_test.cc | 221 ++++++++++++++++++++++++++++++++++++++++ tests/src/pcm_test.cc | 103 +++++++++++++++++++ 7 files changed, 654 insertions(+) create mode 100644 BUILD create mode 100644 WORKSPACE create mode 100644 tests/include/pcm_test_device.h create mode 100644 tests/src/pcm_out_test.cc create mode 100644 tests/src/pcm_params_test.cc create mode 100644 tests/src/pcm_test.cc diff --git a/.gitignore b/.gitignore index befe4ba..8773c34 100644 --- a/.gitignore +++ b/.gitignore @@ -20,3 +20,4 @@ /utils/tinymix /utils/tinypcminfo +/bazel* diff --git a/BUILD b/BUILD new file mode 100644 index 0000000..dba4ab5 --- /dev/null +++ b/BUILD @@ -0,0 +1,57 @@ +# BUILD +# +# 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. +# + +cc_library( + name = "tinyalsa", + srcs = glob(["src/*.c"]), + includes = ["include"], + hdrs = glob([ + "include/**/*.h", + "src/*.h", + ]), + visibility = ["//visibility:public"], +) + +cc_test( + name = "tinyalsa_tests", + srcs = glob([ + "tests/src/*.cc", + "tests/include/*.h", + ]), + includes = ["tests/include"], + deps = [ + "//:tinyalsa", + "@googletest//:gtest_main" + ], + linkopts = [ + "-ldl", + ], + copts = [ + "-std=c++17", + ], +) diff --git a/WORKSPACE b/WORKSPACE new file mode 100644 index 0000000..02b57bf --- /dev/null +++ b/WORKSPACE @@ -0,0 +1,7 @@ +load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository") + +git_repository( + name = "googletest", + remote = "https://github.com/google/googletest", + branch = "master", +) diff --git a/tests/include/pcm_test_device.h b/tests/include/pcm_test_device.h new file mode 100644 index 0000000..1932737 --- /dev/null +++ b/tests/include/pcm_test_device.h @@ -0,0 +1,49 @@ +/* pcm_test.h +** +** 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. +*/ + +#ifndef TINYALSA_TESTS_PCM_TEST_H_ +#define TINYALSA_TESTS_PCM_TEST_H_ + +namespace tinyalsa { +namespace testing { + +#ifndef TEST_LOOPBACK_CARD +#define TEST_LOOPBACK_CARD 2 +#endif + +#ifndef TEST_LOOPBACK_PALYBACK_DEVICE +#define TEST_LOOPBACK_PALYBACK_DEVICE 0 +#endif + +constexpr unsigned int kLoopbackCard = TEST_LOOPBACK_CARD; +constexpr unsigned int kLoopbackPlaybackDevice = TEST_LOOPBACK_PALYBACK_DEVICE; + +} // namespace testing +} // namespace tinyalse + +#endif diff --git a/tests/src/pcm_out_test.cc b/tests/src/pcm_out_test.cc new file mode 100644 index 0000000..5939565 --- /dev/null +++ b/tests/src/pcm_out_test.cc @@ -0,0 +1,216 @@ +/* pcm_out_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 +#include +#include + +#include + +#include "tinyalsa/pcm.h" + +namespace tinyalsa { +namespace testing { + +class PcmOutTest : public ::testing::Test { + protected: + PcmOutTest() : pcm_object(nullptr) {} + virtual ~PcmOutTest() = default; + + virtual void SetUp() override { + pcm_object = pcm_open(kLoopbackCard, kLoopbackPlaybackDevice, PCM_OUT, &kDefaultConfig); + ASSERT_NE(pcm_object, nullptr); + ASSERT_TRUE(pcm_is_ready(pcm_object)); + } + + virtual void TearDown() override { + ASSERT_EQ(pcm_close(pcm_object), 0); + } + + static constexpr unsigned int kDefaultChannels = 2; + static constexpr unsigned int kDefaultSamplingRate = 48000; + static constexpr unsigned int kDefaultPeriodSize = 1024; + static constexpr unsigned int kDefaultPeriodCount = 3; + static constexpr pcm_config kDefaultConfig = { + .channels = kDefaultChannels, + .rate = kDefaultSamplingRate, + .period_size = kDefaultPeriodSize, + .period_count = kDefaultPeriodCount, + .format = PCM_FORMAT_S16_LE, + .start_threshold = kDefaultPeriodSize, + .stop_threshold = kDefaultPeriodSize * kDefaultPeriodCount, + .silence_threshold = 0, + .silence_size = 0, + }; + + pcm* pcm_object; +}; + +TEST_F(PcmOutTest, GetFileDescriptor) { + ASSERT_GT(pcm_get_file_descriptor(pcm_object), 0); +} + +TEST_F(PcmOutTest, GetChannels) { + ASSERT_EQ(pcm_get_channels(pcm_object), kDefaultConfig.channels); +} + +TEST_F(PcmOutTest, GetSamplingRate) { + ASSERT_EQ(pcm_get_rate(pcm_object), kDefaultConfig.rate); +} + +TEST_F(PcmOutTest, GetFormat) { + ASSERT_EQ(pcm_get_format(pcm_object), kDefaultConfig.format); + +} + +TEST_F(PcmOutTest, GetErrorMessage) { + ASSERT_STREQ(pcm_get_error(pcm_object), ""); +} + +TEST_F(PcmOutTest, GetConfig) { + ASSERT_EQ(pcm_get_config(nullptr), nullptr); + ASSERT_EQ(std::memcmp(pcm_get_config(pcm_object), &kDefaultConfig, sizeof(pcm_config)), 0); +} + +TEST_F(PcmOutTest, SetConfig) { + ASSERT_EQ(pcm_set_config(nullptr, nullptr), -EFAULT); + ASSERT_EQ(pcm_set_config(pcm_object, nullptr), 0); +} + +TEST_F(PcmOutTest, GetBufferSize) { + unsigned int buffer_size = pcm_get_buffer_size(pcm_object); + ASSERT_EQ(buffer_size, kDefaultConfig.period_count * kDefaultConfig.period_size); +} + +TEST_F(PcmOutTest, FramesBytesConvert) { + unsigned int bytes = pcm_frames_to_bytes(pcm_object, 1); + ASSERT_EQ(bytes, pcm_format_to_bits(kDefaultConfig.format) / 8 * kDefaultConfig.channels); + + unsigned int frames = pcm_bytes_to_frames(pcm_object, bytes + 1); + ASSERT_EQ(frames, 1); +} + +TEST_F(PcmOutTest, GetAvailableAndTimestamp) { + unsigned int available = 0; + timespec time = { 0 }; + + ASSERT_LT(pcm_get_htimestamp(nullptr, nullptr, nullptr), 0); + + ASSERT_EQ(pcm_get_htimestamp(pcm_object, &available, &time), 0); + ASSERT_NE(available, 0); + // ASSERT_NE(time.tv_nsec | time.tv_sec, 0); +} + +TEST_F(PcmOutTest, GetSubdevice) { + ASSERT_EQ(pcm_get_subdevice(pcm_object), 0); +} + +TEST_F(PcmOutTest, Readi) { + size_t buffer_size = pcm_frames_to_bytes(pcm_object, kDefaultConfig.period_size); + auto buffer = std::make_unique(buffer_size); + + unsigned int frames = pcm_bytes_to_frames(pcm_object, buffer_size); + ASSERT_EQ(pcm_readi(pcm_object, buffer.get(), frames), -EINVAL); +} + +TEST_F(PcmOutTest, Writei) { + constexpr uint32_t write_count = 20; + + size_t buffer_size = pcm_frames_to_bytes(pcm_object, kDefaultConfig.period_size); + auto buffer = std::make_unique(buffer_size); + for (uint32_t i = 0; i < buffer_size; ++i) { + buffer[i] = static_cast(i); + } + + int written_frames = 0; + unsigned int frames = pcm_bytes_to_frames(pcm_object, buffer_size); + auto start = std::chrono::steady_clock::now(); + for (uint32_t i = 0; i < write_count; ++i) { + written_frames = pcm_writei(pcm_object, buffer.get(), frames); + ASSERT_EQ(written_frames, frames); + } + + std::chrono::duration difference = std::chrono::steady_clock::now() - start; + std::chrono::milliseconds expected_elapsed_time_ms(frames * + (write_count - kDefaultConfig.period_count) / (kDefaultConfig.rate / 1000)); + + std::cout << difference.count() << std::endl; + std::cout << expected_elapsed_time_ms.count() << std::endl; + + ASSERT_NEAR(difference.count() * 1000, expected_elapsed_time_ms.count(), 100); +} + +class PcmOutMmapTest : public PcmOutTest { + protected: + PcmOutMmapTest() = default; + ~PcmOutMmapTest() = default; + + virtual void SetUp() override { + pcm_object = pcm_open(kLoopbackCard, kLoopbackPlaybackDevice, PCM_OUT | PCM_MMAP, + &kDefaultConfig); + ASSERT_NE(pcm_object, nullptr); + ASSERT_TRUE(pcm_is_ready(pcm_object)); + } + + virtual void TearDown() override { + ASSERT_EQ(pcm_close(pcm_object), 0); + } +}; + +TEST_F(PcmOutMmapTest, Write) { + constexpr uint32_t write_count = 20; + + size_t buffer_size = pcm_frames_to_bytes(pcm_object, kDefaultConfig.period_size); + auto buffer = std::make_unique(buffer_size); + for (uint32_t i = 0; i < buffer_size; ++i) { + buffer[i] = static_cast(i); + } + + int written_frames = 0; + unsigned int frames = pcm_bytes_to_frames(pcm_object, buffer_size); + pcm_start(pcm_object); + auto start = std::chrono::steady_clock::now(); + for (uint32_t i = 0; i < write_count; ++i) { + written_frames = pcm_mmap_write(pcm_object, buffer.get(), buffer_size); + ASSERT_EQ(written_frames, frames); + } + pcm_stop(pcm_object); + + std::chrono::duration difference = std::chrono::steady_clock::now() - start; + std::chrono::milliseconds expected_elapsed_time_ms(frames * + (write_count - kDefaultConfig.period_count) / (kDefaultConfig.rate / 1000)); + + std::cout << difference.count() << std::endl; + std::cout << expected_elapsed_time_ms.count() << std::endl; + + ASSERT_NEAR(difference.count() * 1000, expected_elapsed_time_ms.count(), 100); +} + +} // namespace testing +} // namespace tinyalse diff --git a/tests/src/pcm_params_test.cc b/tests/src/pcm_params_test.cc new file mode 100644 index 0000000..18c3c2c --- /dev/null +++ b/tests/src/pcm_params_test.cc @@ -0,0 +1,221 @@ +/* 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 +#include +#include + +#include + +#include "tinyalsa/pcm.h" + +namespace tinyalsa { +namespace testing { + +static inline unsigned int OrAllBits(const pcm_mask *mask) { + unsigned int res = 0; + for (uint32_t i = 0; i < 32 / sizeof(unsigned int); ++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(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(100)), 0); + ASSERT_EQ(pcm_params_get_max(params, static_cast(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(count), sizeof(long_string)); + ASSERT_GT(static_cast(count), 0); + + char short_string[1] = { 0 }; + count = pcm_params_to_string(params, short_string, sizeof(short_string)); + ASSERT_GT(static_cast(count), sizeof(short_string)); + + int proper_string_len = count; + int proper_string_size = proper_string_len + 1; + auto proper_string = std::make_unique(proper_string_size); + count = pcm_params_to_string(params, proper_string.get(), proper_string_size); + ASSERT_GT(static_cast(count), 0); + ASSERT_EQ(static_cast(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 tinyalse diff --git a/tests/src/pcm_test.cc b/tests/src/pcm_test.cc new file mode 100644 index 0000000..cf88fb7 --- /dev/null +++ b/tests/src/pcm_test.cc @@ -0,0 +1,103 @@ +/* pcm_out_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 +#include + +#include + +#include "tinyalsa/pcm.h" + +namespace tinyalsa { +namespace testing { + +TEST(PcmTest, FormatToBits) { + // FIXME: Should we return 16 bits for INVALID? + ASSERT_EQ(pcm_format_to_bits(PCM_FORMAT_INVALID), 16); + + ASSERT_EQ(pcm_format_to_bits(PCM_FORMAT_S16_LE), 16); + ASSERT_EQ(pcm_format_to_bits(PCM_FORMAT_S32_LE), 32); + ASSERT_EQ(pcm_format_to_bits(PCM_FORMAT_S8), 8); + ASSERT_EQ(pcm_format_to_bits(PCM_FORMAT_S24_LE), 32); + ASSERT_EQ(pcm_format_to_bits(PCM_FORMAT_S24_3LE), 24); + ASSERT_EQ(pcm_format_to_bits(PCM_FORMAT_S16_BE), 16); + ASSERT_EQ(pcm_format_to_bits(PCM_FORMAT_S24_BE), 32); + ASSERT_EQ(pcm_format_to_bits(PCM_FORMAT_S24_3BE), 24); + ASSERT_EQ(pcm_format_to_bits(PCM_FORMAT_S32_BE), 32); +} + +TEST(PcmTest, OpenAndCloseOutPcm) { + static constexpr unsigned int kDefaultChannels = 2; + static constexpr unsigned int kDefaultSamplingRate = 48000; + static constexpr unsigned int kDefaultPeriodSize = 1024; + static constexpr unsigned int kDefaultPeriodCount = 3; + static constexpr pcm_config kDefaultConfig = { + .channels = kDefaultChannels, + .rate = kDefaultSamplingRate, + .period_size = kDefaultPeriodSize, + .period_count = kDefaultPeriodCount, + .format = PCM_FORMAT_S16_LE, + .start_threshold = kDefaultPeriodSize, + .stop_threshold = kDefaultPeriodSize * kDefaultPeriodCount, + .silence_threshold = 0, + .silence_size = 0, + }; + + pcm *pcm_object = pcm_open(1000, 1000, PCM_OUT, &kDefaultConfig); + ASSERT_FALSE(pcm_is_ready(pcm_object)); + ASSERT_EQ(pcm_close(pcm_object), 0); + + // assume card 0, device 0 is always available + pcm_object = pcm_open(0, 0, PCM_OUT, &kDefaultConfig); + ASSERT_TRUE(pcm_is_ready(pcm_object)); + ASSERT_EQ(pcm_close(pcm_object), 0); + + pcm_object = pcm_open(0, 0, PCM_OUT | PCM_MMAP, &kDefaultConfig); + ASSERT_TRUE(pcm_is_ready(pcm_object)); + ASSERT_EQ(pcm_close(pcm_object), 0); + + pcm_object = pcm_open(0, 0, PCM_OUT | PCM_MMAP | PCM_NOIRQ, &kDefaultConfig); + ASSERT_TRUE(pcm_is_ready(pcm_object)); + ASSERT_EQ(pcm_close(pcm_object), 0); + + pcm_object = pcm_open(0, 0, PCM_OUT | PCM_NONBLOCK, &kDefaultConfig); + ASSERT_TRUE(pcm_is_ready(pcm_object)); + ASSERT_EQ(pcm_close(pcm_object), 0); + + pcm_object = pcm_open(0, 0, PCM_OUT | PCM_MONOTONIC, &kDefaultConfig); + ASSERT_TRUE(pcm_is_ready(pcm_object)); + ASSERT_EQ(pcm_close(pcm_object), 0); + + std::string name = "hw:0,0"; + pcm_object = pcm_open_by_name(name.c_str(), PCM_OUT, &kDefaultConfig); + ASSERT_TRUE(pcm_is_ready(pcm_object)); + ASSERT_EQ(pcm_close(pcm_object), 0); +} + +} // namespace testing +} // namespace tinyalse -- cgit v1.2.3