FreeRDP
Loading...
Searching...
No Matches
tsmf_pulse.c
1
20#include <freerdp/config.h>
21#include <freerdp/utils/helpers.h>
22
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <unistd.h>
27
28#include <winpr/crt.h>
29
30#include <pulse/pulseaudio.h>
31
32#include "tsmf_audio.h"
33
34typedef struct
35{
36 ITSMFAudioDevice iface;
37
38 char device[32];
39 pa_threaded_mainloop* mainloop;
40 pa_context* context;
41 pa_sample_spec sample_spec;
42 pa_stream* stream;
43} TSMFPulseAudioDevice;
44
45static void tsmf_pulse_context_state_callback(pa_context* context, void* userdata)
46{
47 TSMFPulseAudioDevice* pulse = (TSMFPulseAudioDevice*)userdata;
48 pa_context_state_t state = pa_context_get_state(context);
49
50 switch (state)
51 {
52 case PA_CONTEXT_READY:
53 DEBUG_TSMF("PA_CONTEXT_READY");
54 pa_threaded_mainloop_signal(pulse->mainloop, 0);
55 break;
56
57 case PA_CONTEXT_FAILED:
58 case PA_CONTEXT_TERMINATED:
59 DEBUG_TSMF("state %d", state);
60 pa_threaded_mainloop_signal(pulse->mainloop, 0);
61 break;
62
63 default:
64 DEBUG_TSMF("state %d", state);
65 break;
66 }
67}
68
69static BOOL tsmf_pulse_connect(TSMFPulseAudioDevice* pulse)
70{
71 pa_context_state_t state = PA_CONTEXT_FAILED;
72
73 if (!pulse->context)
74 return FALSE;
75
76 if (pa_context_connect(pulse->context, nullptr, 0, nullptr))
77 {
78 WLog_ERR(TAG, "pa_context_connect failed (%d)", pa_context_errno(pulse->context));
79 return FALSE;
80 }
81
82 pa_threaded_mainloop_lock(pulse->mainloop);
83
84 if (pa_threaded_mainloop_start(pulse->mainloop) < 0)
85 {
86 pa_threaded_mainloop_unlock(pulse->mainloop);
87 WLog_ERR(TAG, "pa_threaded_mainloop_start failed (%d)", pa_context_errno(pulse->context));
88 return FALSE;
89 }
90
91 for (;;)
92 {
93 state = pa_context_get_state(pulse->context);
94
95 if (state == PA_CONTEXT_READY)
96 break;
97
98 if (!PA_CONTEXT_IS_GOOD(state))
99 {
100 DEBUG_TSMF("bad context state (%d)", pa_context_errno(pulse->context));
101 break;
102 }
103
104 pa_threaded_mainloop_wait(pulse->mainloop);
105 }
106
107 pa_threaded_mainloop_unlock(pulse->mainloop);
108
109 if (state == PA_CONTEXT_READY)
110 {
111 DEBUG_TSMF("connected");
112 return TRUE;
113 }
114 else
115 {
116 pa_context_disconnect(pulse->context);
117 return FALSE;
118 }
119}
120
121static BOOL tsmf_pulse_open(ITSMFAudioDevice* audio, const char* device)
122{
123 TSMFPulseAudioDevice* pulse = (TSMFPulseAudioDevice*)audio;
124
125 if (device)
126 {
127 strncpy(pulse->device, device, sizeof(pulse->device) - 1);
128 }
129
130 pulse->mainloop = pa_threaded_mainloop_new();
131
132 if (!pulse->mainloop)
133 {
134 WLog_ERR(TAG, "pa_threaded_mainloop_new failed");
135 return FALSE;
136 }
137
138 pulse->context = pa_context_new(pa_threaded_mainloop_get_api(pulse->mainloop),
139 freerdp_getApplicationDetailsString());
140
141 if (!pulse->context)
142 {
143 WLog_ERR(TAG, "pa_context_new failed");
144 return FALSE;
145 }
146
147 pa_context_set_state_callback(pulse->context, tsmf_pulse_context_state_callback, pulse);
148
149 if (!tsmf_pulse_connect(pulse))
150 {
151 WLog_ERR(TAG, "tsmf_pulse_connect failed");
152 return FALSE;
153 }
154
155 DEBUG_TSMF("open device %s", pulse->device);
156 return TRUE;
157}
158
159static void tsmf_pulse_stream_success_callback(WINPR_ATTR_UNUSED pa_stream* stream,
160 WINPR_ATTR_UNUSED int success, void* userdata)
161{
162 TSMFPulseAudioDevice* pulse = (TSMFPulseAudioDevice*)userdata;
163 pa_threaded_mainloop_signal(pulse->mainloop, 0);
164}
165
166static void tsmf_pulse_wait_for_operation(TSMFPulseAudioDevice* pulse, pa_operation* operation)
167{
168 if (operation == nullptr)
169 return;
170
171 while (pa_operation_get_state(operation) == PA_OPERATION_RUNNING)
172 {
173 pa_threaded_mainloop_wait(pulse->mainloop);
174 }
175
176 pa_operation_unref(operation);
177}
178
179static void tsmf_pulse_stream_state_callback(pa_stream* stream, void* userdata)
180{
181 TSMFPulseAudioDevice* pulse = (TSMFPulseAudioDevice*)userdata;
182 WINPR_ASSERT(pulse);
183
184 pa_stream_state_t state = pa_stream_get_state(stream);
185
186 switch (state)
187 {
188 case PA_STREAM_READY:
189 DEBUG_TSMF("PA_STREAM_READY");
190 pa_threaded_mainloop_signal(pulse->mainloop, 0);
191 break;
192
193 case PA_STREAM_FAILED:
194 case PA_STREAM_TERMINATED:
195 DEBUG_TSMF("state %d", state);
196 pa_threaded_mainloop_signal(pulse->mainloop, 0);
197 break;
198
199 default:
200 DEBUG_TSMF("state %d", state);
201 break;
202 }
203}
204
205static void tsmf_pulse_stream_request_callback(WINPR_ATTR_UNUSED pa_stream* stream,
206 WINPR_ATTR_UNUSED size_t length, void* userdata)
207{
208 TSMFPulseAudioDevice* pulse = (TSMFPulseAudioDevice*)userdata;
209 DEBUG_TSMF("%" PRIdz "", length);
210 pa_threaded_mainloop_signal(pulse->mainloop, 0);
211}
212
213static BOOL tsmf_pulse_close_stream(TSMFPulseAudioDevice* pulse)
214{
215 if (!pulse->context || !pulse->stream)
216 return FALSE;
217
218 DEBUG_TSMF("");
219 pa_threaded_mainloop_lock(pulse->mainloop);
220 pa_stream_set_write_callback(pulse->stream, nullptr, nullptr);
221 tsmf_pulse_wait_for_operation(
222 pulse, pa_stream_drain(pulse->stream, tsmf_pulse_stream_success_callback, pulse));
223 pa_stream_disconnect(pulse->stream);
224 pa_stream_unref(pulse->stream);
225 pulse->stream = nullptr;
226 pa_threaded_mainloop_unlock(pulse->mainloop);
227 return TRUE;
228}
229
230static BOOL tsmf_pulse_open_stream(TSMFPulseAudioDevice* pulse)
231{
232 pa_stream_state_t state = PA_STREAM_FAILED;
233 pa_buffer_attr buffer_attr = WINPR_C_ARRAY_INIT;
234
235 if (!pulse->context)
236 return FALSE;
237
238 DEBUG_TSMF("");
239 pa_threaded_mainloop_lock(pulse->mainloop);
240 pulse->stream = pa_stream_new(pulse->context, freerdp_getApplicationDetailsString(),
241 &pulse->sample_spec, nullptr);
242
243 if (!pulse->stream)
244 {
245 pa_threaded_mainloop_unlock(pulse->mainloop);
246 WLog_ERR(TAG, "pa_stream_new failed (%d)", pa_context_errno(pulse->context));
247 return FALSE;
248 }
249
250 pa_stream_set_state_callback(pulse->stream, tsmf_pulse_stream_state_callback, pulse);
251 pa_stream_set_write_callback(pulse->stream, tsmf_pulse_stream_request_callback, pulse);
252 buffer_attr.maxlength = (uint32_t)pa_usec_to_bytes(500000, &pulse->sample_spec);
253 buffer_attr.tlength = (uint32_t)pa_usec_to_bytes(250000, &pulse->sample_spec);
254 buffer_attr.prebuf = (UINT32)-1;
255 buffer_attr.minreq = (UINT32)-1;
256 buffer_attr.fragsize = (UINT32)-1;
257
258 if (pa_stream_connect_playback(
259 pulse->stream, pulse->device[0] ? pulse->device : nullptr, &buffer_attr,
260 PA_STREAM_ADJUST_LATENCY | PA_STREAM_INTERPOLATE_TIMING | PA_STREAM_AUTO_TIMING_UPDATE,
261 nullptr, nullptr) < 0)
262 {
263 pa_threaded_mainloop_unlock(pulse->mainloop);
264 WLog_ERR(TAG, "pa_stream_connect_playback failed (%d)", pa_context_errno(pulse->context));
265 return FALSE;
266 }
267
268 for (;;)
269 {
270 state = pa_stream_get_state(pulse->stream);
271
272 if (state == PA_STREAM_READY)
273 break;
274
275 if (!PA_STREAM_IS_GOOD(state))
276 {
277 WLog_ERR(TAG, "bad stream state (%d)", pa_context_errno(pulse->context));
278 break;
279 }
280
281 pa_threaded_mainloop_wait(pulse->mainloop);
282 }
283
284 pa_threaded_mainloop_unlock(pulse->mainloop);
285
286 if (state == PA_STREAM_READY)
287 {
288 DEBUG_TSMF("connected");
289 return TRUE;
290 }
291 else
292 {
293 tsmf_pulse_close_stream(pulse);
294 return FALSE;
295 }
296}
297
298static BOOL tsmf_pulse_set_format(ITSMFAudioDevice* audio, UINT32 sample_rate, UINT32 channels,
299 WINPR_ATTR_UNUSED UINT32 bits_per_sample)
300{
301 TSMFPulseAudioDevice* pulse = (TSMFPulseAudioDevice*)audio;
302 DEBUG_TSMF("sample_rate %" PRIu32 " channels %" PRIu32 " bits_per_sample %" PRIu32 "",
303 sample_rate, channels, bits_per_sample);
304 pulse->sample_spec.rate = sample_rate;
305
306 WINPR_ASSERT(channels <= UINT8_MAX);
307 pulse->sample_spec.channels = (uint8_t)channels;
308 pulse->sample_spec.format = PA_SAMPLE_S16LE;
309 return tsmf_pulse_open_stream(pulse);
310}
311
312static BOOL tsmf_pulse_play(ITSMFAudioDevice* audio, const BYTE* data, UINT32 data_size)
313{
314 TSMFPulseAudioDevice* pulse = (TSMFPulseAudioDevice*)audio;
315 const BYTE* src = nullptr;
316 size_t len = 0;
317 int ret = 0;
318 DEBUG_TSMF("data_size %" PRIu32 "", data_size);
319
320 if (pulse->stream)
321 {
322 pa_threaded_mainloop_lock(pulse->mainloop);
323 src = data;
324
325 while (data_size > 0)
326 {
327 while ((len = pa_stream_writable_size(pulse->stream)) == 0)
328 {
329 DEBUG_TSMF("waiting");
330 pa_threaded_mainloop_wait(pulse->mainloop);
331 }
332
333 if (len == (size_t)-1)
334 break;
335
336 if (len > data_size)
337 len = data_size;
338
339 ret = pa_stream_write(pulse->stream, src, len, nullptr, 0LL, PA_SEEK_RELATIVE);
340
341 if (ret < 0)
342 {
343 DEBUG_TSMF("pa_stream_write failed (%d)", pa_context_errno(pulse->context));
344 break;
345 }
346
347 src += len;
348 data_size -= len;
349 }
350
351 pa_threaded_mainloop_unlock(pulse->mainloop);
352 }
353
354 return TRUE;
355}
356
357static UINT64 tsmf_pulse_get_latency(ITSMFAudioDevice* audio)
358{
359 pa_usec_t usec = 0;
360 UINT64 latency = 0;
361 TSMFPulseAudioDevice* pulse = (TSMFPulseAudioDevice*)audio;
362
363 if (pulse->stream && pa_stream_get_latency(pulse->stream, &usec, nullptr) == 0)
364 {
365 latency = ((UINT64)usec) * 10LL;
366 }
367
368 return latency;
369}
370
371static BOOL tsmf_pulse_flush(ITSMFAudioDevice* audio)
372{
373 TSMFPulseAudioDevice* pulse = (TSMFPulseAudioDevice*)audio;
374 pa_threaded_mainloop_lock(pulse->mainloop);
375 tsmf_pulse_wait_for_operation(
376 pulse, pa_stream_flush(pulse->stream, tsmf_pulse_stream_success_callback, pulse));
377 pa_threaded_mainloop_unlock(pulse->mainloop);
378 return TRUE;
379}
380
381static void tsmf_pulse_free(ITSMFAudioDevice* audio)
382{
383 TSMFPulseAudioDevice* pulse = (TSMFPulseAudioDevice*)audio;
384 DEBUG_TSMF("");
385 tsmf_pulse_close_stream(pulse);
386
387 if (pulse->mainloop)
388 {
389 pa_threaded_mainloop_stop(pulse->mainloop);
390 }
391
392 if (pulse->context)
393 {
394 pa_context_disconnect(pulse->context);
395 pa_context_unref(pulse->context);
396 pulse->context = nullptr;
397 }
398
399 if (pulse->mainloop)
400 {
401 pa_threaded_mainloop_free(pulse->mainloop);
402 pulse->mainloop = nullptr;
403 }
404
405 free(pulse);
406}
407
408FREERDP_ENTRY_POINT(UINT VCAPITYPE pulse_freerdp_tsmf_client_audio_subsystem_entry(void* ptr))
409{
410 ITSMFAudioDevice** sptr = (ITSMFAudioDevice**)ptr;
411 WINPR_ASSERT(sptr);
412 *sptr = nullptr;
413
414 TSMFPulseAudioDevice* pulse = (TSMFPulseAudioDevice*)calloc(1, sizeof(TSMFPulseAudioDevice));
415
416 if (!pulse)
417 return ERROR_OUTOFMEMORY;
418
419 pulse->iface.Open = tsmf_pulse_open;
420 pulse->iface.SetFormat = tsmf_pulse_set_format;
421 pulse->iface.Play = tsmf_pulse_play;
422 pulse->iface.GetLatency = tsmf_pulse_get_latency;
423 pulse->iface.Flush = tsmf_pulse_flush;
424 pulse->iface.Free = tsmf_pulse_free;
425 *sptr = &pulse->iface;
426 return CHANNEL_RC_OK;
427}