summaryrefslogtreecommitdiffstatshomepage
path: root/3rdparty/portaudio/src/hostapi/pulseaudio/pa_linux_pulseaudio_internal.h
blob: 84bff257993106caa7e003b746cb3a5a33ec5dd6 (plain) (blame)
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*
 * PulseAudio host to play natively in Linux based systems without
 * ALSA emulation
 *
 * Copyright (c) 2014-2023 Tuukka Pasanen <tuukka.pasanen@ilmi.fi>
 * Copyright (c) 2016 Sqweek
 *
 * Based on the Open Source API proposed by Ross Bencina
 * Copyright (c) 1999-2002 Ross Bencina, Phil Burk
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files
 * (the "Software"), to deal in the Software without restriction,
 * including without limitation the rights to use, copy, modify, merge,
 * publish, distribute, sublicense, and/or sell copies of the Software,
 * and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

/*
 * The text above constitutes the entire PortAudio license; however,
 * the PortAudio community also makes the following non-binding requests:
 *
 * Any person wishing to distribute modifications to the Software is
 * requested to send the modifications to the original developer so that
 * they can be incorporated into the canonical version. It is also
 * requested that these non-binding requests be included along with the
 * license above.
 */

#ifndef _PA_HOSTAPI_PULSEAUDIO_H_
#define _PA_HOSTAPI_PULSEAUDIO_H_

#include "pa_util.h"
#include "pa_allocation.h"
#include "pa_hostapi.h"
#include "pa_stream.h"
#include "pa_cpuload.h"
#include "pa_process.h"

#include "pa_unix_util.h"
#include "pa_ringbuffer.h"
#include "pa_debugprint.h"

/* PulseAudio headers */
#include <stdio.h>
#include <string.h>
#include <pulse/pulseaudio.h>

#ifdef __cplusplus
extern "C"
{
#endif                          /* __cplusplus */

/* prototypes for functions declared in this file */

#define PA_PULSEAUDIO_SET_LAST_HOST_ERROR(errorCode, errorText) \
    PaUtil_SetLastHostErrorInfo(paInDevelopment, errorCode, errorText)

#define PAPULSEAUDIO_MAX_DEVICECOUNT 1024
#define PAPULSEAUDIO_MAX_DEVICENAME 1024

/* Default latency values to expose. Chosen by trial and error to be reasonable. */
#define PA_PULSEAUDIO_DEFAULT_MIN_LATENCY 0.010
#define PA_PULSEAUDIO_DEFAULT_MAX_LATENCY 0.080

/* Just some value that Pulseaudio can handle */
#define PAPULSEAUDIO_FRAMESPERBUFFERUNSPEC 32

/* Assuming of 2 seconds of 44100 Hz sample rate with FLOAT (4 bytes) and stereo channels (2 channels).
   You should have pretty good size buffer with this. If output/intput doesn't happens in 2 second we
   have more trouble than this buffer.
   @todo change this to something more sophisticated */
#define PULSEAUDIO_BUFFER_SIZE (96100 * 4 * 2)

typedef struct
{
    PaUtilHostApiRepresentation inheritedHostApiRep;
    PaUtilStreamInterface callbackStreamInterface;
    PaUtilStreamInterface blockingStreamInterface;

    PaUtilAllocationGroup *allocations;

    PaHostApiIndex hostApiIndex;
    PaDeviceInfo deviceInfoArray[PAPULSEAUDIO_MAX_DEVICECOUNT];
    char *pulseaudioDeviceNames[PAPULSEAUDIO_MAX_DEVICECOUNT];
    pa_sample_spec pulseaudioDefaultSampleSpec;

    /* PulseAudio stuff goes here */
    pa_threaded_mainloop *mainloop;
    pa_mainloop_api *mainloopApi;
    pa_context *context;
    int deviceCount;
    pa_time_event *timeEvent;
}
PaPulseAudio_HostApiRepresentation;

/* PaPulseAudio_Stream - a stream data structure specifically for this implementation */

typedef struct PaPulseAudio_Stream
{
    PaUtilStreamRepresentation streamRepresentation;
    PaUtilCpuLoadMeasurer cpuLoadMeasurer;
    PaUtilBufferProcessor bufferProcessor;
    PaPulseAudio_HostApiRepresentation *hostapi;

    unsigned long framesPerHostCallback;
    pa_threaded_mainloop *mainloop;
    pa_context *context;
    pa_sample_spec outputSampleSpec;
    pa_sample_spec inputSampleSpec;
    pa_stream *outputStream;
    pa_stream *inputStream;
    pa_buffer_attr outputBufferAttr;
    pa_buffer_attr inputBufferAttr;
    int outputUnderflows;
    int outputChannelCount;
    int inputChannelCount;

    size_t maxFramesPerBuffer;
    size_t maxFramesHostPerBuffer;
    int outputFrameSize;
    int inputFrameSize;

    PaDeviceIndex inputDevice;
    PaDeviceIndex outputDevice;

    char *outputStreamName;
    char *inputStreamName;

    PaUtilRingBuffer inputRing;

    size_t missedBytes;

    /* Used in communication between threads
     *
     * State machine works like this:
     * When stream is wanted to start with Pa_StartStream
     * then isActive is 1 if opening of devices goes well
     * and isStopped is then 0.
     *
     * When requested to stop isStopped is 1 on isActive is 0
     * and nothing should be written to ouput or read from input
     * anymore
     *
     * Pulseaudio does not like this as it creates streams and they
     * start when they are ready and it can be after we have
     * exited Pa_StartStream or before if get's kicked up very fast
     *
     * pulseaudioIsActive and pulseaudioIsStopped are used to find if
     * there is stream active or stopped in pulseaudio side. They
     * live their own life besides isActive and isStopped to make sure
     * that portaudio will have input and output available before
     * reading or writing to stream.
     */
    volatile sig_atomic_t isActive;
    volatile sig_atomic_t isStopped;
    volatile sig_atomic_t pulseaudioIsActive;
    volatile sig_atomic_t pulseaudioIsStopped;

}
PaPulseAudio_Stream;

/* PulseAudio Error checking macro */
#define PA_PULSEAUDIO_IS_ERROR(pastream, errorCode) \
    if( !(pastream) || \
        !(pastream)->context || \
        !PA_CONTEXT_IS_GOOD( pa_context_get_state( (pastream)->context ) ) || \
        ( (pastream)->outputStream && \
        !PA_STREAM_IS_GOOD( pa_stream_get_state( (pastream)->outputStream ) ) ) || \
        ( (pastream)->inputStream && \
        !PA_STREAM_IS_GOOD( pa_stream_get_state( (pastream)->inputStream ) ) ) ) \
    { \
        if( !(pastream) || \
            ( (pastream)->context && \
              pa_context_get_state( (pastream)->context ) == PA_CONTEXT_FAILED ) || \
            ( (pastream)->outputStream && \
              pa_stream_get_state( (pastream)->outputStream ) == PA_STREAM_FAILED ) || \
            ( (pastream)->inputStream && \
               pa_stream_get_state( (pastream)->inputStream ) == PA_STREAM_FAILED ) ) \
            { \
                return errorCode; \
            } \
    } \
    if( !pastream->isActive || pastream->isStopped ) \
    { \
            return paStreamIsStopped; \
    }

void PaPulseAudio_Lock( pa_threaded_mainloop *mainloop );

void PaPulseAudio_UnLock( pa_threaded_mainloop *mainloop );

PaError PaPulseAudio_Initialize( PaUtilHostApiRepresentation ** hostApi,
                                 PaHostApiIndex index );

void Terminate( struct PaUtilHostApiRepresentation *hostApi );


PaError IsFormatSupported( struct PaUtilHostApiRepresentation *hostApi,
                           const PaStreamParameters * inputParameters,
                           const PaStreamParameters * outputParameters,
                           double sampleRate );

PaError OpenStream( struct PaUtilHostApiRepresentation *hostApi,
                    PaStream ** s,
                    const PaStreamParameters * inputParameters,
                    const PaStreamParameters * outputParameters,
                    double sampleRate,
                    unsigned long framesPerBuffer,
                    PaStreamFlags streamFlags,
                    PaStreamCallback * streamCallback,
                    void *userData );


PaError IsStreamStopped( PaStream * s );
PaError IsStreamActive( PaStream * stream );

PaTime GetStreamTime( PaStream * stream );
double GetStreamCpuLoad( PaStream * stream );

PaPulseAudio_HostApiRepresentation *PaPulseAudio_New( void );
void PaPulseAudio_Free( PaPulseAudio_HostApiRepresentation * ptr );

int PaPulseAudio_CheckConnection( PaPulseAudio_HostApiRepresentation * ptr );

void PaPulseAudio_CheckContextStateCb( pa_context * c,
                                       void *userdata );
void PaPulseAudio_ServerInfoCb( pa_context *c,
                                const pa_server_info *i,
                                void *userdata );

void PaPulseAudio_SinkListCb( pa_context * c,
                              const pa_sink_info * l,
                              int eol,
                              void *userdata );

void PaPulseAudio_SourceListCb( pa_context * c,
                                const pa_source_info * l,
                                int eol,
                                void *userdata );

void PaPulseAudio_StreamStateCb( pa_stream * s,
                                 void *userdata );

void PaPulseAudio_StreamStartedCb( pa_stream * s,
                                   void *userdata );

void PaPulseAudio_StreamUnderflowCb( pa_stream * s,
                                     void *userdata );

PaError PaPulseAudio_ConvertPortaudioFormatToPaPulseAudio_( PaSampleFormat portaudiosf,
                                                            pa_sample_spec * pulseaudiosf
);

#ifdef __cplusplus
}
#endif                          /* __cplusplus */


#endif