Initial commit - from Precise source
[freerdp-ubuntu-pcb-backport.git] / channels / rdpsnd / alsa / rdpsnd_alsa.c
1 /**
2  * FreeRDP: A Remote Desktop Protocol client.
3  * Audio Output Virtual Channel
4  *
5  * Copyright 2009-2011 Jay Sorg
6  * Copyright 2010-2011 Vic Lee
7  *
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
11  *
12  *     http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
19  */
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
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>
29
30 #include "rdpsnd_main.h"
31
32 typedef struct rdpsnd_alsa_plugin rdpsndAlsaPlugin;
33 struct rdpsnd_alsa_plugin
34 {
35         rdpsndDevicePlugin device;
36
37         char* device_name;
38         snd_pcm_t* out_handle;
39         uint32 source_rate;
40         uint32 actual_rate;
41         snd_pcm_format_t format;
42         uint32 source_channels;
43         uint32 actual_channels;
44         int bytes_per_channel;
45         int wformat;
46         int block_size;
47         int latency;
48         ADPCM adpcm;
49 };
50
51 static void rdpsnd_alsa_set_params(rdpsndAlsaPlugin* alsa)
52 {
53         snd_pcm_hw_params_t* hw_params;
54         snd_pcm_sw_params_t* sw_params;
55         int error;
56         snd_pcm_uframes_t frames;
57         snd_pcm_uframes_t start_threshold;
58
59         snd_pcm_drop(alsa->out_handle);
60
61         error = snd_pcm_hw_params_malloc(&hw_params);
62         if (error < 0)
63         {
64                 DEBUG_WARN("snd_pcm_hw_params_malloc failed");
65                 return;
66         }
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,
71                 alsa->format);
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 */
78         else
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,
83                 &frames);
84         snd_pcm_hw_params(alsa->out_handle, hw_params);
85         snd_pcm_hw_params_free(hw_params);
86
87         error = snd_pcm_sw_params_malloc(&sw_params);
88         if (error < 0)
89         {
90                 DEBUG_WARN("snd_pcm_sw_params_malloc failed");
91                 return;
92         }
93         snd_pcm_sw_params_current(alsa->out_handle, sw_params);
94         if (alsa->latency == 0)
95                 start_threshold = 0;
96         else
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);
101
102         snd_pcm_prepare(alsa->out_handle);
103
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))
108         {
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);
111         }
112 }
113
114 static void rdpsnd_alsa_set_format(rdpsndDevicePlugin* device, rdpsndFormat* format, int latency)
115 {
116         rdpsndAlsaPlugin* alsa = (rdpsndAlsaPlugin*)device;
117
118         if (format != NULL)
119         {
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)
125                 {
126                         case 1: /* PCM */
127                                 switch (format->wBitsPerSample)
128                                 {
129                                         case 8:
130                                                 alsa->format = SND_PCM_FORMAT_S8;
131                                                 alsa->bytes_per_channel = 1;
132                                                 break;
133                                         case 16:
134                                                 alsa->format = SND_PCM_FORMAT_S16_LE;
135                                                 alsa->bytes_per_channel = 2;
136                                                 break;
137                                 }
138                                 break;
139
140                         case 0x11: /* IMA ADPCM */
141                                 alsa->format = SND_PCM_FORMAT_S16_LE;
142                                 alsa->bytes_per_channel = 2;
143                                 break;
144                 }
145                 alsa->wformat = format->wFormatTag;
146                 alsa->block_size = format->nBlockAlign;
147         }
148
149         alsa->latency = latency;
150
151         rdpsnd_alsa_set_params(alsa);
152 }
153
154 static void rdpsnd_alsa_open(rdpsndDevicePlugin* device, rdpsndFormat* format, int latency)
155 {
156         rdpsndAlsaPlugin* alsa = (rdpsndAlsaPlugin*)device;
157         int error;
158
159         if (alsa->out_handle != 0)
160                 return;
161
162         DEBUG_SVC("opening");
163
164         error = snd_pcm_open(&alsa->out_handle, alsa->device_name,
165                 SND_PCM_STREAM_PLAYBACK, 0);
166         if (error < 0)
167         {
168                 DEBUG_WARN("snd_pcm_open failed");
169         }
170         else
171         {
172                 memset(&alsa->adpcm, 0, sizeof(ADPCM));
173                 rdpsnd_alsa_set_format(device, format, latency);
174         }
175 }
176
177 static void rdpsnd_alsa_close(rdpsndDevicePlugin* device)
178 {
179         rdpsndAlsaPlugin* alsa = (rdpsndAlsaPlugin*)device;
180
181         if (alsa->out_handle != 0)
182         {
183                 DEBUG_SVC("close");
184                 snd_pcm_drain(alsa->out_handle);
185                 snd_pcm_close(alsa->out_handle);
186                 alsa->out_handle = 0;
187         }
188 }
189
190 static void rdpsnd_alsa_free(rdpsndDevicePlugin* device)
191 {
192         rdpsndAlsaPlugin* alsa = (rdpsndAlsaPlugin*)device;
193
194         rdpsnd_alsa_close(device);
195         xfree(alsa->device_name);
196         xfree(alsa);
197 }
198
199 static boolean rdpsnd_alsa_format_supported(rdpsndDevicePlugin* device, rdpsndFormat* format)
200 {
201         switch (format->wFormatTag)
202         {
203                 case 1: /* PCM */
204                         if (format->cbSize == 0 &&
205                                 format->nSamplesPerSec <= 48000 &&
206                                 (format->wBitsPerSample == 8 || format->wBitsPerSample == 16) &&
207                                 (format->nChannels == 1 || format->nChannels == 2))
208                         {
209                                 return true;
210                         }
211                         break;
212
213                 case 0x11: /* IMA ADPCM */
214                         if (format->nSamplesPerSec <= 48000 &&
215                                 format->wBitsPerSample == 4 &&
216                                 (format->nChannels == 1 || format->nChannels == 2))
217                         {
218                                 return true;
219                         }
220                         break;
221         }
222         return false;
223 }
224
225 static void rdpsnd_alsa_set_volume(rdpsndDevicePlugin* device, uint32 value)
226 {
227 }
228
229 static void rdpsnd_alsa_play(rdpsndDevicePlugin* device, uint8* data, int size)
230 {
231         rdpsndAlsaPlugin* alsa = (rdpsndAlsaPlugin*)device;
232         uint8* decoded_data;
233         int decoded_size;
234         uint8* src;
235         uint8* resampled_data;
236         int len;
237         int error;
238         int frames;
239         int rbytes_per_frame;
240         int sbytes_per_frame;
241         uint8* pindex;
242         uint8* end;
243
244         if (alsa->out_handle == 0)
245                 return;
246
247         if (alsa->wformat == 0x11)
248         {
249                 decoded_data = dsp_decode_ima_adpcm(&alsa->adpcm,
250                         data, size, alsa->source_channels, alsa->block_size, &decoded_size);
251                 size = decoded_size;
252                 src = decoded_data;
253         }
254         else
255         {
256                 decoded_data = NULL;
257                 src = data;
258         }
259
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)
263         {
264                 DEBUG_WARN("error len mod");
265                 return;
266         }
267
268         if ((alsa->source_rate == alsa->actual_rate) &&
269                 (alsa->source_channels == alsa->actual_channels))
270         {
271                 resampled_data = NULL;
272         }
273         else
274         {
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;
282         }
283
284         pindex = src;
285         end = pindex + size;
286         while (pindex < end)
287         {
288                 len = end - pindex;
289                 frames = len / rbytes_per_frame;
290                 error = snd_pcm_writei(alsa->out_handle, pindex, frames);
291                 if (error == -EPIPE)
292                 {
293                         snd_pcm_recover(alsa->out_handle, error, 0);
294                         error = 0;
295                 }
296                 else if (error < 0)
297                 {
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);
302                         break;
303                 }
304                 pindex += error * rbytes_per_frame;
305         }
306
307         if (resampled_data)
308                 xfree(resampled_data);
309         if (decoded_data)
310                 xfree(decoded_data);
311 }
312
313 static void rdpsnd_alsa_start(rdpsndDevicePlugin* device)
314 {
315         rdpsndAlsaPlugin* alsa = (rdpsndAlsaPlugin*)device;
316
317         if (alsa->out_handle == 0)
318                 return;
319
320         snd_pcm_start(alsa->out_handle);
321 }
322
323 int FreeRDPRdpsndDeviceEntry(PFREERDP_RDPSND_DEVICE_ENTRY_POINTS pEntryPoints)
324 {
325         rdpsndAlsaPlugin* alsa;
326         RDP_PLUGIN_DATA* data;
327
328         alsa = xnew(rdpsndAlsaPlugin);
329
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;
338
339         data = pEntryPoints->plugin_data;
340         if (data && strcmp((char*)data->data[0], "alsa") == 0)
341         {
342                 alsa->device_name = xstrdup((char*)data->data[1]);
343         }
344         if (alsa->device_name == NULL)
345         {
346                 alsa->device_name = xstrdup("default");
347         }
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;
355
356         pEntryPoints->pRegisterRdpsndDevice(pEntryPoints->rdpsnd, (rdpsndDevicePlugin*)alsa);
357
358         return 0;
359 }
360