mirror of
https://gitlab.freedesktop.org/pipewire/pipewire.git
synced 2025-11-03 09:01:54 -05:00
pulse-server: module-pipe-source: don't queue partial samples
If the number of bytes read is not a multiple of the stride, then partial samples may be queued. Avoid that by storing the last partial sample (if any) in a separate buffer, and only queue full samples.
This commit is contained in:
parent
8a61374592
commit
73ea24a356
1 changed files with 44 additions and 33 deletions
|
|
@ -54,54 +54,65 @@ struct module_pipesrc_data {
|
||||||
bool do_unlink;
|
bool do_unlink;
|
||||||
char *filename;
|
char *filename;
|
||||||
int fd;
|
int fd;
|
||||||
int stride;
|
|
||||||
|
uint32_t stride;
|
||||||
|
|
||||||
|
uint32_t leftover_count;
|
||||||
|
uint8_t leftover[]; /* `stride` bytes for storing a partial sample */
|
||||||
};
|
};
|
||||||
|
|
||||||
static void playback_process(void *data)
|
static void playback_process(void *data)
|
||||||
{
|
{
|
||||||
struct module_pipesrc_data *impl = data;
|
struct module_pipesrc_data *impl = data;
|
||||||
struct pw_buffer *b;
|
struct spa_chunk *chunk;
|
||||||
struct spa_buffer *buf;
|
struct pw_buffer *buffer;
|
||||||
int bytes_read;
|
struct spa_data *d;
|
||||||
uint8_t *dst;
|
uint32_t left, leftover;
|
||||||
|
ssize_t bytes_read;
|
||||||
|
|
||||||
if ((b = pw_stream_dequeue_buffer(impl->playback)) == NULL) {
|
if ((buffer = pw_stream_dequeue_buffer(impl->playback)) == NULL) {
|
||||||
pw_log_warn("Out of playback buffers: %m");
|
pw_log_warn("Out of playback buffers: %m");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
buf = b->buffer;
|
d = &buffer->buffer->datas[0];
|
||||||
if ((dst = buf->datas[0].data) == NULL)
|
if (d->data == NULL)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
buf->datas[0].chunk->offset = 0;
|
left = d->maxsize;
|
||||||
buf->datas[0].chunk->stride = impl->stride;
|
spa_assert(left >= impl->leftover_count);
|
||||||
buf->datas[0].chunk->size = 0;
|
|
||||||
|
|
||||||
/*
|
chunk = d->chunk;
|
||||||
* Read from the pipe in a multiple of stride. If the number of bytes
|
|
||||||
* read is not a multiple of stride, we end up with incomplete samples
|
chunk->offset = 0;
|
||||||
* in the buffer. This is especially noticeable when using S24LE.
|
chunk->stride = impl->stride;
|
||||||
*
|
chunk->size = impl->leftover_count;
|
||||||
* Trying to read (buf->datas[0].maxsize / stride) * stride does not
|
|
||||||
* necessarily result in bytes_read being a multiple of stride
|
memcpy(d->data, impl->leftover, impl->leftover_count);
|
||||||
* resulting in us being left with incomplete samples in the buffer
|
|
||||||
* again.
|
left -= impl->leftover_count;
|
||||||
*/
|
|
||||||
bytes_read = read(impl->fd, dst, impl->stride * 4096);
|
bytes_read = read(impl->fd, SPA_PTROFF(d->data, chunk->size, void), left);
|
||||||
if (bytes_read < 0) {
|
if (bytes_read < 0) {
|
||||||
if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK) {
|
const bool important = !(errno == EINTR
|
||||||
pw_log_debug("Error in reading from pipe source: %s",
|
|| errno == EAGAIN
|
||||||
spa_strerror(-errno));
|
|| errno == EWOULDBLOCK);
|
||||||
} else {
|
|
||||||
pw_log_error("Failed to read from pipe source: %s",
|
if (important)
|
||||||
spa_strerror(-errno));
|
pw_log_warn("failed to read from pipe (%s): %s",
|
||||||
|
impl->filename, strerror(errno));
|
||||||
}
|
}
|
||||||
} else {
|
else {
|
||||||
buf->datas[0].chunk->size = bytes_read;
|
chunk->size += bytes_read;
|
||||||
}
|
}
|
||||||
|
|
||||||
pw_stream_queue_buffer(impl->playback, b);
|
leftover = chunk->size % impl->stride;
|
||||||
|
chunk->size -= leftover;
|
||||||
|
|
||||||
|
memcpy(impl->leftover, SPA_PTROFF(d->data, chunk->size, void), leftover);
|
||||||
|
impl->leftover_count = leftover;
|
||||||
|
|
||||||
|
pw_stream_queue_buffer(impl->playback, buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void on_core_error(void *data, uint32_t id, int seq, int res, const char *message)
|
static void on_core_error(void *data, uint32_t id, int seq, int res, const char *message)
|
||||||
|
|
@ -345,7 +356,7 @@ struct module *create_module_pipe_source(struct impl *impl, const char *argument
|
||||||
if ((str = pw_properties_get(props, PW_KEY_MEDIA_CLASS)) == NULL)
|
if ((str = pw_properties_get(props, PW_KEY_MEDIA_CLASS)) == NULL)
|
||||||
pw_properties_set(props, PW_KEY_MEDIA_CLASS, "Audio/Source");
|
pw_properties_set(props, PW_KEY_MEDIA_CLASS, "Audio/Source");
|
||||||
|
|
||||||
module = module_new(impl, &module_pipesource_methods, sizeof(*d));
|
module = module_new(impl, &module_pipesource_methods, sizeof(*d) + stride);
|
||||||
if (module == NULL) {
|
if (module == NULL) {
|
||||||
res = -errno;
|
res = -errno;
|
||||||
goto out;
|
goto out;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue