-
Notifications
You must be signed in to change notification settings - Fork 16
/
CNFA_pulse.c
343 lines (269 loc) · 9.62 KB
/
CNFA_pulse.c
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
//Copyright 2015-2020 <>< Charles Lohr under the MIT/x11, NewBSD or ColorChord License. You choose.
//This file is really rough. Full duplex doesn't seem to work hardly at all.
#include "CNFA.h"
#include "os_generic.h"
#include <stdlib.h>
#include <pulse/simple.h>
#include <pulse/pulseaudio.h>
#include <pulse/error.h>
#include <stdio.h>
#include <string.h>
#define BUFFERSETS 3
//from http://www.freedesktop.org/wiki/Software/PulseAudio/Documentation/Developer/Clients/Samples/AsyncPlayback/
//also http://maemo.org/api_refs/5.0/5.0-final/pulseaudio/pacat_8c-example.html
struct CNFADriverPulse
{
void (*CloseFn)( void * object );
int (*StateFn)( void * object );
CNFACBType callback;
short channelsPlay;
short channelsRec;
int spsPlay;
int spsRec;
void * opaque;
char * sourceNamePlay;
char * sourceNameRec;
og_thread_t thread;
pa_stream * play;
pa_stream * rec;
pa_context * pa_ctx;
pa_mainloop *pa_ml;
int pa_ready;
int buffer;
//More fields may exist on a per-sound-driver basis
};
int CNFAStatePulse( void * v )
{
struct CNFADriverPulse * soundobject = (struct CNFADriverPulse *)v;
return ((soundobject->play)?2:0) | ((soundobject->rec)?1:0);
}
void CloseCNFAPulse( void * v )
{
struct CNFADriverPulse * r = (struct CNFADriverPulse *)v;
if( r )
{
OGCancelThread( r->thread );
OGUSleep(2000);
if( r->play )
{
pa_stream_unref (r->play);
r->play = 0;
}
if( r->rec )
{
pa_stream_unref (r->rec);
r->rec = 0;
}
OGUSleep(2000);
if( r->sourceNamePlay ) free( r->sourceNamePlay );
if( r->sourceNameRec ) free( r->sourceNameRec );
free( r );
}
}
static void * CNFAThread( void * v )
{
struct CNFADriverPulse * r = (struct CNFADriverPulse*)v;
while(1)
{
pa_mainloop_iterate( r->pa_ml, 1, NULL );
}
return 0;
}
static void stream_request_cb(pa_stream *s, size_t length, void *userdata)
{
struct CNFADriverPulse * r = (struct CNFADriverPulse*)userdata;
if( !r->play )
{
return;
}
short bufp[length*r->channelsPlay/sizeof(short)];
r->callback( (struct CNFADriver*)r, bufp, 0, length/(sizeof(short)*r->channelsPlay), 0 );
pa_stream_write(r->play, &bufp[0], length, NULL, 0LL, PA_SEEK_RELATIVE);
}
static void stream_record_cb(pa_stream *s, size_t length, void *userdata)
{
struct CNFADriverPulse * r = (struct CNFADriverPulse*)userdata;
uint16_t * bufr;
if (pa_stream_peek(r->rec, (const void**)&bufr, &length) < 0) {
fprintf(stderr, ("pa_stream_peek() failed: %s\n"), pa_strerror(pa_context_errno(r->pa_ctx)));
return;
}
short * buffer;
buffer = (short*)pa_xmalloc(length);
memcpy(buffer, bufr, length);
pa_stream_drop(r->rec);
r->callback( (struct CNFADriver*)r, 0, buffer, 0, length/(sizeof(short)*r->channelsRec) );
pa_xfree( buffer );
}
static void stream_underflow_cb(pa_stream *s, void *userdata) {
printf("underflow\n");
}
void pa_state_cb(pa_context *c, void *userdata) {
pa_context_state_t state;
int *pa_ready = (int*)userdata;
state = pa_context_get_state(c);
switch (state) {
// These are just here for reference
case PA_CONTEXT_UNCONNECTED:
case PA_CONTEXT_CONNECTING:
case PA_CONTEXT_AUTHORIZING:
case PA_CONTEXT_SETTING_NAME:
default:
break;
case PA_CONTEXT_FAILED:
case PA_CONTEXT_TERMINATED:
*pa_ready = 2;
break;
case PA_CONTEXT_READY:
*pa_ready = 1;
break;
}
}
void * InitCNFAPulse( CNFACBType cb, const char * your_name, int reqSPSPlay, int reqSPSRec, int reqChannelsPlay, int reqChannelsRec, int sugBufferSize, const char * outputSelect, const char * inputSelect, void * opaque )
{
static pa_buffer_attr bufattr;
static pa_sample_spec ss;
int error;
pa_mainloop_api *pa_mlapi;
const char * title = your_name;
struct CNFADriverPulse * r = (struct CNFADriverPulse *)malloc( sizeof( struct CNFADriverPulse ) );
r->pa_ml = pa_mainloop_new();
if( !r->pa_ml )
{
fprintf( stderr, "Failed to initialize pa_mainloop_new()\n" );
goto fail;
}
pa_mlapi = pa_mainloop_get_api(r->pa_ml);
if( !pa_mlapi )
{
fprintf( stderr, "Failed to initialize pa_mainloop_get_api()\n" );
goto fail;
}
r->pa_ctx = pa_context_new(pa_mlapi, title );
pa_context_connect(r->pa_ctx, NULL, PA_CONTEXT_NOFLAGS, NULL);
//TODO: pa_context_set_state_callback
r->CloseFn = CloseCNFAPulse;
r->StateFn = CNFAStatePulse;
r->callback = cb;
r->opaque = opaque;
r->spsPlay = reqSPSPlay;
r->spsRec = reqSPSRec;
r->channelsPlay = reqChannelsPlay;
r->channelsRec = reqChannelsRec;
r->sourceNamePlay = outputSelect?strdup(outputSelect):0;
r->sourceNameRec = inputSelect?strdup(inputSelect):0;
r->play = 0;
r->rec = 0;
r->buffer = sugBufferSize;
printf ("Pulse: from: [O/I] %s/%s (%s) / (%d,%d)x(%d,%d) (%d)\n", r->sourceNamePlay, r->sourceNameRec, title, r->spsPlay, r->spsRec, r->channelsPlay, r->channelsRec, r->buffer );
memset( &ss, 0, sizeof( ss ) );
ss.format = PA_SAMPLE_S16NE;
r->pa_ready = 0;
pa_context_set_state_callback(r->pa_ctx, pa_state_cb, &r->pa_ready);
while (r->pa_ready == 0)
{
pa_mainloop_iterate(r->pa_ml, 1, NULL);
}
int bufBytesPlay = r->buffer * sizeof(short) * r->channelsPlay;
int bufBytesRec = r->buffer * sizeof(short) * r->channelsRec;
if( r->channelsPlay )
{
ss.channels = r->channelsPlay;
ss.rate = r->spsPlay;
if (!(r->play = pa_stream_new(r->pa_ctx, "Play", &ss, NULL))) {
error = -3; //XXX ??? TODO
fprintf(stderr, __FILE__": pa_simple_new() failed: %s\n", pa_strerror(error));
goto fail;
}
pa_stream_set_underflow_callback(r->play, stream_underflow_cb, NULL);
pa_stream_set_write_callback(r->play, stream_request_cb, r );
/* The absolute maximum number of bytes that can be stored in the buffer.
* If this value is exceeded then data will be lost. It is recommended to
* pass (uint32_t) -1 here which will cause the server to fill in the
* maximum possible value.*/
bufattr.maxlength = bufBytesPlay*3;
/* The target fill level of the playback buffer. The server will only send
* requests for more data as long as the buffer has less than this number of
* bytes of data. If you pass (uint32_t) -1 (which is recommended) here the
* server will choose the longest target buffer fill level possible to minimize
* the number of necessary wakeups and maximize drop-out safety. This can exceed
* 2s of buffering. For low-latency applications or applications where latency
* matters you should pass a proper value here. */
bufattr.tlength = bufBytesPlay*3;
/* Number of bytes that need to be in the buffer before playback will commence.
* Start of playback can be forced using pa_stream_trigger() even though the
* prebuffer size hasn't been reached. If a buffer underrun occurs, this
* prebuffering will be again enabled. If the playback shall never stop in case
* of a buffer underrun, this value should be set to 0. In that case the read
* index of the output buffer overtakes the write index, and hence the fill
* level of the buffer is negative. If you pass (uint32_t) -1 here (which is
* recommended) the server will choose the same value as tlength here. */
bufattr.prebuf = (uint32_t)-1;
/* Minimum free number of the bytes in the playback buffer before the server
* will request more data. It is recommended to fill in (uint32_t) -1 here. This
* value influences how much time the sound server has to move data from the
* per-stream server-side playback buffer to the hardware playback buffer. */
bufattr.minreq = (uint32_t)-1;
/* Maximum number of bytes that the server will push in one chunk for record
* streams. If you pass (uint32_t) -1 (which is recommended) here, the server
* will choose the longest fragment setting possible to minimize the number of
* necessary wakeups and maximize drop-out safety. This can exceed 2s of
* buffering. For low-latency applications or applications where latency matters
* you should pass a proper value here. */
bufattr.fragsize = (uint32_t)-1;
int ret = pa_stream_connect_playback(r->play, r->sourceNamePlay, &bufattr,
// PA_STREAM_INTERPOLATE_TIMING
// |PA_STREAM_ADJUST_LATENCY //Some servers don't like the adjust_latency flag.
// |PA_STREAM_AUTO_TIMING_UPDATE, NULL, NULL);
PA_STREAM_NOFLAGS, NULL, NULL );
if( ret < 0 )
{
fprintf(stderr, __FILE__": (PLAY) pa_stream_connect_playback() failed: %s\n", pa_strerror(ret));
goto fail;
}
}
if( r->channelsRec )
{
ss.channels = r->channelsRec;
ss.rate = r->spsRec;
if (!(r->rec = pa_stream_new(r->pa_ctx, "Record", &ss, NULL))) {
error = -3; //XXX ??? TODO
fprintf(stderr, __FILE__": pa_simple_new() failed: %s\n", pa_strerror(error));
goto fail;
}
pa_stream_set_read_callback(r->rec, stream_record_cb, r );
bufattr.fragsize = bufBytesRec;
bufattr.maxlength = (uint32_t)-1;//(uint32_t)-1; //XXX: Todo, should this be low?
bufattr.minreq = bufBytesRec;
bufattr.prebuf = (uint32_t)-1;
bufattr.tlength = bufBytesRec*3;
int ret = pa_stream_connect_record(r->rec, r->sourceNameRec, &bufattr,
// PA_STREAM_INTERPOLATE_TIMING
PA_STREAM_ADJUST_LATENCY //Some servers don't like the adjust_latency flag.
// PA_STREAM_AUTO_TIMING_UPDATE
// PA_STREAM_NOFLAGS
);
printf( "PA REC RES: %d\n", ret );
if( ret < 0 )
{
fprintf(stderr, __FILE__": (REC) pa_stream_connect_playback() failed: %s\n", pa_strerror(ret));
goto fail;
}
}
printf( "Pulse initialized.\n" );
r->thread = OGCreateThread( CNFAThread, r );
if( r->play )
{
stream_request_cb( r->play, bufBytesPlay, r );
}
return r;
fail:
if( r )
{
if( r->play ) pa_xfree (r->play);
if( r->rec ) pa_xfree (r->rec);
free( r );
}
return 0;
}
REGISTER_CNFA( PulseCNFA, 11, "PULSE", InitCNFAPulse );