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 <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2015-12-16 09:18:01 -05:00
parent 0a1a9f1394
commit 2511db3a08
6 changed files with 31 additions and 59 deletions

View File

@ -28,17 +28,10 @@ static void __audio_save()
file_close(&audio_file); 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) static bool __audio_change_state(GstState state)
{ {
GstStateChangeReturn ret = GST_STATE_CHANGE_FAILURE; GstStateChangeReturn ret = GST_STATE_CHANGE_FAILURE;
if (__audio_cur_state() != state) if (audio_cur_state() != state)
ret = gst_element_set_state(audio_player, state); ret = gst_element_set_state(audio_player, state);
return ret != GST_STATE_CHANGE_FAILURE; return ret != GST_STATE_CHANGE_FAILURE;
} }
@ -140,8 +133,7 @@ bool audio_load(struct track *track)
{ {
if (track == audio_track) if (track == audio_track)
return false; return false;
if (__audio_load(track, cur_driver->is_playing() ? GST_STATE_PLAYING : if (__audio_load(track, audio_cur_state())) {
GST_STATE_PAUSED)) {
history_add(audio_track); history_add(audio_track);
return true; return true;
} }
@ -153,6 +145,16 @@ struct track *audio_cur_track()
return audio_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() bool audio_play()
{ {
if (!audio_track) if (!audio_track)
@ -210,15 +212,13 @@ void audio :: next()
struct track *track = tempq_next(); struct track *track = tempq_next();
if (!track) if (!track)
track = queue_next(collection_get_queue()); track = queue_next(collection_get_queue());
if (__audio_load(track, cur_driver->is_playing() ? GST_STATE_PLAYING : if (__audio_load(track, audio_cur_state()))
GST_STATE_PAUSED))
history_add(audio_track); history_add(audio_track);
} }
void audio :: prev() void audio :: prev()
{ {
__audio_load(history_prev(), cur_driver->is_playing() ? __audio_load(history_prev(), audio_cur_state());
GST_STATE_PLAYING : GST_STATE_PAUSED);
} }
void audio :: pause_after(bool enabled, unsigned int n) void audio :: pause_after(bool enabled, unsigned int n)

View File

@ -70,21 +70,7 @@ static void on_load(struct track *track, GstState state)
} }
class GSTDriver : public AudioDriver class GSTDriver : public AudioDriver {} gst_driver;
{
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;
struct audio_ops audio_ops = { struct audio_ops audio_ops = {
on_load, on_load,
@ -122,7 +108,7 @@ static gboolean on_gst_message(GstBus *bus, GstMessage *message, gpointer data)
gst :: play(); gst :: play();
break; break;
case GST_MESSAGE_EOS: case GST_MESSAGE_EOS:
gst_driver->eos(); gst_driver.eos();
o_count->set_value(audio :: pause_count()); o_count->set_value(audio :: pause_count());
o_enabled->set_active(audio :: pause_enabled()); o_enabled->set_active(audio :: pause_enabled());
break; break;
@ -191,7 +177,7 @@ void gst :: next()
void gst :: toggle() void gst :: toggle()
{ {
if (gst_driver->is_playing()) if (audio_cur_state() == GST_STATE_PLAYING)
gst :: pause(); gst :: pause();
else else
gst :: play(); gst :: play();
@ -199,8 +185,6 @@ void gst :: toggle()
void gst :: init_pre() void gst :: init_pre()
{ {
gst_driver = new GSTDriver();
o_next = gui :: get_widget<Gtk::Button>("o_next"); o_next = gui :: get_widget<Gtk::Button>("o_next");
o_pause = gui :: get_widget<Gtk::Button>("o_pause"); o_pause = gui :: get_widget<Gtk::Button>("o_pause");
o_play = gui :: get_widget<Gtk::Button>("o_play"); o_play = gui :: get_widget<Gtk::Button>("o_play");
@ -235,8 +219,3 @@ void gst :: init()
gst_bus_add_watch(gst_bus, on_gst_message, NULL); gst_bus_add_watch(gst_bus, on_gst_message, NULL);
Glib :: signal_timeout().connect(sigc::ptr_fun(on_timeout), 500); Glib :: signal_timeout().connect(sigc::ptr_fun(on_timeout), 500);
} }
void gst :: quit()
{
delete gst_driver;
}

View File

@ -76,7 +76,6 @@ int main(int argc, char **argv)
ocarina_app->run(*window, argc, argv); ocarina_app->run(*window, argc, argv);
core :: deinit(); core :: deinit();
cleanup_tabs(); cleanup_tabs();
gst :: quit();
return 0; return 0;
} }

View File

@ -30,15 +30,6 @@ public:
AudioDriver(); /**< Default AudioDriver constructor. */ AudioDriver(); /**< Default AudioDriver constructor. */
virtual ~AudioDriver(); /**< AudioDriver destructor. */ 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. * Called to handle reaching the end-of-stream.
*/ */
@ -99,6 +90,9 @@ bool audio_load(struct track *);
/* Called to get the current track. */ /* Called to get the current track. */
struct track *audio_cur_track(); struct track *audio_cur_track();
/* Called to get the current playback state. */
GstState audio_cur_state();
/* Called to begin playback. */ /* Called to begin playback. */
bool audio_play(); bool audio_play();

View File

@ -50,7 +50,6 @@ namespace gst
void toggle(); void toggle();
void init_pre(); void init_pre();
void init(); void init();
void quit();
} }

View File

@ -17,13 +17,10 @@ static unsigned int load_count = 0;
static bool test_audio_seek(gint64 pos) static bool test_audio_seek(gint64 pos)
{ {
bool ret = audio_seek(pos); bool ret = audio_seek(pos);
GstState state; GstState state = audio_cur_state();
do {
gst_element_get_state(audio_get_player(), &state, NULL,
GST_CLOCK_TIME_NONE);
} while (state != GST_STATE_PAUSED && state != GST_STATE_PLAYING);
while (state != GST_STATE_PAUSED && state != GST_STATE_PLAYING)
state = audio_cur_state();
return ret; return ret;
} }
@ -35,7 +32,6 @@ public:
TestDriver() : playing(false) {} TestDriver() : playing(false) {}
~TestDriver() {}; ~TestDriver() {};
bool is_playing() { return playing; } bool is_playing() { return playing; }
} driver; } driver;
@ -60,8 +56,8 @@ static struct core_init_data test_init_data = {
static void test_init() static void test_init()
{ {
test_equal(audio_get_player(), NULL); test_equal(audio_get_player(), NULL);
test_equal(audio_cur_track(), NULL); test_equal(audio_cur_track(), NULL);
test_equal(audio_get_player(), NULL); test_equal(audio_cur_state(), GST_STATE_NULL);
core :: init(NULL, NULL, &test_init_data); core :: init(NULL, NULL, &test_init_data);
@ -73,6 +69,7 @@ static void test_init()
test_equal(audio_position(), 0); test_equal(audio_position(), 0);
test_equal(audio_duration(), 0); test_equal(audio_duration(), 0);
test_equal(audio_cur_track(), NULL); test_equal(audio_cur_track(), NULL);
test_equal(audio_cur_state(), GST_STATE_NULL);
test_equal(load_count, 0); test_equal(load_count, 0);
collection_add("tests/Music/Hyrule Symphony"); collection_add("tests/Music/Hyrule Symphony");
@ -87,6 +84,7 @@ static void test_playback()
test_equal(audio_load(track_get(0)), true); test_equal(audio_load(track_get(0)), true);
test_equal(load_count, 1); test_equal(load_count, 1);
test_equal(audio_cur_track(), track_get(0)); 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(queue_size(history_get_queue()), 1);
test_equal(audio_duration(), track_get(0)->tr_length * GST_SECOND); 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(), true);
test_equal(audio_play(), false); test_equal(audio_play(), false);
test_equal(audio_cur_state(), GST_STATE_PLAYING);
test_equal(audio_pause(), true); test_equal(audio_pause(), true);
test_equal(audio_pause(), false); test_equal(audio_pause(), false);
test_equal(audio_cur_state(), GST_STATE_PAUSED);
test_equal(test_audio_seek(5 * GST_SECOND), true); test_equal(test_audio_seek(5 * GST_SECOND), true);
test_equal(audio_position(), 5 * GST_SECOND); test_equal(audio_position(), 5 * GST_SECOND);
test_equal(audio_stop(), true); test_equal(audio_stop(), true);
test_equal(audio_position(), 0); test_equal(audio_position(), 0);
test_equal(audio_cur_state(), GST_STATE_PAUSED);
test_equal(test_audio_seek(42 * GST_SECOND), true); test_equal(test_audio_seek(42 * GST_SECOND), true);
test_equal(audio_position(), 42 * GST_SECOND); test_equal(audio_position(), 42 * GST_SECOND);
test_equal(audio_play(), true); test_equal(audio_play(), true);
test_equal(audio_stop(), true); test_equal(audio_stop(), true);
test_equal(audio_pause(), false); test_equal(audio_cur_state(), GST_STATE_PAUSED);
test_equal(audio_position(), 0); test_equal(audio_position(), 0);
/* Check duration again now that track is fully loaded. */ /* Check duration again now that track is fully loaded. */