Skip to content

Commit 7a5c2ba

Browse files
committed
Simple test cases for supported recorders
1 parent 32769a3 commit 7a5c2ba

7 files changed

Lines changed: 142 additions & 16 deletions

File tree

CMakeLists.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,20 +50,20 @@ target_link_libraries(svar Threads::Threads)
5050

5151
if(ENABLE_ALSA)
5252
pkg_check_modules(ALSA REQUIRED IMPORTED_TARGET alsa)
53-
target_sources(svar PRIVATE src/recorder-alsa.c)
54-
target_link_libraries(svar PkgConfig::ALSA)
53+
target_sources(svarcore PRIVATE src/recorder-alsa.c)
54+
target_link_libraries(svarcore PkgConfig::ALSA)
5555
endif()
5656

5757
if(ENABLE_PIPEWIRE)
5858
pkg_check_modules(PipeWire REQUIRED IMPORTED_TARGET libpipewire-0.3)
59-
target_sources(svar PRIVATE src/recorder-pipewire.c)
60-
target_link_libraries(svar PkgConfig::PipeWire)
59+
target_sources(svarcore PRIVATE src/recorder-pipewire.c)
60+
target_link_libraries(svarcore PkgConfig::PipeWire)
6161
endif()
6262

6363
if(ENABLE_PORTAUDIO)
6464
pkg_check_modules(PortAudio REQUIRED IMPORTED_TARGET portaudio-2.0)
65-
target_sources(svar PRIVATE src/recorder-portaudio.c)
66-
target_link_libraries(svar PkgConfig::PortAudio)
65+
target_sources(svarcore PRIVATE src/recorder-portaudio.c)
66+
target_link_libraries(svarcore PkgConfig::PortAudio)
6767
endif()
6868

6969
if(ENABLE_SNDFILE)

src/recorder.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ struct recorder * recorder_new(
7373

7474
void recorder_free(
7575
struct recorder * r) {
76-
r->free(r);
76+
if (r->free != NULL)
77+
r->free(r);
7778
pthread_mutex_destroy(&r->mutex);
7879
pthread_cond_destroy(&r->cond);
7980
free(r->output_file_template);
@@ -228,7 +229,7 @@ int recorder_monitor(
228229

229230
struct timespec now;
230231
clock_gettime(CLOCK_MONOTONIC_RAW, &now);
231-
if (ts_diff_ms(&now, &activation_time) < r->activation_fadeout_time_ms)
232+
if (ts_diff_ms(&now, &activation_time) <= r->activation_fadeout_time_ms)
232233
return 0;
233234

234235
return -1;

test/tc-pcm.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ START_TEST(test_pcm_rms_db) {
4040

4141
} END_TEST
4242

43-
void tcase_init(Suite * s) {
43+
int tcase_init(Suite * s) {
4444

4545
TCase * tc = tcase_create(__FILE__);
4646
suite_add_tcase(s, tc);
@@ -49,4 +49,5 @@ void tcase_init(Suite * s) {
4949
tcase_add_test(tc, test_pcm_format_size);
5050
tcase_add_test(tc, test_pcm_rms_db);
5151

52+
return 0;
5253
}

test/tc-rbuf.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,13 @@ START_TEST(test_rbuf_read_write) {
7575

7676
} END_TEST
7777

78-
void tcase_init(Suite * s) {
78+
int tcase_init(Suite * s) {
7979

8080
TCase * tc = tcase_create(__FILE__);
8181
suite_add_tcase(s, tc);
8282

8383
tcase_add_test(tc, test_rbuf_init);
8484
tcase_add_test(tc, test_rbuf_read_write);
8585

86+
return 0;
8687
}

test/tc-recorder.c

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/*
2+
* SVAR - tc-recorder.c
3+
* SPDX-FileCopyrightText: 2025 Arkadiusz Bokowy and contributors
4+
* SPDX-License-Identifier: MIT
5+
*/
6+
7+
#if HAVE_CONFIG_H
8+
# include "config.h"
9+
#endif
10+
11+
#include <check.h>
12+
#include <stdint.h>
13+
14+
#include "recorder.h"
15+
#include "recorder-alsa.h"
16+
#include "recorder-pipewire.h"
17+
#include "recorder-portaudio.h"
18+
19+
#if ENABLE_ALSA
20+
START_TEST(test_recorder_alsa) {
21+
22+
struct recorder * rec;
23+
ck_assert_ptr_nonnull(rec = recorder_alsa_new(PCM_FORMAT_S16LE, 2, 44100));
24+
ck_assert_uint_eq(rec->type, RECORDER_TYPE_ALSA);
25+
26+
recorder_list_devices(rec);
27+
recorder_free(rec);
28+
29+
} END_TEST
30+
#endif
31+
32+
#if ENABLE_PIPEWIRE
33+
START_TEST(test_recorder_pipewire) {
34+
35+
struct recorder * rec;
36+
ck_assert_ptr_nonnull(rec = recorder_pipewire_new(PCM_FORMAT_S16LE, 2, 44100));
37+
ck_assert_uint_eq(rec->type, RECORDER_TYPE_PIPEWIRE);
38+
39+
recorder_list_devices(rec);
40+
recorder_free(rec);
41+
42+
} END_TEST
43+
#endif
44+
45+
#if ENABLE_PORTAUDIO
46+
START_TEST(test_recorder_portaudio) {
47+
48+
struct recorder * rec;
49+
ck_assert_ptr_nonnull(rec = recorder_pa_new(PCM_FORMAT_S16LE, 2, 44100));
50+
ck_assert_uint_eq(rec->type, RECORDER_TYPE_PORTAUDIO);
51+
52+
recorder_list_devices(rec);
53+
recorder_free(rec);
54+
55+
} END_TEST
56+
#endif
57+
58+
START_TEST(test_recorder_monitor) {
59+
60+
struct recorder * rec;
61+
ck_assert_ptr_nonnull(rec = recorder_new(PCM_FORMAT_U8, 1, 44100));
62+
rec->monitor = true;
63+
64+
uint8_t pcm[] = { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120 };
65+
ck_assert_int_eq(recorder_monitor(rec, pcm, sizeof(pcm)), -2);
66+
67+
recorder_free(rec);
68+
69+
} END_TEST
70+
71+
START_TEST(test_recorder_process) {
72+
73+
struct recorder * rec;
74+
ck_assert_ptr_nonnull(rec = recorder_new(PCM_FORMAT_U8, 1, 44100));
75+
rec->activation_threshold_level_db = -42.0;
76+
77+
uint8_t pcm[] = { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120 };
78+
ck_assert_int_eq(recorder_process(rec, pcm, sizeof(pcm)), 0);
79+
ck_assert_uint_eq(rbuf_read_linear_capacity(&rec->rb), sizeof(pcm));
80+
81+
recorder_free(rec);
82+
83+
} END_TEST
84+
85+
START_TEST(test_recorder_type_to_string) {
86+
#if ENABLE_ALSA
87+
ck_assert_str_eq(recorder_type_to_string(RECORDER_TYPE_ALSA), "ALSA");
88+
#endif
89+
#if ENABLE_PIPEWIRE
90+
ck_assert_str_eq(recorder_type_to_string(RECORDER_TYPE_PIPEWIRE), "PipeWire");
91+
#endif
92+
#if ENABLE_PORTAUDIO
93+
ck_assert_str_eq(recorder_type_to_string(RECORDER_TYPE_PORTAUDIO), "PortAudio");
94+
#endif
95+
} END_TEST
96+
97+
int tcase_init(Suite * s) {
98+
99+
TCase * tc = tcase_create(__FILE__);
100+
suite_add_tcase(s, tc);
101+
102+
#if ENABLE_ALSA
103+
tcase_add_test(tc, test_recorder_alsa);
104+
#endif
105+
106+
#if ENABLE_PIPEWIRE
107+
tcase_add_test(tc, test_recorder_pipewire);
108+
#endif
109+
110+
#if ENABLE_PORTAUDIO
111+
tcase_add_test(tc, test_recorder_portaudio);
112+
#endif
113+
114+
tcase_add_test(tc, test_recorder_monitor);
115+
tcase_add_test(tc, test_recorder_process);
116+
tcase_add_test(tc, test_recorder_type_to_string);
117+
118+
return 0;
119+
}

test/tc-writer.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ START_TEST(test_writer_type_to_string) {
231231
#endif
232232
} END_TEST
233233

234-
void tcase_init(Suite * s) {
234+
int tcase_init(Suite * s) {
235235

236236
TCase * tc = tcase_create(__FILE__);
237237
suite_add_tcase(s, tc);
@@ -262,4 +262,5 @@ void tcase_init(Suite * s) {
262262
tcase_add_test(tc, test_writer_type_to_extension);
263263
tcase_add_test(tc, test_writer_type_to_string);
264264

265+
return 0;
265266
}

test/test-runner.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,21 @@
66

77
#include <check.h>
88

9-
extern void tcase_init(Suite * s);
9+
extern int tcase_init(Suite * s);
1010

1111
int main(void) {
1212

1313
Suite * s = suite_create(__FILE__);
1414
SRunner * sr = srunner_create(s);
1515

16-
tcase_init(s);
16+
int rv;
17+
if ((rv = tcase_init(s)) != 0)
18+
goto fail;
1719

1820
srunner_run_all(sr, CK_ENV);
19-
int nf = srunner_ntests_failed(sr);
20-
srunner_free(sr);
21+
rv = srunner_ntests_failed(sr) == 0 ? 0 : 1;
2122

22-
return nf == 0 ? 0 : 1;
23+
fail:
24+
srunner_free(sr);
25+
return rv;
2326
}

0 commit comments

Comments
 (0)