| 1 |
#define __STDC_FORMAT_MACROS |
| 2 |
#include "cubeb/cubeb.h" |
| 3 |
#include <atomic> |
| 4 |
#include <cassert> |
| 5 |
#include <cmath> |
| 6 |
#include <cstdarg> |
| 7 |
#include <cstring> |
| 8 |
#include <inttypes.h> |
| 9 |
#include <iostream> |
| 10 |
#include <vector> |
| 11 |
#ifdef _WIN32 |
| 12 |
#include <objbase.h> // Used by CoInitialize() |
| 13 |
#endif |
| 14 |
|
| 15 |
#ifndef M_PI |
| 16 |
#define M_PI 3.14159263 |
| 17 |
#endif |
| 18 |
|
| 19 |
// Default values if none specified |
| 20 |
#define DEFAULT_RATE 44100 |
| 21 |
#define DEFAULT_OUTPUT_CHANNELS 2 |
| 22 |
#define DEFAULT_INPUT_CHANNELS 1 |
| 23 |
|
| 24 |
static const char* state_to_string(cubeb_state state) { |
| 25 |
switch (state) { |
| 26 |
case CUBEB_STATE_STARTED: |
| 27 |
return "CUBEB_STATE_STARTED"; |
| 28 |
case CUBEB_STATE_STOPPED: |
| 29 |
return "CUBEB_STATE_STOPPED"; |
| 30 |
case CUBEB_STATE_DRAINED: |
| 31 |
return "CUBEB_STATE_DRAINED"; |
| 32 |
case CUBEB_STATE_ERROR: |
| 33 |
return "CUBEB_STATE_ERROR"; |
| 34 |
default: |
| 35 |
return "Undefined state"; |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
static const char* device_type_to_string(cubeb_device_type type) { |
| 40 |
switch (type) { |
| 41 |
case CUBEB_DEVICE_TYPE_INPUT: |
| 42 |
return "input"; |
| 43 |
case CUBEB_DEVICE_TYPE_OUTPUT: |
| 44 |
return "output"; |
| 45 |
case CUBEB_DEVICE_TYPE_UNKNOWN: |
| 46 |
return "unknown"; |
| 47 |
default: |
| 48 |
assert(false); |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
static const char* device_state_to_string(cubeb_device_state state) { |
| 53 |
switch (state) { |
| 54 |
case CUBEB_DEVICE_STATE_DISABLED: |
| 55 |
return "disabled"; |
| 56 |
case CUBEB_DEVICE_STATE_UNPLUGGED: |
| 57 |
return "unplugged"; |
| 58 |
case CUBEB_DEVICE_STATE_ENABLED: |
| 59 |
return "enabled"; |
| 60 |
default: |
| 61 |
assert(false); |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
void print_log(const char* msg, ...) { |
| 66 |
va_list args; |
| 67 |
va_start(args, msg); |
| 68 |
vprintf(msg, args); |
| 69 |
printf("\n"); |
| 70 |
va_end(args); |
| 71 |
} |
| 72 |
|
| 73 |
class cubeb_client final { |
| 74 |
public: |
| 75 |
cubeb_client() {} |
| 76 |
~cubeb_client() {} |
| 77 |
|
| 78 |
bool init(char const * backend_name = nullptr); |
| 79 |
cubeb_devid select_device(cubeb_device_type type) const; |
| 80 |
bool init_stream(); |
| 81 |
bool start_stream(); |
| 82 |
bool stop_stream(); |
| 83 |
bool destroy_stream() const; |
| 84 |
bool destroy(); |
| 85 |
bool activate_log(cubeb_log_level log_level) const; |
| 86 |
void set_latency_testing(bool on); |
| 87 |
void set_latency_frames(uint32_t latency_frames); |
| 88 |
uint64_t get_stream_position() const; |
| 89 |
uint32_t get_stream_output_latency() const; |
| 90 |
uint32_t get_stream_input_latency() const; |
| 91 |
uint32_t get_max_channel_count() const; |
| 92 |
|
| 93 |
long user_data_cb(cubeb_stream* stm, void* user, const void* input_buffer, |
| 94 |
void* output_buffer, long nframes); |
| 95 |
|
| 96 |
void user_state_cb(cubeb_stream* stm, void* user, cubeb_state state); |
| 97 |
|
| 98 |
bool register_device_collection_changed(cubeb_device_type devtype) const; |
| 99 |
bool unregister_device_collection_changed(cubeb_device_type devtype) const; |
| 100 |
|
| 101 |
cubeb_stream_params output_params = {}; |
| 102 |
cubeb_devid output_device = nullptr; |
| 103 |
|
| 104 |
cubeb_stream_params input_params = {}; |
| 105 |
cubeb_devid input_device = nullptr; |
| 106 |
|
| 107 |
void force_drain() { _force_drain = true; } |
| 108 |
|
| 109 |
private: |
| 110 |
bool has_input() { return input_params.rate != 0; } |
| 111 |
bool has_output() { return output_params.rate != 0; } |
| 112 |
|
| 113 |
cubeb* context = nullptr; |
| 114 |
|
| 115 |
cubeb_stream* stream = nullptr; |
| 116 |
|
| 117 |
/* Accessed only from client and audio thread. */ |
| 118 |
std::atomic<uint32_t> _rate = {0}; |
| 119 |
std::atomic<uint32_t> _channels = {0}; |
| 120 |
std::atomic<bool> _latency_testing = {false}; |
| 121 |
std::atomic<uint32_t> _latency_frames = {0}; // if !0, override. Else, use min. |
| 122 |
std::atomic<bool> _force_drain = {false}; |
| 123 |
|
| 124 |
|
| 125 |
/* Accessed only from audio thread. */ |
| 126 |
uint32_t _total_frames = 0; |
| 127 |
}; |
| 128 |
|
| 129 |
bool cubeb_client::init(char const * backend_name) { |
| 130 |
int rv = cubeb_init(&context, "Cubeb Test Application", backend_name); |
| 131 |
if (rv != CUBEB_OK) { |
| 132 |
fprintf(stderr, "Could not init cubeb\n"); |
| 133 |
return false; |
| 134 |
} |
| 135 |
fprintf(stderr, "Init cubeb backend: %s\n", cubeb_get_backend_id(context)); |
| 136 |
return true; |
| 137 |
} |
| 138 |
|
| 139 |
static long user_data_cb_s(cubeb_stream* stm, void* user, |
| 140 |
const void* input_buffer, void* output_buffer, |
| 141 |
long nframes) { |
| 142 |
assert(user); |
| 143 |
return static_cast<cubeb_client*>(user)->user_data_cb(stm, user, input_buffer, |
| 144 |
output_buffer, nframes); |
| 145 |
} |
| 146 |
|
| 147 |
static void user_state_cb_s(cubeb_stream* stm, void* user, cubeb_state state) { |
| 148 |
assert(user); |
| 149 |
return static_cast<cubeb_client*>(user)->user_state_cb(stm, user, state); |
| 150 |
} |
| 151 |
|
| 152 |
void input_device_changed_callback_s(cubeb* context, void* user) { |
| 153 |
fprintf(stderr, "input_device_changed_callback_s\n"); |
| 154 |
} |
| 155 |
|
| 156 |
void output_device_changed_callback_s(cubeb* context, void* user) { |
| 157 |
fprintf(stderr, "output_device_changed_callback_s\n"); |
| 158 |
} |
| 159 |
|
| 160 |
void io_device_changed_callback_s(cubeb* context, void* user) { |
| 161 |
fprintf(stderr, "io_device_changed_callback\n"); |
| 162 |
} |
| 163 |
|
| 164 |
bool cubeb_client::init_stream() { |
| 165 |
assert(has_input() || has_output()); |
| 166 |
|
| 167 |
_rate = has_output() ? output_params.rate : input_params.rate; |
| 168 |
_channels = has_output() ? output_params.channels : input_params.channels; |
| 169 |
|
| 170 |
cubeb_stream_params params; |
| 171 |
params.rate = _rate; |
| 172 |
params.channels = 2; |
| 173 |
params.format = CUBEB_SAMPLE_FLOAT32NE; |
| 174 |
|
| 175 |
uint32_t latency = 0; |
| 176 |
int rv = cubeb_get_min_latency(context, ¶ms, &latency); |
| 177 |
if (rv != CUBEB_OK) { |
| 178 |
fprintf(stderr, "Could not get min latency."); |
| 179 |
return false; |
| 180 |
} |
| 181 |
|
| 182 |
if (_latency_frames) { |
| 183 |
latency = _latency_frames.load(); |
| 184 |
printf("Opening a stream with a forced latency of %d frames\n", latency); |
| 185 |
} |
| 186 |
|
| 187 |
rv = |
| 188 |
cubeb_stream_init(context, &stream, "Stream", input_device, |
| 189 |
has_input() ? &input_params : nullptr, output_device, |
| 190 |
has_output() ? &output_params : nullptr, latency, user_data_cb_s, user_state_cb_s, this); |
| 191 |
if (rv != CUBEB_OK) { |
| 192 |
fprintf(stderr, "Could not open the stream\n"); |
| 193 |
return false; |
| 194 |
} |
| 195 |
return true; |
| 196 |
} |
| 197 |
|
| 198 |
bool cubeb_client::start_stream() { |
| 199 |
_force_drain = false; |
| 200 |
int rv = cubeb_stream_start(stream); |
| 201 |
if (rv != CUBEB_OK) { |
| 202 |
fprintf(stderr, "Could not start the stream\n"); |
| 203 |
return false; |
| 204 |
} |
| 205 |
return true; |
| 206 |
} |
| 207 |
|
| 208 |
bool cubeb_client::stop_stream() { |
| 209 |
_force_drain = false; |
| 210 |
int rv = cubeb_stream_stop(stream); |
| 211 |
if (rv != CUBEB_OK) { |
| 212 |
fprintf(stderr, "Could not stop the stream\n"); |
| 213 |
return false; |
| 214 |
} |
| 215 |
return true; |
| 216 |
} |
| 217 |
|
| 218 |
uint64_t cubeb_client::get_stream_position() const { |
| 219 |
uint64_t pos = 0; |
| 220 |
int rv = cubeb_stream_get_position(stream, &pos); |
| 221 |
if (rv != CUBEB_OK) { |
| 222 |
fprintf(stderr, "Could not get the position of the stream\n"); |
| 223 |
return 0; |
| 224 |
} |
| 225 |
return pos; |
| 226 |
} |
| 227 |
|
| 228 |
uint32_t cubeb_client::get_stream_output_latency() const { |
| 229 |
uint32_t latency = 0; |
| 230 |
int rv = cubeb_stream_get_latency(stream, &latency); |
| 231 |
if (rv != CUBEB_OK) { |
| 232 |
fprintf(stderr, "Could not get the latency of the stream\n"); |
| 233 |
return 0; |
| 234 |
} |
| 235 |
return latency; |
| 236 |
} |
| 237 |
|
| 238 |
uint32_t cubeb_client::get_stream_input_latency() const { |
| 239 |
uint32_t latency = 0; |
| 240 |
int rv = cubeb_stream_get_input_latency(stream, &latency); |
| 241 |
if (rv != CUBEB_OK) { |
| 242 |
fprintf(stderr, "Could not get the latency of the input stream\n"); |
| 243 |
return 0; |
| 244 |
} |
| 245 |
return latency; |
| 246 |
} |
| 247 |
|
| 248 |
uint32_t cubeb_client::get_max_channel_count() const { |
| 249 |
uint32_t channels = 0; |
| 250 |
int rv = cubeb_get_max_channel_count(context, &channels); |
| 251 |
if (rv != CUBEB_OK) { |
| 252 |
fprintf(stderr, "Could not get max channel count\n"); |
| 253 |
return 0; |
| 254 |
} |
| 255 |
return channels; |
| 256 |
} |
| 257 |
|
| 258 |
bool cubeb_client::destroy_stream() const { |
| 259 |
cubeb_stream_destroy(stream); |
| 260 |
return true; |
| 261 |
} |
| 262 |
|
| 263 |
bool cubeb_client::destroy() { |
| 264 |
cubeb_destroy(context); |
| 265 |
return true; |
| 266 |
} |
| 267 |
|
| 268 |
bool cubeb_client::activate_log(cubeb_log_level log_level) const { |
| 269 |
cubeb_log_callback log_callback = nullptr; |
| 270 |
if (log_level != CUBEB_LOG_DISABLED) { |
| 271 |
log_callback = print_log; |
| 272 |
} |
| 273 |
|
| 274 |
if (cubeb_set_log_callback(log_level, log_callback) != CUBEB_OK) { |
| 275 |
fprintf(stderr, "Set log callback failed\n"); |
| 276 |
return false; |
| 277 |
} |
| 278 |
return true; |
| 279 |
} |
| 280 |
|
| 281 |
void cubeb_client::set_latency_testing(bool on) { |
| 282 |
_latency_testing = on; |
| 283 |
} |
| 284 |
|
| 285 |
void cubeb_client::set_latency_frames(uint32_t latency_frames) { |
| 286 |
_latency_frames = latency_frames; |
| 287 |
} |
| 288 |
|
| 289 |
static void fill_with_sine_tone(float* buf, uint32_t num_of_frames, |
| 290 |
uint32_t num_of_channels, uint32_t frame_rate, |
| 291 |
uint32_t position) { |
| 292 |
for (uint32_t i = 0; i < num_of_frames; ++i) { |
| 293 |
for (uint32_t c = 0; c < num_of_channels; ++c) { |
| 294 |
buf[i * num_of_channels + c] = |
| 295 |
0.2 * sin(2 * M_PI * (i + position) * 350 / frame_rate); |
| 296 |
buf[i * num_of_channels + c] += |
| 297 |
0.2 * sin(2 * M_PI * (i + position) * 440 / frame_rate); |
| 298 |
} |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
long cubeb_client::user_data_cb(cubeb_stream* stm, void* user, |
| 303 |
const void* input_buffer, void* output_buffer, |
| 304 |
long nframes) { |
| 305 |
if (input_buffer && output_buffer) { |
| 306 |
const float* in = static_cast<const float*>(input_buffer); |
| 307 |
float* out = static_cast<float*>(output_buffer); |
| 308 |
if (_latency_testing) { |
| 309 |
for (int32_t i = 0; i < nframes; i++) { |
| 310 |
// Impulses every second, mixed with the input signal fed back at half |
| 311 |
// gain, to measure the input-to-output latency via feedback. |
| 312 |
uint32_t clock = ((_total_frames + i) % _rate); |
| 313 |
if (!clock) { |
| 314 |
for (uint32_t j = 0; j < _channels; j++) { |
| 315 |
out[i * _channels + j] = 1.0 + in[i] * 0.5; |
| 316 |
} |
| 317 |
} else { |
| 318 |
for (uint32_t j = 0; j < _channels; j++) { |
| 319 |
out[i * _channels + j] = 0.0 + in[i] * 0.5; |
| 320 |
} |
| 321 |
} |
| 322 |
} |
| 323 |
} else { |
| 324 |
for (int32_t i = 0; i < nframes; i++) { |
| 325 |
for (uint32_t j = 0; j < _channels; j++) { |
| 326 |
out[i * _channels + j] = in[i]; |
| 327 |
} |
| 328 |
} |
| 329 |
} |
| 330 |
} else if (output_buffer && !input_buffer) { |
| 331 |
fill_with_sine_tone(static_cast<float*>(output_buffer), nframes, _channels, |
| 332 |
_rate, _total_frames); |
| 333 |
} |
| 334 |
|
| 335 |
_total_frames += nframes; |
| 336 |
|
| 337 |
if (_force_drain) { |
| 338 |
return nframes - 1; |
| 339 |
} |
| 340 |
|
| 341 |
return nframes; |
| 342 |
} |
| 343 |
|
| 344 |
void cubeb_client::user_state_cb(cubeb_stream* stm, void* user, |
| 345 |
cubeb_state state) { |
| 346 |
fprintf(stderr, "state is %s\n", state_to_string(state)); |
| 347 |
} |
| 348 |
|
| 349 |
bool cubeb_client::register_device_collection_changed( |
| 350 |
cubeb_device_type devtype) const { |
| 351 |
cubeb_device_collection_changed_callback callback = nullptr; |
| 352 |
if (devtype == static_cast<cubeb_device_type>(CUBEB_DEVICE_TYPE_INPUT | |
| 353 |
CUBEB_DEVICE_TYPE_OUTPUT)) { |
| 354 |
callback = io_device_changed_callback_s; |
| 355 |
} else if (devtype & CUBEB_DEVICE_TYPE_OUTPUT) { |
| 356 |
callback = output_device_changed_callback_s; |
| 357 |
} else if (devtype & CUBEB_DEVICE_TYPE_INPUT) { |
| 358 |
callback = input_device_changed_callback_s; |
| 359 |
} |
| 360 |
int r = cubeb_register_device_collection_changed( |
| 361 |
context, devtype, callback, nullptr); |
| 362 |
if (r != CUBEB_OK) { |
| 363 |
return false; |
| 364 |
} |
| 365 |
return true; |
| 366 |
} |
| 367 |
|
| 368 |
bool cubeb_client::unregister_device_collection_changed( |
| 369 |
cubeb_device_type devtype) const { |
| 370 |
int r = cubeb_register_device_collection_changed( |
| 371 |
context, devtype, nullptr, nullptr); |
| 372 |
if (r != CUBEB_OK) { |
| 373 |
return false; |
| 374 |
} |
| 375 |
return true; |
| 376 |
} |
| 377 |
|
| 378 |
enum play_mode { |
| 379 |
RECORD, |
| 380 |
PLAYBACK, |
| 381 |
DUPLEX, |
| 382 |
LATENCY_TESTING, |
| 383 |
COLLECTION_CHANGE, |
| 384 |
}; |
| 385 |
|
| 386 |
struct operation_data { |
| 387 |
play_mode pm; |
| 388 |
uint32_t rate; |
| 389 |
cubeb_device_type collection_device_type; |
| 390 |
}; |
| 391 |
|
| 392 |
void print_help() { |
| 393 |
const char * msg = |
| 394 |
"0: change log level to disabled\n" |
| 395 |
"1: change log level to normal\n" |
| 396 |
"2: change log level to verbose\n" |
| 397 |
"c: get max number of channels\n" |
| 398 |
"p: start a initialized stream\n" |
| 399 |
"s: stop a started stream\n" |
| 400 |
"d: destroy stream\n" |
| 401 |
"e: force stream to drain\n" |
| 402 |
"f: get stream position (client thread)\n" |
| 403 |
"i: change device type to input\n" |
| 404 |
"o: change device type to output\n" |
| 405 |
"a: change device type to input and output\n" |
| 406 |
"k: change device type to unknown\n" |
| 407 |
"r: register device collection changed callback for the current device type\n" |
| 408 |
"u: unregister device collection changed callback for the current device type\n" |
| 409 |
"q: quit\n" |
| 410 |
"h: print this message\n"; |
| 411 |
fprintf(stderr, "%s\n", msg); |
| 412 |
} |
| 413 |
|
| 414 |
bool choose_action(cubeb_client& cl, operation_data * op, int c) { |
| 415 |
// Consume "enter" and "space" |
| 416 |
while (c == 10 || c == 32) { |
| 417 |
c = getchar(); |
| 418 |
} |
| 419 |
if (c == EOF) { |
| 420 |
c = 'q'; |
| 421 |
} |
| 422 |
|
| 423 |
if (c == 'q') { |
| 424 |
if (op->pm == PLAYBACK || op->pm == RECORD || op->pm == DUPLEX || op->pm == LATENCY_TESTING) { |
| 425 |
bool res = cl.stop_stream(); |
| 426 |
if (!res) { |
| 427 |
fprintf(stderr, "stop_stream failed\n"); |
| 428 |
} |
| 429 |
res = cl.destroy_stream(); |
| 430 |
if (!res) { |
| 431 |
fprintf(stderr, "destroy_stream failed\n"); |
| 432 |
} |
| 433 |
} else if (op->pm == COLLECTION_CHANGE) { |
| 434 |
bool res = cl.unregister_device_collection_changed(op->collection_device_type); |
| 435 |
if (!res) { |
| 436 |
fprintf(stderr, "unregister_device_collection_changed failed\n"); |
| 437 |
} |
| 438 |
} |
| 439 |
return false; // exit the loop |
| 440 |
} else if (c == 'h') { |
| 441 |
print_help(); |
| 442 |
} else if (c == '0') { |
| 443 |
cl.activate_log(CUBEB_LOG_DISABLED); |
| 444 |
fprintf(stderr, "Log level changed to DISABLED\n"); |
| 445 |
} else if (c == '1') { |
| 446 |
cl.activate_log(CUBEB_LOG_DISABLED); |
| 447 |
cl.activate_log(CUBEB_LOG_NORMAL); |
| 448 |
fprintf(stderr, "Log level changed to NORMAL\n"); |
| 449 |
} else if (c == '2') { |
| 450 |
cl.activate_log(CUBEB_LOG_DISABLED); |
| 451 |
cl.activate_log(CUBEB_LOG_VERBOSE); |
| 452 |
fprintf(stderr, "Log level changed to VERBOSE\n"); |
| 453 |
} else if (c == 'p') { |
| 454 |
bool res = cl.start_stream(); |
| 455 |
if (res) { |
| 456 |
fprintf(stderr, "start_stream succeed\n"); |
| 457 |
} else { |
| 458 |
fprintf(stderr, "start_stream failed\n"); |
| 459 |
} |
| 460 |
} else if (c == 's') { |
| 461 |
bool res = cl.stop_stream(); |
| 462 |
if (res) { |
| 463 |
fprintf(stderr, "stop_stream succeed\n"); |
| 464 |
} else { |
| 465 |
fprintf(stderr, "stop_stream failed\n"); |
| 466 |
} |
| 467 |
} else if (c == 'd') { |
| 468 |
bool res = cl.destroy_stream(); |
| 469 |
if (res) { |
| 470 |
fprintf(stderr, "destroy_stream succeed\n"); |
| 471 |
} else { |
| 472 |
fprintf(stderr, "destroy_stream failed\n"); |
| 473 |
} |
| 474 |
} else if (c == 'e') { |
| 475 |
cl.force_drain(); |
| 476 |
} else if (c == 'c') { |
| 477 |
uint32_t channel_count = cl.get_max_channel_count(); |
| 478 |
fprintf(stderr, "max channel count (default output device): %u\n", channel_count); |
| 479 |
} else if (c == 'f') { |
| 480 |
uint64_t pos = cl.get_stream_position(); |
| 481 |
uint32_t latency; |
| 482 |
fprintf(stderr, "stream position %" PRIu64 ".", pos); |
| 483 |
if(op->pm == PLAYBACK || op->pm == DUPLEX) { |
| 484 |
latency = cl.get_stream_output_latency(); |
| 485 |
fprintf(stderr, " (output latency %" PRIu32 ")", latency); |
| 486 |
} |
| 487 |
if(op->pm == RECORD || op->pm == DUPLEX) { |
| 488 |
latency = cl.get_stream_input_latency(); |
| 489 |
fprintf(stderr, " (input latency %" PRIu32 ")", latency); |
| 490 |
} |
| 491 |
fprintf(stderr, "\n"); |
| 492 |
} else if (c == 'i') { |
| 493 |
op->collection_device_type = CUBEB_DEVICE_TYPE_INPUT; |
| 494 |
fprintf(stderr, "collection device type changed to INPUT\n"); |
| 495 |
} else if (c == 'o') { |
| 496 |
op->collection_device_type = CUBEB_DEVICE_TYPE_OUTPUT; |
| 497 |
fprintf(stderr, "collection device type changed to OUTPUT\n"); |
| 498 |
} else if (c == 'a') { |
| 499 |
op->collection_device_type = static_cast<cubeb_device_type>(CUBEB_DEVICE_TYPE_INPUT | CUBEB_DEVICE_TYPE_OUTPUT); |
| 500 |
fprintf(stderr, "collection device type changed to INPUT | OUTPUT\n"); |
| 501 |
} else if (c == 'k') { |
| 502 |
op->collection_device_type = CUBEB_DEVICE_TYPE_UNKNOWN; |
| 503 |
fprintf(stderr, "collection device type changed to UNKNOWN\n"); |
| 504 |
} else if (c == 'r') { |
| 505 |
bool res = cl.register_device_collection_changed(op->collection_device_type); |
| 506 |
if (res) { |
| 507 |
fprintf(stderr, "register_device_collection_changed succeed\n"); |
| 508 |
} else { |
| 509 |
fprintf(stderr, "register_device_collection_changed failed\n"); |
| 510 |
} |
| 511 |
} else if (c == 'u') { |
| 512 |
bool res = cl.unregister_device_collection_changed(op->collection_device_type); |
| 513 |
if (res) { |
| 514 |
fprintf(stderr, "unregister_device_collection_changed succeed\n"); |
| 515 |
} else { |
| 516 |
fprintf(stderr, "unregister_device_collection_changed failed\n"); |
| 517 |
} |
| 518 |
} else { |
| 519 |
fprintf(stderr, "Error: '%c' is not a valid entry\n", c); |
| 520 |
} |
| 521 |
|
| 522 |
return true; // Loop up |
| 523 |
} |
| 524 |
|
| 525 |
cubeb_devid cubeb_client::select_device(cubeb_device_type type) const |
| 526 |
{ |
| 527 |
assert(type == CUBEB_DEVICE_TYPE_INPUT || type == CUBEB_DEVICE_TYPE_OUTPUT); |
| 528 |
|
| 529 |
cubeb_device_collection collection; |
| 530 |
if (cubeb_enumerate_devices(context, type, &collection) == |
| 531 |
CUBEB_ERROR_NOT_SUPPORTED) { |
| 532 |
fprintf(stderr, |
| 533 |
"Not support %s device selection. Force to use default device\n", |
| 534 |
device_type_to_string(type)); |
| 535 |
return nullptr; |
| 536 |
} |
| 537 |
|
| 538 |
assert(collection.count); |
| 539 |
fprintf(stderr, "Found %zu %s devices. Choose one:\n", collection.count, |
| 540 |
device_type_to_string(type)); |
| 541 |
|
| 542 |
std::vector<cubeb_devid> devices; |
| 543 |
devices.emplace_back(nullptr); |
| 544 |
fprintf(stderr, "# 0\n\tname: system default device\n"); |
| 545 |
for (size_t i = 0; i < collection.count; i++) { |
| 546 |
assert(collection.device[i].type == type); |
| 547 |
fprintf(stderr, |
| 548 |
"# %zu %s\n" |
| 549 |
"\tname: %s\n" |
| 550 |
"\tdevice id: %s\n" |
| 551 |
"\tmax channels: %u\n" |
| 552 |
"\tstate: %s\n", |
| 553 |
devices.size(), |
| 554 |
collection.device[i].preferred ? " (PREFERRED)" : "", |
| 555 |
collection.device[i].friendly_name, collection.device[i].device_id, |
| 556 |
collection.device[i].max_channels, |
| 557 |
device_state_to_string(collection.device[i].state)); |
| 558 |
devices.emplace_back(collection.device[i].devid); |
| 559 |
} |
| 560 |
|
| 561 |
cubeb_device_collection_destroy(context, &collection); |
| 562 |
|
| 563 |
size_t number; |
| 564 |
std::cout << "Enter device number: "; |
| 565 |
std::cin >> number; |
| 566 |
while (!std::cin || number >= devices.size()) { |
| 567 |
std::cin.clear(); |
| 568 |
std::cin.ignore(100, '\n'); |
| 569 |
std::cout << "Error: Please enter a valid numeric input. Enter again: "; |
| 570 |
std::cin >> number; |
| 571 |
} |
| 572 |
return devices[number]; |
| 573 |
} |
| 574 |
|
| 575 |
int main(int argc, char* argv[]) { |
| 576 |
#ifdef _WIN32 |
| 577 |
CoInitialize(nullptr); |
| 578 |
#endif |
| 579 |
|
| 580 |
operation_data op; |
| 581 |
op.pm = PLAYBACK; |
| 582 |
if (argc > 1) { |
| 583 |
if ('r' == argv[1][0]) { |
| 584 |
op.pm = RECORD; |
| 585 |
} else if ('p' == argv[1][0]) { |
| 586 |
op.pm = PLAYBACK; |
| 587 |
} else if ('d' == argv[1][0]) { |
| 588 |
op.pm = DUPLEX; |
| 589 |
} else if ('l' == argv[1][0]) { |
| 590 |
op.pm = LATENCY_TESTING; |
| 591 |
} else if ('c' == argv[1][0]) { |
| 592 |
op.pm = COLLECTION_CHANGE; |
| 593 |
} |
| 594 |
} |
| 595 |
op.rate = DEFAULT_RATE; |
| 596 |
uint32_t latency_override = 0; |
| 597 |
if (op.pm == LATENCY_TESTING && argc > 2) { |
| 598 |
latency_override = strtoul(argv[2], NULL, 10); |
| 599 |
printf("LATENCY_TESTING %d\n", latency_override); |
| 600 |
} else if (argc > 2) { |
| 601 |
op.rate = strtoul(argv[2], NULL, 0); |
| 602 |
} |
| 603 |
|
| 604 |
bool res = false; |
| 605 |
cubeb_client cl; |
| 606 |
cl.activate_log(CUBEB_LOG_NORMAL); |
| 607 |
fprintf(stderr, "Log level is DISABLED\n"); |
| 608 |
cl.init(/* default backend */); |
| 609 |
|
| 610 |
op.collection_device_type = CUBEB_DEVICE_TYPE_UNKNOWN; |
| 611 |
fprintf(stderr, "collection device type is UNKNOWN\n"); |
| 612 |
if (op.pm == COLLECTION_CHANGE) { |
| 613 |
op.collection_device_type = CUBEB_DEVICE_TYPE_OUTPUT; |
| 614 |
fprintf(stderr, "collection device type changed to OUTPUT\n"); |
| 615 |
res = cl.register_device_collection_changed(op.collection_device_type); |
| 616 |
if (res) { |
| 617 |
fprintf(stderr, "register_device_collection_changed succeed\n"); |
| 618 |
} else { |
| 619 |
fprintf(stderr, "register_device_collection_changed failed\n"); |
| 620 |
} |
| 621 |
} else { |
| 622 |
if (op.pm == PLAYBACK || op.pm == DUPLEX || op.pm == LATENCY_TESTING) { |
| 623 |
cl.output_device = cl.select_device(CUBEB_DEVICE_TYPE_OUTPUT); |
| 624 |
cl.output_params = {CUBEB_SAMPLE_FLOAT32NE, op.rate, DEFAULT_OUTPUT_CHANNELS, |
| 625 |
CUBEB_LAYOUT_STEREO, CUBEB_STREAM_PREF_NONE}; |
| 626 |
} |
| 627 |
if (op.pm == RECORD || op.pm == DUPLEX || op.pm == LATENCY_TESTING) { |
| 628 |
cl.input_device = cl.select_device(CUBEB_DEVICE_TYPE_INPUT); |
| 629 |
cl.input_params = {CUBEB_SAMPLE_FLOAT32NE, op.rate, DEFAULT_INPUT_CHANNELS, CUBEB_LAYOUT_UNDEFINED, CUBEB_STREAM_PREF_NONE}; |
| 630 |
} |
| 631 |
if (op.pm == LATENCY_TESTING) { |
| 632 |
cl.set_latency_testing(true); |
| 633 |
if (latency_override) { |
| 634 |
cl.set_latency_frames(latency_override); |
| 635 |
} |
| 636 |
} |
| 637 |
res = cl.init_stream(); |
| 638 |
if (!res) { |
| 639 |
fprintf(stderr, "stream_init failed\n"); |
| 640 |
return -1; |
| 641 |
} |
| 642 |
fprintf(stderr, "stream_init succeed\n"); |
| 643 |
|
| 644 |
res = cl.start_stream(); |
| 645 |
if (res) { |
| 646 |
fprintf(stderr, "stream_start succeed\n"); |
| 647 |
} else { |
| 648 |
fprintf(stderr, "stream_init failed\n"); |
| 649 |
} |
| 650 |
} |
| 651 |
|
| 652 |
// User input |
| 653 |
do { |
| 654 |
fprintf(stderr, "press `q` to abort or `h` for help\n"); |
| 655 |
} while (choose_action(cl, &op, getchar())); |
| 656 |
|
| 657 |
cl.destroy(); |
| 658 |
|
| 659 |
return 0; |
| 660 |
} |