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;

View file

@ -22,11 +22,11 @@
#include <string.h>
#include <stdio.h>
#include <sys/timerfd.h>
#include <poll.h>
#include <spa/id-map.h>
#include <spa/log.h>
#include <spa/node.h>
#include <spa/loop.h>
#include <spa/list.h>
#include <spa/audio/format.h>
#include <lib/props.h>
@ -71,15 +71,15 @@ struct _SpaAudioTestSrc {
URI uri;
SpaIDMap *map;
SpaLog *log;
SpaPoll *data_loop;
SpaLoop *data_loop;
SpaAudioTestSrcProps props[2];
SpaNodeEventCallback event_cb;
void *user_data;
SpaPollItem timer;
SpaPollFd fds[1];
bool timer_enabled;
SpaSource timer_source;
struct itimerspec timerspec;
SpaPortInfo info;
@ -256,20 +256,19 @@ fill_buffer (SpaAudioTestSrc *this, ATSBuffer *b)
return SPA_RESULT_OK;
}
static int audiotestsrc_on_output (SpaPollNotifyData *data);
static SpaResult update_poll_enabled (SpaAudioTestSrc *this, bool enabled);
static SpaResult update_loop_enabled (SpaAudioTestSrc *this, bool enabled);
static int
audiotestsrc_on_output (SpaPollNotifyData *data)
static void
audiotestsrc_on_output (SpaSource *source)
{
SpaAudioTestSrc *this = data->user_data;
SpaAudioTestSrc *this = source->data;
ATSBuffer *b;
if (spa_list_is_empty (&this->empty)) {
if (!this->props[1].live)
update_poll_enabled (this, false);
return 0;
update_loop_enabled (this, false);
return;
}
b = spa_list_first (&this->empty, ATSBuffer, list);
spa_list_remove (&b->list);
@ -288,19 +287,17 @@ audiotestsrc_on_output (SpaPollNotifyData *data)
if (this->props[1].live) {
uint64_t expirations, next_time;
if (read (this->fds[0].fd, &expirations, sizeof (uint64_t)) < sizeof (uint64_t))
if (read (this->timer_source.fd, &expirations, sizeof (uint64_t)) < sizeof (uint64_t))
perror ("read timerfd");
next_time = this->start_time + this->elapsed_time;
this->timerspec.it_value.tv_sec = next_time / SPA_NSEC_PER_SEC;
this->timerspec.it_value.tv_nsec = next_time % SPA_NSEC_PER_SEC;
timerfd_settime (this->fds[0].fd, TFD_TIMER_ABSTIME, &this->timerspec, NULL);
timerfd_settime (this->timer_source.fd, TFD_TIMER_ABSTIME, &this->timerspec, NULL);
}
spa_list_insert (this->ready.prev, &b->list);
send_have_output (this);
return 0;
}
static void
@ -310,10 +307,15 @@ update_state (SpaAudioTestSrc *this, SpaNodeState state)
}
static SpaResult
update_poll_enabled (SpaAudioTestSrc *this, bool enabled)
update_loop_enabled (SpaAudioTestSrc *this, bool enabled)
{
if (this->event_cb) {
this->timer.enabled = enabled;
this->timer_enabled = enabled;
if (enabled)
this->timer_source.mask = SPA_IO_IN;
else
this->timer_source.mask = 0;
if (this->props[1].live) {
if (enabled) {
uint64_t next_time = this->start_time + this->elapsed_time;
@ -324,18 +326,11 @@ update_poll_enabled (SpaAudioTestSrc *this, bool enabled)
this->timerspec.it_value.tv_sec = 0;
this->timerspec.it_value.tv_nsec = 0;
}
timerfd_settime (this->fds[0].fd, TFD_TIMER_ABSTIME, &this->timerspec, NULL);
this->timer.fds = this->fds;
this->timer.n_fds = 1;
this->timer.idle_cb = NULL;
this->timer.after_cb = audiotestsrc_on_output;
} else {
this->timer.fds = NULL;
this->timer.n_fds = 0;
this->timer.idle_cb = audiotestsrc_on_output;
this->timer.after_cb = NULL;
timerfd_settime (this->timer_source.fd, TFD_TIMER_ABSTIME, &this->timerspec, NULL);
this->timer_source.func = audiotestsrc_on_output;
}
spa_poll_update_item (this->data_loop, &this->timer);
spa_loop_update_source (this->data_loop, &this->timer_source);
}
return SPA_RESULT_OK;
}
@ -377,7 +372,7 @@ spa_audiotestsrc_node_send_command (SpaNode *node,
this->elapsed_time = 0;
this->started = true;
update_poll_enabled (this, true);
update_loop_enabled (this, true);
update_state (this, SPA_NODE_STATE_STREAMING);
break;
}
@ -393,7 +388,7 @@ spa_audiotestsrc_node_send_command (SpaNode *node,
return SPA_RESULT_OK;
this->started = false;
update_poll_enabled (this, false);
update_loop_enabled (this, false);
update_state (this, SPA_NODE_STATE_PAUSED);
break;
}
@ -419,14 +414,14 @@ spa_audiotestsrc_node_set_event_callback (SpaNode *node,
this = SPA_CONTAINER_OF (node, SpaAudioTestSrc, node);
if (event_cb == NULL && this->event_cb) {
spa_poll_remove_item (this->data_loop, &this->timer);
spa_loop_remove_source (this->data_loop, &this->timer_source);
}
this->event_cb = event_cb;
this->user_data = user_data;
if (this->event_cb) {
spa_poll_add_item (this->data_loop, &this->timer);
spa_loop_add_source (this->data_loop, &this->timer_source);
}
return SPA_RESULT_OK;
}
@ -801,7 +796,7 @@ spa_audiotestsrc_node_port_reuse_buffer (SpaNode *node,
spa_list_insert (this->empty.prev, &b->list);
if (!this->props[1].live)
update_poll_enabled (this, true);
update_loop_enabled (this, true);
return SPA_RESULT_OK;
}
@ -958,7 +953,7 @@ audiotestsrc_clear (SpaHandle *handle)
this = (SpaAudioTestSrc *) handle;
close (this->fds[0].fd);
close (this->timer_source.fd);
return SPA_RESULT_OK;
}
@ -986,7 +981,7 @@ audiotestsrc_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;
}
if (this->map == NULL) {
@ -1009,21 +1004,15 @@ audiotestsrc_init (const SpaHandleFactory *factory,
spa_list_init (&this->empty);
spa_list_init (&this->ready);
this->fds[0].fd = timerfd_create (CLOCK_MONOTONIC, TFD_NONBLOCK | TFD_CLOEXEC);
this->fds[0].events = POLLIN | POLLPRI | POLLERR;
this->fds[0].revents = 0;
this->timer_source.func = audiotestsrc_on_output;
this->timer_source.data = this;
this->timer_source.fd = timerfd_create (CLOCK_MONOTONIC, TFD_NONBLOCK | TFD_CLOEXEC);
this->timer_source.mask = SPA_IO_IN | SPA_IO_ERR;
this->timerspec.it_value.tv_sec = 0;
this->timerspec.it_value.tv_nsec = 0;
this->timerspec.it_interval.tv_sec = 0;
this->timerspec.it_interval.tv_nsec = 0;
this->timer.id = 0;
this->timer.enabled = false;
this->timer.idle_cb = NULL;
this->timer.before_cb = NULL;
this->timer.after_cb = NULL;
this->timer.user_data = this;
this->info.flags = SPA_PORT_INFO_FLAG_CAN_USE_BUFFERS |
SPA_PORT_INFO_FLAG_NO_REF;
if (this->props[1].live)

View file

@ -22,13 +22,12 @@
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <poll.h>
#include <libudev.h>
#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>
@ -54,7 +53,7 @@ struct _SpaV4l2Monitor {
URI uri;
SpaIDMap *map;
SpaLog *log;
SpaPoll *main_loop;
SpaLoop *main_loop;
SpaMonitorEventCallback event_cb;
void *user_data;
@ -65,9 +64,7 @@ struct _SpaV4l2Monitor {
V4l2Item uitem;
int fd;
SpaPollFd fds[1];
SpaPollItem poll;
SpaSource source;
};
static SpaResult
@ -180,19 +177,18 @@ fill_item (V4l2Item *item, struct udev_device *udevice)
}
static int
v4l2_on_fd_events (SpaPollNotifyData *data)
static void
v4l2_on_fd_events (SpaSource *source)
{
SpaV4l2Monitor *this = data->user_data;
SpaV4l2Monitor *this = source->data;
struct udev_device *dev;
const char *action;
SpaMonitorItem *item;
dev = udev_monitor_receive_device (this->umonitor);
fill_item (&this->uitem, dev);
if (dev == NULL)
return 0;
return;
if ((action = udev_device_get_action (dev)) == NULL)
action = "change";
@ -208,8 +204,6 @@ v4l2_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
@ -241,23 +235,14 @@ spa_v4l2_monitor_set_event_callback (SpaMonitor *monitor,
NULL);
udev_monitor_enable_receiving (this->umonitor);
this->fd = udev_monitor_get_fd (this->umonitor);;
this->source.func = v4l2_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;
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 = v4l2_on_fd_events;
this->poll.user_data = this;
spa_poll_add_item (this->main_loop, &this->poll);
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;
@ -372,7 +357,7 @@ v4l2_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

@ -28,6 +28,7 @@
#include <spa/video/format.h>
#include <spa/list.h>
#include <spa/log.h>
#include <spa/loop.h>
#include <spa/id-map.h>
#include <lib/debug.h>
#include <lib/props.h>
@ -88,8 +89,8 @@ typedef struct {
typedef struct {
SpaLog *log;
SpaPoll *main_loop;
SpaPoll *data_loop;
SpaLoop *main_loop;
SpaLoop *data_loop;
bool export_buf;
bool started;
@ -115,8 +116,8 @@ typedef struct {
unsigned int n_buffers;
SpaList ready;
SpaPollFd fds[1];
SpaPollItem poll;
bool source_enabled;
SpaSource source;
SpaPortInfo info;
SpaAllocParam *params[2];
@ -228,7 +229,7 @@ spa_v4l2_source_node_set_props (SpaNode *node,
}
static SpaResult
do_pause_done (SpaPoll *poll,
do_pause_done (SpaLoop *loop,
bool async,
uint32_t seq,
size_t size,
@ -253,7 +254,7 @@ do_pause_done (SpaPoll *poll,
}
static SpaResult
do_pause (SpaPoll *poll,
do_pause (SpaLoop *loop,
bool async,
uint32_t seq,
size_t size,
@ -275,7 +276,7 @@ do_pause (SpaPoll *poll,
ac.event.size = sizeof (SpaNodeEventAsyncComplete);
ac.seq = seq;
ac.res = res;
spa_poll_invoke (this->state[0].main_loop,
spa_loop_invoke (this->state[0].main_loop,
do_pause_done,
seq,
sizeof (ac),
@ -286,7 +287,7 @@ do_pause (SpaPoll *poll,
}
static SpaResult
do_start_done (SpaPoll *poll,
do_start_done (SpaLoop *loop,
bool async,
uint32_t seq,
size_t size,
@ -307,7 +308,7 @@ do_start_done (SpaPoll *poll,
}
static SpaResult
do_start (SpaPoll *poll,
do_start (SpaLoop *loop,
bool async,
uint32_t seq,
size_t size,
@ -329,7 +330,7 @@ do_start (SpaPoll *poll,
ac.event.size = sizeof (SpaNodeEventAsyncComplete);
ac.seq = seq;
ac.res = res;
spa_poll_invoke (this->state[0].main_loop,
spa_loop_invoke (this->state[0].main_loop,
do_start_done,
seq,
sizeof (ac),
@ -372,7 +373,7 @@ spa_v4l2_source_node_send_command (SpaNode *node,
if ((res = spa_v4l2_stream_on (this)) < 0)
return res;
return spa_poll_invoke (this->state[0].data_loop,
return spa_loop_invoke (this->state[0].data_loop,
do_start,
++this->seq,
command->size,
@ -392,7 +393,7 @@ spa_v4l2_source_node_send_command (SpaNode *node,
if (!state->started)
return SPA_RESULT_OK;
return spa_poll_invoke (this->state[0].data_loop,
return spa_loop_invoke (this->state[0].data_loop,
do_pause,
++this->seq,
command->size,
@ -1007,9 +1008,9 @@ v4l2_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__MainLoop) == 0)
else if (strcmp (support[i].uri, SPA_LOOP__MainLoop) == 0)
this->state[0].main_loop = support[i].data;
else if (strcmp (support[i].uri, SPA_POLL__DataLoop) == 0)
else if (strcmp (support[i].uri, SPA_LOOP__DataLoop) == 0)
this->state[0].data_loop = support[i].data;
}
if (this->map == NULL) {

View file

@ -9,7 +9,7 @@
#define CLEAR(x) memset(&(x), 0, sizeof(x))
static int v4l2_on_fd_events (SpaPollNotifyData *data);
static void v4l2_on_fd_events (SpaSource *source);
static int
xioctl (int fd, int request, void *arg)
@ -69,19 +69,11 @@ spa_v4l2_open (SpaV4l2Source *this)
return -1;
}
state->fds[0].fd = state->fd;
state->fds[0].events = POLLIN | POLLPRI | POLLERR;
state->fds[0].revents = 0;
state->poll.id = 0;
state->poll.enabled = false;
state->poll.fds = state->fds;
state->poll.n_fds = 1;
state->poll.idle_cb = NULL;
state->poll.before_cb = NULL;
state->poll.after_cb = v4l2_on_fd_events;
state->poll.user_data = this;
spa_poll_add_item (state->data_loop, &state->poll);
state->source.func = v4l2_on_fd_events;
state->source.data = this;
state->source.fd = state->fd;
state->source.mask = SPA_IO_IN | SPA_IO_ERR;
state->source.rmask = 0;
state->opened = true;
@ -158,7 +150,8 @@ spa_v4l2_close (SpaV4l2Source *this)
spa_log_info (state->log, "v4l2: close");
spa_poll_remove_item (state->data_loop, &state->poll);
if (state->source_enabled)
spa_loop_remove_source (state->data_loop, &state->source);
if (close(state->fd))
perror ("close");
@ -890,27 +883,25 @@ mmap_read (SpaV4l2Source *this)
return SPA_RESULT_OK;
}
static int
v4l2_on_fd_events (SpaPollNotifyData *data)
static void
v4l2_on_fd_events (SpaSource *source)
{
SpaV4l2Source *this = data->user_data;
SpaV4l2Source *this = source->data;
SpaNodeEventHaveOutput ho;
if (data->fds[0].revents & POLLERR)
return -1;
if (source->rmask & SPA_IO_ERR)
return;
if (!(data->fds[0].revents & POLLIN))
return 0;
if (!(source->rmask & SPA_IO_IN))
return;
if (mmap_read (this) < 0)
return 0;
return;
ho.event.type = SPA_NODE_EVENT_TYPE_HAVE_OUTPUT;
ho.event.size = sizeof (ho);
ho.port_id = 0;
this->event_cb (&this->node, &ho.event, this->user_data);
return 0;
}
static SpaResult
@ -1150,8 +1141,13 @@ static SpaResult
spa_v4l2_port_set_enabled (SpaV4l2Source *this, bool enabled)
{
SpaV4l2State *state = &this->state[0];
state->poll.enabled = enabled;
spa_poll_update_item (state->data_loop, &state->poll);
if (state->source_enabled != enabled) {
state->source_enabled = enabled;
if (enabled)
spa_loop_add_source (state->data_loop, &state->source);
else
spa_loop_remove_source (state->data_loop, &state->source);
}
return SPA_RESULT_OK;
}

View file

@ -23,10 +23,10 @@
#include <string.h>
#include <stdio.h>
#include <sys/timerfd.h>
#include <poll.h>
#include <spa/id-map.h>
#include <spa/log.h>
#include <spa/loop.h>
#include <spa/node.h>
#include <spa/list.h>
#include <spa/video/format.h>
@ -74,15 +74,15 @@ struct _SpaVideoTestSrc {
URI uri;
SpaIDMap *map;
SpaLog *log;
SpaPoll *data_loop;
SpaLoop *data_loop;
SpaVideoTestSrcProps props[2];
SpaNodeEventCallback event_cb;
void *user_data;
SpaPollItem timer;
SpaPollFd fds[1];
SpaSource timer_source;
bool timer_enabled;
struct itimerspec timerspec;
SpaPortInfo info;
@ -205,18 +205,18 @@ fill_buffer (SpaVideoTestSrc *this, VTSBuffer *b)
return SPA_RESULT_OK;
}
static SpaResult update_poll_enabled (SpaVideoTestSrc *this, bool enabled);
static SpaResult update_loop_enabled (SpaVideoTestSrc *this, bool enabled);
static int
videotestsrc_on_output (SpaPollNotifyData *data)
static void
videotestsrc_on_output (SpaSource *source)
{
SpaVideoTestSrc *this = data->user_data;
SpaVideoTestSrc *this = source->data;
VTSBuffer *b;
if (spa_list_is_empty (&this->empty)) {
if (!this->props[1].live)
update_poll_enabled (this, false);
return 0;
update_loop_enabled (this, false);
return;
}
b = spa_list_first (&this->empty, VTSBuffer, list);
spa_list_remove (&b->list);
@ -235,19 +235,17 @@ videotestsrc_on_output (SpaPollNotifyData *data)
if (this->props[1].live) {
uint64_t expirations, next_time;
if (read (this->fds[0].fd, &expirations, sizeof (uint64_t)) < sizeof (uint64_t))
if (read (this->timer_source.fd, &expirations, sizeof (uint64_t)) < sizeof (uint64_t))
perror ("read timerfd");
next_time = this->start_time + this->elapsed_time;
this->timerspec.it_value.tv_sec = next_time / SPA_NSEC_PER_SEC;
this->timerspec.it_value.tv_nsec = next_time % SPA_NSEC_PER_SEC;
timerfd_settime (this->fds[0].fd, TFD_TIMER_ABSTIME, &this->timerspec, NULL);
timerfd_settime (this->timer_source.fd, TFD_TIMER_ABSTIME, &this->timerspec, NULL);
}
spa_list_insert (this->ready.prev, &b->list);
send_have_output (this);
return 0;
}
static void
@ -257,10 +255,15 @@ update_state (SpaVideoTestSrc *this, SpaNodeState state)
}
static SpaResult
update_poll_enabled (SpaVideoTestSrc *this, bool enabled)
update_loop_enabled (SpaVideoTestSrc *this, bool enabled)
{
if (this->event_cb && this->timer.enabled != enabled) {
this->timer.enabled = enabled;
if (this->event_cb && this->timer_enabled != enabled) {
this->timer_enabled = enabled;
if (enabled)
this->timer_source.mask = SPA_IO_IN;
else
this->timer_source.mask = 0;
if (this->props[1].live) {
if (enabled) {
uint64_t next_time = this->start_time + this->elapsed_time;
@ -271,18 +274,9 @@ update_poll_enabled (SpaVideoTestSrc *this, bool enabled)
this->timerspec.it_value.tv_sec = 0;
this->timerspec.it_value.tv_nsec = 0;
}
timerfd_settime (this->fds[0].fd, TFD_TIMER_ABSTIME, &this->timerspec, NULL);
this->timer.fds = this->fds;
this->timer.n_fds = 1;
this->timer.idle_cb = NULL;
this->timer.after_cb = videotestsrc_on_output;
} else {
this->timer.fds = NULL;
this->timer.n_fds = 0;
this->timer.idle_cb = videotestsrc_on_output;
this->timer.after_cb = NULL;
timerfd_settime (this->timer_source.fd, TFD_TIMER_ABSTIME, &this->timerspec, NULL);
}
spa_poll_update_item (this->data_loop, &this->timer);
spa_loop_update_source (this->data_loop, &this->timer_source);
}
return SPA_RESULT_OK;
}
@ -324,7 +318,7 @@ spa_videotestsrc_node_send_command (SpaNode *node,
this->elapsed_time = 0;
this->started = true;
update_poll_enabled (this, true);
update_loop_enabled (this, true);
update_state (this, SPA_NODE_STATE_STREAMING);
break;
}
@ -340,7 +334,7 @@ spa_videotestsrc_node_send_command (SpaNode *node,
return SPA_RESULT_OK;
this->started = false;
update_poll_enabled (this, false);
update_loop_enabled (this, false);
update_state (this, SPA_NODE_STATE_PAUSED);
break;
}
@ -365,15 +359,14 @@ spa_videotestsrc_node_set_event_callback (SpaNode *node,
this = SPA_CONTAINER_OF (node, SpaVideoTestSrc, node);
if (event_cb == NULL && this->event_cb) {
spa_poll_remove_item (this->data_loop, &this->timer);
}
if (event_cb == NULL && this->event_cb)
spa_loop_remove_source (this->data_loop, &this->timer_source);
this->event_cb = event_cb;
this->user_data = user_data;
if (this->event_cb) {
spa_poll_add_item (this->data_loop, &this->timer);
spa_loop_add_source (this->data_loop, &this->timer_source);
}
return SPA_RESULT_OK;
}
@ -748,7 +741,7 @@ spa_videotestsrc_node_port_reuse_buffer (SpaNode *node,
spa_list_insert (this->empty.prev, &b->list);
if (!this->props[1].live)
update_poll_enabled (this, true);
update_loop_enabled (this, true);
return SPA_RESULT_OK;
}
@ -910,7 +903,7 @@ videotestsrc_clear (SpaHandle *handle)
this = (SpaVideoTestSrc *) handle;
close (this->fds[0].fd);
close (this->timer_source.fd);
return SPA_RESULT_OK;
}
@ -938,7 +931,7 @@ videotestsrc_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;
}
if (this->map == NULL) {
@ -957,21 +950,16 @@ videotestsrc_init (const SpaHandleFactory *factory,
spa_list_init (&this->empty);
spa_list_init (&this->ready);
this->fds[0].fd = timerfd_create (CLOCK_MONOTONIC, TFD_NONBLOCK | TFD_CLOEXEC);
this->fds[0].events = POLLIN | POLLPRI | POLLERR;
this->fds[0].revents = 0;
this->timer_source.func = videotestsrc_on_output;
this->timer_source.data = this;
this->timer_source.fd = timerfd_create (CLOCK_MONOTONIC, TFD_NONBLOCK | TFD_CLOEXEC);
this->timer_source.mask = SPA_IO_IN;
this->timer_source.rmask = 0;
this->timerspec.it_value.tv_sec = 0;
this->timerspec.it_value.tv_nsec = 0;
this->timerspec.it_interval.tv_sec = 0;
this->timerspec.it_interval.tv_nsec = 0;
this->timer.id = 0;
this->timer.enabled = false;
this->timer.idle_cb = NULL;
this->timer.before_cb = NULL;
this->timer.after_cb = NULL;
this->timer.user_data = this;
this->info.flags = SPA_PORT_INFO_FLAG_CAN_USE_BUFFERS |
SPA_PORT_INFO_FLAG_NO_REF;
if (this->props[1].live)