8a3388af97391e433ef2dd0bb7e409d5e3602f63
[profile/ivi/pulseaudio-panda.git] / src / module-alsa-sink.c
1 /* $Id$ */
2
3 /***
4   This file is part of polypaudio.
5  
6   polypaudio is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published
8   by the Free Software Foundation; either version 2 of the License,
9   or (at your option) any later version.
10  
11   polypaudio is distributed in the hope that it will be useful, but
12   WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14   General Public License for more details.
15  
16   You should have received a copy of the GNU General Public License
17   along with polypaudio; if not, write to the Free Software
18   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19   USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <assert.h>
27 #include <stdio.h>
28 #include <sys/poll.h>
29
30 #include <asoundlib.h>
31
32 #include "module.h"
33 #include "core.h"
34 #include "memchunk.h"
35 #include "sink.h"
36 #include "modargs.h"
37 #include "util.h"
38 #include "sample-util.h"
39 #include "alsa-util.h"
40
41 struct userdata {
42     snd_pcm_t *pcm_handle;
43     struct pa_sink *sink;
44     void **io_sources;
45     unsigned n_io_sources;
46
47     size_t frame_size, fragment_size;
48     struct pa_memchunk memchunk, silence;
49 };
50
51 static const char* const valid_modargs[] = {
52     "device",
53     "sink_name",
54     "format",
55     "channels",
56     "rate",
57     "fragments",
58     "fragment_size",
59     NULL
60 };
61
62 #define DEFAULT_SINK_NAME "alsa_output"
63 #define DEFAULT_DEVICE "plughw:0,0"
64
65 static void xrun_recovery(struct userdata *u) {
66     assert(u);
67
68     fprintf(stderr, "*** ALSA-XRUN (playback) ***\n");
69     
70     if (snd_pcm_prepare(u->pcm_handle) < 0)
71         fprintf(stderr, "snd_pcm_prepare() failed\n");
72 }
73
74 static void do_write(struct userdata *u) {
75     assert(u);
76
77     for (;;) {
78         struct pa_memchunk *memchunk = NULL;
79         snd_pcm_sframes_t frames;
80         
81         if (u->memchunk.memblock)
82             memchunk = &u->memchunk;
83         else {
84             if (pa_sink_render(u->sink, u->fragment_size, &u->memchunk) < 0)
85                 memchunk = &u->silence;
86             else
87                 memchunk = &u->memchunk;
88         }
89             
90         assert(memchunk->memblock && memchunk->memblock->data && memchunk->length && memchunk->memblock->length && (memchunk->length % u->frame_size) == 0);
91
92         if ((frames = snd_pcm_writei(u->pcm_handle, memchunk->memblock->data + memchunk->index, memchunk->length / u->frame_size)) < 0) {
93             if (frames == -EAGAIN)
94                 return;
95
96             if (frames == -EPIPE) {
97                 xrun_recovery(u);
98                 continue;
99             }
100
101             fprintf(stderr, "snd_pcm_writei() failed\n");
102             return;
103         }
104
105         if (memchunk == &u->memchunk) {
106             size_t l = frames * u->frame_size;
107             memchunk->index += l;
108             memchunk->length -= l;
109
110             if (memchunk->length == 0) {
111                 pa_memblock_unref(memchunk->memblock);
112                 memchunk->memblock = NULL;
113                 memchunk->index = memchunk->length = 0;
114             }
115         }
116         
117         break;
118     }
119 }
120
121 static void io_callback(struct pa_mainloop_api*a, void *id, int fd, enum pa_mainloop_api_io_events events, void *userdata) {
122     struct userdata *u = userdata;
123     assert(u && a && id);
124
125     if (snd_pcm_state(u->pcm_handle) == SND_PCM_STATE_XRUN)
126         xrun_recovery(u);
127
128     do_write(u);
129 }
130
131 static uint32_t sink_get_latency_cb(struct pa_sink *s) {
132     struct userdata *u = s->userdata;
133     snd_pcm_sframes_t frames;
134     assert(s && u && u->sink);
135
136     if (snd_pcm_delay(u->pcm_handle, &frames) < 0) {
137         fprintf(stderr, __FILE__": failed to get delay\n");
138         s->get_latency = NULL;
139         return 0;
140     }
141
142     if (frames < 0)
143         frames = 0;
144     
145     return pa_samples_usec(frames * u->frame_size, &s->sample_spec);
146 }
147
148 int pa_module_init(struct pa_core *c, struct pa_module*m) {
149     struct pa_modargs *ma = NULL;
150     int ret = -1;
151     struct userdata *u = NULL;
152     const char *dev;
153     struct pa_sample_spec ss;
154     unsigned periods, fragsize;
155     snd_pcm_uframes_t buffer_size;
156     size_t frame_size;
157     
158     if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
159         fprintf(stderr, __FILE__": failed to parse module arguments\n");
160         goto fail;
161     }
162
163     ss = c->default_sample_spec;
164     if (pa_modargs_get_sample_spec(ma, &ss) < 0) {
165         fprintf(stderr, __FILE__": failed to parse sample specification\n");
166         goto fail;
167     }
168     frame_size = pa_sample_size(&ss);
169     
170     periods = 12;
171     fragsize = 1024;
172     if (pa_modargs_get_value_u32(ma, "fragments", &periods) < 0 || pa_modargs_get_value_u32(ma, "fragment_size", &fragsize) < 0) {
173         fprintf(stderr, __FILE__": failed to parse buffer metrics\n");
174         goto fail;
175     }
176     buffer_size = fragsize/frame_size*periods;
177     
178     u = malloc(sizeof(struct userdata));
179     assert(u);
180     memset(u, 0, sizeof(struct userdata));
181     m->userdata = u;
182     
183     if (snd_pcm_open(&u->pcm_handle, dev = pa_modargs_get_value(ma, "device", DEFAULT_DEVICE), SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK) < 0) {
184         fprintf(stderr, __FILE__": Error opening PCM device %s\n", dev);
185         goto fail;
186     }
187
188     if (pa_alsa_set_hw_params(u->pcm_handle, &ss, &periods, &buffer_size) < 0) {
189         fprintf(stderr, __FILE__": Failed to set hardware parameters\n");
190         goto fail;
191     }
192
193     u->sink = pa_sink_new(c, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME), 0, &ss);
194     assert(u->sink);
195
196     u->sink->get_latency = sink_get_latency_cb;
197     u->sink->userdata = u;
198     pa_sink_set_owner(u->sink, m);
199     u->sink->description = pa_sprintf_malloc("Advanced Linux Sound Architecture PCM on '%s'", dev);
200
201     if (pa_create_io_sources(u->pcm_handle, c->mainloop, &u->io_sources, &u->n_io_sources, io_callback, u) < 0) {
202         fprintf(stderr, __FILE__": failed to obtain file descriptors\n");
203         goto fail;
204     }
205     
206     u->frame_size = frame_size;
207     u->fragment_size = buffer_size*u->frame_size/periods;
208
209     fprintf(stderr, __FILE__": using %u fragments of size %u bytes.\n", periods, u->fragment_size);
210
211     u->silence.memblock = pa_memblock_new(u->silence.length = u->fragment_size);
212     assert(u->silence.memblock);
213     pa_silence_memblock(u->silence.memblock, &ss);
214     u->silence.index = 0;
215
216     u->memchunk.memblock = NULL;
217     u->memchunk.index = u->memchunk.length = 0;
218     
219     ret = 0;
220      
221 finish:
222      if (ma)
223          pa_modargs_free(ma);
224     
225     return ret;
226
227 fail:
228     
229     if (u)
230         pa_module_done(c, m);
231
232     goto finish;
233 }
234
235 void pa_module_done(struct pa_core *c, struct pa_module*m) {
236     struct userdata *u;
237     assert(c && m);
238
239     if ((u = m->userdata)) {
240         if (u->sink)
241             pa_sink_free(u->sink);
242
243         if (u->io_sources)
244             pa_free_io_sources(c->mainloop, u->io_sources, u->n_io_sources);
245         
246         if (u->pcm_handle) {
247             snd_pcm_drop(u->pcm_handle);
248             snd_pcm_close(u->pcm_handle);
249         }
250
251         if (u->memchunk.memblock)
252             pa_memblock_unref(u->memchunk.memblock);
253         if (u->silence.memblock)
254             pa_memblock_unref(u->silence.memblock);
255         
256         free(u);
257     }
258 }
259