From d9540b29d6e280d21758acc9a3960e9803b4b868 Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Tue, 15 Dec 2015 08:16:06 -0500 Subject: [PATCH] core/audio: Move audio_init() out of the audio namespace I also move the gstreamer playbin back into core/ Signed-off-by: Anna Schumaker --- core/audio.cpp | 33 ++++++++++++++++++++------------ core/core.cpp | 4 ++-- gui/gst.cpp | 23 +++++++++++----------- gui/ocarina.cpp | 5 ++--- include/core/audio.h | 8 +++++++- include/core/core.h | 2 +- include/gui/ocarina.h | 3 ++- tests/core/Sconscript | 2 ++ tests/core/audio.cpp | 44 +++++++++++++++++++++++++++---------------- 9 files changed, 76 insertions(+), 48 deletions(-) diff --git a/core/audio.cpp b/core/audio.cpp index f462a789..e4831ecc 100644 --- a/core/audio.cpp +++ b/core/audio.cpp @@ -10,19 +10,21 @@ extern "C" { } +static struct file audio_file; +static GstElement *audio_player = NULL; + static bool _pause_enabled = false; static unsigned int _pause_count = 0; static struct track *cur_track = NULL; -static struct file f_cur_track; static AudioDriver *cur_driver = NULL; static void save_state() { - file_open(&f_cur_track, OPEN_WRITE); - file_writef(&f_cur_track, "%u\n", cur_track->tr_dbe.dbe_index); - file_close(&f_cur_track); + file_open(&audio_file, OPEN_WRITE); + file_writef(&audio_file, "%u\n", cur_track->tr_dbe.dbe_index); + file_close(&audio_file); } static void _load_track(struct track *track, bool start_playback) @@ -83,16 +85,18 @@ void AudioDriver :: eos() -void audio :: init() +void audio_init(int *argc, char ***argv) { - unsigned int id; + unsigned int track; - file_init(&f_cur_track, "cur_track", 0); - if (file_exists(&f_cur_track)) { - file_open(&f_cur_track, OPEN_READ); - file_readf(&f_cur_track, "%u", &id); - file_close(&f_cur_track); - audio :: load_track(track_get(id)); + gst_init(argc, argv); + audio_player = gst_element_factory_make("playbin", "ocarina_player"); + + file_init(&audio_file, "cur_track", 0); + if (file_open(&audio_file, OPEN_READ)) { + file_readf(&audio_file, "%u", &track); + file_close(&audio_file); + audio :: load_track(track_get(track)); } } @@ -186,3 +190,8 @@ AudioDriver *audio :: get_driver() { return cur_driver; } + +GstElement *audio_get_player() +{ + return audio_player; +} diff --git a/core/core.cpp b/core/core.cpp index ce351a79..4d73f82b 100644 --- a/core/core.cpp +++ b/core/core.cpp @@ -13,7 +13,7 @@ extern "C" { } -void core :: init(struct core_init_data *init) +void core :: init(int *argc, char ***argv, struct core_init_data *init) { filter_init(); tags_init(); @@ -21,7 +21,7 @@ void core :: init(struct core_init_data *init) collection_init(init->collection_ops); history_init(init->history_ops); tempq_init(init->tempq_ops); - audio :: init(); + audio_init(argc, argv); } void core :: deinit() diff --git a/gui/gst.cpp b/gui/gst.cpp index f3307b9f..344b0e45 100644 --- a/gui/gst.cpp +++ b/gui/gst.cpp @@ -15,7 +15,6 @@ extern "C" { static GstBus *gst_bus; -static GstElement *gst_player; static Gtk::Button *o_next; static Gtk::Button *o_pause; @@ -37,7 +36,7 @@ static Glib::RefPtr o_progress; static bool gst_change_state(GstState state) { - GstStateChangeReturn ret = gst_element_set_state(gst_player, state); + GstStateChangeReturn ret = gst_element_set_state(audio_get_player(), state); switch (ret) { case GST_STATE_CHANGE_SUCCESS: case GST_STATE_CHANGE_ASYNC: @@ -66,7 +65,7 @@ public: gchar *str; gst_change_state(GST_STATE_NULL); - g_object_set(G_OBJECT(gst_player), "uri", uri, NULL); + g_object_set(G_OBJECT(audio_get_player()), "uri", uri, NULL); g_free(uri); str = g_strdup_printf("From: %s", track->tr_album->al_name); @@ -105,7 +104,7 @@ public: bool is_playing() { GstState state; - gst_element_get_state(gst_player, + gst_element_get_state(audio_get_player(), &state, NULL, GST_CLOCK_TIME_NONE); @@ -114,7 +113,7 @@ public: void seek_to(int64_t offset) { - gst_element_seek_simple(gst_player, + gst_element_seek_simple(audio_get_player(), GST_FORMAT_TIME, GST_SEEK_FLAG_FLUSH, offset); @@ -123,7 +122,7 @@ public: int64_t position() { int64_t position; - if (gst_element_query_position(gst_player, + if (gst_element_query_position(audio_get_player(), GST_FORMAT_TIME, &position)) return position; @@ -133,7 +132,7 @@ public: int64_t duration() { int64_t duration; - if (gst_element_query_duration(gst_player, + if (gst_element_query_duration(audio_get_player(), GST_FORMAT_TIME, &duration)) return duration; @@ -230,12 +229,8 @@ void gst :: toggle() audio :: play(); } -void gst :: init(int *argc, char ***argv) +void gst :: init_pre() { - gst_init(argc, argv); - - gst_player = gst_element_factory_make("playbin", "ocarina_player"); - gst_bus = gst_pipeline_get_bus(GST_PIPELINE(gst_player)); gst_driver = new GSTDriver(); o_next = gui :: get_widget("o_next"); @@ -264,7 +259,11 @@ void gst :: init(int *argc, char ***argv) o_count->signal_changed().connect(sigc::ptr_fun(on_pause_count)); o_enabled->signal_toggled().connect(sigc::ptr_fun(on_pause_enabled)); +} +void gst :: init() +{ + gst_bus = gst_pipeline_get_bus(GST_PIPELINE(audio_get_player())); gst_bus_add_watch(gst_bus, on_gst_message, NULL); Glib :: signal_timeout().connect(sigc::ptr_fun(on_timeout), 500); } diff --git a/gui/ocarina.cpp b/gui/ocarina.cpp index 41bfb3ae..a2742b97 100644 --- a/gui/ocarina.cpp +++ b/gui/ocarina.cpp @@ -61,9 +61,8 @@ int main(int argc, char **argv) if (!gui::__O_BUILDER->add_from_file(gui::share_file("ocarina6.glade"))) exit(1); - gst :: init(&argc, &argv); - - core :: init(&init_data); + gst :: init_pre(); + core :: init(&argc, &argv, &init_data); plist :: init(); manager :: init(); diff --git a/include/core/audio.h b/include/core/audio.h index 5979a063..c266da7f 100644 --- a/include/core/audio.h +++ b/include/core/audio.h @@ -7,6 +7,7 @@ extern "C" { #include } +#include #include #include @@ -83,7 +84,6 @@ public: namespace audio { - void init(); /**< Initializes the audio layer. */ void play(); /**< Begin playback. */ void pause(); /**< Pause playback. */ @@ -154,4 +154,10 @@ namespace audio AudioDriver *get_driver(); }; + +/* Called to initialize the audio manager. */ +void audio_init(int *, char ***); + + +GstElement *audio_get_player(); #endif /* OCARINA_CORE_AUDIO_H */ diff --git a/include/core/core.h b/include/core/core.h index 84ee525c..0319937c 100644 --- a/include/core/core.h +++ b/include/core/core.h @@ -24,7 +24,7 @@ namespace core * Initializes all components of the core library, including reading * databases from disk and setting up gstreamer. */ - void init(struct core_init_data *); + void init(int *, char ***, struct core_init_data *); void deinit(); } diff --git a/include/gui/ocarina.h b/include/gui/ocarina.h index c4ea08e2..0fce6061 100644 --- a/include/gui/ocarina.h +++ b/include/gui/ocarina.h @@ -44,7 +44,8 @@ namespace gst { void next(); void toggle(); - void init(int *, char ***); + void init_pre(); + void init(); void quit(); } diff --git a/tests/core/Sconscript b/tests/core/Sconscript index 32270481..6c40ff20 100644 --- a/tests/core/Sconscript +++ b/tests/core/Sconscript @@ -42,6 +42,8 @@ res += [ CoreTest("playlist") ] res += [ CoreTest("collection") ] res += [ CoreTest("history") ] res += [ CoreTest("tempq") ] + +core_objs += [ env.Object("../../core/core.cpp") ] res += [ CoreTest("audio") ] Return("res") diff --git a/tests/core/audio.cpp b/tests/core/audio.cpp index 384389de..ede3ec66 100644 --- a/tests/core/audio.cpp +++ b/tests/core/audio.cpp @@ -4,12 +4,8 @@ #include extern "C" { #include -#include -#include -#include -#include -#include } +#include #include "test.h" struct track *TRACK_NULL = NULL; @@ -44,6 +40,28 @@ public: } driver; +static struct core_init_data test_init_data = { + NULL, + NULL, + NULL, + NULL, +}; + + +static void test_init() +{ + test_equal(audio :: current_track(), NULL); + test_equal(audio_get_player(), NULL); + + core :: init(NULL, NULL, &test_init_data); + + test_equal(audio :: current_track(), NULL); + test_not_equal(audio_get_player(), NULL); + + core :: deinit(); +} + + void test_pre_init() { TestDriver *driver = (TestDriver *)audio :: get_driver(); @@ -78,24 +96,17 @@ void test_pre_init() test_equal(audio :: current_track(), TRACK_NULL); } -void test_init() +void test_init2() { struct track *track; test_cp_data_dir(); - audio :: init(); + audio_init(NULL, NULL); track = audio :: current_track(); test_equal(track, TRACK_NULL); - filter_init(); - tags_init(); - playlist_init(NULL); - collection_init(NULL); - history_init(NULL); - tempq_init(NULL); - audio :: init(); - + core :: init(NULL, NULL, &test_init_data); track = audio :: current_track(); test_not_equal(track, TRACK_NULL); } @@ -194,8 +205,9 @@ void test_autopause() } DECLARE_UNIT_TESTS( + UNIT_TEST("Audio Initialization", test_init), UNIT_TEST("Test Audio Pre-Init", test_pre_init), - UNIT_TEST("Test Audio Init", test_init), + UNIT_TEST("Test Audio Init 2", test_init2), UNIT_TEST("Test Audio Playback Controls", test_playback_controls), UNIT_TEST("Test Audio Automatic Pausing", test_autopause), );