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,
168 .query_chmaps = snd_pcm_generic_query_chmaps,
169 .get_chmap = snd_pcm_generic_get_chmap,
170 .set_chmap = snd_pcm_generic_set_chmap,
174 * \brief Creates a new copy PCM
175 * \param pcmp Returns created PCM handle
176 * \param name Name of PCM
177 * \param slave Slave PCM handle
178 * \param close_slave When set, the slave PCM handle is closed with copy PCM
179 * \retval zero on success otherwise a negative error code
180 * \warning Using of this function might be dangerous in the sense
181 * of compatibility reasons. The prototype might be freely
184 int snd_pcm_copy_open(snd_pcm_t **pcmp, const char *name, snd_pcm_t *slave, int close_slave)
187 snd_pcm_copy_t *copy;
189 assert(pcmp && slave);
190 copy = calloc(1, sizeof(snd_pcm_copy_t));
194 snd_pcm_plugin_init(©->plug);
195 copy->plug.read = snd_pcm_copy_read_areas;
196 copy->plug.write = snd_pcm_copy_write_areas;
197 copy->plug.undo_read = snd_pcm_plugin_undo_read_generic;
198 copy->plug.undo_write = snd_pcm_plugin_undo_write_generic;
199 copy->plug.gen.slave = slave;
200 copy->plug.gen.close_slave = close_slave;
202 err = snd_pcm_new(&pcm, SND_PCM_TYPE_COPY, name, slave->stream, slave->mode);
207 pcm->ops = &snd_pcm_copy_ops;
208 pcm->fast_ops = &snd_pcm_plugin_fast_ops;
209 pcm->private_data = copy;
210 pcm->poll_fd = slave->poll_fd;
211 pcm->poll_events = slave->poll_events;
212 pcm->tstamp_type = slave->tstamp_type;
213 snd_pcm_set_hw_ptr(pcm, ©->plug.hw_ptr, -1, 0);
214 snd_pcm_set_appl_ptr(pcm, ©->plug.appl_ptr, -1, 0);
220 /*! \page pcm_plugins
222 \section pcm_plugins_copy Plugin: copy
224 This plugin copies samples from master copy PCM to given slave PCM.
225 The channel count, format and rate must match for both of them.
230 slave STR # Slave name
232 slave { # Slave definition
233 pcm STR # Slave PCM name
235 pcm { } # Slave PCM definition
240 \subsection pcm_plugins_copy_funcref Function reference
243 <LI>snd_pcm_copy_open()
244 <LI>_snd_pcm_copy_open()
250 * \brief Creates a new copy PCM
251 * \param pcmp Returns created PCM handle
252 * \param name Name of PCM
253 * \param root Root configuration node
254 * \param conf Configuration node with copy PCM description
255 * \param stream Stream type
256 * \param mode Stream mode
257 * \retval zero on success otherwise a negative error code
258 * \warning Using of this function might be dangerous in the sense
259 * of compatibility reasons. The prototype might be freely
262 int _snd_pcm_copy_open(snd_pcm_t **pcmp, const char *name,
263 snd_config_t *root, snd_config_t *conf,
264 snd_pcm_stream_t stream, int mode)
266 snd_config_iterator_t i, next;
269 snd_config_t *slave = NULL, *sconf;
270 snd_config_for_each(i, next, conf) {
271 snd_config_t *n = snd_config_iterator_entry(i);
273 if (snd_config_get_id(n, &id) < 0)
275 if (snd_pcm_conf_generic_id(id))
277 if (strcmp(id, "slave") == 0) {
281 SNDERR("Unknown field %s", id);
285 SNDERR("slave is not defined");
288 err = snd_pcm_slave_conf(root, slave, &sconf, 0);
291 err = snd_pcm_open_slave(&spcm, root, sconf, stream, mode, conf);
292 snd_config_delete(sconf);
295 err = snd_pcm_copy_open(pcmp, name, spcm, 1);
301 SND_DLSYM_BUILD_VERSION(_snd_pcm_copy_open, SND_PCM_DLSYM_VERSION);