first version of the universal paraslash scheduler
[paraslash.git] / alsa_writer.c
1 /*
2 * Copyright (C) 2005-2006 Andre Noll <maan@systemlinux.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
17 */
18
19 /** \file alsa_writer.c paraslash's alsa output plugin */
20
21 /*
22 * Based in parts on aplay.c from the alsa-utils-1.0.8 package,
23 * Copyright (c) by Jaroslav Kysela <perex@suse.cz>, which is
24 * based on the vplay program by Michael Beck.
25 */
26
27 #include "para.h"
28 #include "fd.h"
29 #include "string.h"
30 #include "list.h"
31 #include "sched.h"
32 #include "write.h"
33
34 #include <alsa/asoundlib.h>
35
36 #include "write.cmdline.h"
37 #include "error.h"
38
39 extern struct gengetopt_args_info conf;
40
41 #define FORMAT SND_PCM_FORMAT_S16_LE
42
43 /** data specific to the alsa writer */
44 struct private_alsa_data {
45 /** the alsa handle */
46 snd_pcm_t *handle;
47 /** determined and set by alsa_open() */
48 size_t bytes_per_frame;
49 struct timeval next_chunk;
50 };
51
52 /*
53 * open and prepare the PCM handle for writing
54 *
55 * Install PCM software and hardware configuration. Exit on errors.
56 */
57 static int alsa_open(struct writer_node *w)
58 {
59 snd_pcm_hw_params_t *hwparams;
60 snd_pcm_sw_params_t *swparams;
61 snd_pcm_uframes_t buffer_size, xfer_align, start_threshold,
62 stop_threshold;
63 unsigned buffer_time = 0;
64 int err;
65 snd_pcm_info_t *info;
66 snd_pcm_uframes_t period_size;
67 struct private_alsa_data *pad = para_calloc(sizeof(struct private_alsa_data));
68 w->private_data = pad;
69
70 snd_pcm_info_alloca(&info);
71 err = snd_pcm_open(&pad->handle, conf.device_arg,
72 SND_PCM_STREAM_PLAYBACK, 0);
73 if (err < 0)
74 return -E_PCM_OPEN;
75 if ((err = snd_pcm_info(pad->handle, info)) < 0)
76 return -E_SND_PCM_INFO;
77
78 snd_pcm_hw_params_alloca(&hwparams);
79 snd_pcm_sw_params_alloca(&swparams);
80 if (snd_pcm_hw_params_any(pad->handle, hwparams) < 0)
81 return -E_BROKEN_CONF;
82 if (snd_pcm_hw_params_set_access(pad->handle, hwparams,
83 SND_PCM_ACCESS_RW_INTERLEAVED) < 0)
84 return -E_ACCESS_TYPE;
85 if (snd_pcm_hw_params_set_format(pad->handle, hwparams, FORMAT) < 0)
86 return -E_SAMPLE_FORMAT;
87 if (snd_pcm_hw_params_set_channels(pad->handle, hwparams,
88 conf.channels_arg) < 0)
89 return -E_CHANNEL_COUNT;
90 if (snd_pcm_hw_params_set_rate_near(pad->handle, hwparams,
91 (unsigned int*) &conf.sample_rate_arg, 0) < 0)
92 return -E_SET_RATE;
93 err = snd_pcm_hw_params_get_buffer_time_max(hwparams, &buffer_time, 0);
94 if (err < 0 || !buffer_time)
95 return -E_GET_BUFFER_TIME;
96 PARA_DEBUG_LOG("buffer time: %d\n", buffer_time);
97 if (snd_pcm_hw_params_set_buffer_time_near(pad->handle, hwparams,
98 &buffer_time, 0) < 0)
99 return -E_SET_BUFFER_TIME;
100 if (snd_pcm_hw_params(pad->handle, hwparams) < 0)
101 return -E_HW_PARAMS;
102 snd_pcm_hw_params_get_period_size(hwparams, &period_size, 0);
103 snd_pcm_hw_params_get_buffer_size(hwparams, &buffer_size);
104 PARA_DEBUG_LOG("buffer size: %lu, period_size: %lu\n", buffer_size,
105 period_size);
106 if (period_size == buffer_size)
107 return -E_BAD_PERIOD;
108 snd_pcm_sw_params_current(pad->handle, swparams);
109 err = snd_pcm_sw_params_get_xfer_align(swparams, &xfer_align);
110 if (err < 0 || !xfer_align)
111 return -E_GET_XFER;
112 snd_pcm_sw_params_set_avail_min(pad->handle, swparams, period_size);
113 /* round to closest transfer boundary */
114 start_threshold = (buffer_size / xfer_align) * xfer_align;
115 if (start_threshold < 1)
116 start_threshold = 1;
117 if (snd_pcm_sw_params_set_start_threshold(pad->handle, swparams,
118 start_threshold) < 0)
119 return -E_START_THRESHOLD;
120 stop_threshold = buffer_size;
121 if (snd_pcm_sw_params_set_stop_threshold(pad->handle, swparams,
122 stop_threshold) < 0)
123 return -E_STOP_THRESHOLD;
124 if (snd_pcm_sw_params_set_xfer_align(pad->handle, swparams,
125 xfer_align) < 0)
126 return -E_SET_XFER;
127 if (snd_pcm_sw_params(pad->handle, swparams) < 0)
128 return -E_SW_PARAMS;
129 pad->bytes_per_frame = snd_pcm_format_physical_width(FORMAT)
130 * conf.channels_arg / 8;
131 // if (snd_pcm_nonblock(pad->handle, 1))
132 // PARA_ERROR_LOG("%s\n", "failed to set nonblock mode");
133 return period_size * pad->bytes_per_frame;
134 }
135 static void alsa_write_pre_select(struct sched *s, struct task *t)
136 {
137 struct writer_node *wn = t->private_data;
138 struct private_alsa_data *pad = wn->private_data;
139 struct writer_node_group *wng = wn->wng;
140 struct timeval diff;
141
142 t->ret = 0;
143 if (wng->eof && *wng->loaded < pad->bytes_per_frame)
144 return;
145 t->ret = 1;
146 if (*wng->loaded < pad->bytes_per_frame)
147 return;
148 if (tv_diff(&s->now, &pad->next_chunk, &diff) < 0) {
149 if (tv_diff(&s->timeout, &diff, NULL) < 0)
150 s->timeout = diff;
151 } else {
152 s->timeout.tv_sec = 0;
153 s->timeout.tv_usec = 0;
154 }
155 }
156
157 static void alsa_write_post_select(struct sched *s, struct task *t)
158 {
159 struct writer_node *wn = t->private_data;
160 struct private_alsa_data *pad = wn->private_data;
161 struct writer_node_group *wng = wn->wng;
162 size_t frames = *wng->loaded / pad->bytes_per_frame;
163 snd_pcm_sframes_t ret, result = 0;
164 unsigned char *data = (unsigned char*)wng->buf;
165
166 t->ret = 0;
167 if (!frames)
168 return;
169 if (tv_diff(&s->now, &pad->next_chunk, NULL) < 0)
170 return;
171 // PARA_INFO_LOG("%zd frames\n", frames);
172 // while (frames > 0) {
173 ret = snd_pcm_writei(pad->handle, data, frames);
174 if (ret == -EAGAIN || (ret >= 0 && ret < frames)) {
175 struct timeval tv = {0, 1000 * 10};
176 PARA_INFO_LOG("EAGAIN. frames: %d, ret: %lu\n", frames, ret);
177 tv_add(&s->now, &tv, &pad->next_chunk);
178 // snd_pcm_wait(pad->handle, 1);
179 } else if (ret == -EPIPE) {
180 PARA_INFO_LOG("%s", "EPIPE\n");
181 snd_pcm_prepare(pad->handle);
182 } else if (ret < 0) {
183 PARA_INFO_LOG("ALSA ERR %d\n", frames);
184 t->ret = -E_ALSA_WRITE;
185 return;
186 }
187 if (ret >= 0) {
188 result += ret;
189 frames -= ret;
190 data += ret * pad->bytes_per_frame;
191 }
192 // if (ret == -EAGAIN)
193 // break;
194 // }
195 t->ret = result * pad->bytes_per_frame;
196 // PARA_INFO_LOG("ret: %d, frames: %zd\n", t->ret, frames);
197 }
198
199 static void alsa_close(struct writer_node *wn)
200 {
201 struct private_alsa_data *pad = wn->private_data;
202 snd_pcm_drain(pad->handle);
203 snd_pcm_close(pad->handle);
204 snd_config_update_free_global();
205 free(pad);
206 }
207
208 /** the init function of the alsa writer */
209 void alsa_writer_init(struct writer *w)
210 {
211 w->open = alsa_open;
212 w->close = alsa_close;
213 w->pre_select = alsa_write_pre_select;
214 w->post_select = alsa_write_post_select;
215 w->shutdown = NULL; /* nothing to do */
216 }