3 * Copyright (c) 2000 by Abramo Bagnara <abramo@alsa-project.org>
5 * This library is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as
7 * published by the Free Software Foundation; either version 2.1 of
8 * the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include "pcm_local.h"
29 size_t page_size(void)
31 long s = sysconf(_SC_PAGE_SIZE);
36 size_t page_align(size_t size)
39 long psz = page_size();
42 return size + psz - r;
46 size_t page_ptr(size_t object_offset, size_t object_size, size_t *offset, size_t *mmap_offset)
49 long psz = page_size();
52 *mmap_offset = object_offset;
54 *mmap_offset -= object_offset;
55 object_size += object_offset;
56 r = object_size % psz;
58 r = object_size + psz - r;
61 *offset = object_offset;
65 void snd_pcm_mmap_appl_backward(snd_pcm_t *pcm, snd_pcm_uframes_t frames)
67 snd_pcm_sframes_t appl_ptr = *pcm->appl.ptr;
70 appl_ptr += pcm->boundary;
71 *pcm->appl.ptr = appl_ptr;
74 void snd_pcm_mmap_appl_forward(snd_pcm_t *pcm, snd_pcm_uframes_t frames)
76 snd_pcm_uframes_t appl_ptr = *pcm->appl.ptr;
78 if (appl_ptr >= pcm->boundary)
79 appl_ptr -= pcm->boundary;
80 *pcm->appl.ptr = appl_ptr;
83 void snd_pcm_mmap_hw_backward(snd_pcm_t *pcm, snd_pcm_uframes_t frames)
85 snd_pcm_sframes_t hw_ptr = *pcm->hw.ptr;
88 hw_ptr += pcm->boundary;
89 *pcm->hw.ptr = hw_ptr;
92 void snd_pcm_mmap_hw_forward(snd_pcm_t *pcm, snd_pcm_uframes_t frames)
94 snd_pcm_uframes_t hw_ptr = *pcm->hw.ptr;
96 if (hw_ptr >= pcm->boundary)
97 hw_ptr -= pcm->boundary;
98 *pcm->hw.ptr = hw_ptr;
101 static snd_pcm_sframes_t snd_pcm_mmap_write_areas(snd_pcm_t *pcm,
102 const snd_pcm_channel_area_t *areas,
103 snd_pcm_uframes_t offset,
104 snd_pcm_uframes_t size)
106 snd_pcm_uframes_t xfer = 0;
108 if (snd_pcm_mmap_playback_avail(pcm) < size) {
109 SNDMSG("too short avail %ld to size %ld", snd_pcm_mmap_playback_avail(pcm), size);
113 const snd_pcm_channel_area_t *pcm_areas;
114 snd_pcm_uframes_t pcm_offset;
115 snd_pcm_uframes_t frames = size;
116 snd_pcm_sframes_t result;
118 snd_pcm_mmap_begin(pcm, &pcm_areas, &pcm_offset, &frames);
119 snd_pcm_areas_copy(pcm_areas, pcm_offset,
122 frames, pcm->format);
123 result = snd_pcm_mmap_commit(pcm, pcm_offset, frames);
125 return xfer > 0 ? (snd_pcm_sframes_t)xfer : result;
130 return (snd_pcm_sframes_t)xfer;
133 static snd_pcm_sframes_t snd_pcm_mmap_read_areas(snd_pcm_t *pcm,
134 const snd_pcm_channel_area_t *areas,
135 snd_pcm_uframes_t offset,
136 snd_pcm_uframes_t size)
138 snd_pcm_uframes_t xfer = 0;
140 if (snd_pcm_mmap_capture_avail(pcm) < size) {
141 SNDMSG("too short avail %ld to size %ld", snd_pcm_mmap_capture_avail(pcm), size);
145 const snd_pcm_channel_area_t *pcm_areas;
146 snd_pcm_uframes_t pcm_offset;
147 snd_pcm_uframes_t frames = size;
148 snd_pcm_sframes_t result;
150 snd_pcm_mmap_begin(pcm, &pcm_areas, &pcm_offset, &frames);
151 snd_pcm_areas_copy(areas, offset,
152 pcm_areas, pcm_offset,
154 frames, pcm->format);
155 result = snd_pcm_mmap_commit(pcm, pcm_offset, frames);
157 return xfer > 0 ? (snd_pcm_sframes_t)xfer : result;
162 return (snd_pcm_sframes_t)xfer;
166 * \brief Write interleaved frames to a PCM using direct buffer (mmap)
167 * \param pcm PCM handle
168 * \param buffer frames containing buffer
169 * \param size frames to be written
170 * \return a positive number of frames actually written otherwise a
171 * negative error code
172 * \retval -EBADFD PCM is not in the right state (#SND_PCM_STATE_PREPARED or #SND_PCM_STATE_RUNNING)
173 * \retval -EPIPE an underrun occurred
174 * \retval -ESTRPIPE a suspend event occurred (stream is suspended and waiting for an application recovery)
176 * If the blocking behaviour is selected, then routine waits until
177 * all requested bytes are played or put to the playback ring buffer.
178 * The count of bytes can be less only if a signal or underrun occurred.
180 * If the non-blocking behaviour is selected, then routine doesn't wait at all.
182 snd_pcm_sframes_t snd_pcm_mmap_writei(snd_pcm_t *pcm, const void *buffer, snd_pcm_uframes_t size)
184 snd_pcm_channel_area_t areas[pcm->channels];
185 snd_pcm_areas_from_buf(pcm, areas, (void*)buffer);
186 return snd_pcm_write_areas(pcm, areas, 0, size,
187 snd_pcm_mmap_write_areas);
191 * \brief Write non interleaved frames to a PCM using direct buffer (mmap)
192 * \param pcm PCM handle
193 * \param bufs frames containing buffers (one for each channel)
194 * \param size frames to be written
195 * \return a positive number of frames actually written otherwise a
196 * negative error code
197 * \retval -EBADFD PCM is not in the right state (#SND_PCM_STATE_PREPARED or #SND_PCM_STATE_RUNNING)
198 * \retval -EPIPE an underrun occurred
199 * \retval -ESTRPIPE a suspend event occurred (stream is suspended and waiting for an application recovery)
201 * If the blocking behaviour is selected, then routine waits until
202 * all requested bytes are played or put to the playback ring buffer.
203 * The count of bytes can be less only if a signal or underrun occurred.
205 * If the non-blocking behaviour is selected, then routine doesn't wait at all.
207 snd_pcm_sframes_t snd_pcm_mmap_writen(snd_pcm_t *pcm, void **bufs, snd_pcm_uframes_t size)
209 snd_pcm_channel_area_t areas[pcm->channels];
210 snd_pcm_areas_from_bufs(pcm, areas, bufs);
211 return snd_pcm_write_areas(pcm, areas, 0, size,
212 snd_pcm_mmap_write_areas);
216 * \brief Read interleaved frames from a PCM using direct buffer (mmap)
217 * \param pcm PCM handle
218 * \param buffer frames containing buffer
219 * \param size frames to be written
220 * \return a positive number of frames actually read otherwise a
221 * negative error code
222 * \retval -EBADFD PCM is not in the right state (#SND_PCM_STATE_PREPARED or #SND_PCM_STATE_RUNNING)
223 * \retval -EPIPE an overrun occurred
224 * \retval -ESTRPIPE a suspend event occurred (stream is suspended and waiting for an application recovery)
226 * If the blocking behaviour was selected, then routine waits until
227 * all requested bytes are filled. The count of bytes can be less only
228 * if a signal or underrun occurred.
230 * If the non-blocking behaviour is selected, then routine doesn't wait at all.
232 snd_pcm_sframes_t snd_pcm_mmap_readi(snd_pcm_t *pcm, void *buffer, snd_pcm_uframes_t size)
234 snd_pcm_channel_area_t areas[pcm->channels];
235 snd_pcm_areas_from_buf(pcm, areas, buffer);
236 return snd_pcm_read_areas(pcm, areas, 0, size,
237 snd_pcm_mmap_read_areas);
241 * \brief Read non interleaved frames to a PCM using direct buffer (mmap)
242 * \param pcm PCM handle
243 * \param bufs frames containing buffers (one for each channel)
244 * \param size frames to be written
245 * \return a positive number of frames actually read otherwise a
246 * negative error code
247 * \retval -EBADFD PCM is not in the right state (#SND_PCM_STATE_PREPARED or #SND_PCM_STATE_RUNNING)
248 * \retval -EPIPE an overrun occurred
249 * \retval -ESTRPIPE a suspend event occurred (stream is suspended and waiting for an application recovery)
251 * If the blocking behaviour was selected, then routine waits until
252 * all requested bytes are filled. The count of bytes can be less only
253 * if a signal or underrun occurred.
255 * If the non-blocking behaviour is selected, then routine doesn't wait at all.
257 snd_pcm_sframes_t snd_pcm_mmap_readn(snd_pcm_t *pcm, void **bufs, snd_pcm_uframes_t size)
259 snd_pcm_channel_area_t areas[pcm->channels];
260 snd_pcm_areas_from_bufs(pcm, areas, bufs);
261 return snd_pcm_read_areas(pcm, areas, 0, size,
262 snd_pcm_mmap_read_areas);
265 int snd_pcm_channel_info_shm(snd_pcm_t *pcm, snd_pcm_channel_info_t *info, int shmid)
267 switch (pcm->access) {
268 case SND_PCM_ACCESS_MMAP_INTERLEAVED:
269 case SND_PCM_ACCESS_RW_INTERLEAVED:
270 info->first = info->channel * pcm->sample_bits;
271 info->step = pcm->frame_bits;
273 case SND_PCM_ACCESS_MMAP_NONINTERLEAVED:
274 case SND_PCM_ACCESS_RW_NONINTERLEAVED:
276 info->step = pcm->sample_bits;
279 SNDMSG("invalid access type %d", pcm->access);
283 if (pcm->hw_flags & SND_PCM_HW_PARAMS_EXPORT_BUFFER) {
284 info->type = SND_PCM_AREA_SHM;
285 info->u.shm.shmid = shmid;
286 info->u.shm.area = NULL;
288 info->type = SND_PCM_AREA_LOCAL;
292 int snd_pcm_mmap(snd_pcm_t *pcm)
297 if (CHECK_SANITY(! pcm->setup)) {
298 SNDMSG("PCM not set up");
301 if (CHECK_SANITY(pcm->mmap_channels || pcm->running_areas)) {
302 SNDMSG("Already mmapped");
305 err = pcm->ops->mmap(pcm);
308 if (pcm->mmap_shadow)
310 pcm->mmap_channels = calloc(pcm->channels, sizeof(pcm->mmap_channels[0]));
311 if (!pcm->mmap_channels)
313 pcm->running_areas = calloc(pcm->channels, sizeof(pcm->running_areas[0]));
314 if (!pcm->running_areas) {
315 free(pcm->mmap_channels);
316 pcm->mmap_channels = NULL;
319 for (c = 0; c < pcm->channels; ++c) {
320 snd_pcm_channel_info_t *i = &pcm->mmap_channels[c];
322 err = snd_pcm_channel_info(pcm, i);
324 free(pcm->mmap_channels);
325 free(pcm->running_areas);
326 pcm->mmap_channels = NULL;
327 pcm->running_areas = NULL;
331 for (c = 0; c < pcm->channels; ++c) {
332 snd_pcm_channel_info_t *i = &pcm->mmap_channels[c];
333 snd_pcm_channel_area_t *a = &pcm->running_areas[c];
343 size = i->first + i->step * (pcm->buffer_size - 1) + pcm->sample_bits;
344 for (c1 = c + 1; c1 < pcm->channels; ++c1) {
345 snd_pcm_channel_info_t *i1 = &pcm->mmap_channels[c1];
347 if (i1->type != i->type)
350 case SND_PCM_AREA_MMAP:
351 if (i1->u.mmap.fd != i->u.mmap.fd ||
352 i1->u.mmap.offset != i->u.mmap.offset)
355 case SND_PCM_AREA_SHM:
356 if (i1->u.shm.shmid != i->u.shm.shmid)
359 case SND_PCM_AREA_LOCAL:
364 s = i1->first + i1->step * (pcm->buffer_size - 1) + pcm->sample_bits;
368 size = (size + 7) / 8;
369 size = page_align(size);
371 case SND_PCM_AREA_MMAP:
372 ptr = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_FILE|MAP_SHARED, i->u.mmap.fd, i->u.mmap.offset);
373 if (ptr == MAP_FAILED) {
374 SYSERR("mmap failed");
379 case SND_PCM_AREA_SHM:
380 if (i->u.shm.shmid < 0) {
382 /* FIXME: safer permission? */
383 id = shmget(IPC_PRIVATE, size, 0666);
385 SYSERR("shmget failed");
389 ptr = shmat(i->u.shm.shmid, 0, 0);
390 if (ptr == (void *) -1) {
391 SYSERR("shmat failed");
394 /* automatically remove segment if not used */
395 if (shmctl(id, IPC_RMID, NULL) < 0){
396 SYSERR("shmctl mark remove failed");
399 i->u.shm.area = snd_shm_area_create(id, ptr);
400 if (i->u.shm.area == NULL) {
401 SYSERR("snd_shm_area_create failed");
404 if (pcm->access == SND_PCM_ACCESS_MMAP_INTERLEAVED ||
405 pcm->access == SND_PCM_ACCESS_RW_INTERLEAVED) {
407 for (c1 = c + 1; c1 < pcm->channels; c1++) {
408 snd_pcm_channel_info_t *i1 = &pcm->mmap_channels[c1];
409 if (i1->u.shm.shmid < 0) {
410 i1->u.shm.shmid = id;
411 i1->u.shm.area = snd_shm_area_share(i->u.shm.area);
416 ptr = shmat(i->u.shm.shmid, 0, 0);
417 if (ptr == (void*) -1) {
418 SYSERR("shmat failed");
424 case SND_PCM_AREA_LOCAL:
427 SYSERR("malloc failed");
435 for (c1 = c + 1; c1 < pcm->channels; ++c1) {
436 snd_pcm_channel_info_t *i1 = &pcm->mmap_channels[c1];
437 if (i1->type != i->type)
440 case SND_PCM_AREA_MMAP:
441 if (i1->u.mmap.fd != i->u.mmap.fd ||
442 i1->u.mmap.offset != i->u.mmap.offset)
445 case SND_PCM_AREA_SHM:
446 if (i1->u.shm.shmid != i->u.shm.shmid)
449 case SND_PCM_AREA_LOCAL:
450 if (pcm->access != SND_PCM_ACCESS_MMAP_INTERLEAVED &&
451 pcm->access != SND_PCM_ACCESS_RW_INTERLEAVED)
466 int snd_pcm_munmap(snd_pcm_t *pcm)
471 if (CHECK_SANITY(! pcm->mmap_channels)) {
472 SNDMSG("Not mmapped");
475 if (pcm->mmap_shadow)
476 return pcm->ops->munmap(pcm);
477 for (c = 0; c < pcm->channels; ++c) {
478 snd_pcm_channel_info_t *i = &pcm->mmap_channels[c];
480 size_t size = i->first + i->step * (pcm->buffer_size - 1) + pcm->sample_bits;
483 for (c1 = c + 1; c1 < pcm->channels; ++c1) {
484 snd_pcm_channel_info_t *i1 = &pcm->mmap_channels[c1];
486 if (i1->addr != i->addr)
489 s = i1->first + i1->step * (pcm->buffer_size - 1) + pcm->sample_bits;
493 size = (size + 7) / 8;
494 size = page_align(size);
496 case SND_PCM_AREA_MMAP:
497 err = munmap(i->addr, size);
499 SYSERR("mmap failed");
504 case SND_PCM_AREA_SHM:
506 snd_shm_area_destroy(i->u.shm.area);
507 i->u.shm.area = NULL;
508 if (pcm->access == SND_PCM_ACCESS_MMAP_INTERLEAVED ||
509 pcm->access == SND_PCM_ACCESS_RW_INTERLEAVED) {
511 for (c1 = c + 1; c1 < pcm->channels; c1++) {
512 snd_pcm_channel_info_t *i1 = &pcm->mmap_channels[c1];
513 if (i1->u.shm.area) {
514 snd_shm_area_destroy(i1->u.shm.area);
515 i1->u.shm.area = NULL;
521 case SND_PCM_AREA_LOCAL:
529 err = pcm->ops->munmap(pcm);
532 free(pcm->mmap_channels);
533 free(pcm->running_areas);
534 pcm->mmap_channels = NULL;
535 pcm->running_areas = NULL;
539 snd_pcm_sframes_t snd_pcm_write_mmap(snd_pcm_t *pcm, snd_pcm_uframes_t offset,
540 snd_pcm_uframes_t size)
542 snd_pcm_uframes_t xfer = 0;
543 snd_pcm_sframes_t err = 0;
546 while (xfer < size) {
547 snd_pcm_uframes_t frames = size - xfer;
548 snd_pcm_uframes_t cont = pcm->buffer_size - offset;
551 switch (pcm->access) {
552 case SND_PCM_ACCESS_MMAP_INTERLEAVED:
554 const snd_pcm_channel_area_t *a = snd_pcm_mmap_areas(pcm);
555 const char *buf = snd_pcm_channel_area_addr(a, offset);
556 err = _snd_pcm_writei(pcm, buf, frames);
561 case SND_PCM_ACCESS_MMAP_NONINTERLEAVED:
563 unsigned int channels = pcm->channels;
565 void *bufs[channels];
566 const snd_pcm_channel_area_t *areas = snd_pcm_mmap_areas(pcm);
567 for (c = 0; c < channels; ++c) {
568 const snd_pcm_channel_area_t *a = &areas[c];
569 bufs[c] = snd_pcm_channel_area_addr(a, offset);
571 err = _snd_pcm_writen(pcm, bufs, frames);
577 SNDMSG("invalid access type %d", pcm->access);
583 offset = (offset + frames) % pcm->buffer_size;
590 snd_pcm_sframes_t snd_pcm_read_mmap(snd_pcm_t *pcm, snd_pcm_uframes_t offset,
591 snd_pcm_uframes_t size)
593 snd_pcm_uframes_t xfer = 0;
594 snd_pcm_sframes_t err = 0;
597 while (xfer < size) {
598 snd_pcm_uframes_t frames = size - xfer;
599 snd_pcm_uframes_t cont = pcm->buffer_size - offset;
602 switch (pcm->access) {
603 case SND_PCM_ACCESS_MMAP_INTERLEAVED:
605 const snd_pcm_channel_area_t *a = snd_pcm_mmap_areas(pcm);
606 char *buf = snd_pcm_channel_area_addr(a, offset);
607 err = _snd_pcm_readi(pcm, buf, frames);
612 case SND_PCM_ACCESS_MMAP_NONINTERLEAVED:
614 snd_pcm_uframes_t channels = pcm->channels;
616 void *bufs[channels];
617 const snd_pcm_channel_area_t *areas = snd_pcm_mmap_areas(pcm);
618 for (c = 0; c < channels; ++c) {
619 const snd_pcm_channel_area_t *a = &areas[c];
620 bufs[c] = snd_pcm_channel_area_addr(a, offset);
622 err = _snd_pcm_readn(pcm->fast_op_arg, bufs, frames);
627 SNDMSG("invalid access type %d", pcm->access);
633 offset = (offset + frames) % pcm->buffer_size;