Debian Codegraph @

third_party/rust/cubeb-sys/libcubeb/test/test_record.cpp

raw
1 /*
2 * Copyright © 2016 Mozilla Foundation
3 *
4 * This program is made available under an ISC-style license. See the
5 * accompanying file LICENSE for details.
6 */
7
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
12 #endif
13 #include "cubeb/cubeb.h"
14 #include <atomic>
15 #include <math.h>
16 #include <memory>
17 #include <stdio.h>
18 #include <stdlib.h>
19
20 // #define ENABLE_NORMAL_LOG
21 // #define ENABLE_VERBOSE_LOG
22 #include "common.h"
23
24 #define SAMPLE_FREQUENCY 48000
25 #define STREAM_FORMAT CUBEB_SAMPLE_FLOAT32LE
26
27 struct user_state_record {
28 std::atomic<int> invalid_audio_value{0};
29 };
30
31 long
32 data_cb_record(cubeb_stream * stream, void * user, const void * inputbuffer,
33 void * outputbuffer, long nframes)
34 {
35 user_state_record * u = reinterpret_cast<user_state_record *>(user);
36 float * b = (float *)inputbuffer;
37
38 if (stream == NULL || inputbuffer == NULL || outputbuffer != NULL) {
39 return CUBEB_ERROR;
40 }
41
42 for (long i = 0; i < nframes; i++) {
43 if (b[i] <= -1.0 || b[i] >= 1.0) {
44 u->invalid_audio_value = 1;
45 break;
46 }
47 }
48
49 return nframes;
50 }
51
52 void
53 state_cb_record(cubeb_stream * stream, void * /*user*/, cubeb_state state)
54 {
55 if (stream == NULL)
56 return;
57
58 switch (state) {
59 case CUBEB_STATE_STARTED:
60 fprintf(stderr, "stream started\n");
61 break;
62 case CUBEB_STATE_STOPPED:
63 fprintf(stderr, "stream stopped\n");
64 break;
65 case CUBEB_STATE_DRAINED:
66 fprintf(stderr, "stream drained\n");
67 break;
68 default:
69 fprintf(stderr, "unknown stream state %d\n", state);
70 }
71
72 return;
73 }
74
75 TEST(cubeb, record)
76 {
77 if (cubeb_set_log_callback(CUBEB_LOG_DISABLED, nullptr /*print_log*/) !=
78 CUBEB_OK) {
79 fprintf(stderr, "Set log callback failed\n");
80 }
81 cubeb * ctx;
82 cubeb_stream * stream;
83 cubeb_stream_params params;
84 int r;
85 user_state_record stream_state;
86
87 r = common_init(&ctx, "Cubeb record example");
88 ASSERT_EQ(r, CUBEB_OK) << "Error initializing cubeb library";
89
90 std::unique_ptr<cubeb, decltype(&cubeb_destroy)> cleanup_cubeb_at_exit(
91 ctx, cubeb_destroy);
92
93 /* This test needs an available input device, skip it if this host does not
94 * have one. */
95 if (!can_run_audio_input_test(ctx)) {
96 return;
97 }
98
99 params.format = STREAM_FORMAT;
100 params.rate = SAMPLE_FREQUENCY;
101 params.channels = 1;
102 params.layout = CUBEB_LAYOUT_UNDEFINED;
103 params.prefs = CUBEB_STREAM_PREF_NONE;
104
105 r = cubeb_stream_init(ctx, &stream, "Cubeb record (mono)", NULL, &params,
106 NULL, nullptr, 4096, data_cb_record, state_cb_record,
107 &stream_state);
108 ASSERT_EQ(r, CUBEB_OK) << "Error initializing cubeb stream";
109
110 std::unique_ptr<cubeb_stream, decltype(&cubeb_stream_destroy)>
111 cleanup_stream_at_exit(stream, cubeb_stream_destroy);
112
113 cubeb_stream_start(stream);
114 delay(500);
115 cubeb_stream_stop(stream);
116
117 #ifdef __linux__
118 // user callback does not arrive in Linux, silence the error
119 fprintf(stderr, "Check is disabled in Linux\n");
120 #else
121 ASSERT_FALSE(stream_state.invalid_audio_value.load());
122 #endif
123 }
124
125 #undef SAMPLE_FREQUENCY
126 #undef STREAM_FORMAT