Files
clapper/src/lib/shared/clapper-shared-utils.c
Rafał Dzięgiel d7f069d6c3 lib: Introduce Clapper playback library
An easy to use media playback library (libclapper) as a GstPlayer replacement.

Previously we tried to use upstream `gstplayer` library to control playback and
pass all events from multiple threads GStreamer uses into an app main thread.
Since this caused some thread racy problems and we needed additional ABI breaking
changes to better suit our needs, we ended up with a modified fork of said library
renamed to `gstclapper` as a temporary solution.

This new library simply named `clapper` replaces our previous `gstclapper` solution
and is written completely from scratch by myself. The aim here is to have an easy to
use playback library better suited to work with (but not limited to) GTK and GObject
properties bindings by relying on "notify" signals.

Major differences include:
* Operates on a playback queue (inherits `GListModel` interface) instead of a single URI
* Uses "notify" signals for property changes always dispatched to app thread
* Time is passed/read as decimal number in seconds instead of int64 in nanoseconds
* Integrates `GstDiscoverer` to figure out media info (such as title) before playback
* Easy to use MPRIS support as part of library
* Optional playback remote controls with WebSocket messages

The new library will be distributed with Clapper player. This includes public headers
and GObject Introspection support.

Licensed under LGPL-2.1-or-later.

Enjoy
2024-04-05 21:18:08 +02:00

93 lines
2.5 KiB
C

/*
* Copyright (C) 2024 Rafał Dzięgiel <rafostar.github@gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser 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 "clapper-shared-utils-private.h"
struct ClapperSharedUtilsInvoke
{
GMutex lock;
GCond cond;
gboolean fired;
GThreadFunc func;
gpointer data;
gpointer res;
};
static gboolean
_invoke_func (struct ClapperSharedUtilsInvoke *invoke)
{
g_mutex_lock (&invoke->lock);
invoke->res = invoke->func (invoke->data);
invoke->fired = TRUE;
g_cond_signal (&invoke->cond);
g_mutex_unlock (&invoke->lock);
return G_SOURCE_REMOVE;
}
inline gpointer
clapper_shared_utils_context_invoke_sync (GMainContext *context, GThreadFunc func, gpointer data)
{
struct ClapperSharedUtilsInvoke invoke;
g_mutex_init (&invoke.lock);
g_cond_init (&invoke.cond);
invoke.fired = FALSE;
invoke.func = func;
invoke.data = data;
g_main_context_invoke (context,
(GSourceFunc) _invoke_func, &invoke);
g_mutex_lock (&invoke.lock);
while (!invoke.fired)
g_cond_wait (&invoke.cond, &invoke.lock);
g_mutex_unlock (&invoke.lock);
g_mutex_clear (&invoke.lock);
g_cond_clear (&invoke.cond);
return invoke.res;
}
inline gpointer
clapper_shared_utils_context_invoke_sync_full (GMainContext *context, GThreadFunc func, gpointer data, GDestroyNotify destroy_func)
{
gpointer res = clapper_shared_utils_context_invoke_sync (context, func, data);
if (destroy_func)
destroy_func (data);
return res;
}
inline GSource *
clapper_shared_utils_context_timeout_add_full (GMainContext *context, gint priority, guint interval,
GSourceFunc func, gpointer data, GDestroyNotify destroy_func)
{
GSource *source = g_timeout_source_new (interval);
g_source_set_priority (source, priority);
g_source_set_callback (source, func, data, destroy_func);
g_source_attach (source, context);
return source;
}