Raw ↗
+ +
3538b96 fix(portability): resolve headless ALSA teardown segfault, MSVC rate ctl linkage, and CMIO DAL struct alignment
saifmukhtar · 2026-11-24
1 /*
2 * SPDX-License-Identifier: AGPL-3.0-or-later WITH Classpath-exception-2.0
3 * Copyright (c) 2026 Saif Mukhtar
4 *
5 * OpenAVX - Open Audio-Video eXchange
6 * tools/avx_audio_output.c - Audio playback backend (PulseAudio, ALSA, pipe)
7 */
8  
9 #define _GNU_SOURCE
10 #include "avx_platform.h"
11 #include "avx_audio_output.h"
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15  
16 /* PulseAudio Simple API Types */
17 typedef struct pa_sample_spec_t {
18 int format; /* 5 = PA_SAMPLE_FLOAT32LE */
19 uint32_t rate;
20 uint8_t channels;
21 } pa_sample_spec_t;
22  
23 typedef void* (*pa_simple_new_fn)(const char*, const char*, int, const char*, const char*, const pa_sample_spec_t*, const void*, const void*, int*);
24 typedef int (*pa_simple_write_fn)(void*, const void*, size_t, int*);
25 typedef void (*pa_simple_free_fn)(void*);
26 typedef int (*pa_simple_drain_fn)(void*, int*);
27  
28 /* ALSA PCM API Types */
29 typedef void snd_pcm_t;
30 typedef int (*snd_pcm_open_fn)(snd_pcm_t**, const char*, int, int);
31 typedef int (*snd_pcm_set_params_fn)(snd_pcm_t*, int, int, unsigned int, unsigned int, int, unsigned int);
32 typedef long (*snd_pcm_writei_fn)(snd_pcm_t*, const void*, unsigned long);
33 typedef int (*snd_pcm_recover_fn)(snd_pcm_t*, int, int);
34 typedef int (*snd_pcm_close_fn)(snd_pcm_t*);
35  
36 struct avx_audio_output_t {
37 avx_audio_backend_e backend;
38 char backend_name[64];
39 int sample_rate;
40 int channels;
41  
42 /* Interleave buffer */
43 float* interleave_buf;
44 size_t interleave_cap;
45  
46 /* PulseAudio Context */
47 void* pulse_lib;
48 void* pulse_handle;
49 pa_simple_write_fn pulse_write;
50 pa_simple_free_fn pulse_free;
51  
52 /* ALSA Context */
53 void* alsa_lib;
54 snd_pcm_t* alsa_handle;
55 snd_pcm_writei_fn alsa_writei;
56 snd_pcm_recover_fn alsa_recover;
57 snd_pcm_close_fn alsa_close;
58 int (*alsa_free_global)(void);
59  
60 /* Subprocess Pipe Context */
61 FILE* pipe_fp;
62 };
63  
64 static bool init_pulse(avx_audio_output_t* ao) {
65 const char* libs[] = { "libpulse-simple.so.0", "libpulse-simple.so", NULL };
66 for (int i = 0; libs[i]; ++i) {
67 ao->pulse_lib = avx_dlopen(libs[i]);
68 if (ao->pulse_lib) break;
69 }
70 if (!ao->pulse_lib) return false;
71  
72 pa_simple_new_fn p_new = (pa_simple_new_fn)avx_dlsym(ao->pulse_lib, "pa_simple_new");
73 ao->pulse_write = (pa_simple_write_fn)avx_dlsym(ao->pulse_lib, "pa_simple_write");
74 ao->pulse_free = (pa_simple_free_fn)avx_dlsym(ao->pulse_lib, "pa_simple_free");
75  
76 if (!p_new || !ao->pulse_write || !ao->pulse_free) {
77 ao->pulse_write = NULL;
78 ao->pulse_free = NULL;
79 avx_dlclose(ao->pulse_lib);
80 ao->pulse_lib = NULL;
81 return false;
82 }
83  
84 pa_sample_spec_t ss = {
85 .format = 5, /* PA_SAMPLE_FLOAT32LE */
86 .rate = (uint32_t)ao->sample_rate,
87 .channels = (uint8_t)ao->channels
88 };
89  
90 int err = 0;
91 ao->pulse_handle = p_new(NULL, "openavx-monitor", 1 /* PA_STREAM_PLAYBACK */,
92 NULL, "OpenAVX Live Audio", &ss, NULL, NULL, &err);
93 if (!ao->pulse_handle) {
94 ao->pulse_write = NULL;
95 ao->pulse_free = NULL;
96 avx_dlclose(ao->pulse_lib);
97 ao->pulse_lib = NULL;
98 return false;
99 }
100  
101 snprintf(ao->backend_name, sizeof(ao->backend_name), "PulseAudio (libpulse-simple)");
102 ao->backend = AVX_AUDIO_BACKEND_PULSE;
103 return true;
104 }
105  
106 static bool init_alsa(avx_audio_output_t* ao) {
107 const char* libs[] = { "libasound.so.2", "libasound.so", NULL };
108 for (int i = 0; libs[i]; ++i) {
109 ao->alsa_lib = avx_dlopen(libs[i]);
110 if (ao->alsa_lib) break;
111 }
112 if (!ao->alsa_lib) return false;
113  
114 snd_pcm_open_fn p_open = (snd_pcm_open_fn)avx_dlsym(ao->alsa_lib, "snd_pcm_open");
115 snd_pcm_set_params_fn p_params = (snd_pcm_set_params_fn)avx_dlsym(ao->alsa_lib, "snd_pcm_set_params");
116 ao->alsa_writei = (snd_pcm_writei_fn)avx_dlsym(ao->alsa_lib, "snd_pcm_writei");
117 ao->alsa_recover = (snd_pcm_recover_fn)avx_dlsym(ao->alsa_lib, "snd_pcm_recover");
118 ao->alsa_close = (snd_pcm_close_fn)avx_dlsym(ao->alsa_lib, "snd_pcm_close");
119 ao->alsa_free_global = (int (*)(void))avx_dlsym(ao->alsa_lib, "snd_config_update_free_global");
120  
121 if (!p_open || !p_params || !ao->alsa_writei || !ao->alsa_close) {
122 ao->alsa_writei = NULL;
123 ao->alsa_recover = NULL;
124 ao->alsa_close = NULL;
125 ao->alsa_free_global = NULL;
126 avx_dlclose(ao->alsa_lib);
127 ao->alsa_lib = NULL;
128 return false;
129 }
130  
131 /* Open default playback device */
132 if (p_open(&ao->alsa_handle, "default", 0 /* SND_PCM_STREAM_PLAYBACK */, 0) != 0) {
133 ao->alsa_writei = NULL;
134 ao->alsa_recover = NULL;
135 ao->alsa_close = NULL;
136 ao->alsa_free_global = NULL;
137 avx_dlclose(ao->alsa_lib);
138 ao->alsa_lib = NULL;
139 return false;
140 }
141  
142 /* Format 14 = SND_PCM_FORMAT_FLOAT_LE, Access 3 = SND_PCM_ACCESS_RW_INTERLEAVED */
143 int ret = p_params(ao->alsa_handle, 14, 3, (unsigned int)ao->channels,
144 (unsigned int)ao->sample_rate, 1, 50000 /* 50ms latency */);
145 if (ret != 0) {
146 ao->alsa_close(ao->alsa_handle);
147 ao->alsa_handle = NULL;
148 ao->alsa_writei = NULL;
149 ao->alsa_recover = NULL;
150 ao->alsa_close = NULL;
151 ao->alsa_free_global = NULL;
152 avx_dlclose(ao->alsa_lib);
153 ao->alsa_lib = NULL;
154 return false;
155 }
156  
157 snprintf(ao->backend_name, sizeof(ao->backend_name), "ALSA (libasound.so.2)");
158 ao->backend = AVX_AUDIO_BACKEND_ALSA;
159 return true;
160 }
161  
162 static bool init_pipe(avx_audio_output_t* ao) {
163 char cmd[256];
164 if (access("/bin/pw-play", X_OK) == 0 || access("/usr/bin/pw-play", X_OK) == 0) {
165 snprintf(cmd, sizeof(cmd), "pw-play --format f32 --rate %d --channels %d - >/dev/null 2>&1",
166 ao->sample_rate, ao->channels);
167 ao->pipe_fp = popen(cmd, "w");
168 if (ao->pipe_fp) {
169 snprintf(ao->backend_name, sizeof(ao->backend_name), "Pipe (pw-play)");
170 ao->backend = AVX_AUDIO_BACKEND_PIPE;
171 return true;
172 }
173 }
174  
175 if (access("/bin/aplay", X_OK) == 0 || access("/usr/bin/aplay", X_OK) == 0) {
176 snprintf(cmd, sizeof(cmd), "aplay -q -t raw -f FLOAT_LE -r %d -c %d - >/dev/null 2>&1",
177 ao->sample_rate, ao->channels);
178 ao->pipe_fp = popen(cmd, "w");
179 if (ao->pipe_fp) {
180 snprintf(ao->backend_name, sizeof(ao->backend_name), "Pipe (aplay)");
181 ao->backend = AVX_AUDIO_BACKEND_PIPE;
182 return true;
183 }
184 }
185  
186 return false;
187 }
188  
189 avx_audio_output_t* avx_audio_output_create(const char* backend_name, int sample_rate, int channels) {
190 if (sample_rate <= 0) sample_rate = 48000;
191 if (channels <= 0) channels = 2;
192  
193 avx_audio_output_t* ao = (avx_audio_output_t*)calloc(1, sizeof(*ao));
194 if (!ao) return NULL;
195  
196 ao->sample_rate = sample_rate;
197 ao->channels = channels;
198 ao->interleave_buf = NULL;
199 ao->interleave_cap = 0;
200  
201 bool req_pulse = backend_name && (strcmp(backend_name, "pulse") == 0 || strcmp(backend_name, "pulseaudio") == 0);
202 bool req_alsa = backend_name && (strcmp(backend_name, "alsa") == 0);
203 bool req_pipe = backend_name && (strcmp(backend_name, "pipe") == 0);
204 bool req_none = backend_name && (strcmp(backend_name, "none") == 0 || strcmp(backend_name, "null") == 0);
205 bool req_auto = !backend_name || strcmp(backend_name, "auto") == 0 || backend_name[0] == '\0';
206  
207 if (req_none) {
208 ao->backend = AVX_AUDIO_BACKEND_NONE;
209 snprintf(ao->backend_name, sizeof(ao->backend_name), "Disabled (Null Sink)");
210 return ao;
211 }
212  
213 if (req_pulse || req_auto) {
214 if (init_pulse(ao)) return ao;
215 }
216  
217 if (req_alsa || req_auto) {
218 if (init_alsa(ao)) return ao;
219 }
220  
221 if (req_pipe || req_auto) {
222 if (init_pipe(ao)) return ao;
223 }
224  
225 /* Fallback to null sink */
226 ao->backend = AVX_AUDIO_BACKEND_NONE;
227 snprintf(ao->backend_name, sizeof(ao->backend_name), "Disabled (No Audio Device)");
228 return ao;
229 }
230  
231 bool avx_audio_output_write(avx_audio_output_t* ao, const oavx_audio_frame_t* aframe) {
232 if (!ao || !aframe || !aframe->planes[0] || aframe->frames == 0 || aframe->channels == 0) {
233 return false;
234 }
235 if (ao->backend == AVX_AUDIO_BACKEND_NONE) {
236 return true; /* Silently discard */
237 }
238  
239 /* Reallocate scratch interleave buffer */
240 size_t total_samples = (size_t)aframe->frames * aframe->channels;
241 if (ao->interleave_cap < total_samples) {
242 float* nb = (float*)realloc(ao->interleave_buf, total_samples * sizeof(float));
243 if (!nb) return false;
244 ao->interleave_buf = nb;
245 ao->interleave_cap = total_samples;
246 }
247  
248 /* Interleave if planar */
249 if (aframe->format == OAVX_AUDIO_FMT_FLT_INTERLEAVED) {
250 memcpy(ao->interleave_buf, aframe->planes[0], total_samples * sizeof(float));
251 } else {
252 for (uint32_t s = 0; s < aframe->frames; ++s) {
253 for (uint16_t ch = 0; ch < aframe->channels; ++ch) {
254 const float* ch_ptr;
255 if (ch < 8 && aframe->planes[ch]) {
256 ch_ptr = (const float*)aframe->planes[ch];
257 ao->interleave_buf[s * aframe->channels + ch] = ch_ptr[s];
258 } else if (aframe->stride_bytes > 0) {
259 ch_ptr = (const float*)((const char*)aframe->planes[0] + ch * aframe->stride_bytes);
260 ao->interleave_buf[s * aframe->channels + ch] = ch_ptr[s];
261 } else {
262 ao->interleave_buf[s * aframe->channels + ch] = 0.0f;
263 }
264 }
265 }
266 }
267  
268 size_t byte_count = total_samples * sizeof(float);
269  
270 if (ao->backend == AVX_AUDIO_BACKEND_PULSE && ao->pulse_handle && ao->pulse_write) {
271 int err = 0;
272 int r = ao->pulse_write(ao->pulse_handle, ao->interleave_buf, byte_count, &err);
273 return (r == 0);
274 } else if (ao->backend == AVX_AUDIO_BACKEND_ALSA && ao->alsa_handle && ao->alsa_writei) {
275 long written = ao->alsa_writei(ao->alsa_handle, ao->interleave_buf, (unsigned long)aframe->frames);
276 if (written < 0 && ao->alsa_recover) {
277 written = ao->alsa_recover(ao->alsa_handle, (int)written, 1);
278 if (written == 0) {
279 ao->alsa_writei(ao->alsa_handle, ao->interleave_buf, (unsigned long)aframe->frames);
280 }
281 }
282 return (written >= 0);
283 } else if (ao->backend == AVX_AUDIO_BACKEND_PIPE && ao->pipe_fp) {
284 size_t w = fwrite(ao->interleave_buf, sizeof(float), total_samples, ao->pipe_fp);
285 return (w == total_samples);
286 }
287  
288 return true;
289 }
290  
291 const char* avx_audio_output_get_backend_name(const avx_audio_output_t* ao) {
292 if (!ao) return "None";
293 return ao->backend_name;
294 }
295  
296 void avx_audio_output_destroy(avx_audio_output_t* ao) {
297 if (!ao) return;
298  
299 if (ao->pulse_handle && ao->pulse_free) {
300 ao->pulse_free(ao->pulse_handle);
301 ao->pulse_handle = NULL;
302 }
303 if (ao->pulse_lib) {
304 avx_dlclose(ao->pulse_lib);
305 ao->pulse_lib = NULL;
306 }
307  
308 if (ao->alsa_handle && ao->alsa_close) {
309 ao->alsa_close(ao->alsa_handle);
310 ao->alsa_handle = NULL;
311 }
312 if (ao->alsa_lib && ao->alsa_free_global) {
313 ao->alsa_free_global();
314 ao->alsa_free_global = NULL;
315 }
316 if (ao->alsa_lib) {
317 avx_dlclose(ao->alsa_lib);
318 ao->alsa_lib = NULL;
319 }
320  
321 if (ao->pipe_fp) {
322 pclose(ao->pipe_fp);
323 ao->pipe_fp = NULL;
324 }
325  
326 if (ao->interleave_buf) {
327 free(ao->interleave_buf);
328 ao->interleave_buf = NULL;
329 ao->interleave_cap = 0;
330 }
331  
332 free(ao);
333 }