| 1 |
#if defined (UNIX) && defined (JACK) |
| 2 |
/* |
| 3 |
* $Id$ |
| 4 |
* PortAudio Portable Real-Time Audio Library |
| 5 |
* Latest Version at: http://www.portaudio.com |
| 6 |
* JACK Implementation by Joshua Haberman |
| 7 |
* |
| 8 |
* Copyright (c) 2004 Stefan Westerfeld <[email protected]> |
| 9 |
* Copyright (c) 2004 Arve Knudsen <[email protected]> |
| 10 |
* Copyright (c) 2002 Joshua Haberman <[email protected]> |
| 11 |
* |
| 12 |
* Based on the Open Source API proposed by Ross Bencina |
| 13 |
* Copyright (c) 1999-2002 Ross Bencina, Phil Burk |
| 14 |
* |
| 15 |
* Permission is hereby granted, free of charge, to any person obtaining |
| 16 |
* a copy of this software and associated documentation files |
| 17 |
* (the "Software"), to deal in the Software without restriction, |
| 18 |
* including without limitation the rights to use, copy, modify, merge, |
| 19 |
* publish, distribute, sublicense, and/or sell copies of the Software, |
| 20 |
* and to permit persons to whom the Software is furnished to do so, |
| 21 |
* subject to the following conditions: |
| 22 |
* |
| 23 |
* The above copyright notice and this permission notice shall be |
| 24 |
* included in all copies or substantial portions of the Software. |
| 25 |
* |
| 26 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 27 |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 28 |
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 29 |
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR |
| 30 |
* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF |
| 31 |
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 32 |
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 33 |
*/ |
| 34 |
|
| 35 |
/* |
| 36 |
* The text above constitutes the entire PortAudio license; however, |
| 37 |
* the PortAudio community also makes the following non-binding requests: |
| 38 |
* |
| 39 |
* Any person wishing to distribute modifications to the Software is |
| 40 |
* requested to send the modifications to the original developer so that |
| 41 |
* they can be incorporated into the canonical version. It is also |
| 42 |
* requested that these non-binding requests be included along with the |
| 43 |
* license above. |
| 44 |
*/ |
| 45 |
|
| 46 |
/** |
| 47 |
@file |
| 48 |
@ingroup hostapi_src |
| 49 |
*/ |
| 50 |
|
| 51 |
#include <string.h> |
| 52 |
#include <regex.h> |
| 53 |
#include <stdlib.h> |
| 54 |
#include <stdio.h> |
| 55 |
#include <assert.h> |
| 56 |
#include <sys/types.h> |
| 57 |
#include <unistd.h> |
| 58 |
#include <errno.h> /* EBUSY */ |
| 59 |
#include <signal.h> /* sig_atomic_t */ |
| 60 |
#include <math.h> |
| 61 |
#include <semaphore.h> |
| 62 |
|
| 63 |
#include <jack/types.h> |
| 64 |
#include <jack/jack.h> |
| 65 |
|
| 66 |
#include "pa_util.h" |
| 67 |
#include "pa_hostapi.h" |
| 68 |
#include "pa_stream.h" |
| 69 |
#include "pa_process.h" |
| 70 |
#include "pa_allocation.h" |
| 71 |
#include "pa_cpuload.h" |
| 72 |
#include "pa_ringbuffer.h" |
| 73 |
#include "pa_debugprint.h" |
| 74 |
|
| 75 |
#include "pa_jack.h" |
| 76 |
|
| 77 |
static pthread_t mainThread_; |
| 78 |
static char *jackErr_ = NULL; |
| 79 |
static const char* clientName_ = "PortAudio"; |
| 80 |
static const char* port_regex_suffix = ":.*"; |
| 81 |
|
| 82 |
#define STRINGIZE_HELPER(expr) #expr |
| 83 |
#define STRINGIZE(expr) STRINGIZE_HELPER(expr) |
| 84 |
|
| 85 |
/* Check PaError */ |
| 86 |
#define ENSURE_PA(expr) \ |
| 87 |
do { \ |
| 88 |
PaError paErr; \ |
| 89 |
if( (paErr = (expr)) < paNoError ) \ |
| 90 |
{ \ |
| 91 |
if( (paErr) == paUnanticipatedHostError && pthread_self() == mainThread_ ) \ |
| 92 |
{ \ |
| 93 |
const char *err = jackErr_; \ |
| 94 |
if (! err ) err = "unknown error"; \ |
| 95 |
PaUtil_SetLastHostErrorInfo( paJACK, -1, err ); \ |
| 96 |
} \ |
| 97 |
PaUtil_DebugPrint(( "Expression '" #expr "' failed in '" __FILE__ "', line: " STRINGIZE( __LINE__ ) "\n" )); \ |
| 98 |
result = paErr; \ |
| 99 |
goto error; \ |
| 100 |
} \ |
| 101 |
} while( 0 ) |
| 102 |
|
| 103 |
#define UNLESS(expr, code) \ |
| 104 |
do { \ |
| 105 |
if( (expr) == 0 ) \ |
| 106 |
{ \ |
| 107 |
if( (code) == paUnanticipatedHostError && pthread_self() == mainThread_ ) \ |
| 108 |
{ \ |
| 109 |
const char *err = jackErr_; \ |
| 110 |
if (!err) err = "unknown error"; \ |
| 111 |
PaUtil_SetLastHostErrorInfo( paJACK, -1, err ); \ |
| 112 |
} \ |
| 113 |
PaUtil_DebugPrint(( "Expression '" #expr "' failed in '" __FILE__ "', line: " STRINGIZE( __LINE__ ) "\n" )); \ |
| 114 |
result = (code); \ |
| 115 |
goto error; \ |
| 116 |
} \ |
| 117 |
} while( 0 ) |
| 118 |
|
| 119 |
#define ASSERT_CALL(expr, success) \ |
| 120 |
do { \ |
| 121 |
int err = (expr); \ |
| 122 |
assert( err == success ); \ |
| 123 |
} while( 0 ) |
| 124 |
|
| 125 |
/* |
| 126 |
* Functions that directly map to the PortAudio stream interface |
| 127 |
*/ |
| 128 |
|
| 129 |
static void Terminate( struct PaUtilHostApiRepresentation *hostApi ); |
| 130 |
static PaError IsFormatSupported( struct PaUtilHostApiRepresentation *hostApi, |
| 131 |
const PaStreamParameters *inputParameters, |
| 132 |
const PaStreamParameters *outputParameters, |
| 133 |
double sampleRate ); |
| 134 |
static PaError OpenStream( struct PaUtilHostApiRepresentation *hostApi, |
| 135 |
PaStream** s, |
| 136 |
const PaStreamParameters *inputParameters, |
| 137 |
const PaStreamParameters *outputParameters, |
| 138 |
double sampleRate, |
| 139 |
unsigned long framesPerBuffer, |
| 140 |
PaStreamFlags streamFlags, |
| 141 |
PaStreamCallback *streamCallback, |
| 142 |
void *userData ); |
| 143 |
static PaError CloseStream( PaStream* stream ); |
| 144 |
static PaError StartStream( PaStream *stream ); |
| 145 |
static PaError StopStream( PaStream *stream ); |
| 146 |
static PaError AbortStream( PaStream *stream ); |
| 147 |
static PaError IsStreamStopped( PaStream *s ); |
| 148 |
static PaError IsStreamActive( PaStream *stream ); |
| 149 |
/*static PaTime GetStreamInputLatency( PaStream *stream );*/ |
| 150 |
/*static PaTime GetStreamOutputLatency( PaStream *stream );*/ |
| 151 |
static PaTime GetStreamTime( PaStream *stream ); |
| 152 |
static double GetStreamCpuLoad( PaStream* stream ); |
| 153 |
|
| 154 |
|
| 155 |
/* |
| 156 |
* Data specific to this API |
| 157 |
*/ |
| 158 |
|
| 159 |
struct PaJackStream; |
| 160 |
|
| 161 |
typedef struct |
| 162 |
{ |
| 163 |
PaUtilHostApiRepresentation commonHostApiRep; |
| 164 |
PaUtilStreamInterface callbackStreamInterface; |
| 165 |
PaUtilStreamInterface blockingStreamInterface; |
| 166 |
|
| 167 |
PaUtilAllocationGroup *deviceInfoMemory; |
| 168 |
|
| 169 |
jack_client_t *jack_client; |
| 170 |
int jack_buffer_size; |
| 171 |
PaHostApiIndex hostApiIndex; |
| 172 |
|
| 173 |
pthread_mutex_t mtx; |
| 174 |
pthread_cond_t cond; |
| 175 |
unsigned long inputBase, outputBase; |
| 176 |
|
| 177 |
/* For dealing with the process thread */ |
| 178 |
volatile int xrun; /* Received xrun notification from JACK? */ |
| 179 |
struct PaJackStream * volatile toAdd, * volatile toRemove; |
| 180 |
struct PaJackStream *processQueue; |
| 181 |
volatile sig_atomic_t jackIsDown; |
| 182 |
} |
| 183 |
PaJackHostApiRepresentation; |
| 184 |
|
| 185 |
/* PaJackStream - a stream data structure specifically for this implementation */ |
| 186 |
|
| 187 |
typedef struct PaJackStream |
| 188 |
{ |
| 189 |
PaUtilStreamRepresentation streamRepresentation; |
| 190 |
PaUtilBufferProcessor bufferProcessor; |
| 191 |
PaUtilCpuLoadMeasurer cpuLoadMeasurer; |
| 192 |
PaJackHostApiRepresentation *hostApi; |
| 193 |
|
| 194 |
/* our input and output ports */ |
| 195 |
jack_port_t **local_input_ports; |
| 196 |
jack_port_t **local_output_ports; |
| 197 |
|
| 198 |
/* the input and output ports of the client we are connecting to */ |
| 199 |
jack_port_t **remote_input_ports; |
| 200 |
jack_port_t **remote_output_ports; |
| 201 |
|
| 202 |
int num_incoming_connections; |
| 203 |
int num_outgoing_connections; |
| 204 |
|
| 205 |
jack_client_t *jack_client; |
| 206 |
|
| 207 |
/* The stream is running if it's still producing samples. |
| 208 |
* The stream is active if samples it produced are still being heard. |
| 209 |
*/ |
| 210 |
volatile sig_atomic_t is_running; |
| 211 |
volatile sig_atomic_t is_active; |
| 212 |
/* Used to signal processing thread that stream should start or stop, respectively */ |
| 213 |
volatile sig_atomic_t doStart, doStop, doAbort; |
| 214 |
|
| 215 |
jack_nframes_t t0; |
| 216 |
|
| 217 |
PaUtilAllocationGroup *stream_memory; |
| 218 |
|
| 219 |
/* These are useful in the process callback */ |
| 220 |
|
| 221 |
int callbackResult; |
| 222 |
int isSilenced; |
| 223 |
int xrun; |
| 224 |
|
| 225 |
/* These are useful for the blocking API */ |
| 226 |
|
| 227 |
int isBlockingStream; |
| 228 |
PaUtilRingBuffer inFIFO; |
| 229 |
PaUtilRingBuffer outFIFO; |
| 230 |
volatile sig_atomic_t data_available; |
| 231 |
sem_t data_semaphore; |
| 232 |
int bytesPerFrame; |
| 233 |
int samplesPerFrame; |
| 234 |
|
| 235 |
struct PaJackStream *next; |
| 236 |
} |
| 237 |
PaJackStream; |
| 238 |
|
| 239 |
/* In calls to jack_get_ports() this filter expression is used instead of "" |
| 240 |
* to prevent any other types (eg Midi ports etc) being listed */ |
| 241 |
#define JACK_PORT_TYPE_FILTER "audio" |
| 242 |
|
| 243 |
#define TRUE 1 |
| 244 |
#define FALSE 0 |
| 245 |
|
| 246 |
/* |
| 247 |
* Functions specific to this API |
| 248 |
*/ |
| 249 |
|
| 250 |
static int JackCallback( jack_nframes_t frames, void *userData ); |
| 251 |
|
| 252 |
|
| 253 |
/* |
| 254 |
* |
| 255 |
* Implementation |
| 256 |
* |
| 257 |
*/ |
| 258 |
|
| 259 |
/* ---- blocking emulation layer ---- */ |
| 260 |
|
| 261 |
/* Allocate buffer. */ |
| 262 |
static PaError BlockingInitFIFO( PaUtilRingBuffer *rbuf, long numFrames, long bytesPerFrame ) |
| 263 |
{ |
| 264 |
long numBytes = numFrames * bytesPerFrame; |
| 265 |
char *buffer = (char *) malloc( numBytes ); |
| 266 |
if( buffer == NULL ) return paInsufficientMemory; |
| 267 |
memset( buffer, 0, numBytes ); |
| 268 |
return (PaError) PaUtil_InitializeRingBuffer( rbuf, 1, numBytes, buffer ); |
| 269 |
} |
| 270 |
|
| 271 |
/* Free buffer. */ |
| 272 |
static PaError BlockingTermFIFO( PaUtilRingBuffer *rbuf ) |
| 273 |
{ |
| 274 |
if( rbuf->buffer ) free( rbuf->buffer ); |
| 275 |
rbuf->buffer = NULL; |
| 276 |
return paNoError; |
| 277 |
} |
| 278 |
|
| 279 |
static int |
| 280 |
BlockingCallback( const void *inputBuffer, |
| 281 |
void *outputBuffer, |
| 282 |
unsigned long framesPerBuffer, |
| 283 |
const PaStreamCallbackTimeInfo* timeInfo, |
| 284 |
PaStreamCallbackFlags statusFlags, |
| 285 |
void *userData ) |
| 286 |
{ |
| 287 |
struct PaJackStream *stream = (PaJackStream *)userData; |
| 288 |
long numBytes = stream->bytesPerFrame * framesPerBuffer; |
| 289 |
|
| 290 |
/* This may get called with NULL inputBuffer during initial setup. */ |
| 291 |
if( inputBuffer != NULL ) |
| 292 |
{ |
| 293 |
PaUtil_WriteRingBuffer( &stream->inFIFO, inputBuffer, numBytes ); |
| 294 |
} |
| 295 |
if( outputBuffer != NULL ) |
| 296 |
{ |
| 297 |
int numRead = PaUtil_ReadRingBuffer( &stream->outFIFO, outputBuffer, numBytes ); |
| 298 |
/* Zero out remainder of buffer if we run out of data. */ |
| 299 |
memset( (char *)outputBuffer + numRead, 0, numBytes - numRead ); |
| 300 |
} |
| 301 |
|
| 302 |
if( !stream->data_available ) |
| 303 |
{ |
| 304 |
stream->data_available = 1; |
| 305 |
sem_post( &stream->data_semaphore ); |
| 306 |
} |
| 307 |
return paContinue; |
| 308 |
} |
| 309 |
|
| 310 |
static PaError |
| 311 |
BlockingBegin( PaJackStream *stream, int minimum_buffer_size ) |
| 312 |
{ |
| 313 |
long doRead = 0; |
| 314 |
long doWrite = 0; |
| 315 |
PaError result = paNoError; |
| 316 |
long numFrames; |
| 317 |
|
| 318 |
doRead = stream->local_input_ports != NULL; |
| 319 |
doWrite = stream->local_output_ports != NULL; |
| 320 |
/* <FIXME> */ |
| 321 |
stream->samplesPerFrame = 2; |
| 322 |
stream->bytesPerFrame = sizeof(float) * stream->samplesPerFrame; |
| 323 |
/* </FIXME> */ |
| 324 |
numFrames = 32; |
| 325 |
while (numFrames < minimum_buffer_size) |
| 326 |
numFrames *= 2; |
| 327 |
|
| 328 |
if( doRead ) |
| 329 |
{ |
| 330 |
ENSURE_PA( BlockingInitFIFO( &stream->inFIFO, numFrames, stream->bytesPerFrame ) ); |
| 331 |
} |
| 332 |
if( doWrite ) |
| 333 |
{ |
| 334 |
long numBytes; |
| 335 |
|
| 336 |
ENSURE_PA( BlockingInitFIFO( &stream->outFIFO, numFrames, stream->bytesPerFrame ) ); |
| 337 |
|
| 338 |
/* Make Write FIFO appear full initially. */ |
| 339 |
numBytes = PaUtil_GetRingBufferWriteAvailable( &stream->outFIFO ); |
| 340 |
PaUtil_AdvanceRingBufferWriteIndex( &stream->outFIFO, numBytes ); |
| 341 |
} |
| 342 |
|
| 343 |
stream->data_available = 0; |
| 344 |
sem_init( &stream->data_semaphore, 0, 0 ); |
| 345 |
|
| 346 |
error: |
| 347 |
return result; |
| 348 |
} |
| 349 |
|
| 350 |
static void |
| 351 |
BlockingEnd( PaJackStream *stream ) |
| 352 |
{ |
| 353 |
BlockingTermFIFO( &stream->inFIFO ); |
| 354 |
BlockingTermFIFO( &stream->outFIFO ); |
| 355 |
|
| 356 |
sem_destroy( &stream->data_semaphore ); |
| 357 |
} |
| 358 |
|
| 359 |
static PaError BlockingReadStream( PaStream* s, void *data, unsigned long numFrames ) |
| 360 |
{ |
| 361 |
PaError result = paNoError; |
| 362 |
PaJackStream *stream = (PaJackStream *)s; |
| 363 |
|
| 364 |
long bytesRead; |
| 365 |
char *p = (char *) data; |
| 366 |
long numBytes = stream->bytesPerFrame * numFrames; |
| 367 |
while( numBytes > 0 ) |
| 368 |
{ |
| 369 |
bytesRead = PaUtil_ReadRingBuffer( &stream->inFIFO, p, numBytes ); |
| 370 |
numBytes -= bytesRead; |
| 371 |
p += bytesRead; |
| 372 |
if( numBytes > 0 ) |
| 373 |
{ |
| 374 |
/* see write for an explanation */ |
| 375 |
if( stream->data_available ) |
| 376 |
stream->data_available = 0; |
| 377 |
else |
| 378 |
sem_wait( &stream->data_semaphore ); |
| 379 |
} |
| 380 |
} |
| 381 |
|
| 382 |
return result; |
| 383 |
} |
| 384 |
|
| 385 |
static PaError BlockingWriteStream( PaStream* s, const void *data, unsigned long numFrames ) |
| 386 |
{ |
| 387 |
PaError result = paNoError; |
| 388 |
PaJackStream *stream = (PaJackStream *)s; |
| 389 |
long bytesWritten; |
| 390 |
char *p = (char *) data; |
| 391 |
long numBytes = stream->bytesPerFrame * numFrames; |
| 392 |
while( numBytes > 0 ) |
| 393 |
{ |
| 394 |
bytesWritten = PaUtil_WriteRingBuffer( &stream->outFIFO, p, numBytes ); |
| 395 |
numBytes -= bytesWritten; |
| 396 |
p += bytesWritten; |
| 397 |
if( numBytes > 0 ) |
| 398 |
{ |
| 399 |
/* we use the following algorithm: |
| 400 |
* (1) write data |
| 401 |
* (2) if some data didn't fit into the ringbuffer, set data_available to 0 |
| 402 |
* to indicate to the audio that if space becomes available, we want to know |
| 403 |
* (3) retry to write data (because it might be that between (1) and (2) |
| 404 |
* new space in the buffer became available) |
| 405 |
* (4) if this failed, we are sure that the buffer is really empty and |
| 406 |
* we will definitely receive a notification when it becomes available |
| 407 |
* thus we can safely sleep |
| 408 |
* |
| 409 |
* if the algorithm bailed out in step (3) before, it leaks a count of 1 |
| 410 |
* on the semaphore; however, it doesn't matter, because if we block in (4), |
| 411 |
* we also do it in a loop |
| 412 |
*/ |
| 413 |
if( stream->data_available ) |
| 414 |
stream->data_available = 0; |
| 415 |
else |
| 416 |
sem_wait( &stream->data_semaphore ); |
| 417 |
} |
| 418 |
} |
| 419 |
|
| 420 |
return result; |
| 421 |
} |
| 422 |
|
| 423 |
static signed long |
| 424 |
BlockingGetStreamReadAvailable( PaStream* s ) |
| 425 |
{ |
| 426 |
PaJackStream *stream = (PaJackStream *)s; |
| 427 |
|
| 428 |
int bytesFull = PaUtil_GetRingBufferReadAvailable( &stream->inFIFO ); |
| 429 |
return bytesFull / stream->bytesPerFrame; |
| 430 |
} |
| 431 |
|
| 432 |
static signed long |
| 433 |
BlockingGetStreamWriteAvailable( PaStream* s ) |
| 434 |
{ |
| 435 |
PaJackStream *stream = (PaJackStream *)s; |
| 436 |
|
| 437 |
int bytesEmpty = PaUtil_GetRingBufferWriteAvailable( &stream->outFIFO ); |
| 438 |
return bytesEmpty / stream->bytesPerFrame; |
| 439 |
} |
| 440 |
|
| 441 |
static PaError |
| 442 |
BlockingWaitEmpty( PaStream *s ) |
| 443 |
{ |
| 444 |
PaJackStream *stream = (PaJackStream *)s; |
| 445 |
|
| 446 |
while( PaUtil_GetRingBufferReadAvailable( &stream->outFIFO ) > 0 ) |
| 447 |
{ |
| 448 |
stream->data_available = 0; |
| 449 |
sem_wait( &stream->data_semaphore ); |
| 450 |
} |
| 451 |
return 0; |
| 452 |
} |
| 453 |
|
| 454 |
/* ---- jack driver ---- */ |
| 455 |
|
| 456 |
/* copy null terminated string source to destination, escaping regex characters with '\\' in the process */ |
| 457 |
static void copy_string_and_escape_regex_chars( char *destination, const char *source, size_t destbuffersize ) |
| 458 |
{ |
| 459 |
assert( destination != source ); |
| 460 |
assert( destbuffersize > 0 ); |
| 461 |
|
| 462 |
char *dest = destination; |
| 463 |
/* dest_stop is the last location that we can null-terminate the string */ |
| 464 |
char *dest_stop = destination + (destbuffersize - 1); |
| 465 |
|
| 466 |
const char *src = source; |
| 467 |
|
| 468 |
while ( *src != '\0' && dest != dest_stop ) |
| 469 |
{ |
| 470 |
const char c = *src; |
| 471 |
if ( strchr( "\\()[]{}*+?|$^.", c ) != NULL ) |
| 472 |
{ |
| 473 |
if( (dest + 1) == dest_stop ) |
| 474 |
break; /* only proceed if we can write both c and the escape */ |
| 475 |
|
| 476 |
*dest = '\\'; |
| 477 |
dest++; |
| 478 |
} |
| 479 |
*dest = c; |
| 480 |
dest++; |
| 481 |
|
| 482 |
src++; |
| 483 |
} |
| 484 |
|
| 485 |
*dest = '\0'; |
| 486 |
} |
| 487 |
|
| 488 |
/* BuildDeviceList(): |
| 489 |
* |
| 490 |
* The process of determining a list of PortAudio "devices" from |
| 491 |
* JACK's client/port system is fairly involved, so it is separated |
| 492 |
* into its own routine. |
| 493 |
*/ |
| 494 |
|
| 495 |
static PaError BuildDeviceList( PaJackHostApiRepresentation *jackApi ) |
| 496 |
{ |
| 497 |
/* Utility macros for the repetitive process of allocating memory */ |
| 498 |
|
| 499 |
/* JACK has no concept of a device. To JACK, there are clients |
| 500 |
* which have an arbitrary number of ports. To make this |
| 501 |
* intelligible to PortAudio clients, we will group each JACK client |
| 502 |
* into a device, and make each port of that client a channel */ |
| 503 |
|
| 504 |
PaError result = paNoError; |
| 505 |
PaUtilHostApiRepresentation *commonApi = &jackApi->commonHostApiRep; |
| 506 |
|
| 507 |
const char **jack_ports = NULL; |
| 508 |
char **client_names = NULL; |
| 509 |
char *port_regex_string = NULL; |
| 510 |
// In the worst case scenario, every character would be escaped, doubling the string size. |
| 511 |
// Add 1 for null terminator. |
| 512 |
size_t device_name_regex_escaped_size = jack_client_name_size() * 2 + 1; |
| 513 |
size_t port_regex_size = device_name_regex_escaped_size + strlen(port_regex_suffix); |
| 514 |
int port_index, client_index, i; |
| 515 |
double globalSampleRate; |
| 516 |
regex_t port_regex; |
| 517 |
unsigned long numClients = 0, numPorts = 0; |
| 518 |
char *tmp_client_name = NULL; |
| 519 |
|
| 520 |
commonApi->info.defaultInputDevice = paNoDevice; |
| 521 |
commonApi->info.defaultOutputDevice = paNoDevice; |
| 522 |
commonApi->info.deviceCount = 0; |
| 523 |
|
| 524 |
/* Parse the list of ports, using a regex to grab the client names */ |
| 525 |
ASSERT_CALL( regcomp( &port_regex, "^[^:]*", REG_EXTENDED ), 0 ); |
| 526 |
|
| 527 |
/* since we are rebuilding the list of devices, free all memory |
| 528 |
* associated with the previous list */ |
| 529 |
PaUtil_FreeAllAllocations( jackApi->deviceInfoMemory ); |
| 530 |
|
| 531 |
port_regex_string = PaUtil_GroupAllocateMemory( jackApi->deviceInfoMemory, port_regex_size ); |
| 532 |
tmp_client_name = PaUtil_GroupAllocateMemory( jackApi->deviceInfoMemory, jack_client_name_size() ); |
| 533 |
|
| 534 |
/* We can only retrieve the list of clients indirectly, by first |
| 535 |
* asking for a list of all ports, then parsing the port names |
| 536 |
* according to the client_name:port_name convention (which is |
| 537 |
* enforced by jackd) |
| 538 |
* A: If jack_get_ports returns NULL, there's nothing for us to do */ |
| 539 |
UNLESS( (jack_ports = jack_get_ports( jackApi->jack_client, "", JACK_PORT_TYPE_FILTER, 0 )) && jack_ports[0], paNoError ); |
| 540 |
/* Find number of ports */ |
| 541 |
while( jack_ports[numPorts] ) |
| 542 |
++numPorts; |
| 543 |
/* At least there will be one port per client :) */ |
| 544 |
UNLESS( client_names = PaUtil_GroupAllocateMemory( jackApi->deviceInfoMemory, numPorts * |
| 545 |
sizeof (char *) ), paInsufficientMemory ); |
| 546 |
|
| 547 |
/* Build a list of clients from the list of ports */ |
| 548 |
for( numClients = 0, port_index = 0; jack_ports[port_index] != NULL; port_index++ ) |
| 549 |
{ |
| 550 |
int client_seen = FALSE; |
| 551 |
regmatch_t match_info; |
| 552 |
const char *port = jack_ports[port_index]; |
| 553 |
PA_DEBUG(( "JACK port found: %s\n", port )); |
| 554 |
|
| 555 |
/* extract the client name from the port name, using a regex |
| 556 |
* that parses the clientname:portname syntax */ |
| 557 |
UNLESS( !regexec( &port_regex, port, 1, &match_info, 0 ), paInternalError ); |
| 558 |
assert(match_info.rm_eo - match_info.rm_so < jack_client_name_size()); |
| 559 |
memcpy( tmp_client_name, port + match_info.rm_so, |
| 560 |
match_info.rm_eo - match_info.rm_so ); |
| 561 |
tmp_client_name[match_info.rm_eo - match_info.rm_so] = '\0'; |
| 562 |
|
| 563 |
/* do we know about this port's client yet? */ |
| 564 |
for( i = 0; i < numClients; i++ ) |
| 565 |
{ |
| 566 |
if( strcmp( tmp_client_name, client_names[i] ) == 0 ) |
| 567 |
client_seen = TRUE; |
| 568 |
} |
| 569 |
|
| 570 |
if (client_seen) |
| 571 |
continue; /* A: Nothing to see here, move along */ |
| 572 |
|
| 573 |
UNLESS( client_names[numClients] = (char*)PaUtil_GroupAllocateMemory( jackApi->deviceInfoMemory, |
| 574 |
strlen(tmp_client_name) + 1), paInsufficientMemory ); |
| 575 |
|
| 576 |
/* The alsa_pcm client should go in spot 0. If this |
| 577 |
* is the alsa_pcm client AND we are NOT about to put |
| 578 |
* it in spot 0 put it in spot 0 and move whatever |
| 579 |
* was already in spot 0 to the end. */ |
| 580 |
if( strcmp( "alsa_pcm", tmp_client_name ) == 0 && numClients > 0 ) |
| 581 |
{ |
| 582 |
/* alsa_pcm goes in spot 0 */ |
| 583 |
strcpy( client_names[ numClients ], client_names[0] ); |
| 584 |
strcpy( client_names[0], tmp_client_name ); |
| 585 |
} |
| 586 |
else |
| 587 |
{ |
| 588 |
/* put the new client at the end of the client list */ |
| 589 |
strcpy( client_names[ numClients ], tmp_client_name ); |
| 590 |
} |
| 591 |
++numClients; |
| 592 |
} |
| 593 |
|
| 594 |
/* Now we have a list of clients, which will become the list of |
| 595 |
* PortAudio devices. */ |
| 596 |
|
| 597 |
/* there is one global sample rate all clients must conform to */ |
| 598 |
|
| 599 |
globalSampleRate = jack_get_sample_rate( jackApi->jack_client ); |
| 600 |
UNLESS( commonApi->deviceInfos = (PaDeviceInfo**)PaUtil_GroupAllocateMemory( jackApi->deviceInfoMemory, |
| 601 |
sizeof(PaDeviceInfo*) * numClients ), paInsufficientMemory ); |
| 602 |
|
| 603 |
assert( commonApi->info.deviceCount == 0 ); |
| 604 |
|
| 605 |
/* Create a PaDeviceInfo structure for every client */ |
| 606 |
for( client_index = 0; client_index < numClients; client_index++ ) |
| 607 |
{ |
| 608 |
PaDeviceInfo *curDevInfo; |
| 609 |
const char **clientPorts = NULL; |
| 610 |
|
| 611 |
UNLESS( curDevInfo = (PaDeviceInfo*)PaUtil_GroupAllocateMemory( jackApi->deviceInfoMemory, |
| 612 |
sizeof(PaDeviceInfo) ), paInsufficientMemory ); |
| 613 |
UNLESS( curDevInfo->name = (char*)PaUtil_GroupAllocateMemory( jackApi->deviceInfoMemory, |
| 614 |
strlen(client_names[client_index]) + 1 ), paInsufficientMemory ); |
| 615 |
strcpy( (char *)curDevInfo->name, client_names[client_index] ); |
| 616 |
|
| 617 |
curDevInfo->structVersion = 2; |
| 618 |
curDevInfo->hostApi = jackApi->hostApiIndex; |
| 619 |
|
| 620 |
/* JACK is very inflexible: there is one sample rate the whole |
| 621 |
* system must run at, and all clients must speak IEEE float. */ |
| 622 |
curDevInfo->defaultSampleRate = globalSampleRate; |
| 623 |
|
| 624 |
/* To determine how many input and output channels are available, |
| 625 |
* we re-query jackd with more specific parameters. */ |
| 626 |
copy_string_and_escape_regex_chars( port_regex_string, |
| 627 |
client_names[client_index], |
| 628 |
device_name_regex_escaped_size ); |
| 629 |
strncat( port_regex_string, port_regex_suffix, port_regex_size ); |
| 630 |
|
| 631 |
/* ... what are your output ports (that we could input from)? */ |
| 632 |
clientPorts = jack_get_ports( jackApi->jack_client, port_regex_string, |
| 633 |
JACK_PORT_TYPE_FILTER, JackPortIsOutput); |
| 634 |
curDevInfo->maxInputChannels = 0; |
| 635 |
curDevInfo->defaultLowInputLatency = 0.; |
| 636 |
curDevInfo->defaultHighInputLatency = 0.; |
| 637 |
if( clientPorts ) |
| 638 |
{ |
| 639 |
jack_port_t *p = jack_port_by_name( jackApi->jack_client, clientPorts[0] ); |
| 640 |
curDevInfo->defaultLowInputLatency = curDevInfo->defaultHighInputLatency = |
| 641 |
jack_port_get_latency( p ) / globalSampleRate; |
| 642 |
|
| 643 |
for( i = 0; clientPorts[i] != NULL; i++) |
| 644 |
{ |
| 645 |
/* The number of ports returned is the number of output channels. |
| 646 |
* We don't care what they are, we just care how many */ |
| 647 |
curDevInfo->maxInputChannels++; |
| 648 |
} |
| 649 |
free(clientPorts); |
| 650 |
} |
| 651 |
|
| 652 |
/* ... what are your input ports (that we could output to)? */ |
| 653 |
clientPorts = jack_get_ports( jackApi->jack_client, port_regex_string, |
| 654 |
JACK_PORT_TYPE_FILTER, JackPortIsInput); |
| 655 |
curDevInfo->maxOutputChannels = 0; |
| 656 |
curDevInfo->defaultLowOutputLatency = 0.; |
| 657 |
curDevInfo->defaultHighOutputLatency = 0.; |
| 658 |
if( clientPorts ) |
| 659 |
{ |
| 660 |
jack_port_t *p = jack_port_by_name( jackApi->jack_client, clientPorts[0] ); |
| 661 |
curDevInfo->defaultLowOutputLatency = curDevInfo->defaultHighOutputLatency = |
| 662 |
jack_port_get_latency( p ) / globalSampleRate; |
| 663 |
|
| 664 |
for( i = 0; clientPorts[i] != NULL; i++) |
| 665 |
{ |
| 666 |
/* The number of ports returned is the number of input channels. |
| 667 |
* We don't care what they are, we just care how many */ |
| 668 |
curDevInfo->maxOutputChannels++; |
| 669 |
} |
| 670 |
free(clientPorts); |
| 671 |
} |
| 672 |
|
| 673 |
PA_DEBUG(( "Adding JACK device %s with %d input channels and %d output channels\n", |
| 674 |
client_names[client_index], |
| 675 |
curDevInfo->maxInputChannels, |
| 676 |
curDevInfo->maxOutputChannels )); |
| 677 |
|
| 678 |
/* Add this client to the list of devices */ |
| 679 |
commonApi->deviceInfos[client_index] = curDevInfo; |
| 680 |
++commonApi->info.deviceCount; |
| 681 |
if( commonApi->info.defaultInputDevice == paNoDevice && curDevInfo->maxInputChannels > 0 ) |
| 682 |
commonApi->info.defaultInputDevice = client_index; |
| 683 |
if( commonApi->info.defaultOutputDevice == paNoDevice && curDevInfo->maxOutputChannels > 0 ) |
| 684 |
commonApi->info.defaultOutputDevice = client_index; |
| 685 |
} |
| 686 |
|
| 687 |
error: |
| 688 |
regfree( &port_regex ); |
| 689 |
free( jack_ports ); |
| 690 |
return result; |
| 691 |
} |
| 692 |
|
| 693 |
static void UpdateSampleRate( PaJackStream *stream, double sampleRate ) |
| 694 |
{ |
| 695 |
/* XXX: Maybe not the cleanest way of going about this? */ |
| 696 |
stream->cpuLoadMeasurer.samplingPeriod = stream->bufferProcessor.samplePeriod = 1. / sampleRate; |
| 697 |
stream->streamRepresentation.streamInfo.sampleRate = sampleRate; |
| 698 |
} |
| 699 |
|
| 700 |
static void JackErrorCallback( const char *msg ) |
| 701 |
{ |
| 702 |
if( pthread_self() == mainThread_ ) |
| 703 |
{ |
| 704 |
assert( msg ); |
| 705 |
jackErr_ = realloc( jackErr_, strlen( msg ) + 1 ); |
| 706 |
strcpy( jackErr_, msg ); |
| 707 |
} |
| 708 |
} |
| 709 |
|
| 710 |
static void JackOnShutdown( void *arg ) |
| 711 |
{ |
| 712 |
PaJackHostApiRepresentation *jackApi = (PaJackHostApiRepresentation *)arg; |
| 713 |
PaJackStream *stream = jackApi->processQueue; |
| 714 |
|
| 715 |
PA_DEBUG(( "%s: JACK server is shutting down\n", __FUNCTION__ )); |
| 716 |
for( ; stream; stream = stream->next ) |
| 717 |
{ |
| 718 |
stream->is_active = 0; |
| 719 |
} |
| 720 |
|
| 721 |
/* Make sure that the main thread doesn't get stuck waiting on the condition */ |
| 722 |
ASSERT_CALL( pthread_mutex_lock( &jackApi->mtx ), 0 ); |
| 723 |
jackApi->jackIsDown = 1; |
| 724 |
ASSERT_CALL( pthread_cond_signal( &jackApi->cond ), 0 ); |
| 725 |
ASSERT_CALL( pthread_mutex_unlock( &jackApi->mtx ), 0 ); |
| 726 |
|
| 727 |
} |
| 728 |
|
| 729 |
static int JackSrCb( jack_nframes_t nframes, void *arg ) |
| 730 |
{ |
| 731 |
PaJackHostApiRepresentation *jackApi = (PaJackHostApiRepresentation *)arg; |
| 732 |
double sampleRate = (double)nframes; |
| 733 |
PaJackStream *stream = jackApi->processQueue; |
| 734 |
|
| 735 |
/* Update all streams in process queue */ |
| 736 |
PA_DEBUG(( "%s: Acting on change in JACK samplerate: %f\n", __FUNCTION__, sampleRate )); |
| 737 |
for( ; stream; stream = stream->next ) |
| 738 |
{ |
| 739 |
if( stream->streamRepresentation.streamInfo.sampleRate != sampleRate ) |
| 740 |
{ |
| 741 |
PA_DEBUG(( "%s: Updating samplerate\n", __FUNCTION__ )); |
| 742 |
UpdateSampleRate( stream, sampleRate ); |
| 743 |
} |
| 744 |
} |
| 745 |
|
| 746 |
return 0; |
| 747 |
} |
| 748 |
|
| 749 |
static int JackXRunCb(void *arg) { |
| 750 |
PaJackHostApiRepresentation *hostApi = (PaJackHostApiRepresentation *)arg; |
| 751 |
assert( hostApi ); |
| 752 |
hostApi->xrun = TRUE; |
| 753 |
PA_DEBUG(( "%s: JACK signalled xrun\n", __FUNCTION__ )); |
| 754 |
return 0; |
| 755 |
} |
| 756 |
|
| 757 |
PaError PaJack_Initialize( PaUtilHostApiRepresentation **hostApi, |
| 758 |
PaHostApiIndex hostApiIndex ) |
| 759 |
{ |
| 760 |
PaError result = paNoError; |
| 761 |
PaJackHostApiRepresentation *jackHostApi; |
| 762 |
int activated = 0; |
| 763 |
jack_status_t jackStatus = 0; |
| 764 |
*hostApi = NULL; /* Initialize to NULL */ |
| 765 |
|
| 766 |
UNLESS( jackHostApi = (PaJackHostApiRepresentation*) |
| 767 |
PaUtil_AllocateMemory( sizeof(PaJackHostApiRepresentation) ), paInsufficientMemory ); |
| 768 |
UNLESS( jackHostApi->deviceInfoMemory = PaUtil_CreateAllocationGroup(), paInsufficientMemory ); |
| 769 |
|
| 770 |
mainThread_ = pthread_self(); |
| 771 |
ASSERT_CALL( pthread_mutex_init( &jackHostApi->mtx, NULL ), 0 ); |
| 772 |
ASSERT_CALL( pthread_cond_init( &jackHostApi->cond, NULL ), 0 ); |
| 773 |
|
| 774 |
/* Try to become a client of the JACK server. If we cannot do |
| 775 |
* this, then this API cannot be used. |
| 776 |
* |
| 777 |
* Without the JackNoStartServer option, the jackd server is started |
| 778 |
* automatically which we do not want. |
| 779 |
*/ |
| 780 |
|
| 781 |
jackHostApi->jack_client = jack_client_open( clientName_, JackNoStartServer, &jackStatus ); |
| 782 |
if( !jackHostApi->jack_client ) |
| 783 |
{ |
| 784 |
/* the V19 development docs say that if an implementation |
| 785 |
* detects that it cannot be used, it should return a NULL |
| 786 |
* interface and paNoError */ |
| 787 |
PA_DEBUG(( "%s: Couldn't connect to JACK, status: %d\n", __FUNCTION__, jackStatus )); |
| 788 |
result = paNoError; |
| 789 |
goto error; |
| 790 |
} |
| 791 |
|
| 792 |
jackHostApi->hostApiIndex = hostApiIndex; |
| 793 |
|
| 794 |
*hostApi = &jackHostApi->commonHostApiRep; |
| 795 |
(*hostApi)->info.structVersion = 1; |
| 796 |
(*hostApi)->info.type = paJACK; |
| 797 |
(*hostApi)->info.name = "JACK Audio Connection Kit"; |
| 798 |
|
| 799 |
/* Build a device list by querying the JACK server */ |
| 800 |
ENSURE_PA( BuildDeviceList( jackHostApi ) ); |
| 801 |
|
| 802 |
/* Register functions */ |
| 803 |
|
| 804 |
(*hostApi)->Terminate = Terminate; |
| 805 |
(*hostApi)->OpenStream = OpenStream; |
| 806 |
(*hostApi)->IsFormatSupported = IsFormatSupported; |
| 807 |
|
| 808 |
PaUtil_InitializeStreamInterface( &jackHostApi->callbackStreamInterface, |
| 809 |
CloseStream, StartStream, |
| 810 |
StopStream, AbortStream, |
| 811 |
IsStreamStopped, IsStreamActive, |
| 812 |
GetStreamTime, GetStreamCpuLoad, |
| 813 |
PaUtil_DummyRead, PaUtil_DummyWrite, |
| 814 |
PaUtil_DummyGetReadAvailable, |
| 815 |
PaUtil_DummyGetWriteAvailable ); |
| 816 |
|
| 817 |
PaUtil_InitializeStreamInterface( &jackHostApi->blockingStreamInterface, CloseStream, StartStream, |
| 818 |
StopStream, AbortStream, IsStreamStopped, IsStreamActive, |
| 819 |
GetStreamTime, PaUtil_DummyGetCpuLoad, |
| 820 |
BlockingReadStream, BlockingWriteStream, |
| 821 |
BlockingGetStreamReadAvailable, BlockingGetStreamWriteAvailable ); |
| 822 |
|
| 823 |
jackHostApi->inputBase = jackHostApi->outputBase = 0; |
| 824 |
jackHostApi->xrun = 0; |
| 825 |
jackHostApi->toAdd = jackHostApi->toRemove = NULL; |
| 826 |
jackHostApi->processQueue = NULL; |
| 827 |
jackHostApi->jackIsDown = 0; |
| 828 |
|
| 829 |
jack_on_shutdown( jackHostApi->jack_client, JackOnShutdown, jackHostApi ); |
| 830 |
jack_set_error_function( JackErrorCallback ); |
| 831 |
jackHostApi->jack_buffer_size = jack_get_buffer_size ( jackHostApi->jack_client ); |
| 832 |
/* Don't check for error, may not be supported (deprecated in at least jackdmp) */ |
| 833 |
jack_set_sample_rate_callback( jackHostApi->jack_client, JackSrCb, jackHostApi ); |
| 834 |
UNLESS( !jack_set_xrun_callback( jackHostApi->jack_client, JackXRunCb, jackHostApi ), paUnanticipatedHostError ); |
| 835 |
UNLESS( !jack_set_process_callback( jackHostApi->jack_client, JackCallback, jackHostApi ), paUnanticipatedHostError ); |
| 836 |
UNLESS( !jack_activate( jackHostApi->jack_client ), paUnanticipatedHostError ); |
| 837 |
activated = 1; |
| 838 |
|
| 839 |
return result; |
| 840 |
|
| 841 |
error: |
| 842 |
if( activated ) |
| 843 |
ASSERT_CALL( jack_deactivate( jackHostApi->jack_client ), 0 ); |
| 844 |
|
| 845 |
if( jackHostApi ) |
| 846 |
{ |
| 847 |
if( jackHostApi->jack_client ) |
| 848 |
ASSERT_CALL( jack_client_close( jackHostApi->jack_client ), 0 ); |
| 849 |
|
| 850 |
if( jackHostApi->deviceInfoMemory ) |
| 851 |
{ |
| 852 |
PaUtil_FreeAllAllocations( jackHostApi->deviceInfoMemory ); |
| 853 |
PaUtil_DestroyAllocationGroup( jackHostApi->deviceInfoMemory ); |
| 854 |
} |
| 855 |
|
| 856 |
PaUtil_FreeMemory( jackHostApi ); |
| 857 |
} |
| 858 |
return result; |
| 859 |
} |
| 860 |
|
| 861 |
|
| 862 |
static void Terminate( struct PaUtilHostApiRepresentation *hostApi ) |
| 863 |
{ |
| 864 |
PaJackHostApiRepresentation *jackHostApi = (PaJackHostApiRepresentation*)hostApi; |
| 865 |
|
| 866 |
/* note: this automatically disconnects all ports, since a deactivated |
| 867 |
* client is not allowed to have any ports connected */ |
| 868 |
ASSERT_CALL( jack_deactivate( jackHostApi->jack_client ), 0 ); |
| 869 |
|
| 870 |
ASSERT_CALL( pthread_mutex_destroy( &jackHostApi->mtx ), 0 ); |
| 871 |
ASSERT_CALL( pthread_cond_destroy( &jackHostApi->cond ), 0 ); |
| 872 |
|
| 873 |
ASSERT_CALL( jack_client_close( jackHostApi->jack_client ), 0 ); |
| 874 |
|
| 875 |
if( jackHostApi->deviceInfoMemory ) |
| 876 |
{ |
| 877 |
PaUtil_FreeAllAllocations( jackHostApi->deviceInfoMemory ); |
| 878 |
PaUtil_DestroyAllocationGroup( jackHostApi->deviceInfoMemory ); |
| 879 |
} |
| 880 |
|
| 881 |
PaUtil_FreeMemory( jackHostApi ); |
| 882 |
|
| 883 |
free( jackErr_ ); |
| 884 |
jackErr_ = NULL; |
| 885 |
} |
| 886 |
|
| 887 |
static PaError IsFormatSupported( struct PaUtilHostApiRepresentation *hostApi, |
| 888 |
const PaStreamParameters *inputParameters, |
| 889 |
const PaStreamParameters *outputParameters, |
| 890 |
double sampleRate ) |
| 891 |
{ |
| 892 |
int inputChannelCount = 0, outputChannelCount = 0; |
| 893 |
PaSampleFormat inputSampleFormat, outputSampleFormat; |
| 894 |
|
| 895 |
if( inputParameters ) |
| 896 |
{ |
| 897 |
inputChannelCount = inputParameters->channelCount; |
| 898 |
inputSampleFormat = inputParameters->sampleFormat; |
| 899 |
|
| 900 |
/* unless alternate device specification is supported, reject the use of |
| 901 |
paUseHostApiSpecificDeviceSpecification */ |
| 902 |
|
| 903 |
if( inputParameters->device == paUseHostApiSpecificDeviceSpecification ) |
| 904 |
return paInvalidDevice; |
| 905 |
|
| 906 |
/* check that input device can support inputChannelCount */ |
| 907 |
if( inputChannelCount > hostApi->deviceInfos[ inputParameters->device ]->maxInputChannels ) |
| 908 |
return paInvalidChannelCount; |
| 909 |
|
| 910 |
/* validate inputStreamInfo */ |
| 911 |
if( inputParameters->hostApiSpecificStreamInfo ) |
| 912 |
return paIncompatibleHostApiSpecificStreamInfo; /* this implementation doesn't use custom stream info */ |
| 913 |
} |
| 914 |
else |
| 915 |
{ |
| 916 |
inputChannelCount = 0; |
| 917 |
} |
| 918 |
|
| 919 |
if( outputParameters ) |
| 920 |
{ |
| 921 |
outputChannelCount = outputParameters->channelCount; |
| 922 |
outputSampleFormat = outputParameters->sampleFormat; |
| 923 |
|
| 924 |
/* unless alternate device specification is supported, reject the use of |
| 925 |
paUseHostApiSpecificDeviceSpecification */ |
| 926 |
|
| 927 |
if( outputParameters->device == paUseHostApiSpecificDeviceSpecification ) |
| 928 |
return paInvalidDevice; |
| 929 |
|
| 930 |
/* check that output device can support inputChannelCount */ |
| 931 |
if( outputChannelCount > hostApi->deviceInfos[ outputParameters->device ]->maxOutputChannels ) |
| 932 |
return paInvalidChannelCount; |
| 933 |
|
| 934 |
/* validate outputStreamInfo */ |
| 935 |
if( outputParameters->hostApiSpecificStreamInfo ) |
| 936 |
return paIncompatibleHostApiSpecificStreamInfo; /* this implementation doesn't use custom stream info */ |
| 937 |
} |
| 938 |
else |
| 939 |
{ |
| 940 |
outputChannelCount = 0; |
| 941 |
} |
| 942 |
|
| 943 |
/* |
| 944 |
The following check is not necessary for JACK. |
| 945 |
|
| 946 |
- if a full duplex stream is requested, check that the combination |
| 947 |
of input and output parameters is supported |
| 948 |
|
| 949 |
|
| 950 |
Because the buffer adapter handles conversion between all standard |
| 951 |
sample formats, the following checks are only required if paCustomFormat |
| 952 |
is implemented, or under some other unusual conditions. |
| 953 |
|
| 954 |
- check that input device can support inputSampleFormat, or that |
| 955 |
we have the capability to convert from outputSampleFormat to |
| 956 |
a native format |
| 957 |
|
| 958 |
- check that output device can support outputSampleFormat, or that |
| 959 |
we have the capability to convert from outputSampleFormat to |
| 960 |
a native format |
| 961 |
*/ |
| 962 |
|
| 963 |
/* check that the device supports sampleRate */ |
| 964 |
|
| 965 |
#define ABS(x) ( (x) > 0 ? (x) : -(x) ) |
| 966 |
if( ABS(sampleRate - jack_get_sample_rate(((PaJackHostApiRepresentation *) hostApi)->jack_client )) > 1 ) |
| 967 |
return paInvalidSampleRate; |
| 968 |
#undef ABS |
| 969 |
|
| 970 |
return paFormatIsSupported; |
| 971 |
} |
| 972 |
|
| 973 |
/* Basic stream initialization */ |
| 974 |
static PaError InitializeStream( PaJackStream *stream, PaJackHostApiRepresentation *hostApi, int numInputChannels, |
| 975 |
int numOutputChannels ) |
| 976 |
{ |
| 977 |
PaError result = paNoError; |
| 978 |
assert( stream ); |
| 979 |
|
| 980 |
memset( stream, 0, sizeof (PaJackStream) ); |
| 981 |
UNLESS( stream->stream_memory = PaUtil_CreateAllocationGroup(), paInsufficientMemory ); |
| 982 |
stream->jack_client = hostApi->jack_client; |
| 983 |
stream->hostApi = hostApi; |
| 984 |
|
| 985 |
if( numInputChannels > 0 ) |
| 986 |
{ |
| 987 |
UNLESS( stream->local_input_ports = |
| 988 |
(jack_port_t**) PaUtil_GroupAllocateMemory( stream->stream_memory, sizeof(jack_port_t*) * numInputChannels ), |
| 989 |
paInsufficientMemory ); |
| 990 |
memset( stream->local_input_ports, 0, sizeof(jack_port_t*) * numInputChannels ); |
| 991 |
UNLESS( stream->remote_output_ports = |
| 992 |
(jack_port_t**) PaUtil_GroupAllocateMemory( stream->stream_memory, sizeof(jack_port_t*) * numInputChannels ), |
| 993 |
paInsufficientMemory ); |
| 994 |
memset( stream->remote_output_ports, 0, sizeof(jack_port_t*) * numInputChannels ); |
| 995 |
} |
| 996 |
if( numOutputChannels > 0 ) |
| 997 |
{ |
| 998 |
UNLESS( stream->local_output_ports = |
| 999 |
(jack_port_t**) PaUtil_GroupAllocateMemory( stream->stream_memory, sizeof(jack_port_t*) * numOutputChannels ), |
| 1000 |
paInsufficientMemory ); |
| 1001 |
memset( stream->local_output_ports, 0, sizeof(jack_port_t*) * numOutputChannels ); |
| 1002 |
UNLESS( stream->remote_input_ports = |
| 1003 |
(jack_port_t**) PaUtil_GroupAllocateMemory( stream->stream_memory, sizeof(jack_port_t*) * numOutputChannels ), |
| 1004 |
paInsufficientMemory ); |
| 1005 |
memset( stream->remote_input_ports, 0, sizeof(jack_port_t*) * numOutputChannels ); |
| 1006 |
} |
| 1007 |
|
| 1008 |
stream->num_incoming_connections = numInputChannels; |
| 1009 |
stream->num_outgoing_connections = numOutputChannels; |
| 1010 |
|
| 1011 |
error: |
| 1012 |
return result; |
| 1013 |
} |
| 1014 |
|
| 1015 |
/*! |
| 1016 |
* Free resources associated with stream, and eventually stream itself. |
| 1017 |
* |
| 1018 |
* Frees allocated memory, and closes opened pcms. |
| 1019 |
*/ |
| 1020 |
static void CleanUpStream( PaJackStream *stream, int terminateStreamRepresentation, int terminateBufferProcessor ) |
| 1021 |
{ |
| 1022 |
int i; |
| 1023 |
assert( stream ); |
| 1024 |
|
| 1025 |
if( stream->isBlockingStream ) |
| 1026 |
BlockingEnd( stream ); |
| 1027 |
|
| 1028 |
for( i = 0; i < stream->num_incoming_connections; ++i ) |
| 1029 |
{ |
| 1030 |
if( stream->local_input_ports[i] ) |
| 1031 |
ASSERT_CALL( jack_port_unregister( stream->jack_client, stream->local_input_ports[i] ), 0 ); |
| 1032 |
} |
| 1033 |
for( i = 0; i < stream->num_outgoing_connections; ++i ) |
| 1034 |
{ |
| 1035 |
if( stream->local_output_ports[i] ) |
| 1036 |
ASSERT_CALL( jack_port_unregister( stream->jack_client, stream->local_output_ports[i] ), 0 ); |
| 1037 |
} |
| 1038 |
|
| 1039 |
if( terminateStreamRepresentation ) |
| 1040 |
PaUtil_TerminateStreamRepresentation( &stream->streamRepresentation ); |
| 1041 |
if( terminateBufferProcessor ) |
| 1042 |
PaUtil_TerminateBufferProcessor( &stream->bufferProcessor ); |
| 1043 |
|
| 1044 |
if( stream->stream_memory ) |
| 1045 |
{ |
| 1046 |
PaUtil_FreeAllAllocations( stream->stream_memory ); |
| 1047 |
PaUtil_DestroyAllocationGroup( stream->stream_memory ); |
| 1048 |
} |
| 1049 |
PaUtil_FreeMemory( stream ); |
| 1050 |
} |
| 1051 |
|
| 1052 |
static PaError WaitCondition( PaJackHostApiRepresentation *hostApi ) |
| 1053 |
{ |
| 1054 |
PaError result = paNoError; |
| 1055 |
int err = 0; |
| 1056 |
PaTime pt = PaUtil_GetTime(); |
| 1057 |
struct timespec ts; |
| 1058 |
|
| 1059 |
ts.tv_sec = (time_t) floor( pt + 10 * 60 /* 10 minutes */ ); |
| 1060 |
ts.tv_nsec = (long) ((pt - floor( pt )) * 1000000000); |
| 1061 |
/* XXX: Best enclose in loop, in case of spurious wakeups? */ |
| 1062 |
err = pthread_cond_timedwait( &hostApi->cond, &hostApi->mtx, &ts ); |
| 1063 |
|
| 1064 |
/* Make sure we didn't time out */ |
| 1065 |
UNLESS( err != ETIMEDOUT, paTimedOut ); |
| 1066 |
UNLESS( !err, paInternalError ); |
| 1067 |
|
| 1068 |
error: |
| 1069 |
return result; |
| 1070 |
} |
| 1071 |
|
| 1072 |
static PaError AddStream( PaJackStream *stream ) |
| 1073 |
{ |
| 1074 |
PaError result = paNoError; |
| 1075 |
PaJackHostApiRepresentation *hostApi = stream->hostApi; |
| 1076 |
/* Add to queue of streams that should be processed */ |
| 1077 |
ASSERT_CALL( pthread_mutex_lock( &hostApi->mtx ), 0 ); |
| 1078 |
if( !hostApi->jackIsDown ) |
| 1079 |
{ |
| 1080 |
hostApi->toAdd = stream; |
| 1081 |
/* Unlock mutex and await signal from processing thread */ |
| 1082 |
result = WaitCondition( stream->hostApi ); |
| 1083 |
} |
| 1084 |
ASSERT_CALL( pthread_mutex_unlock( &hostApi->mtx ), 0 ); |
| 1085 |
ENSURE_PA( result ); |
| 1086 |
|
| 1087 |
UNLESS( !hostApi->jackIsDown, paDeviceUnavailable ); |
| 1088 |
|
| 1089 |
error: |
| 1090 |
return result; |
| 1091 |
} |
| 1092 |
|
| 1093 |
/* Remove stream from processing queue */ |
| 1094 |
static PaError RemoveStream( PaJackStream *stream ) |
| 1095 |
{ |
| 1096 |
PaError result = paNoError; |
| 1097 |
PaJackHostApiRepresentation *hostApi = stream->hostApi; |
| 1098 |
|
| 1099 |
/* Add to queue over streams that should be processed */ |
| 1100 |
ASSERT_CALL( pthread_mutex_lock( &hostApi->mtx ), 0 ); |
| 1101 |
if( !hostApi->jackIsDown ) |
| 1102 |
{ |
| 1103 |
hostApi->toRemove = stream; |
| 1104 |
/* Unlock mutex and await signal from processing thread */ |
| 1105 |
result = WaitCondition( stream->hostApi ); |
| 1106 |
} |
| 1107 |
ASSERT_CALL( pthread_mutex_unlock( &hostApi->mtx ), 0 ); |
| 1108 |
ENSURE_PA( result ); |
| 1109 |
|
| 1110 |
error: |
| 1111 |
return result; |
| 1112 |
} |
| 1113 |
|
| 1114 |
/* Add stream to JACK callback processing queue */ |
| 1115 |
static PaError OpenStream( struct PaUtilHostApiRepresentation *hostApi, |
| 1116 |
PaStream** s, |
| 1117 |
const PaStreamParameters *inputParameters, |
| 1118 |
const PaStreamParameters *outputParameters, |
| 1119 |
double sampleRate, |
| 1120 |
unsigned long framesPerBuffer, |
| 1121 |
PaStreamFlags streamFlags, |
| 1122 |
PaStreamCallback *streamCallback, |
| 1123 |
void *userData ) |
| 1124 |
{ |
| 1125 |
PaError result = paNoError; |
| 1126 |
PaJackHostApiRepresentation *jackHostApi = (PaJackHostApiRepresentation*)hostApi; |
| 1127 |
PaJackStream *stream = NULL; |
| 1128 |
char *port_string = PaUtil_GroupAllocateMemory( jackHostApi->deviceInfoMemory, jack_port_name_size() ); |
| 1129 |
// In the worst case every character would be escaped which would double the string length. |
| 1130 |
// Add 1 for null terminator |
| 1131 |
size_t regex_escaped_client_name_size = jack_client_name_size() * 2 + 1; |
| 1132 |
unsigned long regex_size = regex_escaped_client_name_size + strlen(port_regex_suffix); |
| 1133 |
char *regex_pattern = PaUtil_GroupAllocateMemory( jackHostApi->deviceInfoMemory, regex_size ); |
| 1134 |
const char **jack_ports = NULL; |
| 1135 |
/* int jack_max_buffer_size = jack_get_buffer_size( jackHostApi->jack_client ); */ |
| 1136 |
int i; |
| 1137 |
int inputChannelCount, outputChannelCount; |
| 1138 |
const double jackSr = jack_get_sample_rate( jackHostApi->jack_client ); |
| 1139 |
PaSampleFormat inputSampleFormat = 0, outputSampleFormat = 0; |
| 1140 |
int bpInitialized = 0, srInitialized = 0; /* Initialized buffer processor and stream representation? */ |
| 1141 |
unsigned long ofs; |
| 1142 |
|
| 1143 |
/* validate platform specific flags */ |
| 1144 |
if( (streamFlags & paPlatformSpecificFlags) != 0 ) |
| 1145 |
return paInvalidFlag; /* unexpected platform specific flag */ |
| 1146 |
if( (streamFlags & paPrimeOutputBuffersUsingStreamCallback) != 0 ) |
| 1147 |
{ |
| 1148 |
streamFlags &= ~paPrimeOutputBuffersUsingStreamCallback; |
| 1149 |
/*return paInvalidFlag;*/ /* This implementation does not support buffer priming */ |
| 1150 |
} |
| 1151 |
|
| 1152 |
if( framesPerBuffer != paFramesPerBufferUnspecified ) |
| 1153 |
{ |
| 1154 |
/* Jack operates with power of two buffers, and we don't support non-integer buffer adaption (yet) */ |
| 1155 |
/*UNLESS( !(framesPerBuffer & (framesPerBuffer - 1)), paBufferTooBig );*/ /* TODO: Add descriptive error code? */ |
| 1156 |
} |
| 1157 |
|
| 1158 |
/* Preliminary checks */ |
| 1159 |
|
| 1160 |
if( inputParameters ) |
| 1161 |
{ |
| 1162 |
inputChannelCount = inputParameters->channelCount; |
| 1163 |
inputSampleFormat = inputParameters->sampleFormat; |
| 1164 |
|
| 1165 |
/* unless alternate device specification is supported, reject the use of |
| 1166 |
paUseHostApiSpecificDeviceSpecification */ |
| 1167 |
|
| 1168 |
if( inputParameters->device == paUseHostApiSpecificDeviceSpecification ) |
| 1169 |
return paInvalidDevice; |
| 1170 |
|
| 1171 |
/* check that input device can support inputChannelCount */ |
| 1172 |
if( inputChannelCount > hostApi->deviceInfos[ inputParameters->device ]->maxInputChannels ) |
| 1173 |
return paInvalidChannelCount; |
| 1174 |
|
| 1175 |
/* validate inputStreamInfo */ |
| 1176 |
if( inputParameters->hostApiSpecificStreamInfo ) |
| 1177 |
return paIncompatibleHostApiSpecificStreamInfo; /* this implementation doesn't use custom stream info */ |
| 1178 |
} |
| 1179 |
else |
| 1180 |
{ |
| 1181 |
inputChannelCount = 0; |
| 1182 |
} |
| 1183 |
|
| 1184 |
if( outputParameters ) |
| 1185 |
{ |
| 1186 |
outputChannelCount = outputParameters->channelCount; |
| 1187 |
outputSampleFormat = outputParameters->sampleFormat; |
| 1188 |
|
| 1189 |
/* unless alternate device specification is supported, reject the use of |
| 1190 |
paUseHostApiSpecificDeviceSpecification */ |
| 1191 |
|
| 1192 |
if( outputParameters->device == paUseHostApiSpecificDeviceSpecification ) |
| 1193 |
return paInvalidDevice; |
| 1194 |
|
| 1195 |
/* check that output device can support inputChannelCount */ |
| 1196 |
if( outputChannelCount > hostApi->deviceInfos[ outputParameters->device ]->maxOutputChannels ) |
| 1197 |
return paInvalidChannelCount; |
| 1198 |
|
| 1199 |
/* validate outputStreamInfo */ |
| 1200 |
if( outputParameters->hostApiSpecificStreamInfo ) |
| 1201 |
return paIncompatibleHostApiSpecificStreamInfo; /* this implementation doesn't use custom stream info */ |
| 1202 |
} |
| 1203 |
else |
| 1204 |
{ |
| 1205 |
outputChannelCount = 0; |
| 1206 |
} |
| 1207 |
|
| 1208 |
/* ... check that the sample rate exactly matches the ONE acceptable rate |
| 1209 |
* A: This rate isn't necessarily constant though? */ |
| 1210 |
|
| 1211 |
#define ABS(x) ( (x) > 0 ? (x) : -(x) ) |
| 1212 |
if( ABS(sampleRate - jackSr) > 1 ) |
| 1213 |
return paInvalidSampleRate; |
| 1214 |
#undef ABS |
| 1215 |
|
| 1216 |
UNLESS( stream = (PaJackStream*)PaUtil_AllocateMemory( sizeof(PaJackStream) ), paInsufficientMemory ); |
| 1217 |
ENSURE_PA( InitializeStream( stream, jackHostApi, inputChannelCount, outputChannelCount ) ); |
| 1218 |
|
| 1219 |
/* the blocking emulation, if necessary */ |
| 1220 |
stream->isBlockingStream = !streamCallback; |
| 1221 |
if( stream->isBlockingStream ) |
| 1222 |
{ |
| 1223 |
float latency = 0.001; /* 1ms is the absolute minimum we support */ |
| 1224 |
int minimum_buffer_frames = 0; |
| 1225 |
|
| 1226 |
if( inputParameters && inputParameters->suggestedLatency > latency ) |
| 1227 |
latency = inputParameters->suggestedLatency; |
| 1228 |
else if( outputParameters && outputParameters->suggestedLatency > latency ) |
| 1229 |
latency = outputParameters->suggestedLatency; |
| 1230 |
|
| 1231 |
/* the latency the user asked for indicates the minimum buffer size in frames */ |
| 1232 |
minimum_buffer_frames = (int) (latency * jack_get_sample_rate( jackHostApi->jack_client )); |
| 1233 |
|
| 1234 |
/* we also need to be able to store at least three full jack buffers to avoid dropouts */ |
| 1235 |
if( jackHostApi->jack_buffer_size * 3 > minimum_buffer_frames ) |
| 1236 |
minimum_buffer_frames = jackHostApi->jack_buffer_size * 3; |
| 1237 |
|
| 1238 |
/* setup blocking API data structures (FIXME: can fail) */ |
| 1239 |
BlockingBegin( stream, minimum_buffer_frames ); |
| 1240 |
|
| 1241 |
/* install our own callback for the blocking API */ |
| 1242 |
streamCallback = BlockingCallback; |
| 1243 |
userData = stream; |
| 1244 |
|
| 1245 |
PaUtil_InitializeStreamRepresentation( &stream->streamRepresentation, |
| 1246 |
&jackHostApi->blockingStreamInterface, streamCallback, userData ); |
| 1247 |
} |
| 1248 |
else |
| 1249 |
{ |
| 1250 |
PaUtil_InitializeStreamRepresentation( &stream->streamRepresentation, |
| 1251 |
&jackHostApi->callbackStreamInterface, streamCallback, userData ); |
| 1252 |
} |
| 1253 |
srInitialized = 1; |
| 1254 |
PaUtil_InitializeCpuLoadMeasurer( &stream->cpuLoadMeasurer, jackSr ); |
| 1255 |
|
| 1256 |
/* create the JACK ports. We cannot connect them until audio |
| 1257 |
* processing begins */ |
| 1258 |
|
| 1259 |
/* Register a unique set of ports for this stream |
| 1260 |
* TODO: Robust allocation of new port names */ |
| 1261 |
|
| 1262 |
ofs = jackHostApi->inputBase; |
| 1263 |
for( i = 0; i < inputChannelCount; i++ ) |
| 1264 |
{ |
| 1265 |
snprintf( port_string, jack_port_name_size(), "in_%lu", ofs + i ); |
| 1266 |
UNLESS( stream->local_input_ports[i] = jack_port_register( |
| 1267 |
jackHostApi->jack_client, port_string, |
| 1268 |
JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0 ), paInsufficientMemory ); |
| 1269 |
} |
| 1270 |
jackHostApi->inputBase += inputChannelCount; |
| 1271 |
|
| 1272 |
ofs = jackHostApi->outputBase; |
| 1273 |
for( i = 0; i < outputChannelCount; i++ ) |
| 1274 |
{ |
| 1275 |
snprintf( port_string, jack_port_name_size(), "out_%lu", ofs + i ); |
| 1276 |
UNLESS( stream->local_output_ports[i] = jack_port_register( |
| 1277 |
jackHostApi->jack_client, port_string, |
| 1278 |
JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0 ), paInsufficientMemory ); |
| 1279 |
} |
| 1280 |
jackHostApi->outputBase += outputChannelCount; |
| 1281 |
|
| 1282 |
/* look up the jack_port_t's for the remote ports. We could do |
| 1283 |
* this at stream start time, but doing it here ensures the |
| 1284 |
* name lookup only happens once. */ |
| 1285 |
|
| 1286 |
if( inputChannelCount > 0 ) |
| 1287 |
{ |
| 1288 |
int err = 0; |
| 1289 |
|
| 1290 |
/* Get output ports of our capture device */ |
| 1291 |
copy_string_and_escape_regex_chars( regex_pattern, |
| 1292 |
hostApi->deviceInfos[ inputParameters->device ]->name, |
| 1293 |
regex_escaped_client_name_size ); |
| 1294 |
strncat( regex_pattern, port_regex_suffix, regex_size ); |
| 1295 |
UNLESS( jack_ports = jack_get_ports( jackHostApi->jack_client, regex_pattern, |
| 1296 |
JACK_PORT_TYPE_FILTER, JackPortIsOutput ), paUnanticipatedHostError ); |
| 1297 |
for( i = 0; i < inputChannelCount && jack_ports[i]; i++ ) |
| 1298 |
{ |
| 1299 |
if( (stream->remote_output_ports[i] = jack_port_by_name( |
| 1300 |
jackHostApi->jack_client, jack_ports[i] )) == NULL ) |
| 1301 |
{ |
| 1302 |
err = 1; |
| 1303 |
break; |
| 1304 |
} |
| 1305 |
} |
| 1306 |
free( jack_ports ); |
| 1307 |
UNLESS( !err, paInsufficientMemory ); |
| 1308 |
|
| 1309 |
/* Fewer ports than expected? */ |
| 1310 |
UNLESS( i == inputChannelCount, paInternalError ); |
| 1311 |
} |
| 1312 |
|
| 1313 |
if( outputChannelCount > 0 ) |
| 1314 |
{ |
| 1315 |
int err = 0; |
| 1316 |
|
| 1317 |
/* Get input ports of our playback device */ |
| 1318 |
copy_string_and_escape_regex_chars( regex_pattern, |
| 1319 |
hostApi->deviceInfos[ outputParameters->device ]->name, |
| 1320 |
regex_escaped_client_name_size ); |
| 1321 |
strncat( regex_pattern, port_regex_suffix, regex_size ); |
| 1322 |
UNLESS( jack_ports = jack_get_ports( jackHostApi->jack_client, regex_pattern, |
| 1323 |
JACK_PORT_TYPE_FILTER, JackPortIsInput ), paUnanticipatedHostError ); |
| 1324 |
for( i = 0; i < outputChannelCount && jack_ports[i]; i++ ) |
| 1325 |
{ |
| 1326 |
if( (stream->remote_input_ports[i] = jack_port_by_name( |
| 1327 |
jackHostApi->jack_client, jack_ports[i] )) == 0 ) |
| 1328 |
{ |
| 1329 |
err = 1; |
| 1330 |
break; |
| 1331 |
} |
| 1332 |
} |
| 1333 |
free( jack_ports ); |
| 1334 |
UNLESS( !err , paInsufficientMemory ); |
| 1335 |
|
| 1336 |
/* Fewer ports than expected? */ |
| 1337 |
UNLESS( i == outputChannelCount, paInternalError ); |
| 1338 |
} |
| 1339 |
|
| 1340 |
ENSURE_PA( PaUtil_InitializeBufferProcessor( |
| 1341 |
&stream->bufferProcessor, |
| 1342 |
inputChannelCount, |
| 1343 |
inputSampleFormat, |
| 1344 |
paFloat32 | paNonInterleaved, /* hostInputSampleFormat */ |
| 1345 |
outputChannelCount, |
| 1346 |
outputSampleFormat, |
| 1347 |
paFloat32 | paNonInterleaved, /* hostOutputSampleFormat */ |
| 1348 |
jackSr, |
| 1349 |
streamFlags, |
| 1350 |
framesPerBuffer, |
| 1351 |
0, /* Ignored */ |
| 1352 |
paUtilUnknownHostBufferSize, /* Buffer size may vary on JACK's discretion */ |
| 1353 |
streamCallback, |
| 1354 |
userData ) ); |
| 1355 |
bpInitialized = 1; |
| 1356 |
|
| 1357 |
if( stream->num_incoming_connections > 0 ) |
| 1358 |
stream->streamRepresentation.streamInfo.inputLatency = (jack_port_get_latency( stream->remote_output_ports[0] ) |
| 1359 |
- jack_get_buffer_size( jackHostApi->jack_client ) /* One buffer is not counted as latency */ |
| 1360 |
+ PaUtil_GetBufferProcessorInputLatencyFrames( &stream->bufferProcessor )) / sampleRate; |
| 1361 |
if( stream->num_outgoing_connections > 0 ) |
| 1362 |
stream->streamRepresentation.streamInfo.outputLatency = (jack_port_get_latency( stream->remote_input_ports[0] ) |
| 1363 |
- jack_get_buffer_size( jackHostApi->jack_client ) /* One buffer is not counted as latency */ |
| 1364 |
+ PaUtil_GetBufferProcessorOutputLatencyFrames( &stream->bufferProcessor )) / sampleRate; |
| 1365 |
|
| 1366 |
stream->streamRepresentation.streamInfo.sampleRate = jackSr; |
| 1367 |
stream->t0 = jack_frame_time( jackHostApi->jack_client ); /* A: Time should run from Pa_OpenStream */ |
| 1368 |
|
| 1369 |
/* Add to queue of opened streams */ |
| 1370 |
ENSURE_PA( AddStream( stream ) ); |
| 1371 |
|
| 1372 |
*s = (PaStream*)stream; |
| 1373 |
|
| 1374 |
return result; |
| 1375 |
|
| 1376 |
error: |
| 1377 |
if( stream ) |
| 1378 |
CleanUpStream( stream, srInitialized, bpInitialized ); |
| 1379 |
|
| 1380 |
return result; |
| 1381 |
} |
| 1382 |
|
| 1383 |
/* |
| 1384 |
When CloseStream() is called, the multi-api layer ensures that |
| 1385 |
the stream has already been stopped or aborted. |
| 1386 |
*/ |
| 1387 |
static PaError CloseStream( PaStream* s ) |
| 1388 |
{ |
| 1389 |
PaError result = paNoError; |
| 1390 |
PaJackStream *stream = (PaJackStream*)s; |
| 1391 |
|
| 1392 |
/* Remove this stream from the processing queue */ |
| 1393 |
ENSURE_PA( RemoveStream( stream ) ); |
| 1394 |
|
| 1395 |
error: |
| 1396 |
CleanUpStream( stream, 1, 1 ); |
| 1397 |
return result; |
| 1398 |
} |
| 1399 |
|
| 1400 |
static PaError RealProcess( PaJackStream *stream, jack_nframes_t frames ) |
| 1401 |
{ |
| 1402 |
PaError result = paNoError; |
| 1403 |
PaStreamCallbackTimeInfo timeInfo = {0,0,0}; |
| 1404 |
int chn; |
| 1405 |
int framesProcessed; |
| 1406 |
const double sr = jack_get_sample_rate( stream->jack_client ); /* Shouldn't change during the process callback */ |
| 1407 |
PaStreamCallbackFlags cbFlags = 0; |
| 1408 |
|
| 1409 |
/* If the user has returned !paContinue from the callback we'll want to flush the internal buffers, |
| 1410 |
* when these are empty we can finally mark the stream as inactive */ |
| 1411 |
if( stream->callbackResult != paContinue && |
| 1412 |
PaUtil_IsBufferProcessorOutputEmpty( &stream->bufferProcessor ) ) |
| 1413 |
{ |
| 1414 |
stream->is_active = 0; |
| 1415 |
if( stream->streamRepresentation.streamFinishedCallback ) |
| 1416 |
stream->streamRepresentation.streamFinishedCallback( stream->streamRepresentation.userData ); |
| 1417 |
PA_DEBUG(( "%s: Callback finished\n", __FUNCTION__ )); |
| 1418 |
|
| 1419 |
goto end; |
| 1420 |
} |
| 1421 |
|
| 1422 |
timeInfo.currentTime = (jack_frame_time( stream->jack_client ) - stream->t0) / sr; |
| 1423 |
if( stream->num_incoming_connections > 0 ) |
| 1424 |
timeInfo.inputBufferAdcTime = timeInfo.currentTime - jack_port_get_latency( stream->remote_output_ports[0] ) |
| 1425 |
/ sr; |
| 1426 |
if( stream->num_outgoing_connections > 0 ) |
| 1427 |
timeInfo.outputBufferDacTime = timeInfo.currentTime + jack_port_get_latency( stream->remote_input_ports[0] ) |
| 1428 |
/ sr; |
| 1429 |
|
| 1430 |
PaUtil_BeginCpuLoadMeasurement( &stream->cpuLoadMeasurer ); |
| 1431 |
|
| 1432 |
if( stream->xrun ) |
| 1433 |
{ |
| 1434 |
/* XXX: Any way to tell which of these occurred? */ |
| 1435 |
cbFlags = paOutputUnderflow | paInputOverflow; |
| 1436 |
stream->xrun = FALSE; |
| 1437 |
} |
| 1438 |
PaUtil_BeginBufferProcessing( &stream->bufferProcessor, &timeInfo, |
| 1439 |
cbFlags ); |
| 1440 |
|
| 1441 |
if( stream->num_incoming_connections > 0 ) |
| 1442 |
PaUtil_SetInputFrameCount( &stream->bufferProcessor, frames ); |
| 1443 |
if( stream->num_outgoing_connections > 0 ) |
| 1444 |
PaUtil_SetOutputFrameCount( &stream->bufferProcessor, frames ); |
| 1445 |
|
| 1446 |
for( chn = 0; chn < stream->num_incoming_connections; chn++ ) |
| 1447 |
{ |
| 1448 |
jack_default_audio_sample_t *channel_buf = (jack_default_audio_sample_t*) |
| 1449 |
jack_port_get_buffer( stream->local_input_ports[chn], |
| 1450 |
frames ); |
| 1451 |
|
| 1452 |
PaUtil_SetNonInterleavedInputChannel( &stream->bufferProcessor, |
| 1453 |
chn, |
| 1454 |
channel_buf ); |
| 1455 |
} |
| 1456 |
|
| 1457 |
for( chn = 0; chn < stream->num_outgoing_connections; chn++ ) |
| 1458 |
{ |
| 1459 |
jack_default_audio_sample_t *channel_buf = (jack_default_audio_sample_t*) |
| 1460 |
jack_port_get_buffer( stream->local_output_ports[chn], |
| 1461 |
frames ); |
| 1462 |
|
| 1463 |
PaUtil_SetNonInterleavedOutputChannel( &stream->bufferProcessor, |
| 1464 |
chn, |
| 1465 |
channel_buf ); |
| 1466 |
} |
| 1467 |
|
| 1468 |
framesProcessed = PaUtil_EndBufferProcessing( &stream->bufferProcessor, |
| 1469 |
&stream->callbackResult ); |
| 1470 |
/* We've specified a host buffer size mode where every frame should be consumed by the buffer processor */ |
| 1471 |
assert( framesProcessed == frames ); |
| 1472 |
|
| 1473 |
PaUtil_EndCpuLoadMeasurement( &stream->cpuLoadMeasurer, framesProcessed ); |
| 1474 |
|
| 1475 |
end: |
| 1476 |
return result; |
| 1477 |
} |
| 1478 |
|
| 1479 |
/* Update the JACK callback's stream processing queue. */ |
| 1480 |
static PaError UpdateQueue( PaJackHostApiRepresentation *hostApi ) |
| 1481 |
{ |
| 1482 |
PaError result = paNoError; |
| 1483 |
int queueModified = 0; |
| 1484 |
const double jackSr = jack_get_sample_rate( hostApi->jack_client ); |
| 1485 |
int err; |
| 1486 |
|
| 1487 |
if( (err = pthread_mutex_trylock( &hostApi->mtx )) != 0 ) |
| 1488 |
{ |
| 1489 |
assert( err == EBUSY ); |
| 1490 |
return paNoError; |
| 1491 |
} |
| 1492 |
|
| 1493 |
if( hostApi->toAdd ) |
| 1494 |
{ |
| 1495 |
if( hostApi->processQueue ) |
| 1496 |
{ |
| 1497 |
PaJackStream *node = hostApi->processQueue; |
| 1498 |
/* Advance to end of queue */ |
| 1499 |
while( node->next ) |
| 1500 |
node = node->next; |
| 1501 |
|
| 1502 |
node->next = hostApi->toAdd; |
| 1503 |
} |
| 1504 |
else |
| 1505 |
{ |
| 1506 |
/* The only queue entry. */ |
| 1507 |
hostApi->processQueue = (PaJackStream *)hostApi->toAdd; |
| 1508 |
} |
| 1509 |
|
| 1510 |
/* If necessary, update stream state */ |
| 1511 |
if( hostApi->toAdd->streamRepresentation.streamInfo.sampleRate != jackSr ) |
| 1512 |
UpdateSampleRate( hostApi->toAdd, jackSr ); |
| 1513 |
|
| 1514 |
hostApi->toAdd = NULL; |
| 1515 |
queueModified = 1; |
| 1516 |
} |
| 1517 |
if( hostApi->toRemove ) |
| 1518 |
{ |
| 1519 |
int removed = 0; |
| 1520 |
PaJackStream *node = hostApi->processQueue, *prev = NULL; |
| 1521 |
assert( hostApi->processQueue ); |
| 1522 |
|
| 1523 |
while( node ) |
| 1524 |
{ |
| 1525 |
if( node == hostApi->toRemove ) |
| 1526 |
{ |
| 1527 |
if( prev ) |
| 1528 |
prev->next = node->next; |
| 1529 |
else |
| 1530 |
hostApi->processQueue = (PaJackStream *)node->next; |
| 1531 |
|
| 1532 |
removed = 1; |
| 1533 |
break; |
| 1534 |
} |
| 1535 |
|
| 1536 |
prev = node; |
| 1537 |
node = node->next; |
| 1538 |
} |
| 1539 |
UNLESS( removed, paInternalError ); |
| 1540 |
hostApi->toRemove = NULL; |
| 1541 |
PA_DEBUG(( "%s: Removed stream from processing queue\n", __FUNCTION__ )); |
| 1542 |
queueModified = 1; |
| 1543 |
} |
| 1544 |
|
| 1545 |
if( queueModified ) |
| 1546 |
{ |
| 1547 |
/* Signal that we've done what was asked of us */ |
| 1548 |
ASSERT_CALL( pthread_cond_signal( &hostApi->cond ), 0 ); |
| 1549 |
} |
| 1550 |
|
| 1551 |
error: |
| 1552 |
ASSERT_CALL( pthread_mutex_unlock( &hostApi->mtx ), 0 ); |
| 1553 |
|
| 1554 |
return result; |
| 1555 |
} |
| 1556 |
|
| 1557 |
/* Audio processing callback invoked periodically from JACK. */ |
| 1558 |
static int JackCallback( jack_nframes_t frames, void *userData ) |
| 1559 |
{ |
| 1560 |
PaError result = paNoError; |
| 1561 |
PaJackHostApiRepresentation *hostApi = (PaJackHostApiRepresentation *)userData; |
| 1562 |
PaJackStream *stream = NULL; |
| 1563 |
int xrun = hostApi->xrun; |
| 1564 |
hostApi->xrun = 0; |
| 1565 |
|
| 1566 |
assert( hostApi ); |
| 1567 |
|
| 1568 |
ENSURE_PA( UpdateQueue( hostApi ) ); |
| 1569 |
|
| 1570 |
/* Process each stream */ |
| 1571 |
stream = hostApi->processQueue; |
| 1572 |
for( ; stream; stream = stream->next ) |
| 1573 |
{ |
| 1574 |
if( xrun ) /* Don't override if already set */ |
| 1575 |
stream->xrun = 1; |
| 1576 |
|
| 1577 |
/* See if this stream is to be started */ |
| 1578 |
if( stream->doStart ) |
| 1579 |
{ |
| 1580 |
/* If we can't obtain a lock, we'll try next time */ |
| 1581 |
int err = pthread_mutex_trylock( &stream->hostApi->mtx ); |
| 1582 |
if( !err ) |
| 1583 |
{ |
| 1584 |
if( stream->doStart ) /* Could potentially change before obtaining the lock */ |
| 1585 |
{ |
| 1586 |
stream->is_active = 1; |
| 1587 |
stream->doStart = 0; |
| 1588 |
PA_DEBUG(( "%s: Starting stream\n", __FUNCTION__ )); |
| 1589 |
ASSERT_CALL( pthread_cond_signal( &stream->hostApi->cond ), 0 ); |
| 1590 |
stream->callbackResult = paContinue; |
| 1591 |
stream->isSilenced = 0; |
| 1592 |
} |
| 1593 |
|
| 1594 |
ASSERT_CALL( pthread_mutex_unlock( &stream->hostApi->mtx ), 0 ); |
| 1595 |
} |
| 1596 |
else |
| 1597 |
assert( err == EBUSY ); |
| 1598 |
} |
| 1599 |
else if( stream->doStop || stream->doAbort ) /* Should we stop/abort stream? */ |
| 1600 |
{ |
| 1601 |
if( stream->callbackResult == paContinue ) /* Ok, make it stop */ |
| 1602 |
{ |
| 1603 |
PA_DEBUG(( "%s: Stopping stream\n", __FUNCTION__ )); |
| 1604 |
stream->callbackResult = stream->doStop ? paComplete : paAbort; |
| 1605 |
} |
| 1606 |
} |
| 1607 |
|
| 1608 |
if( stream->is_active ) |
| 1609 |
ENSURE_PA( RealProcess( stream, frames ) ); |
| 1610 |
/* If we have just entered inactive state, silence output */ |
| 1611 |
if( !stream->is_active && !stream->isSilenced ) |
| 1612 |
{ |
| 1613 |
int i; |
| 1614 |
|
| 1615 |
/* Silence buffer after entering inactive state */ |
| 1616 |
PA_DEBUG(( "Silencing the output\n" )); |
| 1617 |
for( i = 0; i < stream->num_outgoing_connections; ++i ) |
| 1618 |
{ |
| 1619 |
jack_default_audio_sample_t *buffer = jack_port_get_buffer( stream->local_output_ports[i], frames ); |
| 1620 |
memset( buffer, 0, sizeof (jack_default_audio_sample_t) * frames ); |
| 1621 |
} |
| 1622 |
|
| 1623 |
stream->isSilenced = 1; |
| 1624 |
} |
| 1625 |
|
| 1626 |
if( stream->doStop || stream->doAbort ) |
| 1627 |
{ |
| 1628 |
/* See if RealProcess has acted on the request */ |
| 1629 |
if( !stream->is_active ) /* Ok, signal to the main thread that we've carried out the operation */ |
| 1630 |
{ |
| 1631 |
/* If we can't obtain a lock, we'll try next time */ |
| 1632 |
int err = pthread_mutex_trylock( &stream->hostApi->mtx ); |
| 1633 |
if( !err ) |
| 1634 |
{ |
| 1635 |
stream->doStop = stream->doAbort = 0; |
| 1636 |
ASSERT_CALL( pthread_cond_signal( &stream->hostApi->cond ), 0 ); |
| 1637 |
ASSERT_CALL( pthread_mutex_unlock( &stream->hostApi->mtx ), 0 ); |
| 1638 |
} |
| 1639 |
else |
| 1640 |
assert( err == EBUSY ); |
| 1641 |
} |
| 1642 |
} |
| 1643 |
} |
| 1644 |
|
| 1645 |
return 0; |
| 1646 |
error: |
| 1647 |
return -1; |
| 1648 |
} |
| 1649 |
|
| 1650 |
static PaError StartStream( PaStream *s ) |
| 1651 |
{ |
| 1652 |
PaError result = paNoError; |
| 1653 |
PaJackStream *stream = (PaJackStream*)s; |
| 1654 |
int i; |
| 1655 |
|
| 1656 |
/* Ready the processor */ |
| 1657 |
PaUtil_ResetBufferProcessor( &stream->bufferProcessor ); |
| 1658 |
|
| 1659 |
/* Connect the ports. Note that the ports may already have been connected by someone else in |
| 1660 |
* the meantime, in which case JACK returns EEXIST. */ |
| 1661 |
|
| 1662 |
if( stream->num_incoming_connections > 0 ) |
| 1663 |
{ |
| 1664 |
for( i = 0; i < stream->num_incoming_connections; i++ ) |
| 1665 |
{ |
| 1666 |
int r = jack_connect( stream->jack_client, jack_port_name( stream->remote_output_ports[i] ), |
| 1667 |
jack_port_name( stream->local_input_ports[i] ) ); |
| 1668 |
UNLESS( 0 == r || EEXIST == r, paUnanticipatedHostError ); |
| 1669 |
} |
| 1670 |
} |
| 1671 |
|
| 1672 |
if( stream->num_outgoing_connections > 0 ) |
| 1673 |
{ |
| 1674 |
for( i = 0; i < stream->num_outgoing_connections; i++ ) |
| 1675 |
{ |
| 1676 |
int r = jack_connect( stream->jack_client, jack_port_name( stream->local_output_ports[i] ), |
| 1677 |
jack_port_name( stream->remote_input_ports[i] ) ); |
| 1678 |
UNLESS( 0 == r || EEXIST == r, paUnanticipatedHostError ); |
| 1679 |
} |
| 1680 |
} |
| 1681 |
|
| 1682 |
stream->xrun = FALSE; |
| 1683 |
|
| 1684 |
/* Enable processing */ |
| 1685 |
|
| 1686 |
ASSERT_CALL( pthread_mutex_lock( &stream->hostApi->mtx ), 0 ); |
| 1687 |
stream->doStart = 1; |
| 1688 |
|
| 1689 |
/* Wait for stream to be started */ |
| 1690 |
result = WaitCondition( stream->hostApi ); |
| 1691 |
/* |
| 1692 |
do |
| 1693 |
{ |
| 1694 |
err = pthread_cond_timedwait( &stream->hostApi->cond, &stream->hostApi->mtx, &ts ); |
| 1695 |
} while( !stream->is_active && !err ); |
| 1696 |
*/ |
| 1697 |
if( result != paNoError ) /* Something went wrong, call off the stream start */ |
| 1698 |
{ |
| 1699 |
stream->doStart = 0; |
| 1700 |
stream->is_active = 0; /* Cancel any processing */ |
| 1701 |
} |
| 1702 |
ASSERT_CALL( pthread_mutex_unlock( &stream->hostApi->mtx ), 0 ); |
| 1703 |
|
| 1704 |
ENSURE_PA( result ); |
| 1705 |
|
| 1706 |
stream->is_running = TRUE; |
| 1707 |
PA_DEBUG(( "%s: Stream started\n", __FUNCTION__ )); |
| 1708 |
|
| 1709 |
error: |
| 1710 |
return result; |
| 1711 |
} |
| 1712 |
|
| 1713 |
static PaError RealStop( PaJackStream *stream, int abort ) |
| 1714 |
{ |
| 1715 |
PaError result = paNoError; |
| 1716 |
int i; |
| 1717 |
|
| 1718 |
if( stream->isBlockingStream ) |
| 1719 |
BlockingWaitEmpty ( stream ); |
| 1720 |
|
| 1721 |
ASSERT_CALL( pthread_mutex_lock( &stream->hostApi->mtx ), 0 ); |
| 1722 |
if( abort ) |
| 1723 |
stream->doAbort = 1; |
| 1724 |
else |
| 1725 |
stream->doStop = 1; |
| 1726 |
|
| 1727 |
/* Wait for stream to be stopped */ |
| 1728 |
result = WaitCondition( stream->hostApi ); |
| 1729 |
ASSERT_CALL( pthread_mutex_unlock( &stream->hostApi->mtx ), 0 ); |
| 1730 |
ENSURE_PA( result ); |
| 1731 |
|
| 1732 |
UNLESS( !stream->is_active, paInternalError ); |
| 1733 |
|
| 1734 |
PA_DEBUG(( "%s: Stream stopped\n", __FUNCTION__ )); |
| 1735 |
|
| 1736 |
error: |
| 1737 |
stream->is_running = FALSE; |
| 1738 |
|
| 1739 |
/* Disconnect ports belonging to this stream */ |
| 1740 |
|
| 1741 |
if( !stream->hostApi->jackIsDown ) /* XXX: Well? */ |
| 1742 |
{ |
| 1743 |
for( i = 0; i < stream->num_incoming_connections; i++ ) |
| 1744 |
{ |
| 1745 |
if( jack_port_connected( stream->local_input_ports[i] ) ) |
| 1746 |
{ |
| 1747 |
UNLESS( !jack_port_disconnect( stream->jack_client, stream->local_input_ports[i] ), |
| 1748 |
paUnanticipatedHostError ); |
| 1749 |
} |
| 1750 |
} |
| 1751 |
for( i = 0; i < stream->num_outgoing_connections; i++ ) |
| 1752 |
{ |
| 1753 |
if( jack_port_connected( stream->local_output_ports[i] ) ) |
| 1754 |
{ |
| 1755 |
UNLESS( !jack_port_disconnect( stream->jack_client, stream->local_output_ports[i] ), |
| 1756 |
paUnanticipatedHostError ); |
| 1757 |
} |
| 1758 |
} |
| 1759 |
} |
| 1760 |
|
| 1761 |
return result; |
| 1762 |
} |
| 1763 |
|
| 1764 |
static PaError StopStream( PaStream *s ) |
| 1765 |
{ |
| 1766 |
assert(s); |
| 1767 |
return RealStop( (PaJackStream *)s, 0 ); |
| 1768 |
} |
| 1769 |
|
| 1770 |
static PaError AbortStream( PaStream *s ) |
| 1771 |
{ |
| 1772 |
assert(s); |
| 1773 |
return RealStop( (PaJackStream *)s, 1 ); |
| 1774 |
} |
| 1775 |
|
| 1776 |
static PaError IsStreamStopped( PaStream *s ) |
| 1777 |
{ |
| 1778 |
PaJackStream *stream = (PaJackStream*)s; |
| 1779 |
return !stream->is_running; |
| 1780 |
} |
| 1781 |
|
| 1782 |
|
| 1783 |
static PaError IsStreamActive( PaStream *s ) |
| 1784 |
{ |
| 1785 |
PaJackStream *stream = (PaJackStream*)s; |
| 1786 |
return stream->is_active; |
| 1787 |
} |
| 1788 |
|
| 1789 |
|
| 1790 |
static PaTime GetStreamTime( PaStream *s ) |
| 1791 |
{ |
| 1792 |
PaJackStream *stream = (PaJackStream*)s; |
| 1793 |
|
| 1794 |
/* A: Is this relevant?? --> TODO: what if we're recording-only? */ |
| 1795 |
return (jack_frame_time( stream->jack_client ) - stream->t0) / (PaTime)jack_get_sample_rate( stream->jack_client ); |
| 1796 |
} |
| 1797 |
|
| 1798 |
|
| 1799 |
static double GetStreamCpuLoad( PaStream* s ) |
| 1800 |
{ |
| 1801 |
PaJackStream *stream = (PaJackStream*)s; |
| 1802 |
return PaUtil_GetCpuLoad( &stream->cpuLoadMeasurer ); |
| 1803 |
} |
| 1804 |
|
| 1805 |
PaError PaJack_SetClientName( const char* name ) |
| 1806 |
{ |
| 1807 |
if( strlen( name ) > jack_client_name_size() ) |
| 1808 |
{ |
| 1809 |
/* OK, I don't know any better error code */ |
| 1810 |
return paInvalidFlag; |
| 1811 |
} |
| 1812 |
clientName_ = name; |
| 1813 |
return paNoError; |
| 1814 |
} |
| 1815 |
|
| 1816 |
PaError PaJack_GetClientName(const char** clientName) |
| 1817 |
{ |
| 1818 |
PaError result = paNoError; |
| 1819 |
PaJackHostApiRepresentation* jackHostApi = NULL; |
| 1820 |
PaJackHostApiRepresentation** ref = &jackHostApi; |
| 1821 |
ENSURE_PA( PaUtil_GetHostApiRepresentation( (PaUtilHostApiRepresentation**)ref, paJACK ) ); |
| 1822 |
*clientName = jack_get_client_name( jackHostApi->jack_client ); |
| 1823 |
|
| 1824 |
error: |
| 1825 |
return result; |
| 1826 |
} |
| 1827 |
#endif |
| 1828 |
|