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-04 13:13:01 +00:00
|
|
|
static ssize_t snd_pcm_mmap_playback_frames_avail(snd_pcm_t *pcm)
|
2000-05-08 18:53:38 +00:00
|
|
|
{
|
2000-06-01 21:58:25 +00:00
|
|
|
snd_pcm_stream_t *str = &pcm->stream[SND_PCM_STREAM_PLAYBACK];
|
2000-06-04 13:13:01 +00:00
|
|
|
ssize_t bytes = snd_pcm_mmap_playback_bytes_avail(str);
|
2000-05-27 16:52:17 +00:00
|
|
|
return bytes * 8 / str->bits_per_frame;
|
2000-05-08 18:53:38 +00:00
|
|
|
}
|
|
|
|
|
|
2000-06-04 13:13:01 +00:00
|
|
|
static size_t snd_pcm_mmap_capture_frames_avail(snd_pcm_t *pcm)
|
2000-05-08 18:53:38 +00:00
|
|
|
{
|
2000-06-01 21:58:25 +00:00
|
|
|
snd_pcm_stream_t *str = &pcm->stream[SND_PCM_STREAM_CAPTURE];
|
2000-06-04 13:13:01 +00:00
|
|
|
size_t bytes = snd_pcm_mmap_capture_bytes_avail(str);
|
2000-05-27 16:52:17 +00:00
|
|
|
return bytes * 8 / str->bits_per_frame;
|
2000-05-08 18:53:38 +00:00
|
|
|
}
|
|
|
|
|
|
2000-06-04 13:13:01 +00:00
|
|
|
int snd_pcm_frames_avail(snd_pcm_t *pcm, int stream, ssize_t *frames)
|
2000-05-08 18:53:38 +00:00
|
|
|
{
|
2000-06-01 21:58:25 +00:00
|
|
|
snd_pcm_stream_t *str;
|
2000-06-04 16:24:04 +00:00
|
|
|
assert(pcm);
|
|
|
|
|
assert(stream >= 0 && stream <= 1);
|
2000-05-27 16:52:17 +00:00
|
|
|
str = &pcm->stream[stream];
|
2000-06-04 16:24:04 +00:00
|
|
|
assert(str->mmap_control);
|
2000-05-27 16:52:17 +00:00
|
|
|
if (stream == SND_PCM_STREAM_PLAYBACK)
|
2000-06-04 13:13:01 +00:00
|
|
|
*frames = snd_pcm_mmap_playback_frames_avail(pcm);
|
2000-05-08 18:53:38 +00:00
|
|
|
else
|
2000-06-04 13:13:01 +00:00
|
|
|
*frames = snd_pcm_mmap_capture_frames_avail(pcm);
|
2000-05-08 18:53:38 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int snd_pcm_mmap_playback_ready(snd_pcm_t *pcm)
|
|
|
|
|
{
|
2000-06-01 21:58:25 +00:00
|
|
|
snd_pcm_stream_t *str;
|
2000-05-27 16:52:17 +00:00
|
|
|
str = &pcm->stream[SND_PCM_STREAM_PLAYBACK];
|
2000-06-05 11:38:02 +00:00
|
|
|
if (str->mmap_control->state == SND_PCM_STATE_XRUN)
|
2000-05-08 18:53:38 +00:00
|
|
|
return -EPIPE;
|
2000-06-04 13:13:01 +00:00
|
|
|
return snd_pcm_mmap_playback_bytes_avail(str) >= str->setup.bytes_min;
|
2000-05-08 18:53:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int snd_pcm_mmap_capture_ready(snd_pcm_t *pcm)
|
|
|
|
|
{
|
2000-06-01 21:58:25 +00:00
|
|
|
snd_pcm_stream_t *str;
|
2000-05-08 18:53:38 +00:00
|
|
|
int ret = 0;
|
2000-05-27 16:52:17 +00:00
|
|
|
str = &pcm->stream[SND_PCM_STREAM_CAPTURE];
|
2000-06-05 11:38:02 +00:00
|
|
|
if (str->mmap_control->state == SND_PCM_STATE_XRUN) {
|
2000-05-08 18:53:38 +00:00
|
|
|
ret = -EPIPE;
|
2000-05-27 16:52:17 +00:00
|
|
|
if (str->setup.xrun_mode == SND_PCM_XRUN_DRAIN)
|
2000-05-08 18:53:38 +00:00
|
|
|
return -EPIPE;
|
|
|
|
|
}
|
2000-06-04 13:13:01 +00:00
|
|
|
if (snd_pcm_mmap_capture_bytes_avail(str) >= str->setup.bytes_min)
|
2000-05-23 12:52:06 +00:00
|
|
|
return 1;
|
2000-05-08 18:53:38 +00:00
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2000-05-27 16:52:17 +00:00
|
|
|
int snd_pcm_mmap_ready(snd_pcm_t *pcm, int stream)
|
2000-05-08 18:53:38 +00:00
|
|
|
{
|
2000-06-01 21:58:25 +00:00
|
|
|
snd_pcm_stream_t *str;
|
2000-05-08 18:53:38 +00:00
|
|
|
snd_pcm_mmap_control_t *ctrl;
|
2000-06-04 16:24:04 +00:00
|
|
|
assert(pcm);
|
|
|
|
|
assert(stream >= 0 && stream <= 1);
|
2000-05-27 16:52:17 +00:00
|
|
|
str = &pcm->stream[stream];
|
|
|
|
|
ctrl = str->mmap_control;
|
2000-06-04 16:24:04 +00:00
|
|
|
assert(ctrl);
|
2000-06-05 11:38:02 +00:00
|
|
|
assert(ctrl->state >= SND_PCM_STATE_PREPARED);
|
2000-05-27 16:52:17 +00:00
|
|
|
if (stream == SND_PCM_STREAM_PLAYBACK) {
|
2000-05-08 18:53:38 +00:00
|
|
|
return snd_pcm_mmap_playback_ready(pcm);
|
|
|
|
|
} else {
|
|
|
|
|
return snd_pcm_mmap_capture_ready(pcm);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2000-05-16 15:20:34 +00:00
|
|
|
static size_t snd_pcm_mmap_playback_bytes_xfer(snd_pcm_t *pcm, size_t bytes)
|
|
|
|
|
{
|
2000-06-01 21:58:25 +00:00
|
|
|
snd_pcm_stream_t *str = &pcm->stream[SND_PCM_STREAM_PLAYBACK];
|
2000-05-27 16:52:17 +00:00
|
|
|
snd_pcm_mmap_control_t *ctrl = str->mmap_control;
|
2000-05-08 18:53:38 +00:00
|
|
|
size_t bytes_cont;
|
2000-06-04 13:13:01 +00:00
|
|
|
size_t bytes_avail = snd_pcm_mmap_playback_bytes_avail(str);
|
|
|
|
|
if (bytes_avail < bytes)
|
|
|
|
|
bytes = bytes_avail;
|
|
|
|
|
bytes_cont = str->setup.buffer_size - ctrl->byte_data % str->setup.buffer_size;
|
2000-05-16 15:20:34 +00:00
|
|
|
if (bytes_cont < bytes)
|
|
|
|
|
bytes = bytes_cont;
|
|
|
|
|
return bytes;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static size_t snd_pcm_mmap_capture_bytes_xfer(snd_pcm_t *pcm, size_t bytes)
|
|
|
|
|
{
|
2000-06-01 21:58:25 +00:00
|
|
|
snd_pcm_stream_t *str = &pcm->stream[SND_PCM_STREAM_CAPTURE];
|
2000-05-27 16:52:17 +00:00
|
|
|
snd_pcm_mmap_control_t *ctrl = str->mmap_control;
|
2000-05-16 15:20:34 +00:00
|
|
|
size_t bytes_cont;
|
2000-06-04 13:13:01 +00:00
|
|
|
size_t bytes_avail = snd_pcm_mmap_capture_bytes_avail(str);
|
|
|
|
|
if (bytes_avail < bytes)
|
|
|
|
|
bytes = bytes_avail;
|
|
|
|
|
bytes_cont = str->setup.buffer_size - ctrl->byte_data % str->setup.buffer_size;
|
2000-05-16 15:20:34 +00:00
|
|
|
if (bytes_cont < bytes)
|
|
|
|
|
bytes = bytes_cont;
|
|
|
|
|
return bytes;
|
2000-05-08 18:53:38 +00:00
|
|
|
}
|
|
|
|
|
|
2000-05-24 21:35:55 +00:00
|
|
|
static ssize_t snd_pcm_mmap_playback_frames_xfer(snd_pcm_t *pcm, size_t frames)
|
2000-05-16 15:20:34 +00:00
|
|
|
{
|
2000-06-01 21:58:25 +00:00
|
|
|
snd_pcm_stream_t *str = &pcm->stream[SND_PCM_STREAM_PLAYBACK];
|
2000-05-27 16:52:17 +00:00
|
|
|
size_t bytes = frames * str->bits_per_frame / 8;
|
2000-05-23 12:52:06 +00:00
|
|
|
bytes = snd_pcm_mmap_playback_bytes_xfer(pcm, bytes);
|
2000-05-27 16:52:17 +00:00
|
|
|
return bytes * 8 / str->bits_per_frame;
|
2000-05-16 15:20:34 +00:00
|
|
|
}
|
|
|
|
|
|
2000-05-24 21:35:55 +00:00
|
|
|
static ssize_t snd_pcm_mmap_capture_frames_xfer(snd_pcm_t *pcm, size_t frames)
|
2000-05-16 15:20:34 +00:00
|
|
|
{
|
2000-06-01 21:58:25 +00:00
|
|
|
snd_pcm_stream_t *str = &pcm->stream[SND_PCM_STREAM_CAPTURE];
|
2000-05-27 16:52:17 +00:00
|
|
|
size_t bytes = frames * str->bits_per_frame / 8;
|
2000-05-23 12:52:06 +00:00
|
|
|
bytes = snd_pcm_mmap_capture_bytes_xfer(pcm, bytes);
|
2000-05-27 16:52:17 +00:00
|
|
|
return bytes * 8 / str->bits_per_frame;
|
2000-05-16 15:20:34 +00:00
|
|
|
}
|
|
|
|
|
|
2000-05-27 16:52:17 +00:00
|
|
|
ssize_t snd_pcm_mmap_frames_xfer(snd_pcm_t *pcm, int stream, size_t frames)
|
2000-05-16 15:20:34 +00:00
|
|
|
{
|
2000-06-01 21:58:25 +00:00
|
|
|
snd_pcm_stream_t *str;
|
2000-06-04 16:24:04 +00:00
|
|
|
assert(pcm);
|
|
|
|
|
assert(stream >= 0 && stream <= 1);
|
2000-05-27 16:52:17 +00:00
|
|
|
str = &pcm->stream[stream];
|
2000-06-04 16:24:04 +00:00
|
|
|
assert(str->mmap_control);
|
2000-05-27 16:52:17 +00:00
|
|
|
if (stream == SND_PCM_STREAM_PLAYBACK)
|
2000-05-24 21:35:55 +00:00
|
|
|
return snd_pcm_mmap_playback_frames_xfer(pcm, frames);
|
2000-05-16 15:20:34 +00:00
|
|
|
else
|
2000-05-24 21:35:55 +00:00
|
|
|
return snd_pcm_mmap_capture_frames_xfer(pcm, frames);
|
2000-05-16 15:20:34 +00:00
|
|
|
}
|
|
|
|
|
|
2000-05-27 16:52:17 +00:00
|
|
|
ssize_t snd_pcm_mmap_frames_offset(snd_pcm_t *pcm, int stream)
|
2000-05-16 15:20:34 +00:00
|
|
|
{
|
2000-06-01 21:58:25 +00:00
|
|
|
snd_pcm_stream_t *str;
|
2000-05-16 15:20:34 +00:00
|
|
|
snd_pcm_mmap_control_t *ctrl;
|
2000-06-04 16:24:04 +00:00
|
|
|
assert(pcm);
|
|
|
|
|
assert(stream >= 0 && stream <= 1);
|
2000-05-27 16:52:17 +00:00
|
|
|
str = &pcm->stream[stream];
|
|
|
|
|
ctrl = str->mmap_control;
|
2000-06-04 16:24:04 +00:00
|
|
|
assert(ctrl);
|
2000-05-27 16:52:17 +00:00
|
|
|
return (ctrl->byte_data % str->setup.buffer_size) * 8 / str->bits_per_frame;
|
2000-05-16 15:20:34 +00:00
|
|
|
}
|
2000-05-08 18:53:38 +00:00
|
|
|
|
2000-06-04 13:13:01 +00:00
|
|
|
int snd_pcm_mmap_stream_state(snd_pcm_t *pcm, int stream)
|
2000-05-16 15:20:34 +00:00
|
|
|
{
|
2000-06-01 21:58:25 +00:00
|
|
|
snd_pcm_stream_t *str;
|
2000-06-04 13:13:01 +00:00
|
|
|
assert(pcm);
|
|
|
|
|
assert(stream >= 0 && stream <= 1);
|
2000-05-27 16:52:17 +00:00
|
|
|
str = &pcm->stream[stream];
|
2000-06-04 13:13:01 +00:00
|
|
|
assert(str->mmap_control);
|
2000-06-05 11:38:02 +00:00
|
|
|
return str->mmap_control->state;
|
2000-06-04 13:13:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int snd_pcm_mmap_stream_byte_io(snd_pcm_t *pcm, int stream)
|
|
|
|
|
{
|
|
|
|
|
snd_pcm_stream_t *str;
|
|
|
|
|
assert(pcm);
|
|
|
|
|
assert(stream >= 0 && stream <= 1);
|
|
|
|
|
str = &pcm->stream[stream];
|
|
|
|
|
assert(str->mmap_control);
|
|
|
|
|
return str->mmap_control->byte_io;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int snd_pcm_mmap_stream_byte_data(snd_pcm_t *pcm, int stream)
|
|
|
|
|
{
|
|
|
|
|
snd_pcm_stream_t *str;
|
|
|
|
|
assert(pcm);
|
|
|
|
|
assert(stream >= 0 && stream <= 1);
|
|
|
|
|
str = &pcm->stream[stream];
|
|
|
|
|
assert(str->mmap_control);
|
|
|
|
|
return str->mmap_control->byte_data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ssize_t snd_pcm_mmap_stream_seek(snd_pcm_t *pcm, int stream, off_t offset)
|
|
|
|
|
{
|
|
|
|
|
snd_pcm_stream_t *str;
|
|
|
|
|
ssize_t byte_data;
|
|
|
|
|
assert(pcm);
|
|
|
|
|
assert(stream >= 0 && stream <= 1);
|
|
|
|
|
str = &pcm->stream[stream];
|
|
|
|
|
assert(str->mmap_control);
|
|
|
|
|
byte_data = str->mmap_control->byte_data;
|
|
|
|
|
if (offset == 0)
|
|
|
|
|
return byte_data;
|
2000-06-05 11:38:02 +00:00
|
|
|
switch (str->mmap_control->state) {
|
|
|
|
|
case SND_PCM_STATE_RUNNING:
|
2000-06-04 13:13:01 +00:00
|
|
|
if (str->setup.mode == SND_PCM_MODE_FRAME)
|
|
|
|
|
snd_pcm_stream_byte_io(pcm, stream, 1);
|
|
|
|
|
break;
|
2000-06-05 11:38:02 +00:00
|
|
|
case SND_PCM_STATE_PREPARED:
|
2000-06-04 13:13:01 +00:00
|
|
|
break;
|
|
|
|
|
default:
|
2000-05-16 15:20:34 +00:00
|
|
|
return -EBADFD;
|
2000-06-04 13:13:01 +00:00
|
|
|
}
|
|
|
|
|
if (offset < 0) {
|
|
|
|
|
if (offset < -(ssize_t)str->setup.buffer_size)
|
|
|
|
|
offset = -(ssize_t)str->setup.buffer_size;
|
|
|
|
|
else
|
|
|
|
|
offset -= offset % str->setup.bytes_align;
|
|
|
|
|
byte_data += offset;
|
|
|
|
|
if (byte_data < 0)
|
|
|
|
|
byte_data += str->setup.byte_boundary;
|
2000-05-16 15:20:34 +00:00
|
|
|
} else {
|
2000-06-04 13:13:01 +00:00
|
|
|
size_t bytes_avail;
|
|
|
|
|
if (stream == SND_PCM_STREAM_PLAYBACK)
|
|
|
|
|
bytes_avail = snd_pcm_mmap_playback_bytes_avail(str);
|
|
|
|
|
else
|
|
|
|
|
bytes_avail = snd_pcm_mmap_capture_bytes_avail(str);
|
|
|
|
|
if ((size_t)offset > bytes_avail)
|
|
|
|
|
offset = bytes_avail;
|
|
|
|
|
offset -= offset % str->setup.bytes_align;
|
|
|
|
|
byte_data += offset;
|
|
|
|
|
if ((size_t)byte_data >= str->setup.byte_boundary)
|
|
|
|
|
byte_data -= str->setup.byte_boundary;
|
2000-05-16 15:20:34 +00:00
|
|
|
}
|
2000-06-04 13:13:01 +00:00
|
|
|
str->mmap_control->byte_data = byte_data;
|
|
|
|
|
return byte_data;
|
2000-05-16 15:20:34 +00:00
|
|
|
}
|
2000-05-08 18:53:38 +00:00
|
|
|
|
2000-05-27 16:52:17 +00:00
|
|
|
ssize_t snd_pcm_mmap_write_areas(snd_pcm_t *pcm, snd_pcm_channel_area_t *channels, size_t frames)
|
2000-05-08 18:53:38 +00:00
|
|
|
{
|
2000-06-01 21:58:25 +00:00
|
|
|
snd_pcm_stream_t *str;
|
2000-05-08 18:53:38 +00:00
|
|
|
snd_pcm_mmap_control_t *ctrl;
|
|
|
|
|
size_t offset = 0;
|
|
|
|
|
size_t result = 0;
|
|
|
|
|
int err;
|
|
|
|
|
|
2000-05-27 16:52:17 +00:00
|
|
|
str = &pcm->stream[SND_PCM_STREAM_PLAYBACK];
|
|
|
|
|
ctrl = str->mmap_control;
|
2000-06-05 11:38:02 +00:00
|
|
|
assert(ctrl->state >= SND_PCM_STATE_PREPARED);
|
2000-05-27 16:52:17 +00:00
|
|
|
if (str->setup.mode == SND_PCM_MODE_FRAGMENT) {
|
2000-06-04 16:24:04 +00:00
|
|
|
assert(frames % str->frames_per_frag == 0);
|
2000-05-08 18:53:38 +00:00
|
|
|
} else {
|
2000-06-05 11:38:02 +00:00
|
|
|
if (ctrl->state == SND_PCM_STATE_RUNNING &&
|
2000-05-27 16:52:17 +00:00
|
|
|
str->mode & SND_PCM_NONBLOCK)
|
2000-06-01 21:58:25 +00:00
|
|
|
snd_pcm_stream_byte_io(pcm, SND_PCM_STREAM_PLAYBACK, 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-05-08 18:53:38 +00:00
|
|
|
int ready = snd_pcm_mmap_playback_ready(pcm);
|
|
|
|
|
if (ready < 0)
|
|
|
|
|
return ready;
|
|
|
|
|
if (!ready) {
|
|
|
|
|
struct pollfd pfd;
|
2000-06-05 11:38:02 +00:00
|
|
|
if (ctrl->state != SND_PCM_STATE_RUNNING)
|
2000-05-08 18:53:38 +00:00
|
|
|
return result > 0 ? result : -EPIPE;
|
2000-05-27 16:52:17 +00:00
|
|
|
if (str->mode & SND_PCM_NONBLOCK)
|
2000-05-08 18:53:38 +00:00
|
|
|
return result > 0 ? result : -EAGAIN;
|
2000-05-27 16:52:17 +00:00
|
|
|
pfd.fd = snd_pcm_file_descriptor(pcm, SND_PCM_STREAM_PLAYBACK);
|
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;
|
|
|
|
|
assert(snd_pcm_mmap_playback_ready(pcm));
|
|
|
|
|
}
|
2000-05-24 21:35:55 +00:00
|
|
|
frames1 = snd_pcm_mmap_playback_frames_xfer(pcm, frames);
|
|
|
|
|
assert(frames1 > 0);
|
2000-05-27 16:52:17 +00:00
|
|
|
mmap_offset = snd_pcm_mmap_frames_offset(pcm, SND_PCM_STREAM_PLAYBACK);
|
|
|
|
|
snd_pcm_areas_copy(channels, offset, str->channels, mmap_offset, str->setup.format.channels, frames1, str->setup.format.format);
|
2000-06-05 11:38:02 +00:00
|
|
|
if (ctrl->state == SND_PCM_STATE_XRUN)
|
2000-05-16 15:20:34 +00:00
|
|
|
return result > 0 ? result : -EPIPE;
|
2000-06-04 13:13:01 +00:00
|
|
|
snd_pcm_stream_seek(pcm, SND_PCM_STREAM_PLAYBACK, frames1 * str->bits_per_frame / 8);
|
2000-05-24 21:35:55 +00:00
|
|
|
frames -= frames1;
|
|
|
|
|
offset += frames1;
|
|
|
|
|
result += frames1;
|
2000-06-05 11:38:02 +00:00
|
|
|
if (ctrl->state == SND_PCM_STATE_PREPARED &&
|
2000-05-27 16:52:17 +00:00
|
|
|
(str->setup.start_mode == SND_PCM_START_DATA ||
|
|
|
|
|
(str->setup.start_mode == SND_PCM_START_FULL &&
|
2000-05-16 15:20:34 +00:00
|
|
|
!snd_pcm_mmap_playback_ready(pcm)))) {
|
2000-05-27 16:52:17 +00:00
|
|
|
err = snd_pcm_stream_go(pcm, SND_PCM_STREAM_PLAYBACK);
|
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-05-24 21:35:55 +00:00
|
|
|
ssize_t snd_pcm_mmap_write_frames(snd_pcm_t *pcm, const void *buffer, size_t frames)
|
2000-05-08 18:53:38 +00:00
|
|
|
{
|
2000-06-01 21:58:25 +00:00
|
|
|
snd_pcm_stream_t *str;
|
2000-05-27 16:52:17 +00:00
|
|
|
unsigned int nchannels;
|
2000-06-04 16:24:04 +00:00
|
|
|
assert(pcm);
|
2000-05-27 16:52:17 +00:00
|
|
|
str = &pcm->stream[SND_PCM_STREAM_PLAYBACK];
|
2000-06-04 16:24:04 +00:00
|
|
|
assert(str->mmap_data && str->mmap_control);
|
|
|
|
|
assert(frames == 0 || buffer);
|
2000-05-27 16:52:17 +00:00
|
|
|
nchannels = str->setup.format.channels;
|
2000-06-04 16:24:04 +00:00
|
|
|
assert(str->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;
|
|
|
|
|
channels[channel].first = str->sample_width * channel;
|
|
|
|
|
channels[channel].step = str->bits_per_frame;
|
2000-05-08 18:53:38 +00:00
|
|
|
}
|
2000-05-27 16:52:17 +00:00
|
|
|
return snd_pcm_mmap_write_areas(pcm, channels, frames);
|
2000-05-08 18:53:38 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2000-05-16 15:20:34 +00:00
|
|
|
ssize_t snd_pcm_mmap_write(snd_pcm_t *pcm, const void *buffer, size_t bytes)
|
2000-05-08 18:53:38 +00:00
|
|
|
{
|
2000-06-01 21:58:25 +00:00
|
|
|
snd_pcm_stream_t *str;
|
2000-05-27 16:52:17 +00:00
|
|
|
unsigned int nchannels;
|
2000-05-24 21:35:55 +00:00
|
|
|
ssize_t frames;
|
2000-06-04 16:24:04 +00:00
|
|
|
assert(pcm);
|
2000-05-27 16:52:17 +00:00
|
|
|
str = &pcm->stream[SND_PCM_STREAM_PLAYBACK];
|
2000-06-04 16:24:04 +00:00
|
|
|
assert(str->mmap_data && str->mmap_control);
|
|
|
|
|
assert(bytes == 0 || buffer);
|
2000-05-27 16:52:17 +00:00
|
|
|
nchannels = str->setup.format.channels;
|
2000-06-04 16:24:04 +00:00
|
|
|
assert(str->setup.format.interleave || nchannels == 1);
|
2000-05-27 16:52:17 +00:00
|
|
|
frames = bytes * 8 / str->bits_per_frame;
|
2000-05-24 21:35:55 +00:00
|
|
|
frames = snd_pcm_mmap_write_frames(pcm, buffer, frames);
|
|
|
|
|
if (frames <= 0)
|
|
|
|
|
return frames;
|
2000-05-27 16:52:17 +00:00
|
|
|
return frames * str->bits_per_frame / 8;
|
2000-05-08 18:53:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ssize_t snd_pcm_mmap_writev(snd_pcm_t *pcm, const struct iovec *vector, unsigned long vcount)
|
|
|
|
|
{
|
2000-06-01 21:58:25 +00:00
|
|
|
snd_pcm_stream_t *str;
|
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-04 16:24:04 +00:00
|
|
|
assert(pcm);
|
2000-05-27 16:52:17 +00:00
|
|
|
str = &pcm->stream[SND_PCM_STREAM_PLAYBACK];
|
2000-06-04 16:24:04 +00:00
|
|
|
assert(str->mmap_data && str->mmap_control);
|
|
|
|
|
assert(vcount == 0 || vector);
|
2000-05-27 16:52:17 +00:00
|
|
|
nchannels = str->setup.format.channels;
|
|
|
|
|
if (str->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-05-27 16:52:17 +00:00
|
|
|
size_t frames = vector[b].iov_len * 8 / str->bits_per_frame;
|
2000-05-24 21:35:55 +00:00
|
|
|
ret = snd_pcm_mmap_write_frames(pcm, 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;
|
|
|
|
|
size_t bytes = 0;
|
2000-05-24 21:35:55 +00:00
|
|
|
size_t frames;
|
2000-05-16 15:20:34 +00:00
|
|
|
bytes = vector[0].iov_len;
|
2000-05-27 16:52:17 +00:00
|
|
|
for (v = 0; v < nchannels; ++v) {
|
2000-06-04 16:24:04 +00:00
|
|
|
assert(vector[v].iov_len == bytes);
|
2000-05-27 16:52:17 +00:00
|
|
|
channels[v].addr = vector[v].iov_base;
|
|
|
|
|
channels[v].first = 0;
|
|
|
|
|
channels[v].step = str->sample_width;
|
2000-05-16 15:20:34 +00:00
|
|
|
}
|
2000-05-27 16:52:17 +00:00
|
|
|
frames = bytes * 8 / str->sample_width;
|
|
|
|
|
ret = snd_pcm_mmap_write_areas(pcm, 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-05-27 16:52:17 +00:00
|
|
|
return result * str->bits_per_frame / 8;
|
2000-05-08 18:53:38 +00:00
|
|
|
}
|
|
|
|
|
|
2000-05-27 16:52:17 +00:00
|
|
|
ssize_t snd_pcm_mmap_read_areas(snd_pcm_t *pcm, snd_pcm_channel_area_t *channels, size_t frames)
|
2000-05-08 18:53:38 +00:00
|
|
|
{
|
2000-06-01 21:58:25 +00:00
|
|
|
snd_pcm_stream_t *str;
|
2000-05-08 18:53:38 +00:00
|
|
|
snd_pcm_mmap_control_t *ctrl;
|
|
|
|
|
size_t offset = 0;
|
|
|
|
|
size_t result = 0;
|
|
|
|
|
int err;
|
|
|
|
|
|
2000-05-27 16:52:17 +00:00
|
|
|
str = &pcm->stream[SND_PCM_STREAM_CAPTURE];
|
|
|
|
|
ctrl = str->mmap_control;
|
2000-06-05 11:38:02 +00:00
|
|
|
assert(ctrl->state >= SND_PCM_STATE_PREPARED);
|
2000-05-27 16:52:17 +00:00
|
|
|
if (str->setup.mode == SND_PCM_MODE_FRAGMENT) {
|
2000-06-04 16:24:04 +00:00
|
|
|
assert(frames % str->frames_per_frag == 0);
|
2000-05-08 18:53:38 +00:00
|
|
|
} else {
|
2000-06-05 11:38:02 +00:00
|
|
|
if (ctrl->state == SND_PCM_STATE_RUNNING &&
|
2000-05-27 16:52:17 +00:00
|
|
|
str->mode & SND_PCM_NONBLOCK)
|
2000-06-01 21:58:25 +00:00
|
|
|
snd_pcm_stream_byte_io(pcm, SND_PCM_STREAM_CAPTURE, 1);
|
2000-05-08 18:53:38 +00:00
|
|
|
}
|
2000-06-05 11:38:02 +00:00
|
|
|
if (ctrl->state == SND_PCM_STATE_PREPARED &&
|
2000-05-27 16:52:17 +00:00
|
|
|
str->setup.start_mode == SND_PCM_START_DATA) {
|
|
|
|
|
err = snd_pcm_stream_go(pcm, SND_PCM_STREAM_CAPTURE);
|
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-05-08 18:53:38 +00:00
|
|
|
int ready = snd_pcm_mmap_capture_ready(pcm);
|
|
|
|
|
if (ready < 0)
|
|
|
|
|
return ready;
|
|
|
|
|
if (!ready) {
|
|
|
|
|
struct pollfd pfd;
|
2000-06-05 11:38:02 +00:00
|
|
|
if (ctrl->state != SND_PCM_STATE_RUNNING)
|
2000-05-08 18:53:38 +00:00
|
|
|
return result > 0 ? result : -EPIPE;
|
2000-05-27 16:52:17 +00:00
|
|
|
if (str->mode & SND_PCM_NONBLOCK)
|
2000-05-08 18:53:38 +00:00
|
|
|
return result > 0 ? result : -EAGAIN;
|
2000-05-27 16:52:17 +00:00
|
|
|
pfd.fd = snd_pcm_file_descriptor(pcm, SND_PCM_STREAM_CAPTURE);
|
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;
|
|
|
|
|
assert(snd_pcm_mmap_capture_ready(pcm));
|
|
|
|
|
}
|
2000-05-24 21:35:55 +00:00
|
|
|
frames1 = snd_pcm_mmap_capture_frames_xfer(pcm, frames);
|
|
|
|
|
assert(frames1 > 0);
|
2000-05-27 16:52:17 +00:00
|
|
|
mmap_offset = snd_pcm_mmap_frames_offset(pcm, SND_PCM_STREAM_CAPTURE);
|
|
|
|
|
snd_pcm_areas_copy(str->channels, mmap_offset, channels, offset, str->setup.format.channels, frames1, str->setup.format.format);
|
2000-06-05 11:38:02 +00:00
|
|
|
if (ctrl->state == SND_PCM_STATE_XRUN &&
|
2000-05-27 16:52:17 +00:00
|
|
|
str->setup.xrun_mode == SND_PCM_XRUN_DRAIN)
|
2000-05-16 15:20:34 +00:00
|
|
|
return result > 0 ? result : -EPIPE;
|
2000-06-04 13:13:01 +00:00
|
|
|
snd_pcm_stream_seek(pcm, SND_PCM_STREAM_CAPTURE, frames1 * str->bits_per_frame / 8);
|
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-05-24 21:35:55 +00:00
|
|
|
ssize_t snd_pcm_mmap_read_frames(snd_pcm_t *pcm, const void *buffer, size_t frames)
|
2000-05-08 18:53:38 +00:00
|
|
|
{
|
2000-06-01 21:58:25 +00:00
|
|
|
snd_pcm_stream_t *str;
|
2000-05-27 16:52:17 +00:00
|
|
|
unsigned int nchannels;
|
2000-06-04 16:24:04 +00:00
|
|
|
assert(pcm);
|
2000-05-27 16:52:17 +00:00
|
|
|
str = &pcm->stream[SND_PCM_STREAM_CAPTURE];
|
2000-06-04 16:24:04 +00:00
|
|
|
assert(str->mmap_data && str->mmap_control);
|
|
|
|
|
assert(frames == 0 || buffer);
|
2000-05-27 16:52:17 +00:00
|
|
|
nchannels = str->setup.format.channels;
|
2000-06-04 16:24:04 +00:00
|
|
|
assert(str->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;
|
|
|
|
|
channels[channel].first = str->sample_width * channel;
|
|
|
|
|
channels[channel].step = str->bits_per_frame;
|
2000-05-08 18:53:38 +00:00
|
|
|
}
|
2000-05-27 16:52:17 +00:00
|
|
|
return snd_pcm_mmap_read_areas(pcm, channels, frames);
|
2000-05-08 18:53:38 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2000-05-16 15:20:34 +00:00
|
|
|
ssize_t snd_pcm_mmap_read(snd_pcm_t *pcm, void *buffer, size_t bytes)
|
2000-05-08 18:53:38 +00:00
|
|
|
{
|
2000-06-01 21:58:25 +00:00
|
|
|
snd_pcm_stream_t *str;
|
2000-05-27 16:52:17 +00:00
|
|
|
unsigned int nchannels;
|
2000-05-24 21:35:55 +00:00
|
|
|
ssize_t frames;
|
2000-06-04 16:24:04 +00:00
|
|
|
assert(pcm);
|
2000-05-27 16:52:17 +00:00
|
|
|
str = &pcm->stream[SND_PCM_STREAM_CAPTURE];
|
2000-06-04 16:24:04 +00:00
|
|
|
assert(str->mmap_data && str->mmap_control);
|
|
|
|
|
assert(bytes == 0 || buffer);
|
2000-05-27 16:52:17 +00:00
|
|
|
nchannels = str->setup.format.channels;
|
2000-06-04 16:24:04 +00:00
|
|
|
assert(str->setup.format.interleave || nchannels == 1);
|
2000-05-27 16:52:17 +00:00
|
|
|
frames = bytes * 8 / str->bits_per_frame;
|
2000-05-24 21:35:55 +00:00
|
|
|
frames = snd_pcm_mmap_read_frames(pcm, buffer, frames);
|
|
|
|
|
if (frames <= 0)
|
|
|
|
|
return frames;
|
2000-05-27 16:52:17 +00:00
|
|
|
return frames * str->bits_per_frame / 8;
|
2000-05-08 18:53:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ssize_t snd_pcm_mmap_readv(snd_pcm_t *pcm, const struct iovec *vector, unsigned long vcount)
|
|
|
|
|
{
|
2000-06-01 21:58:25 +00:00
|
|
|
snd_pcm_stream_t *str;
|
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-04 16:24:04 +00:00
|
|
|
assert(pcm);
|
2000-05-27 16:52:17 +00:00
|
|
|
str = &pcm->stream[SND_PCM_STREAM_CAPTURE];
|
2000-06-04 16:24:04 +00:00
|
|
|
assert(str->mmap_data && str->mmap_control);
|
|
|
|
|
assert(vcount == 0 || vector);
|
2000-05-27 16:52:17 +00:00
|
|
|
nchannels = str->setup.format.channels;
|
|
|
|
|
if (str->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-05-27 16:52:17 +00:00
|
|
|
size_t frames = vector[b].iov_len * 8 / str->bits_per_frame;
|
2000-05-24 21:35:55 +00:00
|
|
|
ret = snd_pcm_mmap_read_frames(pcm, 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;
|
|
|
|
|
size_t bytes = 0;
|
2000-05-24 21:35:55 +00:00
|
|
|
size_t frames;
|
2000-05-16 15:20:34 +00:00
|
|
|
bytes = vector[0].iov_len;
|
2000-05-27 16:52:17 +00:00
|
|
|
for (v = 0; v < nchannels; ++v) {
|
2000-06-04 16:24:04 +00:00
|
|
|
assert(vector[v].iov_len == bytes);
|
2000-05-27 16:52:17 +00:00
|
|
|
channels[v].addr = vector[v].iov_base;
|
|
|
|
|
channels[v].first = 0;
|
|
|
|
|
channels[v].step = str->sample_width;
|
2000-05-16 15:20:34 +00:00
|
|
|
}
|
2000-05-27 16:52:17 +00:00
|
|
|
frames = bytes * 8 / str->sample_width;
|
|
|
|
|
ret = snd_pcm_mmap_read_areas(pcm, 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-05-27 16:52:17 +00:00
|
|
|
return result * str->bits_per_frame / 8;
|
2000-05-08 18:53:38 +00:00
|
|
|
}
|
|
|
|
|
|
2000-05-27 16:52:17 +00:00
|
|
|
int snd_pcm_mmap_control(snd_pcm_t *pcm, int stream, snd_pcm_mmap_control_t **control)
|
2000-05-08 18:53:38 +00:00
|
|
|
{
|
2000-06-01 21:58:25 +00:00
|
|
|
snd_pcm_stream_t *str;
|
2000-05-08 18:53:38 +00:00
|
|
|
size_t csize;
|
|
|
|
|
int err;
|
2000-06-04 16:24:04 +00:00
|
|
|
assert(pcm);
|
|
|
|
|
assert(stream >= 0 && stream <= 1);
|
2000-05-27 16:52:17 +00:00
|
|
|
str = &pcm->stream[stream];
|
2000-06-04 16:24:04 +00:00
|
|
|
assert(str->valid_setup);
|
2000-05-27 16:52:17 +00:00
|
|
|
if (str->mmap_control) {
|
2000-06-04 16:24:04 +00:00
|
|
|
if (control)
|
|
|
|
|
*control = str->mmap_control;
|
2000-05-08 18:53:38 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
csize = sizeof(snd_pcm_mmap_control_t);
|
|
|
|
|
|
2000-06-04 16:24:04 +00:00
|
|
|
if ((err = pcm->ops->mmap_control(pcm, stream, &str->mmap_control, csize)) < 0)
|
2000-05-08 18:53:38 +00:00
|
|
|
return err;
|
2000-06-04 16:24:04 +00:00
|
|
|
if (control)
|
|
|
|
|
*control = str->mmap_control;
|
2000-05-27 16:52:17 +00:00
|
|
|
str->mmap_control_size = csize;
|
2000-05-08 18:53:38 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2000-05-27 16:52:17 +00:00
|
|
|
int snd_pcm_mmap_get_areas(snd_pcm_t *pcm, int stream, snd_pcm_channel_area_t *areas)
|
2000-05-16 15:20:34 +00:00
|
|
|
{
|
2000-06-01 21:58:25 +00:00
|
|
|
snd_pcm_stream_t *str;
|
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-04 16:24:04 +00:00
|
|
|
assert(pcm);
|
|
|
|
|
assert(stream >= 0 && stream <= 1);
|
2000-05-27 16:52:17 +00:00
|
|
|
str = &pcm->stream[stream];
|
2000-06-04 16:24:04 +00:00
|
|
|
assert(str->mmap_data);
|
2000-05-27 16:52:17 +00:00
|
|
|
a = calloc(str->setup.format.channels, sizeof(*areas));
|
|
|
|
|
for (channel = 0, ap = a; channel < str->setup.format.channels; ++channel, ++ap) {
|
|
|
|
|
s.channel = channel;
|
|
|
|
|
err = snd_pcm_channel_setup(pcm, stream, &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-04 13:13:01 +00:00
|
|
|
if (ap->step != str->sample_width || ap->first != 0)
|
|
|
|
|
noninterleaved = 0;
|
|
|
|
|
if (ap->addr != a[0].addr ||
|
|
|
|
|
ap->step != str->bits_per_frame ||
|
|
|
|
|
ap->first != channel * str->sample_width)
|
|
|
|
|
interleaved = 0;
|
2000-05-16 15:20:34 +00:00
|
|
|
}
|
2000-06-04 13:13:01 +00:00
|
|
|
if (noninterleaved)
|
|
|
|
|
str->mmap_type = _NONINTERLEAVED;
|
|
|
|
|
else if (interleaved)
|
|
|
|
|
str->mmap_type = _INTERLEAVED;
|
|
|
|
|
else
|
|
|
|
|
str->mmap_type = _COMPLEX;
|
2000-05-27 16:52:17 +00:00
|
|
|
str->channels = a;
|
2000-05-16 15:20:34 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2000-05-27 16:52:17 +00:00
|
|
|
int snd_pcm_mmap_data(snd_pcm_t *pcm, int stream, void **data)
|
2000-05-08 18:53:38 +00:00
|
|
|
{
|
2000-06-01 21:58:25 +00:00
|
|
|
snd_pcm_stream_t *str;
|
2000-05-27 16:52:17 +00:00
|
|
|
snd_pcm_stream_info_t info;
|
2000-05-08 18:53:38 +00:00
|
|
|
size_t bsize;
|
|
|
|
|
int err;
|
2000-06-04 16:24:04 +00:00
|
|
|
assert(pcm);
|
|
|
|
|
assert(stream >= 0 && stream <= 1);
|
2000-05-27 16:52:17 +00:00
|
|
|
str = &pcm->stream[stream];
|
2000-06-04 16:24:04 +00:00
|
|
|
assert(str->valid_setup);
|
2000-05-27 16:52:17 +00:00
|
|
|
if (str->mmap_data) {
|
2000-06-04 16:24:04 +00:00
|
|
|
if (data)
|
|
|
|
|
*data = str->mmap_data;
|
2000-05-08 18:53:38 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2000-05-27 16:52:17 +00:00
|
|
|
info.stream = stream;
|
|
|
|
|
err = snd_pcm_stream_info(pcm, &info);
|
2000-05-08 18:53:38 +00:00
|
|
|
if (err < 0)
|
|
|
|
|
return err;
|
|
|
|
|
bsize = info.mmap_size;
|
2000-05-31 15:09:27 +00:00
|
|
|
if (!(info.flags & SND_PCM_STREAM_INFO_MMAP))
|
|
|
|
|
return -ENXIO;
|
2000-06-04 16:24:04 +00:00
|
|
|
if ((err = pcm->ops->mmap_data(pcm, stream, (void**)&str->mmap_data, bsize)) < 0)
|
2000-05-31 15:09:27 +00:00
|
|
|
return err;
|
2000-06-04 16:24:04 +00:00
|
|
|
if (data)
|
|
|
|
|
*data = str->mmap_data;
|
2000-05-27 16:52:17 +00:00
|
|
|
str->mmap_data_size = bsize;
|
|
|
|
|
err = snd_pcm_mmap_get_areas(pcm, stream, 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-05-27 16:52:17 +00:00
|
|
|
int snd_pcm_mmap(snd_pcm_t *pcm, int stream, snd_pcm_mmap_control_t **control, void **data)
|
2000-05-08 18:53:38 +00:00
|
|
|
{
|
|
|
|
|
int err;
|
2000-05-27 16:52:17 +00:00
|
|
|
err = snd_pcm_mmap_control(pcm, stream, control);
|
2000-05-08 18:53:38 +00:00
|
|
|
if (err < 0)
|
|
|
|
|
return err;
|
2000-05-27 16:52:17 +00:00
|
|
|
err = snd_pcm_mmap_data(pcm, stream, data);
|
2000-05-08 18:53:38 +00:00
|
|
|
if (err < 0) {
|
2000-05-27 16:52:17 +00:00
|
|
|
snd_pcm_munmap_control(pcm, stream);
|
2000-05-08 18:53:38 +00:00
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2000-05-27 16:52:17 +00:00
|
|
|
int snd_pcm_munmap_control(snd_pcm_t *pcm, int stream)
|
2000-05-08 18:53:38 +00:00
|
|
|
{
|
|
|
|
|
int err;
|
2000-06-01 21:58:25 +00:00
|
|
|
snd_pcm_stream_t *str;
|
2000-06-04 16:24:04 +00:00
|
|
|
assert(pcm);
|
|
|
|
|
assert(stream >= 0 && stream <= 1);
|
2000-05-27 16:52:17 +00:00
|
|
|
str = &pcm->stream[stream];
|
2000-06-04 16:24:04 +00:00
|
|
|
assert(str->mmap_control);
|
2000-05-31 15:09:27 +00:00
|
|
|
if ((err = pcm->ops->munmap_control(pcm, stream, str->mmap_control, str->mmap_control_size)) < 0)
|
|
|
|
|
return err;
|
2000-05-27 16:52:17 +00:00
|
|
|
str->mmap_control = 0;
|
|
|
|
|
str->mmap_control_size = 0;
|
2000-05-08 18:53:38 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2000-05-27 16:52:17 +00:00
|
|
|
int snd_pcm_munmap_data(snd_pcm_t *pcm, int stream)
|
2000-05-08 18:53:38 +00:00
|
|
|
{
|
|
|
|
|
int err;
|
2000-06-01 21:58:25 +00:00
|
|
|
snd_pcm_stream_t *str;
|
2000-06-04 16:24:04 +00:00
|
|
|
assert(pcm);
|
|
|
|
|
assert(stream >= 0 && stream <= 1);
|
2000-05-27 16:52:17 +00:00
|
|
|
str = &pcm->stream[stream];
|
2000-06-04 16:24:04 +00:00
|
|
|
assert(str->mmap_data);
|
2000-05-31 15:09:27 +00:00
|
|
|
if ((err = pcm->ops->munmap_data(pcm, stream, str->mmap_data, str->mmap_data_size)) < 0)
|
|
|
|
|
return err;
|
2000-05-27 16:52:17 +00:00
|
|
|
free(str->channels);
|
|
|
|
|
str->mmap_data = 0;
|
|
|
|
|
str->mmap_data_size = 0;
|
2000-05-08 18:53:38 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2000-05-27 16:52:17 +00:00
|
|
|
int snd_pcm_munmap(snd_pcm_t *pcm, int stream)
|
2000-05-08 18:53:38 +00:00
|
|
|
{
|
|
|
|
|
int err;
|
2000-05-27 16:52:17 +00:00
|
|
|
err = snd_pcm_munmap_control(pcm, stream);
|
2000-05-08 18:53:38 +00:00
|
|
|
if (err < 0)
|
|
|
|
|
return err;
|
2000-05-27 16:52:17 +00:00
|
|
|
return snd_pcm_munmap_data(pcm, stream);
|
2000-05-08 18:53:38 +00:00
|
|
|
}
|
|
|
|
|
|