79e6213213aba75c24ef124c7aefb7a19730ee32
[platform/upstream/gstreamer.git] / testsuite / threads / threade.c
1 #include <gst/gst.h>
2
3 /* threadc.c
4  * this tests if we can make a GstBin and iterate it inside a GThread
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 = NULL;
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_link (from, identity);
33     from = identity;
34   }
35   gst_element_link (identity, sink);
36
37   g_object_set (G_OBJECT (src), "num_buffers", 10, "sizetype", 3, NULL);
38 }
39
40 static void
41 iterator (GstElement *bin)
42 {
43   gst_element_set_state (bin, GST_STATE_PLAYING);
44   while (gst_bin_iterate (GST_BIN (bin))) g_print ("+");
45   gst_element_set_state (bin, GST_STATE_NULL);
46   g_print ("\n");
47   done = TRUE;
48 }
49
50 int
51 main (gint argc, gchar *argv[])
52 {
53   int runs = MAX_IDENTITIES * RUNS_PER_IDENTITY;
54   int i;
55   GstElement *pipeline;
56
57   g_thread_init (NULL);
58   gst_init (&argc, &argv);
59
60   for (i = 0; i < runs; ++i)
61   {
62     pipeline = gst_pipeline_new ("main_pipeline");
63     g_assert (pipeline);
64
65     /* connect state change signal */
66     construct_pipeline (pipeline, i / RUNS_PER_IDENTITY + 1);
67
68     done = FALSE;
69     g_thread_create ((GThreadFunc) iterator, pipeline, FALSE, NULL);
70     g_print ("Created GThread\n");
71
72     g_print ("Waiting for thread PLAYING->PAUSED\n");
73     while (!done) /* do nothing */;
74     running = FALSE;
75     g_print ("Unreffing pipeline\n");
76     g_object_unref (G_OBJECT (pipeline));
77   }
78
79   return 0;
80 }
81