diff --git a/pinos/client/build_mkenum.py b/pinos/client/build_mkenum.py deleted file mode 100755 index 9ab27520d..000000000 --- a/pinos/client/build_mkenum.py +++ /dev/null @@ -1,54 +0,0 @@ -#!/usr/bin/env python3 - -# This is in its own file rather than inside meson.build -# because a) mixing the two is ugly and b) trying to -# make special characters such as \n go through all -# backends is a fool's errand. - -import sys, os, shutil, subprocess - -h_array = ['--fhead', - "#ifndef __PINOS_ENUM_TYPES_H__\n#define __PINOS_ENUM_TYPES_H__\n\n#include \n\nG_BEGIN_DECLS\n", - '--fprod', - "\n/* enumerations from \"@filename@\" */\n", - '--vhead', - "GType @enum_name@_get_type (void);\n#define PINOS_TYPE_@ENUMSHORT@ (@enum_name@_get_type())\n", - '--ftail', - "G_END_DECLS\n\n#endif /* __PINOS_ENUM_TYPES_H__ */"] - -c_array = [ - '--fhead', - "#include \"pinos.h\"\n#define C_ENUM(v) ((gint) v)\n#define C_FLAGS(v) ((guint) v)\n ", - '--fprod', - "\n/* enumerations from \"@filename@\" */", - '--vhead', - "GType\n@enum_name@_get_type (void)\n{\n static gsize id = 0;\n static const G@Type@Value values[] = {", - '--vprod', - " { C_@TYPE@(@VALUENAME@), \"@VALUENAME@\", \"@valuenick@\" },", - '--vtail', - " { 0, NULL, NULL }\n };\n\n if (g_once_init_enter (&id)) {\n GType tmp = g_@type@_register_static (\"@EnumName@\", values);\n g_once_init_leave (&id, tmp);\n }\n\n return (GType) id;\n}" - ] - -cmd = [] -argn = 1 -# Find the full command needed to run glib-mkenums -# On UNIX-like, this is just the full path to glib-mkenums -# On Windows, this is the full path to interpreter + full path to glib-mkenums -for arg in sys.argv[1:]: - cmd.append(arg) - argn += 1 - if arg.endswith('glib-mkenums'): - break -ofilename = sys.argv[argn] -headers = sys.argv[argn + 1:] - -if ofilename.endswith('.h'): - arg_array = h_array -else: - arg_array = c_array - -pc = subprocess.Popen(cmd + arg_array + headers, stdout=subprocess.PIPE) -(stdo, _) = pc.communicate() -if pc.returncode != 0: - sys.exit(pc.returncode) -open(ofilename, 'wb').write(stdo) diff --git a/pinos/client/context.c b/pinos/client/context.c index de2ca87b5..758cb4bec 100644 --- a/pinos/client/context.c +++ b/pinos/client/context.c @@ -170,7 +170,6 @@ core_event_update_types (void *object, for (i = 0; i < n_types; i++, first_id++) { SpaType this_id = spa_type_map_get_id (this->type.map, types[i]); - printf ("update %d %s -> %d\n", first_id, types[i], this_id); if (!pinos_map_insert_at (&this->types, first_id, SPA_UINT32_TO_PTR (this_id))) pinos_log_error ("can't add type for client"); } diff --git a/pinos/gst/gstpinossink.c b/pinos/gst/gstpinossink.c index 6810e853b..1de09fd5a 100644 --- a/pinos/gst/gstpinossink.c +++ b/pinos/gst/gstpinossink.c @@ -675,10 +675,14 @@ gst_pinos_sink_render (GstBaseSink * bsink, GstBuffer * buffer) if (buffer->pool != GST_BUFFER_POOL_CAST (pinossink->pool)) { GstBuffer *b = NULL; + GstMapInfo info = { 0, }; gst_buffer_pool_acquire_buffer (GST_BUFFER_POOL_CAST (pinossink->pool), &b, NULL); - /* FIXME, copy */ + gst_buffer_map (b, &info, GST_MAP_WRITE); + gst_buffer_extract (buffer, 0, info.data, info.size); + gst_buffer_unmap (b, &info); + gst_buffer_resize (b, 0, info.size); buffer = b; } diff --git a/pinos/modules/module-autolink.c b/pinos/modules/module-autolink.c index 017740e50..00a643f38 100644 --- a/pinos/modules/module-autolink.c +++ b/pinos/modules/module-autolink.c @@ -35,7 +35,6 @@ typedef struct { PinosListener global_added; PinosListener global_removed; - SpaList node_list; SpaList client_list; } ModuleImpl; @@ -45,6 +44,7 @@ typedef struct { SpaList link; PinosListener resource_added; PinosListener resource_removed; + SpaList node_list; } ClientInfo; typedef struct { @@ -61,11 +61,11 @@ typedef struct { } NodeInfo; static NodeInfo * -find_node_info (ModuleImpl *impl, PinosNode *node) +find_node_info (ClientInfo *cinfo, PinosNode *node) { NodeInfo *info; - spa_list_for_each (info, &impl->node_list, link) { + spa_list_for_each (info, &cinfo->node_list, link) { if (info->node == node) return info; } @@ -99,9 +99,15 @@ node_info_free (NodeInfo *info) static void client_info_free (ClientInfo *cinfo) { + NodeInfo *info, *tmp; + spa_list_remove (&cinfo->link); pinos_signal_remove (&cinfo->resource_added); pinos_signal_remove (&cinfo->resource_removed); + + spa_list_for_each_safe (info, tmp, &cinfo->node_list, link) + node_info_free (info); + free (cinfo); } @@ -285,7 +291,7 @@ on_node_added (ModuleImpl *impl, info->node = node; info->resource = resource; info->info = cinfo; - spa_list_insert (impl->node_list.prev, &info->link); + spa_list_insert (cinfo->node_list.prev, &info->link); spa_list_init (&info->port_unlinked.link); spa_list_init (&info->link_state_changed.link); @@ -325,7 +331,7 @@ on_resource_removed (PinosListener *listener, PinosClientNode *cnode = resource->object; NodeInfo *ninfo; - if ((ninfo = find_node_info (impl, cnode->node))) + if ((ninfo = find_node_info (cinfo, cnode->node))) node_info_free (ninfo); pinos_log_debug ("module %p: node %p removed", impl, cnode->node); @@ -346,6 +352,8 @@ on_global_added (PinosListener *listener, cinfo = calloc (1, sizeof (ClientInfo)); cinfo->impl = impl; cinfo->client = global->object; + spa_list_init (&cinfo->node_list); + spa_list_insert (impl->client_list.prev, &cinfo->link); pinos_signal_add (&client->resource_added, &cinfo->resource_added, on_resource_added); @@ -394,7 +402,6 @@ module_new (PinosCore *core, impl->core = core; impl->properties = properties; - spa_list_init (&impl->node_list); spa_list_init (&impl->client_list); pinos_signal_add (&core->global_added, &impl->global_added, on_global_added); diff --git a/pinos/server/core.c b/pinos/server/core.c index 3316f2381..c80fb237d 100644 --- a/pinos/server/core.c +++ b/pinos/server/core.c @@ -215,7 +215,6 @@ core_update_types (void *object, for (i = 0; i < n_types; i++, first_id++) { uint32_t this_id = spa_type_map_get_id (this->type.map, types[i]); - printf ("update %d %s -> %d\n", first_id, types[i], this_id); if (!pinos_map_insert_at (&client->types, first_id, SPA_UINT32_TO_PTR (this_id))) pinos_log_error ("can't add type for client"); } diff --git a/pinos/server/memfd-wrappers.h b/pinos/server/memfd-wrappers.h deleted file mode 100644 index 8d6ac8b2a..000000000 --- a/pinos/server/memfd-wrappers.h +++ /dev/null @@ -1,59 +0,0 @@ -/* Simple Plugin API - * Copyright (C) 2016 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 - -/* - * No glibc wrappers exist for memfd_create(2), so provide our own. - * - * Also define memfd fcntl sealing macros. While they are already - * defined in the kernel header file , that file as - * a whole conflicts with the original glibc header . - */ - -static inline int memfd_create(const char *name, unsigned int flags) { - return syscall(SYS_memfd_create, name, flags); -} - -/* memfd_create(2) flags */ - -#ifndef MFD_CLOEXEC -#define MFD_CLOEXEC 0x0001U -#endif - -#ifndef MFD_ALLOW_SEALING -#define MFD_ALLOW_SEALING 0x0002U -#endif - -/* fcntl() seals-related flags */ - -#ifndef F_LINUX_SPECIFIC_BASE -#define F_LINUX_SPECIFIC_BASE 1024 -#endif - -#ifndef F_ADD_SEALS -#define F_ADD_SEALS (F_LINUX_SPECIFIC_BASE + 9) -#define F_GET_SEALS (F_LINUX_SPECIFIC_BASE + 10) - -#define F_SEAL_SEAL 0x0001 /* prevent further seals from being set */ -#define F_SEAL_SHRINK 0x0002 /* prevent file from shrinking */ -#define F_SEAL_GROW 0x0004 /* prevent file from growing */ -#define F_SEAL_WRITE 0x0008 /* prevent writes */ -#endif diff --git a/spa/plugins/alsa/alsa-utils.c b/spa/plugins/alsa/alsa-utils.c index 39361981e..90cc28de5 100644 --- a/spa/plugins/alsa/alsa-utils.c +++ b/spa/plugins/alsa/alsa-utils.c @@ -6,6 +6,8 @@ #include #include #include +#include +#include #include #include "alsa-utils.h" @@ -35,6 +37,7 @@ spa_alsa_open (SpaALSAState *state) SND_PCM_NO_AUTO_FORMAT), "open failed"); + state->timerfd = timerfd_create (CLOCK_MONOTONIC, TFD_CLOEXEC | TFD_NONBLOCK); state->opened = true; return 0; @@ -51,6 +54,7 @@ spa_alsa_close (SpaALSAState *state) spa_log_info (state->log, "Device closing"); CHECK (snd_pcm_close (state->hndl), "close failed"); + close (state->timerfd); state->opened = false; return err; @@ -131,6 +135,10 @@ spa_alsa_set_format (SpaALSAState *state, SpaAudioInfo *fmt, SpaPortFormatFlags /* set the interleaved read/write format */ CHECK (snd_pcm_hw_params_set_access(hndl, params, SND_PCM_ACCESS_MMAP_INTERLEAVED), "set_access"); + /* disable ALSA wakeups, we use a timer */ + if (snd_pcm_hw_params_can_disable_period_wakeup (params)) + CHECK (snd_pcm_hw_params_set_period_wakeup (hndl, params, 0), "set_period_wakeup"); + /* set the sample format */ format = spa_alsa_format_to_alsa (&state->type, info->format); spa_log_info (state->log, "Stream parameters are %iHz, %s, %i channels", info->rate, snd_pcm_format_name(format), info->channels); @@ -163,6 +171,7 @@ spa_alsa_set_format (SpaALSAState *state, SpaAudioInfo *fmt, SpaPortFormatFlags state->rate = info->rate; state->frame_size = info->channels * 2; +#if 0 period_size = props->period_size; periods = props->periods; @@ -177,6 +186,17 @@ spa_alsa_set_format (SpaALSAState *state, SpaAudioInfo *fmt, SpaPortFormatFlags state->buffer_frames = periods * state->period_frames; CHECK (snd_pcm_hw_params_set_buffer_size (hndl, params, state->buffer_frames), "set_buffer_size"); +#else + CHECK (snd_pcm_hw_params_get_buffer_size_max (params, &state->buffer_frames), "get_buffer_size_max"); + + CHECK (snd_pcm_hw_params_set_buffer_size_near (hndl, params, &state->buffer_frames), "set_buffer_size_near"); + + dir = 0; + period_size = state->buffer_frames; + CHECK (snd_pcm_hw_params_set_period_size_near (hndl, params, &period_size, &dir), "set_period_size_near"); + state->period_frames = period_size; + periods = state->buffer_frames / state->period_frames; +#endif spa_log_info (state->log, "buffer frames %zd, period frames %zd, periods %u", state->buffer_frames, state->period_frames, periods); @@ -193,6 +213,8 @@ set_swparams (SpaALSAState *state) snd_pcm_t *hndl = state->hndl; int err = 0; snd_pcm_sw_params_t *params; + snd_pcm_uframes_t boundary; + SpaALSAProps *props = &state->props; snd_pcm_sw_params_alloca (¶ms); @@ -203,23 +225,25 @@ set_swparams (SpaALSAState *state) CHECK (snd_pcm_sw_params_set_tstamp_mode (hndl, params, SND_PCM_TSTAMP_ENABLE), "sw_params_set_tstamp_mode"); /* start the transfer */ - CHECK (snd_pcm_sw_params_set_start_threshold (hndl, params, 0U), "set_start_threshold"); + CHECK (snd_pcm_sw_params_set_start_threshold (hndl, params, LONG_MAX), "set_start_threshold"); #if 1 - CHECK (snd_pcm_sw_params_set_stop_threshold (hndl, params, - (state->buffer_frames / state->period_frames) * state->period_frames), "set_stop_threshold"); + CHECK (snd_pcm_sw_params_get_boundary (params, &boundary), "get_boundary"); + + CHECK (snd_pcm_sw_params_set_stop_threshold (hndl, params, boundary), "set_stop_threshold"); #else CHECK (snd_pcm_sw_params_set_stop_threshold (hndl, params, -1), "set_stop_threshold"); #endif - CHECK (snd_pcm_sw_params_set_silence_threshold (hndl, params, 0U), "set_silence_threshold"); +// CHECK (snd_pcm_sw_params_set_silence_threshold (hndl, params, 0U), "set_silence_threshold"); /* enable period events when requested */ - CHECK (snd_pcm_sw_params_set_period_event (hndl, params, props->period_event ? 1 : 0), "set_period_event"); +// CHECK (snd_pcm_sw_params_set_period_event (hndl, params, props->period_event ? 1 : 0), "set_period_event"); + CHECK (snd_pcm_sw_params_set_period_event (hndl, params, 0), "set_period_event"); #if 1 /* allow the transfer when at least period_size samples can be processed */ /* or disable this mechanism when period event is enabled (aka interrupt like style processing) */ - CHECK (snd_pcm_sw_params_set_avail_min (hndl, params, state->period_frames), "set_avail_min"); +// CHECK (snd_pcm_sw_params_set_avail_min (hndl, params, state->period_frames), "set_avail_min"); #else CHECK (snd_pcm_sw_params_set_avail_min (hndl, params, 0), "set_avail_min"); #endif @@ -269,13 +293,15 @@ pull_frames_queue (SpaALSAState *state, snd_pcm_uframes_t offset, snd_pcm_uframes_t frames) { + snd_pcm_uframes_t total_frames = 0, to_write = frames; + if (spa_list_is_empty (&state->ready)) { SpaEvent event = SPA_EVENT_INIT (state->type.event_node.NeedInput); state->event_cb (&state->node, &event, state->user_data); } - if (!spa_list_is_empty (&state->ready)) { + while (!spa_list_is_empty (&state->ready) && to_write > 0) { uint8_t *src, *dst; - size_t n_bytes, size; + size_t n_bytes, n_frames, size; off_t offs; SpaALSABuffer *b; @@ -287,8 +313,8 @@ pull_frames_queue (SpaALSAState *state, 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, frames * state->frame_size); - frames = SPA_MIN (frames, n_bytes / state->frame_size); + 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); @@ -304,11 +330,16 @@ pull_frames_queue (SpaALSAState *state, state->ready_offset = 0; } - } else { - spa_log_warn (state->log, "underrun, want %zd frames", frames); - snd_pcm_areas_silence (my_areas, offset, state->channels, frames, state->format); + total_frames += n_frames; + to_write -= n_frames; } - return frames; + if (total_frames == 0) { + total_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); + } + //spa_log_warn (state->log, "written %zd frames", total_frames); + return total_frames; } static snd_pcm_uframes_t @@ -575,6 +606,119 @@ alsa_on_fd_events (SpaSource *source) } } +static int +alsa_try_resume (SpaALSAState *state) +{ + int res; + + while ((res = snd_pcm_resume (state->hndl)) == -EAGAIN) + usleep (250000); + if (res < 0) { + spa_log_error (state->log, "suspended, failed to resume %s", snd_strerror(res)); + res = snd_pcm_prepare (state->hndl); + if (res < 0) + spa_log_error (state->log, "suspended, failed to prepare %s", snd_strerror(res)); + } + return res; +} + +static inline void +calc_timeout (size_t frames, + size_t cb_threshold, + size_t rate, + struct timespec *ts) +{ + size_t to_play_usec; + + ts->tv_sec = 0; + /* adjust sleep time to target our callback threshold */ + if (frames > cb_threshold) + to_play_usec = (frames - cb_threshold) * 1000000 / rate; + else + to_play_usec = 0; + ts->tv_nsec = to_play_usec * 1000 + 1; + + while (ts->tv_nsec > 1000000000L) { + ts->tv_sec++; + ts->tv_nsec -= 1000000000L; + } +} + +static void +alsa_on_timeout_event (SpaSource *source) +{ + uint64_t exp; + int res; + SpaALSAState *state = source->data; + snd_pcm_t *hndl = state->hndl; + snd_pcm_sframes_t avail; + struct itimerspec ts; + snd_pcm_uframes_t total_written = 0, filled; + const snd_pcm_channel_area_t *my_areas; + + read (state->timerfd, &exp, sizeof (uint64_t)); + + if ((avail = snd_pcm_avail (hndl)) < 0) { + spa_log_error (state->log, "snd_pcm_avail_update error: %s", snd_strerror (avail)); + return; + } + if (avail > state->buffer_frames) + avail = state->buffer_frames; + + filled = state->buffer_frames - avail; + if (filled > state->threshold) { + if (snd_pcm_state (hndl) == SND_PCM_STATE_SUSPENDED) { + spa_log_error (state->log, "suspended: try resume"); + if ((res = alsa_try_resume (state)) < 0) + return; + } + } + else { + snd_pcm_uframes_t to_write = state->buffer_frames - filled; + + while (total_written < to_write) { + snd_pcm_uframes_t written, frames, offset; + + frames = to_write - total_written; + if ((res = snd_pcm_mmap_begin (hndl, &my_areas, &offset, &frames)) < 0) { + spa_log_error (state->log, "snd_pcm_mmap_begin error: %s", snd_strerror(res)); + return; + } + + if (state->ringbuffer) + written = pull_frames_ringbuffer (state, my_areas, offset, frames); + else + written = pull_frames_queue (state, my_areas, offset, frames); + + if (written < frames) + to_write = 0; + + if ((res = snd_pcm_mmap_commit (hndl, offset, written)) < 0) { + spa_log_error (state->log, "snd_pcm_mmap_commit error: %s", snd_strerror(res)); + if (res != -EPIPE && res != -ESTRPIPE) + return; + } + total_written += written; + } + } + if (!state->alsa_started && total_written > 0) { + spa_log_trace (state->log, "snd_pcm_start"); + if ((res = snd_pcm_start (state->hndl)) < 0) { + spa_log_error (state->log, "snd_pcm_start: %s", snd_strerror (res)); + return; + } + state->alsa_started = true; + } + + calc_timeout (total_written + filled, state->threshold, state->rate, &ts.it_value); + +// printf ("timeout %ld %ld %ld %ld\n", total_written, filled, ts.it_value.tv_sec, ts.it_value.tv_nsec); + + ts.it_interval.tv_sec = 0; + ts.it_interval.tv_nsec = 0; + timerfd_settime (state->timerfd, 0, &ts, NULL); +} + SpaResult spa_alsa_start (SpaALSAState *state, bool xrun_recover) { @@ -595,6 +739,7 @@ spa_alsa_start (SpaALSAState *state, bool xrun_recover) for (i = 0; i < state->n_fds; i++) spa_loop_remove_source (state->data_loop, &state->sources[i]); +#if 0 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; @@ -614,15 +759,29 @@ spa_alsa_start (SpaALSAState *state, bool xrun_recover) state->fds[i].revents = 0; spa_loop_add_source (state->data_loop, &state->sources[i]); } +#else + state->n_fds = 1; + state->sources[0].func = alsa_on_timeout_event; + state->sources[0].data = state; + state->sources[0].fd = state->timerfd; + state->sources[0].mask = SPA_IO_IN; + state->sources[0].rmask = 0; + spa_loop_add_source (state->data_loop, &state->sources[0]); +#endif + + state->threshold = 40; + state->alsa_started = false; if (state->stream == SND_PCM_STREAM_PLAYBACK) { - mmap_write (state); + alsa_on_timeout_event (&state->sources[0]); } +#if 0 if ((err = snd_pcm_start (state->hndl)) < 0) { spa_log_error (state->log, "snd_pcm_start: %s", snd_strerror (err)); return SPA_RESULT_ERROR; } +#endif state->started = true; diff --git a/spa/plugins/alsa/alsa-utils.h b/spa/plugins/alsa/alsa-utils.h index 9e2d0863b..bcc1c760f 100644 --- a/spa/plugins/alsa/alsa-utils.h +++ b/spa/plugins/alsa/alsa-utils.h @@ -136,8 +136,8 @@ struct _SpaALSAState { SpaAudioInfo current_format; uint8_t format_buffer[1024]; - snd_pcm_sframes_t buffer_frames; - snd_pcm_sframes_t period_frames; + snd_pcm_uframes_t buffer_frames; + snd_pcm_uframes_t period_frames; snd_pcm_format_t format; int rate; int channels; @@ -161,6 +161,9 @@ struct _SpaALSAState { int n_fds; struct pollfd fds[16]; SpaSource sources[16]; + int timerfd; + bool alsa_started; + int threshold; int64_t sample_count; int64_t last_ticks;