PortAudio 2.0
pa_mac_core_internal.h
Go to the documentation of this file.
1/*
2 * Internal interfaces for PortAudio Apple AUHAL implementation
3 *
4 * PortAudio Portable Real-Time Audio Library
5 * Latest Version at: http://www.portaudio.com
6 *
7 * Written by Bjorn Roche of XO Audio LLC, from PA skeleton code.
8 * Portions copied from code by Dominic Mazzoni (who wrote a HAL implementation)
9 *
10 * Dominic's code was based on code by Phil Burk, Darren Gibbs,
11 * Gord Peters, Stephane Letz, and Greg Pfiel.
12 *
13 * The following people also deserve acknowledgements:
14 *
15 * Olivier Tristan for feedback and testing
16 * Glenn Zelniker and Z-Systems engineering for sponsoring the Blocking I/O
17 * interface.
18 *
19 *
20 * Based on the Open Source API proposed by Ross Bencina
21 * Copyright (c) 1999-2002 Ross Bencina, Phil Burk
22 *
23 * Permission is hereby granted, free of charge, to any person obtaining
24 * a copy of this software and associated documentation files
25 * (the "Software"), to deal in the Software without restriction,
26 * including without limitation the rights to use, copy, modify, merge,
27 * publish, distribute, sublicense, and/or sell copies of the Software,
28 * and to permit persons to whom the Software is furnished to do so,
29 * subject to the following conditions:
30 *
31 * The above copyright notice and this permission notice shall be
32 * included in all copies or substantial portions of the Software.
33 *
34 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
35 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
36 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
37 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
38 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
39 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
40 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
41 */
42
43/*
44 * The text above constitutes the entire PortAudio license; however,
45 * the PortAudio community also makes the following non-binding requests:
46 *
47 * Any person wishing to distribute modifications to the Software is
48 * requested to send the modifications to the original developer so that
49 * they can be incorporated into the canonical version. It is also
50 * requested that these non-binding requests be included along with the
51 * license above.
52 */
53
61#ifndef PA_MAC_CORE_INTERNAL_H__
62#define PA_MAC_CORE_INTERNAL_H__
63
64#include <CoreAudio/CoreAudio.h>
65#include <CoreServices/CoreServices.h>
66#include <AudioUnit/AudioUnit.h>
67#include <AudioToolbox/AudioToolbox.h>
68#include <dispatch/dispatch.h>
69
70#include "portaudio.h"
71#include "pa_util.h"
72#include "pa_hostapi.h"
73#include "pa_stream.h"
74#include "pa_allocation.h"
75#include "pa_cpuload.h"
76#include "pa_process.h"
77#include "pa_ringbuffer.h"
78
80
81/* function prototypes */
82
83#ifdef __cplusplus
84extern "C"
85{
86#endif /* __cplusplus */
87
89
90#ifdef __cplusplus
91}
92#endif /* __cplusplus */
93
94#define RING_BUFFER_ADVANCE_DENOMINATOR (4)
95
96PaError ReadStream( PaStream* stream, void *buffer, unsigned long frames );
97PaError WriteStream( PaStream* stream, const void *buffer, unsigned long frames );
98signed long GetStreamReadAvailable( PaStream* stream );
99signed long GetStreamWriteAvailable( PaStream* stream );
100
101/* PaMacAUHAL - host api datastructure specific to this implementation */
102typedef struct
103{
107
109
110 /* implementation specific data goes here */
112 AudioDeviceID *devIds; /*array of all audio devices*/
113 AudioDeviceID defaultIn;
114 AudioDeviceID defaultOut;
115}
117
119{
120 /* Values in Frames from property queries. */
123 // UInt32 streamLatency; // Seems to be the same as deviceLatency!?
125 /* Current device sample rate. May change!
126 These are initialized to the nominal device sample rate,
127 and updated with the actual sample rate, when/where available.
128 Note that these are the *device* sample rates, prior to any required
129 SR conversion. */
130 Float64 sampleRate;
131 Float64 samplePeriod; // reciprocal
132}
134
135/* stream data structure specifically for this implementation */
136typedef struct PaMacCoreStream
137{
141
142 /* implementation specific data goes here */
144 AudioUnit inputUnit;
145 AudioUnit outputUnit;
146 AudioDeviceID inputDevice;
147 AudioDeviceID outputDevice;
153 /* We use this ring buffer when input and out devs are different. */
155 /* We may need to do SR conversion on input. */
156 AudioConverterRef inputSRConverter;
157 /* We need to preallocate an inputBuffer for reading data. */
158 AudioBufferList inputAudioBufferList;
159 AudioTimeStamp startTime;
160 /* FIXME: instead of volatile, these should be properly memory barriered */
161 volatile uint32_t xrunFlags; /*PaStreamCallbackFlags*/
162 volatile enum {
163 STOPPED = 0, /* playback is completely stopped,
164 and the user has called StopStream(). */
165 CALLBACK_STOPPED = 1, /* callback has requested stop,
166 but user has not yet called StopStream(). */
167 STOPPING = 2, /* The stream is in the process of closing
168 because the user has called StopStream.
169 This state is just used internally;
170 externally it is indistinguishable from
171 ACTIVE.*/
172 ACTIVE = 3 /* The stream is active and running. */
177
178 /* The kAudioOutputUnitProperty_IsRunning listener may not do any work in
179 the CoreAudio context it is delivered in (see startStopCallback); it
180 just pokes this dispatch source, whose handler runs on the serial queue
181 and detects stops that happened without StopStream/AbortStream. */
182 dispatch_queue_t startStopQueue;
183 dispatch_source_t startStopSource;
184 /* Set by StartStream, consumed (atomically) by whoever fires the
185 streamFinishedCallback, so it fires at most once per start. */
186 volatile int32_t streamFinishedArmed;
187
188 /* data updated by main thread and notifications, protected by timingInformationMutex */
190 pthread_mutex_t timingInformationMutex;
191
192 /* These are written by the PA thread or from CoreAudio callbacks. Protected by the mutex. */
196
197 /* Offsets in seconds to be applied to Apple timestamps to convert them to PA timestamps.
198 * While the io proc is active, the following values are only accessed and manipulated by the ioproc */
202}
204
205#endif /* PA_MAC_CORE_INTERNAL_H__ */
Allocation Group prototypes. An Allocation Group makes it easy to allocate multiple blocks of memory ...
Functions to assist in measuring the CPU utilization of a callback stream. Used to implement the Pa_G...
Interfaces and representation structures used by pa_front.c to manage and communicate with host API i...
PaError WriteStream(PaStream *stream, const void *buffer, unsigned long frames)
Definition pa_mac_core_blocking.c:502
PaError ReadStream(PaStream *stream, void *buffer, unsigned long frames)
Definition pa_mac_core_blocking.c:423
struct PaMacCoreDeviceProperties PaMacCoreDeviceProperties
signed long GetStreamReadAvailable(PaStream *stream)
Definition pa_mac_core_blocking.c:625
struct PaMacCoreStream PaMacCoreStream
PaError PaMacCore_Initialize(PaUtilHostApiRepresentation **hostApi, PaHostApiIndex index)
Definition pa_mac_core.c:818
signed long GetStreamWriteAvailable(PaStream *stream)
Definition pa_mac_core_blocking.c:634
Buffer Processor prototypes. A Buffer Processor performs buffer length adaption, coordinates sample f...
Single-reader single-writer lock-free ring buffer.
Stream interfaces, representation structures and helper functions used to interface between pa_front....
Prototypes for utility functions used by PortAudio implementations.
The portable PortAudio API.
void PaStream
Definition portaudio.h:643
int PaError
Definition portaudio.h:122
int PaHostApiIndex
Definition portaudio.h:240
char buffer[NUM_BYTES]
Definition recplay.c:26
Definition pa_mac_core_internal.h:103
long devCount
Definition pa_mac_core_internal.h:111
AudioDeviceID * devIds
Definition pa_mac_core_internal.h:112
PaUtilStreamInterface callbackStreamInterface
Definition pa_mac_core_internal.h:105
PaUtilAllocationGroup * allocations
Definition pa_mac_core_internal.h:108
PaUtilStreamInterface blockingStreamInterface
Definition pa_mac_core_internal.h:106
PaUtilHostApiRepresentation inheritedHostApiRep
Definition pa_mac_core_internal.h:104
AudioDeviceID defaultIn
Definition pa_mac_core_internal.h:113
AudioDeviceID defaultOut
Definition pa_mac_core_internal.h:114
Definition pa_mac_core_blocking.h:79
Definition pa_mac_core_internal.h:119
Float64 sampleRate
Definition pa_mac_core_internal.h:130
Float64 samplePeriod
Definition pa_mac_core_internal.h:131
UInt32 bufferFrameSize
Definition pa_mac_core_internal.h:122
UInt32 deviceLatency
Definition pa_mac_core_internal.h:124
UInt32 safetyOffset
Definition pa_mac_core_internal.h:121
Definition pa_mac_core_internal.h:137
Float64 timestampOffsetCombined
Definition pa_mac_core_internal.h:193
AudioUnit inputUnit
Definition pa_mac_core_internal.h:144
PaUtilRingBuffer inputRingBuffer
Definition pa_mac_core_internal.h:154
size_t inputFramesPerBuffer
Definition pa_mac_core_internal.h:150
PaUtilStreamRepresentation streamRepresentation
Definition pa_mac_core_internal.h:138
double sampleRate
Definition pa_mac_core_internal.h:174
Float64 timestampOffsetInputDevice
Definition pa_mac_core_internal.h:194
Float64 timestampOffsetOutputDevice_ioProcCopy
Definition pa_mac_core_internal.h:201
Float64 timestampOffsetInputDevice_ioProcCopy
Definition pa_mac_core_internal.h:200
dispatch_source_t startStopSource
Definition pa_mac_core_internal.h:183
PaUtilCpuLoadMeasurer cpuLoadMeasurer
Definition pa_mac_core_internal.h:139
size_t outputFramesPerBuffer
Definition pa_mac_core_internal.h:151
PaMacCoreDeviceProperties inputProperties
Definition pa_mac_core_internal.h:175
PaMacCoreDeviceProperties outputProperties
Definition pa_mac_core_internal.h:176
size_t userInChan
Definition pa_mac_core_internal.h:148
Float64 timestampOffsetOutputDevice
Definition pa_mac_core_internal.h:195
AudioUnit outputUnit
Definition pa_mac_core_internal.h:145
AudioDeviceID inputDevice
Definition pa_mac_core_internal.h:146
Float64 timestampOffsetCombined_ioProcCopy
Definition pa_mac_core_internal.h:199
int timingInformationMutexIsInitialized
Definition pa_mac_core_internal.h:189
AudioBufferList inputAudioBufferList
Definition pa_mac_core_internal.h:158
PaUtilBufferProcessor bufferProcessor
Definition pa_mac_core_internal.h:140
size_t userOutChan
Definition pa_mac_core_internal.h:149
AudioTimeStamp startTime
Definition pa_mac_core_internal.h:159
pthread_mutex_t timingInformationMutex
Definition pa_mac_core_internal.h:190
AudioConverterRef inputSRConverter
Definition pa_mac_core_internal.h:156
dispatch_queue_t startStopQueue
Definition pa_mac_core_internal.h:182
bool bufferProcessorIsInitialized
Definition pa_mac_core_internal.h:143
@ STOPPED
Definition pa_mac_core_internal.h:163
@ CALLBACK_STOPPED
Definition pa_mac_core_internal.h:165
@ STOPPING
Definition pa_mac_core_internal.h:167
@ ACTIVE
Definition pa_mac_core_internal.h:172
volatile int32_t streamFinishedArmed
Definition pa_mac_core_internal.h:186
AudioDeviceID outputDevice
Definition pa_mac_core_internal.h:147
PaMacBlio blio
Definition pa_mac_core_internal.h:152
volatile uint32_t xrunFlags
Definition pa_mac_core_internal.h:161
enum PaMacCoreStream::@0 state
Definition pa_allocation.h:66
The main buffer processor data structure.
Definition pa_process.h:253
Definition pa_cpuload.h:56
Definition pa_hostapi.h:194
Definition pa_ringbuffer.h:94
Definition pa_stream.h:67
Definition pa_stream.h:147