FreeRDP
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 
33 typedef 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 
44 static 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 
68 static 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 
120 static 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 
157 static 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 
163 static 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 
176 static 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 
202 static 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 
209 static 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 
226 static 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 = pa_usec_to_bytes(500000, &pulse->sample_spec);
248  buffer_attr.tlength = 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 
293 static 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  pulse->sample_spec.channels = channels;
301  pulse->sample_spec.format = PA_SAMPLE_S16LE;
302  return tsmf_pulse_open_stream(pulse);
303 }
304 
305 static BOOL tsmf_pulse_play(ITSMFAudioDevice* audio, const BYTE* data, UINT32 data_size)
306 {
307  TSMFPulseAudioDevice* pulse = (TSMFPulseAudioDevice*)audio;
308  const BYTE* src = NULL;
309  size_t len = 0;
310  int ret = 0;
311  DEBUG_TSMF("data_size %" PRIu32 "", data_size);
312 
313  if (pulse->stream)
314  {
315  pa_threaded_mainloop_lock(pulse->mainloop);
316  src = data;
317 
318  while (data_size > 0)
319  {
320  while ((len = pa_stream_writable_size(pulse->stream)) == 0)
321  {
322  DEBUG_TSMF("waiting");
323  pa_threaded_mainloop_wait(pulse->mainloop);
324  }
325 
326  if (len == (size_t)-1)
327  break;
328 
329  if (len > data_size)
330  len = data_size;
331 
332  ret = pa_stream_write(pulse->stream, src, len, NULL, 0LL, PA_SEEK_RELATIVE);
333 
334  if (ret < 0)
335  {
336  DEBUG_TSMF("pa_stream_write failed (%d)", pa_context_errno(pulse->context));
337  break;
338  }
339 
340  src += len;
341  data_size -= len;
342  }
343 
344  pa_threaded_mainloop_unlock(pulse->mainloop);
345  }
346 
347  return TRUE;
348 }
349 
350 static UINT64 tsmf_pulse_get_latency(ITSMFAudioDevice* audio)
351 {
352  pa_usec_t usec = 0;
353  UINT64 latency = 0;
354  TSMFPulseAudioDevice* pulse = (TSMFPulseAudioDevice*)audio;
355 
356  if (pulse->stream && pa_stream_get_latency(pulse->stream, &usec, NULL) == 0)
357  {
358  latency = ((UINT64)usec) * 10LL;
359  }
360 
361  return latency;
362 }
363 
364 static BOOL tsmf_pulse_flush(ITSMFAudioDevice* audio)
365 {
366  TSMFPulseAudioDevice* pulse = (TSMFPulseAudioDevice*)audio;
367  pa_threaded_mainloop_lock(pulse->mainloop);
368  tsmf_pulse_wait_for_operation(
369  pulse, pa_stream_flush(pulse->stream, tsmf_pulse_stream_success_callback, pulse));
370  pa_threaded_mainloop_unlock(pulse->mainloop);
371  return TRUE;
372 }
373 
374 static void tsmf_pulse_free(ITSMFAudioDevice* audio)
375 {
376  TSMFPulseAudioDevice* pulse = (TSMFPulseAudioDevice*)audio;
377  DEBUG_TSMF("");
378  tsmf_pulse_close_stream(pulse);
379 
380  if (pulse->mainloop)
381  {
382  pa_threaded_mainloop_stop(pulse->mainloop);
383  }
384 
385  if (pulse->context)
386  {
387  pa_context_disconnect(pulse->context);
388  pa_context_unref(pulse->context);
389  pulse->context = NULL;
390  }
391 
392  if (pulse->mainloop)
393  {
394  pa_threaded_mainloop_free(pulse->mainloop);
395  pulse->mainloop = NULL;
396  }
397 
398  free(pulse);
399 }
400 
401 FREERDP_ENTRY_POINT(UINT VCAPITYPE pulse_freerdp_tsmf_client_audio_subsystem_entry(void* ptr))
402 {
403  ITSMFAudioDevice** sptr = (ITSMFAudioDevice**)ptr;
404  WINPR_ASSERT(sptr);
405  *sptr = NULL;
406 
407  TSMFPulseAudioDevice* pulse = (TSMFPulseAudioDevice*)calloc(1, sizeof(TSMFPulseAudioDevice));
408 
409  if (!pulse)
410  return ERROR_OUTOFMEMORY;
411 
412  pulse->iface.Open = tsmf_pulse_open;
413  pulse->iface.SetFormat = tsmf_pulse_set_format;
414  pulse->iface.Play = tsmf_pulse_play;
415  pulse->iface.GetLatency = tsmf_pulse_get_latency;
416  pulse->iface.Flush = tsmf_pulse_flush;
417  pulse->iface.Free = tsmf_pulse_free;
418  *sptr = &pulse->iface;
419  return CHANNEL_RC_OK;
420 }