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
|
|
|
|
|
* it under the terms of the GNU Library General Public License as
|
|
|
|
|
* published by the Free Software Foundation; either version 2 of
|
|
|
|
|
* 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
|
|
|
|
|
* GNU Library General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
|
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <malloc.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <sys/poll.h>
|
|
|
|
|
#include <sys/uio.h>
|
|
|
|
|
#include "pcm_local.h"
|
|
|
|
|
|
2000-06-21 14:59:20 +00:00
|
|
|
int snd_pcm_frames_avail(snd_pcm_t *handle, ssize_t *frames)
|
2000-05-08 18:53:38 +00:00
|
|
|
{
|
2000-06-21 14:59:20 +00:00
|
|
|
assert(handle);
|
|
|
|
|
assert(handle->mmap_status && handle->mmap_control);
|
|
|
|
|
if (handle->stream == SND_PCM_STREAM_PLAYBACK)
|
|
|
|
|
*frames = snd_pcm_mmap_playback_frames_avail(handle);
|
2000-05-08 18:53:38 +00:00
|
|
|
else
|
2000-06-21 14:59:20 +00:00
|
|
|
*frames = snd_pcm_mmap_capture_frames_avail(handle);
|
2000-05-08 18:53:38 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2000-06-21 14:59:20 +00:00
|
|
|
static int snd_pcm_mmap_playback_ready(snd_pcm_t *handle)
|
2000-05-08 18:53:38 +00:00
|
|
|
{
|
2000-06-21 14:59:20 +00:00
|
|
|
if (handle->mmap_status->state == SND_PCM_STATE_XRUN)
|
2000-05-08 18:53:38 +00:00
|
|
|
return -EPIPE;
|
2000-06-21 14:59:20 +00:00
|
|
|
return snd_pcm_mmap_playback_frames_avail(handle) >= handle->setup.frames_min;
|
2000-05-08 18:53:38 +00:00
|
|
|
}
|
|
|
|
|
|
2000-06-21 14:59:20 +00:00
|
|
|
static int snd_pcm_mmap_capture_ready(snd_pcm_t *handle)
|
2000-05-08 18:53:38 +00:00
|
|
|
{
|
|
|
|
|
int ret = 0;
|
2000-06-21 14:59:20 +00:00
|
|
|
if (handle->mmap_status->state == SND_PCM_STATE_XRUN) {
|
2000-05-08 18:53:38 +00:00
|
|
|
ret = -EPIPE;
|
2000-06-21 14:59:20 +00:00
|
|
|
if (handle->setup.xrun_mode == SND_PCM_XRUN_DRAIN)
|
2000-05-08 18:53:38 +00:00
|
|
|
return -EPIPE;
|
|
|
|
|
}
|
2000-06-21 14:59:20 +00:00
|
|
|
if (snd_pcm_mmap_capture_frames_avail(handle) >= handle->setup.frames_min)
|
2000-05-23 12:52:06 +00:00
|
|
|
return 1;
|
2000-05-08 18:53:38 +00:00
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2000-06-21 14:59:20 +00:00
|
|
|
int snd_pcm_mmap_ready(snd_pcm_t *handle)
|
2000-05-08 18:53:38 +00:00
|
|
|
{
|
2000-06-21 14:59:20 +00:00
|
|
|
assert(handle);
|
|
|
|
|
assert(handle->mmap_status && handle->mmap_control);
|
|
|
|
|
assert(handle->mmap_status->state >= SND_PCM_STATE_PREPARED);
|
|
|
|
|
if (handle->stream == SND_PCM_STREAM_PLAYBACK) {
|
|
|
|
|
return snd_pcm_mmap_playback_ready(handle);
|
2000-05-08 18:53:38 +00:00
|
|
|
} else {
|
2000-06-21 14:59:20 +00:00
|
|
|
return snd_pcm_mmap_capture_ready(handle);
|
2000-05-08 18:53:38 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2000-06-21 14:59:20 +00:00
|
|
|
static size_t snd_pcm_mmap_playback_frames_xfer(snd_pcm_t *handle, size_t frames)
|
2000-05-16 15:20:34 +00:00
|
|
|
{
|
2000-06-21 14:59:20 +00:00
|
|
|
snd_pcm_mmap_control_t *control = handle->mmap_control;
|
2000-06-10 12:39:51 +00:00
|
|
|
size_t frames_cont;
|
2000-06-21 14:59:20 +00:00
|
|
|
size_t frames_avail = snd_pcm_mmap_playback_frames_avail(handle);
|
2000-06-10 12:39:51 +00:00
|
|
|
if (frames_avail < frames)
|
|
|
|
|
frames = frames_avail;
|
2000-06-21 14:59:20 +00:00
|
|
|
frames_cont = handle->setup.buffer_size - control->frame_data % handle->setup.buffer_size;
|
2000-06-10 12:39:51 +00:00
|
|
|
if (frames_cont < frames)
|
|
|
|
|
frames = frames_cont;
|
|
|
|
|
return frames;
|
2000-05-16 15:20:34 +00:00
|
|
|
}
|
|
|
|
|
|
2000-06-21 14:59:20 +00:00
|
|
|
static size_t snd_pcm_mmap_capture_frames_xfer(snd_pcm_t *handle, size_t frames)
|
2000-05-16 15:20:34 +00:00
|
|
|
{
|
2000-06-21 14:59:20 +00:00
|
|
|
snd_pcm_mmap_control_t *control = handle->mmap_control;
|
2000-06-10 12:39:51 +00:00
|
|
|
size_t frames_cont;
|
2000-06-21 14:59:20 +00:00
|
|
|
size_t frames_avail = snd_pcm_mmap_capture_frames_avail(handle);
|
2000-06-10 12:39:51 +00:00
|
|
|
if (frames_avail < frames)
|
|
|
|
|
frames = frames_avail;
|
2000-06-21 14:59:20 +00:00
|
|
|
frames_cont = handle->setup.buffer_size - control->frame_data % handle->setup.buffer_size;
|
2000-06-10 12:39:51 +00:00
|
|
|
if (frames_cont < frames)
|
|
|
|
|
frames = frames_cont;
|
|
|
|
|
return frames;
|
2000-05-16 15:20:34 +00:00
|
|
|
}
|
|
|
|
|
|
2000-06-21 14:59:20 +00:00
|
|
|
ssize_t snd_pcm_mmap_frames_xfer(snd_pcm_t *handle, size_t frames)
|
2000-05-16 15:20:34 +00:00
|
|
|
{
|
2000-06-21 14:59:20 +00:00
|
|
|
assert(handle);
|
|
|
|
|
assert(handle->mmap_status && handle->mmap_control);
|
|
|
|
|
if (handle->stream == SND_PCM_STREAM_PLAYBACK)
|
|
|
|
|
return snd_pcm_mmap_playback_frames_xfer(handle, frames);
|
2000-05-16 15:20:34 +00:00
|
|
|
else
|
2000-06-21 14:59:20 +00:00
|
|
|
return snd_pcm_mmap_capture_frames_xfer(handle, frames);
|
2000-05-16 15:20:34 +00:00
|
|
|
}
|
|
|
|
|
|
2000-06-21 14:59:20 +00:00
|
|
|
ssize_t snd_pcm_mmap_frames_offset(snd_pcm_t *handle)
|
2000-05-16 15:20:34 +00:00
|
|
|
{
|
2000-06-21 14:59:20 +00:00
|
|
|
assert(handle);
|
|
|
|
|
assert(handle->mmap_control);
|
|
|
|
|
return handle->mmap_control->frame_data % handle->setup.buffer_size;
|
2000-06-04 13:13:01 +00:00
|
|
|
}
|
|
|
|
|
|
2000-06-21 14:59:20 +00:00
|
|
|
int snd_pcm_mmap_state(snd_pcm_t *handle)
|
2000-06-04 13:13:01 +00:00
|
|
|
{
|
2000-06-21 14:59:20 +00:00
|
|
|
assert(handle);
|
|
|
|
|
assert(handle->mmap_status);
|
|
|
|
|
return handle->mmap_status->state;
|
2000-06-04 13:13:01 +00:00
|
|
|
}
|
|
|
|
|
|
2000-06-21 14:59:20 +00:00
|
|
|
int snd_pcm_mmap_frame_io(snd_pcm_t *handle)
|
2000-06-04 13:13:01 +00:00
|
|
|
{
|
2000-06-21 14:59:20 +00:00
|
|
|
assert(handle);
|
|
|
|
|
assert(handle->mmap_status);
|
|
|
|
|
return handle->mmap_status->frame_io;
|
2000-06-04 13:13:01 +00:00
|
|
|
}
|
|
|
|
|
|
2000-06-21 14:59:20 +00:00
|
|
|
ssize_t snd_pcm_mmap_frame_data(snd_pcm_t *handle, off_t offset)
|
2000-06-04 13:13:01 +00:00
|
|
|
{
|
2000-06-10 12:39:51 +00:00
|
|
|
ssize_t frame_data;
|
2000-06-21 14:59:20 +00:00
|
|
|
assert(handle);
|
|
|
|
|
assert(handle->mmap_status && handle->mmap_control);
|
|
|
|
|
assert(offset == 0 || handle->type == SND_PCM_TYPE_HW);
|
|
|
|
|
frame_data = handle->mmap_control->frame_data;
|
2000-06-04 13:13:01 +00:00
|
|
|
if (offset == 0)
|
2000-06-10 12:39:51 +00:00
|
|
|
return frame_data;
|
2000-06-21 14:59:20 +00:00
|
|
|
switch (handle->mmap_status->state) {
|
2000-06-05 11:38:02 +00:00
|
|
|
case SND_PCM_STATE_RUNNING:
|
2000-06-21 14:59:20 +00:00
|
|
|
if (handle->setup.mode == SND_PCM_MODE_FRAME)
|
|
|
|
|
snd_pcm_frame_io(handle, 1);
|
2000-06-04 13:13:01 +00:00
|
|
|
break;
|
2000-07-16 11:29:55 +00:00
|
|
|
case SND_PCM_STATE_READY:
|
|
|
|
|
case SND_PCM_STATE_NOTREADY:
|
2000-05-16 15:20:34 +00:00
|
|
|
return -EBADFD;
|
2000-06-04 13:13:01 +00:00
|
|
|
}
|
|
|
|
|
if (offset < 0) {
|
2000-06-21 14:59:20 +00:00
|
|
|
if (offset < -(ssize_t)handle->setup.buffer_size)
|
|
|
|
|
offset = -(ssize_t)handle->setup.buffer_size;
|
2000-06-04 13:13:01 +00:00
|
|
|
else
|
2000-06-21 14:59:20 +00:00
|
|
|
offset -= offset % handle->setup.frames_align;
|
2000-06-10 12:39:51 +00:00
|
|
|
frame_data += offset;
|
|
|
|
|
if (frame_data < 0)
|
2000-06-21 14:59:20 +00:00
|
|
|
frame_data += handle->setup.frame_boundary;
|
2000-05-16 15:20:34 +00:00
|
|
|
} else {
|
2000-06-10 12:39:51 +00:00
|
|
|
size_t frames_avail;
|
2000-06-21 14:59:20 +00:00
|
|
|
if (handle->stream == SND_PCM_STREAM_PLAYBACK)
|
|
|
|
|
frames_avail = snd_pcm_mmap_playback_frames_avail(handle);
|
2000-06-04 13:13:01 +00:00
|
|
|
else
|
2000-06-21 14:59:20 +00:00
|
|
|
frames_avail = snd_pcm_mmap_capture_frames_avail(handle);
|
2000-06-10 12:39:51 +00:00
|
|
|
if ((size_t)offset > frames_avail)
|
|
|
|
|
offset = frames_avail;
|
2000-06-21 14:59:20 +00:00
|
|
|
offset -= offset % handle->setup.frames_align;
|
2000-06-10 12:39:51 +00:00
|
|
|
frame_data += offset;
|
2000-06-21 14:59:20 +00:00
|
|
|
if ((size_t)frame_data >= handle->setup.frame_boundary)
|
|
|
|
|
frame_data -= handle->setup.frame_boundary;
|
2000-05-16 15:20:34 +00:00
|
|
|
}
|
2000-06-21 14:59:20 +00:00
|
|
|
handle->mmap_control->frame_data = frame_data;
|
2000-06-10 12:39:51 +00:00
|
|
|
return frame_data;
|
2000-05-16 15:20:34 +00:00
|
|
|
}
|
2000-05-08 18:53:38 +00:00
|
|
|
|
2000-06-21 14:59:20 +00:00
|
|
|
ssize_t snd_pcm_mmap_write_areas(snd_pcm_t *handle, snd_pcm_channel_area_t *channels, size_t frames)
|
2000-05-08 18:53:38 +00:00
|
|
|
{
|
2000-06-10 12:39:51 +00:00
|
|
|
snd_pcm_mmap_status_t *status;
|
2000-05-08 18:53:38 +00:00
|
|
|
size_t offset = 0;
|
|
|
|
|
size_t result = 0;
|
|
|
|
|
int err;
|
|
|
|
|
|
2000-06-21 14:59:20 +00:00
|
|
|
assert(handle->mmap_data && handle->mmap_status && handle->mmap_control);
|
|
|
|
|
status = handle->mmap_status;
|
2000-06-10 12:39:51 +00:00
|
|
|
assert(status->state >= SND_PCM_STATE_PREPARED);
|
2000-06-21 14:59:20 +00:00
|
|
|
if (handle->setup.mode == SND_PCM_MODE_FRAGMENT) {
|
|
|
|
|
assert(frames % handle->setup.frag_size == 0);
|
2000-05-08 18:53:38 +00:00
|
|
|
} else {
|
2000-06-10 12:39:51 +00:00
|
|
|
if (status->state == SND_PCM_STATE_RUNNING &&
|
2000-06-21 14:59:20 +00:00
|
|
|
handle->mode & SND_PCM_NONBLOCK)
|
|
|
|
|
snd_pcm_frame_io(handle, 1);
|
2000-05-08 18:53:38 +00:00
|
|
|
}
|
2000-05-24 21:35:55 +00:00
|
|
|
while (frames > 0) {
|
2000-05-16 15:20:34 +00:00
|
|
|
ssize_t mmap_offset;
|
2000-05-24 21:35:55 +00:00
|
|
|
size_t frames1;
|
2000-06-21 14:59:20 +00:00
|
|
|
int ready = snd_pcm_mmap_playback_ready(handle);
|
2000-05-08 18:53:38 +00:00
|
|
|
if (ready < 0)
|
|
|
|
|
return ready;
|
|
|
|
|
if (!ready) {
|
|
|
|
|
struct pollfd pfd;
|
2000-06-10 12:39:51 +00:00
|
|
|
if (status->state != SND_PCM_STATE_RUNNING)
|
2000-05-08 18:53:38 +00:00
|
|
|
return result > 0 ? result : -EPIPE;
|
2000-06-21 14:59:20 +00:00
|
|
|
if (handle->mode & SND_PCM_NONBLOCK)
|
2000-05-08 18:53:38 +00:00
|
|
|
return result > 0 ? result : -EAGAIN;
|
2000-06-21 14:59:20 +00:00
|
|
|
pfd.fd = snd_pcm_file_descriptor(handle);
|
2000-05-08 18:53:38 +00:00
|
|
|
pfd.events = POLLOUT | POLLERR;
|
|
|
|
|
ready = poll(&pfd, 1, 10000);
|
|
|
|
|
if (ready < 0)
|
|
|
|
|
return result > 0 ? result : ready;
|
|
|
|
|
if (ready && pfd.revents & POLLERR)
|
|
|
|
|
return result > 0 ? result : -EPIPE;
|
2000-06-21 14:59:20 +00:00
|
|
|
assert(snd_pcm_mmap_playback_ready(handle));
|
2000-05-08 18:53:38 +00:00
|
|
|
}
|
2000-06-21 14:59:20 +00:00
|
|
|
frames1 = snd_pcm_mmap_playback_frames_xfer(handle, frames);
|
2000-05-24 21:35:55 +00:00
|
|
|
assert(frames1 > 0);
|
2000-06-21 14:59:20 +00:00
|
|
|
mmap_offset = snd_pcm_mmap_frames_offset(handle);
|
|
|
|
|
snd_pcm_areas_copy(channels, offset, handle->channels, mmap_offset, handle->setup.format.channels, frames1, handle->setup.format.format);
|
2000-06-10 12:39:51 +00:00
|
|
|
if (status->state == SND_PCM_STATE_XRUN)
|
2000-05-16 15:20:34 +00:00
|
|
|
return result > 0 ? result : -EPIPE;
|
2000-06-21 14:59:20 +00:00
|
|
|
snd_pcm_frame_data(handle, frames1);
|
2000-05-24 21:35:55 +00:00
|
|
|
frames -= frames1;
|
|
|
|
|
offset += frames1;
|
|
|
|
|
result += frames1;
|
2000-06-10 12:39:51 +00:00
|
|
|
if (status->state == SND_PCM_STATE_PREPARED &&
|
2000-06-21 14:59:20 +00:00
|
|
|
(handle->setup.start_mode == SND_PCM_START_DATA ||
|
|
|
|
|
(handle->setup.start_mode == SND_PCM_START_FULL &&
|
|
|
|
|
!snd_pcm_mmap_playback_ready(handle)))) {
|
|
|
|
|
err = snd_pcm_go(handle);
|
2000-05-16 15:20:34 +00:00
|
|
|
if (err < 0)
|
2000-05-08 18:53:38 +00:00
|
|
|
return result > 0 ? result : err;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2000-06-21 14:59:20 +00:00
|
|
|
ssize_t snd_pcm_mmap_write(snd_pcm_t *handle, const void *buffer, size_t frames)
|
2000-05-08 18:53:38 +00:00
|
|
|
{
|
2000-05-27 16:52:17 +00:00
|
|
|
unsigned int nchannels;
|
2000-06-21 14:59:20 +00:00
|
|
|
assert(handle);
|
|
|
|
|
assert(handle->mmap_data && handle->mmap_status && handle->mmap_control);
|
2000-06-04 16:24:04 +00:00
|
|
|
assert(frames == 0 || buffer);
|
2000-06-21 14:59:20 +00:00
|
|
|
nchannels = handle->setup.format.channels;
|
|
|
|
|
assert(handle->setup.format.interleave || nchannels == 1);
|
2000-05-16 15:20:34 +00:00
|
|
|
{
|
2000-05-27 16:52:17 +00:00
|
|
|
snd_pcm_channel_area_t channels[nchannels];
|
|
|
|
|
unsigned int channel;
|
|
|
|
|
for (channel = 0; channel < nchannels; ++channel) {
|
|
|
|
|
channels[channel].addr = (char*)buffer;
|
2000-06-21 14:59:20 +00:00
|
|
|
channels[channel].first = handle->bits_per_sample * channel;
|
|
|
|
|
channels[channel].step = handle->bits_per_frame;
|
2000-05-08 18:53:38 +00:00
|
|
|
}
|
2000-06-21 14:59:20 +00:00
|
|
|
return snd_pcm_mmap_write_areas(handle, channels, frames);
|
2000-05-08 18:53:38 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2000-06-21 14:59:20 +00:00
|
|
|
ssize_t snd_pcm_mmap_writev(snd_pcm_t *handle, const struct iovec *vector, unsigned long vcount)
|
2000-05-08 18:53:38 +00:00
|
|
|
{
|
|
|
|
|
size_t result = 0;
|
2000-05-27 16:52:17 +00:00
|
|
|
unsigned int nchannels;
|
2000-06-21 14:59:20 +00:00
|
|
|
assert(handle);
|
|
|
|
|
assert(handle->mmap_data && handle->mmap_status && handle->mmap_control);
|
2000-06-04 16:24:04 +00:00
|
|
|
assert(vcount == 0 || vector);
|
2000-06-21 14:59:20 +00:00
|
|
|
nchannels = handle->setup.format.channels;
|
|
|
|
|
if (handle->setup.format.interleave) {
|
2000-05-16 15:20:34 +00:00
|
|
|
unsigned int b;
|
2000-05-08 18:53:38 +00:00
|
|
|
for (b = 0; b < vcount; b++) {
|
2000-05-16 15:20:34 +00:00
|
|
|
ssize_t ret;
|
2000-06-10 12:39:51 +00:00
|
|
|
size_t frames = vector[b].iov_len;
|
2000-06-21 14:59:20 +00:00
|
|
|
ret = snd_pcm_mmap_write(handle, vector[b].iov_base, frames);
|
2000-05-16 15:20:34 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
|
if (result <= 0)
|
|
|
|
|
return ret;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2000-05-08 18:53:38 +00:00
|
|
|
result += ret;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2000-05-27 16:52:17 +00:00
|
|
|
snd_pcm_channel_area_t channels[nchannels];
|
2000-05-08 18:53:38 +00:00
|
|
|
unsigned long bcount;
|
2000-05-16 15:20:34 +00:00
|
|
|
unsigned int b;
|
2000-06-04 16:24:04 +00:00
|
|
|
assert(vcount % nchannels == 0);
|
2000-05-27 16:52:17 +00:00
|
|
|
bcount = vcount / nchannels;
|
2000-05-08 18:53:38 +00:00
|
|
|
for (b = 0; b < bcount; b++) {
|
|
|
|
|
unsigned int v;
|
2000-05-16 15:20:34 +00:00
|
|
|
ssize_t ret;
|
2000-06-10 12:39:51 +00:00
|
|
|
size_t frames = vector[0].iov_len;
|
2000-05-27 16:52:17 +00:00
|
|
|
for (v = 0; v < nchannels; ++v) {
|
2000-06-10 12:39:51 +00:00
|
|
|
assert(vector[v].iov_len == frames);
|
2000-05-27 16:52:17 +00:00
|
|
|
channels[v].addr = vector[v].iov_base;
|
|
|
|
|
channels[v].first = 0;
|
2000-06-21 14:59:20 +00:00
|
|
|
channels[v].step = handle->bits_per_sample;
|
2000-05-16 15:20:34 +00:00
|
|
|
}
|
2000-06-21 14:59:20 +00:00
|
|
|
ret = snd_pcm_mmap_write_areas(handle, channels, frames);
|
2000-05-16 15:20:34 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
|
if (result <= 0)
|
|
|
|
|
return ret;
|
|
|
|
|
break;
|
2000-05-08 18:53:38 +00:00
|
|
|
}
|
|
|
|
|
result += ret;
|
2000-05-24 21:35:55 +00:00
|
|
|
if ((size_t)ret != frames)
|
2000-05-08 18:53:38 +00:00
|
|
|
break;
|
2000-05-27 16:52:17 +00:00
|
|
|
vector += nchannels;
|
2000-05-08 18:53:38 +00:00
|
|
|
}
|
|
|
|
|
}
|
2000-06-10 12:39:51 +00:00
|
|
|
return result;
|
2000-05-08 18:53:38 +00:00
|
|
|
}
|
|
|
|
|
|
2000-06-21 14:59:20 +00:00
|
|
|
ssize_t snd_pcm_mmap_read_areas(snd_pcm_t *handle, snd_pcm_channel_area_t *channels, size_t frames)
|
2000-05-08 18:53:38 +00:00
|
|
|
{
|
2000-06-10 12:39:51 +00:00
|
|
|
snd_pcm_mmap_status_t *status;
|
2000-05-08 18:53:38 +00:00
|
|
|
size_t offset = 0;
|
|
|
|
|
size_t result = 0;
|
|
|
|
|
int err;
|
|
|
|
|
|
2000-06-21 14:59:20 +00:00
|
|
|
assert(handle->mmap_data && handle->mmap_status && handle->mmap_control);
|
|
|
|
|
status = handle->mmap_status;
|
2000-06-10 12:39:51 +00:00
|
|
|
assert(status->state >= SND_PCM_STATE_PREPARED);
|
2000-06-21 14:59:20 +00:00
|
|
|
if (handle->setup.mode == SND_PCM_MODE_FRAGMENT) {
|
|
|
|
|
assert(frames % handle->setup.frag_size == 0);
|
2000-05-08 18:53:38 +00:00
|
|
|
} else {
|
2000-06-10 12:39:51 +00:00
|
|
|
if (status->state == SND_PCM_STATE_RUNNING &&
|
2000-06-21 14:59:20 +00:00
|
|
|
handle->mode & SND_PCM_NONBLOCK)
|
|
|
|
|
snd_pcm_frame_io(handle, 1);
|
2000-05-08 18:53:38 +00:00
|
|
|
}
|
2000-06-10 12:39:51 +00:00
|
|
|
if (status->state == SND_PCM_STATE_PREPARED &&
|
2000-06-21 14:59:20 +00:00
|
|
|
handle->setup.start_mode == SND_PCM_START_DATA) {
|
|
|
|
|
err = snd_pcm_go(handle);
|
2000-05-08 18:53:38 +00:00
|
|
|
if (err < 0)
|
|
|
|
|
return err;
|
|
|
|
|
}
|
2000-05-24 21:35:55 +00:00
|
|
|
while (frames > 0) {
|
2000-05-16 15:20:34 +00:00
|
|
|
ssize_t mmap_offset;
|
2000-05-24 21:35:55 +00:00
|
|
|
size_t frames1;
|
2000-06-21 14:59:20 +00:00
|
|
|
int ready = snd_pcm_mmap_capture_ready(handle);
|
2000-05-08 18:53:38 +00:00
|
|
|
if (ready < 0)
|
|
|
|
|
return ready;
|
|
|
|
|
if (!ready) {
|
|
|
|
|
struct pollfd pfd;
|
2000-06-10 12:39:51 +00:00
|
|
|
if (status->state != SND_PCM_STATE_RUNNING)
|
2000-05-08 18:53:38 +00:00
|
|
|
return result > 0 ? result : -EPIPE;
|
2000-06-21 14:59:20 +00:00
|
|
|
if (handle->mode & SND_PCM_NONBLOCK)
|
2000-05-08 18:53:38 +00:00
|
|
|
return result > 0 ? result : -EAGAIN;
|
2000-06-21 14:59:20 +00:00
|
|
|
pfd.fd = snd_pcm_file_descriptor(handle);
|
2000-05-08 18:53:38 +00:00
|
|
|
pfd.events = POLLIN | POLLERR;
|
|
|
|
|
ready = poll(&pfd, 1, 10000);
|
|
|
|
|
if (ready < 0)
|
|
|
|
|
return result > 0 ? result : ready;
|
|
|
|
|
if (ready && pfd.revents & POLLERR)
|
|
|
|
|
return result > 0 ? result : -EPIPE;
|
2000-06-21 14:59:20 +00:00
|
|
|
assert(snd_pcm_mmap_capture_ready(handle));
|
2000-05-08 18:53:38 +00:00
|
|
|
}
|
2000-06-21 14:59:20 +00:00
|
|
|
frames1 = snd_pcm_mmap_capture_frames_xfer(handle, frames);
|
2000-05-24 21:35:55 +00:00
|
|
|
assert(frames1 > 0);
|
2000-06-21 14:59:20 +00:00
|
|
|
mmap_offset = snd_pcm_mmap_frames_offset(handle);
|
|
|
|
|
snd_pcm_areas_copy(handle->channels, mmap_offset, channels, offset, handle->setup.format.channels, frames1, handle->setup.format.format);
|
2000-06-10 12:39:51 +00:00
|
|
|
if (status->state == SND_PCM_STATE_XRUN &&
|
2000-06-21 14:59:20 +00:00
|
|
|
handle->setup.xrun_mode == SND_PCM_XRUN_DRAIN)
|
2000-05-16 15:20:34 +00:00
|
|
|
return result > 0 ? result : -EPIPE;
|
2000-06-21 14:59:20 +00:00
|
|
|
snd_pcm_frame_data(handle, frames1);
|
2000-05-24 21:35:55 +00:00
|
|
|
frames -= frames1;
|
|
|
|
|
offset += frames1;
|
|
|
|
|
result += frames1;
|
2000-05-08 18:53:38 +00:00
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2000-06-21 14:59:20 +00:00
|
|
|
ssize_t snd_pcm_mmap_read(snd_pcm_t *handle, void *buffer, size_t frames)
|
2000-05-08 18:53:38 +00:00
|
|
|
{
|
2000-05-27 16:52:17 +00:00
|
|
|
unsigned int nchannels;
|
2000-06-21 14:59:20 +00:00
|
|
|
assert(handle);
|
|
|
|
|
assert(handle->mmap_data && handle->mmap_status && handle->mmap_control);
|
2000-06-04 16:24:04 +00:00
|
|
|
assert(frames == 0 || buffer);
|
2000-06-21 14:59:20 +00:00
|
|
|
nchannels = handle->setup.format.channels;
|
|
|
|
|
assert(handle->setup.format.interleave || nchannels == 1);
|
2000-05-16 15:20:34 +00:00
|
|
|
{
|
2000-05-27 16:52:17 +00:00
|
|
|
snd_pcm_channel_area_t channels[nchannels];
|
|
|
|
|
unsigned int channel;
|
|
|
|
|
for (channel = 0; channel < nchannels; ++channel) {
|
|
|
|
|
channels[channel].addr = (char*)buffer;
|
2000-06-21 14:59:20 +00:00
|
|
|
channels[channel].first = handle->bits_per_sample * channel;
|
|
|
|
|
channels[channel].step = handle->bits_per_frame;
|
2000-05-08 18:53:38 +00:00
|
|
|
}
|
2000-06-21 14:59:20 +00:00
|
|
|
return snd_pcm_mmap_read_areas(handle, channels, frames);
|
2000-05-08 18:53:38 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2000-06-21 14:59:20 +00:00
|
|
|
ssize_t snd_pcm_mmap_readv(snd_pcm_t *handle, const struct iovec *vector, unsigned long vcount)
|
2000-05-08 18:53:38 +00:00
|
|
|
{
|
|
|
|
|
size_t result = 0;
|
2000-05-27 16:52:17 +00:00
|
|
|
unsigned int nchannels;
|
2000-06-21 14:59:20 +00:00
|
|
|
assert(handle);
|
|
|
|
|
assert(handle->mmap_data && handle->mmap_status && handle->mmap_control);
|
2000-06-04 16:24:04 +00:00
|
|
|
assert(vcount == 0 || vector);
|
2000-06-21 14:59:20 +00:00
|
|
|
nchannels = handle->setup.format.channels;
|
|
|
|
|
if (handle->setup.format.interleave) {
|
2000-05-16 15:20:34 +00:00
|
|
|
unsigned int b;
|
2000-05-08 18:53:38 +00:00
|
|
|
for (b = 0; b < vcount; b++) {
|
2000-05-16 15:20:34 +00:00
|
|
|
ssize_t ret;
|
2000-06-10 12:39:51 +00:00
|
|
|
size_t frames = vector[b].iov_len;
|
2000-06-21 14:59:20 +00:00
|
|
|
ret = snd_pcm_mmap_read(handle, vector[b].iov_base, frames);
|
2000-05-16 15:20:34 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
|
if (result <= 0)
|
|
|
|
|
return ret;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2000-05-08 18:53:38 +00:00
|
|
|
result += ret;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2000-05-27 16:52:17 +00:00
|
|
|
snd_pcm_channel_area_t channels[nchannels];
|
2000-05-08 18:53:38 +00:00
|
|
|
unsigned long bcount;
|
2000-05-16 15:20:34 +00:00
|
|
|
unsigned int b;
|
2000-06-04 16:24:04 +00:00
|
|
|
assert(vcount % nchannels == 0);
|
2000-05-27 16:52:17 +00:00
|
|
|
bcount = vcount / nchannels;
|
2000-05-08 18:53:38 +00:00
|
|
|
for (b = 0; b < bcount; b++) {
|
|
|
|
|
unsigned int v;
|
2000-05-16 15:20:34 +00:00
|
|
|
ssize_t ret;
|
2000-06-10 12:39:51 +00:00
|
|
|
size_t frames = vector[0].iov_len;
|
2000-05-27 16:52:17 +00:00
|
|
|
for (v = 0; v < nchannels; ++v) {
|
2000-06-10 12:39:51 +00:00
|
|
|
assert(vector[v].iov_len == frames);
|
2000-05-27 16:52:17 +00:00
|
|
|
channels[v].addr = vector[v].iov_base;
|
|
|
|
|
channels[v].first = 0;
|
2000-06-21 14:59:20 +00:00
|
|
|
channels[v].step = handle->bits_per_sample;
|
2000-05-16 15:20:34 +00:00
|
|
|
}
|
2000-06-21 14:59:20 +00:00
|
|
|
ret = snd_pcm_mmap_read_areas(handle, channels, frames);
|
2000-05-16 15:20:34 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
|
if (result <= 0)
|
|
|
|
|
return ret;
|
|
|
|
|
break;
|
2000-05-08 18:53:38 +00:00
|
|
|
}
|
|
|
|
|
result += ret;
|
2000-05-24 21:35:55 +00:00
|
|
|
if ((size_t)ret != frames)
|
2000-05-08 18:53:38 +00:00
|
|
|
break;
|
2000-05-27 16:52:17 +00:00
|
|
|
vector += nchannels;
|
2000-05-08 18:53:38 +00:00
|
|
|
}
|
|
|
|
|
}
|
2000-06-10 12:39:51 +00:00
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2000-06-21 14:59:20 +00:00
|
|
|
int snd_pcm_mmap_status(snd_pcm_t *handle, snd_pcm_mmap_status_t **status)
|
2000-06-10 12:39:51 +00:00
|
|
|
{
|
|
|
|
|
int err;
|
2000-06-21 14:59:20 +00:00
|
|
|
assert(handle);
|
|
|
|
|
assert(handle->valid_setup);
|
|
|
|
|
if (handle->mmap_status) {
|
2000-06-10 12:39:51 +00:00
|
|
|
if (status)
|
2000-06-21 14:59:20 +00:00
|
|
|
*status = handle->mmap_status;
|
2000-06-10 12:39:51 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2000-07-19 17:21:15 +00:00
|
|
|
if ((err = handle->fast_ops->mmap_status(handle->fast_op_arg, &handle->mmap_status)) < 0)
|
2000-06-10 12:39:51 +00:00
|
|
|
return err;
|
|
|
|
|
if (status)
|
2000-06-21 14:59:20 +00:00
|
|
|
*status = handle->mmap_status;
|
2000-06-10 12:39:51 +00:00
|
|
|
return 0;
|
2000-05-08 18:53:38 +00:00
|
|
|
}
|
|
|
|
|
|
2000-06-21 14:59:20 +00:00
|
|
|
int snd_pcm_mmap_control(snd_pcm_t *handle, snd_pcm_mmap_control_t **control)
|
2000-05-08 18:53:38 +00:00
|
|
|
{
|
|
|
|
|
int err;
|
2000-06-21 14:59:20 +00:00
|
|
|
assert(handle);
|
|
|
|
|
assert(handle->valid_setup);
|
|
|
|
|
if (handle->mmap_control) {
|
2000-06-04 16:24:04 +00:00
|
|
|
if (control)
|
2000-06-21 14:59:20 +00:00
|
|
|
*control = handle->mmap_control;
|
2000-05-08 18:53:38 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2000-07-19 17:21:15 +00:00
|
|
|
if ((err = handle->fast_ops->mmap_control(handle->fast_op_arg, &handle->mmap_control)) < 0)
|
2000-05-08 18:53:38 +00:00
|
|
|
return err;
|
2000-06-04 16:24:04 +00:00
|
|
|
if (control)
|
2000-06-21 14:59:20 +00:00
|
|
|
*control = handle->mmap_control;
|
2000-05-08 18:53:38 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2000-06-21 14:59:20 +00:00
|
|
|
int snd_pcm_mmap_get_areas(snd_pcm_t *handle, snd_pcm_channel_area_t *areas)
|
2000-05-16 15:20:34 +00:00
|
|
|
{
|
2000-05-27 16:52:17 +00:00
|
|
|
snd_pcm_channel_setup_t s;
|
|
|
|
|
snd_pcm_channel_area_t *a, *ap;
|
|
|
|
|
unsigned int channel;
|
2000-06-04 13:13:01 +00:00
|
|
|
int interleaved = 1, noninterleaved = 1;
|
2000-05-16 15:20:34 +00:00
|
|
|
int err;
|
2000-06-21 14:59:20 +00:00
|
|
|
assert(handle);
|
|
|
|
|
assert(handle->mmap_data);
|
|
|
|
|
a = calloc(handle->setup.format.channels, sizeof(*areas));
|
|
|
|
|
for (channel = 0, ap = a; channel < handle->setup.format.channels; ++channel, ++ap) {
|
2000-05-27 16:52:17 +00:00
|
|
|
s.channel = channel;
|
2000-06-21 14:59:20 +00:00
|
|
|
err = snd_pcm_channel_setup(handle, &s);
|
2000-05-16 15:20:34 +00:00
|
|
|
if (err < 0) {
|
|
|
|
|
free(a);
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
if (areas)
|
2000-05-27 16:52:17 +00:00
|
|
|
areas[channel] = s.area;
|
2000-05-16 15:20:34 +00:00
|
|
|
*ap = s.area;
|
2000-06-21 14:59:20 +00:00
|
|
|
if (ap->step != handle->bits_per_sample || ap->first != 0)
|
2000-06-04 13:13:01 +00:00
|
|
|
noninterleaved = 0;
|
|
|
|
|
if (ap->addr != a[0].addr ||
|
2000-06-21 14:59:20 +00:00
|
|
|
ap->step != handle->bits_per_frame ||
|
|
|
|
|
ap->first != channel * handle->bits_per_sample)
|
2000-06-04 13:13:01 +00:00
|
|
|
interleaved = 0;
|
2000-05-16 15:20:34 +00:00
|
|
|
}
|
2000-06-04 13:13:01 +00:00
|
|
|
if (noninterleaved)
|
2000-06-21 14:59:20 +00:00
|
|
|
handle->mmap_type = _NONINTERLEAVED;
|
2000-06-04 13:13:01 +00:00
|
|
|
else if (interleaved)
|
2000-06-21 14:59:20 +00:00
|
|
|
handle->mmap_type = _INTERLEAVED;
|
2000-06-04 13:13:01 +00:00
|
|
|
else
|
2000-06-21 14:59:20 +00:00
|
|
|
handle->mmap_type = _COMPLEX;
|
|
|
|
|
handle->channels = a;
|
2000-05-16 15:20:34 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2000-06-21 14:59:20 +00:00
|
|
|
int snd_pcm_mmap_data(snd_pcm_t *handle, void **data)
|
2000-05-08 18:53:38 +00:00
|
|
|
{
|
|
|
|
|
int err;
|
2000-06-21 14:59:20 +00:00
|
|
|
assert(handle);
|
|
|
|
|
assert(handle->valid_setup);
|
|
|
|
|
if (handle->mmap_data) {
|
2000-06-04 16:24:04 +00:00
|
|
|
if (data)
|
2000-06-21 14:59:20 +00:00
|
|
|
*data = handle->mmap_data;
|
2000-05-08 18:53:38 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2000-07-01 10:38:29 +00:00
|
|
|
if (handle->setup.mmap_bytes == 0)
|
2000-05-31 15:09:27 +00:00
|
|
|
return -ENXIO;
|
2000-07-19 17:21:15 +00:00
|
|
|
if ((err = handle->fast_ops->mmap_data(handle->fast_op_arg, (void**)&handle->mmap_data, handle->setup.mmap_bytes)) < 0)
|
2000-05-31 15:09:27 +00:00
|
|
|
return err;
|
2000-06-04 16:24:04 +00:00
|
|
|
if (data)
|
2000-06-21 14:59:20 +00:00
|
|
|
*data = handle->mmap_data;
|
|
|
|
|
err = snd_pcm_mmap_get_areas(handle, NULL);
|
2000-05-16 15:20:34 +00:00
|
|
|
if (err < 0)
|
|
|
|
|
return err;
|
2000-05-08 18:53:38 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2000-06-21 14:59:20 +00:00
|
|
|
int snd_pcm_mmap(snd_pcm_t *handle, snd_pcm_mmap_status_t **status, snd_pcm_mmap_control_t **control, void **data)
|
2000-05-08 18:53:38 +00:00
|
|
|
{
|
|
|
|
|
int err;
|
2000-06-21 14:59:20 +00:00
|
|
|
err = snd_pcm_mmap_status(handle, status);
|
2000-05-08 18:53:38 +00:00
|
|
|
if (err < 0)
|
|
|
|
|
return err;
|
2000-06-21 14:59:20 +00:00
|
|
|
err = snd_pcm_mmap_control(handle, control);
|
2000-06-10 12:39:51 +00:00
|
|
|
if (err < 0) {
|
2000-06-21 14:59:20 +00:00
|
|
|
snd_pcm_munmap_status(handle);
|
2000-06-10 12:39:51 +00:00
|
|
|
return err;
|
|
|
|
|
}
|
2000-06-21 14:59:20 +00:00
|
|
|
err = snd_pcm_mmap_data(handle, data);
|
2000-05-08 18:53:38 +00:00
|
|
|
if (err < 0) {
|
2000-06-21 14:59:20 +00:00
|
|
|
snd_pcm_munmap_status(handle);
|
|
|
|
|
snd_pcm_munmap_control(handle);
|
2000-05-08 18:53:38 +00:00
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2000-06-21 14:59:20 +00:00
|
|
|
int snd_pcm_munmap_status(snd_pcm_t *handle)
|
2000-06-10 12:39:51 +00:00
|
|
|
{
|
|
|
|
|
int err;
|
2000-06-21 14:59:20 +00:00
|
|
|
assert(handle);
|
|
|
|
|
assert(handle->mmap_status);
|
2000-07-19 17:21:15 +00:00
|
|
|
if ((err = handle->fast_ops->munmap_status(handle->fast_op_arg, handle->mmap_status)) < 0)
|
2000-06-10 12:39:51 +00:00
|
|
|
return err;
|
2000-06-21 14:59:20 +00:00
|
|
|
handle->mmap_status = 0;
|
2000-06-10 12:39:51 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2000-06-21 14:59:20 +00:00
|
|
|
int snd_pcm_munmap_control(snd_pcm_t *handle)
|
2000-05-08 18:53:38 +00:00
|
|
|
{
|
|
|
|
|
int err;
|
2000-06-21 14:59:20 +00:00
|
|
|
assert(handle);
|
|
|
|
|
assert(handle->mmap_control);
|
2000-07-19 17:21:15 +00:00
|
|
|
if ((err = handle->fast_ops->munmap_control(handle->fast_op_arg, handle->mmap_control)) < 0)
|
2000-05-31 15:09:27 +00:00
|
|
|
return err;
|
2000-06-21 14:59:20 +00:00
|
|
|
handle->mmap_control = 0;
|
2000-05-08 18:53:38 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2000-06-21 14:59:20 +00:00
|
|
|
int snd_pcm_munmap_data(snd_pcm_t *handle)
|
2000-05-08 18:53:38 +00:00
|
|
|
{
|
|
|
|
|
int err;
|
2000-06-21 14:59:20 +00:00
|
|
|
assert(handle);
|
|
|
|
|
assert(handle->mmap_data);
|
2000-07-19 17:21:15 +00:00
|
|
|
if ((err = handle->fast_ops->munmap_data(handle->fast_op_arg, handle->mmap_data, handle->setup.mmap_bytes)) < 0)
|
2000-05-31 15:09:27 +00:00
|
|
|
return err;
|
2000-06-21 14:59:20 +00:00
|
|
|
free(handle->channels);
|
|
|
|
|
handle->channels = 0;
|
|
|
|
|
handle->mmap_data = 0;
|
2000-05-08 18:53:38 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2000-06-21 14:59:20 +00:00
|
|
|
int snd_pcm_munmap(snd_pcm_t *handle)
|
2000-05-08 18:53:38 +00:00
|
|
|
{
|
|
|
|
|
int err;
|
2000-06-21 14:59:20 +00:00
|
|
|
err = snd_pcm_munmap_status(handle);
|
2000-06-10 12:39:51 +00:00
|
|
|
if (err < 0)
|
|
|
|
|
return err;
|
2000-06-21 14:59:20 +00:00
|
|
|
err = snd_pcm_munmap_control(handle);
|
2000-05-08 18:53:38 +00:00
|
|
|
if (err < 0)
|
|
|
|
|
return err;
|
2000-06-21 14:59:20 +00:00
|
|
|
return snd_pcm_munmap_data(handle);
|
2000-05-08 18:53:38 +00:00
|
|
|
}
|
|
|
|
|
|