FreeRDP
audin_alsa.c
1 
22 #include <freerdp/config.h>
23 
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 
28 #include <winpr/crt.h>
29 #include <winpr/synch.h>
30 #include <winpr/thread.h>
31 #include <winpr/cmdline.h>
32 #include <winpr/wlog.h>
33 
34 #include <alsa/asoundlib.h>
35 
36 #include <freerdp/freerdp.h>
37 #include <freerdp/addin.h>
38 #include <freerdp/channels/rdpsnd.h>
39 
40 #include "audin_main.h"
41 
42 typedef struct
43 {
44  IAudinDevice iface;
45 
46  char* device_name;
47  UINT32 frames_per_packet;
48  AUDIO_FORMAT aformat;
49 
50  HANDLE thread;
51  HANDLE stopEvent;
52 
53  AudinReceive receive;
54  void* user_data;
55 
56  rdpContext* rdpcontext;
57  wLog* log;
58  size_t bytes_per_frame;
59 } AudinALSADevice;
60 
61 static snd_pcm_format_t audin_alsa_format(UINT32 wFormatTag, UINT32 bitPerChannel)
62 {
63  switch (wFormatTag)
64  {
65  case WAVE_FORMAT_PCM:
66  switch (bitPerChannel)
67  {
68  case 16:
69  return SND_PCM_FORMAT_S16_LE;
70 
71  case 8:
72  return SND_PCM_FORMAT_S8;
73 
74  default:
75  return SND_PCM_FORMAT_UNKNOWN;
76  }
77 
78  default:
79  return SND_PCM_FORMAT_UNKNOWN;
80  }
81 }
82 
83 static BOOL audin_alsa_set_params(AudinALSADevice* alsa, snd_pcm_t* capture_handle)
84 {
85  int error = 0;
86  SSIZE_T s = 0;
87  UINT32 channels = alsa->aformat.nChannels;
88  snd_pcm_hw_params_t* hw_params = NULL;
89  snd_pcm_format_t format =
90  audin_alsa_format(alsa->aformat.wFormatTag, alsa->aformat.wBitsPerSample);
91 
92  if ((error = snd_pcm_hw_params_malloc(&hw_params)) < 0)
93  {
94  WLog_Print(alsa->log, WLOG_ERROR, "snd_pcm_hw_params_malloc (%s)", snd_strerror(error));
95  return FALSE;
96  }
97 
98  snd_pcm_hw_params_any(capture_handle, hw_params);
99  snd_pcm_hw_params_set_access(capture_handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED);
100  snd_pcm_hw_params_set_format(capture_handle, hw_params, format);
101  snd_pcm_hw_params_set_rate_near(capture_handle, hw_params, &alsa->aformat.nSamplesPerSec, NULL);
102  snd_pcm_hw_params_set_channels_near(capture_handle, hw_params, &channels);
103  snd_pcm_hw_params(capture_handle, hw_params);
104  snd_pcm_hw_params_free(hw_params);
105  snd_pcm_prepare(capture_handle);
106  if (channels > UINT16_MAX)
107  return FALSE;
108  s = snd_pcm_format_size(format, 1);
109  if ((s < 0) || (s > UINT16_MAX))
110  return FALSE;
111  alsa->aformat.nChannels = (UINT16)channels;
112  alsa->bytes_per_frame = (size_t)s * channels;
113  return TRUE;
114 }
115 
116 static DWORD WINAPI audin_alsa_thread_func(LPVOID arg)
117 {
118  DWORD error = CHANNEL_RC_OK;
119  BYTE* buffer = NULL;
120  AudinALSADevice* alsa = (AudinALSADevice*)arg;
121 
122  WINPR_ASSERT(alsa);
123 
124  WLog_Print(alsa->log, WLOG_DEBUG, "in");
125 
126  snd_pcm_t* capture_handle = NULL;
127  const int rc = snd_pcm_open(&capture_handle, alsa->device_name, SND_PCM_STREAM_CAPTURE, 0);
128  if (rc < 0)
129  {
130  WLog_Print(alsa->log, WLOG_ERROR, "snd_pcm_open (%s)", snd_strerror(rc));
131  error = CHANNEL_RC_INITIALIZATION_ERROR;
132  goto out;
133  }
134 
135  if (!audin_alsa_set_params(alsa, capture_handle))
136  {
137  WLog_Print(alsa->log, WLOG_ERROR, "audin_alsa_set_params failed");
138  goto out;
139  }
140 
141  buffer =
142  (BYTE*)calloc(alsa->frames_per_packet + alsa->aformat.nBlockAlign, alsa->bytes_per_frame);
143 
144  if (!buffer)
145  {
146  WLog_Print(alsa->log, WLOG_ERROR, "calloc failed!");
147  error = CHANNEL_RC_NO_MEMORY;
148  goto out;
149  }
150 
151  while (1)
152  {
153  size_t frames = alsa->frames_per_packet;
154  const DWORD status = WaitForSingleObject(alsa->stopEvent, 0);
155 
156  if (status == WAIT_FAILED)
157  {
158  error = GetLastError();
159  WLog_Print(alsa->log, WLOG_ERROR, "WaitForSingleObject failed with error %" PRIu32 "!",
160  error);
161  break;
162  }
163 
164  if (status == WAIT_OBJECT_0)
165  {
166  WLog_Print(alsa->log, WLOG_DEBUG, "alsa->stopEvent requests termination");
167  break;
168  }
169 
170  snd_pcm_sframes_t framesRead = snd_pcm_readi(capture_handle, buffer, frames);
171 
172  if (framesRead == 0)
173  continue;
174 
175  if (framesRead == -EPIPE)
176  {
177  const int res = snd_pcm_recover(capture_handle, (int)framesRead, 0);
178  if (res < 0)
179  WLog_Print(alsa->log, WLOG_WARN, "snd_pcm_recover (%s)", snd_strerror(res));
180 
181  continue;
182  }
183  else if (framesRead < 0)
184  {
185  WLog_Print(alsa->log, WLOG_ERROR, "snd_pcm_readi (%s)", snd_strerror((int)framesRead));
186  error = ERROR_INTERNAL_ERROR;
187  break;
188  }
189 
190  error = alsa->receive(&alsa->aformat, buffer, (long)framesRead * alsa->bytes_per_frame,
191  alsa->user_data);
192 
193  if (error)
194  {
195  WLog_Print(alsa->log, WLOG_ERROR, "audin_alsa_thread_receive failed with error %ld",
196  error);
197  break;
198  }
199  }
200 
201  free(buffer);
202 
203  if (capture_handle)
204  {
205  const int res = snd_pcm_close(capture_handle);
206  if (res < 0)
207  WLog_Print(alsa->log, WLOG_WARN, "snd_pcm_close (%s)", snd_strerror(res));
208  }
209 
210 out:
211  WLog_Print(alsa->log, WLOG_DEBUG, "out");
212 
213  if (error && alsa->rdpcontext)
214  setChannelError(alsa->rdpcontext, error, "audin_alsa_thread_func reported an error");
215 
216  ExitThread(error);
217  return error;
218 }
219 
225 static UINT audin_alsa_free(IAudinDevice* device)
226 {
227  AudinALSADevice* alsa = (AudinALSADevice*)device;
228 
229  if (alsa)
230  free(alsa->device_name);
231 
232  free(alsa);
233  return CHANNEL_RC_OK;
234 }
235 
236 static BOOL audin_alsa_format_supported(IAudinDevice* device, const AUDIO_FORMAT* format)
237 {
238  if (!device || !format)
239  return FALSE;
240 
241  switch (format->wFormatTag)
242  {
243  case WAVE_FORMAT_PCM:
244  if (format->cbSize == 0 && (format->nSamplesPerSec <= 48000) &&
245  (format->wBitsPerSample == 8 || format->wBitsPerSample == 16) &&
246  (format->nChannels == 1 || format->nChannels == 2))
247  {
248  return TRUE;
249  }
250 
251  break;
252 
253  default:
254  return FALSE;
255  }
256 
257  return FALSE;
258 }
259 
265 static UINT audin_alsa_set_format(IAudinDevice* device, const AUDIO_FORMAT* format,
266  UINT32 FramesPerPacket)
267 {
268  AudinALSADevice* alsa = (AudinALSADevice*)device;
269 
270  if (!alsa || !format)
271  return ERROR_INVALID_PARAMETER;
272 
273  alsa->aformat = *format;
274  alsa->frames_per_packet = FramesPerPacket;
275 
276  if (audin_alsa_format(format->wFormatTag, format->wBitsPerSample) == SND_PCM_FORMAT_UNKNOWN)
277  return ERROR_INTERNAL_ERROR;
278 
279  return CHANNEL_RC_OK;
280 }
281 
287 static UINT audin_alsa_open(IAudinDevice* device, AudinReceive receive, void* user_data)
288 {
289  AudinALSADevice* alsa = (AudinALSADevice*)device;
290 
291  if (!device || !receive || !user_data)
292  return ERROR_INVALID_PARAMETER;
293 
294  alsa->receive = receive;
295  alsa->user_data = user_data;
296 
297  if (!(alsa->stopEvent = CreateEvent(NULL, TRUE, FALSE, NULL)))
298  {
299  WLog_Print(alsa->log, WLOG_ERROR, "CreateEvent failed!");
300  goto error_out;
301  }
302 
303  if (!(alsa->thread = CreateThread(NULL, 0, audin_alsa_thread_func, alsa, 0, NULL)))
304  {
305  WLog_Print(alsa->log, WLOG_ERROR, "CreateThread failed!");
306  goto error_out;
307  }
308 
309  return CHANNEL_RC_OK;
310 error_out:
311  (void)CloseHandle(alsa->stopEvent);
312  alsa->stopEvent = NULL;
313  return ERROR_INTERNAL_ERROR;
314 }
315 
321 static UINT audin_alsa_close(IAudinDevice* device)
322 {
323  UINT error = CHANNEL_RC_OK;
324  AudinALSADevice* alsa = (AudinALSADevice*)device;
325 
326  if (!alsa)
327  return ERROR_INVALID_PARAMETER;
328 
329  if (alsa->stopEvent)
330  {
331  (void)SetEvent(alsa->stopEvent);
332 
333  if (WaitForSingleObject(alsa->thread, INFINITE) == WAIT_FAILED)
334  {
335  error = GetLastError();
336  WLog_Print(alsa->log, WLOG_ERROR, "WaitForSingleObject failed with error %" PRIu32 "",
337  error);
338  return error;
339  }
340 
341  (void)CloseHandle(alsa->stopEvent);
342  alsa->stopEvent = NULL;
343  (void)CloseHandle(alsa->thread);
344  alsa->thread = NULL;
345  }
346 
347  alsa->receive = NULL;
348  alsa->user_data = NULL;
349  return error;
350 }
351 
357 static UINT audin_alsa_parse_addin_args(AudinALSADevice* device, const ADDIN_ARGV* args)
358 {
359  int status = 0;
360  DWORD flags = 0;
361  const COMMAND_LINE_ARGUMENT_A* arg = NULL;
362  AudinALSADevice* alsa = device;
363  COMMAND_LINE_ARGUMENT_A audin_alsa_args[] = { { "dev", COMMAND_LINE_VALUE_REQUIRED, "<device>",
364  NULL, NULL, -1, NULL, "audio device name" },
365  { NULL, 0, NULL, NULL, NULL, -1, NULL, NULL } };
366  flags =
367  COMMAND_LINE_SIGIL_NONE | COMMAND_LINE_SEPARATOR_COLON | COMMAND_LINE_IGN_UNKNOWN_KEYWORD;
368  status = CommandLineParseArgumentsA(args->argc, args->argv, audin_alsa_args, flags, alsa, NULL,
369  NULL);
370 
371  if (status < 0)
372  return ERROR_INVALID_PARAMETER;
373 
374  arg = audin_alsa_args;
375 
376  do
377  {
378  if (!(arg->Flags & COMMAND_LINE_VALUE_PRESENT))
379  continue;
380 
381  CommandLineSwitchStart(arg) CommandLineSwitchCase(arg, "dev")
382  {
383  alsa->device_name = _strdup(arg->Value);
384 
385  if (!alsa->device_name)
386  {
387  WLog_Print(alsa->log, WLOG_ERROR, "_strdup failed!");
388  return CHANNEL_RC_NO_MEMORY;
389  }
390  }
391  CommandLineSwitchEnd(arg)
392  } while ((arg = CommandLineFindNextArgumentA(arg)) != NULL);
393 
394  return CHANNEL_RC_OK;
395 }
396 
402 FREERDP_ENTRY_POINT(UINT VCAPITYPE alsa_freerdp_audin_client_subsystem_entry(
404 {
405  const ADDIN_ARGV* args = NULL;
406  AudinALSADevice* alsa = NULL;
407  UINT error = 0;
408  alsa = (AudinALSADevice*)calloc(1, sizeof(AudinALSADevice));
409 
410  if (!alsa)
411  {
412  WLog_ERR(TAG, "calloc failed!");
413  return CHANNEL_RC_NO_MEMORY;
414  }
415 
416  alsa->log = WLog_Get(TAG);
417  alsa->iface.Open = audin_alsa_open;
418  alsa->iface.FormatSupported = audin_alsa_format_supported;
419  alsa->iface.SetFormat = audin_alsa_set_format;
420  alsa->iface.Close = audin_alsa_close;
421  alsa->iface.Free = audin_alsa_free;
422  alsa->rdpcontext = pEntryPoints->rdpcontext;
423  args = pEntryPoints->args;
424 
425  if ((error = audin_alsa_parse_addin_args(alsa, args)))
426  {
427  WLog_Print(alsa->log, WLOG_ERROR,
428  "audin_alsa_parse_addin_args failed with errorcode %" PRIu32 "!", error);
429  goto error_out;
430  }
431 
432  if (!alsa->device_name)
433  {
434  alsa->device_name = _strdup("default");
435 
436  if (!alsa->device_name)
437  {
438  WLog_Print(alsa->log, WLOG_ERROR, "_strdup failed!");
439  error = CHANNEL_RC_NO_MEMORY;
440  goto error_out;
441  }
442  }
443 
444  alsa->frames_per_packet = 128;
445  alsa->aformat.nChannels = 2;
446  alsa->aformat.wBitsPerSample = 16;
447  alsa->aformat.wFormatTag = WAVE_FORMAT_PCM;
448  alsa->aformat.nSamplesPerSec = 44100;
449 
450  if ((error = pEntryPoints->pRegisterAudinDevice(pEntryPoints->plugin, (IAudinDevice*)alsa)))
451  {
452  WLog_Print(alsa->log, WLOG_ERROR, "RegisterAudinDevice failed with error %" PRIu32 "!",
453  error);
454  goto error_out;
455  }
456 
457  return CHANNEL_RC_OK;
458 error_out:
459  free(alsa->device_name);
460  free(alsa);
461  return error;
462 }
Definition: client/audin.h:59