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