23 #include <freerdp/config.h>
29 #include <winpr/crt.h>
30 #include <winpr/cmdline.h>
31 #include <winpr/sysinfo.h>
32 #include <winpr/collections.h>
34 #include <alsa/asoundlib.h>
36 #include <freerdp/types.h>
37 #include <freerdp/codec/dsp.h>
38 #include <freerdp/channels/log.h>
40 #include "rdpsnd_main.h"
44 rdpsndDevicePlugin device;
49 snd_pcm_t* pcm_handle;
50 snd_mixer_t* mixer_handle;
53 snd_pcm_format_t format;
54 UINT32 actual_channels;
56 snd_pcm_uframes_t buffer_size;
57 snd_pcm_uframes_t period_size;
60 #define SND_PCM_CHECK(_func, _status) \
65 WLog_ERR(TAG, "%s: %d\n", (_func), (_status)); \
70 static int rdpsnd_alsa_set_hw_params(rdpsndAlsaPlugin* alsa)
73 snd_pcm_hw_params_t* hw_params = NULL;
74 snd_pcm_uframes_t buffer_size_max = 0;
75 status = snd_pcm_hw_params_malloc(&hw_params);
76 SND_PCM_CHECK(
"snd_pcm_hw_params_malloc", status);
77 status = snd_pcm_hw_params_any(alsa->pcm_handle, hw_params);
78 SND_PCM_CHECK(
"snd_pcm_hw_params_any", status);
81 snd_pcm_hw_params_set_access(alsa->pcm_handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED);
82 SND_PCM_CHECK(
"snd_pcm_hw_params_set_access", status);
84 status = snd_pcm_hw_params_set_format(alsa->pcm_handle, hw_params, alsa->format);
85 SND_PCM_CHECK(
"snd_pcm_hw_params_set_format", status);
87 status = snd_pcm_hw_params_set_rate_near(alsa->pcm_handle, hw_params, &alsa->actual_rate, NULL);
88 SND_PCM_CHECK(
"snd_pcm_hw_params_set_rate_near", status);
90 status = snd_pcm_hw_params_set_channels(alsa->pcm_handle, hw_params, alsa->actual_channels);
91 SND_PCM_CHECK(
"snd_pcm_hw_params_set_channels", status);
93 status = snd_pcm_hw_params_get_buffer_size_max(hw_params, &buffer_size_max);
94 SND_PCM_CHECK(
"snd_pcm_hw_params_get_buffer_size_max", status);
113 int interrupts_per_sec_near = 50;
114 const size_t bytes_per_sec =
115 (1ull * alsa->actual_rate * alsa->aformat.wBitsPerSample / 8 * alsa->actual_channels);
116 alsa->buffer_size = buffer_size_max;
117 alsa->period_size = (bytes_per_sec / interrupts_per_sec_near);
119 if (alsa->period_size > buffer_size_max)
121 WLog_ERR(TAG,
"Warning: requested sound buffer size %lu, got %lu instead\n",
122 alsa->buffer_size, buffer_size_max);
123 alsa->period_size = (buffer_size_max / 8);
128 snd_pcm_hw_params_set_buffer_size_near(alsa->pcm_handle, hw_params, &alsa->buffer_size);
129 SND_PCM_CHECK(
"snd_pcm_hw_params_set_buffer_size_near", status);
131 status = snd_pcm_hw_params_set_period_size_near(alsa->pcm_handle, hw_params, &alsa->period_size,
133 SND_PCM_CHECK(
"snd_pcm_hw_params_set_period_size_near", status);
134 status = snd_pcm_hw_params(alsa->pcm_handle, hw_params);
135 SND_PCM_CHECK(
"snd_pcm_hw_params", status);
136 snd_pcm_hw_params_free(hw_params);
140 static int rdpsnd_alsa_set_sw_params(rdpsndAlsaPlugin* alsa)
143 snd_pcm_sw_params_t* sw_params = NULL;
144 status = snd_pcm_sw_params_malloc(&sw_params);
145 SND_PCM_CHECK(
"snd_pcm_sw_params_malloc", status);
146 status = snd_pcm_sw_params_current(alsa->pcm_handle, sw_params);
147 SND_PCM_CHECK(
"snd_pcm_sw_params_current", status);
148 status = snd_pcm_sw_params_set_avail_min(
149 alsa->pcm_handle, sw_params, (1ULL * alsa->aformat.nChannels * alsa->actual_channels));
150 SND_PCM_CHECK(
"snd_pcm_sw_params_set_avail_min", status);
151 status = snd_pcm_sw_params_set_start_threshold(alsa->pcm_handle, sw_params,
152 alsa->aformat.nBlockAlign);
153 SND_PCM_CHECK(
"snd_pcm_sw_params_set_start_threshold", status);
154 status = snd_pcm_sw_params(alsa->pcm_handle, sw_params);
155 SND_PCM_CHECK(
"snd_pcm_sw_params", status);
156 snd_pcm_sw_params_free(sw_params);
157 status = snd_pcm_prepare(alsa->pcm_handle);
158 SND_PCM_CHECK(
"snd_pcm_prepare", status);
162 static int rdpsnd_alsa_validate_params(rdpsndAlsaPlugin* alsa)
165 snd_pcm_uframes_t buffer_size = 0;
166 snd_pcm_uframes_t period_size = 0;
167 status = snd_pcm_get_params(alsa->pcm_handle, &buffer_size, &period_size);
168 SND_PCM_CHECK(
"snd_pcm_get_params", status);
172 static int rdpsnd_alsa_set_params(rdpsndAlsaPlugin* alsa)
174 snd_pcm_drop(alsa->pcm_handle);
176 if (rdpsnd_alsa_set_hw_params(alsa) < 0)
179 if (rdpsnd_alsa_set_sw_params(alsa) < 0)
182 return rdpsnd_alsa_validate_params(alsa);
185 static BOOL rdpsnd_alsa_set_format(rdpsndDevicePlugin* device,
const AUDIO_FORMAT* format,
188 rdpsndAlsaPlugin* alsa = (rdpsndAlsaPlugin*)device;
192 alsa->aformat = *format;
193 alsa->actual_rate = format->nSamplesPerSec;
194 alsa->actual_channels = format->nChannels;
196 switch (format->wFormatTag)
198 case WAVE_FORMAT_PCM:
199 switch (format->wBitsPerSample)
202 alsa->format = SND_PCM_FORMAT_S8;
206 alsa->format = SND_PCM_FORMAT_S16_LE;
220 alsa->latency = latency;
221 return (rdpsnd_alsa_set_params(alsa) == 0);
224 static void rdpsnd_alsa_close_mixer(rdpsndAlsaPlugin* alsa)
226 if (alsa && alsa->mixer_handle)
228 snd_mixer_close(alsa->mixer_handle);
229 alsa->mixer_handle = NULL;
233 static BOOL rdpsnd_alsa_open_mixer(rdpsndAlsaPlugin* alsa)
237 if (alsa->mixer_handle)
240 status = snd_mixer_open(&alsa->mixer_handle, 0);
244 WLog_ERR(TAG,
"snd_mixer_open failed");
248 status = snd_mixer_attach(alsa->mixer_handle, alsa->device_name);
252 WLog_ERR(TAG,
"snd_mixer_attach failed");
256 status = snd_mixer_selem_register(alsa->mixer_handle, NULL, NULL);
260 WLog_ERR(TAG,
"snd_mixer_selem_register failed");
264 status = snd_mixer_load(alsa->mixer_handle);
268 WLog_ERR(TAG,
"snd_mixer_load failed");
274 rdpsnd_alsa_close_mixer(alsa);
278 static void rdpsnd_alsa_pcm_close(rdpsndAlsaPlugin* alsa)
280 if (alsa && alsa->pcm_handle)
282 snd_pcm_drain(alsa->pcm_handle);
283 snd_pcm_close(alsa->pcm_handle);
284 alsa->pcm_handle = 0;
288 static BOOL rdpsnd_alsa_open(rdpsndDevicePlugin* device,
const AUDIO_FORMAT* format, UINT32 latency)
292 rdpsndAlsaPlugin* alsa = (rdpsndAlsaPlugin*)device;
294 if (alsa->pcm_handle)
299 status = snd_pcm_open(&alsa->pcm_handle, alsa->device_name, SND_PCM_STREAM_PLAYBACK, mode);
303 WLog_ERR(TAG,
"snd_pcm_open failed");
307 return rdpsnd_alsa_set_format(device, format, latency) && rdpsnd_alsa_open_mixer(alsa);
310 static void rdpsnd_alsa_close(rdpsndDevicePlugin* device)
312 rdpsndAlsaPlugin* alsa = (rdpsndAlsaPlugin*)device;
317 rdpsnd_alsa_close_mixer(alsa);
320 static void rdpsnd_alsa_free(rdpsndDevicePlugin* device)
322 rdpsndAlsaPlugin* alsa = (rdpsndAlsaPlugin*)device;
323 rdpsnd_alsa_pcm_close(alsa);
324 rdpsnd_alsa_close_mixer(alsa);
325 free(alsa->device_name);
329 static BOOL rdpsnd_alsa_format_supported(rdpsndDevicePlugin* device,
const AUDIO_FORMAT* format)
331 switch (format->wFormatTag)
333 case WAVE_FORMAT_PCM:
334 if (format->cbSize == 0 && format->nSamplesPerSec <= 48000 &&
335 (format->wBitsPerSample == 8 || format->wBitsPerSample == 16) &&
336 (format->nChannels == 1 || format->nChannels == 2))
349 static UINT32 rdpsnd_alsa_get_volume(rdpsndDevicePlugin* device)
353 long volume_left = 0;
354 long volume_right = 0;
356 UINT16 dwVolumeLeft = 0;
357 UINT16 dwVolumeRight = 0;
358 rdpsndAlsaPlugin* alsa = (rdpsndAlsaPlugin*)device;
359 dwVolumeLeft = ((50 * 0xFFFF) / 100);
360 dwVolumeRight = ((50 * 0xFFFF) / 100);
362 if (!rdpsnd_alsa_open_mixer(alsa))
365 for (snd_mixer_elem_t* elem = snd_mixer_first_elem(alsa->mixer_handle); elem;
366 elem = snd_mixer_elem_next(elem))
368 if (snd_mixer_selem_has_playback_volume(elem))
370 snd_mixer_selem_get_playback_volume_range(elem, &volume_min, &volume_max);
371 snd_mixer_selem_get_playback_volume(elem, SND_MIXER_SCHN_FRONT_LEFT, &volume_left);
372 snd_mixer_selem_get_playback_volume(elem, SND_MIXER_SCHN_FRONT_RIGHT, &volume_right);
374 (UINT16)(((volume_left * 0xFFFF) - volume_min) / (volume_max - volume_min));
376 (UINT16)(((volume_right * 0xFFFF) - volume_min) / (volume_max - volume_min));
381 dwVolume = (dwVolumeLeft << 16) | dwVolumeRight;
385 static BOOL rdpsnd_alsa_set_volume(rdpsndDevicePlugin* device, UINT32 value)
391 long volume_left = 0;
392 long volume_right = 0;
393 rdpsndAlsaPlugin* alsa = (rdpsndAlsaPlugin*)device;
395 if (!rdpsnd_alsa_open_mixer(alsa))
398 left = (value & 0xFFFF);
399 right = ((value >> 16) & 0xFFFF);
401 for (snd_mixer_elem_t* elem = snd_mixer_first_elem(alsa->mixer_handle); elem;
402 elem = snd_mixer_elem_next(elem))
404 if (snd_mixer_selem_has_playback_volume(elem))
406 snd_mixer_selem_get_playback_volume_range(elem, &volume_min, &volume_max);
407 volume_left = volume_min + (left * (volume_max - volume_min)) / 0xFFFF;
408 volume_right = volume_min + (right * (volume_max - volume_min)) / 0xFFFF;
410 if ((snd_mixer_selem_set_playback_volume(elem, SND_MIXER_SCHN_FRONT_LEFT, volume_left) <
412 (snd_mixer_selem_set_playback_volume(elem, SND_MIXER_SCHN_FRONT_RIGHT,
415 WLog_ERR(TAG,
"error setting the volume\n");
424 static UINT rdpsnd_alsa_play(rdpsndDevicePlugin* device,
const BYTE* data,
size_t size)
428 rdpsndAlsaPlugin* alsa = (rdpsndAlsaPlugin*)device;
430 WINPR_ASSERT(data || (size == 0));
431 const size_t frame_size = 1ull * alsa->actual_channels * alsa->aformat.wBitsPerSample / 8;
435 while (offset < size)
437 snd_pcm_sframes_t status =
438 snd_pcm_writei(alsa->pcm_handle, &data[offset], (size - offset) / frame_size);
441 status = snd_pcm_recover(alsa->pcm_handle, (
int)status, 0);
445 WLog_ERR(TAG,
"status: %d\n", status);
446 rdpsnd_alsa_close(device);
447 rdpsnd_alsa_open(device, NULL, alsa->latency);
451 offset += status * frame_size;
455 snd_pcm_sframes_t available = 0;
456 snd_pcm_sframes_t delay = 0;
457 int rc = snd_pcm_avail_delay(alsa->pcm_handle, &available, &delay);
459 if ((rc == 0) && (available == 0))
460 latency = (UINT32)MIN(UINT32_MAX, delay * 1000 / alsa->actual_rate);
463 return latency + alsa->latency;
471 static UINT rdpsnd_alsa_parse_addin_args(rdpsndDevicePlugin* device,
const ADDIN_ARGV* args)
476 rdpsndAlsaPlugin* alsa = (rdpsndAlsaPlugin*)device;
478 NULL, NULL, -1, NULL,
"device" },
479 { NULL, 0, NULL, NULL, NULL, -1, NULL, NULL } };
481 COMMAND_LINE_SIGIL_NONE | COMMAND_LINE_SEPARATOR_COLON | COMMAND_LINE_IGN_UNKNOWN_KEYWORD;
482 status = CommandLineParseArgumentsA(args->argc, args->argv, rdpsnd_alsa_args, flags, alsa, NULL,
487 WLog_ERR(TAG,
"CommandLineParseArgumentsA failed!");
488 return CHANNEL_RC_INITIALIZATION_ERROR;
491 arg = rdpsnd_alsa_args;
495 if (!(arg->Flags & COMMAND_LINE_VALUE_PRESENT))
498 CommandLineSwitchStart(arg) CommandLineSwitchCase(arg,
"dev")
500 alsa->device_name = _strdup(arg->Value);
502 if (!alsa->device_name)
503 return CHANNEL_RC_NO_MEMORY;
505 CommandLineSwitchEnd(arg)
506 }
while ((arg = CommandLineFindNextArgumentA(arg)) != NULL);
508 return CHANNEL_RC_OK;
516 FREERDP_ENTRY_POINT(UINT VCAPITYPE alsa_freerdp_rdpsnd_client_subsystem_entry(
520 rdpsndAlsaPlugin* alsa = NULL;
522 alsa = (rdpsndAlsaPlugin*)calloc(1,
sizeof(rdpsndAlsaPlugin));
526 WLog_ERR(TAG,
"calloc failed!");
527 return CHANNEL_RC_NO_MEMORY;
530 alsa->device.Open = rdpsnd_alsa_open;
531 alsa->device.FormatSupported = rdpsnd_alsa_format_supported;
532 alsa->device.GetVolume = rdpsnd_alsa_get_volume;
533 alsa->device.SetVolume = rdpsnd_alsa_set_volume;
534 alsa->device.Play = rdpsnd_alsa_play;
535 alsa->device.Close = rdpsnd_alsa_close;
536 alsa->device.Free = rdpsnd_alsa_free;
537 args = pEntryPoints->args;
541 if ((error = rdpsnd_alsa_parse_addin_args(&alsa->device, args)))
543 WLog_ERR(TAG,
"rdpsnd_alsa_parse_addin_args failed with error %" PRIu32
"", error);
544 goto error_parse_args;
548 if (!alsa->device_name)
550 alsa->device_name = _strdup(
"default");
552 if (!alsa->device_name)
554 WLog_ERR(TAG,
"_strdup failed!");
555 error = CHANNEL_RC_NO_MEMORY;
560 alsa->pcm_handle = 0;
561 alsa->actual_rate = 22050;
562 alsa->format = SND_PCM_FORMAT_S16_LE;
563 alsa->actual_channels = 2;
564 pEntryPoints->pRegisterRdpsndDevice(pEntryPoints->rdpsnd, (rdpsndDevicePlugin*)alsa);
565 return CHANNEL_RC_OK;
567 free(alsa->device_name);