From 2511db3a0841411d1a0e126f96f3b1a336800a83 Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Wed, 16 Dec 2015 09:18:01 -0500 Subject: [PATCH] core/audio: Add audio_cur_state() function This replaces the is_playing() function of the AudioDriver class. I can also make the GUIs GstDriver static. Signed-off-by: Anna Schumaker --- core/audio.cpp | 28 ++++++++++++++-------------- gui/gst.cpp | 27 +++------------------------ gui/ocarina.cpp | 1 - include/core/audio.h | 12 +++--------- include/gui/ocarina.h | 1 - tests/core/audio.cpp | 21 +++++++++++---------- 6 files changed, 31 insertions(+), 59 deletions(-) diff --git a/core/audio.cpp b/core/audio.cpp index cfd00832..162b0ea9 100644 --- a/core/audio.cpp +++ b/core/audio.cpp @@ -28,17 +28,10 @@ static void __audio_save() file_close(&audio_file); } -static GstState __audio_cur_state() -{ - GstState cur; - gst_element_get_state(audio_player, &cur, NULL, GST_CLOCK_TIME_NONE); - return cur; -} - static bool __audio_change_state(GstState state) { GstStateChangeReturn ret = GST_STATE_CHANGE_FAILURE; - if (__audio_cur_state() != state) + if (audio_cur_state() != state) ret = gst_element_set_state(audio_player, state); return ret != GST_STATE_CHANGE_FAILURE; } @@ -140,8 +133,7 @@ bool audio_load(struct track *track) { if (track == audio_track) return false; - if (__audio_load(track, cur_driver->is_playing() ? GST_STATE_PLAYING : - GST_STATE_PAUSED)) { + if (__audio_load(track, audio_cur_state())) { history_add(audio_track); return true; } @@ -153,6 +145,16 @@ struct track *audio_cur_track() return audio_track; } +GstState audio_cur_state() +{ + GstState cur = GST_STATE_NULL; + if (audio_player) + gst_element_get_state(audio_player, + &cur, NULL, + GST_CLOCK_TIME_NONE); + return cur; +} + bool audio_play() { if (!audio_track) @@ -210,15 +212,13 @@ void audio :: next() struct track *track = tempq_next(); if (!track) track = queue_next(collection_get_queue()); - if (__audio_load(track, cur_driver->is_playing() ? GST_STATE_PLAYING : - GST_STATE_PAUSED)) + if (__audio_load(track, audio_cur_state())) history_add(audio_track); } void audio :: prev() { - __audio_load(history_prev(), cur_driver->is_playing() ? - GST_STATE_PLAYING : GST_STATE_PAUSED); + __audio_load(history_prev(), audio_cur_state()); } void audio :: pause_after(bool enabled, unsigned int n) diff --git a/gui/gst.cpp b/gui/gst.cpp index 1c338c9c..b7bc47c3 100644 --- a/gui/gst.cpp +++ b/gui/gst.cpp @@ -70,21 +70,7 @@ static void on_load(struct track *track, GstState state) } -class GSTDriver : public AudioDriver -{ -public: - bool is_playing() - { - GstState state; - gst_element_get_state(audio_get_player(), - &state, - NULL, - GST_CLOCK_TIME_NONE); - return state == GST_STATE_PLAYING; - } -}; - -static GSTDriver *gst_driver; +class GSTDriver : public AudioDriver {} gst_driver; struct audio_ops audio_ops = { on_load, @@ -122,7 +108,7 @@ static gboolean on_gst_message(GstBus *bus, GstMessage *message, gpointer data) gst :: play(); break; case GST_MESSAGE_EOS: - gst_driver->eos(); + gst_driver.eos(); o_count->set_value(audio :: pause_count()); o_enabled->set_active(audio :: pause_enabled()); break; @@ -191,7 +177,7 @@ void gst :: next() void gst :: toggle() { - if (gst_driver->is_playing()) + if (audio_cur_state() == GST_STATE_PLAYING) gst :: pause(); else gst :: play(); @@ -199,8 +185,6 @@ void gst :: toggle() void gst :: init_pre() { - gst_driver = new GSTDriver(); - o_next = gui :: get_widget("o_next"); o_pause = gui :: get_widget("o_pause"); o_play = gui :: get_widget("o_play"); @@ -235,8 +219,3 @@ void gst :: init() gst_bus_add_watch(gst_bus, on_gst_message, NULL); Glib :: signal_timeout().connect(sigc::ptr_fun(on_timeout), 500); } - -void gst :: quit() -{ - delete gst_driver; -} diff --git a/gui/ocarina.cpp b/gui/ocarina.cpp index 9b3eb8e7..2ba44946 100644 --- a/gui/ocarina.cpp +++ b/gui/ocarina.cpp @@ -76,7 +76,6 @@ int main(int argc, char **argv) ocarina_app->run(*window, argc, argv); core :: deinit(); cleanup_tabs(); - gst :: quit(); return 0; } diff --git a/include/core/audio.h b/include/core/audio.h index 15e39b5d..19b2757e 100644 --- a/include/core/audio.h +++ b/include/core/audio.h @@ -30,15 +30,6 @@ public: AudioDriver(); /**< Default AudioDriver constructor. */ virtual ~AudioDriver(); /**< AudioDriver destructor. */ - - /** - * Called to check if the audio library is currently playing a track. - * - * @return True if audio library is playing, false otherwise. - */ - virtual bool is_playing() = 0; - - /** * Called to handle reaching the end-of-stream. */ @@ -99,6 +90,9 @@ bool audio_load(struct track *); /* Called to get the current track. */ struct track *audio_cur_track(); +/* Called to get the current playback state. */ +GstState audio_cur_state(); + /* Called to begin playback. */ bool audio_play(); diff --git a/include/gui/ocarina.h b/include/gui/ocarina.h index 271c35b7..bcaaf29c 100644 --- a/include/gui/ocarina.h +++ b/include/gui/ocarina.h @@ -50,7 +50,6 @@ namespace gst void toggle(); void init_pre(); void init(); - void quit(); } diff --git a/tests/core/audio.cpp b/tests/core/audio.cpp index 6e4b5804..7147a5ed 100644 --- a/tests/core/audio.cpp +++ b/tests/core/audio.cpp @@ -17,13 +17,10 @@ static unsigned int load_count = 0; static bool test_audio_seek(gint64 pos) { bool ret = audio_seek(pos); - GstState state; - - do { - gst_element_get_state(audio_get_player(), &state, NULL, - GST_CLOCK_TIME_NONE); - } while (state != GST_STATE_PAUSED && state != GST_STATE_PLAYING); + GstState state = audio_cur_state(); + while (state != GST_STATE_PAUSED && state != GST_STATE_PLAYING) + state = audio_cur_state(); return ret; } @@ -35,7 +32,6 @@ public: TestDriver() : playing(false) {} ~TestDriver() {}; - bool is_playing() { return playing; } } driver; @@ -60,8 +56,8 @@ static struct core_init_data test_init_data = { static void test_init() { test_equal(audio_get_player(), NULL); - test_equal(audio_cur_track(), NULL); - test_equal(audio_get_player(), NULL); + test_equal(audio_cur_track(), NULL); + test_equal(audio_cur_state(), GST_STATE_NULL); core :: init(NULL, NULL, &test_init_data); @@ -73,6 +69,7 @@ static void test_init() test_equal(audio_position(), 0); test_equal(audio_duration(), 0); test_equal(audio_cur_track(), NULL); + test_equal(audio_cur_state(), GST_STATE_NULL); test_equal(load_count, 0); collection_add("tests/Music/Hyrule Symphony"); @@ -87,6 +84,7 @@ static void test_playback() test_equal(audio_load(track_get(0)), true); test_equal(load_count, 1); test_equal(audio_cur_track(), track_get(0)); + test_equal(audio_cur_state(), GST_STATE_NULL); test_equal(queue_size(history_get_queue()), 1); test_equal(audio_duration(), track_get(0)->tr_length * GST_SECOND); @@ -101,22 +99,25 @@ static void test_playback() test_equal(audio_play(), true); test_equal(audio_play(), false); + test_equal(audio_cur_state(), GST_STATE_PLAYING); test_equal(audio_pause(), true); test_equal(audio_pause(), false); + test_equal(audio_cur_state(), GST_STATE_PAUSED); test_equal(test_audio_seek(5 * GST_SECOND), true); test_equal(audio_position(), 5 * GST_SECOND); test_equal(audio_stop(), true); test_equal(audio_position(), 0); + test_equal(audio_cur_state(), GST_STATE_PAUSED); test_equal(test_audio_seek(42 * GST_SECOND), true); test_equal(audio_position(), 42 * GST_SECOND); test_equal(audio_play(), true); test_equal(audio_stop(), true); - test_equal(audio_pause(), false); + test_equal(audio_cur_state(), GST_STATE_PAUSED); test_equal(audio_position(), 0); /* Check duration again now that track is fully loaded. */