From 566ef3bb50707e06f2db3462578556457c227a13 Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Tue, 15 Dec 2015 15:50:02 -0500 Subject: [PATCH] core/audio: Move audio_seek() out of the audio namespace --- core/audio.cpp | 12 ++++++++---- gui/gst.cpp | 10 +--------- include/core/audio.h | 17 +++-------------- tests/core/audio.cpp | 29 +++++++++++++++++++++++------ 4 files changed, 35 insertions(+), 33 deletions(-) diff --git a/core/audio.cpp b/core/audio.cpp index e7f14de4..7fcea363 100644 --- a/core/audio.cpp +++ b/core/audio.cpp @@ -162,16 +162,20 @@ bool audio_pause() return __audio_change_state(GST_STATE_PAUSED); } -void audio :: seek_to(int64_t pos) +bool audio_seek(gint64 offset) { - if (audio_track) - cur_driver->seek_to(pos); + if (!audio_track) + return false; + return gst_element_seek_simple(audio_player, + GST_FORMAT_TIME, + GST_SEEK_FLAG_FLUSH, + offset); } void audio :: stop() { audio_pause(); - seek_to(0); + audio_seek(0); } int64_t audio :: position() diff --git a/gui/gst.cpp b/gui/gst.cpp index 01ec7ca1..0b825009 100644 --- a/gui/gst.cpp +++ b/gui/gst.cpp @@ -83,14 +83,6 @@ public: return state == GST_STATE_PLAYING; } - void seek_to(int64_t offset) - { - gst_element_seek_simple(audio_get_player(), - GST_FORMAT_TIME, - GST_SEEK_FLAG_FLUSH, - offset); - } - int64_t position() { int64_t position; @@ -163,7 +155,7 @@ static gboolean on_gst_message(GstBus *bus, GstMessage *message, gpointer data) static bool on_seek(Gtk::ScrollType type, double value) { - audio :: seek_to(value); + audio_seek(value); return true; } diff --git a/include/core/audio.h b/include/core/audio.h index 1887ec4e..4dcd03fb 100644 --- a/include/core/audio.h +++ b/include/core/audio.h @@ -39,13 +39,6 @@ public: virtual bool is_playing() = 0; - /** - * Seek to a specific position in the current track. - * - * @param offset Position from the beginning of the track where we will seek to, in nanoseconds. - */ - virtual void seek_to(int64_t offset) = 0; - /** * Return the current position of the playback. * @@ -73,13 +66,6 @@ public: namespace audio { - /** - * Seek to a specific point in the track. - * - * @param pos Offset (in nanoseconds) from the beginning of the track. - */ - void seek_to(int64_t); - /** * Stop playback and seek to the beginning of the track. */ @@ -150,5 +136,8 @@ bool audio_play(); /* Called to pause playback. */ bool audio_pause(); +/* Called to seek playback to a specific offset, in nanoseconds. */ +bool audio_seek(gint64); + GstElement *audio_get_player(); #endif /* OCARINA_CORE_AUDIO_H */ diff --git a/tests/core/audio.cpp b/tests/core/audio.cpp index afeb5b42..bd1bb5be 100644 --- a/tests/core/audio.cpp +++ b/tests/core/audio.cpp @@ -14,6 +14,19 @@ struct track *TRACK_NULL = NULL; 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); + + return ret; +} + class TestDriver : public AudioDriver { public: @@ -27,7 +40,6 @@ public: bool is_playing() { return playing; } - void seek_to(int64_t offset) { cur_pos = offset; } int64_t position() { return cur_pos; } int64_t duration() { return cur_duration; } } driver; @@ -61,6 +73,7 @@ static void test_init() test_equal(audio_load(NULL), false); test_equal(audio_play(), false); test_equal(audio_pause(), false); + test_equal(audio_seek(7), false); test_equal(audio :: current_track(), NULL); test_equal(load_count, 0); @@ -92,6 +105,9 @@ static void test_playback() test_equal(audio_pause(), true); test_equal(audio_pause(), false); + + test_equal(test_audio_seek(5 * GST_SECOND), true); + test_equal(test_audio_seek(0), true); } static void test_deinit() @@ -119,7 +135,7 @@ void test_pre_init() test_equal(driver->playing, true); driver->playing = false; - audio :: seek_to(4242); + audio_seek(4242); test_equal(driver->position(), (long)0); driver->cur_pos = 4242; @@ -162,7 +178,8 @@ void test_playback_controls() driver->playing = audio_pause(); test_equal(driver->playing, false); - audio :: seek_to(4242); + if (audio_seek(4242)) + driver->cur_pos = 4242; test_equal(driver->cur_pos, (long)4242); test_equal(audio :: position(), (long)4242); @@ -171,7 +188,7 @@ void test_playback_controls() test_equal(driver->playing, false); test_equal(driver->cur_pos, (long)0); - audio :: seek_to(4242); + audio_seek(4242); driver->cur_duration = 424242; test_equal(audio :: position(), (long)4242); test_equal(audio :: duration(), (long)424242); @@ -197,12 +214,12 @@ void test_track_controls() test_not_equal(audio :: current_track(), track); track = track_get(0); - audio :: seek_to(4242); + audio_seek(4242); audio_load(track); test_equal(driver->is_playing(), true); test_equal(audio :: position(), (long)0); - audio :: seek_to(4242); + audio_seek(4242); audio_load(track); test_equal(driver->is_playing(), true); test_equal(audio :: position(), (long)4242);