Makefile.in: Remove special treatment of ortp_send/ortp_recv
[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 /*
20 * Based in parts on aplay.c from the alsa-utils-1.0.8 package,
21 * Copyright (c) by Jaroslav Kysela <perex@suse.cz>, which is
22 * based on the vplay program by Michael Beck.
23 */
24
25 #include "para.h"
26 #include "fd.h"
27 #include "string.h"
28 #include "write.h"
29
30 #include <alsa/asoundlib.h>
31
32 #include "write.cmdline.h"
33 #include "error.h"
34
35 extern struct gengetopt_args_info conf;
36
37 #define FORMAT SND_PCM_FORMAT_S16_LE
38 struct private_alsa_data {
39 snd_pcm_t *handle;
40 size_t bytes_per_frame;
41 };
42
43 /*
44 * open and prepare the PCM handle for writing
45 *
46 * Install PCM software and hardware configuration. Exit on errors.
47 */
48 static int alsa_open(struct writer_node *w)
49 {
50 snd_pcm_hw_params_t *hwparams;
51 snd_pcm_sw_params_t *swparams;
52 snd_pcm_uframes_t buffer_size, xfer_align, start_threshold,
53 stop_threshold;
54 unsigned buffer_time = 0;
55 int err;
56 snd_pcm_info_t *info;
57 snd_output_t *log;
58 snd_pcm_uframes_t period_size;
59 struct private_alsa_data *pad = para_malloc(sizeof(struct private_alsa_data));
60 w->private_data = pad;
61
62 snd_pcm_info_alloca(&info);
63 if (snd_output_stdio_attach(&log, stderr, 0) < 0)
64 return -E_ALSA_LOG;
65 err = snd_pcm_open(&pad->handle, conf.device_arg,
66 SND_PCM_STREAM_PLAYBACK, 0);
67 if (err < 0)
68 return -E_PCM_OPEN;
69 if ((err = snd_pcm_info(pad->handle, info)) < 0)
70 return -E_SND_PCM_INFO;
71
72 snd_pcm_hw_params_alloca(&hwparams);
73 snd_pcm_sw_params_alloca(&swparams);
74 if (snd_pcm_hw_params_any(pad->handle, hwparams) < 0)
75 return -E_BROKEN_CONF;
76 if (snd_pcm_hw_params_set_access(pad->handle, hwparams,
77 SND_PCM_ACCESS_RW_INTERLEAVED) < 0)
78 return -E_ACCESS_TYPE;
79 if (snd_pcm_hw_params_set_format(pad->handle, hwparams, FORMAT) < 0)
80 return -E_SAMPLE_FORMAT;
81 if (snd_pcm_hw_params_set_channels(pad->handle, hwparams,
82 conf.channels_arg) < 0)
83 return -E_CHANNEL_COUNT;
84 if (snd_pcm_hw_params_set_rate_near(pad->handle, hwparams,
85 (unsigned int*) &conf.sample_rate_arg, 0) < 0)
86 return -E_SET_RATE;
87 err = snd_pcm_hw_params_get_buffer_time_max(hwparams, &buffer_time, 0);
88 if (err < 0 || !buffer_time)
89 return -E_GET_BUFFER_TIME;
90 PARA_DEBUG_LOG("buffer time: %d\n", buffer_time);
91 if (snd_pcm_hw_params_set_buffer_time_near(pad->handle, hwparams,
92 &buffer_time, 0) < 0)
93 return -E_SET_BUFFER_TIME;
94 if (snd_pcm_hw_params(pad->handle, hwparams) < 0)
95 return -E_HW_PARAMS;
96 snd_pcm_hw_params_get_period_size(hwparams, &period_size, 0);
97 snd_pcm_hw_params_get_buffer_size(hwparams, &buffer_size);
98 PARA_DEBUG_LOG("buffer size: %lu, period_size: %lu\n", buffer_size,
99 period_size);
100 if (period_size == buffer_size)
101 return -E_BAD_PERIOD;
102 snd_pcm_sw_params_current(pad->handle, swparams);
103 err = snd_pcm_sw_params_get_xfer_align(swparams, &xfer_align);
104 if (err < 0 || !xfer_align)
105 return -E_GET_XFER;
106 snd_pcm_sw_params_set_avail_min(pad->handle, swparams, period_size);
107 /* round to closest transfer boundary */
108 start_threshold = (buffer_size / xfer_align) * xfer_align;
109 if (start_threshold < 1)
110 start_threshold = 1;
111 if (snd_pcm_sw_params_set_start_threshold(pad->handle, swparams,
112 start_threshold) < 0)
113 return -E_START_THRESHOLD;
114 stop_threshold = buffer_size;
115 if (snd_pcm_sw_params_set_stop_threshold(pad->handle, swparams,
116 stop_threshold) < 0)
117 return -E_STOP_THRESHOLD;
118 if (snd_pcm_sw_params_set_xfer_align(pad->handle, swparams,
119 xfer_align) < 0)
120 return -E_SET_XFER;
121 if (snd_pcm_sw_params(pad->handle, swparams) < 0)
122 return -E_SW_PARAMS;
123 pad->bytes_per_frame = snd_pcm_format_physical_width(FORMAT)
124 * conf.channels_arg / 8;
125 return period_size * pad->bytes_per_frame;
126 }
127
128 /**
129 * push out pcm frames
130 * \param data pointer do data to be written
131 * \param nbytes number of bytes (not frames)
132 *
133 * \return Number of bytes written, -E_ALSA_WRITE on errors.
134 */
135 static int alsa_write(char *data, size_t nbytes, struct writer_node *wn)
136 {
137 struct private_alsa_data *pad = wn->private_data;
138 size_t frames = nbytes / pad->bytes_per_frame;
139 unsigned char *d = data;
140 snd_pcm_sframes_t r, result = 0;
141
142 while (frames > 0) {
143 /* write interleaved frames */
144 r = snd_pcm_writei(pad->handle, d, frames);
145 if (r < 0)
146 PARA_ERROR_LOG("write error: %s\n", snd_strerror(r));
147 if (r == -EAGAIN || (r >= 0 && r < frames))
148 snd_pcm_wait(pad->handle, 1);
149 else if (r == -EPIPE)
150 snd_pcm_prepare(pad->handle);
151 else if (r < 0)
152 return -E_ALSA_WRITE;
153 if (r > 0) {
154 result += r;
155 frames -= r;
156 d += r * pad->bytes_per_frame;
157 }
158 }
159 return result * pad->bytes_per_frame;
160 }
161
162 static void alsa_close(struct writer_node *wn)
163 {
164 struct private_alsa_data *pad = wn->private_data;
165 snd_pcm_drain(pad->handle);
166 snd_pcm_close(pad->handle);
167 snd_config_update_free_global();
168 free(pad);
169 }
170
171 void alsa_writer_init(struct writer *w)
172 {
173 w->open = alsa_open;
174 w->write = alsa_write;
175 w->close = alsa_close;
176 w->shutdown = NULL; /* nothing to do */
177 }