2 * FreeRDP: A Remote Desktop Protocol client.
3 * Audio Output Virtual Channel
5 * Copyright 2009-2011 Jay Sorg
6 * Copyright 2010-2011 Vic Lee
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
24 #include <alsa/asoundlib.h>
25 #include <freerdp/types.h>
26 #include <freerdp/utils/memory.h>
27 #include <freerdp/utils/dsp.h>
28 #include <freerdp/utils/svc_plugin.h>
30 #include "rdpsnd_main.h"
32 typedef struct rdpsnd_alsa_plugin rdpsndAlsaPlugin;
33 struct rdpsnd_alsa_plugin
35 rdpsndDevicePlugin device;
38 snd_pcm_t* out_handle;
41 snd_pcm_format_t format;
42 uint32 source_channels;
43 uint32 actual_channels;
44 int bytes_per_channel;
51 static void rdpsnd_alsa_set_params(rdpsndAlsaPlugin* alsa)
53 snd_pcm_hw_params_t* hw_params;
54 snd_pcm_sw_params_t* sw_params;
56 snd_pcm_uframes_t frames;
57 snd_pcm_uframes_t start_threshold;
59 snd_pcm_drop(alsa->out_handle);
61 error = snd_pcm_hw_params_malloc(&hw_params);
64 DEBUG_WARN("snd_pcm_hw_params_malloc failed");
67 snd_pcm_hw_params_any(alsa->out_handle, hw_params);
68 snd_pcm_hw_params_set_access(alsa->out_handle, hw_params,
69 SND_PCM_ACCESS_RW_INTERLEAVED);
70 snd_pcm_hw_params_set_format(alsa->out_handle, hw_params,
72 snd_pcm_hw_params_set_rate_near(alsa->out_handle, hw_params,
73 &alsa->actual_rate, NULL);
74 snd_pcm_hw_params_set_channels_near(alsa->out_handle, hw_params,
75 &alsa->actual_channels);
76 if (alsa->latency < 0)
77 frames = alsa->actual_rate * 4; /* Default to 4-second buffer */
79 frames = alsa->latency * alsa->actual_rate * 2 / 1000; /* Double of the latency */
80 if (frames < alsa->actual_rate / 2)
81 frames = alsa->actual_rate / 2; /* Minimum 0.5-second buffer */
82 snd_pcm_hw_params_set_buffer_size_near(alsa->out_handle, hw_params,
84 snd_pcm_hw_params(alsa->out_handle, hw_params);
85 snd_pcm_hw_params_free(hw_params);
87 error = snd_pcm_sw_params_malloc(&sw_params);
90 DEBUG_WARN("snd_pcm_sw_params_malloc failed");
93 snd_pcm_sw_params_current(alsa->out_handle, sw_params);
94 if (alsa->latency == 0)
97 start_threshold = frames / 2;
98 snd_pcm_sw_params_set_start_threshold(alsa->out_handle, sw_params, start_threshold);
99 snd_pcm_sw_params(alsa->out_handle, sw_params);
100 snd_pcm_sw_params_free(sw_params);
102 snd_pcm_prepare(alsa->out_handle);
104 DEBUG_SVC("hardware buffer %d frames, playback buffer %.2g seconds",
105 (int)frames, (double)frames / 2.0 / (double)alsa->actual_rate);
106 if ((alsa->actual_rate != alsa->source_rate) ||
107 (alsa->actual_channels != alsa->source_channels))
109 DEBUG_SVC("actual rate %d / channel %d is different from source rate %d / channel %d, resampling required.",
110 alsa->actual_rate, alsa->actual_channels, alsa->source_rate, alsa->source_channels);
114 static void rdpsnd_alsa_set_format(rdpsndDevicePlugin* device, rdpsndFormat* format, int latency)
116 rdpsndAlsaPlugin* alsa = (rdpsndAlsaPlugin*)device;
120 alsa->source_rate = format->nSamplesPerSec;
121 alsa->actual_rate = format->nSamplesPerSec;
122 alsa->source_channels = format->nChannels;
123 alsa->actual_channels = format->nChannels;
124 switch (format->wFormatTag)
127 switch (format->wBitsPerSample)
130 alsa->format = SND_PCM_FORMAT_S8;
131 alsa->bytes_per_channel = 1;
134 alsa->format = SND_PCM_FORMAT_S16_LE;
135 alsa->bytes_per_channel = 2;
140 case 0x11: /* IMA ADPCM */
141 alsa->format = SND_PCM_FORMAT_S16_LE;
142 alsa->bytes_per_channel = 2;
145 alsa->wformat = format->wFormatTag;
146 alsa->block_size = format->nBlockAlign;
149 alsa->latency = latency;
151 rdpsnd_alsa_set_params(alsa);
154 static void rdpsnd_alsa_open(rdpsndDevicePlugin* device, rdpsndFormat* format, int latency)
156 rdpsndAlsaPlugin* alsa = (rdpsndAlsaPlugin*)device;
159 if (alsa->out_handle != 0)
162 DEBUG_SVC("opening");
164 error = snd_pcm_open(&alsa->out_handle, alsa->device_name,
165 SND_PCM_STREAM_PLAYBACK, 0);
168 DEBUG_WARN("snd_pcm_open failed");
172 memset(&alsa->adpcm, 0, sizeof(ADPCM));
173 rdpsnd_alsa_set_format(device, format, latency);
177 static void rdpsnd_alsa_close(rdpsndDevicePlugin* device)
179 rdpsndAlsaPlugin* alsa = (rdpsndAlsaPlugin*)device;
181 if (alsa->out_handle != 0)
184 snd_pcm_drain(alsa->out_handle);
185 snd_pcm_close(alsa->out_handle);
186 alsa->out_handle = 0;
190 static void rdpsnd_alsa_free(rdpsndDevicePlugin* device)
192 rdpsndAlsaPlugin* alsa = (rdpsndAlsaPlugin*)device;
194 rdpsnd_alsa_close(device);
195 xfree(alsa->device_name);
199 static boolean rdpsnd_alsa_format_supported(rdpsndDevicePlugin* device, rdpsndFormat* format)
201 switch (format->wFormatTag)
204 if (format->cbSize == 0 &&
205 format->nSamplesPerSec <= 48000 &&
206 (format->wBitsPerSample == 8 || format->wBitsPerSample == 16) &&
207 (format->nChannels == 1 || format->nChannels == 2))
213 case 0x11: /* IMA ADPCM */
214 if (format->nSamplesPerSec <= 48000 &&
215 format->wBitsPerSample == 4 &&
216 (format->nChannels == 1 || format->nChannels == 2))
225 static void rdpsnd_alsa_set_volume(rdpsndDevicePlugin* device, uint32 value)
229 static void rdpsnd_alsa_play(rdpsndDevicePlugin* device, uint8* data, int size)
231 rdpsndAlsaPlugin* alsa = (rdpsndAlsaPlugin*)device;
235 uint8* resampled_data;
239 int rbytes_per_frame;
240 int sbytes_per_frame;
244 if (alsa->out_handle == 0)
247 if (alsa->wformat == 0x11)
249 decoded_data = dsp_decode_ima_adpcm(&alsa->adpcm,
250 data, size, alsa->source_channels, alsa->block_size, &decoded_size);
260 sbytes_per_frame = alsa->source_channels * alsa->bytes_per_channel;
261 rbytes_per_frame = alsa->actual_channels * alsa->bytes_per_channel;
262 if ((size % sbytes_per_frame) != 0)
264 DEBUG_WARN("error len mod");
268 if ((alsa->source_rate == alsa->actual_rate) &&
269 (alsa->source_channels == alsa->actual_channels))
271 resampled_data = NULL;
275 resampled_data = dsp_resample(src, alsa->bytes_per_channel,
276 alsa->source_channels, alsa->source_rate, size / sbytes_per_frame,
277 alsa->actual_channels, alsa->actual_rate, &frames);
278 DEBUG_SVC("resampled %d frames at %d to %d frames at %d",
279 size / sbytes_per_frame, alsa->source_rate, frames, alsa->actual_rate);
280 size = frames * rbytes_per_frame;
281 src = resampled_data;
289 frames = len / rbytes_per_frame;
290 error = snd_pcm_writei(alsa->out_handle, pindex, frames);
293 snd_pcm_recover(alsa->out_handle, error, 0);
298 DEBUG_WARN("error %d", error);
299 snd_pcm_close(alsa->out_handle);
300 alsa->out_handle = 0;
301 rdpsnd_alsa_open(device, NULL, alsa->latency);
304 pindex += error * rbytes_per_frame;
308 xfree(resampled_data);
313 static void rdpsnd_alsa_start(rdpsndDevicePlugin* device)
315 rdpsndAlsaPlugin* alsa = (rdpsndAlsaPlugin*)device;
317 if (alsa->out_handle == 0)
320 snd_pcm_start(alsa->out_handle);
323 int FreeRDPRdpsndDeviceEntry(PFREERDP_RDPSND_DEVICE_ENTRY_POINTS pEntryPoints)
325 rdpsndAlsaPlugin* alsa;
326 RDP_PLUGIN_DATA* data;
328 alsa = xnew(rdpsndAlsaPlugin);
330 alsa->device.Open = rdpsnd_alsa_open;
331 alsa->device.FormatSupported = rdpsnd_alsa_format_supported;
332 alsa->device.SetFormat = rdpsnd_alsa_set_format;
333 alsa->device.SetVolume = rdpsnd_alsa_set_volume;
334 alsa->device.Play = rdpsnd_alsa_play;
335 alsa->device.Start = rdpsnd_alsa_start;
336 alsa->device.Close = rdpsnd_alsa_close;
337 alsa->device.Free = rdpsnd_alsa_free;
339 data = pEntryPoints->plugin_data;
340 if (data && strcmp((char*)data->data[0], "alsa") == 0)
342 alsa->device_name = xstrdup((char*)data->data[1]);
344 if (alsa->device_name == NULL)
346 alsa->device_name = xstrdup("default");
348 alsa->out_handle = 0;
349 alsa->source_rate = 22050;
350 alsa->actual_rate = 22050;
351 alsa->format = SND_PCM_FORMAT_S16_LE;
352 alsa->source_channels = 2;
353 alsa->actual_channels = 2;
354 alsa->bytes_per_channel = 2;
356 pEntryPoints->pRegisterRdpsndDevice(pEntryPoints->rdpsnd, (rdpsndDevicePlugin*)alsa);