mirror of
https://github.com/alsa-project/alsa-lib.git
synced 2026-03-10 05:33:43 -04:00
pcm handle split
This commit is contained in:
parent
0535d28240
commit
60fa939c21
5 changed files with 694 additions and 974 deletions
|
|
@ -25,145 +25,119 @@
|
|||
#include <sys/uio.h>
|
||||
#include "pcm_local.h"
|
||||
|
||||
int snd_pcm_frames_avail(snd_pcm_t *pcm, int stream, ssize_t *frames)
|
||||
int snd_pcm_frames_avail(snd_pcm_t *handle, ssize_t *frames)
|
||||
{
|
||||
snd_pcm_stream_t *str;
|
||||
assert(pcm);
|
||||
assert(stream >= 0 && stream <= 1);
|
||||
str = &pcm->stream[stream];
|
||||
assert(str->mmap_status && str->mmap_control);
|
||||
if (stream == SND_PCM_STREAM_PLAYBACK)
|
||||
*frames = snd_pcm_mmap_playback_frames_avail(str);
|
||||
assert(handle);
|
||||
assert(handle->mmap_status && handle->mmap_control);
|
||||
if (handle->stream == SND_PCM_STREAM_PLAYBACK)
|
||||
*frames = snd_pcm_mmap_playback_frames_avail(handle);
|
||||
else
|
||||
*frames = snd_pcm_mmap_capture_frames_avail(str);
|
||||
*frames = snd_pcm_mmap_capture_frames_avail(handle);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int snd_pcm_mmap_playback_ready(snd_pcm_t *pcm)
|
||||
static int snd_pcm_mmap_playback_ready(snd_pcm_t *handle)
|
||||
{
|
||||
snd_pcm_stream_t *str;
|
||||
str = &pcm->stream[SND_PCM_STREAM_PLAYBACK];
|
||||
if (str->mmap_status->state == SND_PCM_STATE_XRUN)
|
||||
if (handle->mmap_status->state == SND_PCM_STATE_XRUN)
|
||||
return -EPIPE;
|
||||
return snd_pcm_mmap_playback_frames_avail(str) >= str->setup.frames_min;
|
||||
return snd_pcm_mmap_playback_frames_avail(handle) >= handle->setup.frames_min;
|
||||
}
|
||||
|
||||
static int snd_pcm_mmap_capture_ready(snd_pcm_t *pcm)
|
||||
static int snd_pcm_mmap_capture_ready(snd_pcm_t *handle)
|
||||
{
|
||||
snd_pcm_stream_t *str;
|
||||
int ret = 0;
|
||||
str = &pcm->stream[SND_PCM_STREAM_CAPTURE];
|
||||
if (str->mmap_status->state == SND_PCM_STATE_XRUN) {
|
||||
if (handle->mmap_status->state == SND_PCM_STATE_XRUN) {
|
||||
ret = -EPIPE;
|
||||
if (str->setup.xrun_mode == SND_PCM_XRUN_DRAIN)
|
||||
if (handle->setup.xrun_mode == SND_PCM_XRUN_DRAIN)
|
||||
return -EPIPE;
|
||||
}
|
||||
if (snd_pcm_mmap_capture_frames_avail(str) >= str->setup.frames_min)
|
||||
if (snd_pcm_mmap_capture_frames_avail(handle) >= handle->setup.frames_min)
|
||||
return 1;
|
||||
return ret;
|
||||
}
|
||||
|
||||
int snd_pcm_mmap_ready(snd_pcm_t *pcm, int stream)
|
||||
int snd_pcm_mmap_ready(snd_pcm_t *handle)
|
||||
{
|
||||
snd_pcm_stream_t *str;
|
||||
assert(pcm);
|
||||
assert(stream >= 0 && stream <= 1);
|
||||
str = &pcm->stream[stream];
|
||||
assert(str->mmap_status && str->mmap_control);
|
||||
assert(str->mmap_status->state >= SND_PCM_STATE_PREPARED);
|
||||
if (stream == SND_PCM_STREAM_PLAYBACK) {
|
||||
return snd_pcm_mmap_playback_ready(pcm);
|
||||
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);
|
||||
} else {
|
||||
return snd_pcm_mmap_capture_ready(pcm);
|
||||
return snd_pcm_mmap_capture_ready(handle);
|
||||
}
|
||||
}
|
||||
|
||||
static size_t snd_pcm_mmap_playback_frames_xfer(snd_pcm_t *pcm, size_t frames)
|
||||
static size_t snd_pcm_mmap_playback_frames_xfer(snd_pcm_t *handle, size_t frames)
|
||||
{
|
||||
snd_pcm_stream_t *str = &pcm->stream[SND_PCM_STREAM_PLAYBACK];
|
||||
snd_pcm_mmap_control_t *control = str->mmap_control;
|
||||
snd_pcm_mmap_control_t *control = handle->mmap_control;
|
||||
size_t frames_cont;
|
||||
size_t frames_avail = snd_pcm_mmap_playback_frames_avail(str);
|
||||
size_t frames_avail = snd_pcm_mmap_playback_frames_avail(handle);
|
||||
if (frames_avail < frames)
|
||||
frames = frames_avail;
|
||||
frames_cont = str->setup.buffer_size - control->frame_data % str->setup.buffer_size;
|
||||
frames_cont = handle->setup.buffer_size - control->frame_data % handle->setup.buffer_size;
|
||||
if (frames_cont < frames)
|
||||
frames = frames_cont;
|
||||
return frames;
|
||||
}
|
||||
|
||||
static size_t snd_pcm_mmap_capture_frames_xfer(snd_pcm_t *pcm, size_t frames)
|
||||
static size_t snd_pcm_mmap_capture_frames_xfer(snd_pcm_t *handle, size_t frames)
|
||||
{
|
||||
snd_pcm_stream_t *str = &pcm->stream[SND_PCM_STREAM_CAPTURE];
|
||||
snd_pcm_mmap_control_t *control = str->mmap_control;
|
||||
snd_pcm_mmap_control_t *control = handle->mmap_control;
|
||||
size_t frames_cont;
|
||||
size_t frames_avail = snd_pcm_mmap_capture_frames_avail(str);
|
||||
size_t frames_avail = snd_pcm_mmap_capture_frames_avail(handle);
|
||||
if (frames_avail < frames)
|
||||
frames = frames_avail;
|
||||
frames_cont = str->setup.buffer_size - control->frame_data % str->setup.buffer_size;
|
||||
frames_cont = handle->setup.buffer_size - control->frame_data % handle->setup.buffer_size;
|
||||
if (frames_cont < frames)
|
||||
frames = frames_cont;
|
||||
return frames;
|
||||
}
|
||||
|
||||
ssize_t snd_pcm_mmap_frames_xfer(snd_pcm_t *pcm, int stream, size_t frames)
|
||||
ssize_t snd_pcm_mmap_frames_xfer(snd_pcm_t *handle, size_t frames)
|
||||
{
|
||||
snd_pcm_stream_t *str;
|
||||
assert(pcm);
|
||||
assert(stream >= 0 && stream <= 1);
|
||||
str = &pcm->stream[stream];
|
||||
assert(str->mmap_status && str->mmap_control);
|
||||
if (stream == SND_PCM_STREAM_PLAYBACK)
|
||||
return snd_pcm_mmap_playback_frames_xfer(pcm, frames);
|
||||
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);
|
||||
else
|
||||
return snd_pcm_mmap_capture_frames_xfer(pcm, frames);
|
||||
return snd_pcm_mmap_capture_frames_xfer(handle, frames);
|
||||
}
|
||||
|
||||
ssize_t snd_pcm_mmap_frames_offset(snd_pcm_t *pcm, int stream)
|
||||
ssize_t snd_pcm_mmap_frames_offset(snd_pcm_t *handle)
|
||||
{
|
||||
snd_pcm_stream_t *str;
|
||||
assert(pcm);
|
||||
assert(stream >= 0 && stream <= 1);
|
||||
str = &pcm->stream[stream];
|
||||
assert(str->mmap_control);
|
||||
return str->mmap_control->frame_data % str->setup.buffer_size;
|
||||
assert(handle);
|
||||
assert(handle->mmap_control);
|
||||
return handle->mmap_control->frame_data % handle->setup.buffer_size;
|
||||
}
|
||||
|
||||
int snd_pcm_mmap_stream_state(snd_pcm_t *pcm, int stream)
|
||||
int snd_pcm_mmap_state(snd_pcm_t *handle)
|
||||
{
|
||||
snd_pcm_stream_t *str;
|
||||
assert(pcm);
|
||||
assert(stream >= 0 && stream <= 1);
|
||||
str = &pcm->stream[stream];
|
||||
assert(str->mmap_status);
|
||||
return str->mmap_status->state;
|
||||
assert(handle);
|
||||
assert(handle->mmap_status);
|
||||
return handle->mmap_status->state;
|
||||
}
|
||||
|
||||
int snd_pcm_mmap_stream_frame_io(snd_pcm_t *pcm, int stream)
|
||||
int snd_pcm_mmap_frame_io(snd_pcm_t *handle)
|
||||
{
|
||||
snd_pcm_stream_t *str;
|
||||
assert(pcm);
|
||||
assert(stream >= 0 && stream <= 1);
|
||||
str = &pcm->stream[stream];
|
||||
assert(str->mmap_status);
|
||||
return str->mmap_status->frame_io;
|
||||
assert(handle);
|
||||
assert(handle->mmap_status);
|
||||
return handle->mmap_status->frame_io;
|
||||
}
|
||||
|
||||
ssize_t snd_pcm_mmap_stream_frame_data(snd_pcm_t *pcm, int stream, off_t offset)
|
||||
ssize_t snd_pcm_mmap_frame_data(snd_pcm_t *handle, off_t offset)
|
||||
{
|
||||
snd_pcm_stream_t *str;
|
||||
ssize_t frame_data;
|
||||
assert(pcm);
|
||||
assert(stream >= 0 && stream <= 1);
|
||||
str = &pcm->stream[stream];
|
||||
assert(str->mmap_status && str->mmap_control);
|
||||
frame_data = str->mmap_control->frame_data;
|
||||
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;
|
||||
if (offset == 0)
|
||||
return frame_data;
|
||||
switch (str->mmap_status->state) {
|
||||
switch (handle->mmap_status->state) {
|
||||
case SND_PCM_STATE_RUNNING:
|
||||
if (str->setup.mode == SND_PCM_MODE_FRAME)
|
||||
snd_pcm_stream_frame_io(pcm, stream, 1);
|
||||
if (handle->setup.mode == SND_PCM_MODE_FRAME)
|
||||
snd_pcm_frame_io(handle, 1);
|
||||
break;
|
||||
case SND_PCM_STATE_PREPARED:
|
||||
break;
|
||||
|
|
@ -171,85 +145,83 @@ ssize_t snd_pcm_mmap_stream_frame_data(snd_pcm_t *pcm, int stream, off_t offset)
|
|||
return -EBADFD;
|
||||
}
|
||||
if (offset < 0) {
|
||||
if (offset < -(ssize_t)str->setup.buffer_size)
|
||||
offset = -(ssize_t)str->setup.buffer_size;
|
||||
if (offset < -(ssize_t)handle->setup.buffer_size)
|
||||
offset = -(ssize_t)handle->setup.buffer_size;
|
||||
else
|
||||
offset -= offset % str->setup.frames_align;
|
||||
offset -= offset % handle->setup.frames_align;
|
||||
frame_data += offset;
|
||||
if (frame_data < 0)
|
||||
frame_data += str->setup.frame_boundary;
|
||||
frame_data += handle->setup.frame_boundary;
|
||||
} else {
|
||||
size_t frames_avail;
|
||||
if (stream == SND_PCM_STREAM_PLAYBACK)
|
||||
frames_avail = snd_pcm_mmap_playback_frames_avail(str);
|
||||
if (handle->stream == SND_PCM_STREAM_PLAYBACK)
|
||||
frames_avail = snd_pcm_mmap_playback_frames_avail(handle);
|
||||
else
|
||||
frames_avail = snd_pcm_mmap_capture_frames_avail(str);
|
||||
frames_avail = snd_pcm_mmap_capture_frames_avail(handle);
|
||||
if ((size_t)offset > frames_avail)
|
||||
offset = frames_avail;
|
||||
offset -= offset % str->setup.frames_align;
|
||||
offset -= offset % handle->setup.frames_align;
|
||||
frame_data += offset;
|
||||
if ((size_t)frame_data >= str->setup.frame_boundary)
|
||||
frame_data -= str->setup.frame_boundary;
|
||||
if ((size_t)frame_data >= handle->setup.frame_boundary)
|
||||
frame_data -= handle->setup.frame_boundary;
|
||||
}
|
||||
str->mmap_control->frame_data = frame_data;
|
||||
handle->mmap_control->frame_data = frame_data;
|
||||
return frame_data;
|
||||
}
|
||||
|
||||
ssize_t snd_pcm_mmap_write_areas(snd_pcm_t *pcm, snd_pcm_channel_area_t *channels, size_t frames)
|
||||
ssize_t snd_pcm_mmap_write_areas(snd_pcm_t *handle, snd_pcm_channel_area_t *channels, size_t frames)
|
||||
{
|
||||
snd_pcm_stream_t *str;
|
||||
snd_pcm_mmap_status_t *status;
|
||||
size_t offset = 0;
|
||||
size_t result = 0;
|
||||
int err;
|
||||
|
||||
str = &pcm->stream[SND_PCM_STREAM_PLAYBACK];
|
||||
assert(str->mmap_data && str->mmap_status && str->mmap_control);
|
||||
status = str->mmap_status;
|
||||
assert(handle->mmap_data && handle->mmap_status && handle->mmap_control);
|
||||
status = handle->mmap_status;
|
||||
assert(status->state >= SND_PCM_STATE_PREPARED);
|
||||
if (str->setup.mode == SND_PCM_MODE_FRAGMENT) {
|
||||
assert(frames % str->setup.frag_size == 0);
|
||||
if (handle->setup.mode == SND_PCM_MODE_FRAGMENT) {
|
||||
assert(frames % handle->setup.frag_size == 0);
|
||||
} else {
|
||||
if (status->state == SND_PCM_STATE_RUNNING &&
|
||||
str->mode & SND_PCM_NONBLOCK)
|
||||
snd_pcm_stream_frame_io(pcm, SND_PCM_STREAM_PLAYBACK, 1);
|
||||
handle->mode & SND_PCM_NONBLOCK)
|
||||
snd_pcm_frame_io(handle, 1);
|
||||
}
|
||||
while (frames > 0) {
|
||||
ssize_t mmap_offset;
|
||||
size_t frames1;
|
||||
int ready = snd_pcm_mmap_playback_ready(pcm);
|
||||
int ready = snd_pcm_mmap_playback_ready(handle);
|
||||
if (ready < 0)
|
||||
return ready;
|
||||
if (!ready) {
|
||||
struct pollfd pfd;
|
||||
if (status->state != SND_PCM_STATE_RUNNING)
|
||||
return result > 0 ? result : -EPIPE;
|
||||
if (str->mode & SND_PCM_NONBLOCK)
|
||||
if (handle->mode & SND_PCM_NONBLOCK)
|
||||
return result > 0 ? result : -EAGAIN;
|
||||
pfd.fd = snd_pcm_file_descriptor(pcm, SND_PCM_STREAM_PLAYBACK);
|
||||
pfd.fd = snd_pcm_file_descriptor(handle);
|
||||
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));
|
||||
assert(snd_pcm_mmap_playback_ready(handle));
|
||||
}
|
||||
frames1 = snd_pcm_mmap_playback_frames_xfer(pcm, frames);
|
||||
frames1 = snd_pcm_mmap_playback_frames_xfer(handle, frames);
|
||||
assert(frames1 > 0);
|
||||
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);
|
||||
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);
|
||||
if (status->state == SND_PCM_STATE_XRUN)
|
||||
return result > 0 ? result : -EPIPE;
|
||||
snd_pcm_stream_frame_data(pcm, SND_PCM_STREAM_PLAYBACK, frames1);
|
||||
snd_pcm_frame_data(handle, frames1);
|
||||
frames -= frames1;
|
||||
offset += frames1;
|
||||
result += frames1;
|
||||
if (status->state == SND_PCM_STATE_PREPARED &&
|
||||
(str->setup.start_mode == SND_PCM_START_DATA ||
|
||||
(str->setup.start_mode == SND_PCM_START_FULL &&
|
||||
!snd_pcm_mmap_playback_ready(pcm)))) {
|
||||
err = snd_pcm_stream_go(pcm, SND_PCM_STREAM_PLAYBACK);
|
||||
(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);
|
||||
if (err < 0)
|
||||
return result > 0 ? result : err;
|
||||
}
|
||||
|
|
@ -257,44 +229,40 @@ ssize_t snd_pcm_mmap_write_areas(snd_pcm_t *pcm, snd_pcm_channel_area_t *channel
|
|||
return result;
|
||||
}
|
||||
|
||||
ssize_t snd_pcm_mmap_write(snd_pcm_t *pcm, const void *buffer, size_t frames)
|
||||
ssize_t snd_pcm_mmap_write(snd_pcm_t *handle, const void *buffer, size_t frames)
|
||||
{
|
||||
snd_pcm_stream_t *str;
|
||||
unsigned int nchannels;
|
||||
assert(pcm);
|
||||
str = &pcm->stream[SND_PCM_STREAM_PLAYBACK];
|
||||
assert(str->mmap_data && str->mmap_status && str->mmap_control);
|
||||
assert(handle);
|
||||
assert(handle->mmap_data && handle->mmap_status && handle->mmap_control);
|
||||
assert(frames == 0 || buffer);
|
||||
nchannels = str->setup.format.channels;
|
||||
assert(str->setup.format.interleave || nchannels == 1);
|
||||
nchannels = handle->setup.format.channels;
|
||||
assert(handle->setup.format.interleave || nchannels == 1);
|
||||
{
|
||||
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->bits_per_sample * channel;
|
||||
channels[channel].step = str->bits_per_frame;
|
||||
channels[channel].first = handle->bits_per_sample * channel;
|
||||
channels[channel].step = handle->bits_per_frame;
|
||||
}
|
||||
return snd_pcm_mmap_write_areas(pcm, channels, frames);
|
||||
return snd_pcm_mmap_write_areas(handle, channels, frames);
|
||||
}
|
||||
}
|
||||
|
||||
ssize_t snd_pcm_mmap_writev(snd_pcm_t *pcm, const struct iovec *vector, unsigned long vcount)
|
||||
ssize_t snd_pcm_mmap_writev(snd_pcm_t *handle, const struct iovec *vector, unsigned long vcount)
|
||||
{
|
||||
snd_pcm_stream_t *str;
|
||||
size_t result = 0;
|
||||
unsigned int nchannels;
|
||||
assert(pcm);
|
||||
str = &pcm->stream[SND_PCM_STREAM_PLAYBACK];
|
||||
assert(str->mmap_data && str->mmap_status && str->mmap_control);
|
||||
assert(handle);
|
||||
assert(handle->mmap_data && handle->mmap_status && handle->mmap_control);
|
||||
assert(vcount == 0 || vector);
|
||||
nchannels = str->setup.format.channels;
|
||||
if (str->setup.format.interleave) {
|
||||
nchannels = handle->setup.format.channels;
|
||||
if (handle->setup.format.interleave) {
|
||||
unsigned int b;
|
||||
for (b = 0; b < vcount; b++) {
|
||||
ssize_t ret;
|
||||
size_t frames = vector[b].iov_len;
|
||||
ret = snd_pcm_mmap_write(pcm, vector[b].iov_base, frames);
|
||||
ret = snd_pcm_mmap_write(handle, vector[b].iov_base, frames);
|
||||
if (ret < 0) {
|
||||
if (result <= 0)
|
||||
return ret;
|
||||
|
|
@ -316,9 +284,9 @@ ssize_t snd_pcm_mmap_writev(snd_pcm_t *pcm, const struct iovec *vector, unsigned
|
|||
assert(vector[v].iov_len == frames);
|
||||
channels[v].addr = vector[v].iov_base;
|
||||
channels[v].first = 0;
|
||||
channels[v].step = str->bits_per_sample;
|
||||
channels[v].step = handle->bits_per_sample;
|
||||
}
|
||||
ret = snd_pcm_mmap_write_areas(pcm, channels, frames);
|
||||
ret = snd_pcm_mmap_write_areas(handle, channels, frames);
|
||||
if (ret < 0) {
|
||||
if (result <= 0)
|
||||
return ret;
|
||||
|
|
@ -333,60 +301,58 @@ ssize_t snd_pcm_mmap_writev(snd_pcm_t *pcm, const struct iovec *vector, unsigned
|
|||
return result;
|
||||
}
|
||||
|
||||
ssize_t snd_pcm_mmap_read_areas(snd_pcm_t *pcm, snd_pcm_channel_area_t *channels, size_t frames)
|
||||
ssize_t snd_pcm_mmap_read_areas(snd_pcm_t *handle, snd_pcm_channel_area_t *channels, size_t frames)
|
||||
{
|
||||
snd_pcm_stream_t *str;
|
||||
snd_pcm_mmap_status_t *status;
|
||||
size_t offset = 0;
|
||||
size_t result = 0;
|
||||
int err;
|
||||
|
||||
str = &pcm->stream[SND_PCM_STREAM_CAPTURE];
|
||||
assert(str->mmap_data && str->mmap_status && str->mmap_control);
|
||||
status = str->mmap_status;
|
||||
assert(handle->mmap_data && handle->mmap_status && handle->mmap_control);
|
||||
status = handle->mmap_status;
|
||||
assert(status->state >= SND_PCM_STATE_PREPARED);
|
||||
if (str->setup.mode == SND_PCM_MODE_FRAGMENT) {
|
||||
assert(frames % str->setup.frag_size == 0);
|
||||
if (handle->setup.mode == SND_PCM_MODE_FRAGMENT) {
|
||||
assert(frames % handle->setup.frag_size == 0);
|
||||
} else {
|
||||
if (status->state == SND_PCM_STATE_RUNNING &&
|
||||
str->mode & SND_PCM_NONBLOCK)
|
||||
snd_pcm_stream_frame_io(pcm, SND_PCM_STREAM_CAPTURE, 1);
|
||||
handle->mode & SND_PCM_NONBLOCK)
|
||||
snd_pcm_frame_io(handle, 1);
|
||||
}
|
||||
if (status->state == SND_PCM_STATE_PREPARED &&
|
||||
str->setup.start_mode == SND_PCM_START_DATA) {
|
||||
err = snd_pcm_stream_go(pcm, SND_PCM_STREAM_CAPTURE);
|
||||
handle->setup.start_mode == SND_PCM_START_DATA) {
|
||||
err = snd_pcm_go(handle);
|
||||
if (err < 0)
|
||||
return err;
|
||||
}
|
||||
while (frames > 0) {
|
||||
ssize_t mmap_offset;
|
||||
size_t frames1;
|
||||
int ready = snd_pcm_mmap_capture_ready(pcm);
|
||||
int ready = snd_pcm_mmap_capture_ready(handle);
|
||||
if (ready < 0)
|
||||
return ready;
|
||||
if (!ready) {
|
||||
struct pollfd pfd;
|
||||
if (status->state != SND_PCM_STATE_RUNNING)
|
||||
return result > 0 ? result : -EPIPE;
|
||||
if (str->mode & SND_PCM_NONBLOCK)
|
||||
if (handle->mode & SND_PCM_NONBLOCK)
|
||||
return result > 0 ? result : -EAGAIN;
|
||||
pfd.fd = snd_pcm_file_descriptor(pcm, SND_PCM_STREAM_CAPTURE);
|
||||
pfd.fd = snd_pcm_file_descriptor(handle);
|
||||
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));
|
||||
assert(snd_pcm_mmap_capture_ready(handle));
|
||||
}
|
||||
frames1 = snd_pcm_mmap_capture_frames_xfer(pcm, frames);
|
||||
frames1 = snd_pcm_mmap_capture_frames_xfer(handle, frames);
|
||||
assert(frames1 > 0);
|
||||
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);
|
||||
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);
|
||||
if (status->state == SND_PCM_STATE_XRUN &&
|
||||
str->setup.xrun_mode == SND_PCM_XRUN_DRAIN)
|
||||
handle->setup.xrun_mode == SND_PCM_XRUN_DRAIN)
|
||||
return result > 0 ? result : -EPIPE;
|
||||
snd_pcm_stream_frame_data(pcm, SND_PCM_STREAM_CAPTURE, frames1);
|
||||
snd_pcm_frame_data(handle, frames1);
|
||||
frames -= frames1;
|
||||
offset += frames1;
|
||||
result += frames1;
|
||||
|
|
@ -394,44 +360,40 @@ ssize_t snd_pcm_mmap_read_areas(snd_pcm_t *pcm, snd_pcm_channel_area_t *channels
|
|||
return result;
|
||||
}
|
||||
|
||||
ssize_t snd_pcm_mmap_read(snd_pcm_t *pcm, void *buffer, size_t frames)
|
||||
ssize_t snd_pcm_mmap_read(snd_pcm_t *handle, void *buffer, size_t frames)
|
||||
{
|
||||
snd_pcm_stream_t *str;
|
||||
unsigned int nchannels;
|
||||
assert(pcm);
|
||||
str = &pcm->stream[SND_PCM_STREAM_CAPTURE];
|
||||
assert(str->mmap_data && str->mmap_status && str->mmap_control);
|
||||
assert(handle);
|
||||
assert(handle->mmap_data && handle->mmap_status && handle->mmap_control);
|
||||
assert(frames == 0 || buffer);
|
||||
nchannels = str->setup.format.channels;
|
||||
assert(str->setup.format.interleave || nchannels == 1);
|
||||
nchannels = handle->setup.format.channels;
|
||||
assert(handle->setup.format.interleave || nchannels == 1);
|
||||
{
|
||||
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->bits_per_sample * channel;
|
||||
channels[channel].step = str->bits_per_frame;
|
||||
channels[channel].first = handle->bits_per_sample * channel;
|
||||
channels[channel].step = handle->bits_per_frame;
|
||||
}
|
||||
return snd_pcm_mmap_read_areas(pcm, channels, frames);
|
||||
return snd_pcm_mmap_read_areas(handle, channels, frames);
|
||||
}
|
||||
}
|
||||
|
||||
ssize_t snd_pcm_mmap_readv(snd_pcm_t *pcm, const struct iovec *vector, unsigned long vcount)
|
||||
ssize_t snd_pcm_mmap_readv(snd_pcm_t *handle, const struct iovec *vector, unsigned long vcount)
|
||||
{
|
||||
snd_pcm_stream_t *str;
|
||||
size_t result = 0;
|
||||
unsigned int nchannels;
|
||||
assert(pcm);
|
||||
str = &pcm->stream[SND_PCM_STREAM_CAPTURE];
|
||||
assert(str->mmap_data && str->mmap_status && str->mmap_control);
|
||||
assert(handle);
|
||||
assert(handle->mmap_data && handle->mmap_status && handle->mmap_control);
|
||||
assert(vcount == 0 || vector);
|
||||
nchannels = str->setup.format.channels;
|
||||
if (str->setup.format.interleave) {
|
||||
nchannels = handle->setup.format.channels;
|
||||
if (handle->setup.format.interleave) {
|
||||
unsigned int b;
|
||||
for (b = 0; b < vcount; b++) {
|
||||
ssize_t ret;
|
||||
size_t frames = vector[b].iov_len;
|
||||
ret = snd_pcm_mmap_read(pcm, vector[b].iov_base, frames);
|
||||
ret = snd_pcm_mmap_read(handle, vector[b].iov_base, frames);
|
||||
if (ret < 0) {
|
||||
if (result <= 0)
|
||||
return ret;
|
||||
|
|
@ -453,9 +415,9 @@ ssize_t snd_pcm_mmap_readv(snd_pcm_t *pcm, const struct iovec *vector, unsigned
|
|||
assert(vector[v].iov_len == frames);
|
||||
channels[v].addr = vector[v].iov_base;
|
||||
channels[v].first = 0;
|
||||
channels[v].step = str->bits_per_sample;
|
||||
channels[v].step = handle->bits_per_sample;
|
||||
}
|
||||
ret = snd_pcm_mmap_read_areas(pcm, channels, frames);
|
||||
ret = snd_pcm_mmap_read_areas(handle, channels, frames);
|
||||
if (ret < 0) {
|
||||
if (result <= 0)
|
||||
return ret;
|
||||
|
|
@ -470,64 +432,55 @@ ssize_t snd_pcm_mmap_readv(snd_pcm_t *pcm, const struct iovec *vector, unsigned
|
|||
return result;
|
||||
}
|
||||
|
||||
int snd_pcm_mmap_status(snd_pcm_t *pcm, int stream, snd_pcm_mmap_status_t **status)
|
||||
int snd_pcm_mmap_status(snd_pcm_t *handle, snd_pcm_mmap_status_t **status)
|
||||
{
|
||||
snd_pcm_stream_t *str;
|
||||
int err;
|
||||
assert(pcm);
|
||||
assert(stream >= 0 && stream <= 1);
|
||||
str = &pcm->stream[stream];
|
||||
assert(str->valid_setup);
|
||||
if (str->mmap_status) {
|
||||
assert(handle);
|
||||
assert(handle->valid_setup);
|
||||
if (handle->mmap_status) {
|
||||
if (status)
|
||||
*status = str->mmap_status;
|
||||
*status = handle->mmap_status;
|
||||
return 0;
|
||||
}
|
||||
|
||||
if ((err = pcm->ops->mmap_status(pcm, stream, &str->mmap_status)) < 0)
|
||||
if ((err = handle->ops->mmap_status(handle->op_arg, &handle->mmap_status)) < 0)
|
||||
return err;
|
||||
if (status)
|
||||
*status = str->mmap_status;
|
||||
*status = handle->mmap_status;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int snd_pcm_mmap_control(snd_pcm_t *pcm, int stream, snd_pcm_mmap_control_t **control)
|
||||
int snd_pcm_mmap_control(snd_pcm_t *handle, snd_pcm_mmap_control_t **control)
|
||||
{
|
||||
snd_pcm_stream_t *str;
|
||||
int err;
|
||||
assert(pcm);
|
||||
assert(stream >= 0 && stream <= 1);
|
||||
str = &pcm->stream[stream];
|
||||
assert(str->valid_setup);
|
||||
if (str->mmap_control) {
|
||||
assert(handle);
|
||||
assert(handle->valid_setup);
|
||||
if (handle->mmap_control) {
|
||||
if (control)
|
||||
*control = str->mmap_control;
|
||||
*control = handle->mmap_control;
|
||||
return 0;
|
||||
}
|
||||
|
||||
if ((err = pcm->ops->mmap_control(pcm, stream, &str->mmap_control)) < 0)
|
||||
if ((err = handle->ops->mmap_control(handle->op_arg, &handle->mmap_control)) < 0)
|
||||
return err;
|
||||
if (control)
|
||||
*control = str->mmap_control;
|
||||
*control = handle->mmap_control;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int snd_pcm_mmap_get_areas(snd_pcm_t *pcm, int stream, snd_pcm_channel_area_t *areas)
|
||||
int snd_pcm_mmap_get_areas(snd_pcm_t *handle, snd_pcm_channel_area_t *areas)
|
||||
{
|
||||
snd_pcm_stream_t *str;
|
||||
snd_pcm_channel_setup_t s;
|
||||
snd_pcm_channel_area_t *a, *ap;
|
||||
unsigned int channel;
|
||||
int interleaved = 1, noninterleaved = 1;
|
||||
int err;
|
||||
assert(pcm);
|
||||
assert(stream >= 0 && stream <= 1);
|
||||
str = &pcm->stream[stream];
|
||||
assert(str->mmap_data);
|
||||
a = calloc(str->setup.format.channels, sizeof(*areas));
|
||||
for (channel = 0, ap = a; channel < str->setup.format.channels; ++channel, ++ap) {
|
||||
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) {
|
||||
s.channel = channel;
|
||||
err = snd_pcm_channel_setup(pcm, stream, &s);
|
||||
err = snd_pcm_channel_setup(handle, &s);
|
||||
if (err < 0) {
|
||||
free(a);
|
||||
return err;
|
||||
|
|
@ -535,131 +488,118 @@ int snd_pcm_mmap_get_areas(snd_pcm_t *pcm, int stream, snd_pcm_channel_area_t *a
|
|||
if (areas)
|
||||
areas[channel] = s.area;
|
||||
*ap = s.area;
|
||||
if (ap->step != str->bits_per_sample || ap->first != 0)
|
||||
if (ap->step != handle->bits_per_sample || ap->first != 0)
|
||||
noninterleaved = 0;
|
||||
if (ap->addr != a[0].addr ||
|
||||
ap->step != str->bits_per_frame ||
|
||||
ap->first != channel * str->bits_per_sample)
|
||||
ap->step != handle->bits_per_frame ||
|
||||
ap->first != channel * handle->bits_per_sample)
|
||||
interleaved = 0;
|
||||
}
|
||||
if (noninterleaved)
|
||||
str->mmap_type = _NONINTERLEAVED;
|
||||
handle->mmap_type = _NONINTERLEAVED;
|
||||
else if (interleaved)
|
||||
str->mmap_type = _INTERLEAVED;
|
||||
handle->mmap_type = _INTERLEAVED;
|
||||
else
|
||||
str->mmap_type = _COMPLEX;
|
||||
str->channels = a;
|
||||
handle->mmap_type = _COMPLEX;
|
||||
handle->channels = a;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int snd_pcm_mmap_data(snd_pcm_t *pcm, int stream, void **data)
|
||||
int snd_pcm_mmap_data(snd_pcm_t *handle, void **data)
|
||||
{
|
||||
snd_pcm_stream_t *str;
|
||||
snd_pcm_stream_info_t info;
|
||||
snd_pcm_info_t info;
|
||||
size_t bsize;
|
||||
int err;
|
||||
assert(pcm);
|
||||
assert(stream >= 0 && stream <= 1);
|
||||
str = &pcm->stream[stream];
|
||||
assert(str->valid_setup);
|
||||
if (str->mmap_data) {
|
||||
assert(handle);
|
||||
assert(handle->valid_setup);
|
||||
if (handle->mmap_data) {
|
||||
if (data)
|
||||
*data = str->mmap_data;
|
||||
*data = handle->mmap_data;
|
||||
return 0;
|
||||
}
|
||||
|
||||
info.stream = stream;
|
||||
err = snd_pcm_stream_info(pcm, &info);
|
||||
err = snd_pcm_info(handle, &info);
|
||||
if (err < 0)
|
||||
return err;
|
||||
bsize = info.mmap_size;
|
||||
if (!(info.flags & SND_PCM_STREAM_INFO_MMAP))
|
||||
if (!(info.flags & SND_PCM_INFO_MMAP))
|
||||
return -ENXIO;
|
||||
if ((err = pcm->ops->mmap_data(pcm, stream, (void**)&str->mmap_data, bsize)) < 0)
|
||||
if ((err = handle->ops->mmap_data(handle->op_arg, (void**)&handle->mmap_data, bsize)) < 0)
|
||||
return err;
|
||||
if (data)
|
||||
*data = str->mmap_data;
|
||||
str->mmap_data_size = bsize;
|
||||
err = snd_pcm_mmap_get_areas(pcm, stream, NULL);
|
||||
*data = handle->mmap_data;
|
||||
handle->mmap_data_size = bsize;
|
||||
err = snd_pcm_mmap_get_areas(handle, NULL);
|
||||
if (err < 0)
|
||||
return err;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int snd_pcm_mmap(snd_pcm_t *pcm, int stream, snd_pcm_mmap_status_t **status, snd_pcm_mmap_control_t **control, void **data)
|
||||
int snd_pcm_mmap(snd_pcm_t *handle, snd_pcm_mmap_status_t **status, snd_pcm_mmap_control_t **control, void **data)
|
||||
{
|
||||
int err;
|
||||
err = snd_pcm_mmap_status(pcm, stream, status);
|
||||
err = snd_pcm_mmap_status(handle, status);
|
||||
if (err < 0)
|
||||
return err;
|
||||
err = snd_pcm_mmap_control(pcm, stream, control);
|
||||
err = snd_pcm_mmap_control(handle, control);
|
||||
if (err < 0) {
|
||||
snd_pcm_munmap_status(pcm, stream);
|
||||
snd_pcm_munmap_status(handle);
|
||||
return err;
|
||||
}
|
||||
err = snd_pcm_mmap_data(pcm, stream, data);
|
||||
err = snd_pcm_mmap_data(handle, data);
|
||||
if (err < 0) {
|
||||
snd_pcm_munmap_status(pcm, stream);
|
||||
snd_pcm_munmap_control(pcm, stream);
|
||||
snd_pcm_munmap_status(handle);
|
||||
snd_pcm_munmap_control(handle);
|
||||
return err;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int snd_pcm_munmap_status(snd_pcm_t *pcm, int stream)
|
||||
int snd_pcm_munmap_status(snd_pcm_t *handle)
|
||||
{
|
||||
int err;
|
||||
snd_pcm_stream_t *str;
|
||||
assert(pcm);
|
||||
assert(stream >= 0 && stream <= 1);
|
||||
str = &pcm->stream[stream];
|
||||
assert(str->mmap_status);
|
||||
if ((err = pcm->ops->munmap_status(pcm, stream, str->mmap_status)) < 0)
|
||||
assert(handle);
|
||||
assert(handle->mmap_status);
|
||||
if ((err = handle->ops->munmap_status(handle->op_arg, handle->mmap_status)) < 0)
|
||||
return err;
|
||||
str->mmap_status = 0;
|
||||
handle->mmap_status = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int snd_pcm_munmap_control(snd_pcm_t *pcm, int stream)
|
||||
int snd_pcm_munmap_control(snd_pcm_t *handle)
|
||||
{
|
||||
int err;
|
||||
snd_pcm_stream_t *str;
|
||||
assert(pcm);
|
||||
assert(stream >= 0 && stream <= 1);
|
||||
str = &pcm->stream[stream];
|
||||
assert(str->mmap_control);
|
||||
if ((err = pcm->ops->munmap_control(pcm, stream, str->mmap_control)) < 0)
|
||||
assert(handle);
|
||||
assert(handle->mmap_control);
|
||||
if ((err = handle->ops->munmap_control(handle->op_arg, handle->mmap_control)) < 0)
|
||||
return err;
|
||||
str->mmap_control = 0;
|
||||
handle->mmap_control = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int snd_pcm_munmap_data(snd_pcm_t *pcm, int stream)
|
||||
int snd_pcm_munmap_data(snd_pcm_t *handle)
|
||||
{
|
||||
int err;
|
||||
snd_pcm_stream_t *str;
|
||||
assert(pcm);
|
||||
assert(stream >= 0 && stream <= 1);
|
||||
str = &pcm->stream[stream];
|
||||
assert(str->mmap_data);
|
||||
if ((err = pcm->ops->munmap_data(pcm, stream, str->mmap_data, str->mmap_data_size)) < 0)
|
||||
assert(handle);
|
||||
assert(handle->mmap_data);
|
||||
if ((err = handle->ops->munmap_data(handle->op_arg, handle->mmap_data, handle->mmap_data_size)) < 0)
|
||||
return err;
|
||||
free(str->channels);
|
||||
str->channels = 0;
|
||||
str->mmap_data = 0;
|
||||
str->mmap_data_size = 0;
|
||||
free(handle->channels);
|
||||
handle->channels = 0;
|
||||
handle->mmap_data = 0;
|
||||
handle->mmap_data_size = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int snd_pcm_munmap(snd_pcm_t *pcm, int stream)
|
||||
int snd_pcm_munmap(snd_pcm_t *handle)
|
||||
{
|
||||
int err;
|
||||
err = snd_pcm_munmap_status(pcm, stream);
|
||||
err = snd_pcm_munmap_status(handle);
|
||||
if (err < 0)
|
||||
return err;
|
||||
err = snd_pcm_munmap_control(pcm, stream);
|
||||
err = snd_pcm_munmap_control(handle);
|
||||
if (err < 0)
|
||||
return err;
|
||||
return snd_pcm_munmap_data(pcm, stream);
|
||||
return snd_pcm_munmap_data(handle);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue