some more thread tests
[platform/upstream/gstreamer.git] / testsuite / threads / threadd.c
1 #include <gst/gst.h>
2
3 /* threadc.c
4  * this tests if we can make a GstThread, with enough cothreads to stress it
5  */
6
7 #define MAX_IDENTITIES 29
8 #define RUNS_PER_IDENTITY 5
9
10 gboolean running = FALSE;
11 gboolean done = FALSE;
12
13 static void
14 construct_pipeline (GstElement *pipeline, gint identities) 
15 {
16   GstElement *src, *sink, *identity;
17   GstElement *from;
18   int i;
19
20   src      = gst_element_factory_make ("fakesrc",  NULL);
21   sink     = gst_element_factory_make ("fakesink", NULL);
22   g_assert (src);
23   g_assert (sink);
24   gst_bin_add_many (GST_BIN (pipeline), src, sink, NULL);
25   from = src;
26
27   for (i = 0; i < identities; ++i)
28   {
29     identity = gst_element_factory_make ("identity", NULL);
30     g_assert (identity);
31     gst_bin_add (GST_BIN (pipeline), identity);
32     gst_element_connect (from, identity);
33     from = identity;
34   }
35   gst_element_connect (identity, sink);
36
37   g_object_set (G_OBJECT (src), "num_buffers", 10, "sizetype", 3, NULL);
38 }
39
40 void
41 state_changed (GstElement *el, gint arg1, gint arg2, gpointer user_data)
42 {
43   GstElementState state = gst_element_get_state (el);
44   
45   g_print ("element %s has changed state to %s\n", 
46            GST_ELEMENT_NAME (el), 
47            gst_element_state_get_name (state));
48   if (state == GST_STATE_PLAYING) running = TRUE;
49   /* if we move from PLAYING to PAUSED, we're done */
50   if (state == GST_STATE_PAUSED && running) 
51     done = TRUE;
52 }
53
54 int
55 main (gint argc, gchar *argv[])
56 {
57   int runs = MAX_IDENTITIES * RUNS_PER_IDENTITY;
58   int i;
59   gulong id;
60   GstElement *thread;
61   
62   gst_init (&argc, &argv);
63
64   for (i = 0; i < runs; ++i)
65   {
66     thread = gst_thread_new ("main_thread");
67     g_assert (thread);
68
69     /* connect state change signal */
70     id = g_signal_connect (G_OBJECT (thread), "state_change", 
71                            G_CALLBACK (state_changed), NULL);
72     construct_pipeline (thread, i / RUNS_PER_IDENTITY + 1);
73
74     g_print ("Setting thread to play with %d identities\n", 
75              i / RUNS_PER_IDENTITY + 1);
76     done = FALSE;
77     gst_element_set_state (thread, GST_STATE_PLAYING);
78
79     g_print ("Waiting for thread PLAYING->PAUSED\n");
80     while (!done) /* do nothing */;
81     running = FALSE;
82     g_print ("Coming out of the main GStreamer loop\n");
83     g_signal_handler_disconnect (G_OBJECT (thread), id);
84     gst_element_set_state (thread, GST_STATE_NULL);
85     g_print ("Unreffing thread\n");
86     g_object_unref (G_OBJECT (thread));
87   }
88
89   return 0;
90 }
91