1 /*
2  * Internal blocking 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 
54 /**
55  @file
56  @ingroup hostapi_src
57 */
58 
59 #ifndef PA_MAC_CORE_BLOCKING_H_
60 #define PA_MAC_CORE_BLOCKING_H_
61 
62 #include "pa_ringbuffer.h"
63 #include "portaudio.h"
64 #include "pa_mac_core_utilities.h"
65 
66 /*
67  * Number of milliseconds to busy wait while waiting for data in blocking calls.
68  */
69 #define PA_MAC_BLIO_BUSY_WAIT_SLEEP_INTERVAL (5)
70 /*
71  * Define exactly one of these blocking methods
72  * PA_MAC_BLIO_MUTEX is not actively maintained.
73  */
74 #define PA_MAC_BLIO_BUSY_WAIT
75 /*
76 #define PA_MAC_BLIO_MUTEX
77 */
78 
79 typedef struct {
80     PaUtilRingBuffer inputRingBuffer;
81     PaUtilRingBuffer outputRingBuffer;
82     ring_buffer_size_t ringBufferFrames;
83     PaSampleFormat inputSampleFormat;
84     size_t inputSampleSizeActual;
85     size_t inputSampleSizePow2;
86     PaSampleFormat outputSampleFormat;
87     size_t outputSampleSizeActual;
88     size_t outputSampleSizePow2;
89 
90     int inChan;
91     int outChan;
92 
93     //PaStreamCallbackFlags statusFlags;
94     uint32_t statusFlags;
95     PaError errors;
96 
97     /* Here we handle blocking, using condition variables. */
98 #ifdef  PA_MAC_BLIO_MUTEX
99     volatile bool isInputEmpty;
100     pthread_mutex_t inputMutex;
101     pthread_cond_t inputCond;
102 
103     volatile bool isOutputFull;
104     pthread_mutex_t outputMutex;
105     pthread_cond_t outputCond;
106 #endif
107 }
108 PaMacBlio;
109 
110 /*
111  * These functions operate on condition and related variables.
112  */
113 
114 PaError initializeBlioRingBuffers(
115                                        PaMacBlio *blio,
116                                        PaSampleFormat inputSampleFormat,
117                                        PaSampleFormat outputSampleFormat,
118                                        long ringBufferSizeInFrames,
119                                        int inChan,
120                                        int outChan );
121 PaError destroyBlioRingBuffers( PaMacBlio *blio );
122 PaError resetBlioRingBuffers( PaMacBlio *blio );
123 
124 int BlioCallback(
125         const void *input, void *output,
126         unsigned long frameCount,
127         const PaStreamCallbackTimeInfo* timeInfo,
128         PaStreamCallbackFlags statusFlags,
129         void *userData );
130 
131 PaError waitUntilBlioWriteBufferIsEmpty( PaMacBlio *blio, double sampleRate,
132                                         size_t framesPerBuffer );
133 
134 #endif /*PA_MAC_CORE_BLOCKING_H_*/
135