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