From a8fd3f49bf6509bd4545aec3292d2fc022d847ca Mon Sep 17 00:00:00 2001 From: charles kerr Date: Fri, 1 Jan 2016 15:58:55 -0600 Subject: make a SoundPlayer interface so we can mock it in the tests this requires an annoying amount of scaffolding: 1. implement gst and mock classes to implement the SoundPlayer interface 2. modify notifier to take a SoundPlayer argument in its ctor 3. modify service to take a Notifier argument in its ctor instead of instantiating it on its own 4. change main to update the startup steps for player/notifier/service --- src/CMakeLists.txt | 4 +- src/main.c | 12 +++- src/notifier.c | 61 ++++++++++++++--- src/notifier.h | 6 +- src/service.c | 38 +++++++++-- src/service.h | 7 +- src/sound-player-gst.c | 173 ++++++++++++++++++++++++++++++++++++++++++++++++ src/sound-player-gst.h | 70 ++++++++++++++++++++ src/sound-player-mock.c | 94 ++++++++++++++++++++++++++ src/sound-player-mock.h | 75 +++++++++++++++++++++ src/sound-player.c | 45 +++++++++++++ src/sound-player.h | 72 ++++++++++++++++++++ src/sound.c | 104 ----------------------------- src/sound.h | 31 --------- 14 files changed, 639 insertions(+), 153 deletions(-) create mode 100644 src/sound-player-gst.c create mode 100644 src/sound-player-gst.h create mode 100644 src/sound-player-mock.c create mode 100644 src/sound-player-mock.h create mode 100644 src/sound-player.c create mode 100644 src/sound-player.h delete mode 100644 src/sound.c delete mode 100644 src/sound.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index ed0e0ff..c99ce46 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -20,7 +20,9 @@ set(SERVICE_MANUAL_SOURCES testing.c service.c utils.c - sound.c) + sound-player.c + sound-player-gst.c + sound-player-mock.c) # generated sources include(GdbusCodegen) diff --git a/src/main.c b/src/main.c index 03e100d..2837b59 100644 --- a/src/main.c +++ b/src/main.c @@ -23,7 +23,9 @@ #include #include "device.h" +#include "notifier.h" #include "service.h" +#include "sound-player-gst.h" #include "testing.h" /*** @@ -40,6 +42,8 @@ on_name_lost (gpointer instance G_GNUC_UNUSED, gpointer loop) int main (int argc G_GNUC_UNUSED, char ** argv G_GNUC_UNUSED) { + IndicatorPowerSoundPlayer * sound_player; + IndicatorPowerNotifier * notifier; IndicatorPowerService * service; IndicatorPowerTesting * testing; GMainLoop * loop; @@ -50,7 +54,9 @@ main (int argc G_GNUC_UNUSED, char ** argv G_GNUC_UNUSED) textdomain (GETTEXT_PACKAGE); /* run */ - service = indicator_power_service_new (NULL); + sound_player = indicator_power_sound_player_gst_new (); + notifier = indicator_power_notifier_new (sound_player); + service = indicator_power_service_new(NULL, notifier); testing = indicator_power_testing_new (service); loop = g_main_loop_new (NULL, FALSE); g_signal_connect (service, INDICATOR_POWER_SERVICE_SIGNAL_NAME_LOST, @@ -59,7 +65,9 @@ main (int argc G_GNUC_UNUSED, char ** argv G_GNUC_UNUSED) /* cleanup */ g_main_loop_unref (loop); - g_clear_object (&service); g_clear_object (&testing); + g_clear_object (&service); + g_clear_object (¬ifier); + g_clear_object (&sound_player); return 0; } diff --git a/src/notifier.c b/src/notifier.c index 63e7c89..b50f48c 100644 --- a/src/notifier.c +++ b/src/notifier.c @@ -22,10 +22,10 @@ #include "dbus-shared.h" #include "notifier.h" #include "utils.h" -#include "sound.h" +#include "sound-player.h" #ifdef HAS_URLDISPATCHER -# include +#include #endif #include @@ -51,10 +51,12 @@ enum { PROP_0, PROP_BATTERY, + PROP_SOUND_PLAYER, LAST_PROP }; #define PROP_BATTERY_NAME "battery" +#define PROP_SOUND_PLAYER_NAME "sound-player" static GParamSpec * properties[LAST_PROP]; @@ -82,6 +84,8 @@ typedef struct gboolean caps_queried; gboolean actions_supported; + + IndicatorPowerSoundPlayer * sound_player; } IndicatorPowerNotifierPrivate; @@ -140,17 +144,19 @@ get_battery_power_level (IndicatorPowerDevice * battery) ***/ static void -play_low_battery_sound (void) +play_low_battery_sound (IndicatorPowerNotifier * self) { - const gchar * key; + const gchar * const key = "Low battery.ogg"; gchar * filename; + priv_t * const p = get_priv(self); + + g_return_if_fail (p->sound_player != NULL); - key = "Low battery.ogg"; filename = datafile_find(DATAFILE_TYPE_SOUND, key); if (filename != NULL) { gchar * uri = g_filename_to_uri(filename, NULL, NULL); - sound_play_uri(uri); + indicator_power_sound_player_play_uri (p->sound_player, uri); g_free(uri); g_free(filename); } @@ -330,7 +336,7 @@ on_battery_property_changed (IndicatorPowerNotifier * self) ((new_power_level != POWER_LEVEL_OK) && new_discharging && !old_discharging)) { notification_show (self); - play_low_battery_sound(); + play_low_battery_sound (self); } else if (!new_discharging || (new_power_level == POWER_LEVEL_OK)) { @@ -361,6 +367,10 @@ my_get_property (GObject * o, g_value_set_object (value, p->battery); break; + case PROP_SOUND_PLAYER: + g_value_set_object (value, p->sound_player); + break; + default: G_OBJECT_WARN_INVALID_PROPERTY_ID (o, property_id, pspec); } @@ -380,6 +390,10 @@ my_set_property (GObject * o, indicator_power_notifier_set_battery (self, g_value_get_object(value)); break; + case PROP_SOUND_PLAYER: + indicator_power_notifier_set_sound_player (self, g_value_get_object(value)); + break; + default: G_OBJECT_WARN_INVALID_PROPERTY_ID (o, property_id, pspec); } @@ -446,6 +460,13 @@ indicator_power_notifier_class_init (IndicatorPowerNotifierClass * klass) G_TYPE_OBJECT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS); + properties[PROP_SOUND_PLAYER] = g_param_spec_object ( + PROP_SOUND_PLAYER_NAME, + "Sound Player", + "The current battery", + G_TYPE_OBJECT, + G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS); + g_object_class_install_properties (object_class, LAST_PROP, properties); } @@ -454,9 +475,11 @@ indicator_power_notifier_class_init (IndicatorPowerNotifierClass * klass) ***/ IndicatorPowerNotifier * -indicator_power_notifier_new (void) +indicator_power_notifier_new (IndicatorPowerSoundPlayer * sound_player) { - GObject * o = g_object_new (INDICATOR_TYPE_POWER_NOTIFIER, NULL); + GObject * o = g_object_new (INDICATOR_TYPE_POWER_NOTIFIER, + PROP_SOUND_PLAYER_NAME, sound_player, + NULL); return INDICATOR_POWER_NOTIFIER (o); } @@ -495,6 +518,26 @@ indicator_power_notifier_set_battery (IndicatorPowerNotifier * self, } } +void +indicator_power_notifier_set_sound_player (IndicatorPowerNotifier * self, + IndicatorPowerSoundPlayer * sound_player) +{ + priv_t * p; + + g_return_if_fail(INDICATOR_IS_POWER_NOTIFIER(self)); + g_return_if_fail((sound_player == NULL) || INDICATOR_IS_POWER_SOUND_PLAYER(sound_player)); + + p = get_priv (self); + + if (p->sound_player == sound_player) + return; + + g_clear_object(&p->sound_player); + + if (sound_player != NULL) + p->sound_player = g_object_ref(sound_player); +} + void indicator_power_notifier_set_bus (IndicatorPowerNotifier * self, GDBusConnection * bus) diff --git a/src/notifier.h b/src/notifier.h index 18e25d7..9625c07 100644 --- a/src/notifier.h +++ b/src/notifier.h @@ -23,6 +23,7 @@ #include #include "device.h" +#include "sound-player.h" G_BEGIN_DECLS @@ -55,7 +56,7 @@ struct _IndicatorPowerNotifierClass GType indicator_power_notifier_get_type (void); -IndicatorPowerNotifier * indicator_power_notifier_new (void); +IndicatorPowerNotifier * indicator_power_notifier_new (IndicatorPowerSoundPlayer * sound_player); void indicator_power_notifier_set_bus (IndicatorPowerNotifier * self, GDBusConnection * connection); @@ -63,6 +64,9 @@ void indicator_power_notifier_set_bus (IndicatorPowerNotifier * self, void indicator_power_notifier_set_battery (IndicatorPowerNotifier * self, IndicatorPowerDevice * battery); +void indicator_power_notifier_set_sound_player (IndicatorPowerNotifier * self, + IndicatorPowerSoundPlayer * battery); + #define POWER_LEVEL_STR_OK "ok" #define POWER_LEVEL_STR_LOW "low" #define POWER_LEVEL_STR_VERY_LOW "very_low" diff --git a/src/service.c b/src/service.c index 73fcf46..07f1106 100644 --- a/src/service.c +++ b/src/service.c @@ -52,6 +52,7 @@ enum PROP_0, PROP_BUS, PROP_DEVICE_PROVIDER, + PROP_NOTIFIER, LAST_PROP }; @@ -976,7 +977,8 @@ on_bus_acquired (GDBusConnection * connection, g_object_notify_by_pspec (G_OBJECT(self), properties[PROP_BUS]); /* export the battery properties */ - indicator_power_notifier_set_bus (p->notifier, connection); + if (p->notifier != NULL) + indicator_power_notifier_set_bus (p->notifier, connection); /* export the actions */ if ((id = g_dbus_connection_export_action_group (connection, @@ -1118,6 +1120,10 @@ my_get_property (GObject * o, g_value_set_object (value, p->device_provider); break; + case PROP_NOTIFIER: + g_value_set_object (value, p->notifier); + break; + default: G_OBJECT_WARN_INVALID_PROPERTY_ID (o, property_id, pspec); } @@ -1137,6 +1143,10 @@ my_set_property (GObject * o, indicator_power_service_set_device_provider (self, g_value_get_object (value)); break; + case PROP_NOTIFIER: + indicator_power_service_set_notifier (self, g_value_get_object (value)); + break; + default: G_OBJECT_WARN_INVALID_PROPERTY_ID (o, property_id, pspec); } @@ -1179,6 +1189,7 @@ my_dispose (GObject * o) g_clear_object (&p->conn); indicator_power_service_set_device_provider (self, NULL); + indicator_power_service_set_notifier (self, NULL); G_OBJECT_CLASS (indicator_power_service_parent_class)->dispose (o); } @@ -1200,8 +1211,6 @@ indicator_power_service_init (IndicatorPowerService * self) p->settings = g_settings_new ("org.ayatana.indicator.power"); - p->notifier = indicator_power_notifier_new (); - p->brightness = indicator_power_brightness_new(); g_signal_connect_swapped(p->brightness, "notify::percentage", G_CALLBACK(update_brightness_action_state), self); @@ -1269,10 +1278,12 @@ indicator_power_service_class_init (IndicatorPowerServiceClass * klass) ***/ IndicatorPowerService * -indicator_power_service_new (IndicatorPowerDeviceProvider * device_provider) +indicator_power_service_new (IndicatorPowerDeviceProvider * device_provider, + IndicatorPowerNotifier * notifier) { GObject * o = g_object_new (INDICATOR_TYPE_POWER_SERVICE, "device-provider", device_provider, + "notifier", notifier, NULL); return INDICATOR_POWER_SERVICE (o); @@ -1311,6 +1322,25 @@ indicator_power_service_set_device_provider (IndicatorPowerService * self, } } +void +indicator_power_service_set_notifier (IndicatorPowerService * self, + IndicatorPowerNotifier * notifier) +{ + priv_t * p; + + g_return_if_fail (INDICATOR_IS_POWER_SERVICE (self)); + g_return_if_fail (!notifier || INDICATOR_IS_POWER_NOTIFIER (notifier)); + p = self->priv; + + if (p->notifier != notifier) + { + g_clear_object (&p->notifier); + p->notifier = g_object_ref (notifier); + indicator_power_notifier_set_bus (p->notifier, p->conn); + } +} + + /* If a device has multiple batteries and uses only one of them at a time, they should be presented as separate items inside the battery menu, but everywhere else they should be aggregated (bug 880881). diff --git a/src/service.h b/src/service.h index 76ed10f..00ed3e6 100644 --- a/src/service.h +++ b/src/service.h @@ -24,6 +24,7 @@ #include #include "device-provider.h" +#include "notifier.h" G_BEGIN_DECLS @@ -64,11 +65,15 @@ struct _IndicatorPowerServiceClass GType indicator_power_service_get_type (void); -IndicatorPowerService * indicator_power_service_new (IndicatorPowerDeviceProvider * provider); +IndicatorPowerService * indicator_power_service_new (IndicatorPowerDeviceProvider * provider, + IndicatorPowerNotifier * notifier); void indicator_power_service_set_device_provider (IndicatorPowerService * self, IndicatorPowerDeviceProvider * provider); +void indicator_power_service_set_notifier (IndicatorPowerService * self, + IndicatorPowerNotifier * notifier); + IndicatorPowerDevice * indicator_power_service_choose_primary_device (GList * devices); diff --git a/src/sound-player-gst.c b/src/sound-player-gst.c new file mode 100644 index 0000000..9b3f11a --- /dev/null +++ b/src/sound-player-gst.c @@ -0,0 +1,173 @@ +/* + * Copyright 2013 Canonical Ltd. + * + * Authors: + * Charles Kerr + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 3, as published + * by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranties of + * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#include "sound-player.h" +#include "sound-player-gst.h" + +#include + + +/*** +**** private struct +***/ + +typedef struct +{ + int unused; +} +IndicatorPowerSoundPlayerGSTPrivate; + +typedef IndicatorPowerSoundPlayerGSTPrivate priv_t; + +#define get_priv(o) ((priv_t*)indicator_power_sound_player_gst_get_instance_private(o)) + + +/*** +**** GObject boilerplate +***/ + +static void indicator_power_device_provider_interface_init ( + IndicatorPowerSoundPlayerInterface * iface); + +G_DEFINE_TYPE_WITH_CODE ( + IndicatorPowerSoundPlayerGST, + indicator_power_sound_player_gst, + G_TYPE_OBJECT, + G_ADD_PRIVATE(IndicatorPowerSoundPlayerGST) + G_IMPLEMENT_INTERFACE (INDICATOR_TYPE_POWER_SOUND_PLAYER, + indicator_power_device_provider_interface_init)) + +/*** +**** GSTREAMER +***/ + +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; +} + +/*** +**** IndicatorPowerSoundPlayer virtual functions +***/ + +static void +my_play_uri (IndicatorPowerSoundPlayer * self G_GNUC_UNUSED, const gchar * uri) +{ + GstElement * element; + GstBus * bus; + + /* create the element */ + 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); +} + +/*** +**** Instantiation +***/ + +static void +indicator_power_sound_player_gst_class_init (IndicatorPowerSoundPlayerGSTClass * klass) +{ + gst_init_once(); +} + +static void +indicator_power_device_provider_interface_init (IndicatorPowerSoundPlayerInterface * iface) +{ + iface->play_uri = my_play_uri; +} + +static void +indicator_power_sound_player_gst_init (IndicatorPowerSoundPlayerGST * self G_GNUC_UNUSED) +{ +} + +/*** +**** Public API +***/ + +IndicatorPowerSoundPlayer * +indicator_power_sound_player_gst_new(void) +{ + gpointer o = g_object_new (INDICATOR_TYPE_POWER_SOUND_PLAYER_GST, NULL); + + return INDICATOR_POWER_SOUND_PLAYER (o); +} diff --git a/src/sound-player-gst.h b/src/sound-player-gst.h new file mode 100644 index 0000000..6ef8866 --- /dev/null +++ b/src/sound-player-gst.h @@ -0,0 +1,70 @@ +/* + * Copyright 2013 Canonical Ltd. + * + * Authors: + * Charles Kerr + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 3, as published + * by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranties of + * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#ifndef __INDICATOR_POWER_SOUND_PLAYER_GST__H__ +#define __INDICATOR_POWER_SOUND_PLAYER_GST__H__ + +#include /* parent class */ + +#include "device-provider.h" + +G_BEGIN_DECLS + +#define INDICATOR_TYPE_POWER_SOUND_PLAYER_GST \ + (indicator_power_sound_player_gst_get_type()) + +#define INDICATOR_POWER_SOUND_PLAYER_GST(o) \ + (G_TYPE_CHECK_INSTANCE_CAST ((o), \ + INDICATOR_TYPE_POWER_SOUND_PLAYER_GST, \ + IndicatorPowerSoundPlayerGST)) + +#define INDICATOR_POWER_SOUND_PLAYER_GST_GET_CLASS(o) \ + (G_TYPE_INSTANCE_GET_CLASS ((o), \ + INDICATOR_TYPE_POWER_SOUND_PLAYER_GST, \ + IndicatorPowerSoundPlayerGSTClass)) + +#define INDICATOR_IS_POWER_SOUND_PLAYER_GST(o) \ + (G_TYPE_CHECK_INSTANCE_TYPE ((o), \ + INDICATOR_TYPE_POWER_SOUND_PLAYER_GST)) + +typedef struct _IndicatorPowerSoundPlayerGST + IndicatorPowerSoundPlayerGST; +typedef struct _IndicatorPowerSoundPlayerGSTClass + IndicatorPowerSoundPlayerGSTClass; + +/** + * An IndicatorPowerSoundPlayer which gets its devices from GST. + */ +struct _IndicatorPowerSoundPlayerGST +{ + GObject parent_instance; +}; + +struct _IndicatorPowerSoundPlayerGSTClass +{ + GObjectClass parent_class; +}; + +GType indicator_power_sound_player_gst_get_type (void); + +IndicatorPowerSoundPlayer * indicator_power_sound_player_gst_new (void); + +G_END_DECLS + +#endif /* __INDICATOR_POWER_SOUND_PLAYER_GST__H__ */ diff --git a/src/sound-player-mock.c b/src/sound-player-mock.c new file mode 100644 index 0000000..17eff21 --- /dev/null +++ b/src/sound-player-mock.c @@ -0,0 +1,94 @@ +/* + * Copyright 2014 Canonical Ltd. + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 3, as published + * by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranties of + * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + * + * Authors: + * Charles Kerr + */ + +#include "sound-player.h" +#include "sound-player-mock.h" + +enum +{ + SIGNAL_URI_PLAYED, + LAST_SIGNAL +}; + +static guint signals[LAST_SIGNAL] = { 0 }; + +/*** +**** GObject boilerplate +***/ + +static void indicator_power_sound_player_interface_init ( + IndicatorPowerSoundPlayerInterface * iface); + +G_DEFINE_TYPE_WITH_CODE ( + IndicatorPowerSoundPlayerMock, + indicator_power_sound_player_mock, + G_TYPE_OBJECT, + G_IMPLEMENT_INTERFACE (INDICATOR_TYPE_POWER_SOUND_PLAYER, + indicator_power_sound_player_interface_init)) + +/*** +**** IndicatorPowerSoundPlayer virtual functions +***/ + +static void +my_play_uri (IndicatorPowerSoundPlayer * self, const gchar * uri) +{ + g_signal_emit (self, signals[SIGNAL_URI_PLAYED], 0, uri, NULL); +} + +/*** +**** Instantiation +***/ + +static void +indicator_power_sound_player_mock_class_init (IndicatorPowerSoundPlayerMockClass * klass G_GNUC_UNUSED) +{ + signals[SIGNAL_URI_PLAYED] = g_signal_new ( + "uri-played", + G_TYPE_FROM_CLASS(klass), + G_SIGNAL_RUN_LAST, + G_STRUCT_OFFSET (IndicatorPowerSoundPlayerMockClass, uri_played), + NULL, NULL, + g_cclosure_marshal_VOID__STRING, + G_TYPE_NONE, 1, G_TYPE_STRING); +} + +static void +indicator_power_sound_player_interface_init (IndicatorPowerSoundPlayerInterface * iface) +{ + iface->play_uri = my_play_uri; +} + +static void +indicator_power_sound_player_mock_init (IndicatorPowerSoundPlayerMock * self G_GNUC_UNUSED) +{ +} + +/*** +**** Public API +***/ + +IndicatorPowerSoundPlayer * +indicator_power_sound_player_mock_new (void) +{ + gpointer o = g_object_new (INDICATOR_TYPE_POWER_SOUND_PLAYER_MOCK, NULL); + + return INDICATOR_POWER_SOUND_PLAYER (o); +} + diff --git a/src/sound-player-mock.h b/src/sound-player-mock.h new file mode 100644 index 0000000..f7f2653 --- /dev/null +++ b/src/sound-player-mock.h @@ -0,0 +1,75 @@ +/* + * Copyright 2014 Canonical Ltd. + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 3, as published + * by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranties of + * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + * + * Authors: + * Charles Kerr + */ + +#ifndef __INDICATOR_POWER_SOUND_PLAYER_MOCK__H__ +#define __INDICATOR_POWER_SOUND_PLAYER_MOCK__H__ + +#include /* parent class */ + +#include "sound-player.h" + +G_BEGIN_DECLS + +#define INDICATOR_TYPE_POWER_SOUND_PLAYER_MOCK \ + (indicator_power_sound_player_mock_get_type()) + +#define INDICATOR_POWER_SOUND_PLAYER_MOCK(o) \ + (G_TYPE_CHECK_INSTANCE_CAST ((o), \ + INDICATOR_TYPE_POWER_SOUND_PLAYER_MOCK, \ + IndicatorPowerSoundPlayerMock)) + +#define INDICATOR_POWER_SOUND_PLAYER_MOCK_GET_CLASS(o) \ + (G_TYPE_INSTANCE_GET_CLASS ((o), \ + INDICATOR_TYPE_POWER_SOUND_PLAYER_MOCK, \ + IndicatorPowerSoundPlayerMockClass)) + +#define INDICATOR_IS_POWER_SOUND_PLAYER_MOCK(o) \ + (G_TYPE_CHECK_INSTANCE_TYPE ((o), \ + INDICATOR_TYPE_POWER_SOUND_PLAYER_MOCK)) + +typedef struct _IndicatorPowerSoundPlayerMock + IndicatorPowerSoundPlayerMock; +typedef struct _IndicatorPowerSoundPlayerMockPriv + IndicatorPowerSoundPlayerMockPriv; +typedef struct _IndicatorPowerSoundPlayerMockClass + IndicatorPowerSoundPlayerMockClass; + +/** + * An IndicatorPowerSoundPlayer which gets its devices from Mock. + */ +struct _IndicatorPowerSoundPlayerMock +{ + GObject parent_instance; +}; + +struct _IndicatorPowerSoundPlayerMockClass +{ + GObjectClass parent_class; + + /* signals */ + void (*uri_played) (IndicatorPowerSoundPlayer * self, const gchar* uri); +}; + +GType indicator_power_sound_player_mock_get_type (void); + +IndicatorPowerSoundPlayer * indicator_power_sound_player_mock_new (void); + +G_END_DECLS + +#endif /* __INDICATOR_POWER_SOUND_PLAYER_MOCK__H__ */ diff --git a/src/sound-player.c b/src/sound-player.c new file mode 100644 index 0000000..f6421ad --- /dev/null +++ b/src/sound-player.c @@ -0,0 +1,45 @@ +/* + * Copyright 2013 Canonical Ltd. + * + * Authors: + * Charles Kerr + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 3, as published + * by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranties of + * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#include "sound-player.h" + +G_DEFINE_INTERFACE (IndicatorPowerSoundPlayer, + indicator_power_sound_player, + 0) + +static void +indicator_power_sound_player_default_init (IndicatorPowerSoundPlayerInterface * klass G_GNUC_UNUSED) +{ +} + +/*** +**** PUBLIC API +***/ + +void +indicator_power_sound_player_play_uri (IndicatorPowerSoundPlayer * self, + const gchar * uri) +{ + IndicatorPowerSoundPlayerInterface * iface; + + g_return_if_fail (INDICATOR_IS_POWER_SOUND_PLAYER (self)); + iface = INDICATOR_POWER_SOUND_PLAYER_GET_INTERFACE (self); + g_return_if_fail (iface->play_uri != NULL); + iface->play_uri (self, uri); +} diff --git a/src/sound-player.h b/src/sound-player.h new file mode 100644 index 0000000..ac7e261 --- /dev/null +++ b/src/sound-player.h @@ -0,0 +1,72 @@ +/* + * Copyright 2013 Canonical Ltd. + * + * Authors: + * Charles Kerr + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 3, as published + * by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranties of + * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#ifndef __INDICATOR_POWER_SOUND_PLAYER__H__ +#define __INDICATOR_POWER_SOUND_PLAYER__H__ + +#include + +G_BEGIN_DECLS + +#define INDICATOR_TYPE_POWER_SOUND_PLAYER \ + (indicator_power_sound_player_get_type ()) + +#define INDICATOR_POWER_SOUND_PLAYER(obj) \ + (G_TYPE_CHECK_INSTANCE_CAST ((obj), \ + INDICATOR_TYPE_POWER_SOUND_PLAYER, \ + IndicatorPowerSoundPlayer)) + +#define INDICATOR_IS_POWER_SOUND_PLAYER(obj) \ + (G_TYPE_CHECK_INSTANCE_TYPE ((obj), INDICATOR_TYPE_POWER_SOUND_PLAYER)) + +#define INDICATOR_POWER_SOUND_PLAYER_GET_INTERFACE(inst) \ + (G_TYPE_INSTANCE_GET_INTERFACE ((inst), \ + INDICATOR_TYPE_POWER_SOUND_PLAYER, \ + IndicatorPowerSoundPlayerInterface)) + +typedef struct _IndicatorPowerSoundPlayer + IndicatorPowerSoundPlayer; + +typedef struct _IndicatorPowerSoundPlayerInterface + IndicatorPowerSoundPlayerInterface; + +/** + * An interface class for an object that plays sounds. + */ +struct _IndicatorPowerSoundPlayerInterface +{ + GTypeInterface parent_iface; + + /* virtual functions */ + void (*play_uri) (IndicatorPowerSoundPlayer * self, + const gchar * uri); +}; + +GType indicator_power_sound_player_get_type (void); + +/*** +**** +***/ + +void indicator_power_sound_player_play_uri (IndicatorPowerSoundPlayer * self, + const gchar * uri); + +G_END_DECLS + +#endif /* __INDICATOR_POWER_SOUND_PLAYER__H__ */ diff --git a/src/sound.c b/src/sound.c deleted file mode 100644 index 37c2a8d..0000000 --- a/src/sound.c +++ /dev/null @@ -1,104 +0,0 @@ -/* - * Copyright 2015 Canonical Ltd. - * - * This program is free software: you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 3, as published - * by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranties of - * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR - * PURPOSE. See the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program. If not, see . - * - * Authors: - * Charles Kerr - */ - -#include "sound.h" - -#include - -#include - -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_uri(const char* uri) -{ - 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 deleted file mode 100644 index f201fe9..0000000 --- a/src/sound.h +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Copyright 2015 Canonical Ltd. - * - * This program is free software: you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 3, as published - * by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranties of - * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR - * PURPOSE. See the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program. If not, see . - * - * Authors: - * Charles Kerr - */ - -#ifndef __INDICATOR_POWER_SOUND_H__ -#define __INDICATOR_POWER_SOUND_H__ - -#include - -G_BEGIN_DECLS - -void sound_play_uri(const char* uri); - -G_END_DECLS - -#endif /* __INDICATOR_POWER_SOUND_H__ */ -- cgit v1.2.3