core/audio: Listen for GstStateChanged messages

And use these to trigger the state changed callback.  Additionally, this
callback can now be used by tests to determine when we're done seeking.

Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
Anna Schumaker 2017-04-05 16:52:23 -04:00
parent c03530f318
commit fc6e3ff464
4 changed files with 60 additions and 33 deletions

View File

@ -26,16 +26,9 @@ static struct audio_callbacks *audio_cb = NULL;
static bool __audio_change_state(GstState state)
{
GstStateChangeReturn ret = GST_STATE_CHANGE_FAILURE;
if (audio_cur_state() != state)
ret = gst_element_set_state(audio_pipeline, state);
if (ret == GST_STATE_CHANGE_FAILURE)
if (audio_cur_state() == state)
return false;
if (audio_cb)
audio_cb->audio_cb_state_change(state);
return true;
return gst_element_set_state(audio_pipeline, state) != GST_STATE_CHANGE_FAILURE;
}
static void __audio_gst_load(struct track *track, GstState state)
@ -100,6 +93,9 @@ static gboolean __audio_message(GstBus *bus, GstMessage *message, gpointer data)
break;
case GST_MESSAGE_EOS:
audio_eos();
break;
case GST_MESSAGE_STATE_CHANGED:
audio_state_changed(message);
default:
break;
}
@ -276,6 +272,21 @@ struct track *audio_prev()
return __audio_do_load(playlist_prev(), GST_STATE_PLAYING);
}
void audio_state_changed(GstMessage *message)
{
GstElement *src = GST_ELEMENT(GST_MESSAGE_SRC(message));
GstState old, state, next;
if (!audio_cb || src != audio_pipeline)
return;
gst_message_parse_state_changed(message, &old, &state, &next);
if (next != GST_STATE_VOID_PENDING)
return;
if (state == GST_STATE_PLAYING || state == GST_STATE_PAUSED)
audio_cb->audio_cb_state_change(state);
}
struct track *audio_eos()
{
/* Mark current track as played */

View File

@ -72,6 +72,9 @@ struct track *audio_next();
/* Called to load the previous track. */
struct track *audio_prev();
/* Called when the state of the pipeline has changed. */
void audio_state_changed(GstMessage *);
/* Called when playback has reached the end-of-stream position. */
struct track *audio_eos();

View File

@ -3,20 +3,23 @@
*/
#include <core/core.h>
#include <tests/test.h>
#include <tests/loop.h>
static unsigned int load_count = 0;
static unsigned int state_count = 0;
static int pause_count = 0;
static bool test_audio_seek(gint64 pos)
static unsigned int test_wait_state(void)
{
bool ret = audio_seek(pos);
GstState state = audio_cur_state();
GstBus *bus = gst_pipeline_get_bus(GST_PIPELINE(test_audio_pipeline()));
while (state != GST_STATE_PAUSED && state != GST_STATE_PLAYING)
state = audio_cur_state();
return ret;
g_usleep(G_USEC_PER_SEC / 15);
while (gst_bus_have_pending(bus))
test_main_loop();
gst_object_unref(bus);
return state_count;
}
static void test_send_error()
@ -53,6 +56,7 @@ static void test_init()
g_assert_null(audio_next());
core_init(NULL, NULL, NULL, &test_audio_cb, IDLE_SYNC);
test_loop_init();
g_assert_false(audio_load(NULL));
g_assert_null(audio_next());
@ -87,8 +91,8 @@ static void test_playback()
else
g_assert_false(audio_load(tracks[i]));
g_assert_cmpuint(playlist_size(playlist_lookup(PL_SYSTEM, "History")), ==, 1);
g_assert_cmpuint(load_count, ==, 1);
g_assert_cmpuint(state_count, ==, 1);
g_assert_cmpuint(load_count, ==, 1);
g_assert_cmpuint(test_wait_state(), ==, 1);
g_assert_cmpuint(audio_cur_state(), ==, GST_STATE_PLAYING);
g_assert(audio_cur_track() == tracks[0]);
g_assert_cmpuint(audio_duration(), ==, tracks[0]->tr_length * GST_SECOND);
@ -103,19 +107,21 @@ static void test_playback()
g_assert_true(audio_pause());
g_assert_false(audio_pause());
g_assert_cmpuint(state_count, ==, 2);
g_assert_cmpuint(test_wait_state(), ==, 2);
g_assert_cmpuint(audio_cur_state(), ==, GST_STATE_PAUSED);
g_assert_true(test_audio_seek(5 * GST_SECOND));
g_assert_cmpuint(audio_position(), ==, 5 * GST_SECOND);
g_assert_true(test_audio_seek(42 * GST_SECOND));
g_assert_cmpuint(audio_position(), ==, 42 * GST_SECOND);
g_assert_true(audio_seek(5 * GST_SECOND));
g_assert_cmpuint(test_wait_state(), ==, 3);
g_assert_cmpuint(audio_position(), ==, 5 * GST_SECOND);
g_assert_true(audio_seek(42 * GST_SECOND));
g_assert_cmpuint(test_wait_state(), ==, 4);
g_assert_cmpuint(audio_position(), ==, 42 * GST_SECOND);
g_assert_true(audio_play());
g_assert_false(audio_play());
g_assert_cmpuint(state_count, ==, 3);
g_assert_cmpuint(test_wait_state(), ==, 5);
g_assert_true(audio_pause());
g_assert_cmpuint(state_count, ==, 4);
g_assert_cmpuint(test_wait_state(), ==, 6);
g_assert_cmpuint(audio_cur_state(), ==, GST_STATE_PAUSED);
/* Check duration again now that track is fully loaded. */
@ -143,7 +149,7 @@ static void test_next()
g_assert_cmpuint(audio_cur_state(), ==, GST_STATE_PLAYING);
g_assert(audio_cur_track() == track_get(i));
}
g_assert_cmpuint(state_count, ==, 3);
g_assert_cmpuint(test_wait_state(), ==, 3);
g_assert_cmpuint(playlist_size(playlist_lookup(PL_SYSTEM, "Queued Tracks")), ==, 0);
/* Tracks should now be picked from the collection. */
@ -157,7 +163,7 @@ static void test_next()
g_assert_cmpuint(audio_cur_state(), ==, GST_STATE_PLAYING);
g_assert_cmpuint(audio_cur_track()->tr_track, ==, i);
}
g_assert_cmpuint(state_count, ==, 6);
g_assert_cmpuint(test_wait_state(), ==, 6);
}
static void test_prev()
@ -170,32 +176,32 @@ static void test_prev()
g_assert(playlist_at(history, 0) == track);
g_assert_cmpuint(audio_cur_state(), ==, GST_STATE_PLAYING);
g_assert_cmpuint(audio_cur_track()->tr_track, ==, 2);
g_assert_cmpuint(state_count, ==, 1);
g_assert_cmpuint(test_wait_state(), ==, 1);
g_assert_cmpuint(audio_prev()->tr_track, ==, 1);
g_assert(playlist_at(history, 0) == track);
g_assert_cmpuint(audio_cur_state(), ==, GST_STATE_PLAYING);
g_assert_cmpuint(audio_cur_track()->tr_track, ==, 1);
g_assert_cmpuint(state_count, ==, 2);
g_assert_cmpuint(test_wait_state(), ==, 2);
g_assert_true(audio_pause());
g_assert_cmpuint(audio_prev()->tr_track, ==, track_get(0)->tr_track);
g_assert(playlist_at(history, 0) == track);
g_assert_cmpuint(audio_cur_state(), ==, GST_STATE_PLAYING);
g_assert_cmpuint(audio_cur_track()->tr_track, ==, track_get(0)->tr_track);
g_assert_cmpuint(state_count, ==, 4);
g_assert_cmpuint(test_wait_state(), ==, 4);
g_assert_cmpuint(audio_prev()->tr_track, ==, track_get(1)->tr_track);
g_assert(playlist_at(history, 0) == track);
g_assert_cmpuint(audio_cur_state(), ==, GST_STATE_PLAYING);
g_assert_cmpuint(audio_cur_track()->tr_track, ==, track_get(1)->tr_track);
g_assert_cmpuint(state_count, ==, 5);
g_assert_cmpuint(test_wait_state(), ==, 5);
g_assert_cmpuint(audio_prev()->tr_track, ==, track_get(2)->tr_track);
g_assert(playlist_at(history, 0) == track);
g_assert_cmpuint(audio_cur_state(), ==, GST_STATE_PLAYING);
g_assert_cmpuint(audio_cur_track()->tr_track, ==, track_get(2)->tr_track);
g_assert_cmpuint(state_count, ==, 6);
g_assert_cmpuint(test_wait_state(), ==, 6);
}
void test_autopause()
@ -220,13 +226,13 @@ void test_autopause()
g_assert_cmpuint(audio_cur_state(), ==, GST_STATE_PLAYING);
g_assert(playlist_at(history, 0) == audio_cur_track());
}
g_assert_cmpuint(state_count, ==, 5);
g_assert_cmpuint(test_wait_state(), ==, 5);
audio_eos();
while (idle_run_task()) {}
g_assert_cmpint(pause_count, ==, -1);
g_assert_cmpuint(audio_cur_state(), ==, GST_STATE_PAUSED);
g_assert_cmpuint(state_count, ==, 6);
g_assert_cmpuint(test_wait_state(), ==, 6);
test_send_error();
g_assert_cmpuint(audio_cur_state(), ==, GST_STATE_PAUSED);
@ -235,6 +241,7 @@ void test_autopause()
static void test_deinit()
{
core_deinit();
test_loop_deinit();
g_assert_null(audio_cur_track());
g_assert_null(test_audio_pipeline());
}

View File

@ -12,6 +12,7 @@
#include <gui/treeview.h>
#include <gui/window.h>
#include <tests/test.h>
#include <tests/loop.h>
static void test_audio_init()
{
@ -45,6 +46,7 @@ static void test_audio_load()
g_assert_cmpstr(gtk_label_get_text(gui_duration()), ==, length);
g_assert_cmpstr(gtk_label_get_text(gui_position()), ==, "0:00");
test_main_loop();
g_assert_false(gtk_widget_is_visible(GTK_WIDGET(gui_play_button())));
g_assert_true( gtk_widget_is_visible(GTK_WIDGET(gui_pause_button())));
@ -58,11 +60,13 @@ static void test_audio_buttons()
audio_load(track_get(0));
gtk_button_clicked(gui_pause_button());
test_main_loop();
g_assert_cmpuint(audio_cur_state(), ==, GST_STATE_PAUSED);
g_assert_true( gtk_widget_is_visible(GTK_WIDGET(gui_play_button())));
g_assert_false(gtk_widget_is_visible(GTK_WIDGET(gui_pause_button())));
gtk_button_clicked(gui_play_button());
test_main_loop();
g_assert_cmpuint(audio_cur_state(), ==, GST_STATE_PLAYING);
g_assert_false(gtk_widget_is_visible(GTK_WIDGET(gui_play_button())));
g_assert_true( gtk_widget_is_visible(GTK_WIDGET(gui_pause_button())));
@ -124,6 +128,7 @@ int main(int argc, char **argv)
gui_sidebar_init();
gui_playlist_init();
gui_audio_init();
test_loop_init();
gui_pl_library_add("tests/Music/Hyrule Symphony");
while (idle_run_task()) {};
@ -135,6 +140,7 @@ int main(int argc, char **argv)
ret = g_test_run();
core_deinit();
test_loop_deinit();
gui_audio_deinit();
gui_filter_deinit();
gui_treeview_deinit();