From 046c2b1caed3060df9263d874ddeff42ee6f60e4 Mon Sep 17 00:00:00 2001 From: Wim Taymans Date: Sat, 26 Nov 2016 19:18:51 +0100 Subject: [PATCH] remove last glib bits Improve pinossink --- pinos/client/meson.build | 4 +- pinos/client/properties.c | 139 ++++++++++++++++++++++++-------------- pinos/gst/gstpinossink.c | 39 +++++++---- pinos/server/link.c | 25 ++++--- pinos/server/main-loop.c | 8 +-- pinos/server/module.c | 48 +++++++------ pinos/server/node.c | 7 +- pinos/server/port.c | 1 - 8 files changed, 162 insertions(+), 109 deletions(-) diff --git a/pinos/client/meson.build b/pinos/client/meson.build index f19d07463..f6333f762 100644 --- a/pinos/client/meson.build +++ b/pinos/client/meson.build @@ -52,10 +52,10 @@ libpinos = shared_library('pinos', pinos_sources, include_directories : [configinc, spa_inc], link_with : spalib, install : true, - dependencies : [glib_dep, dbus_dep, mathlib, pthread_lib], + dependencies : [dbus_dep, mathlib, pthread_lib], ) pinos_dep = declare_dependency(link_with : libpinos, include_directories : [configinc, spa_inc], - dependencies : [glib_dep, pthread_lib], + dependencies : [pthread_lib], ) diff --git a/pinos/client/properties.c b/pinos/client/properties.c index 0b7cd246f..09e3aa759 100644 --- a/pinos/client/properties.c +++ b/pinos/client/properties.c @@ -17,19 +17,45 @@ * Boston, MA 02110-1301, USA. */ -#include - #include "pinos/client/pinos.h" #include "pinos/client/properties.h" +typedef struct { + char *key; + char *value; +} PropItem; + struct _PinosProperties { - GHashTable *hashtable; + PinosArray items; }; static void -copy_func (const char *key, const char *value, GHashTable *copy) +add_func (PinosProperties *props, char *key, char *value) { - g_hash_table_insert (copy, g_strdup (key), g_strdup (value)); + PropItem *item; + item = pinos_array_add (&props->items, sizeof (PropItem)); + item->key = key; + item->value = value; +} + +static void +clear_item (PropItem *item) +{ + free (item->key); + free (item->value); +} + +static int +find_index (PinosProperties *props, const char *key) +{ + int i, len = pinos_array_get_len (&props->items, PropItem); + + for (i = 0; i < len; i++) { + PropItem *item = pinos_array_get_unchecked (&props->items, i, PropItem); + if (strcmp (item->key, key) == 0) + return i; + } + return -1; } /** @@ -48,13 +74,13 @@ pinos_properties_new (const char *key, ...) va_list varargs; const char *value; - props = g_new (PinosProperties, 1); - props->hashtable = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free); + props = calloc (1, sizeof (PinosProperties)); + pinos_array_init (&props->items); va_start (varargs, key); while (key != NULL) { value = va_arg (varargs, char *); - copy_func (key, value, props->hashtable); + add_func (props, strdup (key), strdup (value)); key = va_arg (varargs, char *); } va_end (varargs); @@ -74,11 +100,11 @@ PinosProperties * pinos_properties_copy (PinosProperties *properties) { PinosProperties *copy; - - g_return_val_if_fail (properties != NULL, NULL); + PropItem *item; copy = pinos_properties_new (NULL, NULL); - g_hash_table_foreach (properties->hashtable, (GHFunc) copy_func, copy->hashtable); + pinos_array_for_each (item, &properties->items) + add_func (copy, strdup (item->key), strdup (item->value)); return copy; } @@ -119,10 +145,40 @@ pinos_properties_merge (PinosProperties *oldprops, void pinos_properties_free (PinosProperties *properties) { - g_return_if_fail (properties != NULL); + PropItem *item; - g_hash_table_unref (properties->hashtable); - g_free (properties); + pinos_array_for_each (item, &properties->items) + clear_item (item); + + pinos_array_clear (&properties->items); + free (properties); +} + +static void +do_replace (PinosProperties *properties, + char *key, + char *value) +{ + int index = find_index (properties, key); + + if (index == -1) { + add_func (properties, key, value); + } else { + PropItem *item = pinos_array_get_unchecked (&properties->items, index, PropItem); + + clear_item (item); + if (value == NULL) { + PropItem *other = pinos_array_get_unchecked (&properties->items, + pinos_array_get_len (&properties->items, PropItem) - 1, + PropItem); + item->key = other->key; + item->value = other->value; + properties->items.size -= sizeof (PropItem); + } else { + item->key = key; + item->value = value; + } + } } /** @@ -140,13 +196,7 @@ pinos_properties_set (PinosProperties *properties, const char *key, const char *value) { - g_return_if_fail (properties != NULL); - g_return_if_fail (key != NULL); - - if (value == NULL) - g_hash_table_remove (properties->hashtable, key); - else - g_hash_table_replace (properties->hashtable, g_strdup (key), g_strdup (value)); + do_replace (properties, strdup (key), value ? strdup (value) : NULL); } /** @@ -166,16 +216,13 @@ pinos_properties_setf (PinosProperties *properties, ...) { va_list varargs; - - g_return_if_fail (properties != NULL); - g_return_if_fail (key != NULL); - g_return_if_fail (format != NULL); + char *value; va_start (varargs, format); - g_hash_table_replace (properties->hashtable, - g_strdup (key), - g_strdup_vprintf (format, varargs)); + vasprintf (&value, format, varargs); va_end (varargs); + + do_replace (properties, strdup (key), value); } /** @@ -191,10 +238,12 @@ const char * pinos_properties_get (PinosProperties *properties, const char *key) { - g_return_val_if_fail (properties != NULL, NULL); - g_return_val_if_fail (key != NULL, NULL); + int index = find_index (properties, key); - return g_hash_table_lookup (properties->hashtable, key); + if (index == -1) + return NULL; + + return pinos_array_get_unchecked (&properties->items, index, PropItem)->value; } /** @@ -215,29 +264,17 @@ const char * pinos_properties_iterate (PinosProperties *properties, void **state) { - static void * dummy = GINT_TO_POINTER (1); - const char *res = NULL; - GList *items; + unsigned int index; - g_return_val_if_fail (properties != NULL, NULL); - g_return_val_if_fail (state != NULL, NULL); + if (*state == NULL) + index = 0; + else + index = SPA_PTR_TO_INT (*state); - if (*state == dummy) + if (!pinos_array_check_index (&properties->items, index, PropItem)) return NULL; - if (*state == NULL) { - items = g_hash_table_get_keys (properties->hashtable); - } else { - items = *state; - } + *state = SPA_INT_TO_PTR (index + 1); - if (items) { - res = items->data; - items = g_list_delete_link (items, items); - if (items == NULL) - items = dummy; - } - *state = items; - - return res; + return pinos_array_get_unchecked (&properties->items, index, PropItem)->key; } diff --git a/pinos/gst/gstpinossink.c b/pinos/gst/gstpinossink.c index 4071334c9..5233a97b8 100644 --- a/pinos/gst/gstpinossink.c +++ b/pinos/gst/gstpinossink.c @@ -118,13 +118,20 @@ gst_pinos_sink_finalize (GObject * object) { GstPinosSink *pinossink = GST_PINOS_SINK (object); + g_object_unref (pinossink->pool); + + pinos_thread_main_loop_destroy (pinossink->main_loop); + pinossink->main_loop = NULL; + + pinos_loop_destroy (pinossink->loop); + pinossink->loop = NULL; + if (pinossink->properties) gst_structure_free (pinossink->properties); g_object_unref (pinossink->allocator); - g_object_unref (pinossink->pool); - g_hash_table_unref (pinossink->buf_ids); g_free (pinossink->path); g_free (pinossink->client_name); + g_hash_table_unref (pinossink->buf_ids); G_OBJECT_CLASS (parent_class)->finalize (object); } @@ -222,6 +229,10 @@ gst_pinos_sink_init (GstPinosSink * sink) sink->buf_ids = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, (GDestroyNotify) gst_buffer_unref); + + sink->loop = pinos_loop_new (); + sink->main_loop = pinos_thread_main_loop_new (sink->loop, "pinos-sink-loop"); + GST_DEBUG ("loop %p %p", sink->loop, sink->main_loop); } static GstCaps * @@ -710,7 +721,8 @@ gst_pinos_sink_stop (GstBaseSink * basesink) if (pinossink->stream) { pinos_stream_stop (pinossink->stream); pinos_stream_disconnect (pinossink->stream); - g_clear_object (&pinossink->stream); + pinos_stream_destroy (pinossink->stream); + pinossink->stream = NULL; pinossink->pool->stream = NULL; } pinos_thread_main_loop_unlock (pinossink->main_loop); @@ -746,10 +758,6 @@ on_ctx_state_changed (PinosListener *listener, static gboolean gst_pinos_sink_open (GstPinosSink * pinossink) { - pinossink->loop = pinos_loop_new (); - GST_DEBUG ("loop %p", pinossink->loop); - - pinossink->main_loop = pinos_thread_main_loop_new (pinossink->loop, "pinos-sink-loop"); if (pinos_thread_main_loop_start (pinossink->main_loop) != SPA_RESULT_OK) goto mainloop_error; @@ -758,7 +766,7 @@ gst_pinos_sink_open (GstPinosSink * pinossink) pinos_signal_add (&pinossink->ctx->state_changed, &pinossink->ctx_state_changed, on_ctx_state_changed); - pinos_context_connect(pinossink->ctx); + pinos_context_connect (pinossink->ctx); while (TRUE) { PinosContextState state = pinossink->ctx->state; @@ -814,11 +822,16 @@ gst_pinos_sink_close (GstPinosSink * pinossink) pinos_thread_main_loop_unlock (pinossink->main_loop); pinos_thread_main_loop_stop (pinossink->main_loop); - pinos_thread_main_loop_destroy (pinossink->main_loop); - pinos_stream_destroy (pinossink->stream); - pinos_context_destroy (pinossink->ctx); - pinos_loop_destroy (pinossink->loop); - pinossink->loop = NULL; + + if (pinossink->stream) { + pinos_stream_destroy (pinossink->stream); + pinossink->stream = NULL; + } + + if (pinossink->ctx) { + pinos_context_destroy (pinossink->ctx); + pinossink->ctx = NULL; + } return TRUE; } diff --git a/pinos/server/link.c b/pinos/server/link.c index f1d66b12f..9e923a32f 100644 --- a/pinos/server/link.c +++ b/pinos/server/link.c @@ -23,7 +23,6 @@ #include #include "pinos/client/pinos.h" -#include "pinos/client/enumtypes.h" #include "pinos/server/link.h" @@ -315,7 +314,7 @@ do_allocation (PinosLink *this, SpaNodeState in_state, SpaNodeState out_state) in_flags = SPA_PORT_INFO_FLAG_CAN_USE_BUFFERS; impl->n_buffers = this->output->n_buffers; impl->buffers = this->output->buffers; - impl->allocated = FALSE; + impl->allocated = false; pinos_log_debug ("reusing %d output buffers %p", impl->n_buffers, impl->buffers); } else { unsigned int i, j; @@ -409,7 +408,7 @@ do_allocation (PinosLink *this, SpaNodeState in_state, SpaNodeState out_state) } } pinos_log_debug ("allocated %d buffers %p %zd", impl->n_buffers, impl->buffers, minsize); - impl->allocated = TRUE; + impl->allocated = true; } if (out_flags & SPA_PORT_INFO_FLAG_CAN_ALLOC_BUFFERS) { @@ -424,9 +423,9 @@ do_allocation (PinosLink *this, SpaNodeState in_state, SpaNodeState out_state) pinos_main_loop_defer (this->core->main_loop, this->output->node, res, NULL, NULL); this->output->buffers = impl->buffers; this->output->n_buffers = impl->n_buffers; - this->output->allocated = TRUE; + this->output->allocated = true; this->output->buffer_mem = impl->buffer_mem; - impl->allocated = FALSE; + impl->allocated = false; pinos_log_debug ("allocated %d buffers %p from output port", impl->n_buffers, impl->buffers); } else if (in_flags & SPA_PORT_INFO_FLAG_CAN_ALLOC_BUFFERS) { if ((res = spa_node_port_alloc_buffers (this->input->node->node, @@ -440,9 +439,9 @@ do_allocation (PinosLink *this, SpaNodeState in_state, SpaNodeState out_state) pinos_main_loop_defer (this->core->main_loop, this->input->node, res, NULL, NULL); this->input->buffers = impl->buffers; this->input->n_buffers = impl->n_buffers; - this->input->allocated = TRUE; + this->input->allocated = true; this->input->buffer_mem = impl->buffer_mem; - impl->allocated = FALSE; + impl->allocated = false; pinos_log_debug ("allocated %d buffers %p from input port", impl->n_buffers, impl->buffers); } } @@ -460,7 +459,7 @@ do_allocation (PinosLink *this, SpaNodeState in_state, SpaNodeState out_state) pinos_main_loop_defer (this->core->main_loop, this->input->node, res, NULL, NULL); this->input->buffers = impl->buffers; this->input->n_buffers = impl->n_buffers; - this->input->allocated = FALSE; + this->input->allocated = false; } else if (out_flags & SPA_PORT_INFO_FLAG_CAN_USE_BUFFERS) { pinos_log_debug ("using %d buffers %p on output port", impl->n_buffers, impl->buffers); @@ -475,7 +474,7 @@ do_allocation (PinosLink *this, SpaNodeState in_state, SpaNodeState out_state) pinos_main_loop_defer (this->core->main_loop, this->output->node, res, NULL, NULL); this->output->buffers = impl->buffers; this->output->n_buffers = impl->n_buffers; - this->output->allocated = FALSE; + this->output->allocated = false; } else { asprintf (&error, "no common buffer alloc found"); goto error; @@ -487,10 +486,10 @@ error: { this->output->buffers = NULL; this->output->n_buffers = 0; - this->output->allocated = FALSE; + this->output->allocated = false; this->input->buffers = NULL; this->input->n_buffers = 0; - this->input->allocated = FALSE; + this->input->allocated = false; pinos_link_report_error (this, error); return res; } @@ -520,7 +519,7 @@ do_start (PinosLink *this, SpaNodeState in_state, SpaNodeState out_state) static SpaResult check_states (PinosLink *this, - gpointer user_data, + void *user_data, SpaResult res) { SpaNodeState in_state, out_state; @@ -586,7 +585,7 @@ on_output_async_complete_notify (PinosListener *listener, } static void -on_port_unlinked (PinosPort *port, PinosLink *this, SpaResult res, gulong id) +on_port_unlinked (PinosPort *port, PinosLink *this, SpaResult res, uint32_t id) { pinos_signal_emit (&this->core->port_unlinked, this, port); diff --git a/pinos/server/main-loop.c b/pinos/server/main-loop.c index 636470918..402fb8d67 100644 --- a/pinos/server/main-loop.c +++ b/pinos/server/main-loop.c @@ -22,8 +22,6 @@ #include #include -#include - #include "spa/include/spa/list.h" #include "spa/include/spa/ringbuffer.h" #include "pinos/client/log.h" @@ -113,11 +111,11 @@ main_loop_defer (PinosMainLoop *loop, pinos_log_debug ("main-loop %p: wait sync object %p", loop, obj); item->seq = SPA_ID_INVALID; item->res = res; - have_work = TRUE; + have_work = true; } else { item->seq = SPA_ID_INVALID; item->res = res; - have_work = TRUE; + have_work = true; pinos_log_debug ("main-loop %p: defer object %p", loop, obj); } spa_list_insert (impl->work_list.prev, &item->link); @@ -164,7 +162,7 @@ main_loop_defer_complete (PinosMainLoop *loop, pinos_log_debug ("main-loop %p: found defered %d for object %p", loop, seq, obj); item->seq = SPA_ID_INVALID; item->res = res; - have_work = TRUE; + have_work = true; } } if (!have_work) { diff --git a/pinos/server/module.c b/pinos/server/module.c index b253ed4b8..484394223 100644 --- a/pinos/server/module.c +++ b/pinos/server/module.c @@ -23,7 +23,10 @@ #endif #include -#include +#include +#include +#include + #include "pinos/client/pinos.h" #include "pinos/client/utils.h" @@ -42,42 +45,47 @@ static char * find_module (const char * path, const char *name) { char *filename; - GDir *dir; - const gchar *entry; - GError *err = NULL; + struct dirent *entry; + struct stat s; + DIR *dir; - filename = g_strconcat (path, G_DIR_SEPARATOR_S, name, ".", G_MODULE_SUFFIX, NULL); + asprintf (&filename, "%s/%s.so", path, name); - if (g_file_test (filename, G_FILE_TEST_IS_REGULAR)) { + if (stat (filename, &s) == 0 && S_ISREG (s.st_mode)) { /* found a regular file with name */ return filename; } - g_clear_pointer (&filename, g_free); + free (filename); + filename = NULL; /* now recurse down in subdirectories and look for it there */ - dir = g_dir_open (path, 0, &err); + dir = opendir (path); if (dir == NULL) { - pinos_log_warn ("could not open %s: %s", path, err->message); - g_error_free (err); + pinos_log_warn ("could not open %s: %s", path, strerror (errno)); return NULL; } - while ((entry = g_dir_read_name (dir))) { - gchar *newpath; + while ((entry = readdir (dir))) { + char *newpath; - newpath = g_build_filename (path, entry, NULL); - if (g_file_test (newpath, G_FILE_TEST_IS_DIR)) { + if (strcmp (entry->d_name, ".") == 0 || + strcmp (entry->d_name, "..") == 0) + continue; + + asprintf (&newpath, "%s/%s", path, entry->d_name); + + if (stat (newpath, &s) == 0 && S_ISDIR (s.st_mode)) { filename = find_module (newpath, name); } - g_free (newpath); + free (newpath); if (filename != NULL) break; } - g_dir_close (dir); + closedir (dir); return filename; } @@ -95,8 +103,8 @@ find_module (const char * path, const char *name) */ PinosModule * pinos_module_load (PinosCore *core, - const gchar *name, - const gchar *args, + const char *name, + const char *args, char **err) { PinosModule *this; @@ -113,7 +121,7 @@ pinos_module_load (PinosCore *core, pinos_log_debug ("PINOS_MODULE_DIR set to: %s", module_dir); - l = pinos_split_strv (module_dir, G_SEARCHPATH_SEPARATOR_S, 0, &n_paths); + l = pinos_split_strv (module_dir, "/", 0, &n_paths); for (i = 0; l[i] != NULL; i++) { filename = find_module (l[i], name); if (filename != NULL) @@ -147,7 +155,7 @@ pinos_module_load (PinosCore *core, this->name = strdup (name); this->core = core; - if (!init_func (this, (gchar *) args)) + if (!init_func (this, (char *) args)) goto init_failed; pinos_log_debug ("loaded module: %s", this->name); diff --git a/pinos/server/node.c b/pinos/server/node.c index 965398304..2701f15d3 100644 --- a/pinos/server/node.c +++ b/pinos/server/node.c @@ -22,7 +22,6 @@ #include #include "pinos/client/pinos.h" -#include "pinos/client/enumtypes.h" #include "pinos/server/node.h" #include "pinos/server/data-loop.h" @@ -639,10 +638,10 @@ pinos_node_get_free_port (PinosNode *node, static void on_state_complete (PinosNode *node, - gpointer data, + void *data, SpaResult res) { - PinosNodeState state = GPOINTER_TO_INT (data); + PinosNodeState state = SPA_PTR_TO_INT (data); char *error = NULL; pinos_log_debug ("node %p: state complete %d", node, res); @@ -702,7 +701,7 @@ pinos_node_set_state (PinosNode *node, node, res, (PinosDeferFunc) on_state_complete, - GINT_TO_POINTER (state)); + SPA_INT_TO_PTR (state)); return res; } diff --git a/pinos/server/port.c b/pinos/server/port.c index 6a25e0d07..fe3ec16c1 100644 --- a/pinos/server/port.c +++ b/pinos/server/port.c @@ -22,7 +22,6 @@ #include #include "pinos/client/pinos.h" -#include "pinos/client/enumtypes.h" #include "pinos/server/port.h"