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), "freerdp");
138
139 if (!pulse->context)
140 {
141 WLog_ERR(TAG, "pa_context_new failed");
142 return FALSE;
143 }
144
145 pa_context_set_state_callback(pulse->context, tsmf_pulse_context_state_callback, pulse);
146
147 if (!tsmf_pulse_connect(pulse))
148 {
149 WLog_ERR(TAG, "tsmf_pulse_connect failed");
150 return FALSE;
151 }
152
153 DEBUG_TSMF("open device %s", pulse->device);
154 return TRUE;
155}
156
157static void tsmf_pulse_stream_success_callback(pa_stream* stream, int success, void* userdata)
158{
159 TSMFPulseAudioDevice* pulse = (TSMFPulseAudioDevice*)userdata;
160 pa_threaded_mainloop_signal(pulse->mainloop, 0);
161}
162
163static void tsmf_pulse_wait_for_operation(TSMFPulseAudioDevice* pulse, pa_operation* operation)
164{
165 if (operation == NULL)
166 return;
167
168 while (pa_operation_get_state(operation) == PA_OPERATION_RUNNING)
169 {
170 pa_threaded_mainloop_wait(pulse->mainloop);
171 }
172
173 pa_operation_unref(operation);
174}
175
176static void tsmf_pulse_stream_state_callback(pa_stream* stream, void* userdata)
177{
178 TSMFPulseAudioDevice* pulse = (TSMFPulseAudioDevice*)userdata;
179 WINPR_ASSERT(pulse);
180
181 pa_stream_state_t state = pa_stream_get_state(stream);
182
183 switch (state)
184 {
185 case PA_STREAM_READY:
186 DEBUG_TSMF("PA_STREAM_READY");
187 pa_threaded_mainloop_signal(pulse->mainloop, 0);
188 break;
189
190 case PA_STREAM_FAILED:
191 case PA_STREAM_TERMINATED:
192 DEBUG_TSMF("state %d", state);
193 pa_threaded_mainloop_signal(pulse->mainloop, 0);
194 break;
195
196 default:
197 DEBUG_TSMF("state %d", state);
198 break;
199 }
200}
201
202static void tsmf_pulse_stream_request_callback(pa_stream* stream, size_t length, void* userdata)
203{
204 TSMFPulseAudioDevice* pulse = (TSMFPulseAudioDevice*)userdata;
205 DEBUG_TSMF("%" PRIdz "", length);
206 pa_threaded_mainloop_signal(pulse->mainloop, 0);
207}
208
209static BOOL tsmf_pulse_close_stream(TSMFPulseAudioDevice* pulse)
210{
211 if (!pulse->context || !pulse->stream)
212 return FALSE;
213
214 DEBUG_TSMF("");
215 pa_threaded_mainloop_lock(pulse->mainloop);
216 pa_stream_set_write_callback(pulse->stream, NULL, NULL);
217 tsmf_pulse_wait_for_operation(
218 pulse, pa_stream_drain(pulse->stream, tsmf_pulse_stream_success_callback, pulse));
219 pa_stream_disconnect(pulse->stream);
220 pa_stream_unref(pulse->stream);
221 pulse->stream = NULL;
222 pa_threaded_mainloop_unlock(pulse->mainloop);
223 return TRUE;
224}
225
226static BOOL tsmf_pulse_open_stream(TSMFPulseAudioDevice* pulse)
227{
228 pa_stream_state_t state = PA_STREAM_FAILED;
229 pa_buffer_attr buffer_attr = { 0 };
230
231 if (!pulse->context)
232 return FALSE;
233
234 DEBUG_TSMF("");
235 pa_threaded_mainloop_lock(pulse->mainloop);
236 pulse->stream = pa_stream_new(pulse->context, "freerdp", &pulse->sample_spec, NULL);
237
238 if (!pulse->stream)
239 {
240 pa_threaded_mainloop_unlock(pulse->mainloop);
241 WLog_ERR(TAG, "pa_stream_new failed (%d)", pa_context_errno(pulse->context));
242 return FALSE;
243 }
244
245 pa_stream_set_state_callback(pulse->stream, tsmf_pulse_stream_state_callback, pulse);
246 pa_stream_set_write_callback(pulse->stream, tsmf_pulse_stream_request_callback, pulse);
247 buffer_attr.maxlength = (uint32_t)pa_usec_to_bytes(500000, &pulse->sample_spec);
248 buffer_attr.tlength = (uint32_t)pa_usec_to_bytes(250000, &pulse->sample_spec);
249 buffer_attr.prebuf = (UINT32)-1;
250 buffer_attr.minreq = (UINT32)-1;
251 buffer_attr.fragsize = (UINT32)-1;
252
253 if (pa_stream_connect_playback(
254 pulse->stream, pulse->device[0] ? pulse->device : NULL, &buffer_attr,
255 PA_STREAM_ADJUST_LATENCY | PA_STREAM_INTERPOLATE_TIMING | PA_STREAM_AUTO_TIMING_UPDATE,
256 NULL, NULL) < 0)
257 {
258 pa_threaded_mainloop_unlock(pulse->mainloop);
259 WLog_ERR(TAG, "pa_stream_connect_playback failed (%d)", pa_context_errno(pulse->context));
260 return FALSE;
261 }
262
263 for (;;)
264 {
265 state = pa_stream_get_state(pulse->stream);
266
267 if (state == PA_STREAM_READY)
268 break;
269
270 if (!PA_STREAM_IS_GOOD(state))
271 {
272 WLog_ERR(TAG, "bad stream state (%d)", pa_context_errno(pulse->context));
273 break;
274 }
275
276 pa_threaded_mainloop_wait(pulse->mainloop);
277 }
278
279 pa_threaded_mainloop_unlock(pulse->mainloop);
280
281 if (state == PA_STREAM_READY)
282 {
283 DEBUG_TSMF("connected");
284 return TRUE;
285 }
286 else
287 {
288 tsmf_pulse_close_stream(pulse);
289 return FALSE;
290 }
291}
292
293static BOOL tsmf_pulse_set_format(ITSMFAudioDevice* audio, UINT32 sample_rate, UINT32 channels,
294 UINT32 bits_per_sample)
295{
296 TSMFPulseAudioDevice* pulse = (TSMFPulseAudioDevice*)audio;
297 DEBUG_TSMF("sample_rate %" PRIu32 " channels %" PRIu32 " bits_per_sample %" PRIu32 "",
298 sample_rate, channels, bits_per_sample);
299 pulse->sample_spec.rate = sample_rate;
300
301 WINPR_ASSERT(channels <= UINT8_MAX);
302 pulse->sample_spec.channels = (uint8_t)channels;
303 pulse->sample_spec.format = PA_SAMPLE_S16LE;
304 return tsmf_pulse_open_stream(pulse);
305}
306
307static BOOL tsmf_pulse_play(ITSMFAudioDevice* audio, const BYTE* data, UINT32 data_size)
308{
309 TSMFPulseAudioDevice* pulse = (TSMFPulseAudioDevice*)audio;
310 const BYTE* src = NULL;
311 size_t len = 0;
312 int ret = 0;
313 DEBUG_TSMF("data_size %" PRIu32 "", data_size);
314
315 if (pulse->stream)
316 {
317 pa_threaded_mainloop_lock(pulse->mainloop);
318 src = data;
319
320 while (data_size > 0)
321 {
322 while ((len = pa_stream_writable_size(pulse->stream)) == 0)
323 {
324 DEBUG_TSMF("waiting");
325 pa_threaded_mainloop_wait(pulse->mainloop);
326 }
327
328 if (len == (size_t)-1)
329 break;
330
331 if (len > data_size)
332 len = data_size;
333
334 ret = pa_stream_write(pulse->stream, src, len, NULL, 0LL, PA_SEEK_RELATIVE);
335
336 if (ret < 0)
337 {
338 DEBUG_TSMF("pa_stream_write failed (%d)", pa_context_errno(pulse->context));
339 break;
340 }
341
342 src += len;
343 data_size -= len;
344 }
345
346 pa_threaded_mainloop_unlock(pulse->mainloop);
347 }
348
349 return TRUE;
350}
351
352static UINT64 tsmf_pulse_get_latency(ITSMFAudioDevice* audio)
353{
354 pa_usec_t usec = 0;
355 UINT64 latency = 0;
356 TSMFPulseAudioDevice* pulse = (TSMFPulseAudioDevice*)audio;
357
358 if (pulse->stream && pa_stream_get_latency(pulse->stream, &usec, NULL) == 0)
359 {
360 latency = ((UINT64)usec) * 10LL;
361 }
362
363 return latency;
364}
365
366static BOOL tsmf_pulse_flush(ITSMFAudioDevice* audio)
367{
368 TSMFPulseAudioDevice* pulse = (TSMFPulseAudioDevice*)audio;
369 pa_threaded_mainloop_lock(pulse->mainloop);
370 tsmf_pulse_wait_for_operation(
371 pulse, pa_stream_flush(pulse->stream, tsmf_pulse_stream_success_callback, pulse));
372 pa_threaded_mainloop_unlock(pulse->mainloop);
373 return TRUE;
374}
375
376static void tsmf_pulse_free(ITSMFAudioDevice* audio)
377{
378 TSMFPulseAudioDevice* pulse = (TSMFPulseAudioDevice*)audio;
379 DEBUG_TSMF("");
380 tsmf_pulse_close_stream(pulse);
381
382 if (pulse->mainloop)
383 {
384 pa_threaded_mainloop_stop(pulse->mainloop);
385 }
386
387 if (pulse->context)
388 {
389 pa_context_disconnect(pulse->context);
390 pa_context_unref(pulse->context);
391 pulse->context = NULL;
392 }
393
394 if (pulse->mainloop)
395 {
396 pa_threaded_mainloop_free(pulse->mainloop);
397 pulse->mainloop = NULL;
398 }
399
400 free(pulse);
401}
402
403FREERDP_ENTRY_POINT(UINT VCAPITYPE pulse_freerdp_tsmf_client_audio_subsystem_entry(void* ptr))
404{
405 ITSMFAudioDevice** sptr = (ITSMFAudioDevice**)ptr;
406 WINPR_ASSERT(sptr);
407 *sptr = NULL;
408
409 TSMFPulseAudioDevice* pulse = (TSMFPulseAudioDevice*)calloc(1, sizeof(TSMFPulseAudioDevice));
410
411 if (!pulse)
412 return ERROR_OUTOFMEMORY;
413
414 pulse->iface.Open = tsmf_pulse_open;
415 pulse->iface.SetFormat = tsmf_pulse_set_format;
416 pulse->iface.Play = tsmf_pulse_play;
417 pulse->iface.GetLatency = tsmf_pulse_get_latency;
418 pulse->iface.Flush = tsmf_pulse_flush;
419 pulse->iface.Free = tsmf_pulse_free;
420 *sptr = &pulse->iface;
421 return CHANNEL_RC_OK;
422}