Work on event loop

Make a new epoll based event loop and use it for the data tranport.
Simplify the spa event api a little and rename to SpaLoop
This commit is contained in:
Wim Taymans 2016-11-18 17:46:01 +01:00
parent 0d2fa5ebc8
commit ae93f15965
33 changed files with 1286 additions and 954 deletions

View file

@ -29,7 +29,7 @@
#include <spa/log.h>
#include <spa/id-map.h>
#include <spa/poll.h>
#include <spa/loop.h>
#include <spa/monitor.h>
#include <lib/debug.h>
@ -56,7 +56,7 @@ struct _SpaALSAMonitor {
URI uri;
SpaIDMap *map;
SpaLog *log;
SpaPoll *main_loop;
SpaLoop *main_loop;
SpaMonitorEventCallback event_cb;
void *user_data;
@ -68,8 +68,7 @@ struct _SpaALSAMonitor {
ALSAItem uitem;
int fd;
SpaPollFd fds[1];
SpaPollItem poll;
SpaSource source;
};
static SpaResult
@ -244,17 +243,17 @@ fill_item (SpaALSAMonitor *this, ALSAItem *item, struct udev_device *udevice)
return 0;
}
static int
alsa_on_fd_events (SpaPollNotifyData *data)
static void
alsa_on_fd_events (SpaSource *source)
{
SpaALSAMonitor *this = data->user_data;
SpaALSAMonitor *this = source->data;
struct udev_device *dev;
const char *str;
SpaMonitorItem *item;
dev = udev_monitor_receive_device (this->umonitor);
if (fill_item (this, &this->uitem, dev) < 0)
return 0;
return;
if ((str = udev_device_get_action (dev)) == NULL)
str = "change";
@ -270,8 +269,6 @@ alsa_on_fd_events (SpaPollNotifyData *data)
}
item->event.size = sizeof (this->uitem);
this->event_cb (&this->monitor, &item->event, this->user_data);
return 0;
}
static SpaResult
@ -303,22 +300,15 @@ spa_alsa_monitor_set_event_callback (SpaMonitor *monitor,
NULL);
udev_monitor_enable_receiving (this->umonitor);
this->fd = udev_monitor_get_fd (this->umonitor);;
this->fds[0].fd = this->fd;
this->fds[0].events = POLLIN | POLLPRI | POLLERR;
this->fds[0].revents = 0;
this->poll.id = 0;
this->poll.enabled = true;
this->poll.fds = this->fds;
this->poll.n_fds = 1;
this->poll.idle_cb = NULL;
this->poll.before_cb = NULL;
this->poll.after_cb = alsa_on_fd_events;
this->poll.user_data = this;
spa_poll_add_item (this->main_loop, &this->poll);
this->source.func = alsa_on_fd_events;
this->source.data = this;
this->source.fd = udev_monitor_get_fd (this->umonitor);;
this->source.mask = SPA_IO_IN | SPA_IO_ERR;
spa_loop_add_source (this->main_loop, &this->source);
} else {
spa_poll_remove_item (this->main_loop, &this->poll);
spa_loop_remove_source (this->main_loop, &this->source);
}
return SPA_RESULT_OK;
@ -433,7 +423,7 @@ alsa_monitor_init (const SpaHandleFactory *factory,
this->map = support[i].data;
else if (strcmp (support[i].uri, SPA_LOG_URI) == 0)
this->log = support[i].data;
else if (strcmp (support[i].uri, SPA_POLL__MainLoop) == 0)
else if (strcmp (support[i].uri, SPA_LOOP__MainLoop) == 0)
this->main_loop = support[i].data;
}
if (this->map == NULL) {

View file

@ -152,7 +152,7 @@ spa_alsa_sink_node_set_props (SpaNode *node,
}
static SpaResult
do_send_event (SpaPoll *poll,
do_send_event (SpaLoop *loop,
bool async,
uint32_t seq,
size_t size,
@ -167,7 +167,7 @@ do_send_event (SpaPoll *poll,
}
static SpaResult
do_command (SpaPoll *poll,
do_command (SpaLoop *loop,
bool async,
uint32_t seq,
size_t size,
@ -197,7 +197,7 @@ do_command (SpaPoll *poll,
ac.event.size = sizeof (SpaNodeEventAsyncComplete);
ac.seq = seq;
ac.res = res;
spa_poll_invoke (this->main_loop,
spa_loop_invoke (this->main_loop,
do_send_event,
SPA_ID_INVALID,
sizeof (ac),
@ -231,7 +231,7 @@ spa_alsa_sink_node_send_command (SpaNode *node,
if (this->n_buffers == 0)
return SPA_RESULT_NO_BUFFERS;
return spa_poll_invoke (this->data_loop,
return spa_loop_invoke (this->data_loop,
do_command,
++this->seq,
command->size,
@ -790,9 +790,9 @@ alsa_sink_init (const SpaHandleFactory *factory,
this->map = support[i].data;
else if (strcmp (support[i].uri, SPA_LOG_URI) == 0)
this->log = support[i].data;
else if (strcmp (support[i].uri, SPA_POLL__DataLoop) == 0)
else if (strcmp (support[i].uri, SPA_LOOP__DataLoop) == 0)
this->data_loop = support[i].data;
else if (strcmp (support[i].uri, SPA_POLL__MainLoop) == 0)
else if (strcmp (support[i].uri, SPA_LOOP__MainLoop) == 0)
this->main_loop = support[i].data;
}
if (this->map == NULL) {

View file

@ -153,7 +153,7 @@ spa_alsa_source_node_set_props (SpaNode *node,
}
static SpaResult
do_send_event (SpaPoll *poll,
do_send_event (SpaLoop *loop,
bool async,
uint32_t seq,
size_t size,
@ -168,7 +168,7 @@ do_send_event (SpaPoll *poll,
}
static SpaResult
do_start (SpaPoll *poll,
do_start (SpaLoop *loop,
bool async,
uint32_t seq,
size_t size,
@ -188,7 +188,7 @@ do_start (SpaPoll *poll,
ac.event.size = sizeof (SpaNodeEventAsyncComplete);
ac.seq = seq;
ac.res = res;
spa_poll_invoke (this->main_loop,
spa_loop_invoke (this->main_loop,
do_send_event,
SPA_ID_INVALID,
sizeof (ac),
@ -199,7 +199,7 @@ do_start (SpaPoll *poll,
}
static SpaResult
do_pause (SpaPoll *poll,
do_pause (SpaLoop *loop,
bool async,
uint32_t seq,
size_t size,
@ -219,7 +219,7 @@ do_pause (SpaPoll *poll,
ac.event.size = sizeof (SpaNodeEventAsyncComplete);
ac.seq = seq;
ac.res = res;
spa_poll_invoke (this->main_loop,
spa_loop_invoke (this->main_loop,
do_send_event,
SPA_ID_INVALID,
sizeof (ac),
@ -252,7 +252,7 @@ spa_alsa_source_node_send_command (SpaNode *node,
if (this->n_buffers == 0)
return SPA_RESULT_NO_BUFFERS;
return spa_poll_invoke (this->data_loop,
return spa_loop_invoke (this->data_loop,
do_start,
++this->seq,
0,
@ -268,7 +268,7 @@ spa_alsa_source_node_send_command (SpaNode *node,
if (this->n_buffers == 0)
return SPA_RESULT_NO_BUFFERS;
return spa_poll_invoke (this->data_loop,
return spa_loop_invoke (this->data_loop,
do_pause,
++this->seq,
0,
@ -888,9 +888,9 @@ alsa_source_init (const SpaHandleFactory *factory,
this->map = support[i].data;
else if (strcmp (support[i].uri, SPA_LOG_URI) == 0)
this->log = support[i].data;
else if (strcmp (support[i].uri, SPA_POLL__DataLoop) == 0)
else if (strcmp (support[i].uri, SPA_LOOP__DataLoop) == 0)
this->data_loop = support[i].data;
else if (strcmp (support[i].uri, SPA_POLL__MainLoop) == 0)
else if (strcmp (support[i].uri, SPA_LOOP__MainLoop) == 0)
this->main_loop = support[i].data;
}
if (this->map == NULL) {

View file

@ -12,7 +12,7 @@
#define CHECK(s,msg) if ((err = (s)) < 0) { spa_log_error (state->log, msg ": %s", snd_strerror(err)); return err; }
static int alsa_on_fd_events (SpaPollNotifyData *data);
static void alsa_on_fd_events (SpaSource *source);
static int
spa_alsa_open (SpaALSAState *state)
@ -35,16 +35,6 @@ spa_alsa_open (SpaALSAState *state)
SND_PCM_NO_AUTO_FORMAT), "open failed");
state->poll.id = 0;
state->poll.enabled = false;
state->poll.fds = state->fds;
state->poll.n_fds = 0;
state->poll.idle_cb = NULL;
state->poll.before_cb = NULL;
state->poll.after_cb = alsa_on_fd_events;
state->poll.user_data = state;
spa_poll_add_item (state->data_loop, &state->poll);
state->opened = true;
return 0;
@ -58,8 +48,6 @@ spa_alsa_close (SpaALSAState *state)
if (!state->opened)
return 0;
spa_poll_remove_item (state->data_loop, &state->poll);
spa_log_info (state->log, "Device closing");
CHECK (snd_pcm_close (state->hndl), "close failed");
@ -508,44 +496,48 @@ mmap_read (SpaALSAState *state)
return 0;
}
static int
alsa_on_fd_events (SpaPollNotifyData *data)
static void
alsa_on_fd_events (SpaSource *source)
{
SpaALSAState *state = data->user_data;
SpaALSAState *state = source->data;
snd_pcm_t *hndl = state->hndl;
int err;
unsigned short revents = 0;
#if 0
snd_pcm_poll_descriptors_revents (hndl,
(struct pollfd *)data->fds,
data->n_fds,
state->fds,
state->n_fds,
&revents);
if (revents & POLLERR) {
spa_log_debug (state->log, "revents: %d %d", revents, state->n_fds);
#endif
revents = source->rmask;
if (revents & SPA_IO_ERR) {
if ((err = xrun_recovery (state, hndl, err)) < 0) {
spa_log_error (state->log, "error: %s", snd_strerror (err));
return -1;
return;
}
}
if (state->stream == SND_PCM_STREAM_CAPTURE) {
if (!(revents & POLLIN))
return 0;
if (!(revents & SPA_IO_IN))
return;
mmap_read (state);
} else {
if (!(revents & POLLOUT))
return 0;
if (!(revents & SPA_IO_OUT))
return;
mmap_write (state);
}
return 0;
}
SpaResult
spa_alsa_start (SpaALSAState *state, bool xrun_recover)
{
int err;
int err, i;
if (state->started)
return SPA_RESULT_OK;
@ -559,19 +551,33 @@ spa_alsa_start (SpaALSAState *state, bool xrun_recover)
return SPA_RESULT_ERROR;
}
if ((state->poll.n_fds = snd_pcm_poll_descriptors_count (state->hndl)) <= 0) {
spa_log_error (state->log, "Invalid poll descriptors count %d", state->poll.n_fds);
for (i = 0; i < state->n_fds; i++)
spa_loop_remove_source (state->data_loop, &state->sources[i]);
if ((state->n_fds = snd_pcm_poll_descriptors_count (state->hndl)) <= 0) {
spa_log_error (state->log, "Invalid poll descriptors count %d", state->n_fds);
return SPA_RESULT_ERROR;
}
if ((err = snd_pcm_poll_descriptors (state->hndl, (struct pollfd *)state->fds, state->poll.n_fds)) < 0) {
if ((err = snd_pcm_poll_descriptors (state->hndl, state->fds, state->n_fds)) < 0) {
spa_log_error (state->log, "snd_pcm_poll_descriptors: %s", snd_strerror(err));
return SPA_RESULT_ERROR;
}
if (!xrun_recover) {
state->poll.enabled = true;
spa_poll_update_item (state->data_loop, &state->poll);
for (i = 0; i < state->n_fds; i++) {
state->sources[i].func = alsa_on_fd_events;
state->sources[i].data = state;
state->sources[i].fd = state->fds[i].fd;
state->sources[i].mask = 0;
if (state->fds[i].events & POLLIN)
state->sources[i].mask |= SPA_IO_IN;
if (state->fds[i].events & POLLOUT)
state->sources[i].mask |= SPA_IO_OUT;
if (state->fds[i].events & POLLERR)
state->sources[i].mask |= SPA_IO_ERR;
if (state->fds[i].events & POLLHUP)
state->sources[i].mask |= SPA_IO_HUP;
spa_loop_add_source (state->data_loop, &state->sources[i]);
}
if (state->stream == SND_PCM_STREAM_PLAYBACK) {
@ -591,15 +597,14 @@ spa_alsa_start (SpaALSAState *state, bool xrun_recover)
SpaResult
spa_alsa_pause (SpaALSAState *state, bool xrun_recover)
{
int err;
int err, i;
if (!state->started)
return SPA_RESULT_OK;
if (!xrun_recover) {
state->poll.enabled = false;
spa_poll_update_item (state->data_loop, &state->poll);
}
for (i = 0; i < state->n_fds; i++)
spa_loop_remove_source (state->data_loop, &state->sources[i]);
state->n_fds = 0;
if ((err = snd_pcm_drop (state->hndl)) < 0)
spa_log_error (state->log, "snd_pcm_drop %s", snd_strerror (err));

View file

@ -32,6 +32,7 @@ extern "C" {
#include <spa/log.h>
#include <spa/list.h>
#include <spa/node.h>
#include <spa/loop.h>
#include <spa/ringbuffer.h>
#include <spa/audio/format.h>
@ -73,8 +74,8 @@ struct _SpaALSAState {
URI uri;
SpaIDMap *map;
SpaLog *log;
SpaPoll *main_loop;
SpaPoll *data_loop;
SpaLoop *main_loop;
SpaLoop *data_loop;
snd_pcm_stream_t stream;
snd_output_t *output;
@ -114,8 +115,9 @@ struct _SpaALSAState {
size_t ready_offset;
bool started;
SpaPollFd fds[16];
SpaPollItem poll;
int n_fds;
struct pollfd fds[16];
SpaSource sources[16];
int64_t sample_count;
int64_t last_ticks;