From f0aafb5b51a4b2d0fad05be3273d4946109895ad Mon Sep 17 00:00:00 2001 From: Wim Taymans Date: Thu, 20 Apr 2017 19:25:14 +0200 Subject: [PATCH] alsa: work on ringbuffer data transport Add ringbuffer test --- pinos/modules/module-flatpak.c | 4 + spa/include/spa/ringbuffer.h | 2 +- spa/plugins/alsa/alsa-sink.c | 14 +- spa/plugins/alsa/alsa-utils.c | 145 +++---- spa/plugins/alsa/alsa-utils.h | 2 - spa/plugins/audiotestsrc/audiotestsrc.c | 34 +- spa/tests/meson.build | 6 +- spa/tests/test-mixer.c | 6 +- spa/tests/test-ringbuffer.c | 489 ++++++++++++++++++++++++ 9 files changed, 589 insertions(+), 113 deletions(-) create mode 100644 spa/tests/test-ringbuffer.c diff --git a/pinos/modules/module-flatpak.c b/pinos/modules/module-flatpak.c index 10620278d..402fabb24 100644 --- a/pinos/modules/module-flatpak.c +++ b/pinos/modules/module-flatpak.c @@ -82,6 +82,8 @@ close_request (AsyncPending *p) DBusMessage *m = NULL; ModuleImpl *impl = p->info->impl; + pinos_log_debug ("pending %p: handle %s", p, p->handle); + if (!(m = dbus_message_new_method_call ("org.freedesktop.portal.Request", p->handle, "org.freedesktop.portal.Request", @@ -116,6 +118,7 @@ free_pending (PinosAccessData *d) if (!p->handled) close_request(p); + pinos_log_debug ("pending %p: handle %s", p, p->handle); spa_list_remove (&p->link); free (p->handle); } @@ -134,6 +137,7 @@ add_pending (ClientInfo *cinfo, const char *handle, PinosAccessData *access_data p->handle = strdup (handle); p->access_data = ad; p->handled = false; + pinos_log_debug ("pending %p: handle %s", p, handle); spa_list_insert (cinfo->async_pending.prev, &p->link); } diff --git a/spa/include/spa/ringbuffer.h b/spa/include/spa/ringbuffer.h index abff0130a..e559bd6de 100644 --- a/spa/include/spa/ringbuffer.h +++ b/spa/include/spa/ringbuffer.h @@ -160,7 +160,7 @@ spa_ringbuffer_read_advance (SpaRingbuffer *rbuf, * * Returns: the fill level of @rbuf. values < 0 mean * there was an underrun. values > rbuf->size means there - * was an overrun. Subsctract from the buffer size to get + * was an overrun. Subtract from the buffer size to get * the number of bytes available for writing. */ static inline int32_t diff --git a/spa/plugins/alsa/alsa-sink.c b/spa/plugins/alsa/alsa-sink.c index d69a06633..61218e7e3 100644 --- a/spa/plugins/alsa/alsa-sink.c +++ b/spa/plugins/alsa/alsa-sink.c @@ -266,7 +266,6 @@ spa_alsa_clear_buffers (SpaALSASink *this) if (this->n_buffers > 0) { spa_list_init (&this->ready); this->n_buffers = 0; - this->ringbuffer = NULL; } return SPA_RESULT_OK; } @@ -450,8 +449,6 @@ spa_alsa_sink_node_port_use_buffers (SpaNode *node, b->h = spa_buffer_find_meta (b->outbuf, SPA_META_TYPE_HEADER); b->rb = spa_buffer_find_meta (b->outbuf, SPA_META_TYPE_RINGBUFFER); - if (b->rb) - this->ringbuffer = b; switch (buffers[i]->datas[0].type) { case SPA_DATA_TYPE_MEMFD: @@ -583,13 +580,10 @@ spa_alsa_sink_node_process_input (SpaNode *node) input->status = SPA_RESULT_INVALID_BUFFER_ID; return SPA_RESULT_ERROR; } - if (this->ringbuffer) { - this->ringbuffer->outstanding = true; - this->ringbuffer = b; - } else { - spa_list_insert (this->ready.prev, &b->link); - spa_log_trace (this->log, "alsa-sink %p: queue buffer %u", this, input->buffer_id); - } + + spa_log_trace (this->log, "alsa-sink %p: queue buffer %u", this, input->buffer_id); + + spa_list_insert (this->ready.prev, &b->link); b->outstanding = false; input->buffer_id = SPA_ID_INVALID; input->status = SPA_RESULT_OK; diff --git a/spa/plugins/alsa/alsa-utils.c b/spa/plugins/alsa/alsa-utils.c index f2860a29d..2d4da65dc 100644 --- a/spa/plugins/alsa/alsa-utils.c +++ b/spa/plugins/alsa/alsa-utils.c @@ -325,11 +325,11 @@ set_swparams (SpaALSAState *state) return 0; } -static snd_pcm_uframes_t -pull_frames_queue (SpaALSAState *state, - const snd_pcm_channel_area_t *my_areas, - snd_pcm_uframes_t offset, - snd_pcm_uframes_t frames) +static inline snd_pcm_uframes_t +pull_frames (SpaALSAState *state, + const snd_pcm_channel_area_t *my_areas, + snd_pcm_uframes_t offset, + snd_pcm_uframes_t frames) { snd_pcm_uframes_t total_frames = 0, to_write = frames; SpaPortIO *io = state->io; @@ -348,22 +348,46 @@ pull_frames_queue (SpaALSAState *state, size_t n_bytes, n_frames, size; off_t offs; SpaALSABuffer *b; + bool reuse = false; + SpaData *d; b = spa_list_first (&state->ready, SpaALSABuffer, link); + d = b->outbuf->datas; - offs = SPA_MIN (b->outbuf->datas[0].chunk->offset, b->outbuf->datas[0].maxsize); - src = SPA_MEMBER (b->outbuf->datas[0].data, offs, uint8_t); - size = SPA_MIN (b->outbuf->datas[0].chunk->size, b->outbuf->datas[0].maxsize - offs); - - src = SPA_MEMBER (src, state->ready_offset, uint8_t); dst = SPA_MEMBER (my_areas[0].addr, offset * state->frame_size, uint8_t); - n_bytes = SPA_MIN (size - state->ready_offset, to_write * state->frame_size); - n_frames = SPA_MIN (to_write, n_bytes / state->frame_size); - memcpy (dst, src, n_bytes); + if (b->rb) { + SpaRingbuffer *ringbuffer = &b->rb->ringbuffer; + uint32_t index; + int32_t avail; - state->ready_offset += n_bytes; - if (state->ready_offset >= size) { + avail = spa_ringbuffer_get_read_index (ringbuffer, &index); + + n_bytes = SPA_MIN (avail, to_write * state->frame_size); + n_frames = SPA_MIN (to_write, n_bytes / state->frame_size); + + spa_ringbuffer_read_data (ringbuffer, + d[0].data, + index & ringbuffer->mask, + dst, + n_bytes); + + spa_ringbuffer_read_advance (ringbuffer, n_bytes); + reuse = avail == n_bytes; + } else { + offs = SPA_MIN (d[0].chunk->offset + state->ready_offset, d[0].maxsize); + size = SPA_MIN (d[0].chunk->size, d[0].maxsize - offs); + src = SPA_MEMBER (d[0].data, offs, uint8_t); + + n_bytes = SPA_MIN (size, to_write * state->frame_size); + n_frames = SPA_MIN (to_write, n_bytes / state->frame_size); + + memcpy (dst, src, n_bytes); + + state->ready_offset += n_bytes; + reuse = (state->ready_offset >= size); + } + if (reuse) { SpaEventNodeReuseBuffer rb = SPA_EVENT_NODE_REUSE_BUFFER_INIT (state->type.event_node.ReuseBuffer, 0, b->outbuf->id); @@ -378,7 +402,7 @@ pull_frames_queue (SpaALSAState *state, to_write -= n_frames; } if (total_frames == 0) { - total_frames = state->threshold; + total_frames = SPA_MIN (frames, state->threshold); spa_log_warn (state->log, "underrun, want %zd frames", total_frames); snd_pcm_areas_silence (my_areas, offset, state->channels, total_frames, state->format); } @@ -386,58 +410,13 @@ pull_frames_queue (SpaALSAState *state, } static snd_pcm_uframes_t -pull_frames_ringbuffer (SpaALSAState *state, - const snd_pcm_channel_area_t *my_areas, - snd_pcm_uframes_t offset, - snd_pcm_uframes_t frames) -{ - int32_t avail; - uint32_t index; - size_t size; - SpaALSABuffer *b; - uint8_t *src, *dst; - - b = state->ringbuffer; - - src = b->outbuf->datas[0].data; - dst = SPA_MEMBER (my_areas[0].addr, offset * state->frame_size, uint8_t); - - avail = spa_ringbuffer_get_read_index (&b->rb->ringbuffer, &index); - - size = SPA_MIN (avail, frames * state->frame_size); - - spa_log_trace (state->log, "%u %d %zd %zd", index, avail, offset, size); - - if (size > 0) { - spa_ringbuffer_read_data (&b->rb->ringbuffer, - src, - index & b->rb->ringbuffer.mask, - dst, - size); - spa_ringbuffer_read_advance (&b->rb->ringbuffer, size); - frames = size / state->frame_size; - } else { - spa_log_warn (state->log, "underrun"); - snd_pcm_areas_silence (my_areas, offset, state->channels, frames, state->format); - } - - b->outstanding = true; - { - SpaEventNodeReuseBuffer rb = SPA_EVENT_NODE_REUSE_BUFFER_INIT (state->type.event_node.ReuseBuffer, - 0, b->outbuf->id); - state->event_cb (&state->node, (SpaEvent*)&rb, state->user_data); - } - - return frames; -} - -static snd_pcm_uframes_t -push_frames_queue (SpaALSAState *state, - const snd_pcm_channel_area_t *my_areas, - snd_pcm_uframes_t offset, - snd_pcm_uframes_t frames) +push_frames (SpaALSAState *state, + const snd_pcm_channel_area_t *my_areas, + snd_pcm_uframes_t offset, + snd_pcm_uframes_t frames) { snd_pcm_uframes_t total_frames = 0; + SpaPortIO *io = state->io; if (spa_list_is_empty (&state->free)) { spa_log_warn (state->log, "no more buffers"); @@ -447,7 +426,6 @@ push_frames_queue (SpaALSAState *state, size_t n_bytes; SpaALSABuffer *b; SpaData *d; - SpaPortIO *io; b = spa_list_first (&state->free, SpaALSABuffer, link); spa_list_remove (&b->link); @@ -470,7 +448,7 @@ push_frames_queue (SpaALSAState *state, d[0].chunk->size = n_bytes; d[0].chunk->stride = 0; - if ((io = state->io)) { + { SpaEvent event = SPA_EVENT_INIT (state->type.event_node.HaveOutput); b->outstanding = true; @@ -483,15 +461,6 @@ push_frames_queue (SpaALSAState *state, return total_frames; } -static snd_pcm_uframes_t -push_frames_ringbuffer (SpaALSAState *state, - const snd_pcm_channel_area_t *my_areas, - snd_pcm_uframes_t offset, - snd_pcm_uframes_t frames) -{ - return frames; -} - static int alsa_try_resume (SpaALSAState *state) { @@ -518,9 +487,9 @@ calc_timeout (size_t target, ts->tv_sec = now->tv_sec; ts->tv_nsec = now->tv_nsec; if (target > current) - ts->tv_nsec += (target - current) * SPA_NSEC_PER_SEC / rate; + ts->tv_nsec += ((target - current) * SPA_NSEC_PER_SEC) / rate; - while (ts->tv_nsec > SPA_NSEC_PER_SEC) { + while (ts->tv_nsec >= SPA_NSEC_PER_SEC) { ts->tv_sec++; ts->tv_nsec -= SPA_NSEC_PER_SEC; } @@ -579,10 +548,7 @@ alsa_on_playback_timeout_event (SpaSource *source) return; } - if (state->ringbuffer) - written = pull_frames_ringbuffer (state, my_areas, offset, frames); - else - written = pull_frames_queue (state, my_areas, offset, frames); + written = pull_frames (state, my_areas, offset, frames); if (written < frames) to_write = 0; @@ -607,8 +573,9 @@ alsa_on_playback_timeout_event (SpaSource *source) calc_timeout (total_written + filled, state->threshold, state->rate, &htstamp, &ts.it_value); - spa_log_trace (state->log, "timeout %ld %ld %ld %ld", total_written, filled, - ts.it_value.tv_sec, ts.it_value.tv_nsec); + spa_log_trace (state->log, "timeout %ld %ld %ld %ld %ld", total_written, filled, + ts.it_value.tv_sec, ts.it_value.tv_nsec, + ts.it_value.tv_nsec - htstamp.tv_nsec); ts.it_interval.tv_sec = 0; ts.it_interval.tv_nsec = 0; @@ -663,12 +630,8 @@ alsa_on_capture_timeout_event (SpaSource *source) return; } - if (state->ringbuffer) - read = push_frames_ringbuffer (state, my_areas, offset, frames); - else - read = push_frames_queue (state, my_areas, offset, frames); - - if (read < to_read) + read = push_frames (state, my_areas, offset, frames); + if (read < frames) to_read = 0; if ((res = snd_pcm_mmap_commit (hndl, offset, read)) < 0) { diff --git a/spa/plugins/alsa/alsa-utils.h b/spa/plugins/alsa/alsa-utils.h index d6fd37920..a6d9216ae 100644 --- a/spa/plugins/alsa/alsa-utils.h +++ b/spa/plugins/alsa/alsa-utils.h @@ -144,8 +144,6 @@ struct _SpaALSAState { SpaALSABuffer buffers[MAX_BUFFERS]; unsigned int n_buffers; - bool use_ringbuffer; - SpaALSABuffer *ringbuffer; SpaList free; SpaList ready; diff --git a/spa/plugins/audiotestsrc/audiotestsrc.c b/spa/plugins/audiotestsrc/audiotestsrc.c index f9d434f1a..883772763 100644 --- a/spa/plugins/audiotestsrc/audiotestsrc.c +++ b/spa/plugins/audiotestsrc/audiotestsrc.c @@ -98,6 +98,7 @@ struct _ATSBuffer { SpaBuffer *outbuf; bool outstanding; SpaMetaHeader *h; + SpaMetaRingbuffer *rb; SpaList link; }; @@ -296,6 +297,7 @@ audiotestsrc_make_buffer (SpaAudioTestSrc *this) n_bytes = b->outbuf->datas[0].maxsize; if (io->range.min_size != 0) { + n_bytes = SPA_MIN (n_bytes, io->range.min_size); if (io->range.max_size < n_bytes) n_bytes = io->range.max_size; } @@ -303,12 +305,33 @@ audiotestsrc_make_buffer (SpaAudioTestSrc *this) spa_log_trace (this->log, "audiotestsrc %p: dequeue buffer %d %d %d", this, b->outbuf->id, b->outbuf->datas[0].maxsize, n_bytes); - n_samples = n_bytes / this->bpf; - this->render_func (this, b->outbuf->datas[0].data, n_samples); + if (b->rb) { + int32_t filled, avail; + uint32_t index, offset; - b->outbuf->datas[0].chunk->offset = 0; - b->outbuf->datas[0].chunk->size = n_bytes; - b->outbuf->datas[0].chunk->stride = 0; + filled = spa_ringbuffer_get_write_index (&b->rb->ringbuffer, &index); + avail = b->rb->ringbuffer.size - filled; + n_bytes = SPA_MIN (avail, n_bytes); + + n_samples = n_bytes / this->bpf; + + offset = index & b->rb->ringbuffer.mask; + + if (offset + n_bytes > b->rb->ringbuffer.size) { + uint32_t l0 = b->rb->ringbuffer.size - offset; + this->render_func (this, SPA_MEMBER (b->outbuf->datas[0].data, offset, void), l0 / this->bpf); + this->render_func (this, b->outbuf->datas[0].data, (n_bytes - l0) / this->bpf); + } else { + this->render_func (this, SPA_MEMBER (b->outbuf->datas[0].data, offset, void), n_samples); + } + spa_ringbuffer_write_advance (&b->rb->ringbuffer, n_bytes); + } else { + n_samples = n_bytes / this->bpf; + this->render_func (this, b->outbuf->datas[0].data, n_samples); + b->outbuf->datas[0].chunk->size = n_bytes; + b->outbuf->datas[0].chunk->offset = 0; + b->outbuf->datas[0].chunk->stride = 0; + } if (b->h) { b->h->seq = this->sample_count; @@ -703,6 +726,7 @@ spa_audiotestsrc_node_port_use_buffers (SpaNode *node, b->outbuf = buffers[i]; b->outstanding = false; b->h = spa_buffer_find_meta (buffers[i], SPA_META_TYPE_HEADER); + b->rb = spa_buffer_find_meta (buffers[i], SPA_META_TYPE_RINGBUFFER); switch (d[0].type) { case SPA_DATA_TYPE_MEMPTR: diff --git a/spa/tests/meson.build b/spa/tests/meson.build index 3d4f00c2a..bed9485ce 100644 --- a/spa/tests/meson.build +++ b/spa/tests/meson.build @@ -3,7 +3,11 @@ executable('test-mixer', 'test-mixer.c', dependencies : [dl_lib, pthread_lib], link_with : spalib, install : false) - +executable('test-ringbuffer', 'test-ringbuffer.c', + include_directories : [spa_inc, spa_libinc ], + dependencies : [dl_lib, pthread_lib], + link_with : spalib, + install : false) executable('test-v4l2', 'test-v4l2.c', include_directories : [spa_inc, spa_libinc ], dependencies : [dl_lib, sdl_dep, pthread_lib], diff --git a/spa/tests/test-mixer.c b/spa/tests/test-mixer.c index 05ae44ce2..dae67f4c9 100644 --- a/spa/tests/test-mixer.c +++ b/spa/tests/test-mixer.c @@ -312,8 +312,7 @@ make_nodes (AppData *data) spa_pod_builder_init (&b, buffer, sizeof (buffer)); spa_pod_builder_props (&b, &f[0], data->type.props, SPA_POD_PROP (&f[1], data->type.props_device, 0, SPA_POD_TYPE_STRING, 1, "hw:1"), - SPA_POD_PROP (&f[1], data->type.props_min_latency, 0, SPA_POD_TYPE_INT, 1, 256), - SPA_POD_PROP (&f[1], data->type.props_live, 0, SPA_POD_TYPE_BOOL, 1, false)); + SPA_POD_PROP (&f[1], data->type.props_min_latency, 0, SPA_POD_TYPE_INT, 1, 128)); props = SPA_POD_BUILDER_DEREF (&b, f[0].ref, SpaProps); if ((res = spa_node_set_props (data->sink, props)) < 0) @@ -336,7 +335,8 @@ make_nodes (AppData *data) spa_pod_builder_init (&b, buffer, sizeof (buffer)); spa_pod_builder_props (&b, &f[0], data->type.props, SPA_POD_PROP (&f[1], data->type.props_freq, 0, SPA_POD_TYPE_DOUBLE, 1, 600.0), - SPA_POD_PROP (&f[1], data->type.props_volume, 0, SPA_POD_TYPE_DOUBLE, 1, 0.5)); + SPA_POD_PROP (&f[1], data->type.props_volume, 0, SPA_POD_TYPE_DOUBLE, 1, 0.5), + SPA_POD_PROP (&f[1], data->type.props_live, 0, SPA_POD_TYPE_BOOL, 1, false)); props = SPA_POD_BUILDER_DEREF (&b, f[0].ref, SpaProps); if ((res = spa_node_set_props (data->source1, props)) < 0) diff --git a/spa/tests/test-ringbuffer.c b/spa/tests/test-ringbuffer.c new file mode 100644 index 000000000..ee1d4647d --- /dev/null +++ b/spa/tests/test-ringbuffer.c @@ -0,0 +1,489 @@ +/* Spa + * Copyright (C) 2017 Wim Taymans + * + * 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 library 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., 51 Franklin St, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +typedef struct { + uint32_t node; + uint32_t props; + uint32_t format; + uint32_t props_device; + uint32_t props_freq; + uint32_t props_volume; + uint32_t props_min_latency; + uint32_t props_live; + SpaTypeMediaType media_type; + SpaTypeMediaSubtype media_subtype; + SpaTypeFormatAudio format_audio; + SpaTypeAudioFormat audio_format; + SpaTypeEventNode event_node; + SpaTypeCommandNode command_node; +} Type; + +static inline void +init_type (Type *type, SpaTypeMap *map) +{ + type->node = spa_type_map_get_id (map, SPA_TYPE__Node); + type->props = spa_type_map_get_id (map, SPA_TYPE__Props); + type->format = spa_type_map_get_id (map, SPA_TYPE__Format); + type->props_device = spa_type_map_get_id (map, SPA_TYPE_PROPS__device); + type->props_freq = spa_type_map_get_id (map, SPA_TYPE_PROPS__frequency); + type->props_volume = spa_type_map_get_id (map, SPA_TYPE_PROPS__volume); + type->props_min_latency = spa_type_map_get_id (map, SPA_TYPE_PROPS__minLatency); + type->props_live = spa_type_map_get_id (map, SPA_TYPE_PROPS__live); + spa_type_media_type_map (map, &type->media_type); + spa_type_media_subtype_map (map, &type->media_subtype); + spa_type_format_audio_map (map, &type->format_audio); + spa_type_audio_format_map (map, &type->audio_format); + spa_type_event_node_map (map, &type->event_node); + spa_type_command_node_map (map, &type->command_node); +} + +typedef struct { + SpaBuffer buffer; + SpaMeta metas[2]; + SpaMetaHeader header; + SpaMetaRingbuffer rb; + SpaData datas[1]; + SpaChunk chunks[1]; +} Buffer; + +typedef struct { + SpaTypeMap *map; + SpaLog *log; + SpaLoop data_loop; + Type type; + + SpaSupport support[4]; + uint32_t n_support; + + SpaNode *sink; + SpaPortIO source_sink_io[1]; + + SpaNode *source; + SpaBuffer *source_buffers[1]; + Buffer source_buffer[1]; + + bool running; + pthread_t thread; + + SpaSource sources[16]; + unsigned int n_sources; + + bool rebuild_fds; + struct pollfd fds[16]; + unsigned int n_fds; +} AppData; + +#define BUFFER_SIZE 4096 + +static void +init_buffer (AppData *data, SpaBuffer **bufs, Buffer *ba, int n_buffers, size_t size) +{ + int i; + + for (i = 0; i < n_buffers; i++) { + Buffer *b = &ba[i]; + bufs[i] = &b->buffer; + + b->buffer.id = i; + b->buffer.n_metas = 2; + b->buffer.metas = b->metas; + b->buffer.n_datas = 1; + b->buffer.datas = b->datas; + + b->header.flags = 0; + b->header.seq = 0; + b->header.pts = 0; + b->header.dts_offset = 0; + b->metas[0].type = SPA_META_TYPE_HEADER; + b->metas[0].data = &b->header; + b->metas[0].size = sizeof (b->header); + + spa_ringbuffer_init (&b->rb.ringbuffer, size); + b->metas[1].type = SPA_META_TYPE_RINGBUFFER; + b->metas[1].data = &b->rb; + b->metas[1].size = sizeof (b->rb); + + b->datas[0].type = SPA_DATA_TYPE_MEMPTR; + b->datas[0].flags = 0; + b->datas[0].fd = -1; + b->datas[0].mapoffset = 0; + b->datas[0].maxsize = size; + b->datas[0].data = malloc (size); + b->datas[0].chunk = &b->chunks[0]; + b->datas[0].chunk->offset = 0; + b->datas[0].chunk->size = size; + b->datas[0].chunk->stride = 0; + } +} + +static SpaResult +make_node (AppData *data, SpaNode **node, const char *lib, const char *name, bool async) +{ + SpaHandle *handle; + SpaResult res; + void *hnd; + SpaEnumHandleFactoryFunc enum_func; + unsigned int i; + uint32_t state = 0; + SpaDictItem items[1]; + SpaDict dict = SPA_DICT_INIT (1, items); + + if ((hnd = dlopen (lib, RTLD_NOW)) == NULL) { + printf ("can't load %s: %s\n", lib, dlerror()); + return SPA_RESULT_ERROR; + } + if ((enum_func = dlsym (hnd, "spa_enum_handle_factory")) == NULL) { + printf ("can't find enum function\n"); + return SPA_RESULT_ERROR; + } + + items[0].key = "asynchronous"; + items[0].value = async ? "1" : "0"; + + for (i = 0; ;i++) { + const SpaHandleFactory *factory; + void *iface; + + if ((res = enum_func (&factory, state++)) < 0) { + if (res != SPA_RESULT_ENUM_END) + printf ("can't enumerate factories: %d\n", res); + break; + } + if (strcmp (factory->name, name)) + continue; + + handle = calloc (1, factory->size); + if ((res = spa_handle_factory_init (factory, handle, &dict, data->support, data->n_support)) < 0) { + printf ("can't make factory instance: %d\n", res); + return res; + } + if ((res = spa_handle_get_interface (handle, data->type.node, &iface)) < 0) { + printf ("can't get interface %d\n", res); + return res; + } + *node = iface; + return SPA_RESULT_OK; + } + return SPA_RESULT_ERROR; +} + +static void +on_sink_event (SpaNode *node, SpaEvent *event, void *user_data) +{ + AppData *data = user_data; + SpaResult res; + + if (SPA_EVENT_TYPE (event) == data->type.event_node.NeedInput) { + res = spa_node_process_output (data->source); + if (res != SPA_RESULT_HAVE_OUTPUT) + printf ("got process_output error from source %d\n", res); + + if ((res = spa_node_process_input (data->sink)) < 0) + printf ("got process_input error from sink %d\n", res); + } + else if (SPA_EVENT_TYPE (event) == data->type.event_node.ReuseBuffer) { + SpaEventNodeReuseBuffer *rb = (SpaEventNodeReuseBuffer *) event; + + data->source_sink_io[0].buffer_id = rb->body.buffer_id.value; + } + else { + printf ("got event %d\n", SPA_EVENT_TYPE (event)); + } +} + +static SpaResult +do_add_source (SpaLoop *loop, + SpaSource *source) +{ + AppData *data = SPA_CONTAINER_OF (loop, AppData, data_loop); + + data->sources[data->n_sources] = *source; + data->n_sources++; + data->rebuild_fds = true; + + return SPA_RESULT_OK; +} + +static SpaResult +do_update_source (SpaSource *source) +{ + return SPA_RESULT_OK; +} + +static void +do_remove_source (SpaSource *source) +{ +} + +static SpaResult +do_invoke (SpaLoop *loop, + SpaInvokeFunc func, + uint32_t seq, + size_t size, + void *data, + void *user_data) +{ + return func (loop, false, seq, size, data, user_data); +} + +static SpaResult +make_nodes (AppData *data) +{ + SpaResult res; + SpaProps *props; + SpaPODBuilder b = { 0 }; + SpaPODFrame f[2]; + uint8_t buffer[128]; + + if ((res = make_node (data, &data->sink, + "build/spa/plugins/alsa/libspa-alsa.so", + "alsa-sink", true)) < 0) { + printf ("can't create alsa-sink: %d\n", res); + return res; + } + spa_node_set_event_callback (data->sink, on_sink_event, data); + + spa_pod_builder_init (&b, buffer, sizeof (buffer)); + spa_pod_builder_props (&b, &f[0], data->type.props, + SPA_POD_PROP (&f[1], data->type.props_device, 0, SPA_POD_TYPE_STRING, 1, "hw:1"), + SPA_POD_PROP (&f[1], data->type.props_min_latency, 0, SPA_POD_TYPE_INT, 1, 128)); + props = SPA_POD_BUILDER_DEREF (&b, f[0].ref, SpaProps); + + if ((res = spa_node_set_props (data->sink, props)) < 0) + printf ("got set_props error %d\n", res); + + if ((res = make_node (data, &data->source, + "build/spa/plugins/audiotestsrc/libspa-audiotestsrc.so", + "audiotestsrc", false)) < 0) { + printf ("can't create audiotestsrc: %d\n", res); + return res; + } + + spa_pod_builder_init (&b, buffer, sizeof (buffer)); + spa_pod_builder_props (&b, &f[0], data->type.props, + SPA_POD_PROP (&f[1], data->type.props_live, 0, SPA_POD_TYPE_BOOL, 1, false)); + props = SPA_POD_BUILDER_DEREF (&b, f[0].ref, SpaProps); + + if ((res = spa_node_set_props (data->source, props)) < 0) + printf ("got set_props error %d\n", res); + return res; +} + +static SpaResult +negotiate_formats (AppData *data) +{ + SpaResult res; + SpaFormat *format, *filter; + uint32_t state = 0; + SpaPODBuilder b = { 0 }; + SpaPODFrame f[2]; + uint8_t buffer[256]; + + spa_pod_builder_init (&b, buffer, sizeof (buffer)); + spa_pod_builder_format (&b, &f[0], data->type.format, + data->type.media_type.audio, data->type.media_subtype.raw, + SPA_POD_PROP (&f[1], data->type.format_audio.format, 0, + SPA_POD_TYPE_ID, 1, + data->type.audio_format.S16), + SPA_POD_PROP (&f[1], data->type.format_audio.layout, 0, + SPA_POD_TYPE_INT, 1, + SPA_AUDIO_LAYOUT_INTERLEAVED), + SPA_POD_PROP (&f[1], data->type.format_audio.rate, 0, + SPA_POD_TYPE_INT, 1, + 44100), + SPA_POD_PROP (&f[1], data->type.format_audio.channels, 0, + SPA_POD_TYPE_INT, 1, + 2)); + filter = SPA_POD_BUILDER_DEREF (&b, f[0].ref, SpaFormat); + + if ((res = spa_node_port_enum_formats (data->sink, SPA_DIRECTION_INPUT, 0, &format, filter, state)) < 0) + return res; + + + if ((res = spa_node_port_set_format (data->sink, SPA_DIRECTION_INPUT, 0, 0, format)) < 0) + return res; + + spa_node_port_set_io (data->source, SPA_DIRECTION_OUTPUT, 0, &data->source_sink_io[0]); + spa_node_port_set_io (data->sink, SPA_DIRECTION_INPUT, 0, &data->source_sink_io[0]); + + if ((res = spa_node_port_set_format (data->source, SPA_DIRECTION_OUTPUT, 0, 0, format)) < 0) + return res; + + init_buffer (data, data->source_buffers, data->source_buffer, 1, BUFFER_SIZE); + if ((res = spa_node_port_use_buffers (data->sink, SPA_DIRECTION_INPUT, 0, data->source_buffers, 1)) < 0) + return res; + if ((res = spa_node_port_use_buffers (data->source, SPA_DIRECTION_OUTPUT, 0, data->source_buffers, 1)) < 0) + return res; + + return SPA_RESULT_OK; +} + +static void * +loop (void *user_data) +{ + AppData *data = user_data; + + printf ("enter thread %d\n", data->n_sources); + while (data->running) { + int i, r; + + /* rebuild */ + if (data->rebuild_fds) { + for (i = 0; i < data->n_sources; i++) { + SpaSource *p = &data->sources[i]; + data->fds[i].fd = p->fd; + data->fds[i].events = p->mask; + } + data->n_fds = data->n_sources; + data->rebuild_fds = false; + } + + r = poll ((struct pollfd *) data->fds, data->n_fds, -1); + if (r < 0) { + if (errno == EINTR) + continue; + break; + } + if (r == 0) { + fprintf (stderr, "select timeout"); + break; + } + + /* after */ + for (i = 0; i < data->n_sources; i++) { + SpaSource *p = &data->sources[i]; + p->rmask = 0; + if (data->fds[i].revents & POLLIN) + p->rmask |= SPA_IO_IN; + if (data->fds[i].revents & POLLOUT) + p->rmask |= SPA_IO_OUT; + if (data->fds[i].revents & POLLHUP) + p->rmask |= SPA_IO_HUP; + if (data->fds[i].revents & POLLERR) + p->rmask |= SPA_IO_ERR; + } + for (i = 0; i < data->n_sources; i++) { + SpaSource *p = &data->sources[i]; + if (p->rmask) + p->func (p); + } + } + printf ("leave thread\n"); + + return NULL; +} + +static void +run_async_sink (AppData *data) +{ + SpaResult res; + int err; + + { + SpaCommand cmd = SPA_COMMAND_INIT (data->type.command_node.Start); + if ((res = spa_node_send_command (data->source, &cmd)) < 0) + printf ("got source error %d\n", res); + if ((res = spa_node_send_command (data->sink, &cmd)) < 0) + printf ("got sink error %d\n", res); + } + + data->running = true; + if ((err = pthread_create (&data->thread, NULL, loop, data)) != 0) { + printf ("can't create thread: %d %s", err, strerror (err)); + data->running = false; + } + + printf ("sleeping for 1000 seconds\n"); + sleep (1000); + + if (data->running) { + data->running = false; + pthread_join (data->thread, NULL); + } + + { + SpaCommand cmd = SPA_COMMAND_INIT (data->type.command_node.Pause); + if ((res = spa_node_send_command (data->sink, &cmd)) < 0) + printf ("got sink error %d\n", res); + if ((res = spa_node_send_command (data->source, &cmd)) < 0) + printf ("got source error %d\n", res); + } +} + +int +main (int argc, char *argv[]) +{ + AppData data = { NULL }; + SpaResult res; + const char *str; + + data.map = spa_type_map_get_default(); + data.log = spa_log_get_default(); + data.data_loop.size = sizeof (SpaLoop); + data.data_loop.add_source = do_add_source; + data.data_loop.update_source = do_update_source; + data.data_loop.remove_source = do_remove_source; + data.data_loop.invoke = do_invoke; + + if ((str = getenv ("PINOS_DEBUG"))) + data.log->level = atoi (str); + + data.support[0].type = SPA_TYPE__TypeMap; + data.support[0].data = data.map; + data.support[1].type = SPA_TYPE__Log; + data.support[1].data = data.log; + data.support[2].type = SPA_TYPE_LOOP__DataLoop; + data.support[2].data = &data.data_loop; + data.support[3].type = SPA_TYPE_LOOP__MainLoop; + data.support[3].data = &data.data_loop; + data.n_support = 4; + + init_type (&data.type, data.map); + + if ((res = make_nodes (&data)) < 0) { + printf ("can't make nodes: %d\n", res); + return -1; + } + if ((res = negotiate_formats (&data)) < 0) { + printf ("can't negotiate nodes: %d\n", res); + return -1; + } + + run_async_sink (&data); +}