FreeRDP
rdpsnd_pulse.c
1 
22 #include <freerdp/config.h>
23 
24 #include <errno.h>
25 
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <time.h>
30 
31 #include <winpr/crt.h>
32 #include <winpr/cast.h>
33 #include <winpr/assert.h>
34 #include <winpr/stream.h>
35 #include <winpr/cmdline.h>
36 
37 #include <pulse/pulseaudio.h>
38 
39 #include <freerdp/types.h>
40 #include <freerdp/codec/dsp.h>
41 
42 #include "rdpsnd_main.h"
43 
44 typedef struct
45 {
46  rdpsndDevicePlugin device;
47 
48  char* device_name;
49  pa_threaded_mainloop* mainloop;
50  pa_context* context;
51  pa_sample_spec sample_spec;
52  pa_stream* stream;
53  UINT32 latency;
54  UINT32 volume;
55  time_t reconnect_delay_seconds;
56  time_t reconnect_time;
57 } rdpsndPulsePlugin;
58 
59 static BOOL rdpsnd_check_pulse(rdpsndPulsePlugin* pulse, BOOL haveStream)
60 {
61  BOOL rc = TRUE;
62  WINPR_ASSERT(pulse);
63 
64  if (!pulse->context)
65  {
66  WLog_WARN(TAG, "pulse->context=%p", pulse->context);
67  rc = FALSE;
68  }
69 
70  if (haveStream)
71  {
72  if (!pulse->stream)
73  {
74  WLog_WARN(TAG, "pulse->stream=%p", pulse->stream);
75  rc = FALSE;
76  }
77  }
78 
79  if (!pulse->mainloop)
80  {
81  WLog_WARN(TAG, "pulse->mainloop=%p", pulse->mainloop);
82  rc = FALSE;
83  }
84 
85  return rc;
86 }
87 
88 static BOOL rdpsnd_pulse_format_supported(rdpsndDevicePlugin* device, const AUDIO_FORMAT* format);
89 
90 static void rdpsnd_pulse_get_sink_info(pa_context* c, const pa_sink_info* i, int eol,
91  void* userdata)
92 {
93  UINT16 dwVolumeLeft = ((50 * 0xFFFF) / 100); /* 50% */
94  UINT16 dwVolumeRight = ((50 * 0xFFFF) / 100); /* 50% */
95  rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)userdata;
96 
97  WINPR_ASSERT(c);
98  if (!rdpsnd_check_pulse(pulse, FALSE) || !i)
99  return;
100 
101  for (uint8_t x = 0; x < i->volume.channels; x++)
102  {
103  pa_volume_t volume = i->volume.values[x];
104 
105  if (volume >= PA_VOLUME_NORM)
106  volume = PA_VOLUME_NORM - 1;
107 
108  switch (x)
109  {
110  case 0:
111  dwVolumeLeft = (UINT16)volume;
112  break;
113 
114  case 1:
115  dwVolumeRight = (UINT16)volume;
116  break;
117 
118  default:
119  break;
120  }
121  }
122 
123  pulse->volume = ((UINT32)dwVolumeLeft << 16U) | dwVolumeRight;
124 }
125 
126 static void rdpsnd_pulse_context_state_callback(pa_context* context, void* userdata)
127 {
128  rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)userdata;
129 
130  WINPR_ASSERT(context);
131  WINPR_ASSERT(pulse);
132 
133  pa_context_state_t state = pa_context_get_state(context);
134 
135  switch (state)
136  {
137  case PA_CONTEXT_READY:
138  pa_threaded_mainloop_signal(pulse->mainloop, 0);
139  break;
140 
141  case PA_CONTEXT_FAILED:
142  // Destroy context now, create new one for next connection attempt
143  pa_context_unref(pulse->context);
144  pulse->context = NULL;
145  if (pulse->reconnect_delay_seconds >= 0)
146  pulse->reconnect_time = time(NULL) + pulse->reconnect_delay_seconds;
147  pa_threaded_mainloop_signal(pulse->mainloop, 0);
148  break;
149 
150  case PA_CONTEXT_TERMINATED:
151  pa_threaded_mainloop_signal(pulse->mainloop, 0);
152  break;
153 
154  default:
155  break;
156  }
157 }
158 
159 static BOOL rdpsnd_pulse_connect(rdpsndDevicePlugin* device)
160 {
161  BOOL rc = 0;
162  pa_operation* o = NULL;
163  pa_context_state_t state = PA_CONTEXT_FAILED;
164  rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
165 
166  if (!rdpsnd_check_pulse(pulse, FALSE))
167  return FALSE;
168 
169  pa_threaded_mainloop_lock(pulse->mainloop);
170 
171  if (pa_context_connect(pulse->context, NULL, 0, NULL) < 0)
172  {
173  pa_threaded_mainloop_unlock(pulse->mainloop);
174  return FALSE;
175  }
176 
177  for (;;)
178  {
179  state = pa_context_get_state(pulse->context);
180 
181  if (state == PA_CONTEXT_READY)
182  break;
183 
184  if (!PA_CONTEXT_IS_GOOD(state))
185  {
186  break;
187  }
188 
189  pa_threaded_mainloop_wait(pulse->mainloop);
190  }
191 
192  o = pa_context_get_sink_info_by_index(pulse->context, 0, rdpsnd_pulse_get_sink_info, pulse);
193 
194  if (o)
195  pa_operation_unref(o);
196 
197  if (state == PA_CONTEXT_READY)
198  {
199  rc = TRUE;
200  }
201  else
202  {
203  pa_context_disconnect(pulse->context);
204  rc = FALSE;
205  }
206 
207  pa_threaded_mainloop_unlock(pulse->mainloop);
208  return rc;
209 }
210 
211 static void rdpsnd_pulse_stream_success_callback(pa_stream* stream, int success, void* userdata)
212 {
213  rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)userdata;
214 
215  if (!rdpsnd_check_pulse(pulse, TRUE))
216  return;
217 
218  pa_threaded_mainloop_signal(pulse->mainloop, 0);
219 }
220 
221 static void rdpsnd_pulse_wait_for_operation(rdpsndPulsePlugin* pulse, pa_operation* operation)
222 {
223  if (!rdpsnd_check_pulse(pulse, TRUE))
224  return;
225 
226  if (!operation)
227  return;
228 
229  while (pa_operation_get_state(operation) == PA_OPERATION_RUNNING)
230  {
231  pa_threaded_mainloop_wait(pulse->mainloop);
232  }
233 
234  pa_operation_unref(operation);
235 }
236 
237 static void rdpsnd_pulse_stream_state_callback(pa_stream* stream, void* userdata)
238 {
239  rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)userdata;
240 
241  WINPR_ASSERT(stream);
242  if (!rdpsnd_check_pulse(pulse, TRUE))
243  return;
244 
245  pa_stream_state_t state = pa_stream_get_state(stream);
246 
247  switch (state)
248  {
249  case PA_STREAM_READY:
250  pa_threaded_mainloop_signal(pulse->mainloop, 0);
251  break;
252 
253  case PA_STREAM_FAILED:
254  case PA_STREAM_TERMINATED:
255  // Stream object is about to be destroyed, clean up our pointer
256  pulse->stream = NULL;
257  pa_threaded_mainloop_signal(pulse->mainloop, 0);
258  break;
259 
260  default:
261  break;
262  }
263 }
264 
265 static void rdpsnd_pulse_stream_request_callback(pa_stream* stream, size_t length, void* userdata)
266 {
267  rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)userdata;
268 
269  WINPR_ASSERT(stream);
270  if (!rdpsnd_check_pulse(pulse, TRUE))
271  return;
272 
273  pa_threaded_mainloop_signal(pulse->mainloop, 0);
274 }
275 
276 static void rdpsnd_pulse_close(rdpsndDevicePlugin* device)
277 {
278  rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
279 
280  WINPR_ASSERT(pulse);
281 
282  if (!rdpsnd_check_pulse(pulse, FALSE))
283  return;
284 
285  pa_threaded_mainloop_lock(pulse->mainloop);
286  if (pulse->stream)
287  {
288  rdpsnd_pulse_wait_for_operation(
289  pulse, pa_stream_drain(pulse->stream, rdpsnd_pulse_stream_success_callback, pulse));
290  pa_stream_disconnect(pulse->stream);
291  pa_stream_unref(pulse->stream);
292  pulse->stream = NULL;
293  }
294  pa_threaded_mainloop_unlock(pulse->mainloop);
295 }
296 
297 static BOOL rdpsnd_pulse_set_format_spec(rdpsndPulsePlugin* pulse, const AUDIO_FORMAT* format)
298 {
299  pa_sample_spec sample_spec = { 0 };
300 
301  WINPR_ASSERT(format);
302 
303  if (!rdpsnd_check_pulse(pulse, FALSE))
304  return FALSE;
305 
306  if (!rdpsnd_pulse_format_supported(&pulse->device, format))
307  return FALSE;
308 
309  sample_spec.rate = format->nSamplesPerSec;
310  sample_spec.channels = WINPR_ASSERTING_INT_CAST(uint8_t, format->nChannels);
311 
312  switch (format->wFormatTag)
313  {
314  case WAVE_FORMAT_PCM:
315  switch (format->wBitsPerSample)
316  {
317  case 8:
318  sample_spec.format = PA_SAMPLE_U8;
319  break;
320 
321  case 16:
322  sample_spec.format = PA_SAMPLE_S16LE;
323  break;
324 
325  default:
326  return FALSE;
327  }
328 
329  break;
330 
331  default:
332  return FALSE;
333  }
334 
335  pulse->sample_spec = sample_spec;
336  return TRUE;
337 }
338 
339 static BOOL rdpsnd_pulse_context_connect(rdpsndDevicePlugin* device)
340 {
341  rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
342 
343  pulse->context = pa_context_new(pa_threaded_mainloop_get_api(pulse->mainloop), "freerdp");
344 
345  if (!pulse->context)
346  return FALSE;
347 
348  pa_context_set_state_callback(pulse->context, rdpsnd_pulse_context_state_callback, pulse);
349 
350  if (!rdpsnd_pulse_connect((rdpsndDevicePlugin*)pulse))
351  return FALSE;
352 
353  return TRUE;
354 }
355 
356 static BOOL rdpsnd_pulse_open_stream(rdpsndDevicePlugin* device)
357 {
358  pa_stream_state_t state = PA_STREAM_FAILED;
359  int flags = PA_STREAM_NOFLAGS;
360  pa_buffer_attr buffer_attr = { 0 };
361  char ss[PA_SAMPLE_SPEC_SNPRINT_MAX] = { 0 };
362  rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
363 
364  if (pa_sample_spec_valid(&pulse->sample_spec) == 0)
365  {
366  pa_sample_spec_snprint(ss, sizeof(ss), &pulse->sample_spec);
367  return FALSE;
368  }
369 
370  pa_threaded_mainloop_lock(pulse->mainloop);
371  if (!pulse->context)
372  {
373  pa_threaded_mainloop_unlock(pulse->mainloop);
374  if (pulse->reconnect_delay_seconds >= 0 && time(NULL) - pulse->reconnect_time >= 0)
375  rdpsnd_pulse_context_connect(device);
376  pa_threaded_mainloop_lock(pulse->mainloop);
377  }
378 
379  if (!rdpsnd_check_pulse(pulse, FALSE))
380  {
381  pa_threaded_mainloop_unlock(pulse->mainloop);
382  return FALSE;
383  }
384 
385  pulse->stream = pa_stream_new(pulse->context, "freerdp", &pulse->sample_spec, NULL);
386 
387  if (!pulse->stream)
388  {
389  pa_threaded_mainloop_unlock(pulse->mainloop);
390  return FALSE;
391  }
392 
393  /* register essential callbacks */
394  pa_stream_set_state_callback(pulse->stream, rdpsnd_pulse_stream_state_callback, pulse);
395  pa_stream_set_write_callback(pulse->stream, rdpsnd_pulse_stream_request_callback, pulse);
396  flags = PA_STREAM_INTERPOLATE_TIMING | PA_STREAM_AUTO_TIMING_UPDATE;
397 
398  if (pulse->latency > 0)
399  {
400  const size_t val = pa_usec_to_bytes(1000ULL * pulse->latency, &pulse->sample_spec);
401  buffer_attr.maxlength = UINT32_MAX;
402  buffer_attr.tlength = (val > UINT32_MAX) ? UINT32_MAX : (UINT32)val;
403  buffer_attr.prebuf = UINT32_MAX;
404  buffer_attr.minreq = UINT32_MAX;
405  buffer_attr.fragsize = UINT32_MAX;
406  flags |= PA_STREAM_ADJUST_LATENCY;
407  }
408 
409  // NOLINTNEXTLINE(clang-analyzer-optin.core.EnumCastOutOfRange)
410  pa_stream_flags_t eflags = (pa_stream_flags_t)flags;
411  if (pa_stream_connect_playback(pulse->stream, pulse->device_name,
412  pulse->latency > 0 ? &buffer_attr : NULL, eflags, NULL,
413  NULL) < 0)
414  {
415  WLog_ERR(TAG, "error connecting playback stream");
416  pa_stream_unref(pulse->stream);
417  pulse->stream = NULL;
418  pa_threaded_mainloop_unlock(pulse->mainloop);
419  return FALSE;
420  }
421 
422  while (pulse->stream)
423  {
424  state = pa_stream_get_state(pulse->stream);
425 
426  if (state == PA_STREAM_READY)
427  break;
428 
429  if (!PA_STREAM_IS_GOOD(state))
430  {
431  break;
432  }
433 
434  pa_threaded_mainloop_wait(pulse->mainloop);
435  }
436 
437  pa_threaded_mainloop_unlock(pulse->mainloop);
438 
439  if (state == PA_STREAM_READY)
440  return TRUE;
441 
442  rdpsnd_pulse_close(device);
443  return FALSE;
444 }
445 
446 static BOOL rdpsnd_pulse_open(rdpsndDevicePlugin* device, const AUDIO_FORMAT* format,
447  UINT32 latency)
448 {
449  rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
450 
451  WINPR_ASSERT(format);
452 
453  if (!rdpsnd_check_pulse(pulse, FALSE))
454  return TRUE;
455 
456  if (!rdpsnd_pulse_set_format_spec(pulse, format))
457  return FALSE;
458 
459  pulse->latency = latency;
460 
461  return rdpsnd_pulse_open_stream(device);
462 }
463 
464 static void rdpsnd_pulse_free(rdpsndDevicePlugin* device)
465 {
466  rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
467 
468  if (!pulse)
469  return;
470 
471  rdpsnd_pulse_close(device);
472 
473  if (pulse->mainloop)
474  pa_threaded_mainloop_stop(pulse->mainloop);
475 
476  if (pulse->context)
477  {
478  pa_context_disconnect(pulse->context);
479  pa_context_unref(pulse->context);
480  pulse->context = NULL;
481  }
482 
483  if (pulse->mainloop)
484  {
485  pa_threaded_mainloop_free(pulse->mainloop);
486  pulse->mainloop = NULL;
487  }
488 
489  free(pulse->device_name);
490  free(pulse);
491 }
492 
493 static BOOL rdpsnd_pulse_default_format(rdpsndDevicePlugin* device, const AUDIO_FORMAT* desired,
494  AUDIO_FORMAT* defaultFormat)
495 {
496  rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
497 
498  if (!pulse || !defaultFormat)
499  return FALSE;
500 
501  *defaultFormat = *desired;
502  defaultFormat->data = NULL;
503  defaultFormat->cbSize = 0;
504  defaultFormat->wFormatTag = WAVE_FORMAT_PCM;
505  if ((defaultFormat->nChannels < 1) || (defaultFormat->nChannels > PA_CHANNELS_MAX))
506  defaultFormat->nChannels = 2;
507  if ((defaultFormat->nSamplesPerSec < 1) || (defaultFormat->nSamplesPerSec > PA_RATE_MAX))
508  defaultFormat->nSamplesPerSec = 44100;
509  if ((defaultFormat->wBitsPerSample != 8) && (defaultFormat->wBitsPerSample != 16))
510  defaultFormat->wBitsPerSample = 16;
511 
512  defaultFormat->nBlockAlign = defaultFormat->nChannels * defaultFormat->wBitsPerSample / 8;
513  defaultFormat->nAvgBytesPerSec = defaultFormat->nBlockAlign * defaultFormat->nSamplesPerSec;
514  return TRUE;
515 }
516 
517 BOOL rdpsnd_pulse_format_supported(rdpsndDevicePlugin* device, const AUDIO_FORMAT* format)
518 {
519  WINPR_ASSERT(device);
520  WINPR_ASSERT(format);
521 
522  switch (format->wFormatTag)
523  {
524  case WAVE_FORMAT_PCM:
525  if (format->cbSize == 0 && (format->nSamplesPerSec <= PA_RATE_MAX) &&
526  (format->wBitsPerSample == 8 || format->wBitsPerSample == 16) &&
527  (format->nChannels >= 1 && format->nChannels <= PA_CHANNELS_MAX))
528  {
529  return TRUE;
530  }
531 
532  break;
533 
534  default:
535  break;
536  }
537 
538  return FALSE;
539 }
540 
541 static UINT32 rdpsnd_pulse_get_volume(rdpsndDevicePlugin* device)
542 {
543  pa_operation* o = NULL;
544  rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
545 
546  if (!rdpsnd_check_pulse(pulse, FALSE))
547  return 0;
548 
549  pa_threaded_mainloop_lock(pulse->mainloop);
550  o = pa_context_get_sink_info_by_index(pulse->context, 0, rdpsnd_pulse_get_sink_info, pulse);
551  if (o)
552  pa_operation_unref(o);
553  pa_threaded_mainloop_unlock(pulse->mainloop);
554  return pulse->volume;
555 }
556 
557 static void rdpsnd_set_volume_success_cb(pa_context* c, int success, void* userdata)
558 {
559  rdpsndPulsePlugin* pulse = userdata;
560 
561  if (!rdpsnd_check_pulse(pulse, TRUE))
562  return;
563  WINPR_ASSERT(c);
564 
565  WLog_INFO(TAG, "%d", success);
566 }
567 
568 static BOOL rdpsnd_pulse_set_volume(rdpsndDevicePlugin* device, UINT32 value)
569 {
570  pa_cvolume cv = { 0 };
571  pa_volume_t left = 0;
572  pa_volume_t right = 0;
573  pa_operation* operation = NULL;
574  rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
575 
576  if (!rdpsnd_check_pulse(pulse, TRUE))
577  {
578  WLog_WARN(TAG, "%s called before pulse backend was initialized");
579  return FALSE;
580  }
581 
582  left = (pa_volume_t)(value & 0xFFFF);
583  right = (pa_volume_t)((value >> 16) & 0xFFFF);
584  pa_cvolume_init(&cv);
585  cv.channels = 2;
586  cv.values[0] = PA_VOLUME_MUTED + (left * (PA_VOLUME_NORM - PA_VOLUME_MUTED)) / PA_VOLUME_NORM;
587  cv.values[1] = PA_VOLUME_MUTED + (right * (PA_VOLUME_NORM - PA_VOLUME_MUTED)) / PA_VOLUME_NORM;
588  pa_threaded_mainloop_lock(pulse->mainloop);
589  operation = pa_context_set_sink_input_volume(pulse->context, pa_stream_get_index(pulse->stream),
590  &cv, rdpsnd_set_volume_success_cb, pulse);
591 
592  if (operation)
593  pa_operation_unref(operation);
594 
595  pa_threaded_mainloop_unlock(pulse->mainloop);
596  return TRUE;
597 }
598 
599 static UINT rdpsnd_pulse_play(rdpsndDevicePlugin* device, const BYTE* data, size_t size)
600 {
601  size_t length = 0;
602  void* pa_data = NULL;
603  int status = 0;
604  pa_usec_t latency = 0;
605  int negative = 0;
606  rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
607 
608  if (!data)
609  return 0;
610 
611  pa_threaded_mainloop_lock(pulse->mainloop);
612 
613  if (!rdpsnd_check_pulse(pulse, TRUE))
614  {
615  pa_threaded_mainloop_unlock(pulse->mainloop);
616  // Discard this playback request and just attempt to reconnect the stream
617  WLog_DBG(TAG, "reconnecting playback stream");
618  rdpsnd_pulse_open_stream(device);
619  return 0;
620  }
621 
622  while (size > 0)
623  {
624  length = size;
625 
626  status = pa_stream_begin_write(pulse->stream, &pa_data, &length);
627 
628  if (status < 0)
629  break;
630 
631  memcpy(pa_data, data, length);
632 
633  status = pa_stream_write(pulse->stream, pa_data, length, NULL, 0LL, PA_SEEK_RELATIVE);
634 
635  if (status < 0)
636  {
637  break;
638  }
639 
640  data += length;
641  size -= length;
642  }
643 
644  if (pa_stream_get_latency(pulse->stream, &latency, &negative) != 0)
645  latency = 0;
646 
647  pa_threaded_mainloop_unlock(pulse->mainloop);
648 
649  const pa_usec_t val = latency / 1000;
650  if (val > UINT32_MAX)
651  return UINT32_MAX;
652  return (UINT32)val;
653 }
654 
655 static UINT rdpsnd_pulse_parse_addin_args(rdpsndDevicePlugin* device, const ADDIN_ARGV* args)
656 {
657  int status = 0;
658  DWORD flags = 0;
659  const COMMAND_LINE_ARGUMENT_A* arg = NULL;
660  rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
661  COMMAND_LINE_ARGUMENT_A rdpsnd_pulse_args[] = {
662  { "dev", COMMAND_LINE_VALUE_REQUIRED, "<device>", NULL, NULL, -1, NULL, "device" },
663  { "reconnect_delay_seconds", COMMAND_LINE_VALUE_REQUIRED, "<reconnect_delay_seconds>", NULL,
664  NULL, -1, NULL, "reconnect_delay_seconds" },
665  { NULL, 0, NULL, NULL, NULL, -1, NULL, NULL }
666  };
667  flags =
668  COMMAND_LINE_SIGIL_NONE | COMMAND_LINE_SEPARATOR_COLON | COMMAND_LINE_IGN_UNKNOWN_KEYWORD;
669 
670  WINPR_ASSERT(pulse);
671  WINPR_ASSERT(args);
672 
673  status = CommandLineParseArgumentsA(args->argc, args->argv, rdpsnd_pulse_args, flags, pulse,
674  NULL, NULL);
675 
676  if (status < 0)
677  return ERROR_INVALID_DATA;
678 
679  arg = rdpsnd_pulse_args;
680 
681  do
682  {
683  if (!(arg->Flags & COMMAND_LINE_VALUE_PRESENT))
684  continue;
685 
686  CommandLineSwitchStart(arg) CommandLineSwitchCase(arg, "dev")
687  {
688  pulse->device_name = _strdup(arg->Value);
689 
690  if (!pulse->device_name)
691  return ERROR_OUTOFMEMORY;
692  }
693  CommandLineSwitchCase(arg, "reconnect_delay_seconds")
694  {
695  unsigned long val = strtoul(arg->Value, NULL, 0);
696 
697  if ((errno != 0) || (val > INT32_MAX))
698  return ERROR_INVALID_DATA;
699 
700  pulse->reconnect_delay_seconds = (time_t)val;
701  }
702  CommandLineSwitchEnd(arg)
703  } while ((arg = CommandLineFindNextArgumentA(arg)) != NULL);
704 
705  return CHANNEL_RC_OK;
706 }
707 
708 FREERDP_ENTRY_POINT(UINT VCAPITYPE pulse_freerdp_rdpsnd_client_subsystem_entry(
710 {
711  const ADDIN_ARGV* args = NULL;
712  rdpsndPulsePlugin* pulse = NULL;
713  UINT ret = 0;
714 
715  WINPR_ASSERT(pEntryPoints);
716 
717  pulse = (rdpsndPulsePlugin*)calloc(1, sizeof(rdpsndPulsePlugin));
718 
719  if (!pulse)
720  return CHANNEL_RC_NO_MEMORY;
721 
722  pulse->device.Open = rdpsnd_pulse_open;
723  pulse->device.FormatSupported = rdpsnd_pulse_format_supported;
724  pulse->device.GetVolume = rdpsnd_pulse_get_volume;
725  pulse->device.SetVolume = rdpsnd_pulse_set_volume;
726  pulse->device.Play = rdpsnd_pulse_play;
727  pulse->device.Close = rdpsnd_pulse_close;
728  pulse->device.Free = rdpsnd_pulse_free;
729  pulse->device.DefaultFormat = rdpsnd_pulse_default_format;
730  args = pEntryPoints->args;
731 
732  if (args->argc > 1)
733  {
734  ret = rdpsnd_pulse_parse_addin_args(&pulse->device, args);
735 
736  if (ret != CHANNEL_RC_OK)
737  {
738  WLog_ERR(TAG, "error parsing arguments");
739  goto error;
740  }
741  }
742  pulse->reconnect_delay_seconds = 5;
743  pulse->reconnect_time = time(NULL);
744 
745  ret = CHANNEL_RC_NO_MEMORY;
746  pulse->mainloop = pa_threaded_mainloop_new();
747 
748  if (!pulse->mainloop)
749  goto error;
750 
751  pa_threaded_mainloop_lock(pulse->mainloop);
752 
753  if (pa_threaded_mainloop_start(pulse->mainloop) < 0)
754  {
755  pa_threaded_mainloop_unlock(pulse->mainloop);
756  goto error;
757  }
758 
759  pa_threaded_mainloop_unlock(pulse->mainloop);
760 
761  if (!rdpsnd_pulse_context_connect((rdpsndDevicePlugin*)pulse))
762  goto error;
763 
764  pEntryPoints->pRegisterRdpsndDevice(pEntryPoints->rdpsnd, (rdpsndDevicePlugin*)pulse);
765  return CHANNEL_RC_OK;
766 error:
767  rdpsnd_pulse_free((rdpsndDevicePlugin*)pulse);
768  return ret;
769 }
Definition: client/rdpsnd.h:76