284e66363c71aa48499d91bc4c693a151ee0506f
[platform/upstream/gstreamer.git] / testsuite / threads / threadi.c
1 /*
2  * Test three ways of going non-lineairly to PLAYING. Both tests have a
3  * thread containing a fakesrc/sink.
4  *
5  * Test1 tests by adding fakesrc/fakesink, setting fakesrc to PLAYING
6  * (which should increment the container state) and then synchronizing
7  * state and see if the bin iterates. This reflects bug #123775.
8  *
9  * Test2 does the same, but emits EOS directly. This will (in case of
10  * race conditions) sometimes lead to a state-change before the previous
11  * one succeeded. This bug is not fixed yet (999998).
12  *
13  * Test3 tests by adding fakesrc, putting thread to PLAYING, adding
14  * fakesink, syncing state and see if it iterates. The group is sometimes
15  * activated before fakesink is added to the bin, which is a bug in opt
16  * and a race in core that is not fixed yet (999999).
17  */
18
19 #include <gst/gst.h>
20
21 static GstElement *pipeline, *fakesrc, *fakesink;
22
23 static gboolean
24 cb_timeout (gpointer data)
25 {
26   g_assert_not_reached ();
27
28   return FALSE;
29 }
30
31 static gboolean
32 cb_quit (gpointer data)
33 {
34   gst_main_quit ();
35
36   g_print ("Quit mainloop\n");
37
38   /* once */
39   return FALSE;
40 }
41
42 #if TESTNUM != 123775
43 static void
44 cb_eos (gpointer data)
45 {
46   g_print ("Received EOS\n");
47
48   g_idle_add ((GSourceFunc) cb_quit, NULL);
49 }
50 #else
51 static void
52 cb_data (gpointer data)
53 {
54   static gboolean first = TRUE;
55
56   g_print ("Received data\n");
57
58   if (first) {
59     first = FALSE;
60     g_idle_add ((GSourceFunc) cb_quit, NULL);
61   }
62 }
63 #endif
64
65 static void
66 cb_state (GstElement * element, GstElementState old_state,
67     GstElementState new_state, gpointer data)
68 {
69   g_print ("Changed state from %d to %d\n", old_state, new_state);
70 }
71
72 static gboolean
73 cb_play (gpointer data)
74 {
75   GstElementStateReturn res;
76
77 #if TESTNUM != 999999
78   g_print ("Setting state on fakesrc\n");
79   gst_element_set_state (fakesrc, GST_STATE_PLAYING);
80   g_print ("Done\n");
81 #else
82   g_print ("Setting state on pipeline w/o fakesink\n");
83   gst_element_set_state (pipeline, GST_STATE_PLAYING);
84   g_print ("Adding fakesink\n");
85   gst_bin_add (GST_BIN (pipeline), fakesink);
86   g_print ("Done\n");
87 #endif
88   g_print ("Syncing state in pipeline\n");
89   res = gst_bin_sync_children_state (GST_BIN (data));
90   g_assert (res == GST_STATE_SUCCESS);
91   g_print ("Set to playing correctly: %d\n", GST_STATE (pipeline));
92
93   /* once */
94   return FALSE;
95 }
96
97 gint
98 main (gint argc, gchar * argv[])
99 {
100   gint id;
101
102   gst_init (&argc, &argv);
103
104   g_print ("Will do a test to see if bug %d is fixed\n", TESTNUM);
105
106   pipeline = gst_thread_new ("p");
107   g_signal_connect (pipeline, "state-change", G_CALLBACK (cb_state), NULL);
108   fakesrc = gst_element_factory_make ("fakesrc", "src");
109   fakesink = gst_element_factory_make ("fakesink", "sink");
110 #if TESTNUM != 123775
111   g_object_set (G_OBJECT (fakesrc), "num-buffers", 0, NULL);
112   g_signal_connect (pipeline, "eos", G_CALLBACK (cb_eos), NULL);
113 #else
114   g_object_set (G_OBJECT (fakesink), "signal-handoffs", TRUE, NULL);
115   g_signal_connect (fakesink, "handoff", G_CALLBACK (cb_data), NULL);
116 #endif
117
118 #if TESTNUM != 999999
119   gst_bin_add_many (GST_BIN (pipeline), fakesrc, fakesink, NULL);
120 #else
121   gst_bin_add (GST_BIN (pipeline), fakesrc);
122 #endif
123
124   gst_element_link (fakesrc, fakesink);
125   g_idle_add ((GSourceFunc) cb_play, pipeline);
126
127   /* give 5 seconds */
128   id = g_timeout_add (5000, (GSourceFunc) cb_timeout, NULL);
129   g_print ("Enter mainloop\n");
130   gst_main ();
131   g_source_remove (id);
132
133   gst_element_set_state (pipeline, GST_STATE_NULL);
134   gst_object_unref (GST_OBJECT (pipeline));
135
136   g_print ("Done with test to show bug %d, fixed correctly\n", TESTNUM);
137
138   return 0;
139 }