| 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. Loops input back to output and check audio |
| 9 |
* is flowing. */ |
| 10 |
#include "gtest/gtest.h" |
| 11 |
#if !defined(_XOPEN_SOURCE) |
| 12 |
#define _XOPEN_SOURCE 600 |
| 13 |
#endif |
| 14 |
#include "cubeb/cubeb.h" |
| 15 |
#include <atomic> |
| 16 |
#include <math.h> |
| 17 |
#include <memory> |
| 18 |
#include <stdio.h> |
| 19 |
#include <stdlib.h> |
| 20 |
|
| 21 |
// #define ENABLE_NORMAL_LOG |
| 22 |
// #define ENABLE_VERBOSE_LOG |
| 23 |
#include "common.h" |
| 24 |
|
| 25 |
#define SAMPLE_FREQUENCY 48000 |
| 26 |
#define STREAM_FORMAT CUBEB_SAMPLE_FLOAT32LE |
| 27 |
#define INPUT_CHANNELS 1 |
| 28 |
#define INPUT_LAYOUT CUBEB_LAYOUT_MONO |
| 29 |
#define OUTPUT_CHANNELS 2 |
| 30 |
#define OUTPUT_LAYOUT CUBEB_LAYOUT_STEREO |
| 31 |
|
| 32 |
struct user_state_duplex { |
| 33 |
std::atomic<int> invalid_audio_value{0}; |
| 34 |
}; |
| 35 |
|
| 36 |
long |
| 37 |
data_cb_duplex(cubeb_stream * stream, void * user, const void * inputbuffer, |
| 38 |
void * outputbuffer, long nframes) |
| 39 |
{ |
| 40 |
user_state_duplex * u = reinterpret_cast<user_state_duplex *>(user); |
| 41 |
float * ib = (float *)inputbuffer; |
| 42 |
float * ob = (float *)outputbuffer; |
| 43 |
|
| 44 |
if (stream == NULL || inputbuffer == NULL || outputbuffer == NULL) { |
| 45 |
return CUBEB_ERROR; |
| 46 |
} |
| 47 |
|
| 48 |
// Loop back: upmix the single input channel to the two output channels, |
| 49 |
// checking if there is noise in the process. |
| 50 |
long output_index = 0; |
| 51 |
for (long i = 0; i < nframes; i++) { |
| 52 |
if (ib[i] <= -1.0 || ib[i] >= 1.0) { |
| 53 |
u->invalid_audio_value = 1; |
| 54 |
} |
| 55 |
ob[output_index] = ob[output_index + 1] = ib[i]; |
| 56 |
output_index += 2; |
| 57 |
} |
| 58 |
|
| 59 |
return nframes; |
| 60 |
} |
| 61 |
|
| 62 |
void |
| 63 |
state_cb_duplex(cubeb_stream * stream, void * /*user*/, cubeb_state state) |
| 64 |
{ |
| 65 |
if (stream == NULL) |
| 66 |
return; |
| 67 |
|
| 68 |
switch (state) { |
| 69 |
case CUBEB_STATE_STARTED: |
| 70 |
fprintf(stderr, "stream started\n"); |
| 71 |
break; |
| 72 |
case CUBEB_STATE_STOPPED: |
| 73 |
fprintf(stderr, "stream stopped\n"); |
| 74 |
break; |
| 75 |
case CUBEB_STATE_DRAINED: |
| 76 |
fprintf(stderr, "stream drained\n"); |
| 77 |
break; |
| 78 |
default: |
| 79 |
fprintf(stderr, "unknown stream state %d\n", state); |
| 80 |
} |
| 81 |
|
| 82 |
return; |
| 83 |
} |
| 84 |
|
| 85 |
TEST(cubeb, duplex) |
| 86 |
{ |
| 87 |
cubeb * ctx; |
| 88 |
cubeb_stream * stream; |
| 89 |
cubeb_stream_params input_params; |
| 90 |
cubeb_stream_params output_params; |
| 91 |
int r; |
| 92 |
user_state_duplex stream_state; |
| 93 |
uint32_t latency_frames = 0; |
| 94 |
|
| 95 |
r = common_init(&ctx, "Cubeb duplex example"); |
| 96 |
ASSERT_EQ(r, CUBEB_OK) << "Error initializing cubeb library"; |
| 97 |
|
| 98 |
std::unique_ptr<cubeb, decltype(&cubeb_destroy)> cleanup_cubeb_at_exit( |
| 99 |
ctx, cubeb_destroy); |
| 100 |
|
| 101 |
/* This test needs an available input device, skip it if this host does not |
| 102 |
* have one. */ |
| 103 |
if (!can_run_audio_input_test(ctx)) { |
| 104 |
return; |
| 105 |
} |
| 106 |
|
| 107 |
/* typical user-case: mono input, stereo output, low latency. */ |
| 108 |
input_params.format = STREAM_FORMAT; |
| 109 |
input_params.rate = SAMPLE_FREQUENCY; |
| 110 |
input_params.channels = INPUT_CHANNELS; |
| 111 |
input_params.layout = INPUT_LAYOUT; |
| 112 |
input_params.prefs = CUBEB_STREAM_PREF_NONE; |
| 113 |
output_params.format = STREAM_FORMAT; |
| 114 |
output_params.rate = SAMPLE_FREQUENCY; |
| 115 |
output_params.channels = OUTPUT_CHANNELS; |
| 116 |
output_params.layout = OUTPUT_LAYOUT; |
| 117 |
output_params.prefs = CUBEB_STREAM_PREF_NONE; |
| 118 |
|
| 119 |
r = cubeb_get_min_latency(ctx, &output_params, &latency_frames); |
| 120 |
ASSERT_EQ(r, CUBEB_OK) << "Could not get minimal latency"; |
| 121 |
|
| 122 |
r = cubeb_stream_init(ctx, &stream, "Cubeb duplex", NULL, &input_params, NULL, |
| 123 |
&output_params, latency_frames, data_cb_duplex, |
| 124 |
state_cb_duplex, &stream_state); |
| 125 |
ASSERT_EQ(r, CUBEB_OK) << "Error initializing cubeb stream"; |
| 126 |
|
| 127 |
std::unique_ptr<cubeb_stream, decltype(&cubeb_stream_destroy)> |
| 128 |
cleanup_stream_at_exit(stream, cubeb_stream_destroy); |
| 129 |
|
| 130 |
cubeb_stream_start(stream); |
| 131 |
delay(500); |
| 132 |
cubeb_stream_stop(stream); |
| 133 |
|
| 134 |
ASSERT_FALSE(stream_state.invalid_audio_value.load()); |
| 135 |
} |
| 136 |
|
| 137 |
void |
| 138 |
device_collection_changed_callback(cubeb * context, void * user) |
| 139 |
{ |
| 140 |
fprintf(stderr, "collection changed callback\n"); |
| 141 |
ASSERT_TRUE(false) << "Error: device collection changed callback" |
| 142 |
" called when opening a stream"; |
| 143 |
} |
| 144 |
|
| 145 |
void |
| 146 |
duplex_collection_change_impl(cubeb * ctx) |
| 147 |
{ |
| 148 |
cubeb_stream * stream; |
| 149 |
cubeb_stream_params input_params; |
| 150 |
cubeb_stream_params output_params; |
| 151 |
int r; |
| 152 |
uint32_t latency_frames = 0; |
| 153 |
|
| 154 |
r = cubeb_register_device_collection_changed( |
| 155 |
ctx, static_cast<cubeb_device_type>(CUBEB_DEVICE_TYPE_INPUT), |
| 156 |
device_collection_changed_callback, nullptr); |
| 157 |
ASSERT_EQ(r, CUBEB_OK) << "Error initializing cubeb stream"; |
| 158 |
|
| 159 |
/* typical user-case: mono input, stereo output, low latency. */ |
| 160 |
input_params.format = STREAM_FORMAT; |
| 161 |
input_params.rate = SAMPLE_FREQUENCY; |
| 162 |
input_params.channels = INPUT_CHANNELS; |
| 163 |
input_params.layout = INPUT_LAYOUT; |
| 164 |
input_params.prefs = CUBEB_STREAM_PREF_NONE; |
| 165 |
output_params.format = STREAM_FORMAT; |
| 166 |
output_params.rate = SAMPLE_FREQUENCY; |
| 167 |
output_params.channels = OUTPUT_CHANNELS; |
| 168 |
output_params.layout = OUTPUT_LAYOUT; |
| 169 |
output_params.prefs = CUBEB_STREAM_PREF_NONE; |
| 170 |
|
| 171 |
r = cubeb_get_min_latency(ctx, &output_params, &latency_frames); |
| 172 |
ASSERT_EQ(r, CUBEB_OK) << "Could not get minimal latency"; |
| 173 |
|
| 174 |
r = cubeb_stream_init(ctx, &stream, "Cubeb duplex", NULL, &input_params, NULL, |
| 175 |
&output_params, latency_frames, data_cb_duplex, |
| 176 |
state_cb_duplex, nullptr); |
| 177 |
ASSERT_EQ(r, CUBEB_OK) << "Error initializing cubeb stream"; |
| 178 |
cubeb_stream_destroy(stream); |
| 179 |
} |
| 180 |
|
| 181 |
TEST(cubeb, duplex_collection_change) |
| 182 |
{ |
| 183 |
cubeb * ctx; |
| 184 |
int r; |
| 185 |
|
| 186 |
r = common_init(&ctx, "Cubeb duplex example with collection change"); |
| 187 |
ASSERT_EQ(r, CUBEB_OK) << "Error initializing cubeb library"; |
| 188 |
std::unique_ptr<cubeb, decltype(&cubeb_destroy)> cleanup_cubeb_at_exit( |
| 189 |
ctx, cubeb_destroy); |
| 190 |
|
| 191 |
/* This test needs an available input device, skip it if this host does not |
| 192 |
* have one. */ |
| 193 |
if (!can_run_audio_input_test(ctx)) { |
| 194 |
return; |
| 195 |
} |
| 196 |
|
| 197 |
duplex_collection_change_impl(ctx); |
| 198 |
r = cubeb_register_device_collection_changed( |
| 199 |
ctx, static_cast<cubeb_device_type>(CUBEB_DEVICE_TYPE_INPUT), nullptr, |
| 200 |
nullptr); |
| 201 |
ASSERT_EQ(r, CUBEB_OK); |
| 202 |
} |
| 203 |
|
| 204 |
#ifdef GTEST_HAS_DEATH_TEST |
| 205 |
TEST(cubeb, duplex_collection_change_no_unregister) |
| 206 |
{ |
| 207 |
cubeb * ctx; |
| 208 |
int r; |
| 209 |
|
| 210 |
r = common_init(&ctx, "Cubeb duplex example with collection change"); |
| 211 |
ASSERT_EQ(r, CUBEB_OK) << "Error initializing cubeb library"; |
| 212 |
|
| 213 |
/* This test needs an available input device, skip it if this host does not |
| 214 |
* have one. */ |
| 215 |
if (!can_run_audio_input_test(ctx)) { |
| 216 |
cubeb_destroy(ctx); |
| 217 |
return; |
| 218 |
} |
| 219 |
|
| 220 |
std::unique_ptr<cubeb, decltype(&cubeb_destroy)> cleanup_cubeb_at_exit( |
| 221 |
ctx, [](cubeb * p) noexcept { EXPECT_DEATH(cubeb_destroy(p), ""); }); |
| 222 |
|
| 223 |
duplex_collection_change_impl(ctx); |
| 224 |
} |
| 225 |
#endif |
| 226 |
|
| 227 |
long |
| 228 |
data_cb_input(cubeb_stream * stream, void * user, const void * inputbuffer, |
| 229 |
void * outputbuffer, long nframes) |
| 230 |
{ |
| 231 |
if (stream == NULL || inputbuffer == NULL || outputbuffer != NULL) { |
| 232 |
return CUBEB_ERROR; |
| 233 |
} |
| 234 |
|
| 235 |
return nframes; |
| 236 |
} |
| 237 |
|
| 238 |
void |
| 239 |
state_cb_input(cubeb_stream * stream, void * /*user*/, cubeb_state state) |
| 240 |
{ |
| 241 |
if (stream == NULL) |
| 242 |
return; |
| 243 |
|
| 244 |
switch (state) { |
| 245 |
case CUBEB_STATE_STARTED: |
| 246 |
fprintf(stderr, "stream started\n"); |
| 247 |
break; |
| 248 |
case CUBEB_STATE_STOPPED: |
| 249 |
fprintf(stderr, "stream stopped\n"); |
| 250 |
break; |
| 251 |
case CUBEB_STATE_DRAINED: |
| 252 |
fprintf(stderr, "stream drained\n"); |
| 253 |
break; |
| 254 |
case CUBEB_STATE_ERROR: |
| 255 |
fprintf(stderr, "stream runs into error state\n"); |
| 256 |
break; |
| 257 |
default: |
| 258 |
fprintf(stderr, "unknown stream state %d\n", state); |
| 259 |
} |
| 260 |
|
| 261 |
return; |
| 262 |
} |
| 263 |
|
| 264 |
std::vector<cubeb_devid> |
| 265 |
get_devices(cubeb * ctx, cubeb_device_type type) |
| 266 |
{ |
| 267 |
std::vector<cubeb_devid> devices; |
| 268 |
|
| 269 |
cubeb_device_collection collection; |
| 270 |
int r = cubeb_enumerate_devices(ctx, type, &collection); |
| 271 |
|
| 272 |
if (r != CUBEB_OK) { |
| 273 |
fprintf(stderr, "Failed to enumerate devices\n"); |
| 274 |
return devices; |
| 275 |
} |
| 276 |
|
| 277 |
for (uint32_t i = 0; i < collection.count; i++) { |
| 278 |
if (collection.device[i].state == CUBEB_DEVICE_STATE_ENABLED) { |
| 279 |
devices.emplace_back(collection.device[i].devid); |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
cubeb_device_collection_destroy(ctx, &collection); |
| 284 |
|
| 285 |
return devices; |
| 286 |
} |
| 287 |
|
| 288 |
TEST(cubeb, one_duplex_one_input) |
| 289 |
{ |
| 290 |
cubeb * ctx; |
| 291 |
cubeb_stream * duplex_stream; |
| 292 |
cubeb_stream_params input_params; |
| 293 |
cubeb_stream_params output_params; |
| 294 |
int r; |
| 295 |
user_state_duplex duplex_stream_state; |
| 296 |
uint32_t latency_frames = 0; |
| 297 |
|
| 298 |
r = common_init(&ctx, "Cubeb duplex example"); |
| 299 |
ASSERT_EQ(r, CUBEB_OK) << "Error initializing cubeb library"; |
| 300 |
|
| 301 |
std::unique_ptr<cubeb, decltype(&cubeb_destroy)> cleanup_cubeb_at_exit( |
| 302 |
ctx, cubeb_destroy); |
| 303 |
|
| 304 |
/* This test needs at least two available input devices. */ |
| 305 |
std::vector<cubeb_devid> input_devices = |
| 306 |
get_devices(ctx, CUBEB_DEVICE_TYPE_INPUT); |
| 307 |
if (input_devices.size() < 2) { |
| 308 |
return; |
| 309 |
} |
| 310 |
|
| 311 |
/* This test needs at least one available output device. */ |
| 312 |
std::vector<cubeb_devid> output_devices = |
| 313 |
get_devices(ctx, CUBEB_DEVICE_TYPE_OUTPUT); |
| 314 |
if (output_devices.size() < 1) { |
| 315 |
return; |
| 316 |
} |
| 317 |
|
| 318 |
cubeb_devid duplex_input = input_devices.front(); |
| 319 |
cubeb_devid duplex_output = nullptr; // default device |
| 320 |
cubeb_devid input_only = input_devices.back(); |
| 321 |
|
| 322 |
/* typical use-case: mono voice input, stereo output, low latency. */ |
| 323 |
input_params.format = STREAM_FORMAT; |
| 324 |
input_params.rate = SAMPLE_FREQUENCY; |
| 325 |
input_params.channels = INPUT_CHANNELS; |
| 326 |
input_params.layout = CUBEB_LAYOUT_UNDEFINED; |
| 327 |
input_params.prefs = CUBEB_STREAM_PREF_VOICE; |
| 328 |
|
| 329 |
output_params.format = STREAM_FORMAT; |
| 330 |
output_params.rate = SAMPLE_FREQUENCY; |
| 331 |
output_params.channels = OUTPUT_CHANNELS; |
| 332 |
output_params.layout = OUTPUT_LAYOUT; |
| 333 |
output_params.prefs = CUBEB_STREAM_PREF_NONE; |
| 334 |
|
| 335 |
r = cubeb_get_min_latency(ctx, &output_params, &latency_frames); |
| 336 |
ASSERT_EQ(r, CUBEB_OK) << "Could not get minimal latency"; |
| 337 |
|
| 338 |
r = cubeb_stream_init(ctx, &duplex_stream, "Cubeb duplex", duplex_input, |
| 339 |
&input_params, duplex_output, &output_params, |
| 340 |
latency_frames, data_cb_duplex, state_cb_duplex, |
| 341 |
&duplex_stream_state); |
| 342 |
ASSERT_EQ(r, CUBEB_OK) << "Error initializing duplex cubeb stream"; |
| 343 |
|
| 344 |
std::unique_ptr<cubeb_stream, decltype(&cubeb_stream_destroy)> |
| 345 |
cleanup_stream_at_exit(duplex_stream, cubeb_stream_destroy); |
| 346 |
|
| 347 |
r = cubeb_stream_start(duplex_stream); |
| 348 |
ASSERT_EQ(r, CUBEB_OK) << "Could not start duplex stream"; |
| 349 |
delay(500); |
| 350 |
|
| 351 |
cubeb_stream * input_stream; |
| 352 |
r = cubeb_stream_init(ctx, &input_stream, "Cubeb input", input_only, |
| 353 |
&input_params, NULL, NULL, latency_frames, |
| 354 |
data_cb_input, state_cb_input, nullptr); |
| 355 |
ASSERT_EQ(r, CUBEB_OK) << "Error initializing input-only cubeb stream"; |
| 356 |
|
| 357 |
std::unique_ptr<cubeb_stream, decltype(&cubeb_stream_destroy)> |
| 358 |
cleanup_input_stream_at_exit(input_stream, cubeb_stream_destroy); |
| 359 |
|
| 360 |
r = cubeb_stream_start(input_stream); |
| 361 |
ASSERT_EQ(r, CUBEB_OK) << "Could not start input stream"; |
| 362 |
delay(500); |
| 363 |
|
| 364 |
r = cubeb_stream_stop(duplex_stream); |
| 365 |
ASSERT_EQ(r, CUBEB_OK) << "Could not stop duplex stream"; |
| 366 |
|
| 367 |
r = cubeb_stream_stop(input_stream); |
| 368 |
ASSERT_EQ(r, CUBEB_OK) << "Could not stop input stream"; |
| 369 |
|
| 370 |
ASSERT_FALSE(duplex_stream_state.invalid_audio_value.load()); |
| 371 |
} |
| 372 |
|
| 373 |
#undef SAMPLE_FREQUENCY |
| 374 |
#undef STREAM_FORMAT |
| 375 |
#undef INPUT_CHANNELS |
| 376 |
#undef INPUT_LAYOUT |
| 377 |
#undef OUTPUT_CHANNELS |
| 378 |
#undef OUTPUT_LAYOUT |