/* GStreamer * Copyright (C) 2005 Thomas Vander Stichele * * gstpipeline.c: Unit test for GstPipeline * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public * License along with this library; if not, write to the * Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA. */ #include /* an empty pipeline can go to PLAYING in one go */ GST_START_TEST (test_async_state_change_empty) { GstPipeline *pipeline; pipeline = GST_PIPELINE (gst_pipeline_new (NULL)); fail_unless (pipeline != NULL, "Could not create pipeline"); fail_unless_equals_int (gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_PLAYING), GST_STATE_CHANGE_SUCCESS); gst_object_unref (pipeline); } GST_END_TEST; GST_START_TEST (test_async_state_change_fake_ready) { GstPipeline *pipeline; GstElement *src, *sink; pipeline = GST_PIPELINE (gst_pipeline_new (NULL)); fail_unless (pipeline != NULL, "Could not create pipeline"); src = gst_element_factory_make ("fakesrc", NULL); sink = gst_element_factory_make ("fakesink", NULL); gst_bin_add_many (GST_BIN (pipeline), src, sink, NULL); gst_element_link (src, sink); fail_unless_equals_int (gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_READY), GST_STATE_CHANGE_SUCCESS); gst_object_unref (pipeline); } GST_END_TEST; GST_START_TEST (test_async_state_change_fake) { GstPipeline *pipeline; GstElement *src, *sink; GstBus *bus; gboolean done = FALSE; pipeline = GST_PIPELINE (gst_pipeline_new (NULL)); fail_unless (pipeline != NULL, "Could not create pipeline"); src = gst_element_factory_make ("fakesrc", NULL); sink = gst_element_factory_make ("fakesink", NULL); gst_bin_add_many (GST_BIN (pipeline), src, sink, NULL); gst_element_link (src, sink); bus = gst_pipeline_get_bus (pipeline); fail_unless_equals_int (gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_PLAYING), GST_STATE_CHANGE_ASYNC); while (!done) { GstMessage *message; GstState old, new, pending; message = gst_bus_poll (bus, GST_MESSAGE_STATE_CHANGED, -1); if (message) { gst_message_parse_state_changed (message, &old, &new, &pending); GST_DEBUG_OBJECT (message->src, "state change from %d to %d", old, new); if (message->src == GST_OBJECT (pipeline) && new == GST_STATE_PLAYING) done = TRUE; gst_message_unref (message); } } fail_unless_equals_int (gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_NULL), GST_STATE_CHANGE_SUCCESS); gst_object_unref (bus); gst_object_unref (pipeline); } GST_END_TEST; GST_START_TEST (test_get_bus) { GstPipeline *pipeline; GstBus *bus; pipeline = GST_PIPELINE (gst_pipeline_new (NULL)); fail_unless (pipeline != NULL, "Could not create pipeline"); ASSERT_OBJECT_REFCOUNT (pipeline, "pipeline", 1); bus = gst_pipeline_get_bus (pipeline); ASSERT_OBJECT_REFCOUNT (pipeline, "pipeline after get_bus", 1); ASSERT_OBJECT_REFCOUNT (bus, "bus", 2); gst_object_unref (pipeline); ASSERT_OBJECT_REFCOUNT (bus, "bus after unref pipeline", 1); gst_object_unref (bus); } GST_END_TEST; GMainLoop *loop = NULL; gboolean message_received (GstBus * bus, GstMessage * message, gpointer data) { GstElement *pipeline = GST_ELEMENT (data); GstMessageType type = message->type; GST_DEBUG ("message received"); switch (type) { case GST_MESSAGE_STATE_CHANGED: { GstState old, new, pending; GST_DEBUG ("state change message received"); gst_message_parse_state_changed (message, &old, &new, &pending); GST_DEBUG ("new state %d", new); if (message->src == GST_OBJECT (pipeline) && new == GST_STATE_PLAYING) { GST_DEBUG ("quitting main loop"); g_main_loop_quit (loop); } } break; case GST_MESSAGE_ERROR: { g_print ("error\n"); } break; default: break; } return TRUE; } GST_START_TEST (test_bus) { GstElement *pipeline; GstElement *src, *sink; GstBus *bus; guint id; GstState current; GstStateChangeReturn ret; pipeline = gst_pipeline_new (NULL); fail_unless (pipeline != NULL, "Could not create pipeline"); ASSERT_OBJECT_REFCOUNT (pipeline, "pipeline", 1); src = gst_element_factory_make ("fakesrc", NULL); fail_unless (src != NULL); sink = gst_element_factory_make ("fakesink", NULL); fail_unless (sink != NULL); gst_bin_add_many (GST_BIN (pipeline), src, sink, NULL); fail_unless (gst_element_link (src, sink)); bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline)); ASSERT_OBJECT_REFCOUNT (pipeline, "pipeline after get_bus", 1); ASSERT_OBJECT_REFCOUNT (bus, "bus", 2); id = gst_bus_add_watch (bus, message_received, pipeline); ASSERT_OBJECT_REFCOUNT (pipeline, "pipeline after add_watch", 1); ASSERT_OBJECT_REFCOUNT (bus, "bus after add_watch", 3); ret = gst_element_set_state (pipeline, GST_STATE_PLAYING); fail_unless (ret == GST_STATE_CHANGE_ASYNC); loop = g_main_loop_new (NULL, FALSE); GST_DEBUG ("going into main loop"); g_main_loop_run (loop); GST_DEBUG ("left main loop"); /* PLAYING now */ ASSERT_OBJECT_REFCOUNT_BETWEEN (pipeline, "pipeline after gone to playing", 1, 3); /* cleanup */ GST_DEBUG ("cleanup"); ret = gst_element_set_state (pipeline, GST_STATE_NULL); fail_unless (ret == GST_STATE_CHANGE_SUCCESS); ret = gst_element_get_state (pipeline, ¤t, NULL, GST_CLOCK_TIME_NONE); fail_unless (ret == GST_STATE_CHANGE_SUCCESS); fail_unless (current == GST_STATE_NULL, "state is not NULL but %d", current); ASSERT_OBJECT_REFCOUNT (pipeline, "pipeline at start of cleanup", 1); ASSERT_OBJECT_REFCOUNT (bus, "bus at start of cleanup", 3); fail_unless (g_source_remove (id)); ASSERT_OBJECT_REFCOUNT (bus, "bus after removing source", 2); GST_DEBUG ("unreffing pipeline"); gst_object_unref (pipeline); ASSERT_OBJECT_REFCOUNT (bus, "bus after unref pipeline", 1); gst_object_unref (bus); } GST_END_TEST; static gboolean sink_pad_probe (GstPad * pad, GstBuffer * buffer, GstClockTime * first_timestamp) { gint memory_barrier = 1; volatile gint foo; fail_if (GST_BUFFER_TIMESTAMP (buffer) == GST_CLOCK_TIME_NONE, "testing if buffer timestamps are right, but got CLOCK_TIME_NONE"); if (*first_timestamp == GST_CLOCK_TIME_NONE) *first_timestamp = GST_BUFFER_TIMESTAMP (buffer); /* make sure the other thread sees the update */ foo = g_atomic_int_get (&memory_barrier); return TRUE; } GST_START_TEST (test_base_time) { GstElement *pipeline, *fakesrc, *fakesink; GstPad *sink; GstClockTime observed, lower, upper, base; GstClock *clock; pipeline = gst_element_factory_make ("pipeline", "pipeline"); fakesrc = gst_element_factory_make ("fakesrc", "fakesrc"); fakesink = gst_element_factory_make ("fakesink", "fakesink"); fail_unless (pipeline && fakesrc && fakesink, "couldn't make elements"); g_object_set (fakesrc, "is-live", (gboolean) TRUE, NULL); gst_bin_add_many (GST_BIN (pipeline), fakesrc, fakesink, NULL); gst_element_link (fakesrc, fakesink); sink = gst_element_get_pad (fakesink, "sink"); gst_pad_add_buffer_probe (sink, G_CALLBACK (sink_pad_probe), &observed); observed = GST_CLOCK_TIME_NONE; fail_unless (gst_element_set_state (pipeline, GST_STATE_PAUSED) == GST_STATE_CHANGE_NO_PREROLL, "expected no-preroll from live pipeline"); clock = gst_system_clock_obtain (); fail_unless (clock && GST_IS_CLOCK (clock), "i want a clock dammit"); gst_pipeline_use_clock (GST_PIPELINE (pipeline), clock); lower = gst_clock_get_time (clock); gst_element_set_state (pipeline, GST_STATE_PLAYING); fail_unless (gst_element_get_state (pipeline, NULL, NULL, GST_CLOCK_TIME_NONE) == GST_STATE_CHANGE_SUCCESS, "failed state change"); /* now something a little more than lower was distributed as the base time, * and the buffer was timestamped at the base time or a little bit afterwards */ base = gst_element_get_base_time (pipeline); upper = gst_clock_get_time (clock); /* make sure we don't have other threads running, makes errors nicer */ gst_element_set_state (pipeline, GST_STATE_NULL); fail_if (observed == GST_CLOCK_TIME_NONE, "no timestamp recorded"); fail_unless (base >= lower, "early base time: %" GST_TIME_FORMAT " < %" GST_TIME_FORMAT, GST_TIME_ARGS (base), GST_TIME_ARGS (lower)); fail_unless (upper >= base, "bogus base time: %" GST_TIME_FORMAT " > %" GST_TIME_FORMAT, GST_TIME_ARGS (base), GST_TIME_ARGS (upper)); fail_unless (observed + base >= lower, "early timestamp: %" GST_TIME_FORMAT " < %" GST_TIME_FORMAT, GST_TIME_ARGS (observed), GST_TIME_ARGS (lower)); fail_unless (observed + base <= upper, "late timestamp: %" GST_TIME_FORMAT " > %" GST_TIME_FORMAT, GST_TIME_ARGS (observed), GST_TIME_ARGS (upper)); gst_object_unref (sink); gst_object_unref (clock); gst_object_unref (pipeline); } GST_END_TEST; Suite * gst_pipeline_suite (void) { Suite *s = suite_create ("GstPipeline"); TCase *tc_chain = tcase_create ("pipeline tests"); tcase_set_timeout (tc_chain, 0); suite_add_tcase (s, tc_chain); tcase_add_test (tc_chain, test_async_state_change_empty); tcase_add_test (tc_chain, test_async_state_change_fake_ready); tcase_add_test (tc_chain, test_async_state_change_fake); tcase_add_test (tc_chain, test_get_bus); tcase_add_test (tc_chain, test_bus); tcase_add_test (tc_chain, test_base_time); return s; } int main (int argc, char **argv) { int nf; Suite *s = gst_pipeline_suite (); SRunner *sr = srunner_create (s); gst_check_init (&argc, &argv); srunner_run_all (sr, CK_NORMAL); nf = srunner_ntests_failed (sr); srunner_free (sr); return nf; }