| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
#include <stdio.h> |
| 51 |
#include <string.h> |
| 52 |
#include <math.h> |
| 53 |
#include <fcntl.h> |
| 54 |
#include <sys/ioctl.h> |
| 55 |
#include <unistd.h> |
| 56 |
#include <pthread.h> |
| 57 |
#include <stdlib.h> |
| 58 |
#include <assert.h> |
| 59 |
#include <errno.h> |
| 60 |
#include <sys/types.h> |
| 61 |
#include <sys/stat.h> |
| 62 |
#include <sys/poll.h> |
| 63 |
#include <limits.h> |
| 64 |
#include <semaphore.h> |
| 65 |
|
| 66 |
#ifdef HAVE_SYS_SOUNDCARD_H |
| 67 |
# include <sys/soundcard.h> |
| 68 |
# ifdef __NetBSD__ |
| 69 |
# define DEVICE_NAME_BASE "/dev/audio" |
| 70 |
# else |
| 71 |
# define DEVICE_NAME_BASE "/dev/dsp" |
| 72 |
# endif |
| 73 |
#elif defined(HAVE_LINUX_SOUNDCARD_H) |
| 74 |
# include <linux/soundcard.h> |
| 75 |
# define DEVICE_NAME_BASE "/dev/dsp" |
| 76 |
#elif defined(HAVE_MACHINE_SOUNDCARD_H) |
| 77 |
# include <machine/soundcard.h> |
| 78 |
# define DEVICE_NAME_BASE "/dev/audio" |
| 79 |
#else |
| 80 |
# error No sound card header file |
| 81 |
#endif |
| 82 |
|
| 83 |
#include "portaudio.h" |
| 84 |
#include "pa_util.h" |
| 85 |
#include "pa_allocation.h" |
| 86 |
#include "pa_hostapi.h" |
| 87 |
#include "pa_stream.h" |
| 88 |
#include "pa_cpuload.h" |
| 89 |
#include "pa_process.h" |
| 90 |
#include "pa_unix_util.h" |
| 91 |
#include "pa_debugprint.h" |
| 92 |
|
| 93 |
static int sysErr_; |
| 94 |
static pthread_t mainThread_; |
| 95 |
|
| 96 |
|
| 97 |
#define ENSURE_(expr, code) \ |
| 98 |
do { \ |
| 99 |
if( UNLIKELY( (sysErr_ = (expr)) < 0 ) ) \ |
| 100 |
{ \ |
| 101 |
\ |
| 102 |
if( (code) == paUnanticipatedHostError && pthread_self() == mainThread_ ) \ |
| 103 |
{ \ |
| 104 |
PaUtil_SetLastHostErrorInfo( paOSS, sysErr_, strerror( errno ) ); \ |
| 105 |
} \ |
| 106 |
\ |
| 107 |
PaUtil_DebugPrint(( "Expression '" #expr "' failed in '" __FILE__ "', line: " STRINGIZE( __LINE__ ) "\n" )); \ |
| 108 |
result = (code); \ |
| 109 |
goto error; \ |
| 110 |
} \ |
| 111 |
} while( 0 ); |
| 112 |
|
| 113 |
#ifndef AFMT_S16_NE |
| 114 |
#define AFMT_S16_NE Get_AFMT_S16_NE() |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
static int Get_AFMT_S16_NE( void ) |
| 120 |
{ |
| 121 |
long testData = 1; |
| 122 |
char *ptr = (char *) &testData; |
| 123 |
int isLittle = ( *ptr == 1 ); |
| 124 |
return isLittle ? AFMT_S16_LE : AFMT_S16_BE; |
| 125 |
} |
| 126 |
#endif |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
typedef struct |
| 131 |
{ |
| 132 |
PaUtilHostApiRepresentation inheritedHostApiRep; |
| 133 |
PaUtilStreamInterface callbackStreamInterface; |
| 134 |
PaUtilStreamInterface blockingStreamInterface; |
| 135 |
|
| 136 |
PaUtilAllocationGroup *allocations; |
| 137 |
|
| 138 |
PaHostApiIndex hostApiIndex; |
| 139 |
} |
| 140 |
PaOSSHostApiRepresentation; |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
typedef struct |
| 150 |
{ |
| 151 |
int fd; |
| 152 |
const char *devName; |
| 153 |
int userChannelCount, hostChannelCount; |
| 154 |
int userInterleaved; |
| 155 |
void *buffer; |
| 156 |
PaSampleFormat userFormat, hostFormat; |
| 157 |
double latency; |
| 158 |
unsigned long hostFrames, numBufs; |
| 159 |
void **userBuffers; |
| 160 |
} PaOssStreamComponent; |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
typedef struct PaOssStream |
| 166 |
{ |
| 167 |
PaUtilStreamRepresentation streamRepresentation; |
| 168 |
PaUtilCpuLoadMeasurer cpuLoadMeasurer; |
| 169 |
PaUtilBufferProcessor bufferProcessor; |
| 170 |
|
| 171 |
PaUtilThreading threading; |
| 172 |
|
| 173 |
int sharedDevice; |
| 174 |
unsigned long framesPerHostBuffer; |
| 175 |
int triggered; |
| 176 |
|
| 177 |
int isActive; |
| 178 |
int isStopped; |
| 179 |
|
| 180 |
int lastPosPtr; |
| 181 |
double lastStreamBytes; |
| 182 |
|
| 183 |
int framesProcessed; |
| 184 |
|
| 185 |
double sampleRate; |
| 186 |
|
| 187 |
int callbackMode; |
| 188 |
volatile int callbackStop, callbackAbort; |
| 189 |
|
| 190 |
PaOssStreamComponent *capture, *playback; |
| 191 |
unsigned long pollTimeout; |
| 192 |
sem_t semaphore; |
| 193 |
} |
| 194 |
PaOssStream; |
| 195 |
|
| 196 |
typedef enum { |
| 197 |
StreamMode_In, |
| 198 |
StreamMode_Out |
| 199 |
} StreamMode; |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
static void Terminate( struct PaUtilHostApiRepresentation *hostApi ); |
| 204 |
static PaError IsFormatSupported( struct PaUtilHostApiRepresentation *hostApi, |
| 205 |
const PaStreamParameters *inputParameters, |
| 206 |
const PaStreamParameters *outputParameters, |
| 207 |
double sampleRate ); |
| 208 |
static PaError OpenStream( struct PaUtilHostApiRepresentation *hostApi, |
| 209 |
PaStream** s, |
| 210 |
const PaStreamParameters *inputParameters, |
| 211 |
const PaStreamParameters *outputParameters, |
| 212 |
double sampleRate, |
| 213 |
unsigned long framesPerBuffer, |
| 214 |
PaStreamFlags streamFlags, |
| 215 |
PaStreamCallback *streamCallback, |
| 216 |
void *userData ); |
| 217 |
static PaError CloseStream( PaStream* stream ); |
| 218 |
static PaError StartStream( PaStream *stream ); |
| 219 |
static PaError StopStream( PaStream *stream ); |
| 220 |
static PaError AbortStream( PaStream *stream ); |
| 221 |
static PaError IsStreamStopped( PaStream *s ); |
| 222 |
static PaError IsStreamActive( PaStream *stream ); |
| 223 |
static PaTime GetStreamTime( PaStream *stream ); |
| 224 |
static double GetStreamCpuLoad( PaStream* stream ); |
| 225 |
static PaError ReadStream( PaStream* stream, void *buffer, unsigned long frames ); |
| 226 |
static PaError WriteStream( PaStream* stream, const void *buffer, unsigned long frames ); |
| 227 |
static signed long GetStreamReadAvailable( PaStream* stream ); |
| 228 |
static signed long GetStreamWriteAvailable( PaStream* stream ); |
| 229 |
static PaError BuildDeviceList( PaOSSHostApiRepresentation *hostApi ); |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
PaError PaOSS_Initialize( PaUtilHostApiRepresentation **hostApi, PaHostApiIndex hostApiIndex ) |
| 242 |
{ |
| 243 |
PaError result = paNoError; |
| 244 |
PaOSSHostApiRepresentation *ossHostApi = NULL; |
| 245 |
|
| 246 |
PA_UNLESS( ossHostApi = (PaOSSHostApiRepresentation*)PaUtil_AllocateMemory( sizeof(PaOSSHostApiRepresentation) ), |
| 247 |
paInsufficientMemory ); |
| 248 |
PA_UNLESS( ossHostApi->allocations = PaUtil_CreateAllocationGroup(), paInsufficientMemory ); |
| 249 |
ossHostApi->hostApiIndex = hostApiIndex; |
| 250 |
|
| 251 |
|
| 252 |
*hostApi = &ossHostApi->inheritedHostApiRep; |
| 253 |
(*hostApi)->info.structVersion = 1; |
| 254 |
(*hostApi)->info.type = paOSS; |
| 255 |
(*hostApi)->info.name = "OSS"; |
| 256 |
(*hostApi)->Terminate = Terminate; |
| 257 |
(*hostApi)->OpenStream = OpenStream; |
| 258 |
(*hostApi)->IsFormatSupported = IsFormatSupported; |
| 259 |
|
| 260 |
PA_ENSURE( BuildDeviceList( ossHostApi ) ); |
| 261 |
|
| 262 |
PaUtil_InitializeStreamInterface( &ossHostApi->callbackStreamInterface, CloseStream, StartStream, |
| 263 |
StopStream, AbortStream, IsStreamStopped, IsStreamActive, |
| 264 |
GetStreamTime, GetStreamCpuLoad, |
| 265 |
PaUtil_DummyRead, PaUtil_DummyWrite, |
| 266 |
PaUtil_DummyGetReadAvailable, |
| 267 |
PaUtil_DummyGetWriteAvailable ); |
| 268 |
|
| 269 |
PaUtil_InitializeStreamInterface( &ossHostApi->blockingStreamInterface, CloseStream, StartStream, |
| 270 |
StopStream, AbortStream, IsStreamStopped, IsStreamActive, |
| 271 |
GetStreamTime, PaUtil_DummyGetCpuLoad, |
| 272 |
ReadStream, WriteStream, GetStreamReadAvailable, GetStreamWriteAvailable ); |
| 273 |
|
| 274 |
mainThread_ = pthread_self(); |
| 275 |
|
| 276 |
return result; |
| 277 |
|
| 278 |
error: |
| 279 |
if( ossHostApi ) |
| 280 |
{ |
| 281 |
if( ossHostApi->allocations ) |
| 282 |
{ |
| 283 |
PaUtil_FreeAllAllocations( ossHostApi->allocations ); |
| 284 |
PaUtil_DestroyAllocationGroup( ossHostApi->allocations ); |
| 285 |
} |
| 286 |
|
| 287 |
PaUtil_FreeMemory( ossHostApi ); |
| 288 |
} |
| 289 |
return result; |
| 290 |
} |
| 291 |
|
| 292 |
PaError PaUtil_InitializeDeviceInfo( PaDeviceInfo *deviceInfo, const char *name, PaHostApiIndex hostApiIndex, int maxInputChannels, |
| 293 |
int maxOutputChannels, PaTime defaultLowInputLatency, PaTime defaultLowOutputLatency, PaTime defaultHighInputLatency, |
| 294 |
PaTime defaultHighOutputLatency, double defaultSampleRate, PaUtilAllocationGroup *allocations ) |
| 295 |
{ |
| 296 |
PaError result = paNoError; |
| 297 |
|
| 298 |
deviceInfo->structVersion = 2; |
| 299 |
if( allocations ) |
| 300 |
{ |
| 301 |
size_t len = strlen( name ) + 1; |
| 302 |
PA_UNLESS( deviceInfo->name = PaUtil_GroupAllocateMemory( allocations, len ), paInsufficientMemory ); |
| 303 |
strncpy( (char *)deviceInfo->name, name, len ); |
| 304 |
} |
| 305 |
else |
| 306 |
deviceInfo->name = name; |
| 307 |
|
| 308 |
deviceInfo->hostApi = hostApiIndex; |
| 309 |
deviceInfo->maxInputChannels = maxInputChannels; |
| 310 |
deviceInfo->maxOutputChannels = maxOutputChannels; |
| 311 |
deviceInfo->defaultLowInputLatency = defaultLowInputLatency; |
| 312 |
deviceInfo->defaultLowOutputLatency = defaultLowOutputLatency; |
| 313 |
deviceInfo->defaultHighInputLatency = defaultHighInputLatency; |
| 314 |
deviceInfo->defaultHighOutputLatency = defaultHighOutputLatency; |
| 315 |
deviceInfo->defaultSampleRate = defaultSampleRate; |
| 316 |
|
| 317 |
error: |
| 318 |
return result; |
| 319 |
} |
| 320 |
|
| 321 |
static int CalcHigherLogTwo( int n ) |
| 322 |
{ |
| 323 |
int log2 = 0; |
| 324 |
while( (1<<log2) < n ) log2++; |
| 325 |
return log2; |
| 326 |
} |
| 327 |
|
| 328 |
static PaError QueryDirection( const char *deviceName, StreamMode mode, double *defaultSampleRate, int *maxChannelCount, |
| 329 |
double *defaultLowLatency, double *defaultHighLatency ) |
| 330 |
{ |
| 331 |
PaError result = paNoError; |
| 332 |
int numChannels, maxNumChannels; |
| 333 |
int busy = 0; |
| 334 |
int devHandle = -1; |
| 335 |
int sr; |
| 336 |
*maxChannelCount = 0; |
| 337 |
int temp, frgmt; |
| 338 |
unsigned long fragFrames; |
| 339 |
|
| 340 |
if ( (devHandle = open( deviceName, (mode == StreamMode_In ? O_RDONLY : O_WRONLY) | O_NONBLOCK )) < 0 ) |
| 341 |
{ |
| 342 |
if( errno == EBUSY || errno == EAGAIN ) |
| 343 |
{ |
| 344 |
PA_DEBUG(( "%s: Device %s busy\n", __FUNCTION__, deviceName )); |
| 345 |
} |
| 346 |
else |
| 347 |
{ |
| 348 |
|
| 349 |
if( errno != ENOENT ) |
| 350 |
{ |
| 351 |
PA_DEBUG(( "%s: Can't access device %s: %s\n", __FUNCTION__, deviceName, strerror( errno ) )); |
| 352 |
} |
| 353 |
} |
| 354 |
|
| 355 |
return paDeviceUnavailable; |
| 356 |
} |
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
maxNumChannels = 0; |
| 364 |
for( numChannels = 1; numChannels <= 16; numChannels++ ) |
| 365 |
{ |
| 366 |
temp = numChannels; |
| 367 |
if( ioctl( devHandle, SNDCTL_DSP_CHANNELS, &temp ) < 0 ) |
| 368 |
{ |
| 369 |
busy = EAGAIN == errno || EBUSY == errno; |
| 370 |
|
| 371 |
if( maxNumChannels >= 2 ) |
| 372 |
break; |
| 373 |
} |
| 374 |
else |
| 375 |
{ |
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
if( (numChannels > 2) && (temp != numChannels) ) |
| 380 |
break; |
| 381 |
if( temp > maxNumChannels ) |
| 382 |
maxNumChannels = temp; |
| 383 |
} |
| 384 |
} |
| 385 |
|
| 386 |
|
| 387 |
if( 0 == maxNumChannels && busy ) |
| 388 |
{ |
| 389 |
result = paDeviceUnavailable; |
| 390 |
goto error; |
| 391 |
} |
| 392 |
|
| 393 |
|
| 394 |
if( maxNumChannels < 1 ) |
| 395 |
{ |
| 396 |
int stereo = 1; |
| 397 |
if( ioctl( devHandle, SNDCTL_DSP_STEREO, &stereo ) < 0 ) |
| 398 |
{ |
| 399 |
maxNumChannels = 1; |
| 400 |
} |
| 401 |
else |
| 402 |
{ |
| 403 |
maxNumChannels = (stereo) ? 2 : 1; |
| 404 |
} |
| 405 |
PA_DEBUG(( "%s: use SNDCTL_DSP_STEREO, maxNumChannels = %d\n", __FUNCTION__, maxNumChannels )); |
| 406 |
} |
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
{ |
| 412 |
|
| 413 |
numChannels = PA_MIN( maxNumChannels, 2 ); |
| 414 |
ENSURE_( ioctl( devHandle, SNDCTL_DSP_CHANNELS, &numChannels ), paUnanticipatedHostError ); |
| 415 |
} |
| 416 |
|
| 417 |
|
| 418 |
if( *defaultSampleRate < 0 ) |
| 419 |
{ |
| 420 |
sr = 44100; |
| 421 |
ENSURE_( ioctl( devHandle, SNDCTL_DSP_SPEED, &sr ), paUnanticipatedHostError ); |
| 422 |
|
| 423 |
*defaultSampleRate = sr; |
| 424 |
} |
| 425 |
|
| 426 |
*maxChannelCount = maxNumChannels; |
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
fragFrames = 128; |
| 431 |
frgmt = (4 << 16) + (CalcHigherLogTwo( fragFrames * numChannels * 2 ) & 0xffff); |
| 432 |
ENSURE_( ioctl( devHandle, SNDCTL_DSP_SETFRAGMENT, &frgmt ), paUnanticipatedHostError ); |
| 433 |
|
| 434 |
|
| 435 |
fragFrames = pow( 2, frgmt & 0xffff ) / (numChannels * 2); |
| 436 |
*defaultLowLatency = ((frgmt >> 16) - 1) * fragFrames / *defaultSampleRate; |
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
temp = (fragFrames < 256) ? 4 : (fragFrames < 512) ? 2 : 1; |
| 441 |
*defaultHighLatency = temp * *defaultLowLatency; |
| 442 |
|
| 443 |
error: |
| 444 |
if( devHandle >= 0 ) |
| 445 |
close( devHandle ); |
| 446 |
|
| 447 |
return result; |
| 448 |
} |
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
static PaError QueryDevice( char *deviceName, PaOSSHostApiRepresentation *ossApi, PaDeviceInfo **deviceInfo ) |
| 458 |
{ |
| 459 |
PaError result = paNoError; |
| 460 |
double sampleRate = -1.; |
| 461 |
int maxInputChannels, maxOutputChannels; |
| 462 |
PaTime defaultLowInputLatency, defaultLowOutputLatency, defaultHighInputLatency, defaultHighOutputLatency; |
| 463 |
PaError tmpRes = paNoError; |
| 464 |
int busy = 0; |
| 465 |
*deviceInfo = NULL; |
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
if( (tmpRes = QueryDirection( deviceName, StreamMode_In, &sampleRate, &maxInputChannels, &defaultLowInputLatency, |
| 479 |
&defaultHighInputLatency )) != paNoError ) |
| 480 |
{ |
| 481 |
if( tmpRes != paDeviceUnavailable ) |
| 482 |
{ |
| 483 |
PA_DEBUG(( "%s: Querying device %s for capture failed!\n", __FUNCTION__, deviceName )); |
| 484 |
|
| 485 |
} |
| 486 |
++busy; |
| 487 |
} |
| 488 |
if( (tmpRes = QueryDirection( deviceName, StreamMode_Out, &sampleRate, &maxOutputChannels, &defaultLowOutputLatency, |
| 489 |
&defaultHighOutputLatency )) != paNoError ) |
| 490 |
{ |
| 491 |
if( tmpRes != paDeviceUnavailable ) |
| 492 |
{ |
| 493 |
PA_DEBUG(( "%s: Querying device %s for playback failed!\n", __FUNCTION__, deviceName )); |
| 494 |
|
| 495 |
} |
| 496 |
++busy; |
| 497 |
} |
| 498 |
assert( 0 <= busy && busy <= 2 ); |
| 499 |
if( 2 == busy ) |
| 500 |
{ |
| 501 |
result = paDeviceUnavailable; |
| 502 |
goto error; |
| 503 |
} |
| 504 |
|
| 505 |
PA_UNLESS( *deviceInfo = PaUtil_GroupAllocateMemory( ossApi->allocations, sizeof (PaDeviceInfo) ), paInsufficientMemory ); |
| 506 |
PA_ENSURE( PaUtil_InitializeDeviceInfo( *deviceInfo, deviceName, ossApi->hostApiIndex, maxInputChannels, maxOutputChannels, |
| 507 |
defaultLowInputLatency, defaultLowOutputLatency, defaultHighInputLatency, defaultHighOutputLatency, sampleRate, |
| 508 |
ossApi->allocations ) ); |
| 509 |
|
| 510 |
error: |
| 511 |
return result; |
| 512 |
} |
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
static PaError BuildDeviceList( PaOSSHostApiRepresentation *ossApi ) |
| 522 |
{ |
| 523 |
PaError result = paNoError; |
| 524 |
PaUtilHostApiRepresentation *commonApi = &ossApi->inheritedHostApiRep; |
| 525 |
int i; |
| 526 |
int numDevices = 0, maxDeviceInfos = 1; |
| 527 |
PaDeviceInfo **deviceInfos = NULL; |
| 528 |
|
| 529 |
|
| 530 |
commonApi->info.defaultInputDevice = paNoDevice; |
| 531 |
commonApi->info.defaultOutputDevice = paNoDevice; |
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
for( i = 0; i < 100; i++ ) |
| 539 |
{ |
| 540 |
char deviceName[32]; |
| 541 |
PaDeviceInfo *deviceInfo; |
| 542 |
int testResult; |
| 543 |
|
| 544 |
if( i == 0 ) |
| 545 |
snprintf(deviceName, sizeof (deviceName), "%s", DEVICE_NAME_BASE); |
| 546 |
else |
| 547 |
snprintf(deviceName, sizeof (deviceName), "%s%d", DEVICE_NAME_BASE, i); |
| 548 |
|
| 549 |
|
| 550 |
if( (testResult = QueryDevice( deviceName, ossApi, &deviceInfo )) != paNoError ) |
| 551 |
{ |
| 552 |
if( testResult != paDeviceUnavailable ) |
| 553 |
PA_ENSURE( testResult ); |
| 554 |
|
| 555 |
continue; |
| 556 |
} |
| 557 |
|
| 558 |
++numDevices; |
| 559 |
if( !deviceInfos || numDevices > maxDeviceInfos ) |
| 560 |
{ |
| 561 |
maxDeviceInfos *= 2; |
| 562 |
PA_UNLESS( deviceInfos = (PaDeviceInfo **) realloc( deviceInfos, maxDeviceInfos * sizeof (PaDeviceInfo *) ), |
| 563 |
paInsufficientMemory ); |
| 564 |
} |
| 565 |
{ |
| 566 |
int devIdx = numDevices - 1; |
| 567 |
deviceInfos[devIdx] = deviceInfo; |
| 568 |
|
| 569 |
if( commonApi->info.defaultInputDevice == paNoDevice && deviceInfo->maxInputChannels > 0 ) |
| 570 |
commonApi->info.defaultInputDevice = devIdx; |
| 571 |
if( commonApi->info.defaultOutputDevice == paNoDevice && deviceInfo->maxOutputChannels > 0 ) |
| 572 |
commonApi->info.defaultOutputDevice = devIdx; |
| 573 |
} |
| 574 |
} |
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
PA_DEBUG(("PaOSS %s: Total number of devices found: %d\n", __FUNCTION__, numDevices)); |
| 579 |
|
| 580 |
commonApi->deviceInfos = (PaDeviceInfo**)PaUtil_GroupAllocateMemory( |
| 581 |
ossApi->allocations, sizeof(PaDeviceInfo*) * numDevices ); |
| 582 |
memcpy( commonApi->deviceInfos, deviceInfos, numDevices * sizeof (PaDeviceInfo *) ); |
| 583 |
|
| 584 |
commonApi->info.deviceCount = numDevices; |
| 585 |
|
| 586 |
error: |
| 587 |
free( deviceInfos ); |
| 588 |
|
| 589 |
return result; |
| 590 |
} |
| 591 |
|
| 592 |
static void Terminate( struct PaUtilHostApiRepresentation *hostApi ) |
| 593 |
{ |
| 594 |
PaOSSHostApiRepresentation *ossHostApi = (PaOSSHostApiRepresentation*)hostApi; |
| 595 |
|
| 596 |
if( ossHostApi->allocations ) |
| 597 |
{ |
| 598 |
PaUtil_FreeAllAllocations( ossHostApi->allocations ); |
| 599 |
PaUtil_DestroyAllocationGroup( ossHostApi->allocations ); |
| 600 |
} |
| 601 |
|
| 602 |
PaUtil_FreeMemory( ossHostApi ); |
| 603 |
} |
| 604 |
|
| 605 |
static PaError IsFormatSupported( struct PaUtilHostApiRepresentation *hostApi, |
| 606 |
const PaStreamParameters *inputParameters, |
| 607 |
const PaStreamParameters *outputParameters, |
| 608 |
double sampleRate ) |
| 609 |
{ |
| 610 |
PaError result = paNoError; |
| 611 |
PaDeviceIndex device; |
| 612 |
PaDeviceInfo *deviceInfo; |
| 613 |
char *deviceName; |
| 614 |
int inputChannelCount, outputChannelCount; |
| 615 |
int tempDevHandle = -1; |
| 616 |
int flags; |
| 617 |
PaSampleFormat inputSampleFormat, outputSampleFormat; |
| 618 |
|
| 619 |
if( inputParameters ) |
| 620 |
{ |
| 621 |
inputChannelCount = inputParameters->channelCount; |
| 622 |
inputSampleFormat = inputParameters->sampleFormat; |
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
if( inputParameters->device == paUseHostApiSpecificDeviceSpecification ) |
| 628 |
return paInvalidDevice; |
| 629 |
|
| 630 |
|
| 631 |
if( inputChannelCount > hostApi->deviceInfos[ inputParameters->device ]->maxInputChannels ) |
| 632 |
return paInvalidChannelCount; |
| 633 |
|
| 634 |
|
| 635 |
if( inputParameters->hostApiSpecificStreamInfo ) |
| 636 |
return paIncompatibleHostApiSpecificStreamInfo; |
| 637 |
} |
| 638 |
else |
| 639 |
{ |
| 640 |
inputChannelCount = 0; |
| 641 |
} |
| 642 |
|
| 643 |
if( outputParameters ) |
| 644 |
{ |
| 645 |
outputChannelCount = outputParameters->channelCount; |
| 646 |
outputSampleFormat = outputParameters->sampleFormat; |
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
if( outputParameters->device == paUseHostApiSpecificDeviceSpecification ) |
| 652 |
return paInvalidDevice; |
| 653 |
|
| 654 |
|
| 655 |
if( outputChannelCount > hostApi->deviceInfos[ outputParameters->device ]->maxOutputChannels ) |
| 656 |
return paInvalidChannelCount; |
| 657 |
|
| 658 |
|
| 659 |
if( outputParameters->hostApiSpecificStreamInfo ) |
| 660 |
return paIncompatibleHostApiSpecificStreamInfo; |
| 661 |
} |
| 662 |
else |
| 663 |
{ |
| 664 |
outputChannelCount = 0; |
| 665 |
} |
| 666 |
|
| 667 |
if (inputChannelCount == 0 && outputChannelCount == 0) |
| 668 |
return paInvalidChannelCount; |
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
if (inputChannelCount > 0 && outputChannelCount > 0 && |
| 673 |
inputParameters->device != outputParameters->device) |
| 674 |
return paInvalidDevice; |
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
if (inputChannelCount > 0 && outputChannelCount > 0 && |
| 679 |
inputChannelCount != outputChannelCount) |
| 680 |
return paInvalidChannelCount; |
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
if( inputChannelCount > 0 ) |
| 685 |
{ |
| 686 |
result = PaUtil_DeviceIndexToHostApiDeviceIndex(&device, inputParameters->device, hostApi); |
| 687 |
if (result != paNoError) |
| 688 |
return result; |
| 689 |
} |
| 690 |
else |
| 691 |
{ |
| 692 |
result = PaUtil_DeviceIndexToHostApiDeviceIndex(&device, outputParameters->device, hostApi); |
| 693 |
if (result != paNoError) |
| 694 |
return result; |
| 695 |
} |
| 696 |
|
| 697 |
deviceInfo = hostApi->deviceInfos[device]; |
| 698 |
deviceName = (char *)deviceInfo->name; |
| 699 |
|
| 700 |
flags = O_NONBLOCK; |
| 701 |
if (inputChannelCount > 0 && outputChannelCount > 0) |
| 702 |
flags |= O_RDWR; |
| 703 |
else if (inputChannelCount > 0) |
| 704 |
flags |= O_RDONLY; |
| 705 |
else |
| 706 |
flags |= O_WRONLY; |
| 707 |
|
| 708 |
ENSURE_( tempDevHandle = open( deviceInfo->name, flags ), paDeviceUnavailable ); |
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
error: |
| 716 |
if( tempDevHandle >= 0 ) |
| 717 |
close( tempDevHandle ); |
| 718 |
|
| 719 |
return result; |
| 720 |
} |
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
static PaError ValidateParameters( const PaStreamParameters *parameters, const PaDeviceInfo *deviceInfo, StreamMode mode ) |
| 727 |
{ |
| 728 |
int maxChans; |
| 729 |
|
| 730 |
assert( parameters ); |
| 731 |
|
| 732 |
if( parameters->device == paUseHostApiSpecificDeviceSpecification ) |
| 733 |
{ |
| 734 |
return paInvalidDevice; |
| 735 |
} |
| 736 |
|
| 737 |
maxChans = (mode == StreamMode_In ? deviceInfo->maxInputChannels : |
| 738 |
deviceInfo->maxOutputChannels); |
| 739 |
if( parameters->channelCount > maxChans ) |
| 740 |
{ |
| 741 |
return paInvalidChannelCount; |
| 742 |
} |
| 743 |
|
| 744 |
return paNoError; |
| 745 |
} |
| 746 |
|
| 747 |
static PaError PaOssStreamComponent_Initialize( PaOssStreamComponent *component, const PaStreamParameters *parameters, |
| 748 |
int callbackMode, int fd, const char *deviceName ) |
| 749 |
{ |
| 750 |
PaError result = paNoError; |
| 751 |
assert( component ); |
| 752 |
|
| 753 |
memset( component, 0, sizeof (PaOssStreamComponent) ); |
| 754 |
|
| 755 |
component->fd = fd; |
| 756 |
component->devName = deviceName; |
| 757 |
component->userChannelCount = parameters->channelCount; |
| 758 |
component->userFormat = parameters->sampleFormat; |
| 759 |
component->latency = parameters->suggestedLatency; |
| 760 |
component->userInterleaved = !(parameters->sampleFormat & paNonInterleaved); |
| 761 |
|
| 762 |
if( !callbackMode && !component->userInterleaved ) |
| 763 |
{ |
| 764 |
|
| 765 |
PA_UNLESS( component->userBuffers = PaUtil_AllocateMemory( sizeof (void *) * component->userChannelCount ), |
| 766 |
paInsufficientMemory ); |
| 767 |
} |
| 768 |
|
| 769 |
error: |
| 770 |
return result; |
| 771 |
} |
| 772 |
|
| 773 |
static void PaOssStreamComponent_Terminate( PaOssStreamComponent *component ) |
| 774 |
{ |
| 775 |
assert( component ); |
| 776 |
|
| 777 |
if( component->fd >= 0 ) |
| 778 |
close( component->fd ); |
| 779 |
if( component->buffer ) |
| 780 |
PaUtil_FreeMemory( component->buffer ); |
| 781 |
|
| 782 |
if( component->userBuffers ) |
| 783 |
PaUtil_FreeMemory( component->userBuffers ); |
| 784 |
|
| 785 |
PaUtil_FreeMemory( component ); |
| 786 |
} |
| 787 |
|
| 788 |
static PaError ModifyBlocking( int fd, int blocking ) |
| 789 |
{ |
| 790 |
PaError result = paNoError; |
| 791 |
int fflags; |
| 792 |
|
| 793 |
ENSURE_( fflags = fcntl( fd, F_GETFL ), paUnanticipatedHostError ); |
| 794 |
|
| 795 |
if( blocking ) |
| 796 |
fflags &= ~O_NONBLOCK; |
| 797 |
else |
| 798 |
fflags |= O_NONBLOCK; |
| 799 |
|
| 800 |
ENSURE_( fcntl( fd, F_SETFL, fflags ), paUnanticipatedHostError ); |
| 801 |
|
| 802 |
error: |
| 803 |
return result; |
| 804 |
} |
| 805 |
|
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
static PaError OpenDevices( const char *idevName, const char *odevName, int *idev, int *odev ) |
| 812 |
{ |
| 813 |
PaError result = paNoError; |
| 814 |
int flags = O_NONBLOCK, duplex = 0; |
| 815 |
*idev = *odev = -1; |
| 816 |
|
| 817 |
if( idevName && odevName ) |
| 818 |
{ |
| 819 |
duplex = 1; |
| 820 |
flags |= O_RDWR; |
| 821 |
} |
| 822 |
else if( idevName ) |
| 823 |
flags |= O_RDONLY; |
| 824 |
else |
| 825 |
flags |= O_WRONLY; |
| 826 |
|
| 827 |
|
| 828 |
|
| 829 |
assert( flags & O_NONBLOCK ); |
| 830 |
if( idevName ) |
| 831 |
{ |
| 832 |
ENSURE_( *idev = open( idevName, flags ), paDeviceUnavailable ); |
| 833 |
PA_ENSURE( ModifyBlocking( *idev, 1 ) ); |
| 834 |
} |
| 835 |
if( odevName ) |
| 836 |
{ |
| 837 |
if( !idevName ) |
| 838 |
{ |
| 839 |
ENSURE_( *odev = open( odevName, flags ), paDeviceUnavailable ); |
| 840 |
PA_ENSURE( ModifyBlocking( *odev, 1 ) ); |
| 841 |
} |
| 842 |
else |
| 843 |
{ |
| 844 |
ENSURE_( *odev = dup( *idev ), paUnanticipatedHostError ); |
| 845 |
} |
| 846 |
} |
| 847 |
|
| 848 |
error: |
| 849 |
return result; |
| 850 |
} |
| 851 |
|
| 852 |
static PaError PaOssStream_Initialize( PaOssStream *stream, const PaStreamParameters *inputParameters, const PaStreamParameters *outputParameters, |
| 853 |
PaStreamCallback callback, void *userData, PaStreamFlags streamFlags, |
| 854 |
PaOSSHostApiRepresentation *ossApi ) |
| 855 |
{ |
| 856 |
PaError result = paNoError; |
| 857 |
int idev, odev; |
| 858 |
PaUtilHostApiRepresentation *hostApi = &ossApi->inheritedHostApiRep; |
| 859 |
const char *idevName = NULL, *odevName = NULL; |
| 860 |
|
| 861 |
assert( stream ); |
| 862 |
|
| 863 |
memset( stream, 0, sizeof (PaOssStream) ); |
| 864 |
stream->isStopped = 1; |
| 865 |
|
| 866 |
PA_ENSURE( PaUtil_InitializeThreading( &stream->threading ) ); |
| 867 |
|
| 868 |
if( inputParameters && outputParameters ) |
| 869 |
{ |
| 870 |
if( inputParameters->device == outputParameters->device ) |
| 871 |
stream->sharedDevice = 1; |
| 872 |
} |
| 873 |
|
| 874 |
if( inputParameters ) |
| 875 |
idevName = hostApi->deviceInfos[inputParameters->device]->name; |
| 876 |
if( outputParameters ) |
| 877 |
odevName = hostApi->deviceInfos[outputParameters->device]->name; |
| 878 |
PA_ENSURE( OpenDevices( idevName, odevName, &idev, &odev ) ); |
| 879 |
if( inputParameters ) |
| 880 |
{ |
| 881 |
PA_UNLESS( stream->capture = PaUtil_AllocateMemory( sizeof (PaOssStreamComponent) ), paInsufficientMemory ); |
| 882 |
PA_ENSURE( PaOssStreamComponent_Initialize( stream->capture, inputParameters, callback != NULL, idev, idevName ) ); |
| 883 |
} |
| 884 |
if( outputParameters ) |
| 885 |
{ |
| 886 |
PA_UNLESS( stream->playback = PaUtil_AllocateMemory( sizeof (PaOssStreamComponent) ), paInsufficientMemory ); |
| 887 |
PA_ENSURE( PaOssStreamComponent_Initialize( stream->playback, outputParameters, callback != NULL, odev, odevName ) ); |
| 888 |
} |
| 889 |
|
| 890 |
if( callback != NULL ) |
| 891 |
{ |
| 892 |
PaUtil_InitializeStreamRepresentation( &stream->streamRepresentation, |
| 893 |
&ossApi->callbackStreamInterface, callback, userData ); |
| 894 |
stream->callbackMode = 1; |
| 895 |
} |
| 896 |
else |
| 897 |
{ |
| 898 |
PaUtil_InitializeStreamRepresentation( &stream->streamRepresentation, |
| 899 |
&ossApi->blockingStreamInterface, callback, userData ); |
| 900 |
} |
| 901 |
|
| 902 |
ENSURE_( sem_init( &stream->semaphore, 0, 0 ), paInternalError ); |
| 903 |
|
| 904 |
error: |
| 905 |
return result; |
| 906 |
} |
| 907 |
|
| 908 |
static void PaOssStream_Terminate( PaOssStream *stream ) |
| 909 |
{ |
| 910 |
assert( stream ); |
| 911 |
|
| 912 |
PaUtil_TerminateStreamRepresentation( &stream->streamRepresentation ); |
| 913 |
PaUtil_TerminateThreading( &stream->threading ); |
| 914 |
|
| 915 |
if( stream->capture ) |
| 916 |
PaOssStreamComponent_Terminate( stream->capture ); |
| 917 |
if( stream->playback ) |
| 918 |
PaOssStreamComponent_Terminate( stream->playback ); |
| 919 |
|
| 920 |
sem_destroy( &stream->semaphore ); |
| 921 |
|
| 922 |
PaUtil_FreeMemory( stream ); |
| 923 |
} |
| 924 |
|
| 925 |
|
| 926 |
|
| 927 |
|
| 928 |
static PaError Pa2OssFormat( PaSampleFormat paFormat, int *ossFormat ) |
| 929 |
{ |
| 930 |
switch( paFormat ) |
| 931 |
{ |
| 932 |
case paUInt8: |
| 933 |
*ossFormat = AFMT_U8; |
| 934 |
break; |
| 935 |
case paInt8: |
| 936 |
*ossFormat = AFMT_S8; |
| 937 |
break; |
| 938 |
case paInt16: |
| 939 |
*ossFormat = AFMT_S16_NE; |
| 940 |
break; |
| 941 |
default: |
| 942 |
return paInternalError; |
| 943 |
} |
| 944 |
|
| 945 |
return paNoError; |
| 946 |
} |
| 947 |
|
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
static PaError GetAvailableFormats( PaOssStreamComponent *component, PaSampleFormat *availableFormats ) |
| 952 |
{ |
| 953 |
PaError result = paNoError; |
| 954 |
int mask = 0; |
| 955 |
PaSampleFormat frmts = 0; |
| 956 |
|
| 957 |
ENSURE_( ioctl( component->fd, SNDCTL_DSP_GETFMTS, &mask ), paUnanticipatedHostError ); |
| 958 |
if( mask & AFMT_U8 ) |
| 959 |
frmts |= paUInt8; |
| 960 |
if( mask & AFMT_S8 ) |
| 961 |
frmts |= paInt8; |
| 962 |
if( mask & AFMT_S16_NE ) |
| 963 |
frmts |= paInt16; |
| 964 |
else |
| 965 |
result = paSampleFormatNotSupported; |
| 966 |
|
| 967 |
*availableFormats = frmts; |
| 968 |
|
| 969 |
error: |
| 970 |
return result; |
| 971 |
} |
| 972 |
|
| 973 |
static unsigned int PaOssStreamComponent_FrameSize( PaOssStreamComponent *component ) |
| 974 |
{ |
| 975 |
return Pa_GetSampleSize( component->hostFormat ) * component->hostChannelCount; |
| 976 |
} |
| 977 |
|
| 978 |
|
| 979 |
|
| 980 |
|
| 981 |
static unsigned long PaOssStreamComponent_BufferSize( PaOssStreamComponent *component ) |
| 982 |
{ |
| 983 |
return PaOssStreamComponent_FrameSize( component ) * component->hostFrames * component->numBufs; |
| 984 |
} |
| 985 |
|
| 986 |
|
| 987 |
|
| 988 |
static PaError PaOssStreamComponent_Configure( PaOssStreamComponent *component, double sampleRate, unsigned long |
| 989 |
framesPerBuffer, StreamMode streamMode, PaOssStreamComponent *master ) |
| 990 |
{ |
| 991 |
PaError result = paNoError; |
| 992 |
int temp, nativeFormat; |
| 993 |
int sr = (int)sampleRate; |
| 994 |
PaSampleFormat availableFormats = 0, hostFormat = 0; |
| 995 |
int chans = component->userChannelCount; |
| 996 |
int frgmt; |
| 997 |
int numBufs; |
| 998 |
int bytesPerBuf; |
| 999 |
unsigned long bufSz; |
| 1000 |
unsigned long fragSz; |
| 1001 |
audio_buf_info bufInfo; |
| 1002 |
|
| 1003 |
|
| 1004 |
|
| 1005 |
if( !master ) |
| 1006 |
{ |
| 1007 |
|
| 1008 |
|
| 1009 |
|
| 1010 |
if( framesPerBuffer == paFramesPerBufferUnspecified ) |
| 1011 |
{ |
| 1012 |
|
| 1013 |
fragSz = (unsigned long)(component->latency * sampleRate / 3); |
| 1014 |
bufSz = fragSz * 4; |
| 1015 |
} |
| 1016 |
else |
| 1017 |
{ |
| 1018 |
fragSz = framesPerBuffer; |
| 1019 |
bufSz = (unsigned long)(component->latency * sampleRate) + fragSz; |
| 1020 |
} |
| 1021 |
|
| 1022 |
PA_ENSURE( GetAvailableFormats( component, &availableFormats ) ); |
| 1023 |
hostFormat = PaUtil_SelectClosestAvailableFormat( availableFormats, component->userFormat ); |
| 1024 |
|
| 1025 |
|
| 1026 |
numBufs = (int)PA_MAX( bufSz / fragSz, 2 ); |
| 1027 |
bytesPerBuf = PA_MAX( fragSz * Pa_GetSampleSize( hostFormat ) * chans, 16 ); |
| 1028 |
|
| 1029 |
|
| 1030 |
|
| 1031 |
|
| 1032 |
|
| 1033 |
frgmt = (numBufs << 16) + (CalcHigherLogTwo( bytesPerBuf ) & 0xffff); |
| 1034 |
ENSURE_( ioctl( component->fd, SNDCTL_DSP_SETFRAGMENT, &frgmt ), paUnanticipatedHostError ); |
| 1035 |
|
| 1036 |
|
| 1037 |
|
| 1038 |
|
| 1039 |
|
| 1040 |
PA_ENSURE( Pa2OssFormat( hostFormat, &temp ) ); |
| 1041 |
nativeFormat = temp; |
| 1042 |
ENSURE_( ioctl( component->fd, SNDCTL_DSP_SETFMT, &temp ), paUnanticipatedHostError ); |
| 1043 |
PA_UNLESS( temp == nativeFormat, paInternalError ); |
| 1044 |
|
| 1045 |
|
| 1046 |
ENSURE_( ioctl( component->fd, SNDCTL_DSP_CHANNELS, &chans ), paSampleFormatNotSupported ); |
| 1047 |
|
| 1048 |
PA_UNLESS( chans >= component->userChannelCount, paInvalidChannelCount ); |
| 1049 |
|
| 1050 |
|
| 1051 |
ENSURE_( ioctl( component->fd, SNDCTL_DSP_SPEED, &sr ), paInvalidSampleRate ); |
| 1052 |
|
| 1053 |
|
| 1054 |
if( (fabs( sampleRate - sr ) / sampleRate) > 0.01 ) |
| 1055 |
{ |
| 1056 |
PA_DEBUG(("%s: Wanted %f, closest sample rate was %d\n", __FUNCTION__, sampleRate, sr )); |
| 1057 |
PA_ENSURE( paInvalidSampleRate ); |
| 1058 |
} |
| 1059 |
|
| 1060 |
ENSURE_( ioctl( component->fd, streamMode == StreamMode_In ? SNDCTL_DSP_GETISPACE : SNDCTL_DSP_GETOSPACE, &bufInfo ), |
| 1061 |
paUnanticipatedHostError ); |
| 1062 |
component->numBufs = bufInfo.fragstotal; |
| 1063 |
|
| 1064 |
|
| 1065 |
ENSURE_( ioctl( component->fd, SNDCTL_DSP_GETBLKSIZE, &bytesPerBuf ), paUnanticipatedHostError ); |
| 1066 |
|
| 1067 |
component->hostFrames = bytesPerBuf / Pa_GetSampleSize( hostFormat ) / chans; |
| 1068 |
component->hostChannelCount = chans; |
| 1069 |
component->hostFormat = hostFormat; |
| 1070 |
} |
| 1071 |
else |
| 1072 |
{ |
| 1073 |
component->hostFormat = master->hostFormat; |
| 1074 |
component->hostFrames = master->hostFrames; |
| 1075 |
component->hostChannelCount = master->hostChannelCount; |
| 1076 |
component->numBufs = master->numBufs; |
| 1077 |
} |
| 1078 |
|
| 1079 |
PA_UNLESS( component->buffer = PaUtil_AllocateMemory( PaOssStreamComponent_BufferSize( component ) ), |
| 1080 |
paInsufficientMemory ); |
| 1081 |
|
| 1082 |
error: |
| 1083 |
return result; |
| 1084 |
} |
| 1085 |
|
| 1086 |
static PaError PaOssStreamComponent_Read( PaOssStreamComponent *component, unsigned long *frames ) |
| 1087 |
{ |
| 1088 |
PaError result = paNoError; |
| 1089 |
size_t len = *frames * PaOssStreamComponent_FrameSize( component ); |
| 1090 |
ssize_t bytesRead; |
| 1091 |
|
| 1092 |
ENSURE_( bytesRead = read( component->fd, component->buffer, len ), paUnanticipatedHostError ); |
| 1093 |
*frames = bytesRead / PaOssStreamComponent_FrameSize( component ); |
| 1094 |
|
| 1095 |
|
| 1096 |
error: |
| 1097 |
return result; |
| 1098 |
} |
| 1099 |
|
| 1100 |
static PaError PaOssStreamComponent_Write( PaOssStreamComponent *component, unsigned long *frames ) |
| 1101 |
{ |
| 1102 |
PaError result = paNoError; |
| 1103 |
size_t len = *frames * PaOssStreamComponent_FrameSize( component ); |
| 1104 |
ssize_t bytesWritten; |
| 1105 |
|
| 1106 |
ENSURE_( bytesWritten = write( component->fd, component->buffer, len ), paUnanticipatedHostError ); |
| 1107 |
*frames = bytesWritten / PaOssStreamComponent_FrameSize( component ); |
| 1108 |
|
| 1109 |
|
| 1110 |
error: |
| 1111 |
return result; |
| 1112 |
} |
| 1113 |
|
| 1114 |
|
| 1115 |
|
| 1116 |
|
| 1117 |
|
| 1118 |
|
| 1119 |
static PaError PaOssStream_Configure( PaOssStream *stream, double sampleRate, unsigned long framesPerBuffer, |
| 1120 |
double *inputLatency, double *outputLatency ) |
| 1121 |
{ |
| 1122 |
PaError result = paNoError; |
| 1123 |
int duplex = stream->capture && stream->playback; |
| 1124 |
unsigned long framesPerHostBuffer = 0; |
| 1125 |
|
| 1126 |
|
| 1127 |
if( duplex && stream->sharedDevice ) |
| 1128 |
ENSURE_( ioctl( stream->capture->fd, SNDCTL_DSP_SETDUPLEX, 0 ), paUnanticipatedHostError ); |
| 1129 |
|
| 1130 |
if( stream->capture ) |
| 1131 |
{ |
| 1132 |
PaOssStreamComponent *component = stream->capture; |
| 1133 |
PA_ENSURE( PaOssStreamComponent_Configure( component, sampleRate, framesPerBuffer, StreamMode_In, |
| 1134 |
NULL ) ); |
| 1135 |
|
| 1136 |
assert( component->hostChannelCount > 0 ); |
| 1137 |
assert( component->hostFrames > 0 ); |
| 1138 |
|
| 1139 |
*inputLatency = (component->hostFrames * (component->numBufs - 1)) / sampleRate; |
| 1140 |
} |
| 1141 |
if( stream->playback ) |
| 1142 |
{ |
| 1143 |
PaOssStreamComponent *component = stream->playback, *master = stream->sharedDevice ? stream->capture : NULL; |
| 1144 |
PA_ENSURE( PaOssStreamComponent_Configure( component, sampleRate, framesPerBuffer, StreamMode_Out, |
| 1145 |
master ) ); |
| 1146 |
|
| 1147 |
assert( component->hostChannelCount > 0 ); |
| 1148 |
assert( component->hostFrames > 0 ); |
| 1149 |
|
| 1150 |
*outputLatency = (component->hostFrames * (component->numBufs - 1)) / sampleRate; |
| 1151 |
} |
| 1152 |
|
| 1153 |
if( duplex ) |
| 1154 |
framesPerHostBuffer = PA_MIN( stream->capture->hostFrames, stream->playback->hostFrames ); |
| 1155 |
else if( stream->capture ) |
| 1156 |
framesPerHostBuffer = stream->capture->hostFrames; |
| 1157 |
else if( stream->playback ) |
| 1158 |
framesPerHostBuffer = stream->playback->hostFrames; |
| 1159 |
|
| 1160 |
stream->framesPerHostBuffer = framesPerHostBuffer; |
| 1161 |
stream->pollTimeout = (int) ceil( 1e6 * framesPerHostBuffer / sampleRate ); |
| 1162 |
|
| 1163 |
stream->sampleRate = stream->streamRepresentation.streamInfo.sampleRate = sampleRate; |
| 1164 |
|
| 1165 |
error: |
| 1166 |
return result; |
| 1167 |
} |
| 1168 |
|
| 1169 |
|
| 1170 |
|
| 1171 |
|
| 1172 |
|
| 1173 |
|
| 1174 |
|
| 1175 |
|
| 1176 |
|
| 1177 |
|
| 1178 |
|
| 1179 |
|
| 1180 |
|
| 1181 |
|
| 1182 |
|
| 1183 |
|
| 1184 |
static PaError OpenStream( struct PaUtilHostApiRepresentation *hostApi, |
| 1185 |
PaStream** s, |
| 1186 |
const PaStreamParameters *inputParameters, |
| 1187 |
const PaStreamParameters *outputParameters, |
| 1188 |
double sampleRate, |
| 1189 |
unsigned long framesPerBuffer, |
| 1190 |
PaStreamFlags streamFlags, |
| 1191 |
PaStreamCallback *streamCallback, |
| 1192 |
void *userData ) |
| 1193 |
{ |
| 1194 |
PaError result = paNoError; |
| 1195 |
PaOSSHostApiRepresentation *ossHostApi = (PaOSSHostApiRepresentation*)hostApi; |
| 1196 |
PaOssStream *stream = NULL; |
| 1197 |
int inputChannelCount = 0, outputChannelCount = 0; |
| 1198 |
PaSampleFormat inputSampleFormat = 0, outputSampleFormat = 0, inputHostFormat = 0, outputHostFormat = 0; |
| 1199 |
const PaDeviceInfo *inputDeviceInfo = 0, *outputDeviceInfo = 0; |
| 1200 |
int bpInitialized = 0; |
| 1201 |
double inLatency = 0., outLatency = 0.; |
| 1202 |
int i = 0; |
| 1203 |
|
| 1204 |
|
| 1205 |
if( (streamFlags & paPlatformSpecificFlags) != 0 ) |
| 1206 |
return paInvalidFlag; |
| 1207 |
|
| 1208 |
if( inputParameters ) |
| 1209 |
{ |
| 1210 |
|
| 1211 |
|
| 1212 |
inputDeviceInfo = hostApi->deviceInfos[inputParameters->device]; |
| 1213 |
PA_ENSURE( ValidateParameters( inputParameters, inputDeviceInfo, StreamMode_In ) ); |
| 1214 |
|
| 1215 |
inputChannelCount = inputParameters->channelCount; |
| 1216 |
inputSampleFormat = inputParameters->sampleFormat; |
| 1217 |
} |
| 1218 |
if( outputParameters ) |
| 1219 |
{ |
| 1220 |
outputDeviceInfo = hostApi->deviceInfos[outputParameters->device]; |
| 1221 |
PA_ENSURE( ValidateParameters( outputParameters, outputDeviceInfo, StreamMode_Out ) ); |
| 1222 |
|
| 1223 |
outputChannelCount = outputParameters->channelCount; |
| 1224 |
outputSampleFormat = outputParameters->sampleFormat; |
| 1225 |
} |
| 1226 |
|
| 1227 |
|
| 1228 |
|
| 1229 |
|
| 1230 |
if( inputChannelCount > 0 && outputChannelCount > 0 ) |
| 1231 |
{ |
| 1232 |
if( inputParameters->device == outputParameters->device ) |
| 1233 |
{ |
| 1234 |
if( inputParameters->channelCount != outputParameters->channelCount ) |
| 1235 |
return paInvalidChannelCount; |
| 1236 |
} |
| 1237 |
} |
| 1238 |
|
| 1239 |
|
| 1240 |
if( framesPerBuffer != paFramesPerBufferUnspecified ) |
| 1241 |
{ |
| 1242 |
framesPerBuffer &= INT_MAX; |
| 1243 |
for (i = 1; framesPerBuffer > i; i <<= 1) ; |
| 1244 |
framesPerBuffer = i; |
| 1245 |
} |
| 1246 |
|
| 1247 |
|
| 1248 |
PA_UNLESS( stream = (PaOssStream*)PaUtil_AllocateMemory( sizeof(PaOssStream) ), paInsufficientMemory ); |
| 1249 |
PA_ENSURE( PaOssStream_Initialize( stream, inputParameters, outputParameters, streamCallback, userData, streamFlags, ossHostApi ) ); |
| 1250 |
|
| 1251 |
PA_ENSURE( PaOssStream_Configure( stream, sampleRate, framesPerBuffer, &inLatency, &outLatency ) ); |
| 1252 |
|
| 1253 |
PaUtil_InitializeCpuLoadMeasurer( &stream->cpuLoadMeasurer, sampleRate ); |
| 1254 |
|
| 1255 |
if( inputParameters ) |
| 1256 |
{ |
| 1257 |
inputHostFormat = stream->capture->hostFormat; |
| 1258 |
stream->streamRepresentation.streamInfo.inputLatency = inLatency + |
| 1259 |
PaUtil_GetBufferProcessorInputLatencyFrames( &stream->bufferProcessor ) / sampleRate; |
| 1260 |
} |
| 1261 |
if( outputParameters ) |
| 1262 |
{ |
| 1263 |
outputHostFormat = stream->playback->hostFormat; |
| 1264 |
stream->streamRepresentation.streamInfo.outputLatency = outLatency + |
| 1265 |
PaUtil_GetBufferProcessorOutputLatencyFrames( &stream->bufferProcessor ) / sampleRate; |
| 1266 |
} |
| 1267 |
|
| 1268 |
|
| 1269 |
|
| 1270 |
|
| 1271 |
|
| 1272 |
PA_ENSURE( PaUtil_InitializeBufferProcessor( &stream->bufferProcessor, |
| 1273 |
inputChannelCount, inputSampleFormat, inputHostFormat, outputChannelCount, outputSampleFormat, |
| 1274 |
outputHostFormat, sampleRate, streamFlags, framesPerBuffer, stream->framesPerHostBuffer, |
| 1275 |
paUtilFixedHostBufferSize, streamCallback, userData ) ); |
| 1276 |
bpInitialized = 1; |
| 1277 |
|
| 1278 |
*s = (PaStream*)stream; |
| 1279 |
|
| 1280 |
return result; |
| 1281 |
|
| 1282 |
error: |
| 1283 |
if( bpInitialized ) |
| 1284 |
PaUtil_TerminateBufferProcessor( &stream->bufferProcessor ); |
| 1285 |
if( stream ) |
| 1286 |
PaOssStream_Terminate( stream ); |
| 1287 |
|
| 1288 |
return result; |
| 1289 |
} |
| 1290 |
|
| 1291 |
|
| 1292 |
|
| 1293 |
|
| 1294 |
|
| 1295 |
|
| 1296 |
|
| 1297 |
|
| 1298 |
|
| 1299 |
|
| 1300 |
|
| 1301 |
static PaError PaOssStream_WaitForFrames( PaOssStream *stream, unsigned long *frames ) |
| 1302 |
{ |
| 1303 |
PaError result = paNoError; |
| 1304 |
int pollPlayback = 0, pollCapture = 0; |
| 1305 |
int captureAvail = INT_MAX, playbackAvail = INT_MAX, commonAvail; |
| 1306 |
audio_buf_info bufInfo; |
| 1307 |
|
| 1308 |
fd_set readFds, writeFds; |
| 1309 |
int nfds = 0; |
| 1310 |
struct timeval selectTimeval = {0, 0}; |
| 1311 |
unsigned long timeout = stream->pollTimeout; |
| 1312 |
int captureFd = -1, playbackFd = -1; |
| 1313 |
|
| 1314 |
assert( stream ); |
| 1315 |
assert( frames ); |
| 1316 |
|
| 1317 |
if( stream->capture ) |
| 1318 |
{ |
| 1319 |
pollCapture = 1; |
| 1320 |
captureFd = stream->capture->fd; |
| 1321 |
|
| 1322 |
} |
| 1323 |
if( stream->playback ) |
| 1324 |
{ |
| 1325 |
pollPlayback = 1; |
| 1326 |
playbackFd = stream->playback->fd; |
| 1327 |
|
| 1328 |
} |
| 1329 |
|
| 1330 |
FD_ZERO( &readFds ); |
| 1331 |
FD_ZERO( &writeFds ); |
| 1332 |
|
| 1333 |
while( pollPlayback || pollCapture ) |
| 1334 |
{ |
| 1335 |
#ifdef PTHREAD_CANCELED |
| 1336 |
pthread_testcancel(); |
| 1337 |
#else |
| 1338 |
|
| 1339 |
if( stream->callbackStop || stream->callbackAbort ) |
| 1340 |
{ |
| 1341 |
PA_DEBUG(( "Cancelling PaOssStream_WaitForFrames\n" )); |
| 1342 |
(*frames) = 0; |
| 1343 |
return paNoError; |
| 1344 |
} |
| 1345 |
#endif |
| 1346 |
|
| 1347 |
|
| 1348 |
selectTimeval.tv_usec = timeout; |
| 1349 |
nfds = 0; |
| 1350 |
|
| 1351 |
if( pollCapture ) |
| 1352 |
{ |
| 1353 |
FD_SET( captureFd, &readFds ); |
| 1354 |
nfds = captureFd + 1; |
| 1355 |
} |
| 1356 |
if( pollPlayback ) |
| 1357 |
{ |
| 1358 |
FD_SET( playbackFd, &writeFds ); |
| 1359 |
nfds = PA_MAX( nfds, playbackFd + 1 ); |
| 1360 |
} |
| 1361 |
ENSURE_( select( nfds, &readFds, &writeFds, NULL, &selectTimeval ), paUnanticipatedHostError ); |
| 1362 |
|
| 1363 |
|
| 1364 |
|
| 1365 |
|
| 1366 |
|
| 1367 |
|
| 1368 |
|
| 1369 |
#ifdef PTHREAD_CANCELED |
| 1370 |
pthread_testcancel(); |
| 1371 |
#else |
| 1372 |
|
| 1373 |
if( stream->callbackStop || stream->callbackAbort ) |
| 1374 |
{ |
| 1375 |
PA_DEBUG(( "Cancelling PaOssStream_WaitForFrames\n" )); |
| 1376 |
(*frames) = 0; |
| 1377 |
return paNoError; |
| 1378 |
} |
| 1379 |
#endif |
| 1380 |
if( pollCapture ) |
| 1381 |
{ |
| 1382 |
if( FD_ISSET( captureFd, &readFds ) ) |
| 1383 |
{ |
| 1384 |
FD_CLR( captureFd, &readFds ); |
| 1385 |
pollCapture = 0; |
| 1386 |
} |
| 1387 |
|
| 1388 |
|
| 1389 |
|
| 1390 |
|
| 1391 |
|
| 1392 |
|
| 1393 |
|
| 1394 |
|
| 1395 |
else if( stream->playback ) |
| 1396 |
{ |
| 1397 |
|
| 1398 |
|
| 1399 |
} |
| 1400 |
} |
| 1401 |
if( pollPlayback ) |
| 1402 |
{ |
| 1403 |
if( FD_ISSET( playbackFd, &writeFds ) ) |
| 1404 |
{ |
| 1405 |
FD_CLR( playbackFd, &writeFds ); |
| 1406 |
pollPlayback = 0; |
| 1407 |
} |
| 1408 |
|
| 1409 |
|
| 1410 |
|
| 1411 |
|
| 1412 |
|
| 1413 |
|
| 1414 |
|
| 1415 |
else if( stream->capture ) |
| 1416 |
{ |
| 1417 |
|
| 1418 |
|
| 1419 |
} |
| 1420 |
} |
| 1421 |
} |
| 1422 |
|
| 1423 |
if( stream->capture ) |
| 1424 |
{ |
| 1425 |
ENSURE_( ioctl( captureFd, SNDCTL_DSP_GETISPACE, &bufInfo ), paUnanticipatedHostError ); |
| 1426 |
captureAvail = bufInfo.fragments * stream->capture->hostFrames; |
| 1427 |
if( !captureAvail ) |
| 1428 |
PA_DEBUG(( "%s: captureAvail: 0\n", __FUNCTION__ )); |
| 1429 |
|
| 1430 |
captureAvail = captureAvail == 0 ? INT_MAX : captureAvail; |
| 1431 |
} |
| 1432 |
if( stream->playback ) |
| 1433 |
{ |
| 1434 |
ENSURE_( ioctl( playbackFd, SNDCTL_DSP_GETOSPACE, &bufInfo ), paUnanticipatedHostError ); |
| 1435 |
playbackAvail = bufInfo.fragments * stream->playback->hostFrames; |
| 1436 |
if( !playbackAvail ) |
| 1437 |
{ |
| 1438 |
PA_DEBUG(( "%s: playbackAvail: 0\n", __FUNCTION__ )); |
| 1439 |
} |
| 1440 |
|
| 1441 |
playbackAvail = playbackAvail == 0 ? INT_MAX : playbackAvail; |
| 1442 |
} |
| 1443 |
|
| 1444 |
commonAvail = PA_MIN( captureAvail, playbackAvail ); |
| 1445 |
if( commonAvail == INT_MAX ) |
| 1446 |
commonAvail = 0; |
| 1447 |
commonAvail -= commonAvail % stream->framesPerHostBuffer; |
| 1448 |
|
| 1449 |
assert( commonAvail != INT_MAX ); |
| 1450 |
assert( commonAvail >= 0 ); |
| 1451 |
*frames = commonAvail; |
| 1452 |
|
| 1453 |
error: |
| 1454 |
return result; |
| 1455 |
} |
| 1456 |
|
| 1457 |
|
| 1458 |
|
| 1459 |
|
| 1460 |
|
| 1461 |
static PaError PaOssStream_Prepare( PaOssStream *stream ) |
| 1462 |
{ |
| 1463 |
PaError result = paNoError; |
| 1464 |
int enableBits = 0; |
| 1465 |
|
| 1466 |
if( stream->triggered ) |
| 1467 |
return result; |
| 1468 |
|
| 1469 |
|
| 1470 |
if( stream->playback ) |
| 1471 |
ENSURE_( ioctl( stream->playback->fd, SNDCTL_DSP_SETTRIGGER, &enableBits ), paUnanticipatedHostError ); |
| 1472 |
if( stream->capture ) |
| 1473 |
ENSURE_( ioctl( stream->capture->fd, SNDCTL_DSP_SETTRIGGER, &enableBits ), paUnanticipatedHostError ); |
| 1474 |
|
| 1475 |
if( stream->playback ) |
| 1476 |
{ |
| 1477 |
size_t bufSz = PaOssStreamComponent_BufferSize( stream->playback ); |
| 1478 |
memset( stream->playback->buffer, 0, bufSz ); |
| 1479 |
|
| 1480 |
|
| 1481 |
|
| 1482 |
PA_ENSURE( ModifyBlocking( stream->playback->fd, 0 ) ); |
| 1483 |
while (1) |
| 1484 |
{ |
| 1485 |
if( write( stream->playback->fd, stream->playback->buffer, bufSz ) < 0 ) |
| 1486 |
break; |
| 1487 |
} |
| 1488 |
PA_ENSURE( ModifyBlocking( stream->playback->fd, 1 ) ); |
| 1489 |
} |
| 1490 |
|
| 1491 |
if( stream->sharedDevice ) |
| 1492 |
{ |
| 1493 |
enableBits = PCM_ENABLE_INPUT | PCM_ENABLE_OUTPUT; |
| 1494 |
ENSURE_( ioctl( stream->capture->fd, SNDCTL_DSP_SETTRIGGER, &enableBits ), paUnanticipatedHostError ); |
| 1495 |
} |
| 1496 |
else |
| 1497 |
{ |
| 1498 |
if( stream->capture ) |
| 1499 |
{ |
| 1500 |
enableBits = PCM_ENABLE_INPUT; |
| 1501 |
ENSURE_( ioctl( stream->capture->fd, SNDCTL_DSP_SETTRIGGER, &enableBits ), paUnanticipatedHostError ); |
| 1502 |
} |
| 1503 |
if( stream->playback ) |
| 1504 |
{ |
| 1505 |
enableBits = PCM_ENABLE_OUTPUT; |
| 1506 |
ENSURE_( ioctl( stream->playback->fd, SNDCTL_DSP_SETTRIGGER, &enableBits ), paUnanticipatedHostError ); |
| 1507 |
} |
| 1508 |
} |
| 1509 |
|
| 1510 |
|
| 1511 |
stream->triggered = 1; |
| 1512 |
|
| 1513 |
error: |
| 1514 |
return result; |
| 1515 |
} |
| 1516 |
|
| 1517 |
|
| 1518 |
|
| 1519 |
|
| 1520 |
static PaError PaOssStream_Stop( PaOssStream *stream, int abort ) |
| 1521 |
{ |
| 1522 |
PaError result = paNoError; |
| 1523 |
|
| 1524 |
|
| 1525 |
|
| 1526 |
|
| 1527 |
int captureErr = 0, playbackErr = 0; |
| 1528 |
if( stream->capture ) |
| 1529 |
{ |
| 1530 |
if( (captureErr = ioctl( stream->capture->fd, SNDCTL_DSP_POST, 0 )) < 0 ) |
| 1531 |
{ |
| 1532 |
PA_DEBUG(( "%s: Failed to stop capture device, error: %d\n", __FUNCTION__, captureErr )); |
| 1533 |
} |
| 1534 |
} |
| 1535 |
if( stream->playback && !stream->sharedDevice ) |
| 1536 |
{ |
| 1537 |
if( (playbackErr = ioctl( stream->playback->fd, SNDCTL_DSP_POST, 0 )) < 0 ) |
| 1538 |
{ |
| 1539 |
PA_DEBUG(( "%s: Failed to stop playback device, error: %d\n", __FUNCTION__, playbackErr )); |
| 1540 |
} |
| 1541 |
} |
| 1542 |
|
| 1543 |
if( captureErr || playbackErr ) |
| 1544 |
{ |
| 1545 |
result = paUnanticipatedHostError; |
| 1546 |
} |
| 1547 |
|
| 1548 |
return result; |
| 1549 |
} |
| 1550 |
|
| 1551 |
|
| 1552 |
|
| 1553 |
|
| 1554 |
|
| 1555 |
static void OnExit( void *data ) |
| 1556 |
{ |
| 1557 |
PaOssStream *stream = (PaOssStream *) data; |
| 1558 |
assert( data ); |
| 1559 |
|
| 1560 |
PaUtil_ResetCpuLoadMeasurer( &stream->cpuLoadMeasurer ); |
| 1561 |
|
| 1562 |
PaOssStream_Stop( stream, stream->callbackAbort ); |
| 1563 |
|
| 1564 |
PA_DEBUG(( "OnExit: Stoppage\n" )); |
| 1565 |
|
| 1566 |
|
| 1567 |
if( stream->streamRepresentation.streamFinishedCallback ) |
| 1568 |
stream->streamRepresentation.streamFinishedCallback( stream->streamRepresentation.userData ); |
| 1569 |
|
| 1570 |
stream->callbackAbort = 0; |
| 1571 |
stream->isActive = 0; |
| 1572 |
} |
| 1573 |
|
| 1574 |
static PaError SetUpBuffers( PaOssStream *stream, unsigned long framesAvail ) |
| 1575 |
{ |
| 1576 |
PaError result = paNoError; |
| 1577 |
|
| 1578 |
if( stream->capture ) |
| 1579 |
{ |
| 1580 |
PaUtil_SetInterleavedInputChannels( &stream->bufferProcessor, 0, stream->capture->buffer, |
| 1581 |
stream->capture->hostChannelCount ); |
| 1582 |
PaUtil_SetInputFrameCount( &stream->bufferProcessor, framesAvail ); |
| 1583 |
} |
| 1584 |
if( stream->playback ) |
| 1585 |
{ |
| 1586 |
PaUtil_SetInterleavedOutputChannels( &stream->bufferProcessor, 0, stream->playback->buffer, |
| 1587 |
stream->playback->hostChannelCount ); |
| 1588 |
PaUtil_SetOutputFrameCount( &stream->bufferProcessor, framesAvail ); |
| 1589 |
} |
| 1590 |
|
| 1591 |
return result; |
| 1592 |
} |
| 1593 |
|
| 1594 |
|
| 1595 |
|
| 1596 |
|
| 1597 |
|
| 1598 |
|
| 1599 |
|
| 1600 |
|
| 1601 |
static void *PaOSS_AudioThreadProc( void *userData ) |
| 1602 |
{ |
| 1603 |
PaError result = paNoError; |
| 1604 |
PaOssStream *stream = (PaOssStream*)userData; |
| 1605 |
unsigned long framesAvail = 0, framesProcessed = 0; |
| 1606 |
int callbackResult = paContinue; |
| 1607 |
int triggered = stream->triggered; |
| 1608 |
int initiateProcessing = triggered; |
| 1609 |
PaStreamCallbackFlags cbFlags = 0; |
| 1610 |
PaStreamCallbackTimeInfo timeInfo = {0,0,0}; |
| 1611 |
|
| 1612 |
|
| 1613 |
|
| 1614 |
|
| 1615 |
|
| 1616 |
|
| 1617 |
|
| 1618 |
assert( stream ); |
| 1619 |
|
| 1620 |
pthread_cleanup_push( &OnExit, stream ); |
| 1621 |
|
| 1622 |
|
| 1623 |
|
| 1624 |
|
| 1625 |
|
| 1626 |
PA_ENSURE( PaOssStream_Prepare( stream ) ); |
| 1627 |
|
| 1628 |
|
| 1629 |
if( initiateProcessing ) |
| 1630 |
{ |
| 1631 |
|
| 1632 |
if( stream->capture ) |
| 1633 |
ModifyBlocking( stream->capture->fd, 1 ); |
| 1634 |
if( stream->playback ) |
| 1635 |
ModifyBlocking( stream->playback->fd, 1 ); |
| 1636 |
} |
| 1637 |
|
| 1638 |
while( 1 ) |
| 1639 |
{ |
| 1640 |
#ifdef PTHREAD_CANCELED |
| 1641 |
pthread_testcancel(); |
| 1642 |
#else |
| 1643 |
if( stream->callbackAbort ) |
| 1644 |
{ |
| 1645 |
PA_DEBUG(( "Aborting callback thread\n" )); |
| 1646 |
break; |
| 1647 |
} |
| 1648 |
#endif |
| 1649 |
if( stream->callbackStop && callbackResult == paContinue ) |
| 1650 |
{ |
| 1651 |
PA_DEBUG(( "Setting callbackResult to paComplete\n" )); |
| 1652 |
callbackResult = paComplete; |
| 1653 |
} |
| 1654 |
|
| 1655 |
|
| 1656 |
|
| 1657 |
|
| 1658 |
|
| 1659 |
|
| 1660 |
if( !initiateProcessing ) |
| 1661 |
{ |
| 1662 |
|
| 1663 |
PA_ENSURE( PaOssStream_WaitForFrames( stream, &framesAvail ) ); |
| 1664 |
assert( framesAvail % stream->framesPerHostBuffer == 0 ); |
| 1665 |
} |
| 1666 |
else |
| 1667 |
{ |
| 1668 |
framesAvail = stream->framesPerHostBuffer; |
| 1669 |
} |
| 1670 |
|
| 1671 |
while( framesAvail > 0 ) |
| 1672 |
{ |
| 1673 |
unsigned long frames = framesAvail; |
| 1674 |
|
| 1675 |
#ifdef PTHREAD_CANCELED |
| 1676 |
pthread_testcancel(); |
| 1677 |
#else |
| 1678 |
if( stream->callbackStop ) |
| 1679 |
{ |
| 1680 |
PA_DEBUG(( "Setting callbackResult to paComplete\n" )); |
| 1681 |
callbackResult = paComplete; |
| 1682 |
} |
| 1683 |
|
| 1684 |
if( stream->callbackAbort ) |
| 1685 |
{ |
| 1686 |
PA_DEBUG(( "Aborting callback thread\n" )); |
| 1687 |
break; |
| 1688 |
} |
| 1689 |
#endif |
| 1690 |
PaUtil_BeginCpuLoadMeasurement( &stream->cpuLoadMeasurer ); |
| 1691 |
|
| 1692 |
|
| 1693 |
if ( stream->capture ) |
| 1694 |
{ |
| 1695 |
PA_ENSURE( PaOssStreamComponent_Read( stream->capture, &frames ) ); |
| 1696 |
if( frames < framesAvail ) |
| 1697 |
{ |
| 1698 |
PA_DEBUG(( "Read %lu less frames than requested\n", framesAvail - frames )); |
| 1699 |
framesAvail = frames; |
| 1700 |
} |
| 1701 |
} |
| 1702 |
|
| 1703 |
#if ( SOUND_VERSION >= 0x030904 ) |
| 1704 |
|
| 1705 |
|
| 1706 |
|
| 1707 |
|
| 1708 |
|
| 1709 |
|
| 1710 |
|
| 1711 |
|
| 1712 |
|
| 1713 |
|
| 1714 |
|
| 1715 |
|
| 1716 |
|
| 1717 |
|
| 1718 |
|
| 1719 |
#endif |
| 1720 |
|
| 1721 |
PaUtil_BeginBufferProcessing( &stream->bufferProcessor, &timeInfo, |
| 1722 |
cbFlags ); |
| 1723 |
cbFlags = 0; |
| 1724 |
PA_ENSURE( SetUpBuffers( stream, framesAvail ) ); |
| 1725 |
|
| 1726 |
framesProcessed = PaUtil_EndBufferProcessing( &stream->bufferProcessor, |
| 1727 |
&callbackResult ); |
| 1728 |
assert( framesProcessed == framesAvail ); |
| 1729 |
PaUtil_EndCpuLoadMeasurement( &stream->cpuLoadMeasurer, framesProcessed ); |
| 1730 |
|
| 1731 |
if ( stream->playback ) |
| 1732 |
{ |
| 1733 |
frames = framesAvail; |
| 1734 |
|
| 1735 |
PA_ENSURE( PaOssStreamComponent_Write( stream->playback, &frames ) ); |
| 1736 |
if( frames < framesAvail ) |
| 1737 |
{ |
| 1738 |
|
| 1739 |
PA_DEBUG(( "Wrote %lu less frames than requested\n", framesAvail - frames )); |
| 1740 |
} |
| 1741 |
} |
| 1742 |
|
| 1743 |
framesAvail -= framesProcessed; |
| 1744 |
stream->framesProcessed += framesProcessed; |
| 1745 |
|
| 1746 |
if( callbackResult != paContinue ) |
| 1747 |
break; |
| 1748 |
} |
| 1749 |
|
| 1750 |
if( initiateProcessing || !triggered ) |
| 1751 |
{ |
| 1752 |
|
| 1753 |
if( stream->capture ) |
| 1754 |
PA_ENSURE( ModifyBlocking( stream->capture->fd, 0 ) ); |
| 1755 |
if( stream->playback && !stream->sharedDevice ) |
| 1756 |
PA_ENSURE( ModifyBlocking( stream->playback->fd, 0 ) ); |
| 1757 |
|
| 1758 |
initiateProcessing = 0; |
| 1759 |
sem_post( &stream->semaphore ); |
| 1760 |
} |
| 1761 |
|
| 1762 |
if( callbackResult != paContinue ) |
| 1763 |
{ |
| 1764 |
stream->callbackAbort = callbackResult == paAbort; |
| 1765 |
if( stream->callbackAbort || PaUtil_IsBufferProcessorOutputEmpty( &stream->bufferProcessor ) ) |
| 1766 |
break; |
| 1767 |
} |
| 1768 |
} |
| 1769 |
|
| 1770 |
pthread_cleanup_pop( 1 ); |
| 1771 |
|
| 1772 |
error: |
| 1773 |
pthread_exit( NULL ); |
| 1774 |
} |
| 1775 |
|
| 1776 |
|
| 1777 |
|
| 1778 |
|
| 1779 |
static PaError CloseStream( PaStream* s ) |
| 1780 |
{ |
| 1781 |
PaError result = paNoError; |
| 1782 |
PaOssStream *stream = (PaOssStream*)s; |
| 1783 |
|
| 1784 |
assert( stream ); |
| 1785 |
|
| 1786 |
PaUtil_TerminateBufferProcessor( &stream->bufferProcessor ); |
| 1787 |
PaOssStream_Terminate( stream ); |
| 1788 |
|
| 1789 |
return result; |
| 1790 |
} |
| 1791 |
|
| 1792 |
|
| 1793 |
|
| 1794 |
|
| 1795 |
|
| 1796 |
|
| 1797 |
|
| 1798 |
static PaError StartStream( PaStream *s ) |
| 1799 |
{ |
| 1800 |
PaError result = paNoError; |
| 1801 |
PaOssStream *stream = (PaOssStream*)s; |
| 1802 |
|
| 1803 |
stream->isActive = 1; |
| 1804 |
stream->isStopped = 0; |
| 1805 |
stream->lastPosPtr = 0; |
| 1806 |
stream->lastStreamBytes = 0; |
| 1807 |
stream->framesProcessed = 0; |
| 1808 |
|
| 1809 |
|
| 1810 |
if( stream->bufferProcessor.streamCallback ) |
| 1811 |
{ |
| 1812 |
PA_ENSURE( PaUtil_StartThreading( &stream->threading, &PaOSS_AudioThreadProc, stream ) ); |
| 1813 |
sem_wait( &stream->semaphore ); |
| 1814 |
} |
| 1815 |
else |
| 1816 |
PA_ENSURE( PaOssStream_Prepare( stream ) ); |
| 1817 |
|
| 1818 |
error: |
| 1819 |
return result; |
| 1820 |
} |
| 1821 |
|
| 1822 |
static PaError RealStop( PaOssStream *stream, int abort ) |
| 1823 |
{ |
| 1824 |
PaError result = paNoError; |
| 1825 |
|
| 1826 |
if( stream->callbackMode ) |
| 1827 |
{ |
| 1828 |
if( abort ) |
| 1829 |
stream->callbackAbort = 1; |
| 1830 |
else |
| 1831 |
stream->callbackStop = 1; |
| 1832 |
|
| 1833 |
PA_ENSURE( PaUtil_CancelThreading( &stream->threading, !abort, NULL ) ); |
| 1834 |
|
| 1835 |
stream->callbackStop = stream->callbackAbort = 0; |
| 1836 |
} |
| 1837 |
else |
| 1838 |
PA_ENSURE( PaOssStream_Stop( stream, abort ) ); |
| 1839 |
|
| 1840 |
stream->isStopped = 1; |
| 1841 |
|
| 1842 |
error: |
| 1843 |
return result; |
| 1844 |
} |
| 1845 |
|
| 1846 |
|
| 1847 |
|
| 1848 |
|
| 1849 |
|
| 1850 |
|
| 1851 |
static PaError StopStream( PaStream *s ) |
| 1852 |
{ |
| 1853 |
return RealStop( (PaOssStream *)s, 0 ); |
| 1854 |
} |
| 1855 |
|
| 1856 |
|
| 1857 |
|
| 1858 |
|
| 1859 |
|
| 1860 |
|
| 1861 |
|
| 1862 |
static PaError AbortStream( PaStream *s ) |
| 1863 |
{ |
| 1864 |
return RealStop( (PaOssStream *)s, 1 ); |
| 1865 |
} |
| 1866 |
|
| 1867 |
|
| 1868 |
|
| 1869 |
|
| 1870 |
static PaError IsStreamStopped( PaStream *s ) |
| 1871 |
{ |
| 1872 |
PaOssStream *stream = (PaOssStream*)s; |
| 1873 |
|
| 1874 |
return (stream->isStopped); |
| 1875 |
} |
| 1876 |
|
| 1877 |
|
| 1878 |
|
| 1879 |
|
| 1880 |
static PaError IsStreamActive( PaStream *s ) |
| 1881 |
{ |
| 1882 |
PaOssStream *stream = (PaOssStream*)s; |
| 1883 |
|
| 1884 |
return (stream->isActive); |
| 1885 |
} |
| 1886 |
|
| 1887 |
static PaTime GetStreamTime( PaStream *s ) |
| 1888 |
{ |
| 1889 |
PaOssStream *stream = (PaOssStream*)s; |
| 1890 |
count_info info; |
| 1891 |
int delta; |
| 1892 |
|
| 1893 |
if( stream->playback ) { |
| 1894 |
if( ioctl( stream->playback->fd, SNDCTL_DSP_GETOPTR, &info) == 0 ) { |
| 1895 |
delta = ( info.bytes - stream->lastPosPtr ) ; |
| 1896 |
return (float)(stream->lastStreamBytes + delta) / PaOssStreamComponent_FrameSize( stream->playback ) / stream->sampleRate; |
| 1897 |
} |
| 1898 |
} |
| 1899 |
else { |
| 1900 |
if (ioctl( stream->capture->fd, SNDCTL_DSP_GETIPTR, &info) == 0) { |
| 1901 |
delta = (info.bytes - stream->lastPosPtr) ; |
| 1902 |
return (float)(stream->lastStreamBytes + delta) / PaOssStreamComponent_FrameSize( stream->capture ) / stream->sampleRate; |
| 1903 |
} |
| 1904 |
} |
| 1905 |
|
| 1906 |
|
| 1907 |
|
| 1908 |
return stream->framesProcessed / stream->sampleRate; |
| 1909 |
} |
| 1910 |
|
| 1911 |
|
| 1912 |
static double GetStreamCpuLoad( PaStream* s ) |
| 1913 |
{ |
| 1914 |
PaOssStream *stream = (PaOssStream*)s; |
| 1915 |
|
| 1916 |
return PaUtil_GetCpuLoad( &stream->cpuLoadMeasurer ); |
| 1917 |
} |
| 1918 |
|
| 1919 |
|
| 1920 |
|
| 1921 |
|
| 1922 |
|
| 1923 |
|
| 1924 |
|
| 1925 |
|
| 1926 |
|
| 1927 |
static PaError ReadStream( PaStream* s, |
| 1928 |
void *buffer, |
| 1929 |
unsigned long frames ) |
| 1930 |
{ |
| 1931 |
PaError result = paNoError; |
| 1932 |
PaOssStream *stream = (PaOssStream*)s; |
| 1933 |
int bytesRequested, bytesRead; |
| 1934 |
unsigned long framesRequested; |
| 1935 |
void *userBuffer; |
| 1936 |
|
| 1937 |
|
| 1938 |
|
| 1939 |
if( stream->bufferProcessor.userInputIsInterleaved ) |
| 1940 |
userBuffer = buffer; |
| 1941 |
else |
| 1942 |
{ |
| 1943 |
userBuffer = stream->capture->userBuffers; |
| 1944 |
memcpy( (void *)userBuffer, buffer, sizeof (void *) * stream->capture->userChannelCount ); |
| 1945 |
} |
| 1946 |
|
| 1947 |
while( frames ) |
| 1948 |
{ |
| 1949 |
framesRequested = PA_MIN( frames, stream->capture->hostFrames ); |
| 1950 |
|
| 1951 |
bytesRequested = framesRequested * PaOssStreamComponent_FrameSize( stream->capture ); |
| 1952 |
ENSURE_( (bytesRead = read( stream->capture->fd, stream->capture->buffer, bytesRequested )), |
| 1953 |
paUnanticipatedHostError ); |
| 1954 |
if ( bytesRequested != bytesRead ) |
| 1955 |
{ |
| 1956 |
PA_DEBUG(( "Requested %d bytes, read %d\n", bytesRequested, bytesRead )); |
| 1957 |
return paUnanticipatedHostError; |
| 1958 |
} |
| 1959 |
|
| 1960 |
PaUtil_SetInputFrameCount( &stream->bufferProcessor, stream->capture->hostFrames ); |
| 1961 |
PaUtil_SetInterleavedInputChannels( &stream->bufferProcessor, 0, stream->capture->buffer, stream->capture->hostChannelCount ); |
| 1962 |
PaUtil_CopyInput( &stream->bufferProcessor, &userBuffer, framesRequested ); |
| 1963 |
frames -= framesRequested; |
| 1964 |
} |
| 1965 |
|
| 1966 |
error: |
| 1967 |
return result; |
| 1968 |
} |
| 1969 |
|
| 1970 |
|
| 1971 |
static PaError WriteStream( PaStream *s, const void *buffer, unsigned long frames ) |
| 1972 |
{ |
| 1973 |
PaError result = paNoError; |
| 1974 |
PaOssStream *stream = (PaOssStream*)s; |
| 1975 |
int bytesRequested, bytesWritten; |
| 1976 |
unsigned long framesConverted; |
| 1977 |
const void *userBuffer; |
| 1978 |
|
| 1979 |
|
| 1980 |
|
| 1981 |
if( stream->bufferProcessor.userOutputIsInterleaved ) |
| 1982 |
userBuffer = buffer; |
| 1983 |
else |
| 1984 |
{ |
| 1985 |
|
| 1986 |
userBuffer = stream->playback->userBuffers; |
| 1987 |
memcpy( (void *)userBuffer, buffer, sizeof (void *) * stream->playback->userChannelCount ); |
| 1988 |
} |
| 1989 |
|
| 1990 |
while( frames ) |
| 1991 |
{ |
| 1992 |
PaUtil_SetOutputFrameCount( &stream->bufferProcessor, stream->playback->hostFrames ); |
| 1993 |
PaUtil_SetInterleavedOutputChannels( &stream->bufferProcessor, 0, stream->playback->buffer, stream->playback->hostChannelCount ); |
| 1994 |
|
| 1995 |
framesConverted = PaUtil_CopyOutput( &stream->bufferProcessor, &userBuffer, frames ); |
| 1996 |
frames -= framesConverted; |
| 1997 |
|
| 1998 |
bytesRequested = framesConverted * PaOssStreamComponent_FrameSize( stream->playback ); |
| 1999 |
ENSURE_( (bytesWritten = write( stream->playback->fd, stream->playback->buffer, bytesRequested )), |
| 2000 |
paUnanticipatedHostError ); |
| 2001 |
|
| 2002 |
if ( bytesRequested != bytesWritten ) |
| 2003 |
{ |
| 2004 |
PA_DEBUG(( "Requested %d bytes, wrote %d\n", bytesRequested, bytesWritten )); |
| 2005 |
return paUnanticipatedHostError; |
| 2006 |
} |
| 2007 |
} |
| 2008 |
|
| 2009 |
error: |
| 2010 |
return result; |
| 2011 |
} |
| 2012 |
|
| 2013 |
|
| 2014 |
static signed long GetStreamReadAvailable( PaStream* s ) |
| 2015 |
{ |
| 2016 |
PaError result = paNoError; |
| 2017 |
PaOssStream *stream = (PaOssStream*)s; |
| 2018 |
audio_buf_info info; |
| 2019 |
|
| 2020 |
ENSURE_( ioctl( stream->capture->fd, SNDCTL_DSP_GETISPACE, &info ), paUnanticipatedHostError ); |
| 2021 |
return info.fragments * stream->capture->hostFrames; |
| 2022 |
|
| 2023 |
error: |
| 2024 |
return result; |
| 2025 |
} |
| 2026 |
|
| 2027 |
|
| 2028 |
|
| 2029 |
static signed long GetStreamWriteAvailable( PaStream* s ) |
| 2030 |
{ |
| 2031 |
PaError result = paNoError; |
| 2032 |
PaOssStream *stream = (PaOssStream*)s; |
| 2033 |
int delay = 0; |
| 2034 |
#ifdef SNDCTL_DSP_GETODELAY |
| 2035 |
ENSURE_( ioctl( stream->playback->fd, SNDCTL_DSP_GETODELAY, &delay ), paUnanticipatedHostError ); |
| 2036 |
#endif |
| 2037 |
return (PaOssStreamComponent_BufferSize( stream->playback ) - delay) / PaOssStreamComponent_FrameSize( stream->playback ); |
| 2038 |
|
| 2039 |
|
| 2040 |
#ifdef SNDCTL_DSP_GETODELAY |
| 2041 |
error: |
| 2042 |
return result; |
| 2043 |
#endif |
| 2044 |
} |
| 2045 |
|