4 * \brief PCM Copy Plugin Interface
5 * \author Abramo Bagnara <abramo@alsa-project.org>
9 * PCM - Copy conversion
10 * Copyright (c) 2000 by Abramo Bagnara <abramo@alsa-project.org>
13 * This library is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU Lesser General Public License as
15 * published by the Free Software Foundation; either version 2.1 of
16 * the License, or (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License for more details.
23 * You should have received a copy of the GNU Lesser General Public
24 * License along with this library; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 #include "pcm_local.h"
31 #include "pcm_plugin.h"
34 /* entry for static linking */
35 const char *_snd_module_pcm_copy = "";
40 /* This field need to be the first */
41 snd_pcm_plugin_t plug;
45 static int snd_pcm_copy_hw_refine_cprepare(snd_pcm_t *pcm ATTRIBUTE_UNUSED, snd_pcm_hw_params_t *params)
48 snd_pcm_access_mask_t access_mask = { SND_PCM_ACCBIT_SHM };
49 err = _snd_pcm_hw_param_set_mask(params, SND_PCM_HW_PARAM_ACCESS,
53 params->info &= ~(SND_PCM_INFO_MMAP | SND_PCM_INFO_MMAP_VALID);
57 static int snd_pcm_copy_hw_refine_sprepare(snd_pcm_t *pcm ATTRIBUTE_UNUSED, snd_pcm_hw_params_t *sparams)
59 snd_pcm_access_mask_t saccess_mask = { SND_PCM_ACCBIT_MMAP };
60 _snd_pcm_hw_params_any(sparams);
61 _snd_pcm_hw_param_set_mask(sparams, SND_PCM_HW_PARAM_ACCESS,
66 static int snd_pcm_copy_hw_refine_schange(snd_pcm_t *pcm ATTRIBUTE_UNUSED, snd_pcm_hw_params_t *params,
67 snd_pcm_hw_params_t *sparams)
70 unsigned int links = ~SND_PCM_HW_PARBIT_ACCESS;
71 err = _snd_pcm_hw_params_refine(sparams, links, params);
77 static int snd_pcm_copy_hw_refine_cchange(snd_pcm_t *pcm ATTRIBUTE_UNUSED, snd_pcm_hw_params_t *params,
78 snd_pcm_hw_params_t *sparams)
81 unsigned int links = ~SND_PCM_HW_PARBIT_ACCESS;
82 err = _snd_pcm_hw_params_refine(params, links, sparams);
88 static int snd_pcm_copy_hw_refine(snd_pcm_t *pcm, snd_pcm_hw_params_t *params)
90 return snd_pcm_hw_refine_slave(pcm, params,
91 snd_pcm_copy_hw_refine_cprepare,
92 snd_pcm_copy_hw_refine_cchange,
93 snd_pcm_copy_hw_refine_sprepare,
94 snd_pcm_copy_hw_refine_schange,
95 snd_pcm_generic_hw_refine);
98 static int snd_pcm_copy_hw_params(snd_pcm_t *pcm, snd_pcm_hw_params_t *params)
100 return snd_pcm_hw_params_slave(pcm, params,
101 snd_pcm_copy_hw_refine_cchange,
102 snd_pcm_copy_hw_refine_sprepare,
103 snd_pcm_copy_hw_refine_schange,
104 snd_pcm_generic_hw_params);
107 static snd_pcm_uframes_t
108 snd_pcm_copy_write_areas(snd_pcm_t *pcm,
109 const snd_pcm_channel_area_t *areas,
110 snd_pcm_uframes_t offset,
111 snd_pcm_uframes_t size,
112 const snd_pcm_channel_area_t *slave_areas,
113 snd_pcm_uframes_t slave_offset,
114 snd_pcm_uframes_t *slave_sizep)
116 if (size > *slave_sizep)
118 snd_pcm_areas_copy(slave_areas, slave_offset,
120 pcm->channels, size, pcm->format);
125 static snd_pcm_uframes_t
126 snd_pcm_copy_read_areas(snd_pcm_t *pcm,
127 const snd_pcm_channel_area_t *areas,
128 snd_pcm_uframes_t offset,
129 snd_pcm_uframes_t size,
130 const snd_pcm_channel_area_t *slave_areas,
131 snd_pcm_uframes_t slave_offset,
132 snd_pcm_uframes_t *slave_sizep)
134 if (size > *slave_sizep)
136 snd_pcm_areas_copy(areas, offset,
137 slave_areas, slave_offset,
138 pcm->channels, size, pcm->format);
143 static void snd_pcm_copy_dump(snd_pcm_t *pcm, snd_output_t *out)
145 snd_pcm_copy_t *copy = pcm->private_data;
146 snd_output_printf(out, "Copy conversion PCM\n");
148 snd_output_printf(out, "Its setup is:\n");
149 snd_pcm_dump_setup(pcm, out);
151 snd_output_printf(out, "Slave: ");
152 snd_pcm_dump(copy->plug.gen.slave, out);
155 static const snd_pcm_ops_t snd_pcm_copy_ops = {
156 .close = snd_pcm_generic_close,
157 .info = snd_pcm_generic_info,
158 .hw_refine = snd_pcm_copy_hw_refine,
159 .hw_params = snd_pcm_copy_hw_params,
160 .hw_free = snd_pcm_generic_hw_free,
161 .sw_params = snd_pcm_generic_sw_params,
162 .channel_info = snd_pcm_generic_channel_info,
163 .dump = snd_pcm_copy_dump,
164 .nonblock = snd_pcm_generic_nonblock,
165 .async = snd_pcm_generic_async,
166 .mmap = snd_pcm_generic_mmap,
167 .munmap = snd_pcm_generic_munmap,
171 * \brief Creates a new copy PCM
172 * \param pcmp Returns created PCM handle
173 * \param name Name of PCM
174 * \param slave Slave PCM handle
175 * \param close_slave When set, the slave PCM handle is closed with copy PCM
176 * \retval zero on success otherwise a negative error code
177 * \warning Using of this function might be dangerous in the sense
178 * of compatibility reasons. The prototype might be freely
181 int snd_pcm_copy_open(snd_pcm_t **pcmp, const char *name, snd_pcm_t *slave, int close_slave)
184 snd_pcm_copy_t *copy;
186 assert(pcmp && slave);
187 copy = calloc(1, sizeof(snd_pcm_copy_t));
191 snd_pcm_plugin_init(©->plug);
192 copy->plug.read = snd_pcm_copy_read_areas;
193 copy->plug.write = snd_pcm_copy_write_areas;
194 copy->plug.undo_read = snd_pcm_plugin_undo_read_generic;
195 copy->plug.undo_write = snd_pcm_plugin_undo_write_generic;
196 copy->plug.gen.slave = slave;
197 copy->plug.gen.close_slave = close_slave;
199 err = snd_pcm_new(&pcm, SND_PCM_TYPE_COPY, name, slave->stream, slave->mode);
204 pcm->ops = &snd_pcm_copy_ops;
205 pcm->fast_ops = &snd_pcm_plugin_fast_ops;
206 pcm->private_data = copy;
207 pcm->poll_fd = slave->poll_fd;
208 pcm->poll_events = slave->poll_events;
209 pcm->monotonic = slave->monotonic;
210 snd_pcm_set_hw_ptr(pcm, ©->plug.hw_ptr, -1, 0);
211 snd_pcm_set_appl_ptr(pcm, ©->plug.appl_ptr, -1, 0);
217 /*! \page pcm_plugins
219 \section pcm_plugins_copy Plugin: copy
221 This plugin copies samples from master copy PCM to given slave PCM.
222 The channel count, format and rate must match for both of them.
227 slave STR # Slave name
229 slave { # Slave definition
230 pcm STR # Slave PCM name
232 pcm { } # Slave PCM definition
237 \subsection pcm_plugins_copy_funcref Function reference
240 <LI>snd_pcm_copy_open()
241 <LI>_snd_pcm_copy_open()
247 * \brief Creates a new copy PCM
248 * \param pcmp Returns created PCM handle
249 * \param name Name of PCM
250 * \param root Root configuration node
251 * \param conf Configuration node with copy PCM description
252 * \param stream Stream type
253 * \param mode Stream mode
254 * \retval zero on success otherwise a negative error code
255 * \warning Using of this function might be dangerous in the sense
256 * of compatibility reasons. The prototype might be freely
259 int _snd_pcm_copy_open(snd_pcm_t **pcmp, const char *name,
260 snd_config_t *root, snd_config_t *conf,
261 snd_pcm_stream_t stream, int mode)
263 snd_config_iterator_t i, next;
266 snd_config_t *slave = NULL, *sconf;
267 snd_config_for_each(i, next, conf) {
268 snd_config_t *n = snd_config_iterator_entry(i);
270 if (snd_config_get_id(n, &id) < 0)
272 if (snd_pcm_conf_generic_id(id))
274 if (strcmp(id, "slave") == 0) {
278 SNDERR("Unknown field %s", id);
282 SNDERR("slave is not defined");
285 err = snd_pcm_slave_conf(root, slave, &sconf, 0);
288 err = snd_pcm_open_slave(&spcm, root, sconf, stream, mode, conf);
289 snd_config_delete(sconf);
292 err = snd_pcm_copy_open(pcmp, name, spcm, 1);
298 SND_DLSYM_BUILD_VERSION(_snd_pcm_copy_open, SND_PCM_DLSYM_VERSION);