| 1 |
/* |
| 2 |
* Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com) |
| 3 |
* |
| 4 |
* This program is free software; you can redistribute it and/or modify |
| 5 |
* it under the terms of the GNU General Public License as published by |
| 6 |
* the Free Software Foundation; either version 2 of the License, or |
| 7 |
* (at your option) any later version. |
| 8 |
* |
| 9 |
* This program is distributed in the hope that it will be useful, |
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
* GNU General Public License for more details. |
| 13 |
* |
| 14 |
* You should have received a copy of the GNU General Public License |
| 15 |
* along with this program; if not, write to the Free Software |
| 16 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 |
*/ |
| 18 |
#include <pjmedia-audiodev/audiodev_imp.h> |
| 19 |
#include <pj/assert.h> |
| 20 |
#include <pj/log.h> |
| 21 |
#include <pj/os.h> |
| 22 |
#include <pjmedia/event.h> |
| 23 |
|
| 24 |
#if PJMEDIA_AUDIO_DEV_HAS_COREAUDIO |
| 25 |
|
| 26 |
#include "TargetConditionals.h" |
| 27 |
#if TARGET_OS_IPHONE |
| 28 |
#define COREAUDIO_MAC 0 |
| 29 |
#else |
| 30 |
#define COREAUDIO_MAC 1 |
| 31 |
#endif |
| 32 |
|
| 33 |
#if (TARGET_OS_OSX && defined(__MAC_12_0)) |
| 34 |
#define AUDIO_OBJECT_ELEMENT_MAIN kAudioObjectPropertyElementMain; |
| 35 |
#else |
| 36 |
#define AUDIO_OBJECT_ELEMENT_MAIN kAudioObjectPropertyElementMaster; |
| 37 |
#endif |
| 38 |
|
| 39 |
#include <AudioUnit/AudioUnit.h> |
| 40 |
#include <AudioToolbox/AudioConverter.h> |
| 41 |
#if COREAUDIO_MAC |
| 42 |
#include <CoreAudio/CoreAudio.h> |
| 43 |
#else |
| 44 |
#include <AVFoundation/AVAudioSession.h> |
| 45 |
|
| 46 |
#define AudioDeviceID unsigned |
| 47 |
|
| 48 |
/** |
| 49 |
* As in iOS SDK 4 or later, audio route change property listener is |
| 50 |
* no longer necessary. Just make surethat your application can receive |
| 51 |
* remote control events by adding the code: |
| 52 |
* [[UIApplication sharedApplication] |
| 53 |
* beginReceivingRemoteControlEvents]; |
| 54 |
* Otherwise audio route change (such as headset plug/unplug) will not be |
| 55 |
* processed while your application is in the background mode. |
| 56 |
*/ |
| 57 |
#define USE_AUDIO_ROUTE_CHANGE_PROP_LISTENER 0 |
| 58 |
|
| 59 |
/* Starting iOS SDK 7, Audio Session API is deprecated. */ |
| 60 |
#define USE_AUDIO_SESSION_API 0 |
| 61 |
|
| 62 |
/* For better integration with CallKit features (available starting |
| 63 |
* in iOS 10), let the application setup and manage its own |
| 64 |
* audio session. |
| 65 |
*/ |
| 66 |
#define SETUP_AV_AUDIO_SESSION 0 |
| 67 |
#endif |
| 68 |
|
| 69 |
/* For Mac OS 10.5.x and earlier */ |
| 70 |
#if AUDIO_UNIT_VERSION < 1060 |
| 71 |
#define AudioComponent Component |
| 72 |
#define AudioComponentDescription ComponentDescription |
| 73 |
#define AudioComponentInstance ComponentInstance |
| 74 |
#define AudioComponentFindNext FindNextComponent |
| 75 |
#define AudioComponentInstanceNew OpenAComponent |
| 76 |
#define AudioComponentInstanceDispose CloseComponent |
| 77 |
#endif |
| 78 |
|
| 79 |
|
| 80 |
#define THIS_FILE "coreaudio_dev.c" |
| 81 |
|
| 82 |
/* coreaudio device info */ |
| 83 |
struct coreaudio_dev_info |
| 84 |
{ |
| 85 |
pjmedia_aud_dev_info info; |
| 86 |
AudioDeviceID dev_id; |
| 87 |
}; |
| 88 |
|
| 89 |
/* linked list of streams */ |
| 90 |
struct stream_list |
| 91 |
{ |
| 92 |
PJ_DECL_LIST_MEMBER(struct stream_list); |
| 93 |
struct coreaudio_stream *stream; |
| 94 |
}; |
| 95 |
|
| 96 |
/* coreaudio factory */ |
| 97 |
struct coreaudio_factory |
| 98 |
{ |
| 99 |
pjmedia_aud_dev_factory base; |
| 100 |
pj_pool_t *base_pool; |
| 101 |
pj_pool_t *pool; |
| 102 |
pj_pool_factory *pf; |
| 103 |
pj_mutex_t *mutex; |
| 104 |
|
| 105 |
unsigned dev_count; |
| 106 |
struct coreaudio_dev_info *dev_info; |
| 107 |
|
| 108 |
AudioComponent io_comp; |
| 109 |
pj_bool_t has_vpio; |
| 110 |
struct stream_list streams; |
| 111 |
}; |
| 112 |
|
| 113 |
/* Sound stream. */ |
| 114 |
struct coreaudio_stream |
| 115 |
{ |
| 116 |
pjmedia_aud_stream base; /**< Base stream */ |
| 117 |
pjmedia_aud_param param; /**< Settings */ |
| 118 |
pj_pool_t *pool; /**< Memory pool. */ |
| 119 |
struct coreaudio_factory *cf; |
| 120 |
struct stream_list list_entry; |
| 121 |
|
| 122 |
pjmedia_aud_rec_cb rec_cb; /**< Capture callback. */ |
| 123 |
pjmedia_aud_play_cb play_cb; /**< Playback callback. */ |
| 124 |
void *user_data; /**< Application data. */ |
| 125 |
|
| 126 |
pj_timestamp play_timestamp; |
| 127 |
pj_timestamp rec_timestamp; |
| 128 |
|
| 129 |
pj_int16_t *rec_buf; |
| 130 |
unsigned rec_buf_count; |
| 131 |
pj_int16_t *play_buf; |
| 132 |
unsigned play_buf_count; |
| 133 |
|
| 134 |
pj_bool_t interrupted; |
| 135 |
pj_bool_t quit_flag; |
| 136 |
pj_bool_t running; |
| 137 |
|
| 138 |
pj_bool_t rec_thread_initialized; |
| 139 |
pj_thread_desc rec_thread_desc; |
| 140 |
pj_thread_t *rec_thread; |
| 141 |
|
| 142 |
pj_bool_t play_thread_initialized; |
| 143 |
pj_thread_desc play_thread_desc; |
| 144 |
pj_thread_t *play_thread; |
| 145 |
|
| 146 |
AudioUnit io_units[2]; |
| 147 |
AudioStreamBasicDescription streamFormat; |
| 148 |
AudioBufferList *audio_buf; |
| 149 |
|
| 150 |
AudioConverterRef resample; |
| 151 |
pj_int16_t *resample_buf; |
| 152 |
void *resample_buf_ptr; |
| 153 |
unsigned resample_buf_count; |
| 154 |
unsigned resample_buf_size; |
| 155 |
|
| 156 |
#if !COREAUDIO_MAC |
| 157 |
AVAudioSession *sess; |
| 158 |
#endif |
| 159 |
}; |
| 160 |
|
| 161 |
/* Static variable */ |
| 162 |
static struct coreaudio_factory *cf_instance = NULL; |
| 163 |
|
| 164 |
/* Prototypes */ |
| 165 |
static pj_status_t ca_factory_init(pjmedia_aud_dev_factory *f); |
| 166 |
static pj_status_t ca_factory_destroy(pjmedia_aud_dev_factory *f); |
| 167 |
static pj_status_t ca_factory_refresh(pjmedia_aud_dev_factory *f); |
| 168 |
static unsigned ca_factory_get_dev_count(pjmedia_aud_dev_factory *f); |
| 169 |
static pj_status_t ca_factory_get_dev_info(pjmedia_aud_dev_factory *f, |
| 170 |
unsigned index, |
| 171 |
pjmedia_aud_dev_info *info); |
| 172 |
static pj_status_t ca_factory_default_param(pjmedia_aud_dev_factory *f, |
| 173 |
unsigned index, |
| 174 |
pjmedia_aud_param *param); |
| 175 |
static pj_status_t ca_factory_create_stream(pjmedia_aud_dev_factory *f, |
| 176 |
const pjmedia_aud_param *param, |
| 177 |
pjmedia_aud_rec_cb rec_cb, |
| 178 |
pjmedia_aud_play_cb play_cb, |
| 179 |
void *user_data, |
| 180 |
pjmedia_aud_stream **p_aud_strm); |
| 181 |
|
| 182 |
static pj_status_t ca_stream_get_param(pjmedia_aud_stream *strm, |
| 183 |
pjmedia_aud_param *param); |
| 184 |
static pj_status_t ca_stream_get_cap(pjmedia_aud_stream *strm, |
| 185 |
pjmedia_aud_dev_cap cap, |
| 186 |
void *value); |
| 187 |
static pj_status_t ca_stream_set_cap(pjmedia_aud_stream *strm, |
| 188 |
pjmedia_aud_dev_cap cap, |
| 189 |
const void *value); |
| 190 |
static pj_status_t ca_stream_start(pjmedia_aud_stream *strm); |
| 191 |
static pj_status_t ca_stream_stop(pjmedia_aud_stream *strm); |
| 192 |
static pj_status_t ca_stream_destroy(pjmedia_aud_stream *strm); |
| 193 |
static pj_status_t create_audio_unit(AudioComponent io_comp, |
| 194 |
AudioDeviceID dev_id, |
| 195 |
pjmedia_dir dir, |
| 196 |
struct coreaudio_stream *strm, |
| 197 |
AudioUnit *io_unit); |
| 198 |
#if !COREAUDIO_MAC && USE_AUDIO_SESSION_API != 0 |
| 199 |
static void interruptionListener(void *inClientData, UInt32 inInterruption); |
| 200 |
static void propListener(void * inClientData, |
| 201 |
AudioSessionPropertyID inID, |
| 202 |
UInt32 inDataSize, |
| 203 |
const void * inData); |
| 204 |
#endif |
| 205 |
|
| 206 |
/* Operations */ |
| 207 |
static pjmedia_aud_dev_factory_op factory_op = |
| 208 |
{ |
| 209 |
&ca_factory_init, |
| 210 |
&ca_factory_destroy, |
| 211 |
&ca_factory_get_dev_count, |
| 212 |
&ca_factory_get_dev_info, |
| 213 |
&ca_factory_default_param, |
| 214 |
&ca_factory_create_stream, |
| 215 |
&ca_factory_refresh |
| 216 |
}; |
| 217 |
|
| 218 |
static pjmedia_aud_stream_op stream_op = |
| 219 |
{ |
| 220 |
&ca_stream_get_param, |
| 221 |
&ca_stream_get_cap, |
| 222 |
&ca_stream_set_cap, |
| 223 |
&ca_stream_start, |
| 224 |
&ca_stream_stop, |
| 225 |
&ca_stream_destroy |
| 226 |
}; |
| 227 |
|
| 228 |
|
| 229 |
/**************************************************************************** |
| 230 |
* Factory operations |
| 231 |
*/ |
| 232 |
/* |
| 233 |
* Init coreaudio audio driver. |
| 234 |
*/ |
| 235 |
pjmedia_aud_dev_factory* pjmedia_coreaudio_factory(pj_pool_factory *pf) |
| 236 |
{ |
| 237 |
struct coreaudio_factory *f; |
| 238 |
pj_pool_t *pool; |
| 239 |
|
| 240 |
pool = pj_pool_create(pf, "core audio base", 1000, 1000, NULL); |
| 241 |
f = PJ_POOL_ZALLOC_T(pool, struct coreaudio_factory); |
| 242 |
f->pf = pf; |
| 243 |
f->base_pool = pool; |
| 244 |
f->base.op = &factory_op; |
| 245 |
|
| 246 |
return &f->base; |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
/* API: init factory */ |
| 251 |
static pj_status_t ca_factory_init(pjmedia_aud_dev_factory *f) |
| 252 |
{ |
| 253 |
struct coreaudio_factory *cf = (struct coreaudio_factory*)f; |
| 254 |
AudioComponentDescription desc; |
| 255 |
pj_status_t status; |
| 256 |
#if !COREAUDIO_MAC |
| 257 |
unsigned i; |
| 258 |
#endif |
| 259 |
|
| 260 |
pj_list_init(&cf->streams); |
| 261 |
status = pj_mutex_create_recursive(cf->base_pool, |
| 262 |
"coreaudio", |
| 263 |
&cf->mutex); |
| 264 |
if (status != PJ_SUCCESS) |
| 265 |
return status; |
| 266 |
|
| 267 |
desc.componentType = kAudioUnitType_Output; |
| 268 |
#if COREAUDIO_MAC |
| 269 |
desc.componentSubType = kAudioUnitSubType_HALOutput; |
| 270 |
#else |
| 271 |
desc.componentSubType = kAudioUnitSubType_RemoteIO; |
| 272 |
#endif |
| 273 |
desc.componentManufacturer = kAudioUnitManufacturer_Apple; |
| 274 |
desc.componentFlags = 0; |
| 275 |
desc.componentFlagsMask = 0; |
| 276 |
|
| 277 |
cf->io_comp = AudioComponentFindNext(NULL, &desc); |
| 278 |
if (cf->io_comp == NULL) |
| 279 |
return PJMEDIA_EAUD_INIT; // cannot find IO unit; |
| 280 |
|
| 281 |
desc.componentSubType = kAudioUnitSubType_VoiceProcessingIO; |
| 282 |
if (AudioComponentFindNext(NULL, &desc) != NULL) |
| 283 |
cf->has_vpio = PJ_TRUE; |
| 284 |
|
| 285 |
status = ca_factory_refresh(f); |
| 286 |
if (status != PJ_SUCCESS) |
| 287 |
return status; |
| 288 |
|
| 289 |
#if !COREAUDIO_MAC |
| 290 |
cf->pool = pj_pool_create(cf->pf, "core audio", 1000, 1000, NULL); |
| 291 |
cf->dev_count = 1; |
| 292 |
cf->dev_info = (struct coreaudio_dev_info*) |
| 293 |
pj_pool_calloc(cf->pool, cf->dev_count, |
| 294 |
sizeof(struct coreaudio_dev_info)); |
| 295 |
for (i = 0; i < cf->dev_count; i++) { |
| 296 |
struct coreaudio_dev_info *cdi; |
| 297 |
|
| 298 |
cdi = &cf->dev_info[i]; |
| 299 |
pj_bzero(cdi, sizeof(*cdi)); |
| 300 |
cdi->dev_id = 0; |
| 301 |
pj_ansi_strxcpy(cdi->info.name, "iPhone IO device", |
| 302 |
sizeof(cdi->info.name)); |
| 303 |
pj_ansi_strxcpy(cdi->info.driver, "apple", sizeof(cdi->info.driver)); |
| 304 |
cdi->info.input_count = 1; |
| 305 |
cdi->info.output_count = 1; |
| 306 |
cdi->info.default_samples_per_sec = 8000; |
| 307 |
|
| 308 |
/* Set the device capabilities here */ |
| 309 |
cdi->info.caps = PJMEDIA_AUD_DEV_CAP_INPUT_LATENCY | |
| 310 |
PJMEDIA_AUD_DEV_CAP_OUTPUT_LATENCY | |
| 311 |
PJMEDIA_AUD_DEV_CAP_OUTPUT_VOLUME_SETTING | |
| 312 |
#if USE_AUDIO_SESSION_API != 0 |
| 313 |
PJMEDIA_AUD_DEV_CAP_INPUT_ROUTE | |
| 314 |
PJMEDIA_AUD_DEV_CAP_OUTPUT_ROUTE | |
| 315 |
#endif |
| 316 |
PJMEDIA_AUD_DEV_CAP_EC; |
| 317 |
cdi->info.routes = PJMEDIA_AUD_DEV_ROUTE_LOUDSPEAKER | |
| 318 |
PJMEDIA_AUD_DEV_ROUTE_EARPIECE | |
| 319 |
PJMEDIA_AUD_DEV_ROUTE_BLUETOOTH; |
| 320 |
|
| 321 |
PJ_LOG(4, (THIS_FILE, " dev_id %d: %s (in=%d, out=%d) %dHz", |
| 322 |
i, |
| 323 |
cdi->info.name, |
| 324 |
cdi->info.input_count, |
| 325 |
cdi->info.output_count, |
| 326 |
cdi->info.default_samples_per_sec)); |
| 327 |
} |
| 328 |
|
| 329 |
#if USE_AUDIO_SESSION_API != 0 |
| 330 |
{ |
| 331 |
OSStatus ostatus; |
| 332 |
|
| 333 |
/* Initialize the Audio Session */ |
| 334 |
ostatus = AudioSessionInitialize(NULL, NULL, interruptionListener, |
| 335 |
NULL); |
| 336 |
if (ostatus != kAudioSessionNoError) { |
| 337 |
PJ_LOG(4, (THIS_FILE, |
| 338 |
"Warning: cannot initialize audio session services (%i)", |
| 339 |
ostatus)); |
| 340 |
} |
| 341 |
|
| 342 |
/* Listen for audio routing change notifications. */ |
| 343 |
#if USE_AUDIO_ROUTE_CHANGE_PROP_LISTENER != 0 |
| 344 |
ostatus = AudioSessionAddPropertyListener( |
| 345 |
kAudioSessionProperty_AudioRouteChange, |
| 346 |
propListener, cf); |
| 347 |
if (ostatus != kAudioSessionNoError) { |
| 348 |
PJ_LOG(4, (THIS_FILE, |
| 349 |
"Warning: cannot listen for audio route change " |
| 350 |
"notifications (%i)", ostatus)); |
| 351 |
} |
| 352 |
#endif |
| 353 |
} |
| 354 |
#endif |
| 355 |
|
| 356 |
#if SETUP_AV_AUDIO_SESSION |
| 357 |
/* Initialize audio session category and mode */ |
| 358 |
{ |
| 359 |
AVAudioSession *sess = [AVAudioSession sharedInstance]; |
| 360 |
pj_bool_t err; |
| 361 |
|
| 362 |
if ([sess respondsToSelector:@selector(setCategory:withOptions:error:)]) |
| 363 |
{ |
| 364 |
err = [sess setCategory:AVAudioSessionCategoryPlayAndRecord |
| 365 |
withOptions:AVAudioSessionCategoryOptionAllowBluetooth |
| 366 |
error:nil] != YES; |
| 367 |
} else { |
| 368 |
err = [sess setCategory:AVAudioSessionCategoryPlayAndRecord |
| 369 |
error:nil] != YES; |
| 370 |
} |
| 371 |
if (err) { |
| 372 |
PJ_LOG(3, (THIS_FILE, |
| 373 |
"Warning: failed settting audio session category")); |
| 374 |
} |
| 375 |
|
| 376 |
if ([sess respondsToSelector:@selector(setMode:error:)] && |
| 377 |
[sess setMode:AVAudioSessionModeVoiceChat error:nil] != YES) |
| 378 |
{ |
| 379 |
PJ_LOG(3, (THIS_FILE, "Warning: failed settting audio mode")); |
| 380 |
} |
| 381 |
} |
| 382 |
#endif |
| 383 |
|
| 384 |
cf_instance = cf; |
| 385 |
#endif |
| 386 |
|
| 387 |
PJ_LOG(4, (THIS_FILE, "core audio initialized")); |
| 388 |
|
| 389 |
return PJ_SUCCESS; |
| 390 |
} |
| 391 |
|
| 392 |
/* API: destroy factory */ |
| 393 |
static pj_status_t ca_factory_destroy(pjmedia_aud_dev_factory *f) |
| 394 |
{ |
| 395 |
struct coreaudio_factory *cf = (struct coreaudio_factory*)f; |
| 396 |
pj_pool_t *pool; |
| 397 |
|
| 398 |
pj_assert(cf); |
| 399 |
pj_assert(cf->base_pool); |
| 400 |
pj_assert(pj_list_empty(&cf->streams)); |
| 401 |
|
| 402 |
#if !COREAUDIO_MAC |
| 403 |
#if USE_AUDIO_SESSION_API != 0 && USE_AUDIO_ROUTE_CHANGE_PROP_LISTENER != 0 |
| 404 |
AudioSessionRemovePropertyListenerWithUserData( |
| 405 |
kAudioSessionProperty_AudioRouteChange, propListener, cf); |
| 406 |
#endif |
| 407 |
#endif |
| 408 |
|
| 409 |
if (cf->pool) { |
| 410 |
pj_pool_release(cf->pool); |
| 411 |
cf->pool = NULL; |
| 412 |
} |
| 413 |
|
| 414 |
if (cf->mutex) { |
| 415 |
pj_mutex_lock(cf->mutex); |
| 416 |
cf_instance = NULL; |
| 417 |
pj_mutex_unlock(cf->mutex); |
| 418 |
pj_mutex_destroy(cf->mutex); |
| 419 |
cf->mutex = NULL; |
| 420 |
} |
| 421 |
|
| 422 |
pool = cf->base_pool; |
| 423 |
cf->base_pool = NULL; |
| 424 |
pj_pool_release(pool); |
| 425 |
|
| 426 |
return PJ_SUCCESS; |
| 427 |
} |
| 428 |
|
| 429 |
/* API: refresh the device list */ |
| 430 |
static pj_status_t ca_factory_refresh(pjmedia_aud_dev_factory *f) |
| 431 |
{ |
| 432 |
#if !COREAUDIO_MAC |
| 433 |
/* iPhone doesn't support refreshing the device list */ |
| 434 |
PJ_UNUSED_ARG(f); |
| 435 |
return PJ_SUCCESS; |
| 436 |
#else |
| 437 |
struct coreaudio_factory *cf = (struct coreaudio_factory*)f; |
| 438 |
unsigned i; |
| 439 |
unsigned dev_count; |
| 440 |
AudioObjectPropertyAddress addr; |
| 441 |
AudioDeviceID *dev_ids; |
| 442 |
UInt32 buf_size, dev_size, size = sizeof(AudioDeviceID); |
| 443 |
AudioBufferList *buf = NULL; |
| 444 |
OSStatus ostatus; |
| 445 |
|
| 446 |
if (cf->pool != NULL) { |
| 447 |
pj_pool_release(cf->pool); |
| 448 |
cf->pool = NULL; |
| 449 |
} |
| 450 |
|
| 451 |
cf->dev_count = 0; |
| 452 |
cf->pool = pj_pool_create(cf->pf, "core audio", 1000, 1000, NULL); |
| 453 |
|
| 454 |
/* Find out how many audio devices there are */ |
| 455 |
addr.mSelector = kAudioHardwarePropertyDevices; |
| 456 |
addr.mScope = kAudioObjectPropertyScopeGlobal; |
| 457 |
addr.mElement = AUDIO_OBJECT_ELEMENT_MAIN; |
| 458 |
ostatus = AudioObjectGetPropertyDataSize(kAudioObjectSystemObject, &addr, |
| 459 |
0, NULL, &dev_size); |
| 460 |
if (ostatus != noErr) { |
| 461 |
dev_size = 0; |
| 462 |
} |
| 463 |
|
| 464 |
/* Calculate the number of audio devices available */ |
| 465 |
dev_count = dev_size / size; |
| 466 |
if (dev_count==0) { |
| 467 |
PJ_LOG(4,(THIS_FILE, "core audio found no sound devices")); |
| 468 |
/* Enabling this will cause pjsua-lib initialization to fail when |
| 469 |
* there is no sound device installed in the system, even when pjsua |
| 470 |
* has been run with --null-audio. Moreover, it might be better to |
| 471 |
* think that the core audio backend initialization is successful, |
| 472 |
* regardless there is no audio device installed, as later application |
| 473 |
* can check it using get_dev_count(). |
| 474 |
return PJMEDIA_EAUD_NODEV; |
| 475 |
*/ |
| 476 |
return PJ_SUCCESS; |
| 477 |
} |
| 478 |
PJ_LOG(4, (THIS_FILE, "core audio detected %d devices", |
| 479 |
dev_count)); |
| 480 |
|
| 481 |
/* Get all the audio device IDs */ |
| 482 |
dev_ids = (AudioDeviceID *)pj_pool_calloc(cf->pool, dev_count, size); |
| 483 |
if (!dev_ids) |
| 484 |
return PJ_ENOMEM; |
| 485 |
ostatus = AudioObjectGetPropertyData(kAudioObjectSystemObject, &addr, |
| 486 |
0, NULL, |
| 487 |
&dev_size, (void *)dev_ids); |
| 488 |
if (ostatus != noErr ) { |
| 489 |
/* This should not happen since we have successfully retrieved |
| 490 |
* the property data size before |
| 491 |
*/ |
| 492 |
return PJMEDIA_EAUD_INIT; |
| 493 |
} |
| 494 |
|
| 495 |
if (dev_size > 1) { |
| 496 |
AudioDeviceID dev_id = kAudioObjectUnknown; |
| 497 |
unsigned idx = 0; |
| 498 |
|
| 499 |
/* Find default audio input device */ |
| 500 |
addr.mSelector = kAudioHardwarePropertyDefaultInputDevice; |
| 501 |
addr.mScope = kAudioObjectPropertyScopeGlobal; |
| 502 |
addr.mElement = AUDIO_OBJECT_ELEMENT_MAIN; |
| 503 |
size = sizeof(dev_id); |
| 504 |
|
| 505 |
ostatus = AudioObjectGetPropertyData(kAudioObjectSystemObject, |
| 506 |
&addr, 0, NULL, |
| 507 |
&size, (void *)&dev_id); |
| 508 |
if (ostatus == noErr && dev_id != dev_ids[idx]) { |
| 509 |
AudioDeviceID temp_id = dev_ids[idx]; |
| 510 |
|
| 511 |
for (i = idx + 1; i < dev_count; i++) { |
| 512 |
if (dev_ids[i] == dev_id) { |
| 513 |
dev_ids[idx++] = dev_id; |
| 514 |
dev_ids[i] = temp_id; |
| 515 |
break; |
| 516 |
} |
| 517 |
} |
| 518 |
} |
| 519 |
|
| 520 |
/* Find default audio output device */ |
| 521 |
addr.mSelector = kAudioHardwarePropertyDefaultOutputDevice; |
| 522 |
ostatus = AudioObjectGetPropertyData(kAudioObjectSystemObject, |
| 523 |
&addr, 0, NULL, |
| 524 |
&size, (void *)&dev_id); |
| 525 |
if (ostatus == noErr && dev_id != dev_ids[idx]) { |
| 526 |
AudioDeviceID temp_id = dev_ids[idx]; |
| 527 |
|
| 528 |
for (i = idx + 1; i < dev_count; i++) { |
| 529 |
if (dev_ids[i] == dev_id) { |
| 530 |
dev_ids[idx] = dev_id; |
| 531 |
dev_ids[i] = temp_id; |
| 532 |
break; |
| 533 |
} |
| 534 |
} |
| 535 |
} |
| 536 |
} |
| 537 |
|
| 538 |
/* Build the devices' info */ |
| 539 |
cf->dev_info = (struct coreaudio_dev_info*) |
| 540 |
pj_pool_calloc(cf->pool, dev_count, |
| 541 |
sizeof(struct coreaudio_dev_info)); |
| 542 |
buf_size = 0; |
| 543 |
for (i = 0; i < dev_count; i++) { |
| 544 |
struct coreaudio_dev_info *cdi; |
| 545 |
Float64 sampleRate; |
| 546 |
|
| 547 |
cdi = &cf->dev_info[i]; |
| 548 |
pj_bzero(cdi, sizeof(*cdi)); |
| 549 |
cdi->dev_id = dev_ids[i]; |
| 550 |
|
| 551 |
/* Get device name */ |
| 552 |
addr.mSelector = kAudioDevicePropertyDeviceName; |
| 553 |
addr.mScope = kAudioObjectPropertyScopeGlobal; |
| 554 |
addr.mElement = AUDIO_OBJECT_ELEMENT_MAIN; |
| 555 |
size = sizeof(cdi->info.name); |
| 556 |
AudioObjectGetPropertyData(cdi->dev_id, &addr, |
| 557 |
0, NULL, |
| 558 |
&size, (void *)cdi->info.name); |
| 559 |
|
| 560 |
pj_ansi_strxcpy(cdi->info.driver, "core audio", |
| 561 |
sizeof(cdi->info.driver)); |
| 562 |
|
| 563 |
/* Get the number of input channels */ |
| 564 |
addr.mSelector = kAudioDevicePropertyStreamConfiguration; |
| 565 |
addr.mScope = kAudioDevicePropertyScopeInput; |
| 566 |
size = 0; |
| 567 |
ostatus = AudioObjectGetPropertyDataSize(cdi->dev_id, &addr, |
| 568 |
0, NULL, &size); |
| 569 |
if (ostatus == noErr && size > 0) { |
| 570 |
|
| 571 |
if (size > buf_size) { |
| 572 |
buf = pj_pool_alloc(cf->pool, size); |
| 573 |
buf_size = size; |
| 574 |
} |
| 575 |
if (buf) { |
| 576 |
UInt32 idx; |
| 577 |
|
| 578 |
/* Get the input stream configuration */ |
| 579 |
ostatus = AudioObjectGetPropertyData(cdi->dev_id, &addr, |
| 580 |
0, NULL, |
| 581 |
&size, buf); |
| 582 |
if (ostatus == noErr) { |
| 583 |
/* Count the total number of input channels in |
| 584 |
* the stream |
| 585 |
*/ |
| 586 |
for (idx = 0; idx < buf->mNumberBuffers; idx++) { |
| 587 |
cdi->info.input_count += |
| 588 |
buf->mBuffers[idx].mNumberChannels; |
| 589 |
} |
| 590 |
} |
| 591 |
} |
| 592 |
} |
| 593 |
|
| 594 |
/* Get the number of output channels */ |
| 595 |
addr.mScope = kAudioDevicePropertyScopeOutput; |
| 596 |
size = 0; |
| 597 |
ostatus = AudioObjectGetPropertyDataSize(cdi->dev_id, &addr, |
| 598 |
0, NULL, &size); |
| 599 |
if (ostatus == noErr && size > 0) { |
| 600 |
|
| 601 |
if (size > buf_size) { |
| 602 |
buf = pj_pool_alloc(cf->pool, size); |
| 603 |
buf_size = size; |
| 604 |
} |
| 605 |
if (buf) { |
| 606 |
UInt32 idx; |
| 607 |
|
| 608 |
/* Get the output stream configuration */ |
| 609 |
ostatus = AudioObjectGetPropertyData(cdi->dev_id, &addr, |
| 610 |
0, NULL, |
| 611 |
&size, buf); |
| 612 |
if (ostatus == noErr) { |
| 613 |
/* Count the total number of output channels in |
| 614 |
* the stream |
| 615 |
*/ |
| 616 |
for (idx = 0; idx < buf->mNumberBuffers; idx++) { |
| 617 |
cdi->info.output_count += |
| 618 |
buf->mBuffers[idx].mNumberChannels; |
| 619 |
} |
| 620 |
} |
| 621 |
} |
| 622 |
} |
| 623 |
|
| 624 |
/* Get default sample rate */ |
| 625 |
addr.mSelector = kAudioDevicePropertyNominalSampleRate; |
| 626 |
addr.mScope = kAudioObjectPropertyScopeGlobal; |
| 627 |
size = sizeof(Float64); |
| 628 |
ostatus = AudioObjectGetPropertyData (cdi->dev_id, &addr, |
| 629 |
0, NULL, |
| 630 |
&size, &sampleRate); |
| 631 |
cdi->info.default_samples_per_sec = (ostatus == noErr ? |
| 632 |
sampleRate: |
| 633 |
16000); |
| 634 |
|
| 635 |
/* Set device capabilities here */ |
| 636 |
if (cdi->info.input_count > 0) { |
| 637 |
cdi->info.caps |= PJMEDIA_AUD_DEV_CAP_INPUT_LATENCY; |
| 638 |
} |
| 639 |
if (cdi->info.output_count > 0) { |
| 640 |
cdi->info.caps |= PJMEDIA_AUD_DEV_CAP_OUTPUT_LATENCY; |
| 641 |
addr.mSelector = kAudioDevicePropertyVolumeScalar; |
| 642 |
addr.mScope = kAudioDevicePropertyScopeOutput; |
| 643 |
if (AudioObjectHasProperty(cdi->dev_id, &addr)) { |
| 644 |
cdi->info.caps |= PJMEDIA_AUD_DEV_CAP_OUTPUT_VOLUME_SETTING; |
| 645 |
} |
| 646 |
} |
| 647 |
if (cf->has_vpio) { |
| 648 |
cdi->info.caps |= PJMEDIA_AUD_DEV_CAP_EC; |
| 649 |
} |
| 650 |
|
| 651 |
cf->dev_count++; |
| 652 |
|
| 653 |
PJ_LOG(4, (THIS_FILE, " dev_id %d: %s (in=%d, out=%d) %dHz", |
| 654 |
cdi->dev_id, |
| 655 |
cdi->info.name, |
| 656 |
cdi->info.input_count, |
| 657 |
cdi->info.output_count, |
| 658 |
cdi->info.default_samples_per_sec)); |
| 659 |
} |
| 660 |
|
| 661 |
return PJ_SUCCESS; |
| 662 |
#endif |
| 663 |
} |
| 664 |
|
| 665 |
/* API: get number of devices */ |
| 666 |
static unsigned ca_factory_get_dev_count(pjmedia_aud_dev_factory *f) |
| 667 |
{ |
| 668 |
struct coreaudio_factory *cf = (struct coreaudio_factory*)f; |
| 669 |
return cf->dev_count; |
| 670 |
} |
| 671 |
|
| 672 |
/* API: get device info */ |
| 673 |
static pj_status_t ca_factory_get_dev_info(pjmedia_aud_dev_factory *f, |
| 674 |
unsigned index, |
| 675 |
pjmedia_aud_dev_info *info) |
| 676 |
{ |
| 677 |
struct coreaudio_factory *cf = (struct coreaudio_factory*)f; |
| 678 |
|
| 679 |
PJ_ASSERT_RETURN(index < cf->dev_count, PJMEDIA_EAUD_INVDEV); |
| 680 |
|
| 681 |
pj_memcpy(info, &cf->dev_info[index].info, sizeof(*info)); |
| 682 |
|
| 683 |
return PJ_SUCCESS; |
| 684 |
} |
| 685 |
|
| 686 |
/* API: create default device parameter */ |
| 687 |
static pj_status_t ca_factory_default_param(pjmedia_aud_dev_factory *f, |
| 688 |
unsigned index, |
| 689 |
pjmedia_aud_param *param) |
| 690 |
{ |
| 691 |
struct coreaudio_factory *cf = (struct coreaudio_factory*)f; |
| 692 |
struct coreaudio_dev_info *di = &cf->dev_info[index]; |
| 693 |
|
| 694 |
PJ_ASSERT_RETURN(index < cf->dev_count, PJMEDIA_EAUD_INVDEV); |
| 695 |
|
| 696 |
pj_bzero(param, sizeof(*param)); |
| 697 |
if (di->info.input_count && di->info.output_count) { |
| 698 |
param->dir = PJMEDIA_DIR_CAPTURE_PLAYBACK; |
| 699 |
param->rec_id = index; |
| 700 |
param->play_id = index; |
| 701 |
} else if (di->info.input_count) { |
| 702 |
param->dir = PJMEDIA_DIR_CAPTURE; |
| 703 |
param->rec_id = index; |
| 704 |
param->play_id = PJMEDIA_AUD_INVALID_DEV; |
| 705 |
} else if (di->info.output_count) { |
| 706 |
param->dir = PJMEDIA_DIR_PLAYBACK; |
| 707 |
param->play_id = index; |
| 708 |
param->rec_id = PJMEDIA_AUD_INVALID_DEV; |
| 709 |
} else { |
| 710 |
return PJMEDIA_EAUD_INVDEV; |
| 711 |
} |
| 712 |
|
| 713 |
/* Set the mandatory settings here */ |
| 714 |
param->clock_rate = di->info.default_samples_per_sec; |
| 715 |
param->channel_count = 1; |
| 716 |
param->samples_per_frame = di->info.default_samples_per_sec * 20 / 1000; |
| 717 |
param->bits_per_sample = 16; |
| 718 |
|
| 719 |
/* Set the param for device capabilities here */ |
| 720 |
param->flags = PJMEDIA_AUD_DEV_CAP_INPUT_LATENCY | |
| 721 |
PJMEDIA_AUD_DEV_CAP_OUTPUT_LATENCY; |
| 722 |
param->input_latency_ms = PJMEDIA_SND_DEFAULT_REC_LATENCY; |
| 723 |
param->output_latency_ms = PJMEDIA_SND_DEFAULT_PLAY_LATENCY; |
| 724 |
|
| 725 |
return PJ_SUCCESS; |
| 726 |
} |
| 727 |
|
| 728 |
OSStatus resampleProc(AudioConverterRef inAudioConverter, |
| 729 |
UInt32 *ioNumberDataPackets, |
| 730 |
AudioBufferList *ioData, |
| 731 |
AudioStreamPacketDescription **outDataPacketDescription, |
| 732 |
void *inUserData) |
| 733 |
{ |
| 734 |
struct coreaudio_stream *strm = (struct coreaudio_stream*)inUserData; |
| 735 |
|
| 736 |
if (*ioNumberDataPackets > strm->resample_buf_size) |
| 737 |
*ioNumberDataPackets = strm->resample_buf_size; |
| 738 |
|
| 739 |
ioData->mNumberBuffers = 1; |
| 740 |
ioData->mBuffers[0].mNumberChannels = strm->streamFormat.mChannelsPerFrame; |
| 741 |
ioData->mBuffers[0].mData = strm->resample_buf_ptr; |
| 742 |
ioData->mBuffers[0].mDataByteSize = *ioNumberDataPackets * |
| 743 |
strm->streamFormat.mChannelsPerFrame * |
| 744 |
strm->param.bits_per_sample >> 3; |
| 745 |
|
| 746 |
return noErr; |
| 747 |
} |
| 748 |
|
| 749 |
static OSStatus resample_callback(void *inRefCon, |
| 750 |
AudioUnitRenderActionFlags *ioActionFlags, |
| 751 |
const AudioTimeStamp *inTimeStamp, |
| 752 |
UInt32 inBusNumber, |
| 753 |
UInt32 inNumberFrames, |
| 754 |
AudioBufferList *ioData) |
| 755 |
{ |
| 756 |
struct coreaudio_stream *strm = (struct coreaudio_stream*)inRefCon; |
| 757 |
OSStatus ostatus; |
| 758 |
pj_status_t status = 0; |
| 759 |
unsigned nsamples; |
| 760 |
AudioBufferList *buf = strm->audio_buf; |
| 761 |
pj_int16_t *input; |
| 762 |
UInt32 resampleSize; |
| 763 |
|
| 764 |
pj_assert(!strm->quit_flag); |
| 765 |
|
| 766 |
/* Known cases of callback's thread: |
| 767 |
* - The thread may be changed in the middle of a session |
| 768 |
* it happens when plugging/unplugging headphone. |
| 769 |
* - The same thread may be reused in consecutive sessions. The first |
| 770 |
* session will leave TLS set, but release the TLS data address, |
| 771 |
* so the second session must re-register the callback's thread. |
| 772 |
*/ |
| 773 |
if (strm->rec_thread_initialized == 0 || !pj_thread_is_registered()) |
| 774 |
{ |
| 775 |
pj_bzero(strm->rec_thread_desc, sizeof(pj_thread_desc)); |
| 776 |
status = pj_thread_register("ca_rec", strm->rec_thread_desc, |
| 777 |
&strm->rec_thread); |
| 778 |
strm->rec_thread_initialized = 1; |
| 779 |
PJ_LOG(5,(THIS_FILE, "Recorder thread started, (%i frames)", |
| 780 |
inNumberFrames)); |
| 781 |
} |
| 782 |
|
| 783 |
buf->mBuffers[0].mData = NULL; |
| 784 |
buf->mBuffers[0].mDataByteSize = inNumberFrames * |
| 785 |
strm->streamFormat.mChannelsPerFrame; |
| 786 |
/* Render the unit to get input data */ |
| 787 |
ostatus = AudioUnitRender(strm->io_units[0], |
| 788 |
ioActionFlags, |
| 789 |
inTimeStamp, |
| 790 |
inBusNumber, |
| 791 |
inNumberFrames, |
| 792 |
buf); |
| 793 |
|
| 794 |
if (ostatus != noErr) { |
| 795 |
PJ_LOG(5, (THIS_FILE, "Core audio unit render error %i", ostatus)); |
| 796 |
goto on_break; |
| 797 |
} |
| 798 |
input = (pj_int16_t *)buf->mBuffers[0].mData; |
| 799 |
|
| 800 |
resampleSize = strm->resample_buf_size; |
| 801 |
nsamples = inNumberFrames * strm->param.channel_count + |
| 802 |
strm->resample_buf_count; |
| 803 |
|
| 804 |
if (nsamples >= resampleSize) { |
| 805 |
pjmedia_frame frame; |
| 806 |
UInt32 resampleOutput = strm->param.samples_per_frame / |
| 807 |
strm->streamFormat.mChannelsPerFrame; |
| 808 |
AudioBufferList ab; |
| 809 |
|
| 810 |
frame.type = PJMEDIA_FRAME_TYPE_AUDIO; |
| 811 |
frame.buf = (void*) strm->rec_buf; |
| 812 |
frame.size = strm->param.samples_per_frame * |
| 813 |
strm->param.bits_per_sample >> 3; |
| 814 |
frame.bit_info = 0; |
| 815 |
|
| 816 |
ab.mNumberBuffers = 1; |
| 817 |
ab.mBuffers[0].mNumberChannels = strm->streamFormat.mChannelsPerFrame; |
| 818 |
ab.mBuffers[0].mData = strm->rec_buf; |
| 819 |
ab.mBuffers[0].mDataByteSize = frame.size; |
| 820 |
|
| 821 |
/* If buffer is not empty, combine the buffer with the just incoming |
| 822 |
* samples, then call put_frame. |
| 823 |
*/ |
| 824 |
if (strm->resample_buf_count) { |
| 825 |
unsigned chunk_count = resampleSize - strm->resample_buf_count; |
| 826 |
pjmedia_copy_samples(strm->resample_buf + strm->resample_buf_count, |
| 827 |
input, chunk_count); |
| 828 |
|
| 829 |
/* Do the resample */ |
| 830 |
|
| 831 |
strm->resample_buf_ptr = strm->resample_buf; |
| 832 |
ostatus = AudioConverterFillComplexBuffer(strm->resample, |
| 833 |
resampleProc, |
| 834 |
strm, |
| 835 |
&resampleOutput, |
| 836 |
&ab, |
| 837 |
NULL); |
| 838 |
if (ostatus != noErr) { |
| 839 |
goto on_break; |
| 840 |
} |
| 841 |
frame.timestamp.u64 = strm->rec_timestamp.u64; |
| 842 |
|
| 843 |
status = (*strm->rec_cb)(strm->user_data, &frame); |
| 844 |
|
| 845 |
input = input + chunk_count; |
| 846 |
nsamples -= resampleSize; |
| 847 |
strm->resample_buf_count = 0; |
| 848 |
strm->rec_timestamp.u64 += strm->param.samples_per_frame / |
| 849 |
strm->param.channel_count; |
| 850 |
} |
| 851 |
|
| 852 |
|
| 853 |
/* Give all frames we have */ |
| 854 |
while (nsamples >= resampleSize && status == 0) { |
| 855 |
frame.timestamp.u64 = strm->rec_timestamp.u64; |
| 856 |
|
| 857 |
/* Do the resample */ |
| 858 |
strm->resample_buf_ptr = input; |
| 859 |
ab.mBuffers[0].mDataByteSize = frame.size; |
| 860 |
resampleOutput = strm->param.samples_per_frame / |
| 861 |
strm->streamFormat.mChannelsPerFrame; |
| 862 |
ostatus = AudioConverterFillComplexBuffer(strm->resample, |
| 863 |
resampleProc, |
| 864 |
strm, |
| 865 |
&resampleOutput, |
| 866 |
&ab, |
| 867 |
NULL); |
| 868 |
if (ostatus != noErr) { |
| 869 |
goto on_break; |
| 870 |
} |
| 871 |
|
| 872 |
status = (*strm->rec_cb)(strm->user_data, &frame); |
| 873 |
|
| 874 |
input = (pj_int16_t*) input + resampleSize; |
| 875 |
nsamples -= resampleSize; |
| 876 |
strm->rec_timestamp.u64 += strm->param.samples_per_frame / |
| 877 |
strm->param.channel_count; |
| 878 |
} |
| 879 |
|
| 880 |
/* Store the remaining samples into the buffer */ |
| 881 |
if (nsamples && status == 0) { |
| 882 |
strm->resample_buf_count = nsamples; |
| 883 |
pjmedia_copy_samples(strm->resample_buf, input, |
| 884 |
nsamples); |
| 885 |
} |
| 886 |
|
| 887 |
} else { |
| 888 |
/* Not enough samples, let's just store them in the buffer */ |
| 889 |
pjmedia_copy_samples(strm->resample_buf + strm->resample_buf_count, |
| 890 |
input, |
| 891 |
inNumberFrames * strm->param.channel_count); |
| 892 |
strm->resample_buf_count += inNumberFrames * |
| 893 |
strm->param.channel_count; |
| 894 |
} |
| 895 |
|
| 896 |
return noErr; |
| 897 |
|
| 898 |
on_break: |
| 899 |
return -1; |
| 900 |
} |
| 901 |
|
| 902 |
static OSStatus input_callback(void *inRefCon, |
| 903 |
AudioUnitRenderActionFlags *ioActionFlags, |
| 904 |
const AudioTimeStamp *inTimeStamp, |
| 905 |
UInt32 inBusNumber, |
| 906 |
UInt32 inNumberFrames, |
| 907 |
AudioBufferList *ioData) |
| 908 |
{ |
| 909 |
struct coreaudio_stream *strm = (struct coreaudio_stream*)inRefCon; |
| 910 |
OSStatus ostatus; |
| 911 |
pj_status_t status = 0; |
| 912 |
unsigned nsamples; |
| 913 |
AudioBufferList *buf = strm->audio_buf; |
| 914 |
pj_int16_t *input; |
| 915 |
|
| 916 |
pj_assert(!strm->quit_flag); |
| 917 |
|
| 918 |
/* Known cases of callback's thread: |
| 919 |
* - The thread may be changed in the middle of a session |
| 920 |
* it happens when plugging/unplugging headphone. |
| 921 |
* - The same thread may be reused in consecutive sessions. The first |
| 922 |
* session will leave TLS set, but release the TLS data address, |
| 923 |
* so the second session must re-register the callback's thread. |
| 924 |
*/ |
| 925 |
if (strm->rec_thread_initialized == 0 || !pj_thread_is_registered()) |
| 926 |
{ |
| 927 |
pj_bzero(strm->rec_thread_desc, sizeof(pj_thread_desc)); |
| 928 |
status = pj_thread_register("ca_rec", strm->rec_thread_desc, |
| 929 |
&strm->rec_thread); |
| 930 |
strm->rec_thread_initialized = 1; |
| 931 |
PJ_LOG(5,(THIS_FILE, "Recorder thread started, (%i frames)", |
| 932 |
inNumberFrames)); |
| 933 |
} |
| 934 |
|
| 935 |
buf->mBuffers[0].mData = NULL; |
| 936 |
buf->mBuffers[0].mDataByteSize = inNumberFrames * |
| 937 |
strm->streamFormat.mChannelsPerFrame; |
| 938 |
/* Render the unit to get input data */ |
| 939 |
ostatus = AudioUnitRender(strm->io_units[0], |
| 940 |
ioActionFlags, |
| 941 |
inTimeStamp, |
| 942 |
inBusNumber, |
| 943 |
inNumberFrames, |
| 944 |
buf); |
| 945 |
|
| 946 |
if (ostatus != noErr) { |
| 947 |
PJ_LOG(5, (THIS_FILE, "Core audio unit render error %i", ostatus)); |
| 948 |
goto on_break; |
| 949 |
} |
| 950 |
input = (pj_int16_t *)buf->mBuffers[0].mData; |
| 951 |
|
| 952 |
/* Calculate number of samples we've got */ |
| 953 |
nsamples = inNumberFrames * strm->param.channel_count + |
| 954 |
strm->rec_buf_count; |
| 955 |
if (nsamples >= strm->param.samples_per_frame) { |
| 956 |
pjmedia_frame frame; |
| 957 |
|
| 958 |
frame.type = PJMEDIA_FRAME_TYPE_AUDIO; |
| 959 |
frame.size = strm->param.samples_per_frame * |
| 960 |
strm->param.bits_per_sample >> 3; |
| 961 |
frame.bit_info = 0; |
| 962 |
|
| 963 |
/* If buffer is not empty, combine the buffer with the just incoming |
| 964 |
* samples, then call put_frame. |
| 965 |
*/ |
| 966 |
if (strm->rec_buf_count) { |
| 967 |
unsigned chunk_count = 0; |
| 968 |
|
| 969 |
chunk_count = strm->param.samples_per_frame - strm->rec_buf_count; |
| 970 |
pjmedia_copy_samples(strm->rec_buf + strm->rec_buf_count, |
| 971 |
input, chunk_count); |
| 972 |
|
| 973 |
frame.buf = (void*) strm->rec_buf; |
| 974 |
frame.timestamp.u64 = strm->rec_timestamp.u64; |
| 975 |
|
| 976 |
status = (*strm->rec_cb)(strm->user_data, &frame); |
| 977 |
|
| 978 |
input = input + chunk_count; |
| 979 |
nsamples -= strm->param.samples_per_frame; |
| 980 |
strm->rec_buf_count = 0; |
| 981 |
strm->rec_timestamp.u64 += strm->param.samples_per_frame / |
| 982 |
strm->param.channel_count; |
| 983 |
} |
| 984 |
|
| 985 |
/* Give all frames we have */ |
| 986 |
while (nsamples >= strm->param.samples_per_frame && status == 0) { |
| 987 |
frame.buf = (void*) input; |
| 988 |
frame.timestamp.u64 = strm->rec_timestamp.u64; |
| 989 |
|
| 990 |
status = (*strm->rec_cb)(strm->user_data, &frame); |
| 991 |
|
| 992 |
input = (pj_int16_t*) input + strm->param.samples_per_frame; |
| 993 |
nsamples -= strm->param.samples_per_frame; |
| 994 |
strm->rec_timestamp.u64 += strm->param.samples_per_frame / |
| 995 |
strm->param.channel_count; |
| 996 |
} |
| 997 |
|
| 998 |
/* Store the remaining samples into the buffer */ |
| 999 |
if (nsamples && status == 0) { |
| 1000 |
strm->rec_buf_count = nsamples; |
| 1001 |
pjmedia_copy_samples(strm->rec_buf, input, |
| 1002 |
nsamples); |
| 1003 |
} |
| 1004 |
|
| 1005 |
} else { |
| 1006 |
/* Not enough samples, let's just store them in the buffer */ |
| 1007 |
pjmedia_copy_samples(strm->rec_buf + strm->rec_buf_count, |
| 1008 |
input, |
| 1009 |
inNumberFrames * strm->param.channel_count); |
| 1010 |
strm->rec_buf_count += inNumberFrames * strm->param.channel_count; |
| 1011 |
} |
| 1012 |
|
| 1013 |
return noErr; |
| 1014 |
|
| 1015 |
on_break: |
| 1016 |
if (status != PJ_SUCCESS && !strm->quit_flag) { |
| 1017 |
pjmedia_event e; |
| 1018 |
|
| 1019 |
PJ_PERROR(3, (THIS_FILE, status, "CoreAudio input callback stopped due to error")); |
| 1020 |
|
| 1021 |
/* Broadcast CoreAudio input error */ |
| 1022 |
pjmedia_event_init(&e, PJMEDIA_EVENT_AUD_DEV_ERROR, |
| 1023 |
&strm->rec_timestamp, &strm->base); |
| 1024 |
e.data.aud_dev_err.dir = PJMEDIA_DIR_CAPTURE; |
| 1025 |
e.data.aud_dev_err.status = status; |
| 1026 |
e.data.aud_dev_err.id = strm->param.rec_id; |
| 1027 |
|
| 1028 |
pjmedia_event_publish(NULL, &strm->base, &e, |
| 1029 |
PJMEDIA_EVENT_PUBLISH_DEFAULT); |
| 1030 |
} |
| 1031 |
return -1; |
| 1032 |
} |
| 1033 |
|
| 1034 |
/* Copy 16-bit signed int samples to a destination buffer, which can be |
| 1035 |
* either 16-bit signed int, or float. |
| 1036 |
*/ |
| 1037 |
static void copy_samples(void *dst, unsigned *dst_pos, unsigned dst_elmt_size, |
| 1038 |
pj_int16_t *src, unsigned nsamples) |
| 1039 |
{ |
| 1040 |
if (dst_elmt_size == sizeof(pj_int16_t)) { |
| 1041 |
/* Destination is also 16-bit signed int. */ |
| 1042 |
pjmedia_copy_samples((pj_int16_t*)dst + *dst_pos, src, nsamples); |
| 1043 |
} else { |
| 1044 |
/* Convert it first to float. */ |
| 1045 |
unsigned i; |
| 1046 |
float *fdst = (float *)dst; |
| 1047 |
|
| 1048 |
pj_assert(dst_elmt_size == sizeof(Float32)); |
| 1049 |
|
| 1050 |
for (i = 0; i< nsamples; i++) { |
| 1051 |
/* Value needs to be between -1.0 to 1.0 */ |
| 1052 |
fdst[*dst_pos + i] = (float)src[i] / 32768.0f; |
| 1053 |
} |
| 1054 |
} |
| 1055 |
*dst_pos += nsamples; |
| 1056 |
} |
| 1057 |
|
| 1058 |
static OSStatus output_renderer(void *inRefCon, |
| 1059 |
AudioUnitRenderActionFlags *ioActionFlags, |
| 1060 |
const AudioTimeStamp *inTimeStamp, |
| 1061 |
UInt32 inBusNumber, |
| 1062 |
UInt32 inNumberFrames, |
| 1063 |
AudioBufferList *ioData) |
| 1064 |
{ |
| 1065 |
struct coreaudio_stream *stream = (struct coreaudio_stream*)inRefCon; |
| 1066 |
pj_status_t status = 0; |
| 1067 |
unsigned nsamples_req = inNumberFrames * stream->param.channel_count; |
| 1068 |
void *output = ioData->mBuffers[0].mData; |
| 1069 |
unsigned elmt_size = ioData->mBuffers[0].mDataByteSize / inNumberFrames / |
| 1070 |
stream->param.channel_count; |
| 1071 |
unsigned output_pos = 0; |
| 1072 |
|
| 1073 |
pj_assert(!stream->quit_flag); |
| 1074 |
|
| 1075 |
/* Known cases of callback's thread: |
| 1076 |
* - The thread may be changed in the middle of a session |
| 1077 |
* it happens when plugging/unplugging headphone. |
| 1078 |
* - The same thread may be reused in consecutive sessions. The first |
| 1079 |
* session will leave TLS set, but release the TLS data address, |
| 1080 |
* so the second session must re-register the callback's thread. |
| 1081 |
*/ |
| 1082 |
if (stream->play_thread_initialized == 0 || !pj_thread_is_registered()) |
| 1083 |
{ |
| 1084 |
pj_bzero(stream->play_thread_desc, sizeof(pj_thread_desc)); |
| 1085 |
status = pj_thread_register("coreaudio", stream->play_thread_desc, |
| 1086 |
&stream->play_thread); |
| 1087 |
stream->play_thread_initialized = 1; |
| 1088 |
PJ_LOG(5,(THIS_FILE, "Player thread started, (%i frames)", |
| 1089 |
inNumberFrames)); |
| 1090 |
} |
| 1091 |
|
| 1092 |
|
| 1093 |
/* Check if any buffered samples */ |
| 1094 |
if (stream->play_buf_count) { |
| 1095 |
/* samples buffered >= requested by sound device */ |
| 1096 |
if (stream->play_buf_count >= nsamples_req) { |
| 1097 |
copy_samples(output, &output_pos, elmt_size, |
| 1098 |
stream->play_buf, nsamples_req); |
| 1099 |
stream->play_buf_count -= nsamples_req; |
| 1100 |
pjmedia_move_samples(stream->play_buf, |
| 1101 |
stream->play_buf + nsamples_req, |
| 1102 |
stream->play_buf_count); |
| 1103 |
nsamples_req = 0; |
| 1104 |
|
| 1105 |
return noErr; |
| 1106 |
} |
| 1107 |
|
| 1108 |
/* samples buffered < requested by sound device */ |
| 1109 |
copy_samples(output, &output_pos, elmt_size, |
| 1110 |
stream->play_buf, stream->play_buf_count); |
| 1111 |
nsamples_req -= stream->play_buf_count; |
| 1112 |
stream->play_buf_count = 0; |
| 1113 |
} |
| 1114 |
|
| 1115 |
/* Fill output buffer as requested */ |
| 1116 |
while (nsamples_req && status == 0) { |
| 1117 |
pjmedia_frame frame; |
| 1118 |
|
| 1119 |
frame.type = PJMEDIA_FRAME_TYPE_AUDIO; |
| 1120 |
frame.size = stream->param.samples_per_frame * |
| 1121 |
stream->param.bits_per_sample >> 3; |
| 1122 |
frame.timestamp.u64 = stream->play_timestamp.u64; |
| 1123 |
frame.bit_info = 0; |
| 1124 |
|
| 1125 |
if (nsamples_req >= stream->param.samples_per_frame) { |
| 1126 |
/* If the output buffer is 16-bit signed int, we can |
| 1127 |
* directly use the supplied buffer. |
| 1128 |
*/ |
| 1129 |
if (elmt_size == sizeof(pj_int16_t)) { |
| 1130 |
frame.buf = (pj_int16_t *)output + output_pos; |
| 1131 |
} else { |
| 1132 |
frame.buf = stream->play_buf; |
| 1133 |
} |
| 1134 |
status = (*stream->play_cb)(stream->user_data, &frame); |
| 1135 |
if (status != PJ_SUCCESS) |
| 1136 |
goto on_break; |
| 1137 |
|
| 1138 |
if (frame.type != PJMEDIA_FRAME_TYPE_AUDIO) |
| 1139 |
pj_bzero(frame.buf, frame.size); |
| 1140 |
|
| 1141 |
nsamples_req -= stream->param.samples_per_frame; |
| 1142 |
if (elmt_size == sizeof(pj_int16_t)) { |
| 1143 |
output_pos += stream->param.samples_per_frame; |
| 1144 |
} else { |
| 1145 |
copy_samples(output, &output_pos, elmt_size, stream->play_buf, |
| 1146 |
stream->param.samples_per_frame); |
| 1147 |
} |
| 1148 |
} else { |
| 1149 |
frame.buf = stream->play_buf; |
| 1150 |
status = (*stream->play_cb)(stream->user_data, &frame); |
| 1151 |
if (status != PJ_SUCCESS) |
| 1152 |
goto on_break; |
| 1153 |
|
| 1154 |
if (frame.type != PJMEDIA_FRAME_TYPE_AUDIO) |
| 1155 |
pj_bzero(frame.buf, frame.size); |
| 1156 |
|
| 1157 |
copy_samples(output, &output_pos, elmt_size, |
| 1158 |
stream->play_buf, nsamples_req); |
| 1159 |
stream->play_buf_count = stream->param.samples_per_frame - |
| 1160 |
nsamples_req; |
| 1161 |
pjmedia_move_samples(stream->play_buf, |
| 1162 |
stream->play_buf+nsamples_req, |
| 1163 |
stream->play_buf_count); |
| 1164 |
nsamples_req = 0; |
| 1165 |
} |
| 1166 |
|
| 1167 |
stream->play_timestamp.u64 += stream->param.samples_per_frame / |
| 1168 |
stream->param.channel_count; |
| 1169 |
} |
| 1170 |
|
| 1171 |
return noErr; |
| 1172 |
|
| 1173 |
on_break: |
| 1174 |
if (status != PJ_SUCCESS && !stream->quit_flag) { |
| 1175 |
pjmedia_event e; |
| 1176 |
|
| 1177 |
PJ_PERROR(3, (THIS_FILE, status, "CoreAudio output callback stopped due to error")); |
| 1178 |
|
| 1179 |
/* Broadcast CoreAudio output error */ |
| 1180 |
pjmedia_event_init(&e, PJMEDIA_EVENT_AUD_DEV_ERROR, |
| 1181 |
&stream->play_timestamp, &stream->base); |
| 1182 |
e.data.aud_dev_err.dir = PJMEDIA_DIR_PLAYBACK; |
| 1183 |
e.data.aud_dev_err.status = status; |
| 1184 |
e.data.aud_dev_err.id = stream->param.play_id; |
| 1185 |
|
| 1186 |
pjmedia_event_publish(NULL, &stream->base, &e, |
| 1187 |
PJMEDIA_EVENT_PUBLISH_DEFAULT); |
| 1188 |
} |
| 1189 |
return -1; |
| 1190 |
} |
| 1191 |
|
| 1192 |
#if !COREAUDIO_MAC && USE_AUDIO_SESSION_API != 0 |
| 1193 |
static void propListener(void *inClientData, |
| 1194 |
AudioSessionPropertyID inID, |
| 1195 |
UInt32 inDataSize, |
| 1196 |
const void * inData) |
| 1197 |
{ |
| 1198 |
struct coreaudio_factory *cf = (struct coreaudio_factory*)inClientData; |
| 1199 |
struct stream_list *it, *itBegin; |
| 1200 |
CFDictionaryRef routeDictionary; |
| 1201 |
CFNumberRef reason; |
| 1202 |
SInt32 reasonVal; |
| 1203 |
pj_assert(cf); |
| 1204 |
|
| 1205 |
if (inID != kAudioSessionProperty_AudioRouteChange) |
| 1206 |
return; |
| 1207 |
|
| 1208 |
routeDictionary = (CFDictionaryRef)inData; |
| 1209 |
reason = (CFNumberRef) |
| 1210 |
CFDictionaryGetValue( |
| 1211 |
routeDictionary, |
| 1212 |
CFSTR(kAudioSession_AudioRouteChangeKey_Reason)); |
| 1213 |
CFNumberGetValue(reason, kCFNumberSInt32Type, &reasonVal); |
| 1214 |
|
| 1215 |
if (reasonVal != kAudioSessionRouteChangeReason_OldDeviceUnavailable) { |
| 1216 |
PJ_LOG(3, (THIS_FILE, "ignoring audio route change...")); |
| 1217 |
return; |
| 1218 |
} |
| 1219 |
|
| 1220 |
PJ_LOG(3, (THIS_FILE, "audio route changed")); |
| 1221 |
|
| 1222 |
pj_mutex_lock(cf->mutex); |
| 1223 |
itBegin = &cf->streams; |
| 1224 |
for (it = itBegin->next; it != itBegin; it = it->next) { |
| 1225 |
if (it->stream->interrupted) |
| 1226 |
continue; |
| 1227 |
|
| 1228 |
/* |
| 1229 |
status = ca_stream_stop((pjmedia_aud_stream *)it->stream); |
| 1230 |
status = ca_stream_start((pjmedia_aud_stream *)it->stream); |
| 1231 |
if (status != PJ_SUCCESS) { |
| 1232 |
PJ_LOG(3, (THIS_FILE, |
| 1233 |
"Error: failed to restart the audio unit (%i)", |
| 1234 |
status)); |
| 1235 |
continue; |
| 1236 |
} |
| 1237 |
PJ_LOG(3, (THIS_FILE, "core audio unit successfully restarted")); |
| 1238 |
*/ |
| 1239 |
} |
| 1240 |
pj_mutex_unlock(cf->mutex); |
| 1241 |
} |
| 1242 |
|
| 1243 |
static void interruptionListener(void *inClientData, UInt32 inInterruption) |
| 1244 |
{ |
| 1245 |
struct stream_list *it, *itBegin; |
| 1246 |
pj_status_t status; |
| 1247 |
static pj_thread_desc thread_desc; |
| 1248 |
pj_thread_t *thread; |
| 1249 |
|
| 1250 |
/* Register the thread with PJLIB, this is must for any external threads |
| 1251 |
* which need to use the PJLIB framework. |
| 1252 |
*/ |
| 1253 |
if (!pj_thread_is_registered()) { |
| 1254 |
pj_bzero(thread_desc, sizeof(pj_thread_desc)); |
| 1255 |
status = pj_thread_register("intListener", thread_desc, &thread); |
| 1256 |
} |
| 1257 |
|
| 1258 |
PJ_LOG(3, (THIS_FILE, "Session interrupted! --- %s ---", |
| 1259 |
inInterruption == kAudioSessionBeginInterruption ? |
| 1260 |
"Begin Interruption" : "End Interruption")); |
| 1261 |
|
| 1262 |
if (!cf_instance) |
| 1263 |
return; |
| 1264 |
|
| 1265 |
pj_mutex_lock(cf_instance->mutex); |
| 1266 |
itBegin = &cf_instance->streams; |
| 1267 |
for (it = itBegin->next; it != itBegin; it = it->next) { |
| 1268 |
if (inInterruption == kAudioSessionEndInterruption && |
| 1269 |
it->stream->interrupted == PJ_TRUE) |
| 1270 |
{ |
| 1271 |
UInt32 audioCategory; |
| 1272 |
OSStatus ostatus; |
| 1273 |
|
| 1274 |
/* Make sure that your application can receive remote control |
| 1275 |
* events by adding the code: |
| 1276 |
* [[UIApplication sharedApplication] |
| 1277 |
* beginReceivingRemoteControlEvents]; |
| 1278 |
* Otherwise audio unit will fail to restart while your |
| 1279 |
* application is in the background mode. |
| 1280 |
*/ |
| 1281 |
/* Make sure we set the correct audio category before restarting */ |
| 1282 |
audioCategory = kAudioSessionCategory_PlayAndRecord; |
| 1283 |
ostatus = AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, |
| 1284 |
sizeof(audioCategory), |
| 1285 |
&audioCategory); |
| 1286 |
if (ostatus != kAudioSessionNoError) { |
| 1287 |
PJ_LOG(4, (THIS_FILE, |
| 1288 |
"Warning: cannot set the audio session category (%i)", |
| 1289 |
ostatus)); |
| 1290 |
} |
| 1291 |
|
| 1292 |
/* Restart the stream */ |
| 1293 |
status = ca_stream_start((pjmedia_aud_stream*)it->stream); |
| 1294 |
if (status != PJ_SUCCESS) { |
| 1295 |
PJ_LOG(3, (THIS_FILE, |
| 1296 |
"Error: failed to restart the audio unit (%i)", |
| 1297 |
status)); |
| 1298 |
continue; |
| 1299 |
} |
| 1300 |
PJ_LOG(3, (THIS_FILE, "core audio unit successfully resumed" |
| 1301 |
" after interruption")); |
| 1302 |
} else if (inInterruption == kAudioSessionBeginInterruption && |
| 1303 |
it->stream->running == PJ_TRUE) |
| 1304 |
{ |
| 1305 |
status = ca_stream_stop((pjmedia_aud_stream*)it->stream); |
| 1306 |
it->stream->interrupted = PJ_TRUE; |
| 1307 |
} |
| 1308 |
} |
| 1309 |
pj_mutex_unlock(cf_instance->mutex); |
| 1310 |
} |
| 1311 |
|
| 1312 |
#endif |
| 1313 |
|
| 1314 |
#if COREAUDIO_MAC |
| 1315 |
/* Internal: create audio converter for resampling the recorder device */ |
| 1316 |
static pj_status_t create_audio_resample(struct coreaudio_stream *strm, |
| 1317 |
AudioStreamBasicDescription *desc) |
| 1318 |
{ |
| 1319 |
OSStatus ostatus; |
| 1320 |
|
| 1321 |
pj_assert(strm->streamFormat.mSampleRate != desc->mSampleRate); |
| 1322 |
pj_assert(NULL == strm->resample); |
| 1323 |
pj_assert(NULL == strm->resample_buf); |
| 1324 |
|
| 1325 |
/* Create the audio converter */ |
| 1326 |
ostatus = AudioConverterNew(desc, &strm->streamFormat, &strm->resample); |
| 1327 |
if (ostatus != noErr) { |
| 1328 |
return PJMEDIA_AUDIODEV_ERRNO_FROM_COREAUDIO(ostatus); |
| 1329 |
} |
| 1330 |
|
| 1331 |
/* |
| 1332 |
* Allocate the buffer required to hold enough input data |
| 1333 |
*/ |
| 1334 |
strm->resample_buf_size = (unsigned)(desc->mSampleRate * |
| 1335 |
strm->param.samples_per_frame / |
| 1336 |
strm->param.clock_rate); |
| 1337 |
strm->resample_buf = (pj_int16_t*) |
| 1338 |
pj_pool_alloc(strm->pool, |
| 1339 |
strm->resample_buf_size * |
| 1340 |
strm->param.bits_per_sample >> 3); |
| 1341 |
if (!strm->resample_buf) |
| 1342 |
return PJ_ENOMEM; |
| 1343 |
strm->resample_buf_count = 0; |
| 1344 |
|
| 1345 |
return PJ_SUCCESS; |
| 1346 |
} |
| 1347 |
#endif |
| 1348 |
|
| 1349 |
/* Internal: create audio unit for recorder/playback device */ |
| 1350 |
static pj_status_t create_audio_unit(AudioComponent io_comp, |
| 1351 |
AudioDeviceID dev_id, |
| 1352 |
pjmedia_dir dir, |
| 1353 |
struct coreaudio_stream *strm, |
| 1354 |
AudioUnit *io_unit) |
| 1355 |
{ |
| 1356 |
OSStatus ostatus; |
| 1357 |
|
| 1358 |
#if !COREAUDIO_MAC |
| 1359 |
strm->sess = [AVAudioSession sharedInstance]; |
| 1360 |
#endif |
| 1361 |
|
| 1362 |
/* Create an audio unit to interface with the device */ |
| 1363 |
ostatus = AudioComponentInstanceNew(io_comp, io_unit); |
| 1364 |
if (ostatus != noErr) { |
| 1365 |
return PJMEDIA_AUDIODEV_ERRNO_FROM_COREAUDIO(ostatus); |
| 1366 |
} |
| 1367 |
|
| 1368 |
/* Set audio unit's properties for capture device */ |
| 1369 |
if (dir & PJMEDIA_DIR_CAPTURE) { |
| 1370 |
UInt32 enable = 1; |
| 1371 |
|
| 1372 |
/* Enable input */ |
| 1373 |
ostatus = AudioUnitSetProperty(*io_unit, |
| 1374 |
kAudioOutputUnitProperty_EnableIO, |
| 1375 |
kAudioUnitScope_Input, |
| 1376 |
1, |
| 1377 |
&enable, |
| 1378 |
sizeof(enable)); |
| 1379 |
if (ostatus != noErr && !strm->param.ec_enabled) { |
| 1380 |
PJ_LOG(4, (THIS_FILE, |
| 1381 |
"Warning: cannot enable IO of capture device %d", |
| 1382 |
dev_id)); |
| 1383 |
} |
| 1384 |
|
| 1385 |
/* Disable output */ |
| 1386 |
if (!(dir & PJMEDIA_DIR_PLAYBACK)) { |
| 1387 |
enable = 0; |
| 1388 |
ostatus = AudioUnitSetProperty(*io_unit, |
| 1389 |
kAudioOutputUnitProperty_EnableIO, |
| 1390 |
kAudioUnitScope_Output, |
| 1391 |
0, |
| 1392 |
&enable, |
| 1393 |
sizeof(enable)); |
| 1394 |
if (ostatus != noErr && !strm->param.ec_enabled) { |
| 1395 |
PJ_LOG(4, (THIS_FILE, |
| 1396 |
"Warning: cannot disable IO of capture device %d", |
| 1397 |
dev_id)); |
| 1398 |
} |
| 1399 |
} |
| 1400 |
} |
| 1401 |
|
| 1402 |
/* Set audio unit's properties for playback device */ |
| 1403 |
if (dir & PJMEDIA_DIR_PLAYBACK) { |
| 1404 |
UInt32 enable = 1; |
| 1405 |
|
| 1406 |
/* Enable output */ |
| 1407 |
ostatus = AudioUnitSetProperty(*io_unit, |
| 1408 |
kAudioOutputUnitProperty_EnableIO, |
| 1409 |
kAudioUnitScope_Output, |
| 1410 |
0, |
| 1411 |
&enable, |
| 1412 |
sizeof(enable)); |
| 1413 |
if (ostatus != noErr && !strm->param.ec_enabled) |
| 1414 |
{ |
| 1415 |
PJ_LOG(4, (THIS_FILE, |
| 1416 |
"Warning: cannot enable IO of playback device %d", |
| 1417 |
dev_id)); |
| 1418 |
} |
| 1419 |
|
| 1420 |
} |
| 1421 |
|
| 1422 |
#if COREAUDIO_MAC |
| 1423 |
if (!strm->param.ec_enabled) { |
| 1424 |
/* When using VPIO on Mac, we shouldn't set the current device. |
| 1425 |
* Doing so will cause getting the buffer size later to fail |
| 1426 |
* with kAudioUnitErr_InvalidProperty error (-10879). |
| 1427 |
*/ |
| 1428 |
PJ_LOG(4, (THIS_FILE, "Setting current device %d", dev_id)); |
| 1429 |
ostatus = AudioUnitSetProperty(*io_unit, |
| 1430 |
kAudioOutputUnitProperty_CurrentDevice, |
| 1431 |
kAudioUnitScope_Global, |
| 1432 |
0, |
| 1433 |
&dev_id, |
| 1434 |
sizeof(dev_id)); |
| 1435 |
if (ostatus != noErr) { |
| 1436 |
PJ_LOG(3, (THIS_FILE, "Failed setting current device %d, " |
| 1437 |
"error: %d", dev_id, ostatus)); |
| 1438 |
return PJMEDIA_AUDIODEV_ERRNO_FROM_COREAUDIO(ostatus); |
| 1439 |
} |
| 1440 |
} |
| 1441 |
#endif |
| 1442 |
|
| 1443 |
if (dir & PJMEDIA_DIR_CAPTURE) { |
| 1444 |
#if COREAUDIO_MAC |
| 1445 |
AudioStreamBasicDescription deviceFormat; |
| 1446 |
UInt32 size; |
| 1447 |
|
| 1448 |
if (!strm->param.ec_enabled) { |
| 1449 |
/* Keep the sample rate from the device, otherwise we will confuse |
| 1450 |
* AUHAL. |
| 1451 |
*/ |
| 1452 |
size = sizeof(AudioStreamBasicDescription); |
| 1453 |
ostatus = AudioUnitGetProperty(*io_unit, |
| 1454 |
kAudioUnitProperty_StreamFormat, |
| 1455 |
kAudioUnitScope_Input, |
| 1456 |
1, |
| 1457 |
&deviceFormat, |
| 1458 |
&size); |
| 1459 |
if (ostatus != noErr) { |
| 1460 |
PJ_LOG(3, (THIS_FILE, "Failed getting stream format of device" |
| 1461 |
" %d, error: %d", dev_id, ostatus)); |
| 1462 |
return PJMEDIA_AUDIODEV_ERRNO_FROM_COREAUDIO(ostatus); |
| 1463 |
} |
| 1464 |
strm->streamFormat.mSampleRate = deviceFormat.mSampleRate; |
| 1465 |
} |
| 1466 |
#endif |
| 1467 |
|
| 1468 |
/* When setting the stream format, we have to make sure the sample |
| 1469 |
* rate is supported. Setting an unsupported sample rate will cause |
| 1470 |
* AudioUnitRender() to fail later. |
| 1471 |
*/ |
| 1472 |
ostatus = AudioUnitSetProperty(*io_unit, |
| 1473 |
kAudioUnitProperty_StreamFormat, |
| 1474 |
kAudioUnitScope_Output, |
| 1475 |
1, |
| 1476 |
&strm->streamFormat, |
| 1477 |
sizeof(strm->streamFormat)); |
| 1478 |
if (ostatus != noErr) { |
| 1479 |
PJ_LOG(3, (THIS_FILE, "Failed setting stream format of device %d" |
| 1480 |
", error: %d", dev_id, ostatus)); |
| 1481 |
return PJMEDIA_AUDIODEV_ERRNO_FROM_COREAUDIO(ostatus); |
| 1482 |
} |
| 1483 |
|
| 1484 |
#if COREAUDIO_MAC |
| 1485 |
strm->streamFormat.mSampleRate = strm->param.clock_rate; |
| 1486 |
size = sizeof(AudioStreamBasicDescription); |
| 1487 |
ostatus = AudioUnitGetProperty (*io_unit, |
| 1488 |
kAudioUnitProperty_StreamFormat, |
| 1489 |
kAudioUnitScope_Output, |
| 1490 |
1, |
| 1491 |
&deviceFormat, |
| 1492 |
&size); |
| 1493 |
if (ostatus == noErr) { |
| 1494 |
if (strm->streamFormat.mSampleRate != deviceFormat.mSampleRate) { |
| 1495 |
PJ_LOG(4, (THIS_FILE, "Creating audio resample from %d to %d", |
| 1496 |
(int)deviceFormat.mSampleRate, |
| 1497 |
(int)strm->streamFormat.mSampleRate)); |
| 1498 |
pj_status_t rc = create_audio_resample(strm, &deviceFormat); |
| 1499 |
if (PJ_SUCCESS != rc) { |
| 1500 |
PJ_LOG(3, (THIS_FILE, "Failed creating resample %d", |
| 1501 |
rc)); |
| 1502 |
return rc; |
| 1503 |
} |
| 1504 |
} |
| 1505 |
} else { |
| 1506 |
PJ_LOG(3, (THIS_FILE, "Failed getting stream format of device %d" |
| 1507 |
" (2), error: %d", dev_id, ostatus)); |
| 1508 |
return PJMEDIA_AUDIODEV_ERRNO_FROM_COREAUDIO(ostatus); |
| 1509 |
} |
| 1510 |
#endif |
| 1511 |
} |
| 1512 |
|
| 1513 |
if (dir & PJMEDIA_DIR_PLAYBACK) { |
| 1514 |
AURenderCallbackStruct output_cb; |
| 1515 |
AudioStreamBasicDescription streamFormat = strm->streamFormat; |
| 1516 |
#if !COREAUDIO_MAC |
| 1517 |
BOOL isMacCatalystApp = false; |
| 1518 |
|
| 1519 |
#ifdef __IPHONE_13_0 |
| 1520 |
if (@available(iOS 13.0, *)) { |
| 1521 |
/* According to Apple's doc, the property isMacCatalystApp is true |
| 1522 |
* when the process is: |
| 1523 |
* - A Mac app built with Mac Catalyst, or an iOS app running on Apple silicon. |
| 1524 |
* - Running on a Mac. |
| 1525 |
*/ |
| 1526 |
isMacCatalystApp = [NSProcessInfo processInfo].isMacCatalystApp; |
| 1527 |
} |
| 1528 |
#endif |
| 1529 |
#endif |
| 1530 |
|
| 1531 |
/* Set the stream format */ |
| 1532 |
if (strm->param.ec_enabled |
| 1533 |
#if !COREAUDIO_MAC |
| 1534 |
&& isMacCatalystApp |
| 1535 |
#endif |
| 1536 |
) { |
| 1537 |
/* When using VPIO on Mac, we need to use float data. Using |
| 1538 |
* signed integer will generate no errors, but strangely, |
| 1539 |
* no sound will be played. |
| 1540 |
*/ |
| 1541 |
streamFormat.mFormatFlags = kLinearPCMFormatFlagIsFloat | |
| 1542 |
kLinearPCMFormatFlagIsPacked; |
| 1543 |
streamFormat.mBitsPerChannel = sizeof(float) * 8; |
| 1544 |
streamFormat.mBytesPerFrame = streamFormat.mChannelsPerFrame * |
| 1545 |
streamFormat.mBitsPerChannel / 8; |
| 1546 |
streamFormat.mBytesPerPacket = streamFormat.mBytesPerFrame * |
| 1547 |
streamFormat.mFramesPerPacket; |
| 1548 |
} |
| 1549 |
|
| 1550 |
ostatus = AudioUnitSetProperty(*io_unit, |
| 1551 |
kAudioUnitProperty_StreamFormat, |
| 1552 |
kAudioUnitScope_Input, |
| 1553 |
0, |
| 1554 |
&streamFormat, |
| 1555 |
sizeof(streamFormat)); |
| 1556 |
if (ostatus != noErr) { |
| 1557 |
PJ_LOG(4, (THIS_FILE, |
| 1558 |
"Warning: cannot set playback stream format of dev %d", |
| 1559 |
dev_id)); |
| 1560 |
} |
| 1561 |
|
| 1562 |
/* Set render callback */ |
| 1563 |
output_cb.inputProc = output_renderer; |
| 1564 |
output_cb.inputProcRefCon = strm; |
| 1565 |
ostatus = AudioUnitSetProperty(*io_unit, |
| 1566 |
kAudioUnitProperty_SetRenderCallback, |
| 1567 |
kAudioUnitScope_Input, |
| 1568 |
0, |
| 1569 |
&output_cb, |
| 1570 |
sizeof(output_cb)); |
| 1571 |
if (ostatus != noErr) { |
| 1572 |
PJ_LOG(3, (THIS_FILE, "Failed setting render callback %d", |
| 1573 |
ostatus)); |
| 1574 |
return PJMEDIA_AUDIODEV_ERRNO_FROM_COREAUDIO(ostatus); |
| 1575 |
} |
| 1576 |
|
| 1577 |
/* Allocate playback buffer */ |
| 1578 |
strm->play_buf = (pj_int16_t*)pj_pool_alloc(strm->pool, |
| 1579 |
strm->param.samples_per_frame * |
| 1580 |
(strm->param.ec_enabled? 2: 1) * |
| 1581 |
strm->param.bits_per_sample >> 3); |
| 1582 |
if (!strm->play_buf) |
| 1583 |
return PJ_ENOMEM; |
| 1584 |
strm->play_buf_count = 0; |
| 1585 |
} |
| 1586 |
|
| 1587 |
if (dir & PJMEDIA_DIR_CAPTURE) { |
| 1588 |
AURenderCallbackStruct input_cb; |
| 1589 |
#if COREAUDIO_MAC |
| 1590 |
AudioBuffer *ab; |
| 1591 |
UInt32 size, buf_size; |
| 1592 |
#endif |
| 1593 |
|
| 1594 |
/* Set input callback */ |
| 1595 |
input_cb.inputProc = strm->resample ? resample_callback : |
| 1596 |
input_callback; |
| 1597 |
input_cb.inputProcRefCon = strm; |
| 1598 |
ostatus = AudioUnitSetProperty( |
| 1599 |
*io_unit, |
| 1600 |
kAudioOutputUnitProperty_SetInputCallback, |
| 1601 |
kAudioUnitScope_Global, |
| 1602 |
1, |
| 1603 |
&input_cb, |
| 1604 |
sizeof(input_cb)); |
| 1605 |
if (ostatus != noErr) { |
| 1606 |
PJ_LOG(3, (THIS_FILE, "Failed setting input callback %d", |
| 1607 |
ostatus)); |
| 1608 |
return PJMEDIA_AUDIODEV_ERRNO_FROM_COREAUDIO(ostatus); |
| 1609 |
} |
| 1610 |
|
| 1611 |
#if COREAUDIO_MAC |
| 1612 |
/* Set device's buffer frame size */ |
| 1613 |
size = sizeof(UInt32); |
| 1614 |
buf_size = (20 * strm->streamFormat.mSampleRate) / 1000; |
| 1615 |
ostatus = AudioUnitSetProperty (*io_unit, |
| 1616 |
kAudioDevicePropertyBufferFrameSize, |
| 1617 |
kAudioUnitScope_Global, |
| 1618 |
0, |
| 1619 |
&buf_size, |
| 1620 |
size); |
| 1621 |
if (ostatus != noErr) { |
| 1622 |
PJ_LOG(3, (THIS_FILE, "Failed setting buffer size %d", |
| 1623 |
ostatus)); |
| 1624 |
return PJMEDIA_AUDIODEV_ERRNO_FROM_COREAUDIO(ostatus); |
| 1625 |
} |
| 1626 |
|
| 1627 |
/* Get device's buffer frame size */ |
| 1628 |
ostatus = AudioUnitGetProperty(*io_unit, |
| 1629 |
kAudioDevicePropertyBufferFrameSize, |
| 1630 |
kAudioUnitScope_Global, |
| 1631 |
0, |
| 1632 |
&buf_size, |
| 1633 |
&size); |
| 1634 |
if (ostatus != noErr) { |
| 1635 |
PJ_LOG(3, (THIS_FILE, "Failed getting buffer size %d", |
| 1636 |
ostatus)); |
| 1637 |
return PJMEDIA_AUDIODEV_ERRNO_FROM_COREAUDIO(ostatus); |
| 1638 |
} |
| 1639 |
|
| 1640 |
/* Allocate audio buffer */ |
| 1641 |
strm->audio_buf = (AudioBufferList*)pj_pool_alloc(strm->pool, |
| 1642 |
sizeof(AudioBufferList) + sizeof(AudioBuffer)); |
| 1643 |
if (!strm->audio_buf) |
| 1644 |
return PJ_ENOMEM; |
| 1645 |
|
| 1646 |
strm->audio_buf->mNumberBuffers = 1; |
| 1647 |
ab = &strm->audio_buf->mBuffers[0]; |
| 1648 |
ab->mNumberChannels = strm->streamFormat.mChannelsPerFrame; |
| 1649 |
ab->mDataByteSize = buf_size * ab->mNumberChannels * |
| 1650 |
strm->streamFormat.mBitsPerChannel >> 3; |
| 1651 |
ab->mData = pj_pool_alloc(strm->pool, |
| 1652 |
ab->mDataByteSize); |
| 1653 |
if (!ab->mData) |
| 1654 |
return PJ_ENOMEM; |
| 1655 |
|
| 1656 |
#else |
| 1657 |
/* We will let AudioUnitRender() to allocate the buffer |
| 1658 |
* for us later |
| 1659 |
*/ |
| 1660 |
strm->audio_buf = (AudioBufferList*)pj_pool_alloc(strm->pool, |
| 1661 |
sizeof(AudioBufferList) + sizeof(AudioBuffer)); |
| 1662 |
if (!strm->audio_buf) |
| 1663 |
return PJ_ENOMEM; |
| 1664 |
|
| 1665 |
strm->audio_buf->mNumberBuffers = 1; |
| 1666 |
strm->audio_buf->mBuffers[0].mNumberChannels = |
| 1667 |
strm->streamFormat.mChannelsPerFrame; |
| 1668 |
|
| 1669 |
#endif |
| 1670 |
|
| 1671 |
/* Allocate recording buffer */ |
| 1672 |
strm->rec_buf = (pj_int16_t*)pj_pool_alloc(strm->pool, |
| 1673 |
strm->param.samples_per_frame * |
| 1674 |
strm->param.bits_per_sample >> 3); |
| 1675 |
if (!strm->rec_buf) |
| 1676 |
return PJ_ENOMEM; |
| 1677 |
strm->rec_buf_count = 0; |
| 1678 |
} |
| 1679 |
|
| 1680 |
/* Initialize the audio unit */ |
| 1681 |
ostatus = AudioUnitInitialize(*io_unit); |
| 1682 |
if (ostatus != noErr) { |
| 1683 |
PJ_LOG(3, (THIS_FILE, "Failed initializing audio unit %d", ostatus)); |
| 1684 |
return PJMEDIA_AUDIODEV_ERRNO_FROM_COREAUDIO(ostatus); |
| 1685 |
} |
| 1686 |
|
| 1687 |
return PJ_SUCCESS; |
| 1688 |
} |
| 1689 |
|
| 1690 |
/* API: create stream */ |
| 1691 |
static pj_status_t ca_factory_create_stream(pjmedia_aud_dev_factory *f, |
| 1692 |
const pjmedia_aud_param *param, |
| 1693 |
pjmedia_aud_rec_cb rec_cb, |
| 1694 |
pjmedia_aud_play_cb play_cb, |
| 1695 |
void *user_data, |
| 1696 |
pjmedia_aud_stream **p_aud_strm) |
| 1697 |
{ |
| 1698 |
struct coreaudio_factory *cf = (struct coreaudio_factory*)f; |
| 1699 |
pj_pool_t *pool; |
| 1700 |
struct coreaudio_stream *strm; |
| 1701 |
pj_status_t status; |
| 1702 |
|
| 1703 |
/* Create and Initialize stream descriptor */ |
| 1704 |
pool = pj_pool_create(cf->pf, "coreaudio-dev", 1000, 1000, NULL); |
| 1705 |
PJ_ASSERT_RETURN(pool != NULL, PJ_ENOMEM); |
| 1706 |
|
| 1707 |
strm = PJ_POOL_ZALLOC_T(pool, struct coreaudio_stream); |
| 1708 |
pj_list_init(&strm->list_entry); |
| 1709 |
strm->list_entry.stream = strm; |
| 1710 |
strm->cf = cf; |
| 1711 |
pj_memcpy(&strm->param, param, sizeof(*param)); |
| 1712 |
strm->pool = pool; |
| 1713 |
strm->rec_cb = rec_cb; |
| 1714 |
strm->play_cb = play_cb; |
| 1715 |
strm->user_data = user_data; |
| 1716 |
|
| 1717 |
/* Set the stream format */ |
| 1718 |
strm->streamFormat.mSampleRate = param->clock_rate; |
| 1719 |
strm->streamFormat.mFormatID = kAudioFormatLinearPCM; |
| 1720 |
strm->streamFormat.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger |
| 1721 |
| kLinearPCMFormatFlagIsPacked; |
| 1722 |
strm->streamFormat.mBitsPerChannel = strm->param.bits_per_sample; |
| 1723 |
strm->streamFormat.mChannelsPerFrame = param->channel_count; |
| 1724 |
strm->streamFormat.mBytesPerFrame = strm->streamFormat.mChannelsPerFrame |
| 1725 |
* strm->streamFormat.mBitsPerChannel |
| 1726 |
/ 8; |
| 1727 |
strm->streamFormat.mFramesPerPacket = 1; |
| 1728 |
strm->streamFormat.mBytesPerPacket = strm->streamFormat.mBytesPerFrame * |
| 1729 |
strm->streamFormat.mFramesPerPacket; |
| 1730 |
|
| 1731 |
/* Apply input/output routes settings before we create the audio units */ |
| 1732 |
if (param->flags & PJMEDIA_AUD_DEV_CAP_INPUT_ROUTE) { |
| 1733 |
ca_stream_set_cap(&strm->base, |
| 1734 |
PJMEDIA_AUD_DEV_CAP_INPUT_ROUTE, |
| 1735 |
¶m->input_route); |
| 1736 |
} |
| 1737 |
if (param->flags & PJMEDIA_AUD_DEV_CAP_OUTPUT_ROUTE) { |
| 1738 |
ca_stream_set_cap(&strm->base, |
| 1739 |
PJMEDIA_AUD_DEV_CAP_OUTPUT_ROUTE, |
| 1740 |
¶m->output_route); |
| 1741 |
} |
| 1742 |
if (param->flags & PJMEDIA_AUD_DEV_CAP_EC) { |
| 1743 |
#if COREAUDIO_MAC |
| 1744 |
/* Temporarily disable VPIO on Mac for stereo due to recording sound |
| 1745 |
* artefacts. |
| 1746 |
*/ |
| 1747 |
if (param->channel_count > 1) { |
| 1748 |
strm->param.ec_enabled = PJ_FALSE; |
| 1749 |
} |
| 1750 |
#endif |
| 1751 |
status = ca_stream_set_cap(&strm->base, |
| 1752 |
PJMEDIA_AUD_DEV_CAP_EC, |
| 1753 |
&strm->param.ec_enabled); |
| 1754 |
if (status != PJ_SUCCESS) |
| 1755 |
strm->param.ec_enabled = PJ_FALSE; |
| 1756 |
} else { |
| 1757 |
pj_bool_t ec = PJ_FALSE; |
| 1758 |
ca_stream_set_cap(&strm->base, |
| 1759 |
PJMEDIA_AUD_DEV_CAP_EC, &ec); |
| 1760 |
} |
| 1761 |
|
| 1762 |
#if !TARGET_OS_IPHONE |
| 1763 |
if (strm->param.ec_enabled && |
| 1764 |
param->rec_id != PJMEDIA_AUD_DEFAULT_CAPTURE_DEV && |
| 1765 |
param->play_id != PJMEDIA_AUD_DEFAULT_PLAYBACK_DEV) |
| 1766 |
{ |
| 1767 |
PJ_LOG(4, (THIS_FILE, "Warning: audio device id settings are " |
| 1768 |
"ignored when using VPIO")); |
| 1769 |
} |
| 1770 |
#endif |
| 1771 |
|
| 1772 |
strm->io_units[0] = strm->io_units[1] = NULL; |
| 1773 |
if ((param->dir == PJMEDIA_DIR_CAPTURE_PLAYBACK && |
| 1774 |
param->rec_id == param->play_id) || |
| 1775 |
(param->flags & PJMEDIA_AUD_DEV_CAP_EC && strm->param.ec_enabled)) |
| 1776 |
{ |
| 1777 |
/* If both input and output are on the same device or if EC is enabled, |
| 1778 |
* only create one audio unit to interface with the device(s). |
| 1779 |
*/ |
| 1780 |
status = create_audio_unit(cf->io_comp, |
| 1781 |
cf->dev_info[param->rec_id].dev_id, |
| 1782 |
param->dir, strm, &strm->io_units[0]); |
| 1783 |
if (status != PJ_SUCCESS) |
| 1784 |
goto on_error; |
| 1785 |
} else { |
| 1786 |
unsigned nunits = 0; |
| 1787 |
|
| 1788 |
if (param->dir & PJMEDIA_DIR_CAPTURE) { |
| 1789 |
status = create_audio_unit(cf->io_comp, |
| 1790 |
cf->dev_info[param->rec_id].dev_id, |
| 1791 |
PJMEDIA_DIR_CAPTURE, |
| 1792 |
strm, &strm->io_units[nunits++]); |
| 1793 |
if (status != PJ_SUCCESS) |
| 1794 |
goto on_error; |
| 1795 |
} |
| 1796 |
if (param->dir & PJMEDIA_DIR_PLAYBACK) { |
| 1797 |
|
| 1798 |
status = create_audio_unit(cf->io_comp, |
| 1799 |
cf->dev_info[param->play_id].dev_id, |
| 1800 |
PJMEDIA_DIR_PLAYBACK, |
| 1801 |
strm, &strm->io_units[nunits++]); |
| 1802 |
if (status != PJ_SUCCESS) |
| 1803 |
goto on_error; |
| 1804 |
} |
| 1805 |
} |
| 1806 |
|
| 1807 |
/* Apply the remaining settings */ |
| 1808 |
if (param->flags & PJMEDIA_AUD_DEV_CAP_INPUT_LATENCY) { |
| 1809 |
ca_stream_get_cap(&strm->base, |
| 1810 |
PJMEDIA_AUD_DEV_CAP_INPUT_LATENCY, |
| 1811 |
&strm->param.input_latency_ms); |
| 1812 |
} |
| 1813 |
if (param->flags & PJMEDIA_AUD_DEV_CAP_OUTPUT_LATENCY) { |
| 1814 |
ca_stream_get_cap(&strm->base, |
| 1815 |
PJMEDIA_AUD_DEV_CAP_OUTPUT_LATENCY, |
| 1816 |
&strm->param.output_latency_ms); |
| 1817 |
} |
| 1818 |
if (param->flags & PJMEDIA_AUD_DEV_CAP_OUTPUT_VOLUME_SETTING) { |
| 1819 |
ca_stream_set_cap(&strm->base, |
| 1820 |
PJMEDIA_AUD_DEV_CAP_OUTPUT_VOLUME_SETTING, |
| 1821 |
¶m->output_vol); |
| 1822 |
} |
| 1823 |
|
| 1824 |
pj_mutex_lock(strm->cf->mutex); |
| 1825 |
pj_assert(pj_list_empty(&strm->list_entry)); |
| 1826 |
pj_list_insert_after(&strm->cf->streams, &strm->list_entry); |
| 1827 |
pj_mutex_unlock(strm->cf->mutex); |
| 1828 |
|
| 1829 |
/* Done */ |
| 1830 |
strm->base.op = &stream_op; |
| 1831 |
*p_aud_strm = &strm->base; |
| 1832 |
|
| 1833 |
return PJ_SUCCESS; |
| 1834 |
|
| 1835 |
on_error: |
| 1836 |
ca_stream_destroy((pjmedia_aud_stream *)strm); |
| 1837 |
return status; |
| 1838 |
} |
| 1839 |
|
| 1840 |
/* API: Get stream info. */ |
| 1841 |
static pj_status_t ca_stream_get_param(pjmedia_aud_stream *s, |
| 1842 |
pjmedia_aud_param *pi) |
| 1843 |
{ |
| 1844 |
struct coreaudio_stream *strm = (struct coreaudio_stream*)s; |
| 1845 |
|
| 1846 |
PJ_ASSERT_RETURN(strm && pi, PJ_EINVAL); |
| 1847 |
|
| 1848 |
pj_memcpy(pi, &strm->param, sizeof(*pi)); |
| 1849 |
|
| 1850 |
/* Update the device capabilities' values */ |
| 1851 |
if (ca_stream_get_cap(s, PJMEDIA_AUD_DEV_CAP_INPUT_LATENCY, |
| 1852 |
&pi->input_latency_ms) == PJ_SUCCESS) |
| 1853 |
{ |
| 1854 |
pi->flags |= PJMEDIA_AUD_DEV_CAP_INPUT_LATENCY; |
| 1855 |
} |
| 1856 |
if (ca_stream_get_cap(s, PJMEDIA_AUD_DEV_CAP_OUTPUT_LATENCY, |
| 1857 |
&pi->output_latency_ms) == PJ_SUCCESS) |
| 1858 |
{ |
| 1859 |
pi->flags |= PJMEDIA_AUD_DEV_CAP_OUTPUT_LATENCY; |
| 1860 |
} |
| 1861 |
if (ca_stream_get_cap(s, PJMEDIA_AUD_DEV_CAP_OUTPUT_VOLUME_SETTING, |
| 1862 |
&pi->output_vol) == PJ_SUCCESS) |
| 1863 |
{ |
| 1864 |
pi->flags |= PJMEDIA_AUD_DEV_CAP_OUTPUT_VOLUME_SETTING; |
| 1865 |
} |
| 1866 |
if (ca_stream_get_cap(s, PJMEDIA_AUD_DEV_CAP_INPUT_ROUTE, |
| 1867 |
&pi->input_route) == PJ_SUCCESS) |
| 1868 |
{ |
| 1869 |
pi->flags |= PJMEDIA_AUD_DEV_CAP_INPUT_ROUTE; |
| 1870 |
} |
| 1871 |
if (ca_stream_get_cap(s, PJMEDIA_AUD_DEV_CAP_OUTPUT_ROUTE, |
| 1872 |
&pi->output_route) == PJ_SUCCESS) |
| 1873 |
{ |
| 1874 |
pi->flags |= PJMEDIA_AUD_DEV_CAP_OUTPUT_ROUTE; |
| 1875 |
} |
| 1876 |
if (ca_stream_get_cap(s, PJMEDIA_AUD_DEV_CAP_EC, |
| 1877 |
&pi->ec_enabled) == PJ_SUCCESS) |
| 1878 |
{ |
| 1879 |
pi->flags |= PJMEDIA_AUD_DEV_CAP_EC; |
| 1880 |
} |
| 1881 |
|
| 1882 |
return PJ_SUCCESS; |
| 1883 |
} |
| 1884 |
|
| 1885 |
/* API: get capability */ |
| 1886 |
static pj_status_t ca_stream_get_cap(pjmedia_aud_stream *s, |
| 1887 |
pjmedia_aud_dev_cap cap, |
| 1888 |
void *pval) |
| 1889 |
{ |
| 1890 |
struct coreaudio_stream *strm = (struct coreaudio_stream*)s; |
| 1891 |
|
| 1892 |
PJ_UNUSED_ARG(strm); |
| 1893 |
|
| 1894 |
PJ_ASSERT_RETURN(s && pval, PJ_EINVAL); |
| 1895 |
|
| 1896 |
if (cap==PJMEDIA_AUD_DEV_CAP_INPUT_LATENCY && |
| 1897 |
(strm->param.dir & PJMEDIA_DIR_CAPTURE)) |
| 1898 |
{ |
| 1899 |
#if COREAUDIO_MAC |
| 1900 |
UInt32 latency, size = sizeof(UInt32); |
| 1901 |
|
| 1902 |
/* Recording latency */ |
| 1903 |
if (AudioUnitGetProperty (strm->io_units[0], |
| 1904 |
kAudioDevicePropertyLatency, |
| 1905 |
kAudioUnitScope_Input, |
| 1906 |
1, |
| 1907 |
&latency, |
| 1908 |
&size) == noErr) |
| 1909 |
{ |
| 1910 |
UInt32 latency2; |
| 1911 |
if (AudioUnitGetProperty (strm->io_units[0], |
| 1912 |
kAudioDevicePropertyBufferFrameSize, |
| 1913 |
kAudioUnitScope_Input, |
| 1914 |
1, |
| 1915 |
&latency2, |
| 1916 |
&size) == noErr) |
| 1917 |
{ |
| 1918 |
strm->param.input_latency_ms = (latency + latency2) * 1000 / |
| 1919 |
strm->param.clock_rate; |
| 1920 |
strm->param.input_latency_ms++; |
| 1921 |
} |
| 1922 |
} |
| 1923 |
#else |
| 1924 |
if ([strm->sess respondsToSelector:@selector(inputLatency)]) { |
| 1925 |
strm->param.input_latency_ms = |
| 1926 |
(unsigned)(([strm->sess inputLatency] + |
| 1927 |
[strm->sess IOBufferDuration]) * 1000); |
| 1928 |
strm->param.input_latency_ms++; |
| 1929 |
} else |
| 1930 |
return PJMEDIA_EAUD_INVCAP; |
| 1931 |
#endif |
| 1932 |
|
| 1933 |
*(unsigned*)pval = strm->param.input_latency_ms; |
| 1934 |
return PJ_SUCCESS; |
| 1935 |
} else if (cap==PJMEDIA_AUD_DEV_CAP_OUTPUT_LATENCY && |
| 1936 |
(strm->param.dir & PJMEDIA_DIR_PLAYBACK)) |
| 1937 |
{ |
| 1938 |
#if COREAUDIO_MAC |
| 1939 |
UInt32 latency, size = sizeof(UInt32); |
| 1940 |
AudioUnit *io_unit = strm->io_units[1] ? &strm->io_units[1] : |
| 1941 |
&strm->io_units[0]; |
| 1942 |
|
| 1943 |
/* Playback latency */ |
| 1944 |
if (AudioUnitGetProperty (*io_unit, |
| 1945 |
kAudioDevicePropertyLatency, |
| 1946 |
kAudioUnitScope_Output, |
| 1947 |
0, |
| 1948 |
&latency, |
| 1949 |
&size) == noErr) |
| 1950 |
{ |
| 1951 |
UInt32 latency2; |
| 1952 |
if (AudioUnitGetProperty (*io_unit, |
| 1953 |
kAudioDevicePropertyBufferFrameSize, |
| 1954 |
kAudioUnitScope_Output, |
| 1955 |
0, |
| 1956 |
&latency2, |
| 1957 |
&size) == noErr) |
| 1958 |
{ |
| 1959 |
strm->param.output_latency_ms = (latency + latency2) * 1000 / |
| 1960 |
strm->param.clock_rate; |
| 1961 |
strm->param.output_latency_ms++; |
| 1962 |
} |
| 1963 |
} |
| 1964 |
#else |
| 1965 |
if ([strm->sess respondsToSelector:@selector(outputLatency)]) { |
| 1966 |
strm->param.output_latency_ms = |
| 1967 |
(unsigned)(([strm->sess outputLatency] + |
| 1968 |
[strm->sess IOBufferDuration]) * 1000); |
| 1969 |
strm->param.output_latency_ms++; |
| 1970 |
} else |
| 1971 |
return PJMEDIA_EAUD_INVCAP; |
| 1972 |
#endif |
| 1973 |
*(unsigned*)pval = (++strm->param.output_latency_ms * 2); |
| 1974 |
return PJ_SUCCESS; |
| 1975 |
} else if (cap==PJMEDIA_AUD_DEV_CAP_OUTPUT_VOLUME_SETTING && |
| 1976 |
(strm->param.dir & PJMEDIA_DIR_PLAYBACK)) |
| 1977 |
{ |
| 1978 |
#if COREAUDIO_MAC |
| 1979 |
OSStatus ostatus; |
| 1980 |
Float32 volume; |
| 1981 |
UInt32 size = sizeof(Float32); |
| 1982 |
|
| 1983 |
/* Output volume setting */ |
| 1984 |
ostatus = AudioUnitGetProperty (strm->io_units[1] ? strm->io_units[1] : |
| 1985 |
strm->io_units[0], |
| 1986 |
kAudioDevicePropertyVolumeScalar, |
| 1987 |
kAudioUnitScope_Output, |
| 1988 |
0, |
| 1989 |
&volume, |
| 1990 |
&size); |
| 1991 |
if (ostatus != noErr) |
| 1992 |
return PJMEDIA_AUDIODEV_ERRNO_FROM_COREAUDIO(ostatus); |
| 1993 |
|
| 1994 |
*(unsigned*)pval = (unsigned)(volume * 100); |
| 1995 |
return PJ_SUCCESS; |
| 1996 |
#else |
| 1997 |
if ([strm->sess respondsToSelector:@selector(outputVolume)]) { |
| 1998 |
*(unsigned*)pval = (unsigned)([strm->sess outputVolume] * 100); |
| 1999 |
return PJ_SUCCESS; |
| 2000 |
} else |
| 2001 |
return PJMEDIA_EAUD_INVCAP; |
| 2002 |
#endif |
| 2003 |
|
| 2004 |
#if !COREAUDIO_MAC |
| 2005 |
#if USE_AUDIO_SESSION_API != 0 |
| 2006 |
} else if (cap==PJMEDIA_AUD_DEV_CAP_INPUT_ROUTE && |
| 2007 |
(strm->param.dir & PJMEDIA_DIR_CAPTURE)) |
| 2008 |
{ |
| 2009 |
UInt32 btooth, size = sizeof(UInt32); |
| 2010 |
OSStatus ostatus; |
| 2011 |
|
| 2012 |
ostatus = AudioSessionGetProperty ( |
| 2013 |
kAudioSessionProperty_OverrideCategoryEnableBluetoothInput, |
| 2014 |
&size, &btooth); |
| 2015 |
if (ostatus != kAudioSessionNoError) { |
| 2016 |
return PJMEDIA_AUDIODEV_ERRNO_FROM_COREAUDIO(ostatus); |
| 2017 |
} |
| 2018 |
|
| 2019 |
*(pjmedia_aud_dev_route*)pval = btooth? |
| 2020 |
PJMEDIA_AUD_DEV_ROUTE_BLUETOOTH: |
| 2021 |
PJMEDIA_AUD_DEV_ROUTE_DEFAULT; |
| 2022 |
return PJ_SUCCESS; |
| 2023 |
} else if (cap==PJMEDIA_AUD_DEV_CAP_OUTPUT_ROUTE && |
| 2024 |
(strm->param.dir & PJMEDIA_DIR_PLAYBACK)) |
| 2025 |
{ |
| 2026 |
CFStringRef route; |
| 2027 |
UInt32 size = sizeof(CFStringRef); |
| 2028 |
OSStatus ostatus; |
| 2029 |
|
| 2030 |
ostatus = AudioSessionGetProperty (kAudioSessionProperty_AudioRoute, |
| 2031 |
&size, &route); |
| 2032 |
if (ostatus != kAudioSessionNoError) { |
| 2033 |
return PJMEDIA_AUDIODEV_ERRNO_FROM_COREAUDIO(ostatus); |
| 2034 |
} |
| 2035 |
|
| 2036 |
if (!route) { |
| 2037 |
*(pjmedia_aud_dev_route*)pval = PJMEDIA_AUD_DEV_ROUTE_DEFAULT; |
| 2038 |
} else if (CFStringHasPrefix(route, CFSTR("Headset"))) { |
| 2039 |
*(pjmedia_aud_dev_route*)pval = PJMEDIA_AUD_DEV_ROUTE_EARPIECE; |
| 2040 |
} else { |
| 2041 |
*(pjmedia_aud_dev_route*)pval = PJMEDIA_AUD_DEV_ROUTE_DEFAULT; |
| 2042 |
} |
| 2043 |
|
| 2044 |
CFRelease(route); |
| 2045 |
|
| 2046 |
return PJ_SUCCESS; |
| 2047 |
#endif |
| 2048 |
} else if (cap==PJMEDIA_AUD_DEV_CAP_EC) { |
| 2049 |
AudioComponentDescription desc; |
| 2050 |
OSStatus ostatus; |
| 2051 |
|
| 2052 |
ostatus = AudioComponentGetDescription(strm->cf->io_comp, &desc); |
| 2053 |
if (ostatus != noErr) { |
| 2054 |
return PJMEDIA_AUDIODEV_ERRNO_FROM_COREAUDIO(ostatus); |
| 2055 |
} |
| 2056 |
|
| 2057 |
*(pj_bool_t*)pval = (desc.componentSubType == |
| 2058 |
kAudioUnitSubType_VoiceProcessingIO); |
| 2059 |
return PJ_SUCCESS; |
| 2060 |
#endif |
| 2061 |
} else { |
| 2062 |
return PJMEDIA_EAUD_INVCAP; |
| 2063 |
} |
| 2064 |
} |
| 2065 |
|
| 2066 |
/* API: set capability */ |
| 2067 |
static pj_status_t ca_stream_set_cap(pjmedia_aud_stream *s, |
| 2068 |
pjmedia_aud_dev_cap cap, |
| 2069 |
const void *pval) |
| 2070 |
{ |
| 2071 |
struct coreaudio_stream *strm = (struct coreaudio_stream*)s; |
| 2072 |
|
| 2073 |
PJ_ASSERT_RETURN(s && pval, PJ_EINVAL); |
| 2074 |
|
| 2075 |
if (cap==PJMEDIA_AUD_DEV_CAP_EC) { |
| 2076 |
AudioComponentDescription desc; |
| 2077 |
AudioComponent io_comp; |
| 2078 |
|
| 2079 |
desc.componentType = kAudioUnitType_Output; |
| 2080 |
desc.componentSubType = (*(pj_bool_t*)pval)? |
| 2081 |
kAudioUnitSubType_VoiceProcessingIO : |
| 2082 |
#if COREAUDIO_MAC |
| 2083 |
kAudioUnitSubType_HALOutput; |
| 2084 |
#else |
| 2085 |
kAudioUnitSubType_RemoteIO; |
| 2086 |
#endif |
| 2087 |
desc.componentManufacturer = kAudioUnitManufacturer_Apple; |
| 2088 |
desc.componentFlags = 0; |
| 2089 |
desc.componentFlagsMask = 0; |
| 2090 |
|
| 2091 |
io_comp = AudioComponentFindNext(NULL, &desc); |
| 2092 |
if (io_comp == NULL) |
| 2093 |
return PJMEDIA_AUDIODEV_ERRNO_FROM_COREAUDIO(-1); |
| 2094 |
strm->cf->io_comp = io_comp; |
| 2095 |
strm->param.ec_enabled = *(pj_bool_t*)pval; |
| 2096 |
|
| 2097 |
PJ_LOG(4, (THIS_FILE, "Using %s audio unit", |
| 2098 |
(desc.componentSubType == |
| 2099 |
kAudioUnitSubType_VoiceProcessingIO? |
| 2100 |
"VoiceProcessingIO": "default"))); |
| 2101 |
|
| 2102 |
return PJ_SUCCESS; |
| 2103 |
} |
| 2104 |
#if COREAUDIO_MAC |
| 2105 |
else if (cap==PJMEDIA_AUD_DEV_CAP_OUTPUT_VOLUME_SETTING && |
| 2106 |
(strm->param.dir & PJMEDIA_DIR_PLAYBACK)) |
| 2107 |
{ |
| 2108 |
OSStatus ostatus; |
| 2109 |
Float32 volume = *(unsigned*)pval; |
| 2110 |
|
| 2111 |
/* Output volume setting */ |
| 2112 |
volume /= 100.0; |
| 2113 |
ostatus = AudioUnitSetProperty (strm->io_units[1] ? strm->io_units[1] : |
| 2114 |
strm->io_units[0], |
| 2115 |
kAudioDevicePropertyVolumeScalar, |
| 2116 |
kAudioUnitScope_Output, |
| 2117 |
0, |
| 2118 |
&volume, |
| 2119 |
sizeof(Float32)); |
| 2120 |
if (ostatus != noErr) { |
| 2121 |
return PJMEDIA_AUDIODEV_ERRNO_FROM_COREAUDIO(ostatus); |
| 2122 |
} |
| 2123 |
strm->param.output_vol = *(unsigned*)pval; |
| 2124 |
return PJ_SUCCESS; |
| 2125 |
} |
| 2126 |
|
| 2127 |
#else |
| 2128 |
else if ((cap==PJMEDIA_AUD_DEV_CAP_INPUT_LATENCY && |
| 2129 |
(strm->param.dir & PJMEDIA_DIR_CAPTURE)) || |
| 2130 |
(cap==PJMEDIA_AUD_DEV_CAP_OUTPUT_LATENCY && |
| 2131 |
(strm->param.dir & PJMEDIA_DIR_PLAYBACK))) |
| 2132 |
{ |
| 2133 |
NSTimeInterval duration = *(unsigned *)pval; |
| 2134 |
unsigned latency; |
| 2135 |
|
| 2136 |
/* For low-latency audio streaming, you can set this value to |
| 2137 |
* as low as 5 ms (the default is 23ms). However, lowering the |
| 2138 |
* latency may cause a decrease in audio quality. |
| 2139 |
*/ |
| 2140 |
duration /= 1000; |
| 2141 |
if ([strm->sess setPreferredIOBufferDuration:duration error:nil] |
| 2142 |
!= YES) |
| 2143 |
{ |
| 2144 |
PJ_LOG(4, (THIS_FILE, |
| 2145 |
"Error: cannot set the preferred buffer duration")); |
| 2146 |
return PJMEDIA_EAUD_INVOP; |
| 2147 |
} |
| 2148 |
|
| 2149 |
ca_stream_get_cap(s, PJMEDIA_AUD_DEV_CAP_INPUT_LATENCY, &latency); |
| 2150 |
ca_stream_get_cap(s, PJMEDIA_AUD_DEV_CAP_OUTPUT_LATENCY, &latency); |
| 2151 |
|
| 2152 |
return PJ_SUCCESS; |
| 2153 |
} |
| 2154 |
|
| 2155 |
#if USE_AUDIO_SESSION_API != 0 |
| 2156 |
|
| 2157 |
else if (cap==PJMEDIA_AUD_DEV_CAP_INPUT_ROUTE && |
| 2158 |
(strm->param.dir & PJMEDIA_DIR_CAPTURE)) |
| 2159 |
{ |
| 2160 |
UInt32 btooth = *(pjmedia_aud_dev_route*)pval == |
| 2161 |
PJMEDIA_AUD_DEV_ROUTE_BLUETOOTH ? 1 : 0; |
| 2162 |
OSStatus ostatus; |
| 2163 |
|
| 2164 |
ostatus = AudioSessionSetProperty ( |
| 2165 |
kAudioSessionProperty_OverrideCategoryEnableBluetoothInput, |
| 2166 |
sizeof(btooth), &btooth); |
| 2167 |
if (ostatus != kAudioSessionNoError) { |
| 2168 |
return PJMEDIA_AUDIODEV_ERRNO_FROM_COREAUDIO(ostatus); |
| 2169 |
} |
| 2170 |
strm->param.input_route = *(pjmedia_aud_dev_route*)pval; |
| 2171 |
return PJ_SUCCESS; |
| 2172 |
} else if (cap==PJMEDIA_AUD_DEV_CAP_OUTPUT_ROUTE && |
| 2173 |
(strm->param.dir & PJMEDIA_DIR_PLAYBACK)) |
| 2174 |
{ |
| 2175 |
OSStatus ostatus; |
| 2176 |
UInt32 route = *(pjmedia_aud_dev_route*)pval == |
| 2177 |
PJMEDIA_AUD_DEV_ROUTE_LOUDSPEAKER ? |
| 2178 |
kAudioSessionOverrideAudioRoute_Speaker : |
| 2179 |
kAudioSessionOverrideAudioRoute_None; |
| 2180 |
|
| 2181 |
ostatus = AudioSessionSetProperty ( |
| 2182 |
kAudioSessionProperty_OverrideAudioRoute, |
| 2183 |
sizeof(route), &route); |
| 2184 |
if (ostatus != kAudioSessionNoError) { |
| 2185 |
return PJMEDIA_AUDIODEV_ERRNO_FROM_COREAUDIO(ostatus); |
| 2186 |
} |
| 2187 |
strm->param.output_route = *(pjmedia_aud_dev_route*)pval; |
| 2188 |
return PJ_SUCCESS; |
| 2189 |
} |
| 2190 |
#endif |
| 2191 |
#endif |
| 2192 |
|
| 2193 |
return PJMEDIA_EAUD_INVCAP; |
| 2194 |
} |
| 2195 |
|
| 2196 |
/* API: Start stream. */ |
| 2197 |
static pj_status_t ca_stream_start(pjmedia_aud_stream *strm) |
| 2198 |
{ |
| 2199 |
struct coreaudio_stream *stream = (struct coreaudio_stream*)strm; |
| 2200 |
OSStatus ostatus; |
| 2201 |
UInt32 i; |
| 2202 |
|
| 2203 |
if (stream->running) |
| 2204 |
return PJ_SUCCESS; |
| 2205 |
|
| 2206 |
stream->quit_flag = 0; |
| 2207 |
stream->interrupted = PJ_FALSE; |
| 2208 |
stream->rec_buf_count = 0; |
| 2209 |
stream->play_buf_count = 0; |
| 2210 |
stream->resample_buf_count = 0; |
| 2211 |
|
| 2212 |
if (stream->resample) { |
| 2213 |
ostatus = AudioConverterReset(stream->resample); |
| 2214 |
if (ostatus != noErr) |
| 2215 |
return PJMEDIA_AUDIODEV_ERRNO_FROM_COREAUDIO(ostatus); |
| 2216 |
} |
| 2217 |
|
| 2218 |
#if !COREAUDIO_MAC |
| 2219 |
if ([stream->sess setActive:true error:nil] != YES) { |
| 2220 |
PJ_LOG(4, (THIS_FILE, "Warning: cannot activate audio session")); |
| 2221 |
} |
| 2222 |
#endif |
| 2223 |
|
| 2224 |
for (i = 0; i < 2; i++) { |
| 2225 |
if (stream->io_units[i] == NULL) break; |
| 2226 |
ostatus = AudioOutputUnitStart(stream->io_units[i]); |
| 2227 |
if (ostatus != noErr) { |
| 2228 |
if (i == 1) |
| 2229 |
AudioOutputUnitStop(stream->io_units[0]); |
| 2230 |
return PJMEDIA_AUDIODEV_ERRNO_FROM_COREAUDIO(ostatus); |
| 2231 |
} |
| 2232 |
} |
| 2233 |
|
| 2234 |
stream->running = PJ_TRUE; |
| 2235 |
|
| 2236 |
PJ_LOG(4, (THIS_FILE, "core audio stream started")); |
| 2237 |
|
| 2238 |
return PJ_SUCCESS; |
| 2239 |
} |
| 2240 |
|
| 2241 |
/* API: Stop stream. */ |
| 2242 |
static pj_status_t ca_stream_stop(pjmedia_aud_stream *strm) |
| 2243 |
{ |
| 2244 |
struct coreaudio_stream *stream = (struct coreaudio_stream*)strm; |
| 2245 |
OSStatus ostatus; |
| 2246 |
unsigned i; |
| 2247 |
int should_deactivate; |
| 2248 |
struct stream_list *it, *itBegin; |
| 2249 |
|
| 2250 |
if (!stream->running) |
| 2251 |
return PJ_SUCCESS; |
| 2252 |
|
| 2253 |
for (i = 0; i < 2; i++) { |
| 2254 |
if (stream->io_units[i] == NULL) break; |
| 2255 |
ostatus = AudioOutputUnitStop(stream->io_units[i]); |
| 2256 |
if (ostatus != noErr) { |
| 2257 |
if (i == 0 && stream->io_units[1]) |
| 2258 |
AudioOutputUnitStop(stream->io_units[1]); |
| 2259 |
return PJMEDIA_AUDIODEV_ERRNO_FROM_COREAUDIO(ostatus); |
| 2260 |
} |
| 2261 |
} |
| 2262 |
|
| 2263 |
/* Check whether we need to deactivate the audio session. */ |
| 2264 |
pj_mutex_lock(stream->cf->mutex); |
| 2265 |
pj_assert(!pj_list_empty(&stream->cf->streams)); |
| 2266 |
pj_assert(!pj_list_empty(&stream->list_entry)); |
| 2267 |
stream->running = PJ_FALSE; |
| 2268 |
should_deactivate = PJ_TRUE; |
| 2269 |
itBegin = &stream->cf->streams; |
| 2270 |
for (it = itBegin->next; it != itBegin; it = it->next) { |
| 2271 |
if (it->stream->running) { |
| 2272 |
should_deactivate = PJ_FALSE; |
| 2273 |
break; |
| 2274 |
} |
| 2275 |
} |
| 2276 |
pj_mutex_unlock(stream->cf->mutex); |
| 2277 |
|
| 2278 |
if (should_deactivate) { |
| 2279 |
#if !COREAUDIO_MAC && SETUP_AV_AUDIO_SESSION |
| 2280 |
if ([stream->sess |
| 2281 |
respondsToSelector:@selector(setActive:withOptions:error:)]) |
| 2282 |
{ |
| 2283 |
[stream->sess setActive:NO |
| 2284 |
withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation |
| 2285 |
error:nil]; |
| 2286 |
} else { |
| 2287 |
if ([stream->sess setActive:NO error:nil] != YES) { |
| 2288 |
PJ_LOG(4, (THIS_FILE, "Warning: cannot deactivate " |
| 2289 |
"audio session")); |
| 2290 |
} |
| 2291 |
} |
| 2292 |
#endif |
| 2293 |
} |
| 2294 |
|
| 2295 |
stream->quit_flag = 1; |
| 2296 |
stream->play_thread_initialized = 0; |
| 2297 |
stream->rec_thread_initialized = 0; |
| 2298 |
pj_bzero(stream->rec_thread_desc, sizeof(pj_thread_desc)); |
| 2299 |
pj_bzero(stream->play_thread_desc, sizeof(pj_thread_desc)); |
| 2300 |
|
| 2301 |
PJ_LOG(4, (THIS_FILE, "core audio stream stopped")); |
| 2302 |
|
| 2303 |
return PJ_SUCCESS; |
| 2304 |
} |
| 2305 |
|
| 2306 |
|
| 2307 |
/* API: Destroy stream. */ |
| 2308 |
static pj_status_t ca_stream_destroy(pjmedia_aud_stream *strm) |
| 2309 |
{ |
| 2310 |
struct coreaudio_stream *stream = (struct coreaudio_stream*)strm; |
| 2311 |
unsigned i; |
| 2312 |
|
| 2313 |
PJ_ASSERT_RETURN(stream != NULL, PJ_EINVAL); |
| 2314 |
|
| 2315 |
ca_stream_stop(strm); |
| 2316 |
|
| 2317 |
for (i = 0; i < 2; i++) { |
| 2318 |
if (stream->io_units[i]) { |
| 2319 |
AudioUnitUninitialize(stream->io_units[i]); |
| 2320 |
AudioComponentInstanceDispose(stream->io_units[i]); |
| 2321 |
stream->io_units[i] = NULL; |
| 2322 |
} |
| 2323 |
} |
| 2324 |
|
| 2325 |
if (stream->resample) |
| 2326 |
AudioConverterDispose(stream->resample); |
| 2327 |
|
| 2328 |
pj_mutex_lock(stream->cf->mutex); |
| 2329 |
if (!pj_list_empty(&stream->list_entry)) |
| 2330 |
pj_list_erase(&stream->list_entry); |
| 2331 |
pj_mutex_unlock(stream->cf->mutex); |
| 2332 |
|
| 2333 |
pj_pool_release(stream->pool); |
| 2334 |
|
| 2335 |
return PJ_SUCCESS; |
| 2336 |
} |
| 2337 |
|
| 2338 |
#endif /* PJMEDIA_AUDIO_DEV_HAS_COREAUDIO */ |