diff options
author | charles kerr <charlesk@canonical.com> | 2016-01-01 13:53:48 -0600 |
---|---|---|
committer | Mike Gabriel <mike.gabriel@das-netzwerkteam.de> | 2021-08-28 10:17:14 +0200 |
commit | 2b939453598cc1fd8f1c40e5d6cbfd63cb852fb4 (patch) | |
tree | 79862b45afb8da2faf9f2a5d949976dcf10e34b7 /src | |
parent | 4fc7a4e4d0346e1aad32057536301620e138bf30 (diff) | |
download | ayatana-indicator-power-2b939453598cc1fd8f1c40e5d6cbfd63cb852fb4.tar.gz ayatana-indicator-power-2b939453598cc1fd8f1c40e5d6cbfd63cb852fb4.tar.bz2 ayatana-indicator-power-2b939453598cc1fd8f1c40e5d6cbfd63cb852fb4.zip |
use gstreamer to play the sound
Diffstat (limited to 'src')
-rw-r--r-- | src/notifier.c | 4 | ||||
-rw-r--r-- | src/sound.c | 81 | ||||
-rw-r--r-- | src/sound.h | 2 |
3 files changed, 82 insertions, 5 deletions
diff --git a/src/notifier.c b/src/notifier.c index 47690ca..63e7c89 100644 --- a/src/notifier.c +++ b/src/notifier.c @@ -149,7 +149,9 @@ play_low_battery_sound (void) filename = datafile_find(DATAFILE_TYPE_SOUND, key); if (filename != NULL) { - sound_play_file(filename); + gchar * uri = g_filename_to_uri(filename, NULL, NULL); + sound_play_uri(uri); + g_free(uri); g_free(filename); } else diff --git a/src/sound.c b/src/sound.c index d92aee8..37c2a8d 100644 --- a/src/sound.c +++ b/src/sound.c @@ -19,11 +19,86 @@ #include "sound.h" -#include <gio/gio.h> +#include <glib.h> + +#include <gst/gst.h> + +static void +gst_init_once(void) +{ + static gboolean gst_init_checked = FALSE; + + if (G_UNLIKELY(!gst_init_checked)) + { + GError* error = NULL; + if (!gst_init_check(NULL, NULL, &error)) + { + g_critical("Unable to play alarm sound: %s", error->message); + g_error_free(error); + } + gst_init_checked = TRUE; + } +} + +static gboolean bus_callback(GstBus* bus G_GNUC_UNUSED, GstMessage* msg, gpointer gelement) +{ + const GstMessageType message_type = GST_MESSAGE_TYPE(msg); + + if (GST_MESSAGE_SRC(msg) != gelement) + return G_SOURCE_CONTINUE; + + /* on eos, cleanup the element and cancel our gst bus subscription */ + if (message_type == GST_MESSAGE_EOS) + { + g_debug("got GST_MESSAGE_EOS on sound play"); + gst_element_set_state(GST_ELEMENT(gelement), GST_STATE_NULL); + gst_object_unref(gelement); + return G_SOURCE_REMOVE; + } + + /* on stream start, set the media role to 'alert' if we're using pulsesink */ + if (message_type == GST_MESSAGE_STREAM_START) + { + GstElement* audio_sink = NULL; + g_debug("got GST_MESSAGE_STREAM_START on sound play"); + g_object_get(gelement, "audio-sink", &audio_sink, NULL); + if (audio_sink != NULL) + { + GstPluginFeature* feature; + feature = GST_PLUGIN_FEATURE_CAST(GST_ELEMENT_GET_CLASS(audio_sink)->elementfactory); + if (feature && g_strcmp0(gst_plugin_feature_get_name(feature), "pulsesink") == 0) + { + const gchar* const props_str = "props,media.role=alert"; + GstStructure* props = gst_structure_from_string(props_str, NULL); + g_debug("setting audio sink properties to '%s'", props_str); + g_object_set(audio_sink, "stream-properties", props, NULL); + g_clear_pointer(&props, gst_structure_free); + } + gst_object_unref(audio_sink); + } + } + + return G_SOURCE_CONTINUE; +} void -sound_play_file(const char* filename) +sound_play_uri(const char* uri) { - g_message("playing sound '%s'", filename); + GstElement * element; + GstBus * bus; + + gst_init_once(); + + element = gst_element_factory_make("playbin", NULL); + + /* start listening for gst events */ + bus = gst_pipeline_get_bus(GST_PIPELINE(element)); + gst_bus_add_watch(bus, bus_callback, element); + gst_object_unref(bus); + + /* play the sound */ + g_debug("Playing '%s'", uri); + g_object_set(element, "uri", uri, NULL); + gst_element_set_state(element, GST_STATE_PLAYING); } diff --git a/src/sound.h b/src/sound.h index a241e5d..f201fe9 100644 --- a/src/sound.h +++ b/src/sound.h @@ -24,7 +24,7 @@ G_BEGIN_DECLS -void sound_play_file(const char* filename); +void sound_play_uri(const char* uri); G_END_DECLS |