2 * Copyright © 2016 Mozilla Foundation
4 * This program is made available under an ISC-style license. See the
5 * accompanying file LICENSE for details.
8 /* libcubeb api/function test. Record the mic and check there is sound. */
9 #include "gtest/gtest.h"
10 #if !defined(_XOPEN_SOURCE)
11 #define _XOPEN_SOURCE 600
17 #include "cubeb/cubeb.h"
20 //#define ENABLE_NORMAL_LOG
21 //#define ENABLE_VERBOSE_LOG
24 #define SAMPLE_FREQUENCY 48000
25 #define STREAM_FORMAT CUBEB_SAMPLE_FLOAT32LE
27 struct user_state_record
29 std::atomic
<int> invalid_audio_value
{ 0 };
32 long data_cb_record(cubeb_stream
* stream
, void * user
, const void * inputbuffer
, void * outputbuffer
, long nframes
)
34 user_state_record
* u
= reinterpret_cast<user_state_record
*>(user
);
35 float *b
= (float *)inputbuffer
;
37 if (stream
== NULL
|| inputbuffer
== NULL
|| outputbuffer
!= NULL
) {
41 for (long i
= 0; i
< nframes
; i
++) {
42 if (b
[i
] <= -1.0 || b
[i
] >= 1.0) {
43 u
->invalid_audio_value
= 1;
51 void state_cb_record(cubeb_stream
* stream
, void * /*user*/, cubeb_state state
)
57 case CUBEB_STATE_STARTED
:
58 fprintf(stderr
, "stream started\n"); break;
59 case CUBEB_STATE_STOPPED
:
60 fprintf(stderr
, "stream stopped\n"); break;
61 case CUBEB_STATE_DRAINED
:
62 fprintf(stderr
, "stream drained\n"); break;
64 fprintf(stderr
, "unknown stream state %d\n", state
);
72 if (cubeb_set_log_callback(CUBEB_LOG_DISABLED
, nullptr /*print_log*/) != CUBEB_OK
) {
73 fprintf(stderr
, "Set log callback failed\n");
77 cubeb_stream_params params
;
79 user_state_record stream_state
;
81 r
= common_init(&ctx
, "Cubeb record example");
82 ASSERT_EQ(r
, CUBEB_OK
) << "Error initializing cubeb library";
84 std::unique_ptr
<cubeb
, decltype(&cubeb_destroy
)>
85 cleanup_cubeb_at_exit(ctx
, cubeb_destroy
);
87 /* This test needs an available input device, skip it if this host does not
89 if (!can_run_audio_input_test(ctx
)) {
93 params
.format
= STREAM_FORMAT
;
94 params
.rate
= SAMPLE_FREQUENCY
;
96 params
.layout
= CUBEB_LAYOUT_UNDEFINED
;
97 params
.prefs
= CUBEB_STREAM_PREF_NONE
;
99 r
= cubeb_stream_init(ctx
, &stream
, "Cubeb record (mono)", NULL
, ¶ms
, NULL
, nullptr,
100 4096, data_cb_record
, state_cb_record
, &stream_state
);
101 ASSERT_EQ(r
, CUBEB_OK
) << "Error initializing cubeb stream";
103 std::unique_ptr
<cubeb_stream
, decltype(&cubeb_stream_destroy
)>
104 cleanup_stream_at_exit(stream
, cubeb_stream_destroy
);
106 cubeb_stream_start(stream
);
108 cubeb_stream_stop(stream
);
111 // user callback does not arrive in Linux, silence the error
112 fprintf(stderr
, "Check is disabled in Linux\n");
114 ASSERT_FALSE(stream_state
.invalid_audio_value
.load());
118 #undef SAMPLE_FREQUENCY