1 /*
2 * Copyright © 2017 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. Requests a loopback device and checks that
9 output is being looped back to input. NOTE: Usage of output devices while
10 performing this test will cause flakey results! */
11 #include "gtest/gtest.h"
12 #if !defined(_XOPEN_SOURCE)
13 #define _XOPEN_SOURCE 600
14 #endif
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <math.h>
18 #include <algorithm>
19 #include <memory>
20 #include <mutex>
21 #include <string>
22 #include "cubeb/cubeb.h"
23 //#define ENABLE_NORMAL_LOG
24 //#define ENABLE_VERBOSE_LOG
25 #include "common.h"
26 const uint32_t SAMPLE_FREQUENCY = 48000;
27 const uint32_t TONE_FREQUENCY = 440;
28 const double OUTPUT_AMPLITUDE = 0.25;
29 const int32_t NUM_FRAMES_TO_OUTPUT = SAMPLE_FREQUENCY / 20; /* play ~50ms of samples */
30
31 template<typename T> T ConvertSampleToOutput(double input);
ConvertSampleToOutput(double input)32 template<> float ConvertSampleToOutput(double input) { return float(input); }
ConvertSampleToOutput(double input)33 template<> short ConvertSampleToOutput(double input) { return short(input * 32767.0f); }
34
35 template<typename T> double ConvertSampleFromOutput(T sample);
ConvertSampleFromOutput(float sample)36 template<> double ConvertSampleFromOutput(float sample) { return double(sample); }
ConvertSampleFromOutput(short sample)37 template<> double ConvertSampleFromOutput(short sample) { return double(sample / 32767.0); }
38
39 /* Simple cross correlation to help find phase shift. Not a performant impl */
cross_correlate(std::vector<double> & f,std::vector<double> & g,size_t signal_length)40 std::vector<double> cross_correlate(std::vector<double> & f,
41 std::vector<double> & g,
42 size_t signal_length)
43 {
44 /* the length we sweep our window through to find the cross correlation */
45 size_t sweep_length = f.size() - signal_length + 1;
46 std::vector<double> correlation;
47 correlation.reserve(sweep_length);
48 for (size_t i = 0; i < sweep_length; i++) {
49 double accumulator = 0.0;
50 for (size_t j = 0; j < signal_length; j++) {
51 accumulator += f.at(j) * g.at(i + j);
52 }
53 correlation.push_back(accumulator);
54 }
55 return correlation;
56 }
57
58 /* best effort discovery of phase shift between output and (looped) input*/
find_phase(std::vector<double> & output_frames,std::vector<double> & input_frames,size_t signal_length)59 size_t find_phase(std::vector<double> & output_frames,
60 std::vector<double> & input_frames,
61 size_t signal_length)
62 {
63 std::vector<double> correlation = cross_correlate(output_frames, input_frames, signal_length);
64 size_t phase = 0;
65 double max_correlation = correlation.at(0);
66 for (size_t i = 1; i < correlation.size(); i++) {
67 if (correlation.at(i) > max_correlation) {
68 max_correlation = correlation.at(i);
69 phase = i;
70 }
71 }
72 return phase;
73 }
74
normalize_frames(std::vector<double> & frames)75 std::vector<double> normalize_frames(std::vector<double> & frames) {
76 double max = abs(*std::max_element(frames.begin(), frames.end(),
77 [](double a, double b) { return abs(a) < abs(b); }));
78 std::vector<double> normalized_frames;
79 normalized_frames.reserve(frames.size());
80 for (const double frame : frames) {
81 normalized_frames.push_back(frame / max);
82 }
83 return normalized_frames;
84 }
85
86 /* heuristic comparison of aligned output and input signals, gets flaky if TONE_FREQUENCY is too high */
compare_signals(std::vector<double> & output_frames,std::vector<double> & input_frames)87 void compare_signals(std::vector<double> & output_frames,
88 std::vector<double> & input_frames)
89 {
90 ASSERT_EQ(output_frames.size(), input_frames.size()) << "#Output frames != #input frames";
91 size_t num_frames = output_frames.size();
92 std::vector<double> normalized_output_frames = normalize_frames(output_frames);
93 std::vector<double> normalized_input_frames = normalize_frames(input_frames);
94
95 /* calculate mean absolute errors */
96 /* mean absolute errors between output and input */
97 double io_mas = 0.0;
98 /* mean absolute errors between output and silence */
99 double output_silence_mas = 0.0;
100 /* mean absolute errors between input and silence */
101 double input_silence_mas = 0.0;
102 for (size_t i = 0; i < num_frames; i++) {
103 io_mas += abs(normalized_output_frames.at(i) - normalized_input_frames.at(i));
104 output_silence_mas += abs(normalized_output_frames.at(i));
105 input_silence_mas += abs(normalized_input_frames.at(i));
106 }
107 io_mas /= num_frames;
108 output_silence_mas /= num_frames;
109 input_silence_mas /= num_frames;
110
111 ASSERT_LT(io_mas, output_silence_mas)
112 << "Error between output and input should be less than output and silence!";
113 ASSERT_LT(io_mas, input_silence_mas)
114 << "Error between output and input should be less than output and silence!";
115
116 /* make sure extrema are in (roughly) correct location */
117 /* number of maxima + minama expected in the frames*/
118 const long NUM_EXTREMA = 2 * TONE_FREQUENCY * NUM_FRAMES_TO_OUTPUT / SAMPLE_FREQUENCY;
119 /* expected index of first maxima */
120 const long FIRST_MAXIMUM_INDEX = SAMPLE_FREQUENCY / TONE_FREQUENCY / 4;
121 /* Threshold we expect all maxima and minima to be above or below. Ideally
122 the extrema would be 1 or -1, but particularly at the start of loopback
123 the values seen can be significantly lower. */
124 const double THRESHOLD = 0.5;
125
126 for (size_t i = 0; i < NUM_EXTREMA; i++) {
127 bool is_maximum = i % 2 == 0;
128 /* expected offset to current extreme: i * stide between extrema */
129 size_t offset = i * SAMPLE_FREQUENCY / TONE_FREQUENCY / 2;
130 if (is_maximum) {
131 ASSERT_GT(normalized_output_frames.at(FIRST_MAXIMUM_INDEX + offset), THRESHOLD)
132 << "Output frames have unexpected missing maximum!";
133 ASSERT_GT(normalized_input_frames.at(FIRST_MAXIMUM_INDEX + offset), THRESHOLD)
134 << "Input frames have unexpected missing maximum!";
135 } else {
136 ASSERT_LT(normalized_output_frames.at(FIRST_MAXIMUM_INDEX + offset), -THRESHOLD)
137 << "Output frames have unexpected missing minimum!";
138 ASSERT_LT(normalized_input_frames.at(FIRST_MAXIMUM_INDEX + offset), -THRESHOLD)
139 << "Input frames have unexpected missing minimum!";
140 }
141 }
142 }
143
144 struct user_state_loopback {
145 std::mutex user_state_mutex;
146 long position = 0;
147 /* track output */
148 std::vector<double> output_frames;
149 /* track input */
150 std::vector<double> input_frames;
151 };
152
153 template<typename T>
data_cb_loop_duplex(cubeb_stream * stream,void * user,const void * inputbuffer,void * outputbuffer,long nframes)154 long data_cb_loop_duplex(cubeb_stream * stream, void * user, const void * inputbuffer, void * outputbuffer, long nframes)
155 {
156 struct user_state_loopback * u = (struct user_state_loopback *) user;
157 T * ib = (T *) inputbuffer;
158 T * ob = (T *) outputbuffer;
159
160 if (stream == NULL || inputbuffer == NULL || outputbuffer == NULL) {
161 return CUBEB_ERROR;
162 }
163
164 std::lock_guard<std::mutex> lock(u->user_state_mutex);
165 /* generate our test tone on the fly */
166 for (int i = 0; i < nframes; i++) {
167 double tone = 0.0;
168 if (u->position + i < NUM_FRAMES_TO_OUTPUT) {
169 /* generate sine wave */
170 tone = sin(2 * M_PI*(i + u->position) * TONE_FREQUENCY / SAMPLE_FREQUENCY);
171 tone *= OUTPUT_AMPLITUDE;
172 }
173 ob[i] = ConvertSampleToOutput<T>(tone);
174 u->output_frames.push_back(tone);
175 /* store any looped back output, may be silence */
176 u->input_frames.push_back(ConvertSampleFromOutput(ib[i]));
177 }
178
179 u->position += nframes;
180
181 return nframes;
182 }
183
184 template<typename T>
data_cb_loop_input_only(cubeb_stream * stream,void * user,const void * inputbuffer,void * outputbuffer,long nframes)185 long data_cb_loop_input_only(cubeb_stream * stream, void * user, const void * inputbuffer, void * outputbuffer, long nframes)
186 {
187 struct user_state_loopback * u = (struct user_state_loopback *) user;
188 T * ib = (T *) inputbuffer;
189
190 if (outputbuffer != NULL) {
191 // Can't assert as it needs to return, so expect to fail instead
192 EXPECT_EQ(outputbuffer, (void *) NULL) << "outputbuffer should be null in input only callback";
193 return CUBEB_ERROR;
194 }
195
196 if (stream == NULL || inputbuffer == NULL) {
197 return CUBEB_ERROR;
198 }
199
200 std::lock_guard<std::mutex> lock(u->user_state_mutex);
201 for (int i = 0; i < nframes; i++) {
202 u->input_frames.push_back(ConvertSampleFromOutput(ib[i]));
203 }
204
205 return nframes;
206 }
207
208 template<typename T>
data_cb_playback(cubeb_stream * stream,void * user,const void * inputbuffer,void * outputbuffer,long nframes)209 long data_cb_playback(cubeb_stream * stream, void * user, const void * inputbuffer, void * outputbuffer, long nframes)
210 {
211 struct user_state_loopback * u = (struct user_state_loopback *) user;
212 T * ob = (T *) outputbuffer;
213
214 if (stream == NULL || outputbuffer == NULL) {
215 return CUBEB_ERROR;
216 }
217
218 std::lock_guard<std::mutex> lock(u->user_state_mutex);
219 /* generate our test tone on the fly */
220 for (int i = 0; i < nframes; i++) {
221 double tone = 0.0;
222 if (u->position + i < NUM_FRAMES_TO_OUTPUT) {
223 /* generate sine wave */
224 tone = sin(2 * M_PI*(i + u->position) * TONE_FREQUENCY / SAMPLE_FREQUENCY);
225 tone *= OUTPUT_AMPLITUDE;
226 }
227 ob[i] = ConvertSampleToOutput<T>(tone);
228 u->output_frames.push_back(tone);
229 }
230
231 u->position += nframes;
232
233 return nframes;
234 }
235
state_cb_loop(cubeb_stream * stream,void *,cubeb_state state)236 void state_cb_loop(cubeb_stream * stream, void * /*user*/, cubeb_state state)
237 {
238 if (stream == NULL)
239 return;
240
241 switch (state) {
242 case CUBEB_STATE_STARTED:
243 fprintf(stderr, "stream started\n"); break;
244 case CUBEB_STATE_STOPPED:
245 fprintf(stderr, "stream stopped\n"); break;
246 case CUBEB_STATE_DRAINED:
247 fprintf(stderr, "stream drained\n"); break;
248 default:
249 fprintf(stderr, "unknown stream state %d\n", state);
250 }
251
252 return;
253 }
254
run_loopback_duplex_test(bool is_float)255 void run_loopback_duplex_test(bool is_float)
256 {
257 cubeb * ctx;
258 cubeb_stream * stream;
259 cubeb_stream_params input_params;
260 cubeb_stream_params output_params;
261 int r;
262 uint32_t latency_frames = 0;
263
264 r = common_init(&ctx, "Cubeb loopback example: duplex stream");
265 ASSERT_EQ(r, CUBEB_OK) << "Error initializing cubeb library";
266
267 std::unique_ptr<cubeb, decltype(&cubeb_destroy)>
268 cleanup_cubeb_at_exit(ctx, cubeb_destroy);
269
270 input_params.format = is_float ? CUBEB_SAMPLE_FLOAT32NE : CUBEB_SAMPLE_S16LE;
271 input_params.rate = SAMPLE_FREQUENCY;
272 input_params.channels = 1;
273 input_params.layout = CUBEB_LAYOUT_MONO;
274 input_params.prefs = CUBEB_STREAM_PREF_LOOPBACK;
275 output_params.format = is_float ? CUBEB_SAMPLE_FLOAT32NE : CUBEB_SAMPLE_S16LE;
276 output_params.rate = SAMPLE_FREQUENCY;
277 output_params.channels = 1;
278 output_params.layout = CUBEB_LAYOUT_MONO;
279 output_params.prefs = CUBEB_STREAM_PREF_NONE;
280
281 std::unique_ptr<user_state_loopback> user_data(new user_state_loopback());
282 ASSERT_TRUE(!!user_data) << "Error allocating user data";
283
284 r = cubeb_get_min_latency(ctx, &output_params, &latency_frames);
285 ASSERT_EQ(r, CUBEB_OK) << "Could not get minimal latency";
286
287 /* setup a duplex stream with loopback */
288 r = cubeb_stream_init(ctx, &stream, "Cubeb loopback",
289 NULL, &input_params, NULL, &output_params, latency_frames,
290 is_float ? data_cb_loop_duplex<float> : data_cb_loop_duplex<short>,
291 state_cb_loop, user_data.get());
292 ASSERT_EQ(r, CUBEB_OK) << "Error initializing cubeb stream";
293
294 std::unique_ptr<cubeb_stream, decltype(&cubeb_stream_destroy)>
295 cleanup_stream_at_exit(stream, cubeb_stream_destroy);
296
297 cubeb_stream_start(stream);
298 delay(300);
299 cubeb_stream_stop(stream);
300
301 /* access after stop should not happen, but lock just in case and to appease sanitization tools */
302 std::lock_guard<std::mutex> lock(user_data->user_state_mutex);
303 std::vector<double> & output_frames = user_data->output_frames;
304 std::vector<double> & input_frames = user_data->input_frames;
305 ASSERT_EQ(output_frames.size(), input_frames.size())
306 << "#Output frames != #input frames";
307
308 size_t phase = find_phase(user_data->output_frames, user_data->input_frames, NUM_FRAMES_TO_OUTPUT);
309
310 /* extract vectors of just the relevant signal from output and input */
311 auto output_frames_signal_start = output_frames.begin();
312 auto output_frames_signal_end = output_frames.begin() + NUM_FRAMES_TO_OUTPUT;
313 std::vector<double> trimmed_output_frames(output_frames_signal_start, output_frames_signal_end);
314 auto input_frames_signal_start = input_frames.begin() + phase;
315 auto input_frames_signal_end = input_frames.begin() + phase + NUM_FRAMES_TO_OUTPUT;
316 std::vector<double> trimmed_input_frames(input_frames_signal_start, input_frames_signal_end);
317
318 compare_signals(trimmed_output_frames, trimmed_input_frames);
319 }
320
TEST(cubeb,loopback_duplex)321 TEST(cubeb, loopback_duplex)
322 {
323 run_loopback_duplex_test(true);
324 run_loopback_duplex_test(false);
325 }
326
run_loopback_separate_streams_test(bool is_float)327 void run_loopback_separate_streams_test(bool is_float)
328 {
329 cubeb * ctx;
330 cubeb_stream * input_stream;
331 cubeb_stream * output_stream;
332 cubeb_stream_params input_params;
333 cubeb_stream_params output_params;
334 int r;
335 uint32_t latency_frames = 0;
336
337 r = common_init(&ctx, "Cubeb loopback example: separate streams");
338 ASSERT_EQ(r, CUBEB_OK) << "Error initializing cubeb library";
339
340 std::unique_ptr<cubeb, decltype(&cubeb_destroy)>
341 cleanup_cubeb_at_exit(ctx, cubeb_destroy);
342
343 input_params.format = is_float ? CUBEB_SAMPLE_FLOAT32NE : CUBEB_SAMPLE_S16LE;
344 input_params.rate = SAMPLE_FREQUENCY;
345 input_params.channels = 1;
346 input_params.layout = CUBEB_LAYOUT_MONO;
347 input_params.prefs = CUBEB_STREAM_PREF_LOOPBACK;
348 output_params.format = is_float ? CUBEB_SAMPLE_FLOAT32NE : CUBEB_SAMPLE_S16LE;
349 output_params.rate = SAMPLE_FREQUENCY;
350 output_params.channels = 1;
351 output_params.layout = CUBEB_LAYOUT_MONO;
352 output_params.prefs = CUBEB_STREAM_PREF_NONE;
353
354 std::unique_ptr<user_state_loopback> user_data(new user_state_loopback());
355 ASSERT_TRUE(!!user_data) << "Error allocating user data";
356
357 r = cubeb_get_min_latency(ctx, &output_params, &latency_frames);
358 ASSERT_EQ(r, CUBEB_OK) << "Could not get minimal latency";
359
360 /* setup an input stream with loopback */
361 r = cubeb_stream_init(ctx, &input_stream, "Cubeb loopback input only",
362 NULL, &input_params, NULL, NULL, latency_frames,
363 is_float ? data_cb_loop_input_only<float> : data_cb_loop_input_only<short>,
364 state_cb_loop, user_data.get());
365 ASSERT_EQ(r, CUBEB_OK) << "Error initializing cubeb stream";
366
367 std::unique_ptr<cubeb_stream, decltype(&cubeb_stream_destroy)>
368 cleanup_input_stream_at_exit(input_stream, cubeb_stream_destroy);
369
370 /* setup an output stream */
371 r = cubeb_stream_init(ctx, &output_stream, "Cubeb loopback output only",
372 NULL, NULL, NULL, &output_params, latency_frames,
373 is_float ? data_cb_playback<float> : data_cb_playback<short>,
374 state_cb_loop, user_data.get());
375 ASSERT_EQ(r, CUBEB_OK) << "Error initializing cubeb stream";
376
377 std::unique_ptr<cubeb_stream, decltype(&cubeb_stream_destroy)>
378 cleanup_output_stream_at_exit(output_stream, cubeb_stream_destroy);
379
380 cubeb_stream_start(input_stream);
381 cubeb_stream_start(output_stream);
382 delay(300);
383 cubeb_stream_stop(output_stream);
384 cubeb_stream_stop(input_stream);
385
386 /* access after stop should not happen, but lock just in case and to appease sanitization tools */
387 std::lock_guard<std::mutex> lock(user_data->user_state_mutex);
388 std::vector<double> & output_frames = user_data->output_frames;
389 std::vector<double> & input_frames = user_data->input_frames;
390 ASSERT_LE(output_frames.size(), input_frames.size())
391 << "#Output frames should be less or equal to #input frames";
392
393 size_t phase = find_phase(user_data->output_frames, user_data->input_frames, NUM_FRAMES_TO_OUTPUT);
394
395 /* extract vectors of just the relevant signal from output and input */
396 auto output_frames_signal_start = output_frames.begin();
397 auto output_frames_signal_end = output_frames.begin() + NUM_FRAMES_TO_OUTPUT;
398 std::vector<double> trimmed_output_frames(output_frames_signal_start, output_frames_signal_end);
399 auto input_frames_signal_start = input_frames.begin() + phase;
400 auto input_frames_signal_end = input_frames.begin() + phase + NUM_FRAMES_TO_OUTPUT;
401 std::vector<double> trimmed_input_frames(input_frames_signal_start, input_frames_signal_end);
402
403 compare_signals(trimmed_output_frames, trimmed_input_frames);
404 }
405
TEST(cubeb,loopback_separate_streams)406 TEST(cubeb, loopback_separate_streams)
407 {
408 run_loopback_separate_streams_test(true);
409 run_loopback_separate_streams_test(false);
410 }
411
run_loopback_silence_test(bool is_float)412 void run_loopback_silence_test(bool is_float)
413 {
414 cubeb * ctx;
415 cubeb_stream * input_stream;
416 cubeb_stream_params input_params;
417 int r;
418 uint32_t latency_frames = 0;
419
420 r = common_init(&ctx, "Cubeb loopback example: record silence");
421 ASSERT_EQ(r, CUBEB_OK) << "Error initializing cubeb library";
422
423 std::unique_ptr<cubeb, decltype(&cubeb_destroy)>
424 cleanup_cubeb_at_exit(ctx, cubeb_destroy);
425
426 input_params.format = is_float ? CUBEB_SAMPLE_FLOAT32NE : CUBEB_SAMPLE_S16LE;
427 input_params.rate = SAMPLE_FREQUENCY;
428 input_params.channels = 1;
429 input_params.layout = CUBEB_LAYOUT_MONO;
430 input_params.prefs = CUBEB_STREAM_PREF_LOOPBACK;
431
432 std::unique_ptr<user_state_loopback> user_data(new user_state_loopback());
433 ASSERT_TRUE(!!user_data) << "Error allocating user data";
434
435 r = cubeb_get_min_latency(ctx, &input_params, &latency_frames);
436 ASSERT_EQ(r, CUBEB_OK) << "Could not get minimal latency";
437
438 /* setup an input stream with loopback */
439 r = cubeb_stream_init(ctx, &input_stream, "Cubeb loopback input only",
440 NULL, &input_params, NULL, NULL, latency_frames,
441 is_float ? data_cb_loop_input_only<float> : data_cb_loop_input_only<short>,
442 state_cb_loop, user_data.get());
443 ASSERT_EQ(r, CUBEB_OK) << "Error initializing cubeb stream";
444
445 std::unique_ptr<cubeb_stream, decltype(&cubeb_stream_destroy)>
446 cleanup_input_stream_at_exit(input_stream, cubeb_stream_destroy);
447
448 cubeb_stream_start(input_stream);
449 delay(300);
450 cubeb_stream_stop(input_stream);
451
452 /* access after stop should not happen, but lock just in case and to appease sanitization tools */
453 std::lock_guard<std::mutex> lock(user_data->user_state_mutex);
454 std::vector<double> & input_frames = user_data->input_frames;
455
456 /* expect to have at least ~50ms of frames */
457 ASSERT_GE(input_frames.size(), SAMPLE_FREQUENCY / 20);
458 double EPISILON = 0.0001;
459 /* frames should be 0.0, but use epsilon to avoid possible issues with impls
460 that may use ~0.0 silence values. */
461 for (double frame : input_frames) {
462 ASSERT_LT(abs(frame), EPISILON);
463 }
464 }
465
TEST(cubeb,loopback_silence)466 TEST(cubeb, loopback_silence)
467 {
468 run_loopback_silence_test(true);
469 run_loopback_silence_test(false);
470 }
471
run_loopback_device_selection_test(bool is_float)472 void run_loopback_device_selection_test(bool is_float)
473 {
474 cubeb * ctx;
475 cubeb_device_collection collection;
476 cubeb_stream * input_stream;
477 cubeb_stream * output_stream;
478 cubeb_stream_params input_params;
479 cubeb_stream_params output_params;
480 int r;
481 uint32_t latency_frames = 0;
482
483 r = common_init(&ctx, "Cubeb loopback example: device selection, separate streams");
484 ASSERT_EQ(r, CUBEB_OK) << "Error initializing cubeb library";
485
486 std::unique_ptr<cubeb, decltype(&cubeb_destroy)>
487 cleanup_cubeb_at_exit(ctx, cubeb_destroy);
488
489 r = cubeb_enumerate_devices(ctx, CUBEB_DEVICE_TYPE_OUTPUT, &collection);
490 if (r == CUBEB_ERROR_NOT_SUPPORTED) {
491 fprintf(stderr, "Device enumeration not supported"
492 " for this backend, skipping this test.\n");
493 return;
494 }
495
496 ASSERT_EQ(r, CUBEB_OK) << "Error enumerating devices " << r;
497 /* get first preferred output device id */
498 std::string device_id;
499 for (size_t i = 0; i < collection.count; i++) {
500 if (collection.device[i].preferred) {
501 device_id = collection.device[i].device_id;
502 break;
503 }
504 }
505 cubeb_device_collection_destroy(ctx, &collection);
506 if (device_id.empty()) {
507 fprintf(stderr, "Could not find preferred device, aborting test.\n");
508 return;
509 }
510
511 input_params.format = is_float ? CUBEB_SAMPLE_FLOAT32NE : CUBEB_SAMPLE_S16LE;
512 input_params.rate = SAMPLE_FREQUENCY;
513 input_params.channels = 1;
514 input_params.layout = CUBEB_LAYOUT_MONO;
515 input_params.prefs = CUBEB_STREAM_PREF_LOOPBACK;
516 output_params.format = is_float ? CUBEB_SAMPLE_FLOAT32NE : CUBEB_SAMPLE_S16LE;
517 output_params.rate = SAMPLE_FREQUENCY;
518 output_params.channels = 1;
519 output_params.layout = CUBEB_LAYOUT_MONO;
520 output_params.prefs = CUBEB_STREAM_PREF_NONE;
521
522 std::unique_ptr<user_state_loopback> user_data(new user_state_loopback());
523 ASSERT_TRUE(!!user_data) << "Error allocating user data";
524
525 r = cubeb_get_min_latency(ctx, &output_params, &latency_frames);
526 ASSERT_EQ(r, CUBEB_OK) << "Could not get minimal latency";
527
528 /* setup an input stream with loopback */
529 r = cubeb_stream_init(ctx, &input_stream, "Cubeb loopback input only",
530 device_id.c_str(), &input_params, NULL, NULL, latency_frames,
531 is_float ? data_cb_loop_input_only<float> : data_cb_loop_input_only<short>,
532 state_cb_loop, user_data.get());
533 ASSERT_EQ(r, CUBEB_OK) << "Error initializing cubeb stream";
534
535 std::unique_ptr<cubeb_stream, decltype(&cubeb_stream_destroy)>
536 cleanup_input_stream_at_exit(input_stream, cubeb_stream_destroy);
537
538 /* setup an output stream */
539 r = cubeb_stream_init(ctx, &output_stream, "Cubeb loopback output only",
540 NULL, NULL, device_id.c_str(), &output_params, latency_frames,
541 is_float ? data_cb_playback<float> : data_cb_playback<short>,
542 state_cb_loop, user_data.get());
543 ASSERT_EQ(r, CUBEB_OK) << "Error initializing cubeb stream";
544
545 std::unique_ptr<cubeb_stream, decltype(&cubeb_stream_destroy)>
546 cleanup_output_stream_at_exit(output_stream, cubeb_stream_destroy);
547
548 cubeb_stream_start(input_stream);
549 cubeb_stream_start(output_stream);
550 delay(300);
551 cubeb_stream_stop(output_stream);
552 cubeb_stream_stop(input_stream);
553
554 /* access after stop should not happen, but lock just in case and to appease sanitization tools */
555 std::lock_guard<std::mutex> lock(user_data->user_state_mutex);
556 std::vector<double> & output_frames = user_data->output_frames;
557 std::vector<double> & input_frames = user_data->input_frames;
558 ASSERT_LE(output_frames.size(), input_frames.size())
559 << "#Output frames should be less or equal to #input frames";
560
561 size_t phase = find_phase(user_data->output_frames, user_data->input_frames, NUM_FRAMES_TO_OUTPUT);
562
563 /* extract vectors of just the relevant signal from output and input */
564 auto output_frames_signal_start = output_frames.begin();
565 auto output_frames_signal_end = output_frames.begin() + NUM_FRAMES_TO_OUTPUT;
566 std::vector<double> trimmed_output_frames(output_frames_signal_start, output_frames_signal_end);
567 auto input_frames_signal_start = input_frames.begin() + phase;
568 auto input_frames_signal_end = input_frames.begin() + phase + NUM_FRAMES_TO_OUTPUT;
569 std::vector<double> trimmed_input_frames(input_frames_signal_start, input_frames_signal_end);
570
571 compare_signals(trimmed_output_frames, trimmed_input_frames);
572 }
573
TEST(cubeb,loopback_device_selection)574 TEST(cubeb, loopback_device_selection)
575 {
576 run_loopback_device_selection_test(true);
577 run_loopback_device_selection_test(false);
578 }
579