2000-05-08 18:53:38 +00:00
|
|
|
/*
|
|
|
|
|
* PCM Interface - mmap
|
|
|
|
|
* Copyright (c) 2000 by Abramo Bagnara <abramo@alsa-project.org>
|
|
|
|
|
*
|
|
|
|
|
* This library is free software; you can redistribute it and/or modify
|
2001-12-30 09:22:54 +00:00
|
|
|
* it under the terms of the GNU Lesser General Public License as
|
|
|
|
|
* published by the Free Software Foundation; either version 2.1 of
|
2000-05-08 18:53:38 +00:00
|
|
|
* the License, or (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2001-12-30 09:22:54 +00:00
|
|
|
* GNU Lesser General Public License for more details.
|
2000-05-08 18:53:38 +00:00
|
|
|
*
|
2001-12-30 09:22:54 +00:00
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2000-05-08 18:53:38 +00:00
|
|
|
* License along with this library; if not, write to the Free Software
|
2001-12-30 09:22:54 +00:00
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
2000-05-08 18:53:38 +00:00
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <malloc.h>
|
2000-08-04 10:24:12 +00:00
|
|
|
#include <string.h>
|
2000-05-08 18:53:38 +00:00
|
|
|
#include <sys/poll.h>
|
2000-11-20 20:10:46 +00:00
|
|
|
#include <sys/mman.h>
|
|
|
|
|
#include <sys/shm.h>
|
2000-05-08 18:53:38 +00:00
|
|
|
#include "pcm_local.h"
|
|
|
|
|
|
2001-02-21 21:59:35 +00:00
|
|
|
size_t page_size(void)
|
|
|
|
|
{
|
|
|
|
|
long s = sysconf(_SC_PAGE_SIZE);
|
|
|
|
|
assert(s > 0);
|
|
|
|
|
return s;
|
|
|
|
|
}
|
2000-11-20 20:10:46 +00:00
|
|
|
|
2001-02-21 21:59:35 +00:00
|
|
|
size_t page_align(size_t size)
|
|
|
|
|
{
|
|
|
|
|
size_t r;
|
2002-04-23 15:51:29 +00:00
|
|
|
long psz = page_size();
|
2001-02-21 21:59:35 +00:00
|
|
|
r = size % psz;
|
|
|
|
|
if (r)
|
|
|
|
|
return size + psz - r;
|
|
|
|
|
return size;
|
|
|
|
|
}
|
2000-11-20 20:10:46 +00:00
|
|
|
|
2002-04-23 15:51:29 +00:00
|
|
|
size_t page_ptr(size_t object_offset, size_t object_size, size_t *offset, size_t *mmap_offset)
|
|
|
|
|
{
|
|
|
|
|
size_t r;
|
|
|
|
|
long psz = page_size();
|
|
|
|
|
assert(offset);
|
|
|
|
|
assert(mmap_offset);
|
|
|
|
|
*mmap_offset = object_offset;
|
|
|
|
|
object_offset %= psz;
|
|
|
|
|
*mmap_offset -= object_offset;
|
|
|
|
|
object_size += object_offset;
|
|
|
|
|
r = object_size % psz;
|
|
|
|
|
if (r)
|
|
|
|
|
r = object_size + psz - r;
|
|
|
|
|
else
|
|
|
|
|
r = object_size;
|
|
|
|
|
*offset = object_offset;
|
|
|
|
|
return r;
|
|
|
|
|
}
|
|
|
|
|
|
2001-01-15 11:06:53 +00:00
|
|
|
void snd_pcm_mmap_appl_backward(snd_pcm_t *pcm, snd_pcm_uframes_t frames)
|
2000-09-26 09:46:05 +00:00
|
|
|
{
|
2002-04-23 15:51:29 +00:00
|
|
|
snd_pcm_sframes_t appl_ptr = *pcm->appl.ptr;
|
2000-09-26 09:46:05 +00:00
|
|
|
appl_ptr -= frames;
|
|
|
|
|
if (appl_ptr < 0)
|
2000-11-20 20:10:46 +00:00
|
|
|
appl_ptr += pcm->boundary;
|
2002-04-23 15:51:29 +00:00
|
|
|
*pcm->appl.ptr = appl_ptr;
|
2000-09-26 09:46:05 +00:00
|
|
|
}
|
|
|
|
|
|
2001-01-15 11:06:53 +00:00
|
|
|
void snd_pcm_mmap_appl_forward(snd_pcm_t *pcm, snd_pcm_uframes_t frames)
|
2000-05-08 18:53:38 +00:00
|
|
|
{
|
2002-04-23 15:51:29 +00:00
|
|
|
snd_pcm_uframes_t appl_ptr = *pcm->appl.ptr;
|
2000-09-24 09:57:26 +00:00
|
|
|
appl_ptr += frames;
|
2000-11-20 20:10:46 +00:00
|
|
|
if (appl_ptr >= pcm->boundary)
|
|
|
|
|
appl_ptr -= pcm->boundary;
|
2002-04-23 15:51:29 +00:00
|
|
|
*pcm->appl.ptr = appl_ptr;
|
2000-10-05 10:26:07 +00:00
|
|
|
}
|
|
|
|
|
|
2001-01-15 11:06:53 +00:00
|
|
|
void snd_pcm_mmap_hw_backward(snd_pcm_t *pcm, snd_pcm_uframes_t frames)
|
2000-10-05 10:26:07 +00:00
|
|
|
{
|
2002-04-23 15:51:29 +00:00
|
|
|
snd_pcm_sframes_t hw_ptr = *pcm->hw.ptr;
|
2000-10-05 10:26:07 +00:00
|
|
|
hw_ptr -= frames;
|
|
|
|
|
if (hw_ptr < 0)
|
2000-11-20 20:10:46 +00:00
|
|
|
hw_ptr += pcm->boundary;
|
2002-04-23 15:51:29 +00:00
|
|
|
*pcm->hw.ptr = hw_ptr;
|
2000-05-08 18:53:38 +00:00
|
|
|
}
|
|
|
|
|
|
2001-01-15 11:06:53 +00:00
|
|
|
void snd_pcm_mmap_hw_forward(snd_pcm_t *pcm, snd_pcm_uframes_t frames)
|
2000-05-08 18:53:38 +00:00
|
|
|
{
|
2002-04-23 15:51:29 +00:00
|
|
|
snd_pcm_uframes_t hw_ptr = *pcm->hw.ptr;
|
2000-09-24 09:57:26 +00:00
|
|
|
hw_ptr += frames;
|
2000-11-20 20:10:46 +00:00
|
|
|
if (hw_ptr >= pcm->boundary)
|
|
|
|
|
hw_ptr -= pcm->boundary;
|
2002-04-23 15:51:29 +00:00
|
|
|
*pcm->hw.ptr = hw_ptr;
|
2000-05-08 18:53:38 +00:00
|
|
|
}
|
|
|
|
|
|
2001-12-09 12:32:42 +00:00
|
|
|
static snd_pcm_sframes_t snd_pcm_mmap_write_areas(snd_pcm_t *pcm,
|
2001-03-29 17:50:28 +00:00
|
|
|
const snd_pcm_channel_area_t *areas,
|
|
|
|
|
snd_pcm_uframes_t offset,
|
|
|
|
|
snd_pcm_uframes_t size)
|
2001-02-27 13:42:12 +00:00
|
|
|
{
|
2001-12-09 12:32:42 +00:00
|
|
|
snd_pcm_uframes_t xfer = 0;
|
|
|
|
|
|
2001-05-14 15:21:18 +00:00
|
|
|
assert(snd_pcm_mmap_playback_avail(pcm) >= size);
|
2001-02-27 13:42:12 +00:00
|
|
|
while (size > 0) {
|
2001-04-13 15:40:53 +00:00
|
|
|
const snd_pcm_channel_area_t *pcm_areas;
|
|
|
|
|
snd_pcm_uframes_t pcm_offset;
|
2001-02-27 13:42:12 +00:00
|
|
|
snd_pcm_uframes_t frames = size;
|
2002-02-21 15:01:34 +00:00
|
|
|
snd_pcm_sframes_t result;
|
|
|
|
|
|
2001-04-13 15:40:53 +00:00
|
|
|
snd_pcm_mmap_begin(pcm, &pcm_areas, &pcm_offset, &frames);
|
2001-05-14 15:21:18 +00:00
|
|
|
snd_pcm_areas_copy(pcm_areas, pcm_offset,
|
|
|
|
|
areas, offset,
|
|
|
|
|
pcm->channels,
|
|
|
|
|
frames, pcm->format);
|
2002-02-21 15:01:34 +00:00
|
|
|
result = snd_pcm_mmap_commit(pcm, pcm_offset, frames);
|
|
|
|
|
if (result < 0)
|
|
|
|
|
return xfer > 0 ? xfer : result;
|
|
|
|
|
offset += result;
|
|
|
|
|
xfer += result;
|
|
|
|
|
size -= result;
|
2000-09-24 09:57:26 +00:00
|
|
|
}
|
2000-10-14 18:34:51 +00:00
|
|
|
return xfer;
|
2000-05-08 18:53:38 +00:00
|
|
|
}
|
|
|
|
|
|
2001-12-09 12:32:42 +00:00
|
|
|
static snd_pcm_sframes_t snd_pcm_mmap_read_areas(snd_pcm_t *pcm,
|
2001-03-29 17:50:28 +00:00
|
|
|
const snd_pcm_channel_area_t *areas,
|
|
|
|
|
snd_pcm_uframes_t offset,
|
|
|
|
|
snd_pcm_uframes_t size)
|
2001-02-27 13:42:12 +00:00
|
|
|
{
|
2001-12-09 12:32:42 +00:00
|
|
|
snd_pcm_uframes_t xfer = 0;
|
|
|
|
|
|
2001-05-14 15:21:18 +00:00
|
|
|
assert(snd_pcm_mmap_capture_avail(pcm) >= size);
|
2001-02-27 13:42:12 +00:00
|
|
|
while (size > 0) {
|
2001-04-13 15:40:53 +00:00
|
|
|
const snd_pcm_channel_area_t *pcm_areas;
|
|
|
|
|
snd_pcm_uframes_t pcm_offset;
|
2001-02-27 13:42:12 +00:00
|
|
|
snd_pcm_uframes_t frames = size;
|
2002-02-21 15:01:34 +00:00
|
|
|
snd_pcm_sframes_t result;
|
|
|
|
|
|
2001-04-13 15:40:53 +00:00
|
|
|
snd_pcm_mmap_begin(pcm, &pcm_areas, &pcm_offset, &frames);
|
2001-12-09 12:32:42 +00:00
|
|
|
snd_pcm_areas_copy(areas, offset,
|
2001-05-14 15:21:18 +00:00
|
|
|
pcm_areas, pcm_offset,
|
|
|
|
|
pcm->channels,
|
|
|
|
|
frames, pcm->format);
|
2002-02-21 15:01:34 +00:00
|
|
|
result = snd_pcm_mmap_commit(pcm, pcm_offset, frames);
|
|
|
|
|
if (result < 0)
|
|
|
|
|
return xfer > 0 ? xfer : result;
|
|
|
|
|
offset += result;
|
|
|
|
|
xfer += result;
|
|
|
|
|
size -= result;
|
2000-05-08 18:53:38 +00:00
|
|
|
}
|
2000-10-14 18:34:51 +00:00
|
|
|
return xfer;
|
2000-05-08 18:53:38 +00:00
|
|
|
}
|
|
|
|
|
|
2001-10-18 18:05:41 +00:00
|
|
|
/**
|
|
|
|
|
* \brief Write interleaved frames to a PCM using direct buffer (mmap)
|
|
|
|
|
* \param pcm PCM handle
|
|
|
|
|
* \param buffer frames containing buffer
|
|
|
|
|
* \param size frames to be written
|
|
|
|
|
* \return a positive number of frames actually written otherwise a
|
|
|
|
|
* negative error code
|
|
|
|
|
* \retval -EBADFD PCM is not in the right state (#SND_PCM_STATE_PREPARED or #SND_PCM_STATE_RUNNING)
|
2002-03-12 20:14:33 +00:00
|
|
|
* \retval -EPIPE an underrun occurred
|
|
|
|
|
* \retval -ESTRPIPE a suspend event occurred (stream is suspended and waiting for an application recovery)
|
2001-10-18 18:05:41 +00:00
|
|
|
*
|
|
|
|
|
* If the blocking behaviour is selected, then routine waits until
|
|
|
|
|
* all requested bytes are played or put to the playback ring buffer.
|
2002-03-12 20:14:33 +00:00
|
|
|
* The count of bytes can be less only if a signal or underrun occurred.
|
2001-10-18 18:05:41 +00:00
|
|
|
*
|
|
|
|
|
* If the non-blocking behaviour is selected, then routine doesn't wait at all.
|
|
|
|
|
*/
|
2001-01-15 11:06:53 +00:00
|
|
|
snd_pcm_sframes_t snd_pcm_mmap_writei(snd_pcm_t *pcm, const void *buffer, snd_pcm_uframes_t size)
|
2000-05-08 18:53:38 +00:00
|
|
|
{
|
2000-11-20 20:10:46 +00:00
|
|
|
snd_pcm_channel_area_t areas[pcm->channels];
|
2000-09-24 09:57:26 +00:00
|
|
|
snd_pcm_areas_from_buf(pcm, areas, (void*)buffer);
|
|
|
|
|
return snd_pcm_write_areas(pcm, areas, 0, size,
|
|
|
|
|
snd_pcm_mmap_write_areas);
|
2000-05-08 18:53:38 +00:00
|
|
|
}
|
|
|
|
|
|
2001-10-18 18:05:41 +00:00
|
|
|
/**
|
|
|
|
|
* \brief Write non interleaved frames to a PCM using direct buffer (mmap)
|
|
|
|
|
* \param pcm PCM handle
|
|
|
|
|
* \param bufs frames containing buffers (one for each channel)
|
|
|
|
|
* \param size frames to be written
|
|
|
|
|
* \return a positive number of frames actually written otherwise a
|
|
|
|
|
* negative error code
|
|
|
|
|
* \retval -EBADFD PCM is not in the right state (#SND_PCM_STATE_PREPARED or #SND_PCM_STATE_RUNNING)
|
2002-03-12 20:14:33 +00:00
|
|
|
* \retval -EPIPE an underrun occurred
|
|
|
|
|
* \retval -ESTRPIPE a suspend event occurred (stream is suspended and waiting for an application recovery)
|
2001-10-18 18:05:41 +00:00
|
|
|
*
|
|
|
|
|
* If the blocking behaviour is selected, then routine waits until
|
|
|
|
|
* all requested bytes are played or put to the playback ring buffer.
|
2002-03-12 20:14:33 +00:00
|
|
|
* The count of bytes can be less only if a signal or underrun occurred.
|
2001-10-18 18:05:41 +00:00
|
|
|
*
|
|
|
|
|
* If the non-blocking behaviour is selected, then routine doesn't wait at all.
|
|
|
|
|
*/
|
2001-01-15 11:06:53 +00:00
|
|
|
snd_pcm_sframes_t snd_pcm_mmap_writen(snd_pcm_t *pcm, void **bufs, snd_pcm_uframes_t size)
|
2000-05-08 18:53:38 +00:00
|
|
|
{
|
2000-11-20 20:10:46 +00:00
|
|
|
snd_pcm_channel_area_t areas[pcm->channels];
|
2000-09-24 09:57:26 +00:00
|
|
|
snd_pcm_areas_from_bufs(pcm, areas, bufs);
|
|
|
|
|
return snd_pcm_write_areas(pcm, areas, 0, size,
|
|
|
|
|
snd_pcm_mmap_write_areas);
|
2000-06-10 12:39:51 +00:00
|
|
|
}
|
|
|
|
|
|
2001-10-18 18:05:41 +00:00
|
|
|
/**
|
|
|
|
|
* \brief Read interleaved frames from a PCM using direct buffer (mmap)
|
|
|
|
|
* \param pcm PCM handle
|
|
|
|
|
* \param buffer frames containing buffer
|
|
|
|
|
* \param size frames to be written
|
|
|
|
|
* \return a positive number of frames actually read otherwise a
|
|
|
|
|
* negative error code
|
|
|
|
|
* \retval -EBADFD PCM is not in the right state (#SND_PCM_STATE_PREPARED or #SND_PCM_STATE_RUNNING)
|
2002-03-12 20:14:33 +00:00
|
|
|
* \retval -EPIPE an overrun occurred
|
|
|
|
|
* \retval -ESTRPIPE a suspend event occurred (stream is suspended and waiting for an application recovery)
|
2001-10-18 18:05:41 +00:00
|
|
|
*
|
|
|
|
|
* If the blocking behaviour was selected, then routine waits until
|
|
|
|
|
* all requested bytes are filled. The count of bytes can be less only
|
2002-03-12 20:14:33 +00:00
|
|
|
* if a signal or underrun occurred.
|
2001-10-18 18:05:41 +00:00
|
|
|
*
|
|
|
|
|
* If the non-blocking behaviour is selected, then routine doesn't wait at all.
|
|
|
|
|
*/
|
2001-01-15 11:06:53 +00:00
|
|
|
snd_pcm_sframes_t snd_pcm_mmap_readi(snd_pcm_t *pcm, void *buffer, snd_pcm_uframes_t size)
|
2000-09-24 09:57:26 +00:00
|
|
|
{
|
2000-11-20 20:10:46 +00:00
|
|
|
snd_pcm_channel_area_t areas[pcm->channels];
|
2000-09-24 09:57:26 +00:00
|
|
|
snd_pcm_areas_from_buf(pcm, areas, buffer);
|
|
|
|
|
return snd_pcm_read_areas(pcm, areas, 0, size,
|
|
|
|
|
snd_pcm_mmap_read_areas);
|
|
|
|
|
}
|
|
|
|
|
|
2001-10-18 18:05:41 +00:00
|
|
|
/**
|
|
|
|
|
* \brief Read non interleaved frames to a PCM using direct buffer (mmap)
|
|
|
|
|
* \param pcm PCM handle
|
|
|
|
|
* \param bufs frames containing buffers (one for each channel)
|
|
|
|
|
* \param size frames to be written
|
|
|
|
|
* \return a positive number of frames actually read otherwise a
|
|
|
|
|
* negative error code
|
|
|
|
|
* \retval -EBADFD PCM is not in the right state (#SND_PCM_STATE_PREPARED or #SND_PCM_STATE_RUNNING)
|
2002-03-12 20:14:33 +00:00
|
|
|
* \retval -EPIPE an overrun occurred
|
|
|
|
|
* \retval -ESTRPIPE a suspend event occurred (stream is suspended and waiting for an application recovery)
|
2001-10-18 18:05:41 +00:00
|
|
|
*
|
|
|
|
|
* If the blocking behaviour was selected, then routine waits until
|
|
|
|
|
* all requested bytes are filled. The count of bytes can be less only
|
2002-03-12 20:14:33 +00:00
|
|
|
* if a signal or underrun occurred.
|
2001-10-18 18:05:41 +00:00
|
|
|
*
|
|
|
|
|
* If the non-blocking behaviour is selected, then routine doesn't wait at all.
|
|
|
|
|
*/
|
2001-01-15 11:06:53 +00:00
|
|
|
snd_pcm_sframes_t snd_pcm_mmap_readn(snd_pcm_t *pcm, void **bufs, snd_pcm_uframes_t size)
|
2000-09-24 09:57:26 +00:00
|
|
|
{
|
2000-11-20 20:10:46 +00:00
|
|
|
snd_pcm_channel_area_t areas[pcm->channels];
|
2000-09-24 09:57:26 +00:00
|
|
|
snd_pcm_areas_from_bufs(pcm, areas, bufs);
|
|
|
|
|
return snd_pcm_read_areas(pcm, areas, 0, size,
|
|
|
|
|
snd_pcm_mmap_read_areas);
|
|
|
|
|
}
|
|
|
|
|
|
2000-11-20 20:10:46 +00:00
|
|
|
int snd_pcm_channel_info(snd_pcm_t *pcm, snd_pcm_channel_info_t *info)
|
2000-05-16 15:20:34 +00:00
|
|
|
{
|
2000-11-20 20:10:46 +00:00
|
|
|
return pcm->ops->channel_info(pcm, info);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int snd_pcm_channel_info_shm(snd_pcm_t *pcm, snd_pcm_channel_info_t *info,
|
|
|
|
|
int shmid)
|
|
|
|
|
{
|
2001-07-11 14:09:01 +00:00
|
|
|
switch (pcm->access) {
|
2000-11-20 20:10:46 +00:00
|
|
|
case SND_PCM_ACCESS_MMAP_INTERLEAVED:
|
|
|
|
|
case SND_PCM_ACCESS_RW_INTERLEAVED:
|
2001-01-15 15:15:24 +00:00
|
|
|
info->first = info->channel * pcm->sample_bits;
|
|
|
|
|
info->step = pcm->frame_bits;
|
2000-11-20 20:10:46 +00:00
|
|
|
break;
|
|
|
|
|
case SND_PCM_ACCESS_MMAP_NONINTERLEAVED:
|
|
|
|
|
case SND_PCM_ACCESS_RW_NONINTERLEAVED:
|
|
|
|
|
info->first = 0;
|
2001-01-15 15:15:24 +00:00
|
|
|
info->step = pcm->sample_bits;
|
2000-11-20 20:10:46 +00:00
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
assert(0);
|
|
|
|
|
break;
|
2000-05-16 15:20:34 +00:00
|
|
|
}
|
2000-11-20 20:10:46 +00:00
|
|
|
info->addr = 0;
|
|
|
|
|
info->type = SND_PCM_AREA_SHM;
|
|
|
|
|
info->u.shm.shmid = shmid;
|
2000-05-16 15:20:34 +00:00
|
|
|
return 0;
|
2000-11-20 20:10:46 +00:00
|
|
|
}
|
2000-05-16 15:20:34 +00:00
|
|
|
|
2000-10-14 10:31:34 +00:00
|
|
|
int snd_pcm_mmap(snd_pcm_t *pcm)
|
2000-05-08 18:53:38 +00:00
|
|
|
{
|
|
|
|
|
int err;
|
2000-11-20 20:10:46 +00:00
|
|
|
unsigned int c;
|
2000-09-24 09:57:26 +00:00
|
|
|
assert(pcm);
|
2000-11-20 20:10:46 +00:00
|
|
|
assert(pcm->setup);
|
|
|
|
|
assert(!pcm->mmap_channels);
|
|
|
|
|
err = pcm->ops->mmap(pcm);
|
2000-05-16 15:20:34 +00:00
|
|
|
if (err < 0)
|
|
|
|
|
return err;
|
2000-11-20 20:10:46 +00:00
|
|
|
pcm->mmap_channels = calloc(pcm->channels, sizeof(pcm->mmap_channels[0]));
|
|
|
|
|
if (!pcm->mmap_channels)
|
|
|
|
|
return -ENOMEM;
|
|
|
|
|
assert(!pcm->running_areas);
|
|
|
|
|
pcm->running_areas = calloc(pcm->channels, sizeof(pcm->running_areas[0]));
|
|
|
|
|
if (!pcm->running_areas) {
|
|
|
|
|
free(pcm->mmap_channels);
|
|
|
|
|
pcm->mmap_channels = NULL;
|
|
|
|
|
return -ENOMEM;
|
|
|
|
|
}
|
|
|
|
|
for (c = 0; c < pcm->channels; ++c) {
|
|
|
|
|
snd_pcm_channel_info_t *i = &pcm->mmap_channels[c];
|
|
|
|
|
i->channel = c;
|
|
|
|
|
err = snd_pcm_channel_info(pcm, i);
|
|
|
|
|
if (err < 0)
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
for (c = 0; c < pcm->channels; ++c) {
|
|
|
|
|
snd_pcm_channel_info_t *i = &pcm->mmap_channels[c];
|
|
|
|
|
snd_pcm_channel_area_t *a = &pcm->running_areas[c];
|
|
|
|
|
unsigned int c1;
|
|
|
|
|
if (!i->addr) {
|
|
|
|
|
char *ptr;
|
2001-01-15 15:15:24 +00:00
|
|
|
size_t size = i->first + i->step * (pcm->buffer_size - 1) + pcm->sample_bits;
|
2000-11-20 20:10:46 +00:00
|
|
|
for (c1 = c + 1; c1 < pcm->channels; ++c1) {
|
|
|
|
|
snd_pcm_channel_info_t *i1 = &pcm->mmap_channels[c1];
|
|
|
|
|
size_t s;
|
|
|
|
|
if (i1->type != i->type)
|
|
|
|
|
continue;
|
|
|
|
|
switch (i1->type) {
|
|
|
|
|
case SND_PCM_AREA_MMAP:
|
|
|
|
|
if (i1->u.mmap.fd != i->u.mmap.fd ||
|
|
|
|
|
i1->u.mmap.offset != i->u.mmap.offset)
|
|
|
|
|
continue;
|
|
|
|
|
break;
|
|
|
|
|
case SND_PCM_AREA_SHM:
|
|
|
|
|
if (i1->u.shm.shmid != i->u.shm.shmid)
|
|
|
|
|
continue;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
assert(0);
|
|
|
|
|
}
|
2001-01-15 15:15:24 +00:00
|
|
|
s = i1->first + i1->step * (pcm->buffer_size - 1) + pcm->sample_bits;
|
2000-11-20 20:10:46 +00:00
|
|
|
if (s > size)
|
|
|
|
|
size = s;
|
|
|
|
|
}
|
|
|
|
|
size = (size + 7) / 8;
|
2001-02-21 21:59:35 +00:00
|
|
|
size = page_align(size);
|
2000-11-20 20:10:46 +00:00
|
|
|
switch (i->type) {
|
|
|
|
|
case SND_PCM_AREA_MMAP:
|
|
|
|
|
ptr = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_FILE|MAP_SHARED, i->u.mmap.fd, i->u.mmap.offset);
|
|
|
|
|
if (ptr == MAP_FAILED) {
|
|
|
|
|
SYSERR("mmap failed");
|
|
|
|
|
return -errno;
|
|
|
|
|
}
|
|
|
|
|
i->addr = ptr;
|
|
|
|
|
break;
|
|
|
|
|
case SND_PCM_AREA_SHM:
|
|
|
|
|
if (i->u.shm.shmid < 0) {
|
|
|
|
|
int id;
|
|
|
|
|
id = shmget(IPC_PRIVATE, size, 0666);
|
|
|
|
|
if (id < 0) {
|
|
|
|
|
SYSERR("shmget failed");
|
|
|
|
|
return -errno;
|
|
|
|
|
}
|
|
|
|
|
i->u.shm.shmid = id;
|
|
|
|
|
}
|
|
|
|
|
ptr = shmat(i->u.shm.shmid, 0, 0);
|
|
|
|
|
if (ptr == (void*) -1) {
|
|
|
|
|
SYSERR("shmat failed");
|
|
|
|
|
return -errno;
|
|
|
|
|
}
|
|
|
|
|
i->addr = ptr;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
assert(0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for (c1 = c + 1; c1 < pcm->channels; ++c1) {
|
|
|
|
|
snd_pcm_channel_info_t *i1 = &pcm->mmap_channels[c1];
|
|
|
|
|
if (i1->type != i->type)
|
|
|
|
|
continue;
|
|
|
|
|
switch (i1->type) {
|
|
|
|
|
case SND_PCM_AREA_MMAP:
|
|
|
|
|
if (i1->u.mmap.fd != i->u.mmap.fd ||
|
|
|
|
|
i1->u.mmap.offset != i->u.mmap.offset)
|
|
|
|
|
continue;
|
|
|
|
|
break;
|
|
|
|
|
case SND_PCM_AREA_SHM:
|
|
|
|
|
if (i1->u.shm.shmid != i->u.shm.shmid)
|
|
|
|
|
continue;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
assert(0);
|
|
|
|
|
}
|
|
|
|
|
i1->addr = i->addr;
|
|
|
|
|
}
|
|
|
|
|
a->addr = i->addr;
|
|
|
|
|
a->first = i->first;
|
|
|
|
|
a->step = i->step;
|
|
|
|
|
}
|
2000-05-08 18:53:38 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2000-10-14 10:31:34 +00:00
|
|
|
int snd_pcm_munmap(snd_pcm_t *pcm)
|
2000-05-08 18:53:38 +00:00
|
|
|
{
|
|
|
|
|
int err;
|
2000-11-20 20:10:46 +00:00
|
|
|
unsigned int c;
|
2000-09-24 09:57:26 +00:00
|
|
|
assert(pcm);
|
2000-11-20 20:10:46 +00:00
|
|
|
assert(pcm->mmap_channels);
|
|
|
|
|
for (c = 0; c < pcm->channels; ++c) {
|
|
|
|
|
snd_pcm_channel_info_t *i = &pcm->mmap_channels[c];
|
|
|
|
|
unsigned int c1;
|
2001-12-15 17:22:31 +00:00
|
|
|
size_t size = i->first + i->step * (pcm->buffer_size - 1) + pcm->sample_bits;
|
2000-11-20 20:10:46 +00:00
|
|
|
if (!i->addr)
|
|
|
|
|
continue;
|
|
|
|
|
for (c1 = c + 1; c1 < pcm->channels; ++c1) {
|
|
|
|
|
snd_pcm_channel_info_t *i1 = &pcm->mmap_channels[c1];
|
|
|
|
|
size_t s;
|
|
|
|
|
if (i1->addr != i->addr)
|
|
|
|
|
continue;
|
|
|
|
|
i1->addr = NULL;
|
2001-12-15 17:22:31 +00:00
|
|
|
s = i1->first + i1->step * (pcm->buffer_size - 1) + pcm->sample_bits;
|
2000-11-20 20:10:46 +00:00
|
|
|
if (s > size)
|
|
|
|
|
size = s;
|
|
|
|
|
}
|
|
|
|
|
size = (size + 7) / 8;
|
2001-02-21 21:59:35 +00:00
|
|
|
size = page_align(size);
|
2000-11-20 20:10:46 +00:00
|
|
|
switch (i->type) {
|
|
|
|
|
case SND_PCM_AREA_MMAP:
|
|
|
|
|
err = munmap(i->addr, size);
|
|
|
|
|
if (err < 0) {
|
|
|
|
|
SYSERR("mmap failed");
|
|
|
|
|
return -errno;
|
|
|
|
|
}
|
2000-12-11 15:54:55 +00:00
|
|
|
errno = 0;
|
2000-11-20 20:10:46 +00:00
|
|
|
break;
|
|
|
|
|
case SND_PCM_AREA_SHM:
|
|
|
|
|
err = shmdt(i->addr);
|
|
|
|
|
if (err < 0) {
|
|
|
|
|
SYSERR("shmdt failed");
|
|
|
|
|
return -errno;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
assert(0);
|
|
|
|
|
}
|
|
|
|
|
i->addr = NULL;
|
|
|
|
|
}
|
|
|
|
|
err = pcm->ops->munmap(pcm);
|
|
|
|
|
if (err < 0)
|
2000-05-31 15:09:27 +00:00
|
|
|
return err;
|
2000-11-20 20:10:46 +00:00
|
|
|
free(pcm->mmap_channels);
|
2000-11-30 14:15:52 +00:00
|
|
|
free(pcm->running_areas);
|
2000-11-20 20:10:46 +00:00
|
|
|
pcm->mmap_channels = 0;
|
2000-11-30 14:15:52 +00:00
|
|
|
pcm->running_areas = 0;
|
2000-05-08 18:53:38 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2001-01-15 11:06:53 +00:00
|
|
|
snd_pcm_sframes_t snd_pcm_write_mmap(snd_pcm_t *pcm, snd_pcm_uframes_t size)
|
2000-09-24 09:57:26 +00:00
|
|
|
{
|
2001-01-15 11:06:53 +00:00
|
|
|
snd_pcm_uframes_t xfer = 0;
|
|
|
|
|
snd_pcm_sframes_t err = 0;
|
2000-09-24 09:57:26 +00:00
|
|
|
assert(size > 0);
|
|
|
|
|
while (xfer < size) {
|
2001-01-15 11:06:53 +00:00
|
|
|
snd_pcm_uframes_t frames = size - xfer;
|
|
|
|
|
snd_pcm_uframes_t offset = snd_pcm_mmap_hw_offset(pcm);
|
|
|
|
|
snd_pcm_uframes_t cont = pcm->buffer_size - offset;
|
2000-09-24 09:57:26 +00:00
|
|
|
if (cont < frames)
|
|
|
|
|
frames = cont;
|
2001-07-11 14:09:01 +00:00
|
|
|
switch (pcm->access) {
|
2000-11-20 20:10:46 +00:00
|
|
|
case SND_PCM_ACCESS_MMAP_INTERLEAVED:
|
|
|
|
|
{
|
2000-11-30 14:15:52 +00:00
|
|
|
const snd_pcm_channel_area_t *a = snd_pcm_mmap_areas(pcm);
|
2001-02-11 15:45:35 +00:00
|
|
|
const char *buf = snd_pcm_channel_area_addr(a, offset);
|
2001-12-08 21:00:51 +00:00
|
|
|
err = _snd_pcm_writei(pcm, buf, frames);
|
|
|
|
|
if (err >= 0)
|
|
|
|
|
frames = err;
|
2000-11-20 20:10:46 +00:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case SND_PCM_ACCESS_MMAP_NONINTERLEAVED:
|
|
|
|
|
{
|
2001-01-15 11:06:53 +00:00
|
|
|
unsigned int channels = pcm->channels;
|
2000-09-24 09:57:26 +00:00
|
|
|
unsigned int c;
|
|
|
|
|
void *bufs[channels];
|
2000-11-30 14:15:52 +00:00
|
|
|
const snd_pcm_channel_area_t *areas = snd_pcm_mmap_areas(pcm);
|
2000-09-24 09:57:26 +00:00
|
|
|
for (c = 0; c < channels; ++c) {
|
2000-11-30 14:15:52 +00:00
|
|
|
const snd_pcm_channel_area_t *a = &areas[c];
|
2000-09-24 09:57:26 +00:00
|
|
|
bufs[c] = snd_pcm_channel_area_addr(a, offset);
|
|
|
|
|
}
|
2001-12-08 21:00:51 +00:00
|
|
|
err = _snd_pcm_writen(pcm, bufs, frames);
|
|
|
|
|
if (err >= 0)
|
|
|
|
|
frames = err;
|
2000-11-20 20:10:46 +00:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
assert(0);
|
|
|
|
|
err = -EINVAL;
|
|
|
|
|
break;
|
2000-09-24 09:57:26 +00:00
|
|
|
}
|
|
|
|
|
if (err < 0)
|
|
|
|
|
break;
|
|
|
|
|
xfer += frames;
|
|
|
|
|
}
|
|
|
|
|
if (xfer > 0)
|
|
|
|
|
return xfer;
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
2001-01-15 11:06:53 +00:00
|
|
|
snd_pcm_sframes_t snd_pcm_read_mmap(snd_pcm_t *pcm, snd_pcm_uframes_t size)
|
2000-09-24 09:57:26 +00:00
|
|
|
{
|
2001-01-15 11:06:53 +00:00
|
|
|
snd_pcm_uframes_t xfer = 0;
|
|
|
|
|
snd_pcm_sframes_t err = 0;
|
2000-09-24 09:57:26 +00:00
|
|
|
assert(size > 0);
|
|
|
|
|
while (xfer < size) {
|
2001-01-15 11:06:53 +00:00
|
|
|
snd_pcm_uframes_t frames = size - xfer;
|
|
|
|
|
snd_pcm_uframes_t offset = snd_pcm_mmap_hw_offset(pcm);
|
|
|
|
|
snd_pcm_uframes_t cont = pcm->buffer_size - offset;
|
2000-09-24 09:57:26 +00:00
|
|
|
if (cont < frames)
|
|
|
|
|
frames = cont;
|
2001-07-11 14:09:01 +00:00
|
|
|
switch (pcm->access) {
|
2000-11-20 20:10:46 +00:00
|
|
|
case SND_PCM_ACCESS_MMAP_INTERLEAVED:
|
|
|
|
|
{
|
2000-11-30 14:15:52 +00:00
|
|
|
const snd_pcm_channel_area_t *a = snd_pcm_mmap_areas(pcm);
|
2000-09-24 09:57:26 +00:00
|
|
|
char *buf = snd_pcm_channel_area_addr(a, offset);
|
2001-12-08 21:00:51 +00:00
|
|
|
err = _snd_pcm_readi(pcm, buf, frames);
|
|
|
|
|
if (err >= 0)
|
|
|
|
|
frames = err;
|
2000-11-20 20:10:46 +00:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case SND_PCM_ACCESS_MMAP_NONINTERLEAVED:
|
|
|
|
|
{
|
2001-01-15 11:06:53 +00:00
|
|
|
snd_pcm_uframes_t channels = pcm->channels;
|
2000-09-24 09:57:26 +00:00
|
|
|
unsigned int c;
|
|
|
|
|
void *bufs[channels];
|
2000-11-30 14:15:52 +00:00
|
|
|
const snd_pcm_channel_area_t *areas = snd_pcm_mmap_areas(pcm);
|
2000-09-24 09:57:26 +00:00
|
|
|
for (c = 0; c < channels; ++c) {
|
2000-11-30 14:15:52 +00:00
|
|
|
const snd_pcm_channel_area_t *a = &areas[c];
|
2000-09-24 09:57:26 +00:00
|
|
|
bufs[c] = snd_pcm_channel_area_addr(a, offset);
|
|
|
|
|
}
|
2001-12-08 21:00:51 +00:00
|
|
|
err = _snd_pcm_readn(pcm->fast_op_arg, bufs, frames);
|
|
|
|
|
if (err >= 0)
|
|
|
|
|
frames = err;
|
2000-09-24 09:57:26 +00:00
|
|
|
}
|
2000-11-20 20:10:46 +00:00
|
|
|
default:
|
|
|
|
|
assert(0);
|
|
|
|
|
err = -EINVAL;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2000-09-24 09:57:26 +00:00
|
|
|
if (err < 0)
|
|
|
|
|
break;
|
|
|
|
|
xfer += frames;
|
|
|
|
|
}
|
|
|
|
|
if (xfer > 0)
|
|
|
|
|
return xfer;
|
|
|
|
|
return err;
|
|
|
|
|
}
|