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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
|
// Pulse Audio bits for QtSoundmodem
#include <stdio.h>
#include <string.h>
#include <pulse/pulseaudio.h>
#include <pulse/simple.h>
#include <pulse/error.h>
#define UNUSED(x) (void)(x)
extern char CaptureNames[16][256];
extern char PlaybackNames[16][256];
extern int PlaybackCount;
extern int CaptureCount;
#include <dlfcn.h>
void *handle = NULL;
void *shandle = NULL;
pa_mainloop * (*ppa_mainloop_new)(void);
pa_mainloop_api * (*ppa_mainloop_get_api)(pa_mainloop * m);
pa_context * (*ppa_context_new)(pa_mainloop_api *mainloop, const char *name);
int (*ppa_context_connect)(pa_context * c, const char * server, pa_context_flags_t flags, const pa_spawn_api * api);
void (*ppa_context_set_state_callback)(pa_context * c, pa_context_notify_cb_t cb, void * userdata);
int (*ppa_mainloop_iterate)(pa_mainloop * m, int block, int * retval);
void (*ppa_mainloop_free)(pa_mainloop * m);
void (*ppa_context_disconnect)(pa_context * c);
void (*ppa_context_unref)(pa_context * c);
const char * (*ppa_strerror)(int error);
pa_context_state_t(*ppa_context_get_state)(const pa_context *c);
pa_operation * (*ppa_context_get_sink_info_list)(pa_context * c, pa_sink_info_cb_t cb, void * userdata);
pa_operation * (*ppa_context_get_source_info_list)(pa_context * c, pa_source_info_cb_t cb, void * userdata);
void (*ppa_operation_unref)(pa_operation * o);
pa_operation_state_t(*ppa_operation_get_state)(const pa_operation * o);
pa_simple * (*ppa_simple_new)(const char * server,
const char * name,
pa_stream_direction_t dir,
const char * dev,
const char * stream_name,
const pa_sample_spec * ss,
const pa_channel_map * map,
const pa_buffer_attr * attr,
int * error) = NULL;
pa_usec_t(*ppa_simple_get_latency)(pa_simple * s, int * error);
int(*ppa_simple_read)(pa_simple * s, void * data, size_t bytes, int * error);
int(*ppa_simple_write)(pa_simple * s, void * data, size_t bytes, int * error);
int(*ppa_simple_flush)(pa_simple * s, int * error);
void(*ppa_simple_free)(pa_simple * s);
int(*ppa_simple_drain)(pa_simple * s, int * error);
void * getModule(void *handle, char * sym)
{
return dlsym(handle, sym);
}
void * initPulse()
{
// Load the pulse libraries
if (handle)
return handle; // already done
handle = dlopen("libpulse.so", RTLD_LAZY);
if (!handle)
{
fputs(dlerror(), stderr);
return NULL;
}
if ((ppa_mainloop_new = getModule(handle, "pa_mainloop_new")) == NULL) return NULL;
if ((ppa_mainloop_get_api = getModule(handle, "pa_mainloop_get_api")) == NULL) return NULL;
if ((ppa_context_new = getModule(handle, "pa_context_new")) == NULL) return NULL;
if ((ppa_context_connect = getModule(handle, "pa_context_connect")) == NULL) return NULL;
if ((ppa_context_set_state_callback = getModule(handle, "pa_context_set_state_callback")) == NULL) return NULL;
if ((ppa_mainloop_iterate = getModule(handle, "pa_mainloop_iterate")) == NULL) return NULL;
if ((ppa_mainloop_free = getModule(handle, "pa_mainloop_free")) == NULL) return NULL;
if ((ppa_context_disconnect = getModule(handle, "pa_context_disconnect")) == NULL) return NULL;
if ((ppa_context_unref = getModule(handle, "pa_context_unref")) == NULL) return NULL;
if ((ppa_strerror = getModule(handle, "pa_strerror")) == NULL) return NULL;
if ((ppa_context_get_state = getModule(handle, "pa_context_get_state")) == NULL) return NULL;
if ((ppa_context_get_sink_info_list = getModule(handle, "pa_context_get_sink_info_list")) == NULL) return NULL;
if ((ppa_context_get_source_info_list = getModule(handle, "pa_context_get_source_info_list")) == NULL) return NULL;
if ((ppa_operation_unref = getModule(handle, "pa_operation_unref")) == NULL) return NULL;
if ((ppa_operation_get_state = getModule(handle, "pa_operation_get_state")) == NULL) return NULL;
shandle = dlopen("libpulse-simple.so", RTLD_LAZY);
if (!shandle)
{
fputs(dlerror(), stderr);
return NULL;
}
if ((ppa_simple_new = getModule(shandle, "pa_simple_new")) == NULL) return NULL;
if ((ppa_simple_get_latency = getModule(shandle, "pa_simple_get_latency")) == NULL) return NULL;
if ((ppa_simple_read = dlsym(shandle, "pa_simple_read")) == NULL) return NULL;
if ((ppa_simple_write = dlsym(shandle, "pa_simple_write")) == NULL) return NULL;
if ((ppa_simple_flush = dlsym(shandle, "pa_simple_flush")) == NULL) return NULL;
if ((ppa_simple_drain = dlsym(shandle, "pa_simple_drain")) == NULL) return NULL;
if ((ppa_simple_free = dlsym(shandle, "pa_simple_free")) == NULL) return NULL;
return shandle;
}
// Field list is here: http://0pointer.de/lennart/projects/pulseaudio/doxygen/structpa__sink__info.html
typedef struct pa_devicelist {
uint8_t initialized;
char name[512];
uint32_t index;
char description[256];
} pa_devicelist_t;
void pa_state_cb(pa_context *c, void *userdata);
void pa_sinklist_cb(pa_context *c, const pa_sink_info *l, int eol, void *userdata);
void pa_sourcelist_cb(pa_context *c, const pa_source_info *l, int eol, void *userdata);
int pa_get_devicelist(pa_devicelist_t *input, pa_devicelist_t *output);
// This callback gets called when our context changes state. We really only
// care about when it's ready or if it has failed
void pa_state_cb(pa_context *c, void *userdata) {
pa_context_state_t state;
int *pa_ready = userdata;
state = ppa_context_get_state(c);
switch (state) {
// There 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;
}
}
// pa_mainloop will call this function when it's ready to tell us about a sink.
// Since we're not threading, there's no need for mutexes on the devicelist
// structure
void pa_sinklist_cb(pa_context *c, const pa_sink_info *l, int eol, void *userdata)
{
UNUSED(c);
pa_devicelist_t *pa_devicelist = userdata;
int ctr = 0;
// If eol is set to a positive number, you're at the end of the list
if (eol > 0) {
return;
}
// We know we've allocated 16 slots to hold devices. Loop through our
// structure and find the first one that's "uninitialized." Copy the
// contents into it and we're done. If we receive more than 16 devices,
// they're going to get dropped. You could make this dynamically allocate
// space for the device list, but this is a simple example.
for (ctr = 0; ctr < 16; ctr++) {
if (!pa_devicelist[ctr].initialized) {
strncpy(pa_devicelist[ctr].name, l->name, 511);
strncpy(pa_devicelist[ctr].description, l->description, 255);
pa_devicelist[ctr].index = l->index;
pa_devicelist[ctr].initialized = 1;
break;
}
}
}
// See above. This callback is pretty much identical to the previous
void pa_sourcelist_cb(pa_context *c, const pa_source_info *l, int eol, void *userdata)
{
UNUSED(c);
pa_devicelist_t *pa_devicelist = userdata;
int ctr = 0;
if (eol > 0) {
return;
}
for (ctr = 0; ctr < 16; ctr++) {
if (!pa_devicelist[ctr].initialized) {
strncpy(pa_devicelist[ctr].name, l->name, 511);
strncpy(pa_devicelist[ctr].description, l->description, 255);
pa_devicelist[ctr].index = l->index;
pa_devicelist[ctr].initialized = 1;
break;
}
}
}
int pa_get_devicelist(pa_devicelist_t *input, pa_devicelist_t *output) {
// Define our pulse audio loop and connection variables
pa_mainloop *pa_ml;
pa_mainloop_api *pa_mlapi;
pa_operation *pa_op;
pa_context *pa_ctx;
// We'll need these state variables to keep track of our requests
int state = 0;
int pa_ready = 0;
// Initialize our device lists
memset(input, 0, sizeof(pa_devicelist_t) * 16);
memset(output, 0, sizeof(pa_devicelist_t) * 16);
// Create a mainloop API and connection to the default server
pa_ml = ppa_mainloop_new();
pa_mlapi = ppa_mainloop_get_api(pa_ml);
pa_ctx = ppa_context_new(pa_mlapi, "test");
// This function connects to the pulse server
ppa_context_connect(pa_ctx, NULL, 0, NULL);
// This function defines a callback so the server will tell us it's state.
// Our callback will wait for the state to be ready. The callback will
// modify the variable to 1 so we know when we have a connection and it's
// ready.
// If there's an error, the callback will set pa_ready to 2
ppa_context_set_state_callback(pa_ctx, pa_state_cb, &pa_ready);
// Now we'll enter into an infinite loop until we get the data we receive
// or if there's an error
for (;;) {
// We can't do anything until PA is ready, so just iterate the mainloop
// and continue
if (pa_ready == 0) {
ppa_mainloop_iterate(pa_ml, 1, NULL);
continue;
}
// We couldn't get a connection to the server, so exit out
if (pa_ready == 2) {
ppa_context_disconnect(pa_ctx);
ppa_context_unref(pa_ctx);
ppa_mainloop_free(pa_ml);
return -1;
}
// At this point, we're connected to the server and ready to make
// requests
switch (state) {
// State 0: we haven't done anything yet
case 0:
// This sends an operation to the server. pa_sinklist_info is
// our callback function and a pointer to our devicelist will
// be passed to the callback The operation ID is stored in the
// pa_op variable
pa_op = ppa_context_get_sink_info_list(pa_ctx,
pa_sinklist_cb,
output
);
// Update state for next iteration through the loop
state++;
break;
case 1:
// Now we wait for our operation to complete. When it's
// complete our pa_output_devicelist is filled out, and we move
// along to the next state
if (ppa_operation_get_state(pa_op) == PA_OPERATION_DONE) {
ppa_operation_unref(pa_op);
// Now we perform another operation to get the source
// (input device) list just like before. This time we pass
// a pointer to our input structure
pa_op = ppa_context_get_source_info_list(pa_ctx,
pa_sourcelist_cb,
input
);
// Update the state so we know what to do next
state++;
}
break;
case 2:
if (ppa_operation_get_state(pa_op) == PA_OPERATION_DONE) {
// Now we're done, clean up and disconnect and return
ppa_operation_unref(pa_op);
ppa_context_disconnect(pa_ctx);
ppa_context_unref(pa_ctx);
ppa_mainloop_free(pa_ml);
return 0;
}
break;
default:
// We should never see this state
fprintf(stderr, "in state %d\n", state);
return -1;
}
// Iterate the main loop and go again. The second argument is whether
// or not the iteration should block until something is ready to be
// done. Set it to zero for non-blocking.
ppa_mainloop_iterate(pa_ml, 1, NULL);
}
}
int listpulse()
{
int ctr;
PlaybackCount = 0;
CaptureCount = 0;
// This is where we'll store the input device list
pa_devicelist_t pa_input_devicelist[16];
// This is where we'll store the output device list
pa_devicelist_t pa_output_devicelist[16];
if (pa_get_devicelist(pa_input_devicelist, pa_output_devicelist) < 0) {
fprintf(stderr, "failed to get device list\n");
return 1;
}
printf("Pulse Playback Devices\n\n");
for (ctr = 0; ctr < 16; ctr++)
{
if (!pa_output_devicelist[ctr].initialized)
break;
printf("Name: %s\n", pa_output_devicelist[ctr].name);
strcpy(&PlaybackNames[PlaybackCount++][0], pa_output_devicelist[ctr].name);
}
printf("Pulse Capture Devices\n\n");
for (ctr = 0; ctr < 16; ctr++)
{
if (!pa_input_devicelist[ctr].initialized)
break;
printf("Name: %s\n", pa_input_devicelist[ctr].name);
strcpy(&CaptureNames[CaptureCount++][0], pa_input_devicelist[ctr].name);
}
return 0;
}
pa_simple * OpenPulsePlayback(char * Server)
{
pa_simple * s;
pa_sample_spec ss;
ss.format = PA_SAMPLE_S16NE;
ss.channels = 2;
ss.rate = 12000;
int error;
s = (*ppa_simple_new)(NULL, // Use the default server.
"QtSM", // Our application's name.
PA_STREAM_PLAYBACK,
Server,
"Playback", // Description of our stream.
&ss, // Our sample format.
NULL, // Use default channel map
NULL, // Use default buffering attributes.
&error
);
if (s == 0)
printf("Playback pa_simple_new() failed: %s\n", ppa_strerror(error));
else
printf("Playback Handle %x\n", (unsigned int)s);
return s;
}
pa_simple * OpenPulseCapture(char * Server)
{
pa_simple * s;
pa_sample_spec ss;
ss.format = PA_SAMPLE_S16NE;
ss.channels = 2;
ss.rate = 12000;
int error;
pa_buffer_attr attr;
attr.maxlength = -1;
attr.tlength = -1;
attr.prebuf = -1;
attr.minreq = -1;
attr.fragsize = 512;
s = (*ppa_simple_new)(NULL, // Use the default server.
"QtSM", // Our application's name.
PA_STREAM_RECORD,
Server,
"Capture", // Description of our stream.
&ss, // Our sample format.
NULL, // Use default channel map
&attr,
&error
);
if (s == 0)
printf("Capture pa_simple_new() failed: %s\n", ppa_strerror(error));
else
printf("Capture Handle %x\n", (unsigned int)s);
return s;
}
pa_simple * spc = 0; // Capure Handle
pa_simple * spp = 0; // Playback Handle
int pulse_audio_open(char * CaptureDevice, char * PlaybackDevice)
{
pa_usec_t latency;
int error;
spc = OpenPulseCapture(CaptureDevice);
spp = OpenPulsePlayback(PlaybackDevice);
if (spc && spp)
{
if ((latency = ppa_simple_get_latency(spc, &error)) == (pa_usec_t)-1) {
printf("cap simple_get_latency() failed: %s\n", ppa_strerror(error));
}
else
printf("cap %0.0f usec \n", (float)latency);
if ((latency = ppa_simple_get_latency(spp, &error)) == (pa_usec_t)-1) {
printf("play simple_get_latency() failed: %s\n", ppa_strerror(error));
}
else
printf("play %0.0f usec \n", (float)latency);
return 1;
}
else
return 0;
}
void pulse_audio_close()
{
int error;
ppa_simple_flush(spc, &error);
ppa_simple_free(spc);
spc = 0;
ppa_simple_drain(spp, &error);
ppa_simple_free(spp);
spp = 0;
}
int pulse_read(short * samples, int nSamples)
{
int error;
int nBytes = nSamples * 4;
if (spc == 0)
return 0;
if (ppa_simple_read(spc, samples, nBytes, &error) < 0)
{
printf("Pulse pa_simple_read() failed: %s\n", ppa_strerror(error));
return 0;
}
return nSamples;
}
int pulse_write(short * ptr, int len)
{
int k;
int error;
if (spp == 0)
return 0;
k = ppa_simple_write(spp, ptr, len * 4, &error);
if (k < 0)
{
printf("Pulse pa_simple_write() failed: %s\n", ppa_strerror(error));
return -1;
}
return 0;
}
void pulse_flush()
{
int error;
if (spp == 0)
return;
if (ppa_simple_flush(spp, &error) < 0)
printf("Pulse pa_simple_flush() failed: %s\n", ppa_strerror(error));
}
|