| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
#include <pjmedia-audiodev/audiodev_imp.h> |
| 20 |
#include <pj/assert.h> |
| 21 |
#include <pj/log.h> |
| 22 |
#include <pj/os.h> |
| 23 |
#include <pj/string.h> |
| 24 |
#include <pjmedia/event.h> |
| 25 |
|
| 26 |
#if PJMEDIA_AUDIO_DEV_HAS_PORTAUDIO |
| 27 |
|
| 28 |
#include <portaudio.h> |
| 29 |
|
| 30 |
#define THIS_FILE "pa_dev.c" |
| 31 |
#define DRIVER_NAME "PA" |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
struct pa_aud_factory |
| 39 |
{ |
| 40 |
pjmedia_aud_dev_factory base; |
| 41 |
pj_pool_factory *pf; |
| 42 |
pj_pool_t *pool; |
| 43 |
}; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
struct pa_aud_stream |
| 52 |
{ |
| 53 |
pjmedia_aud_stream base; |
| 54 |
|
| 55 |
pj_pool_t *pool; |
| 56 |
pj_str_t name; |
| 57 |
pjmedia_dir dir; |
| 58 |
int play_id; |
| 59 |
int rec_id; |
| 60 |
int bytes_per_sample; |
| 61 |
pj_uint32_t samples_per_sec; |
| 62 |
unsigned samples_per_frame; |
| 63 |
int channel_count; |
| 64 |
|
| 65 |
PaStream *rec_strm; |
| 66 |
PaStream *play_strm; |
| 67 |
|
| 68 |
void *user_data; |
| 69 |
pjmedia_aud_rec_cb rec_cb; |
| 70 |
pjmedia_aud_play_cb play_cb; |
| 71 |
|
| 72 |
pj_timestamp play_timestamp; |
| 73 |
pj_timestamp rec_timestamp; |
| 74 |
pj_uint32_t underflow; |
| 75 |
pj_uint32_t overflow; |
| 76 |
|
| 77 |
pj_bool_t quit_flag; |
| 78 |
|
| 79 |
pj_bool_t rec_thread_exited; |
| 80 |
pj_bool_t rec_thread_initialized; |
| 81 |
pj_thread_desc rec_thread_desc; |
| 82 |
pj_thread_t *rec_thread; |
| 83 |
|
| 84 |
pj_bool_t play_thread_exited; |
| 85 |
pj_bool_t play_thread_initialized; |
| 86 |
pj_thread_desc play_thread_desc; |
| 87 |
pj_thread_t *play_thread; |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
pj_int16_t *rec_buf; |
| 95 |
unsigned rec_buf_count; |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
pj_int16_t *play_buf; |
| 102 |
unsigned play_buf_count; |
| 103 |
}; |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
static pj_status_t pa_init(pjmedia_aud_dev_factory *f); |
| 108 |
static pj_status_t pa_destroy(pjmedia_aud_dev_factory *f); |
| 109 |
static pj_status_t pa_refresh(pjmedia_aud_dev_factory *f); |
| 110 |
static unsigned pa_get_dev_count(pjmedia_aud_dev_factory *f); |
| 111 |
static pj_status_t pa_get_dev_info(pjmedia_aud_dev_factory *f, |
| 112 |
unsigned index, |
| 113 |
pjmedia_aud_dev_info *info); |
| 114 |
static pj_status_t pa_default_param(pjmedia_aud_dev_factory *f, |
| 115 |
unsigned index, |
| 116 |
pjmedia_aud_param *param); |
| 117 |
static pj_status_t pa_create_stream(pjmedia_aud_dev_factory *f, |
| 118 |
const pjmedia_aud_param *param, |
| 119 |
pjmedia_aud_rec_cb rec_cb, |
| 120 |
pjmedia_aud_play_cb play_cb, |
| 121 |
void *user_data, |
| 122 |
pjmedia_aud_stream **p_aud_strm); |
| 123 |
|
| 124 |
|
| 125 |
static pj_status_t strm_get_param(pjmedia_aud_stream *strm, |
| 126 |
pjmedia_aud_param *param); |
| 127 |
static pj_status_t strm_get_cap(pjmedia_aud_stream *strm, |
| 128 |
pjmedia_aud_dev_cap cap, |
| 129 |
void *value); |
| 130 |
static pj_status_t strm_set_cap(pjmedia_aud_stream *strm, |
| 131 |
pjmedia_aud_dev_cap cap, |
| 132 |
const void *value); |
| 133 |
static pj_status_t strm_start(pjmedia_aud_stream *strm); |
| 134 |
static pj_status_t strm_stop(pjmedia_aud_stream *strm); |
| 135 |
static pj_status_t strm_destroy(pjmedia_aud_stream *strm); |
| 136 |
|
| 137 |
|
| 138 |
static pjmedia_aud_dev_factory_op pa_op = |
| 139 |
{ |
| 140 |
&pa_init, |
| 141 |
&pa_destroy, |
| 142 |
&pa_get_dev_count, |
| 143 |
&pa_get_dev_info, |
| 144 |
&pa_default_param, |
| 145 |
&pa_create_stream, |
| 146 |
&pa_refresh |
| 147 |
}; |
| 148 |
|
| 149 |
static pjmedia_aud_stream_op pa_strm_op = |
| 150 |
{ |
| 151 |
&strm_get_param, |
| 152 |
&strm_get_cap, |
| 153 |
&strm_set_cap, |
| 154 |
&strm_start, |
| 155 |
&strm_stop, |
| 156 |
&strm_destroy |
| 157 |
}; |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
static int PaRecorderCallback(const void *input, |
| 162 |
void *output, |
| 163 |
unsigned long frameCount, |
| 164 |
const PaStreamCallbackTimeInfo* timeInfo, |
| 165 |
PaStreamCallbackFlags statusFlags, |
| 166 |
void *userData ) |
| 167 |
{ |
| 168 |
struct pa_aud_stream *stream = (struct pa_aud_stream*) userData; |
| 169 |
pj_status_t status = 0; |
| 170 |
unsigned nsamples; |
| 171 |
|
| 172 |
PJ_UNUSED_ARG(output); |
| 173 |
PJ_UNUSED_ARG(timeInfo); |
| 174 |
|
| 175 |
if (stream->quit_flag) |
| 176 |
goto on_break; |
| 177 |
|
| 178 |
if (input == NULL) |
| 179 |
return paContinue; |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
if (stream->rec_thread_initialized == 0 || !pj_thread_is_registered()) |
| 189 |
{ |
| 190 |
pj_bzero(stream->rec_thread_desc, sizeof(pj_thread_desc)); |
| 191 |
status = pj_thread_register("pa_rec", stream->rec_thread_desc, |
| 192 |
&stream->rec_thread); |
| 193 |
stream->rec_thread_initialized = 1; |
| 194 |
PJ_LOG(5,(THIS_FILE, "Recorder thread started")); |
| 195 |
} |
| 196 |
|
| 197 |
if (statusFlags & paInputUnderflow) |
| 198 |
++stream->underflow; |
| 199 |
if (statusFlags & paInputOverflow) |
| 200 |
++stream->overflow; |
| 201 |
|
| 202 |
|
| 203 |
nsamples = frameCount * stream->channel_count + stream->rec_buf_count; |
| 204 |
|
| 205 |
if (nsamples >= stream->samples_per_frame) |
| 206 |
{ |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
if (stream->rec_buf_count) { |
| 211 |
unsigned chunk_count = 0; |
| 212 |
pjmedia_frame frame; |
| 213 |
|
| 214 |
chunk_count = stream->samples_per_frame - stream->rec_buf_count; |
| 215 |
pjmedia_copy_samples(stream->rec_buf + stream->rec_buf_count, |
| 216 |
(pj_int16_t*)input, chunk_count); |
| 217 |
|
| 218 |
frame.type = PJMEDIA_FRAME_TYPE_AUDIO; |
| 219 |
frame.buf = (void*) stream->rec_buf; |
| 220 |
frame.size = stream->samples_per_frame * stream->bytes_per_sample; |
| 221 |
frame.timestamp.u64 = stream->rec_timestamp.u64; |
| 222 |
frame.bit_info = 0; |
| 223 |
|
| 224 |
status = (*stream->rec_cb)(stream->user_data, &frame); |
| 225 |
|
| 226 |
input = (pj_int16_t*) input + chunk_count; |
| 227 |
nsamples -= stream->samples_per_frame; |
| 228 |
stream->rec_buf_count = 0; |
| 229 |
stream->rec_timestamp.u64 += stream->samples_per_frame / |
| 230 |
stream->channel_count; |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
while (nsamples >= stream->samples_per_frame && status == 0) { |
| 235 |
pjmedia_frame frame; |
| 236 |
|
| 237 |
frame.type = PJMEDIA_FRAME_TYPE_AUDIO; |
| 238 |
frame.buf = (void*) input; |
| 239 |
frame.size = stream->samples_per_frame * stream->bytes_per_sample; |
| 240 |
frame.timestamp.u64 = stream->rec_timestamp.u64; |
| 241 |
frame.bit_info = 0; |
| 242 |
|
| 243 |
status = (*stream->rec_cb)(stream->user_data, &frame); |
| 244 |
|
| 245 |
input = (pj_int16_t*) input + stream->samples_per_frame; |
| 246 |
nsamples -= stream->samples_per_frame; |
| 247 |
stream->rec_timestamp.u64 += stream->samples_per_frame / |
| 248 |
stream->channel_count; |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
if (nsamples && status == 0) { |
| 253 |
stream->rec_buf_count = nsamples; |
| 254 |
pjmedia_copy_samples(stream->rec_buf, (pj_int16_t*)input, |
| 255 |
nsamples); |
| 256 |
} |
| 257 |
|
| 258 |
} else { |
| 259 |
|
| 260 |
pjmedia_copy_samples(stream->rec_buf + stream->rec_buf_count, |
| 261 |
(pj_int16_t*)input, |
| 262 |
frameCount * stream->channel_count); |
| 263 |
stream->rec_buf_count += frameCount * stream->channel_count; |
| 264 |
} |
| 265 |
|
| 266 |
if (status==0) |
| 267 |
return paContinue; |
| 268 |
|
| 269 |
on_break: |
| 270 |
if (status != PJ_SUCCESS && !stream->quit_flag) { |
| 271 |
pjmedia_event e; |
| 272 |
|
| 273 |
PJ_PERROR(3, (THIS_FILE, status, "PortAudio recorder callback stopped due to error")); |
| 274 |
|
| 275 |
|
| 276 |
pjmedia_event_init(&e, PJMEDIA_EVENT_AUD_DEV_ERROR, |
| 277 |
&stream->rec_timestamp, &stream->base); |
| 278 |
e.data.aud_dev_err.dir = PJMEDIA_DIR_CAPTURE; |
| 279 |
e.data.aud_dev_err.status = status; |
| 280 |
e.data.aud_dev_err.id = stream->rec_id; |
| 281 |
|
| 282 |
pjmedia_event_publish(NULL, &stream->base, &e, |
| 283 |
PJMEDIA_EVENT_PUBLISH_DEFAULT); |
| 284 |
} |
| 285 |
stream->rec_thread_exited = 1; |
| 286 |
return paAbort; |
| 287 |
} |
| 288 |
|
| 289 |
static int PaPlayerCallback( const void *input, |
| 290 |
void *output, |
| 291 |
unsigned long frameCount, |
| 292 |
const PaStreamCallbackTimeInfo* timeInfo, |
| 293 |
PaStreamCallbackFlags statusFlags, |
| 294 |
void *userData ) |
| 295 |
{ |
| 296 |
struct pa_aud_stream *stream = (struct pa_aud_stream*) userData; |
| 297 |
pj_status_t status = 0; |
| 298 |
unsigned nsamples_req = frameCount * stream->channel_count; |
| 299 |
|
| 300 |
PJ_UNUSED_ARG(input); |
| 301 |
PJ_UNUSED_ARG(timeInfo); |
| 302 |
|
| 303 |
if (stream->quit_flag) |
| 304 |
goto on_break; |
| 305 |
|
| 306 |
if (output == NULL) |
| 307 |
return paContinue; |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
if (stream->play_thread_initialized == 0 || !pj_thread_is_registered()) |
| 317 |
{ |
| 318 |
pj_bzero(stream->play_thread_desc, sizeof(pj_thread_desc)); |
| 319 |
status = pj_thread_register("portaudio", stream->play_thread_desc, |
| 320 |
&stream->play_thread); |
| 321 |
stream->play_thread_initialized = 1; |
| 322 |
PJ_LOG(5,(THIS_FILE, "Player thread started")); |
| 323 |
} |
| 324 |
|
| 325 |
if (statusFlags & paOutputUnderflow) |
| 326 |
++stream->underflow; |
| 327 |
if (statusFlags & paOutputOverflow) |
| 328 |
++stream->overflow; |
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
if (stream->play_buf_count) { |
| 333 |
|
| 334 |
if (stream->play_buf_count >= nsamples_req) { |
| 335 |
pjmedia_copy_samples((pj_int16_t*)output, stream->play_buf, |
| 336 |
nsamples_req); |
| 337 |
stream->play_buf_count -= nsamples_req; |
| 338 |
pjmedia_move_samples(stream->play_buf, |
| 339 |
stream->play_buf + nsamples_req, |
| 340 |
stream->play_buf_count); |
| 341 |
nsamples_req = 0; |
| 342 |
|
| 343 |
return paContinue; |
| 344 |
} |
| 345 |
|
| 346 |
|
| 347 |
pjmedia_copy_samples((pj_int16_t*)output, stream->play_buf, |
| 348 |
stream->play_buf_count); |
| 349 |
nsamples_req -= stream->play_buf_count; |
| 350 |
output = (pj_int16_t*)output + stream->play_buf_count; |
| 351 |
stream->play_buf_count = 0; |
| 352 |
} |
| 353 |
|
| 354 |
|
| 355 |
while (nsamples_req && status == 0) { |
| 356 |
if (nsamples_req >= stream->samples_per_frame) { |
| 357 |
pjmedia_frame frame; |
| 358 |
|
| 359 |
frame.type = PJMEDIA_FRAME_TYPE_AUDIO; |
| 360 |
frame.buf = output; |
| 361 |
frame.size = stream->samples_per_frame * stream->bytes_per_sample; |
| 362 |
frame.timestamp.u64 = stream->play_timestamp.u64; |
| 363 |
frame.bit_info = 0; |
| 364 |
|
| 365 |
status = (*stream->play_cb)(stream->user_data, &frame); |
| 366 |
if (status != PJ_SUCCESS) |
| 367 |
goto on_break; |
| 368 |
|
| 369 |
if (frame.type != PJMEDIA_FRAME_TYPE_AUDIO) |
| 370 |
pj_bzero(frame.buf, frame.size); |
| 371 |
|
| 372 |
nsamples_req -= stream->samples_per_frame; |
| 373 |
output = (pj_int16_t*)output + stream->samples_per_frame; |
| 374 |
} else { |
| 375 |
pjmedia_frame frame; |
| 376 |
|
| 377 |
frame.type = PJMEDIA_FRAME_TYPE_AUDIO; |
| 378 |
frame.buf = stream->play_buf; |
| 379 |
frame.size = stream->samples_per_frame * stream->bytes_per_sample; |
| 380 |
frame.timestamp.u64 = stream->play_timestamp.u64; |
| 381 |
frame.bit_info = 0; |
| 382 |
|
| 383 |
status = (*stream->play_cb)(stream->user_data, &frame); |
| 384 |
if (status != PJ_SUCCESS) |
| 385 |
goto on_break; |
| 386 |
|
| 387 |
if (frame.type != PJMEDIA_FRAME_TYPE_AUDIO) |
| 388 |
pj_bzero(frame.buf, frame.size); |
| 389 |
|
| 390 |
pjmedia_copy_samples((pj_int16_t*)output, stream->play_buf, |
| 391 |
nsamples_req); |
| 392 |
stream->play_buf_count = stream->samples_per_frame - nsamples_req; |
| 393 |
pjmedia_move_samples(stream->play_buf, |
| 394 |
stream->play_buf+nsamples_req, |
| 395 |
stream->play_buf_count); |
| 396 |
nsamples_req = 0; |
| 397 |
} |
| 398 |
|
| 399 |
stream->play_timestamp.u64 += stream->samples_per_frame / |
| 400 |
stream->channel_count; |
| 401 |
} |
| 402 |
|
| 403 |
if (status==0) |
| 404 |
return paContinue; |
| 405 |
|
| 406 |
on_break: |
| 407 |
if (status != PJ_SUCCESS && !stream->quit_flag) { |
| 408 |
pjmedia_event e; |
| 409 |
|
| 410 |
PJ_PERROR(3, (THIS_FILE, status, "PortAudio playback callback stopped due to error")); |
| 411 |
|
| 412 |
|
| 413 |
pjmedia_event_init(&e, PJMEDIA_EVENT_AUD_DEV_ERROR, |
| 414 |
&stream->play_timestamp, &stream->base); |
| 415 |
e.data.aud_dev_err.dir = PJMEDIA_DIR_PLAYBACK; |
| 416 |
e.data.aud_dev_err.status = status; |
| 417 |
e.data.aud_dev_err.id = stream->play_id; |
| 418 |
|
| 419 |
pjmedia_event_publish(NULL, &stream->base, &e, |
| 420 |
PJMEDIA_EVENT_PUBLISH_DEFAULT); |
| 421 |
} |
| 422 |
stream->play_thread_exited = 1; |
| 423 |
return paAbort; |
| 424 |
} |
| 425 |
|
| 426 |
|
| 427 |
static int PaRecorderPlayerCallback( const void *input, |
| 428 |
void *output, |
| 429 |
unsigned long frameCount, |
| 430 |
const PaStreamCallbackTimeInfo* timeInfo, |
| 431 |
PaStreamCallbackFlags statusFlags, |
| 432 |
void *userData ) |
| 433 |
{ |
| 434 |
int rc; |
| 435 |
|
| 436 |
rc = PaRecorderCallback(input, output, frameCount, timeInfo, |
| 437 |
statusFlags, userData); |
| 438 |
if (rc != paContinue) |
| 439 |
return rc; |
| 440 |
|
| 441 |
rc = PaPlayerCallback(input, output, frameCount, timeInfo, |
| 442 |
statusFlags, userData); |
| 443 |
return rc; |
| 444 |
} |
| 445 |
|
| 446 |
#ifdef USE_PA_DEBUG_PRINT |
| 447 |
|
| 448 |
static void pa_log_cb(const char *log) |
| 449 |
{ |
| 450 |
PJ_LOG(5,(THIS_FILE, "PA message: %s", log)); |
| 451 |
} |
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
typedef void (*PaUtilLogCallback ) (const char *log); |
| 457 |
void PaUtil_SetDebugPrintFunction(PaUtilLogCallback cb); |
| 458 |
#endif |
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
pjmedia_aud_dev_factory* pjmedia_pa_factory(pj_pool_factory *pf) |
| 465 |
{ |
| 466 |
struct pa_aud_factory *f; |
| 467 |
pj_pool_t *pool; |
| 468 |
|
| 469 |
pool = pj_pool_create(pf, "portaudio", 64, 64, NULL); |
| 470 |
f = PJ_POOL_ZALLOC_T(pool, struct pa_aud_factory); |
| 471 |
f->pf = pf; |
| 472 |
f->pool = pool; |
| 473 |
f->base.op = &pa_op; |
| 474 |
|
| 475 |
return &f->base; |
| 476 |
} |
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
static pj_status_t pa_init(pjmedia_aud_dev_factory *f) |
| 481 |
{ |
| 482 |
int err; |
| 483 |
|
| 484 |
PJ_UNUSED_ARG(f); |
| 485 |
|
| 486 |
#ifdef USE_PA_DEBUG_PRINT |
| 487 |
PaUtil_SetDebugPrintFunction(&pa_log_cb); |
| 488 |
#endif |
| 489 |
|
| 490 |
err = Pa_Initialize(); |
| 491 |
|
| 492 |
PJ_LOG(4,(THIS_FILE, |
| 493 |
"PortAudio sound library initialized, status=%d", err)); |
| 494 |
PJ_LOG(4,(THIS_FILE, "PortAudio host api count=%d", |
| 495 |
Pa_GetHostApiCount())); |
| 496 |
PJ_LOG(4,(THIS_FILE, "Sound device count=%d", |
| 497 |
pa_get_dev_count(f))); |
| 498 |
|
| 499 |
return err ? PJMEDIA_AUDIODEV_ERRNO_FROM_PORTAUDIO(err) : PJ_SUCCESS; |
| 500 |
} |
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
static pj_status_t pa_destroy(pjmedia_aud_dev_factory *f) |
| 505 |
{ |
| 506 |
struct pa_aud_factory *pa = (struct pa_aud_factory*)f; |
| 507 |
pj_pool_t *pool; |
| 508 |
int err; |
| 509 |
|
| 510 |
PJ_LOG(4,(THIS_FILE, "PortAudio sound library shutting down..")); |
| 511 |
|
| 512 |
err = Pa_Terminate(); |
| 513 |
|
| 514 |
pool = pa->pool; |
| 515 |
pa->pool = NULL; |
| 516 |
pj_pool_release(pool); |
| 517 |
|
| 518 |
return err ? PJMEDIA_AUDIODEV_ERRNO_FROM_PORTAUDIO(err) : PJ_SUCCESS; |
| 519 |
} |
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
static pj_status_t pa_refresh(pjmedia_aud_dev_factory *f) |
| 524 |
{ |
| 525 |
PJ_UNUSED_ARG(f); |
| 526 |
return PJ_ENOTSUP; |
| 527 |
} |
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
static unsigned pa_get_dev_count(pjmedia_aud_dev_factory *f) |
| 532 |
{ |
| 533 |
int count = Pa_GetDeviceCount(); |
| 534 |
PJ_UNUSED_ARG(f); |
| 535 |
return count < 0 ? 0 : count; |
| 536 |
} |
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
static pj_status_t pa_get_dev_info(pjmedia_aud_dev_factory *f, |
| 541 |
unsigned index, |
| 542 |
pjmedia_aud_dev_info *info) |
| 543 |
{ |
| 544 |
const PaDeviceInfo *pa_info; |
| 545 |
|
| 546 |
PJ_UNUSED_ARG(f); |
| 547 |
|
| 548 |
pa_info = Pa_GetDeviceInfo(index); |
| 549 |
if (!pa_info) |
| 550 |
return PJMEDIA_EAUD_INVDEV; |
| 551 |
|
| 552 |
pj_bzero(info, sizeof(*info)); |
| 553 |
pj_ansi_strxcpy(info->name, pa_info->name, sizeof(info->name)); |
| 554 |
info->input_count = pa_info->maxInputChannels; |
| 555 |
info->output_count = pa_info->maxOutputChannels; |
| 556 |
info->default_samples_per_sec = (unsigned)pa_info->defaultSampleRate; |
| 557 |
pj_ansi_strxcpy(info->driver, DRIVER_NAME, sizeof(info->driver)); |
| 558 |
info->caps = PJMEDIA_AUD_DEV_CAP_INPUT_LATENCY | |
| 559 |
PJMEDIA_AUD_DEV_CAP_OUTPUT_LATENCY; |
| 560 |
|
| 561 |
return PJ_SUCCESS; |
| 562 |
} |
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
static pj_status_t pa_default_param(pjmedia_aud_dev_factory *f, |
| 567 |
unsigned index, |
| 568 |
pjmedia_aud_param *param) |
| 569 |
{ |
| 570 |
pjmedia_aud_dev_info adi; |
| 571 |
pj_status_t status; |
| 572 |
|
| 573 |
PJ_UNUSED_ARG(f); |
| 574 |
|
| 575 |
status = pa_get_dev_info(f, index, &adi); |
| 576 |
if (status != PJ_SUCCESS) |
| 577 |
return status; |
| 578 |
|
| 579 |
pj_bzero(param, sizeof(*param)); |
| 580 |
if (adi.input_count && adi.output_count) { |
| 581 |
param->dir = PJMEDIA_DIR_CAPTURE_PLAYBACK; |
| 582 |
param->rec_id = index; |
| 583 |
param->play_id = index; |
| 584 |
} else if (adi.input_count) { |
| 585 |
param->dir = PJMEDIA_DIR_CAPTURE; |
| 586 |
param->rec_id = index; |
| 587 |
param->play_id = PJMEDIA_AUD_INVALID_DEV; |
| 588 |
} else if (adi.output_count) { |
| 589 |
param->dir = PJMEDIA_DIR_PLAYBACK; |
| 590 |
param->play_id = index; |
| 591 |
param->rec_id = PJMEDIA_AUD_INVALID_DEV; |
| 592 |
} else { |
| 593 |
return PJMEDIA_EAUD_INVDEV; |
| 594 |
} |
| 595 |
|
| 596 |
param->clock_rate = adi.default_samples_per_sec; |
| 597 |
param->channel_count = 1; |
| 598 |
param->samples_per_frame = adi.default_samples_per_sec * 20 / 1000; |
| 599 |
param->bits_per_sample = 16; |
| 600 |
param->flags = adi.caps; |
| 601 |
param->input_latency_ms = PJMEDIA_SND_DEFAULT_REC_LATENCY; |
| 602 |
param->output_latency_ms = PJMEDIA_SND_DEFAULT_PLAY_LATENCY; |
| 603 |
|
| 604 |
return PJ_SUCCESS; |
| 605 |
} |
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
static int pa_get_default_input_dev(int channel_count) |
| 610 |
{ |
| 611 |
int i, count; |
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
#if PJMEDIA_PREFER_DIRECT_SOUND |
| 617 |
if (Pa_HostApiTypeIdToHostApiIndex(paDirectSound) >= 0) { |
| 618 |
const PaHostApiInfo *pHI; |
| 619 |
int index = Pa_HostApiTypeIdToHostApiIndex(paDirectSound); |
| 620 |
pHI = Pa_GetHostApiInfo(index); |
| 621 |
if (pHI) { |
| 622 |
const PaDeviceInfo *paDevInfo = NULL; |
| 623 |
paDevInfo = Pa_GetDeviceInfo(pHI->defaultInputDevice); |
| 624 |
if (paDevInfo && paDevInfo->maxInputChannels >= channel_count) |
| 625 |
return pHI->defaultInputDevice; |
| 626 |
} |
| 627 |
} |
| 628 |
#endif |
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
count = Pa_GetHostApiCount(); |
| 634 |
for (i=0; i < count; ++i) { |
| 635 |
const PaHostApiInfo *pHAInfo; |
| 636 |
|
| 637 |
pHAInfo = Pa_GetHostApiInfo(i); |
| 638 |
if (!pHAInfo) |
| 639 |
continue; |
| 640 |
|
| 641 |
if (pHAInfo->defaultInputDevice >= 0) { |
| 642 |
const PaDeviceInfo *paDevInfo; |
| 643 |
|
| 644 |
paDevInfo = Pa_GetDeviceInfo(pHAInfo->defaultInputDevice); |
| 645 |
|
| 646 |
if (paDevInfo->maxInputChannels >= channel_count) |
| 647 |
return pHAInfo->defaultInputDevice; |
| 648 |
} |
| 649 |
} |
| 650 |
|
| 651 |
|
| 652 |
count = Pa_GetDeviceCount(); |
| 653 |
for (i=0; i<count; ++i) { |
| 654 |
const PaDeviceInfo *paDevInfo; |
| 655 |
|
| 656 |
paDevInfo = Pa_GetDeviceInfo(i); |
| 657 |
if (paDevInfo->maxInputChannels >= channel_count) |
| 658 |
return i; |
| 659 |
} |
| 660 |
|
| 661 |
return -1; |
| 662 |
} |
| 663 |
|
| 664 |
|
| 665 |
static int pa_get_default_output_dev(int channel_count) |
| 666 |
{ |
| 667 |
int i, count; |
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
#if PJMEDIA_PREFER_DIRECT_SOUND |
| 673 |
if (Pa_HostApiTypeIdToHostApiIndex(paDirectSound) >= 0) { |
| 674 |
const PaHostApiInfo *pHI; |
| 675 |
int index = Pa_HostApiTypeIdToHostApiIndex(paDirectSound); |
| 676 |
pHI = Pa_GetHostApiInfo(index); |
| 677 |
if (pHI) { |
| 678 |
const PaDeviceInfo *paDevInfo = NULL; |
| 679 |
paDevInfo = Pa_GetDeviceInfo(pHI->defaultOutputDevice); |
| 680 |
if (paDevInfo && paDevInfo->maxOutputChannels >= channel_count) |
| 681 |
return pHI->defaultOutputDevice; |
| 682 |
} |
| 683 |
} |
| 684 |
#endif |
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
count = Pa_GetHostApiCount(); |
| 690 |
for (i=0; i < count; ++i) { |
| 691 |
const PaHostApiInfo *pHAInfo; |
| 692 |
|
| 693 |
pHAInfo = Pa_GetHostApiInfo(i); |
| 694 |
if (!pHAInfo) |
| 695 |
continue; |
| 696 |
|
| 697 |
if (pHAInfo->defaultOutputDevice >= 0) { |
| 698 |
const PaDeviceInfo *paDevInfo; |
| 699 |
|
| 700 |
paDevInfo = Pa_GetDeviceInfo(pHAInfo->defaultOutputDevice); |
| 701 |
|
| 702 |
if (paDevInfo->maxOutputChannels >= channel_count) |
| 703 |
return pHAInfo->defaultOutputDevice; |
| 704 |
} |
| 705 |
} |
| 706 |
|
| 707 |
|
| 708 |
count = Pa_GetDeviceCount(); |
| 709 |
for (i=0; i<count; ++i) { |
| 710 |
const PaDeviceInfo *paDevInfo; |
| 711 |
|
| 712 |
paDevInfo = Pa_GetDeviceInfo(i); |
| 713 |
if (paDevInfo->maxOutputChannels >= channel_count) |
| 714 |
return i; |
| 715 |
} |
| 716 |
|
| 717 |
return -1; |
| 718 |
} |
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
static pj_status_t create_rec_stream( struct pa_aud_factory *pa, |
| 723 |
const pjmedia_aud_param *param, |
| 724 |
pjmedia_aud_rec_cb rec_cb, |
| 725 |
void *user_data, |
| 726 |
pjmedia_aud_stream **p_snd_strm) |
| 727 |
{ |
| 728 |
pj_pool_t *pool; |
| 729 |
pjmedia_aud_dev_index rec_id; |
| 730 |
struct pa_aud_stream *stream; |
| 731 |
PaStreamParameters inputParam; |
| 732 |
int sampleFormat; |
| 733 |
const PaDeviceInfo *paDevInfo = NULL; |
| 734 |
const PaHostApiInfo *paHostApiInfo = NULL; |
| 735 |
unsigned paFrames, paRate, paLatency; |
| 736 |
const PaStreamInfo *paSI; |
| 737 |
PaError err; |
| 738 |
|
| 739 |
PJ_ASSERT_RETURN(rec_cb && p_snd_strm, PJ_EINVAL); |
| 740 |
|
| 741 |
rec_id = param->rec_id; |
| 742 |
if (rec_id < 0) { |
| 743 |
rec_id = pa_get_default_input_dev(param->channel_count); |
| 744 |
if (rec_id < 0) { |
| 745 |
|
| 746 |
return PJMEDIA_EAUD_NODEFDEV; |
| 747 |
} |
| 748 |
} |
| 749 |
|
| 750 |
paDevInfo = Pa_GetDeviceInfo(rec_id); |
| 751 |
if (!paDevInfo) { |
| 752 |
|
| 753 |
return PJMEDIA_EAUD_INVDEV; |
| 754 |
} |
| 755 |
|
| 756 |
if (param->bits_per_sample == 8) |
| 757 |
sampleFormat = paUInt8; |
| 758 |
else if (param->bits_per_sample == 16) |
| 759 |
sampleFormat = paInt16; |
| 760 |
else if (param->bits_per_sample == 32) |
| 761 |
sampleFormat = paInt32; |
| 762 |
else |
| 763 |
return PJMEDIA_EAUD_SAMPFORMAT; |
| 764 |
|
| 765 |
pool = pj_pool_create(pa->pf, "recstrm", 1024, 1024, NULL); |
| 766 |
if (!pool) |
| 767 |
return PJ_ENOMEM; |
| 768 |
|
| 769 |
stream = PJ_POOL_ZALLOC_T(pool, struct pa_aud_stream); |
| 770 |
stream->pool = pool; |
| 771 |
pj_strdup2_with_null(pool, &stream->name, paDevInfo->name); |
| 772 |
stream->dir = PJMEDIA_DIR_CAPTURE; |
| 773 |
stream->rec_id = rec_id; |
| 774 |
stream->play_id = -1; |
| 775 |
stream->user_data = user_data; |
| 776 |
stream->samples_per_sec = param->clock_rate; |
| 777 |
stream->samples_per_frame = param->samples_per_frame; |
| 778 |
stream->bytes_per_sample = param->bits_per_sample / 8; |
| 779 |
stream->channel_count = param->channel_count; |
| 780 |
stream->rec_cb = rec_cb; |
| 781 |
|
| 782 |
stream->rec_buf = (pj_int16_t*)pj_pool_alloc(pool, |
| 783 |
stream->samples_per_frame * stream->bytes_per_sample); |
| 784 |
stream->rec_buf_count = 0; |
| 785 |
|
| 786 |
pj_bzero(&inputParam, sizeof(inputParam)); |
| 787 |
inputParam.device = rec_id; |
| 788 |
inputParam.channelCount = param->channel_count; |
| 789 |
inputParam.hostApiSpecificStreamInfo = NULL; |
| 790 |
inputParam.sampleFormat = sampleFormat; |
| 791 |
if (param->flags & PJMEDIA_AUD_DEV_CAP_INPUT_LATENCY) |
| 792 |
inputParam.suggestedLatency = param->input_latency_ms / 1000.0; |
| 793 |
else |
| 794 |
inputParam.suggestedLatency = PJMEDIA_SND_DEFAULT_REC_LATENCY / 1000.0; |
| 795 |
|
| 796 |
paHostApiInfo = Pa_GetHostApiInfo(paDevInfo->hostApi); |
| 797 |
|
| 798 |
|
| 799 |
paFrames = param->samples_per_frame / param->channel_count; |
| 800 |
|
| 801 |
err = Pa_OpenStream( &stream->rec_strm, &inputParam, NULL, |
| 802 |
param->clock_rate, paFrames, |
| 803 |
paClipOff, &PaRecorderCallback, stream ); |
| 804 |
if (err != paNoError) { |
| 805 |
pj_pool_release(pool); |
| 806 |
return PJMEDIA_AUDIODEV_ERRNO_FROM_PORTAUDIO(err); |
| 807 |
} |
| 808 |
|
| 809 |
paSI = Pa_GetStreamInfo(stream->rec_strm); |
| 810 |
paRate = (unsigned)paSI->sampleRate; |
| 811 |
paLatency = (unsigned)(paSI->inputLatency * 1000); |
| 812 |
|
| 813 |
PJ_LOG(5,(THIS_FILE, "Opened device %s (%s) for recording, sample " |
| 814 |
"rate=%d, ch=%d, " |
| 815 |
"bits=%d, %d samples per frame, latency=%d ms", |
| 816 |
paDevInfo->name, paHostApiInfo->name, |
| 817 |
paRate, param->channel_count, |
| 818 |
param->bits_per_sample, param->samples_per_frame, |
| 819 |
paLatency)); |
| 820 |
|
| 821 |
*p_snd_strm = &stream->base; |
| 822 |
return PJ_SUCCESS; |
| 823 |
} |
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
static pj_status_t create_play_stream(struct pa_aud_factory *pa, |
| 828 |
const pjmedia_aud_param *param, |
| 829 |
pjmedia_aud_play_cb play_cb, |
| 830 |
void *user_data, |
| 831 |
pjmedia_aud_stream **p_snd_strm) |
| 832 |
{ |
| 833 |
pj_pool_t *pool; |
| 834 |
pjmedia_aud_dev_index play_id; |
| 835 |
struct pa_aud_stream *stream; |
| 836 |
PaStreamParameters outputParam; |
| 837 |
int sampleFormat; |
| 838 |
const PaDeviceInfo *paDevInfo = NULL; |
| 839 |
const PaHostApiInfo *paHostApiInfo = NULL; |
| 840 |
const PaStreamInfo *paSI; |
| 841 |
unsigned paFrames, paRate, paLatency; |
| 842 |
PaError err; |
| 843 |
|
| 844 |
PJ_ASSERT_RETURN(play_cb && p_snd_strm, PJ_EINVAL); |
| 845 |
|
| 846 |
play_id = param->play_id; |
| 847 |
if (play_id < 0) { |
| 848 |
play_id = pa_get_default_output_dev(param->channel_count); |
| 849 |
if (play_id < 0) { |
| 850 |
|
| 851 |
return PJMEDIA_EAUD_NODEFDEV; |
| 852 |
} |
| 853 |
} |
| 854 |
|
| 855 |
paDevInfo = Pa_GetDeviceInfo(play_id); |
| 856 |
if (!paDevInfo) { |
| 857 |
|
| 858 |
return PJMEDIA_EAUD_INVDEV; |
| 859 |
} |
| 860 |
|
| 861 |
if (param->bits_per_sample == 8) |
| 862 |
sampleFormat = paUInt8; |
| 863 |
else if (param->bits_per_sample == 16) |
| 864 |
sampleFormat = paInt16; |
| 865 |
else if (param->bits_per_sample == 32) |
| 866 |
sampleFormat = paInt32; |
| 867 |
else |
| 868 |
return PJMEDIA_EAUD_SAMPFORMAT; |
| 869 |
|
| 870 |
pool = pj_pool_create(pa->pf, "playstrm", 1024, 1024, NULL); |
| 871 |
if (!pool) |
| 872 |
return PJ_ENOMEM; |
| 873 |
|
| 874 |
stream = PJ_POOL_ZALLOC_T(pool, struct pa_aud_stream); |
| 875 |
stream->pool = pool; |
| 876 |
pj_strdup2_with_null(pool, &stream->name, paDevInfo->name); |
| 877 |
stream->dir = PJMEDIA_DIR_PLAYBACK; |
| 878 |
stream->play_id = play_id; |
| 879 |
stream->rec_id = -1; |
| 880 |
stream->user_data = user_data; |
| 881 |
stream->samples_per_sec = param->clock_rate; |
| 882 |
stream->samples_per_frame = param->samples_per_frame; |
| 883 |
stream->bytes_per_sample = param->bits_per_sample / 8; |
| 884 |
stream->channel_count = param->channel_count; |
| 885 |
stream->play_cb = play_cb; |
| 886 |
|
| 887 |
stream->play_buf = (pj_int16_t*)pj_pool_alloc(pool, |
| 888 |
stream->samples_per_frame * |
| 889 |
stream->bytes_per_sample); |
| 890 |
stream->play_buf_count = 0; |
| 891 |
|
| 892 |
pj_bzero(&outputParam, sizeof(outputParam)); |
| 893 |
outputParam.device = play_id; |
| 894 |
outputParam.channelCount = param->channel_count; |
| 895 |
outputParam.hostApiSpecificStreamInfo = NULL; |
| 896 |
outputParam.sampleFormat = sampleFormat; |
| 897 |
if (param->flags & PJMEDIA_AUD_DEV_CAP_OUTPUT_LATENCY) |
| 898 |
outputParam.suggestedLatency=param->output_latency_ms / 1000.0; |
| 899 |
else |
| 900 |
outputParam.suggestedLatency=PJMEDIA_SND_DEFAULT_PLAY_LATENCY/1000.0; |
| 901 |
|
| 902 |
paHostApiInfo = Pa_GetHostApiInfo(paDevInfo->hostApi); |
| 903 |
|
| 904 |
|
| 905 |
paFrames = param->samples_per_frame / param->channel_count; |
| 906 |
|
| 907 |
err = Pa_OpenStream( &stream->play_strm, NULL, &outputParam, |
| 908 |
param->clock_rate, paFrames, |
| 909 |
paClipOff, &PaPlayerCallback, stream ); |
| 910 |
if (err != paNoError) { |
| 911 |
pj_pool_release(pool); |
| 912 |
return PJMEDIA_AUDIODEV_ERRNO_FROM_PORTAUDIO(err); |
| 913 |
} |
| 914 |
|
| 915 |
paSI = Pa_GetStreamInfo(stream->play_strm); |
| 916 |
paRate = (unsigned)(paSI->sampleRate); |
| 917 |
paLatency = (unsigned)(paSI->outputLatency * 1000); |
| 918 |
|
| 919 |
PJ_LOG(5,(THIS_FILE, "Opened device %d: %s(%s) for playing, sample rate=%d" |
| 920 |
", ch=%d, " |
| 921 |
"bits=%d, %d samples per frame, latency=%d ms", |
| 922 |
play_id, paDevInfo->name, paHostApiInfo->name, |
| 923 |
paRate, param->channel_count, |
| 924 |
param->bits_per_sample, param->samples_per_frame, |
| 925 |
paLatency)); |
| 926 |
|
| 927 |
*p_snd_strm = &stream->base; |
| 928 |
|
| 929 |
return PJ_SUCCESS; |
| 930 |
} |
| 931 |
|
| 932 |
|
| 933 |
|
| 934 |
static pj_status_t create_bidir_stream(struct pa_aud_factory *pa, |
| 935 |
const pjmedia_aud_param *param, |
| 936 |
pjmedia_aud_rec_cb rec_cb, |
| 937 |
pjmedia_aud_play_cb play_cb, |
| 938 |
void *user_data, |
| 939 |
pjmedia_aud_stream **p_snd_strm) |
| 940 |
{ |
| 941 |
pj_pool_t *pool; |
| 942 |
pjmedia_aud_dev_index rec_id, play_id; |
| 943 |
struct pa_aud_stream *stream; |
| 944 |
PaStream *paStream = NULL; |
| 945 |
PaStreamParameters inputParam; |
| 946 |
PaStreamParameters outputParam; |
| 947 |
int sampleFormat; |
| 948 |
const PaDeviceInfo *paRecDevInfo = NULL; |
| 949 |
const PaDeviceInfo *paPlayDevInfo = NULL; |
| 950 |
const PaHostApiInfo *paRecHostApiInfo = NULL; |
| 951 |
const PaHostApiInfo *paPlayHostApiInfo = NULL; |
| 952 |
const PaStreamInfo *paSI; |
| 953 |
unsigned paFrames, paRate, paInputLatency, paOutputLatency; |
| 954 |
PaError err; |
| 955 |
|
| 956 |
PJ_ASSERT_RETURN(play_cb && rec_cb && p_snd_strm, PJ_EINVAL); |
| 957 |
|
| 958 |
rec_id = param->rec_id; |
| 959 |
if (rec_id < 0) { |
| 960 |
rec_id = pa_get_default_input_dev(param->channel_count); |
| 961 |
if (rec_id < 0) { |
| 962 |
|
| 963 |
return PJMEDIA_EAUD_NODEFDEV; |
| 964 |
} |
| 965 |
} |
| 966 |
|
| 967 |
paRecDevInfo = Pa_GetDeviceInfo(rec_id); |
| 968 |
if (!paRecDevInfo) { |
| 969 |
|
| 970 |
return PJMEDIA_EAUD_INVDEV; |
| 971 |
} |
| 972 |
|
| 973 |
play_id = param->play_id; |
| 974 |
if (play_id < 0) { |
| 975 |
play_id = pa_get_default_output_dev(param->channel_count); |
| 976 |
if (play_id < 0) { |
| 977 |
|
| 978 |
return PJMEDIA_EAUD_NODEFDEV; |
| 979 |
} |
| 980 |
} |
| 981 |
|
| 982 |
paPlayDevInfo = Pa_GetDeviceInfo(play_id); |
| 983 |
if (!paPlayDevInfo) { |
| 984 |
|
| 985 |
return PJMEDIA_EAUD_INVDEV; |
| 986 |
} |
| 987 |
|
| 988 |
|
| 989 |
if (param->bits_per_sample == 8) |
| 990 |
sampleFormat = paUInt8; |
| 991 |
else if (param->bits_per_sample == 16) |
| 992 |
sampleFormat = paInt16; |
| 993 |
else if (param->bits_per_sample == 32) |
| 994 |
sampleFormat = paInt32; |
| 995 |
else |
| 996 |
return PJMEDIA_EAUD_SAMPFORMAT; |
| 997 |
|
| 998 |
pool = pj_pool_create(pa->pf, "sndstream", 1024, 1024, NULL); |
| 999 |
if (!pool) |
| 1000 |
return PJ_ENOMEM; |
| 1001 |
|
| 1002 |
stream = PJ_POOL_ZALLOC_T(pool, struct pa_aud_stream); |
| 1003 |
stream->pool = pool; |
| 1004 |
pj_strdup2_with_null(pool, &stream->name, paRecDevInfo->name); |
| 1005 |
stream->dir = PJMEDIA_DIR_CAPTURE_PLAYBACK; |
| 1006 |
stream->play_id = play_id; |
| 1007 |
stream->rec_id = rec_id; |
| 1008 |
stream->user_data = user_data; |
| 1009 |
stream->samples_per_sec = param->clock_rate; |
| 1010 |
stream->samples_per_frame = param->samples_per_frame; |
| 1011 |
stream->bytes_per_sample = param->bits_per_sample / 8; |
| 1012 |
stream->channel_count = param->channel_count; |
| 1013 |
stream->rec_cb = rec_cb; |
| 1014 |
stream->play_cb = play_cb; |
| 1015 |
|
| 1016 |
stream->rec_buf = (pj_int16_t*)pj_pool_alloc(pool, |
| 1017 |
stream->samples_per_frame * stream->bytes_per_sample); |
| 1018 |
stream->rec_buf_count = 0; |
| 1019 |
|
| 1020 |
stream->play_buf = (pj_int16_t*)pj_pool_alloc(pool, |
| 1021 |
stream->samples_per_frame * stream->bytes_per_sample); |
| 1022 |
stream->play_buf_count = 0; |
| 1023 |
|
| 1024 |
pj_bzero(&inputParam, sizeof(inputParam)); |
| 1025 |
inputParam.device = rec_id; |
| 1026 |
inputParam.channelCount = param->channel_count; |
| 1027 |
inputParam.hostApiSpecificStreamInfo = NULL; |
| 1028 |
inputParam.sampleFormat = sampleFormat; |
| 1029 |
if (param->flags & PJMEDIA_AUD_DEV_CAP_INPUT_LATENCY) |
| 1030 |
inputParam.suggestedLatency = param->input_latency_ms / 1000.0; |
| 1031 |
else |
| 1032 |
inputParam.suggestedLatency = PJMEDIA_SND_DEFAULT_REC_LATENCY / 1000.0; |
| 1033 |
|
| 1034 |
paRecHostApiInfo = Pa_GetHostApiInfo(paRecDevInfo->hostApi); |
| 1035 |
|
| 1036 |
pj_bzero(&outputParam, sizeof(outputParam)); |
| 1037 |
outputParam.device = play_id; |
| 1038 |
outputParam.channelCount = param->channel_count; |
| 1039 |
outputParam.hostApiSpecificStreamInfo = NULL; |
| 1040 |
outputParam.sampleFormat = sampleFormat; |
| 1041 |
if (param->flags & PJMEDIA_AUD_DEV_CAP_OUTPUT_LATENCY) |
| 1042 |
outputParam.suggestedLatency=param->output_latency_ms / 1000.0; |
| 1043 |
else |
| 1044 |
outputParam.suggestedLatency=PJMEDIA_SND_DEFAULT_PLAY_LATENCY/1000.0; |
| 1045 |
|
| 1046 |
paPlayHostApiInfo = Pa_GetHostApiInfo(paPlayDevInfo->hostApi); |
| 1047 |
|
| 1048 |
|
| 1049 |
paFrames = param->samples_per_frame / param->channel_count; |
| 1050 |
|
| 1051 |
|
| 1052 |
|
| 1053 |
|
| 1054 |
if (rec_id == play_id) { |
| 1055 |
err = Pa_OpenStream( &paStream, &inputParam, &outputParam, |
| 1056 |
param->clock_rate, paFrames, |
| 1057 |
paClipOff, &PaRecorderPlayerCallback, stream ); |
| 1058 |
if (err == paNoError) { |
| 1059 |
|
| 1060 |
stream->play_strm = stream->rec_strm = paStream; |
| 1061 |
} |
| 1062 |
} else { |
| 1063 |
err = -1; |
| 1064 |
} |
| 1065 |
|
| 1066 |
|
| 1067 |
|
| 1068 |
|
| 1069 |
|
| 1070 |
if (paStream == NULL) { |
| 1071 |
|
| 1072 |
err = Pa_OpenStream( &stream->rec_strm, &inputParam, NULL, |
| 1073 |
param->clock_rate, paFrames, |
| 1074 |
paClipOff, &PaRecorderCallback, stream ); |
| 1075 |
if (err == paNoError) { |
| 1076 |
|
| 1077 |
err = Pa_OpenStream( &stream->play_strm, NULL, &outputParam, |
| 1078 |
param->clock_rate, paFrames, |
| 1079 |
paClipOff, &PaPlayerCallback, stream ); |
| 1080 |
if (err != paNoError) |
| 1081 |
Pa_CloseStream(stream->rec_strm); |
| 1082 |
} |
| 1083 |
} |
| 1084 |
|
| 1085 |
if (err != paNoError) { |
| 1086 |
pj_pool_release(pool); |
| 1087 |
return PJMEDIA_AUDIODEV_ERRNO_FROM_PORTAUDIO(err); |
| 1088 |
} |
| 1089 |
|
| 1090 |
paSI = Pa_GetStreamInfo(stream->rec_strm); |
| 1091 |
paRate = (unsigned)(paSI->sampleRate); |
| 1092 |
paInputLatency = (unsigned)(paSI->inputLatency * 1000); |
| 1093 |
paSI = Pa_GetStreamInfo(stream->play_strm); |
| 1094 |
paOutputLatency = (unsigned)(paSI->outputLatency * 1000); |
| 1095 |
|
| 1096 |
PJ_LOG(5,(THIS_FILE, "Opened device %s(%s)/%s(%s) for recording and " |
| 1097 |
"playback, sample rate=%d, ch=%d, " |
| 1098 |
"bits=%d, %d samples per frame, input latency=%d ms, " |
| 1099 |
"output latency=%d ms", |
| 1100 |
paRecDevInfo->name, paRecHostApiInfo->name, |
| 1101 |
paPlayDevInfo->name, paPlayHostApiInfo->name, |
| 1102 |
paRate, param->channel_count, |
| 1103 |
param->bits_per_sample, param->samples_per_frame, |
| 1104 |
paInputLatency, paOutputLatency)); |
| 1105 |
|
| 1106 |
*p_snd_strm = &stream->base; |
| 1107 |
|
| 1108 |
return PJ_SUCCESS; |
| 1109 |
} |
| 1110 |
|
| 1111 |
|
| 1112 |
|
| 1113 |
static pj_status_t pa_create_stream(pjmedia_aud_dev_factory *f, |
| 1114 |
const pjmedia_aud_param *param, |
| 1115 |
pjmedia_aud_rec_cb rec_cb, |
| 1116 |
pjmedia_aud_play_cb play_cb, |
| 1117 |
void *user_data, |
| 1118 |
pjmedia_aud_stream **p_aud_strm) |
| 1119 |
{ |
| 1120 |
struct pa_aud_factory *pa = (struct pa_aud_factory*)f; |
| 1121 |
pj_status_t status; |
| 1122 |
|
| 1123 |
if (param->dir == PJMEDIA_DIR_CAPTURE) { |
| 1124 |
status = create_rec_stream(pa, param, rec_cb, user_data, p_aud_strm); |
| 1125 |
} else if (param->dir == PJMEDIA_DIR_PLAYBACK) { |
| 1126 |
status = create_play_stream(pa, param, play_cb, user_data, p_aud_strm); |
| 1127 |
} else if (param->dir == PJMEDIA_DIR_CAPTURE_PLAYBACK) { |
| 1128 |
status = create_bidir_stream(pa, param, rec_cb, play_cb, user_data, |
| 1129 |
p_aud_strm); |
| 1130 |
} else { |
| 1131 |
return PJ_EINVAL; |
| 1132 |
} |
| 1133 |
|
| 1134 |
if (status != PJ_SUCCESS) |
| 1135 |
return status; |
| 1136 |
|
| 1137 |
(*p_aud_strm)->op = &pa_strm_op; |
| 1138 |
|
| 1139 |
return PJ_SUCCESS; |
| 1140 |
} |
| 1141 |
|
| 1142 |
|
| 1143 |
|
| 1144 |
static pj_status_t strm_get_param(pjmedia_aud_stream *s, |
| 1145 |
pjmedia_aud_param *pi) |
| 1146 |
{ |
| 1147 |
struct pa_aud_stream *strm = (struct pa_aud_stream*)s; |
| 1148 |
const PaStreamInfo *paPlaySI = NULL, *paRecSI = NULL; |
| 1149 |
|
| 1150 |
PJ_ASSERT_RETURN(strm && pi, PJ_EINVAL); |
| 1151 |
PJ_ASSERT_RETURN(strm->play_strm || strm->rec_strm, PJ_EINVALIDOP); |
| 1152 |
|
| 1153 |
if (strm->play_strm) { |
| 1154 |
paPlaySI = Pa_GetStreamInfo(strm->play_strm); |
| 1155 |
} |
| 1156 |
if (strm->rec_strm) { |
| 1157 |
paRecSI = Pa_GetStreamInfo(strm->rec_strm); |
| 1158 |
} |
| 1159 |
|
| 1160 |
pj_bzero(pi, sizeof(*pi)); |
| 1161 |
pi->dir = strm->dir; |
| 1162 |
pi->play_id = strm->play_id; |
| 1163 |
pi->rec_id = strm->rec_id; |
| 1164 |
pi->clock_rate = (unsigned)(paPlaySI ? paPlaySI->sampleRate : |
| 1165 |
paRecSI->sampleRate); |
| 1166 |
pi->channel_count = strm->channel_count; |
| 1167 |
pi->samples_per_frame = strm->samples_per_frame; |
| 1168 |
pi->bits_per_sample = strm->bytes_per_sample * 8; |
| 1169 |
if (paRecSI) { |
| 1170 |
pi->flags |= PJMEDIA_AUD_DEV_CAP_INPUT_LATENCY; |
| 1171 |
pi->input_latency_ms = (unsigned)(paRecSI ? paRecSI->inputLatency * |
| 1172 |
1000 : 0); |
| 1173 |
} |
| 1174 |
if (paPlaySI) { |
| 1175 |
pi->flags |= PJMEDIA_AUD_DEV_CAP_OUTPUT_LATENCY; |
| 1176 |
pi->output_latency_ms = (unsigned)(paPlaySI? paPlaySI->outputLatency * |
| 1177 |
1000 : 0); |
| 1178 |
} |
| 1179 |
|
| 1180 |
return PJ_SUCCESS; |
| 1181 |
} |
| 1182 |
|
| 1183 |
|
| 1184 |
|
| 1185 |
static pj_status_t strm_get_cap(pjmedia_aud_stream *s, |
| 1186 |
pjmedia_aud_dev_cap cap, |
| 1187 |
void *pval) |
| 1188 |
{ |
| 1189 |
struct pa_aud_stream *strm = (struct pa_aud_stream*)s; |
| 1190 |
|
| 1191 |
PJ_ASSERT_RETURN(strm && pval, PJ_EINVAL); |
| 1192 |
|
| 1193 |
if (cap==PJMEDIA_AUD_DEV_CAP_INPUT_LATENCY && strm->rec_strm) { |
| 1194 |
const PaStreamInfo *si = Pa_GetStreamInfo(strm->rec_strm); |
| 1195 |
if (!si) |
| 1196 |
return PJMEDIA_EAUD_SYSERR; |
| 1197 |
|
| 1198 |
*(unsigned*)pval = (unsigned)(si->inputLatency * 1000); |
| 1199 |
return PJ_SUCCESS; |
| 1200 |
} else if (cap==PJMEDIA_AUD_DEV_CAP_OUTPUT_LATENCY && strm->play_strm) { |
| 1201 |
const PaStreamInfo *si = Pa_GetStreamInfo(strm->play_strm); |
| 1202 |
if (!si) |
| 1203 |
return PJMEDIA_EAUD_SYSERR; |
| 1204 |
|
| 1205 |
*(unsigned*)pval = (unsigned)(si->outputLatency * 1000); |
| 1206 |
return PJ_SUCCESS; |
| 1207 |
} else { |
| 1208 |
return PJMEDIA_EAUD_INVCAP; |
| 1209 |
} |
| 1210 |
} |
| 1211 |
|
| 1212 |
|
| 1213 |
|
| 1214 |
static pj_status_t strm_set_cap(pjmedia_aud_stream *strm, |
| 1215 |
pjmedia_aud_dev_cap cap, |
| 1216 |
const void *value) |
| 1217 |
{ |
| 1218 |
PJ_UNUSED_ARG(strm); |
| 1219 |
PJ_UNUSED_ARG(cap); |
| 1220 |
PJ_UNUSED_ARG(value); |
| 1221 |
|
| 1222 |
|
| 1223 |
return PJMEDIA_EAUD_INVCAP; |
| 1224 |
} |
| 1225 |
|
| 1226 |
|
| 1227 |
|
| 1228 |
static pj_status_t strm_start(pjmedia_aud_stream *s) |
| 1229 |
{ |
| 1230 |
struct pa_aud_stream *stream = (struct pa_aud_stream*)s; |
| 1231 |
int err = 0; |
| 1232 |
|
| 1233 |
PJ_LOG(5,(THIS_FILE, "Starting %s stream..", stream->name.ptr)); |
| 1234 |
|
| 1235 |
if (stream->play_strm) |
| 1236 |
err = Pa_StartStream(stream->play_strm); |
| 1237 |
|
| 1238 |
if (err==0 && stream->rec_strm && stream->rec_strm != stream->play_strm) { |
| 1239 |
err = Pa_StartStream(stream->rec_strm); |
| 1240 |
if (err != 0) |
| 1241 |
Pa_StopStream(stream->play_strm); |
| 1242 |
} |
| 1243 |
|
| 1244 |
PJ_LOG(5,(THIS_FILE, "Done, status=%d", err)); |
| 1245 |
|
| 1246 |
return err ? PJMEDIA_AUDIODEV_ERRNO_FROM_PORTAUDIO(err) : PJ_SUCCESS; |
| 1247 |
} |
| 1248 |
|
| 1249 |
|
| 1250 |
|
| 1251 |
static pj_status_t strm_stop(pjmedia_aud_stream *s) |
| 1252 |
{ |
| 1253 |
struct pa_aud_stream *stream = (struct pa_aud_stream*)s; |
| 1254 |
int i, err = 0; |
| 1255 |
|
| 1256 |
stream->quit_flag = 1; |
| 1257 |
for (i=0; !stream->rec_thread_exited && i<100; ++i) |
| 1258 |
pj_thread_sleep(10); |
| 1259 |
for (i=0; !stream->play_thread_exited && i<100; ++i) |
| 1260 |
pj_thread_sleep(10); |
| 1261 |
|
| 1262 |
pj_thread_sleep(1); |
| 1263 |
|
| 1264 |
PJ_LOG(5,(THIS_FILE, "Stopping stream..")); |
| 1265 |
|
| 1266 |
if (stream->play_strm) |
| 1267 |
err = Pa_StopStream(stream->play_strm); |
| 1268 |
|
| 1269 |
if (stream->rec_strm && stream->rec_strm != stream->play_strm) |
| 1270 |
err = Pa_StopStream(stream->rec_strm); |
| 1271 |
|
| 1272 |
stream->play_thread_initialized = 0; |
| 1273 |
stream->rec_thread_initialized = 0; |
| 1274 |
|
| 1275 |
PJ_LOG(5,(THIS_FILE, "Done, status=%d", err)); |
| 1276 |
|
| 1277 |
return err ? PJMEDIA_AUDIODEV_ERRNO_FROM_PORTAUDIO(err) : PJ_SUCCESS; |
| 1278 |
} |
| 1279 |
|
| 1280 |
|
| 1281 |
|
| 1282 |
static pj_status_t strm_destroy(pjmedia_aud_stream *s) |
| 1283 |
{ |
| 1284 |
struct pa_aud_stream *stream = (struct pa_aud_stream*)s; |
| 1285 |
int i, err = 0; |
| 1286 |
|
| 1287 |
stream->quit_flag = 1; |
| 1288 |
for (i=0; !stream->rec_thread_exited && i<100; ++i) { |
| 1289 |
pj_thread_sleep(1); |
| 1290 |
} |
| 1291 |
for (i=0; !stream->play_thread_exited && i<100; ++i) { |
| 1292 |
pj_thread_sleep(1); |
| 1293 |
} |
| 1294 |
|
| 1295 |
PJ_LOG(5,(THIS_FILE, "Closing %.*s: %lu underflow, %lu overflow", |
| 1296 |
(int)stream->name.slen, |
| 1297 |
stream->name.ptr, |
| 1298 |
stream->underflow, stream->overflow)); |
| 1299 |
|
| 1300 |
if (stream->play_strm) |
| 1301 |
err = Pa_CloseStream(stream->play_strm); |
| 1302 |
|
| 1303 |
if (stream->rec_strm && stream->rec_strm != stream->play_strm) |
| 1304 |
err = Pa_CloseStream(stream->rec_strm); |
| 1305 |
|
| 1306 |
pj_pool_release(stream->pool); |
| 1307 |
|
| 1308 |
return err ? PJMEDIA_AUDIODEV_ERRNO_FROM_PORTAUDIO(err) : PJ_SUCCESS; |
| 1309 |
} |
| 1310 |
|
| 1311 |
#endif |
| 1312 |
|