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,
191  WINPR_ASSERTING_INT_CAST(size_t, framesRead) * alsa->bytes_per_frame,
192  alsa->user_data);
193 
194  if (error)
195  {
196  WLog_Print(alsa->log, WLOG_ERROR, "audin_alsa_thread_receive failed with error %ld",
197  error);
198  break;
199  }
200  }
201 
202  free(buffer);
203 
204  if (capture_handle)
205  {
206  const int res = snd_pcm_close(capture_handle);
207  if (res < 0)
208  WLog_Print(alsa->log, WLOG_WARN, "snd_pcm_close (%s)", snd_strerror(res));
209  }
210 
211 out:
212  WLog_Print(alsa->log, WLOG_DEBUG, "out");
213 
214  if (error && alsa->rdpcontext)
215  setChannelError(alsa->rdpcontext, error, "audin_alsa_thread_func reported an error");
216 
217  ExitThread(error);
218  return error;
219 }
220 
226 static UINT audin_alsa_free(IAudinDevice* device)
227 {
228  AudinALSADevice* alsa = (AudinALSADevice*)device;
229 
230  if (alsa)
231  free(alsa->device_name);
232 
233  free(alsa);
234  return CHANNEL_RC_OK;
235 }
236 
237 static BOOL audin_alsa_format_supported(IAudinDevice* device, const AUDIO_FORMAT* format)
238 {
239  if (!device || !format)
240  return FALSE;
241 
242  switch (format->wFormatTag)
243  {
244  case WAVE_FORMAT_PCM:
245  if (format->cbSize == 0 && (format->nSamplesPerSec <= 48000) &&
246  (format->wBitsPerSample == 8 || format->wBitsPerSample == 16) &&
247  (format->nChannels == 1 || format->nChannels == 2))
248  {
249  return TRUE;
250  }
251 
252  break;
253 
254  default:
255  return FALSE;
256  }
257 
258  return FALSE;
259 }
260 
266 static UINT audin_alsa_set_format(IAudinDevice* device, const AUDIO_FORMAT* format,
267  UINT32 FramesPerPacket)
268 {
269  AudinALSADevice* alsa = (AudinALSADevice*)device;
270 
271  if (!alsa || !format)
272  return ERROR_INVALID_PARAMETER;
273 
274  alsa->aformat = *format;
275  alsa->frames_per_packet = FramesPerPacket;
276 
277  if (audin_alsa_format(format->wFormatTag, format->wBitsPerSample) == SND_PCM_FORMAT_UNKNOWN)
278  return ERROR_INTERNAL_ERROR;
279 
280  return CHANNEL_RC_OK;
281 }
282 
288 static UINT audin_alsa_open(IAudinDevice* device, AudinReceive receive, void* user_data)
289 {
290  AudinALSADevice* alsa = (AudinALSADevice*)device;
291 
292  if (!device || !receive || !user_data)
293  return ERROR_INVALID_PARAMETER;
294 
295  alsa->receive = receive;
296  alsa->user_data = user_data;
297 
298  if (!(alsa->stopEvent = CreateEvent(NULL, TRUE, FALSE, NULL)))
299  {
300  WLog_Print(alsa->log, WLOG_ERROR, "CreateEvent failed!");
301  goto error_out;
302  }
303 
304  if (!(alsa->thread = CreateThread(NULL, 0, audin_alsa_thread_func, alsa, 0, NULL)))
305  {
306  WLog_Print(alsa->log, WLOG_ERROR, "CreateThread failed!");
307  goto error_out;
308  }
309 
310  return CHANNEL_RC_OK;
311 error_out:
312  (void)CloseHandle(alsa->stopEvent);
313  alsa->stopEvent = NULL;
314  return ERROR_INTERNAL_ERROR;
315 }
316 
322 static UINT audin_alsa_close(IAudinDevice* device)
323 {
324  UINT error = CHANNEL_RC_OK;
325  AudinALSADevice* alsa = (AudinALSADevice*)device;
326 
327  if (!alsa)
328  return ERROR_INVALID_PARAMETER;
329 
330  if (alsa->stopEvent)
331  {
332  (void)SetEvent(alsa->stopEvent);
333 
334  if (WaitForSingleObject(alsa->thread, INFINITE) == WAIT_FAILED)
335  {
336  error = GetLastError();
337  WLog_Print(alsa->log, WLOG_ERROR, "WaitForSingleObject failed with error %" PRIu32 "",
338  error);
339  return error;
340  }
341 
342  (void)CloseHandle(alsa->stopEvent);
343  alsa->stopEvent = NULL;
344  (void)CloseHandle(alsa->thread);
345  alsa->thread = NULL;
346  }
347 
348  alsa->receive = NULL;
349  alsa->user_data = NULL;
350  return error;
351 }
352 
358 static UINT audin_alsa_parse_addin_args(AudinALSADevice* device, const ADDIN_ARGV* args)
359 {
360  int status = 0;
361  DWORD flags = 0;
362  const COMMAND_LINE_ARGUMENT_A* arg = NULL;
363  AudinALSADevice* alsa = device;
364  COMMAND_LINE_ARGUMENT_A audin_alsa_args[] = { { "dev", COMMAND_LINE_VALUE_REQUIRED, "<device>",
365  NULL, NULL, -1, NULL, "audio device name" },
366  { NULL, 0, NULL, NULL, NULL, -1, NULL, NULL } };
367  flags =
368  COMMAND_LINE_SIGIL_NONE | COMMAND_LINE_SEPARATOR_COLON | COMMAND_LINE_IGN_UNKNOWN_KEYWORD;
369  status = CommandLineParseArgumentsA(args->argc, args->argv, audin_alsa_args, flags, alsa, NULL,
370  NULL);
371 
372  if (status < 0)
373  return ERROR_INVALID_PARAMETER;
374 
375  arg = audin_alsa_args;
376 
377  do
378  {
379  if (!(arg->Flags & COMMAND_LINE_VALUE_PRESENT))
380  continue;
381 
382  CommandLineSwitchStart(arg) CommandLineSwitchCase(arg, "dev")
383  {
384  alsa->device_name = _strdup(arg->Value);
385 
386  if (!alsa->device_name)
387  {
388  WLog_Print(alsa->log, WLOG_ERROR, "_strdup failed!");
389  return CHANNEL_RC_NO_MEMORY;
390  }
391  }
392  CommandLineSwitchEnd(arg)
393  } while ((arg = CommandLineFindNextArgumentA(arg)) != NULL);
394 
395  return CHANNEL_RC_OK;
396 }
397 
403 FREERDP_ENTRY_POINT(UINT VCAPITYPE alsa_freerdp_audin_client_subsystem_entry(
405 {
406  const ADDIN_ARGV* args = NULL;
407  AudinALSADevice* alsa = NULL;
408  UINT error = 0;
409  alsa = (AudinALSADevice*)calloc(1, sizeof(AudinALSADevice));
410 
411  if (!alsa)
412  {
413  WLog_ERR(TAG, "calloc failed!");
414  return CHANNEL_RC_NO_MEMORY;
415  }
416 
417  alsa->log = WLog_Get(TAG);
418  alsa->iface.Open = audin_alsa_open;
419  alsa->iface.FormatSupported = audin_alsa_format_supported;
420  alsa->iface.SetFormat = audin_alsa_set_format;
421  alsa->iface.Close = audin_alsa_close;
422  alsa->iface.Free = audin_alsa_free;
423  alsa->rdpcontext = pEntryPoints->rdpcontext;
424  args = pEntryPoints->args;
425 
426  if ((error = audin_alsa_parse_addin_args(alsa, args)))
427  {
428  WLog_Print(alsa->log, WLOG_ERROR,
429  "audin_alsa_parse_addin_args failed with errorcode %" PRIu32 "!", error);
430  goto error_out;
431  }
432 
433  if (!alsa->device_name)
434  {
435  alsa->device_name = _strdup("default");
436 
437  if (!alsa->device_name)
438  {
439  WLog_Print(alsa->log, WLOG_ERROR, "_strdup failed!");
440  error = CHANNEL_RC_NO_MEMORY;
441  goto error_out;
442  }
443  }
444 
445  alsa->frames_per_packet = 128;
446  alsa->aformat.nChannels = 2;
447  alsa->aformat.wBitsPerSample = 16;
448  alsa->aformat.wFormatTag = WAVE_FORMAT_PCM;
449  alsa->aformat.nSamplesPerSec = 44100;
450 
451  if ((error = pEntryPoints->pRegisterAudinDevice(pEntryPoints->plugin, (IAudinDevice*)alsa)))
452  {
453  WLog_Print(alsa->log, WLOG_ERROR, "RegisterAudinDevice failed with error %" PRIu32 "!",
454  error);
455  goto error_out;
456  }
457 
458  return CHANNEL_RC_OK;
459 error_out:
460  free(alsa->device_name);
461  free(alsa);
462  return error;
463 }
Definition: client/audin.h:59