gst-indent run on core
[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     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 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), gst_element_state_get_name (state));
47   if (state == GST_STATE_PLAYING)
48     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   alarm (10);
63
64   gst_init (&argc, &argv);
65
66   for (i = 0; i < runs; ++i) {
67     thread = gst_thread_new ("main_thread");
68     g_assert (thread);
69
70     /* connect state change signal */
71     id = g_signal_connect (G_OBJECT (thread), "state_change",
72         G_CALLBACK (state_changed), NULL);
73     construct_pipeline (thread, i / RUNS_PER_IDENTITY + 1);
74
75     g_print ("Setting thread to play with %d identities\n",
76         i / RUNS_PER_IDENTITY + 1);
77     done = FALSE;
78     if (gst_element_set_state (thread, GST_STATE_PLAYING) == GST_STATE_FAILURE) {
79       g_warning ("failed to go to PLAYING");
80     } else {
81       g_print ("Waiting for thread PLAYING->PAUSED\n");
82       while (!done)             /* do nothing */
83         ;
84     }
85     running = FALSE;
86     g_print ("Coming out of the main GStreamer loop\n");
87     g_signal_handler_disconnect (G_OBJECT (thread), id);
88     gst_element_set_state (thread, GST_STATE_NULL);
89     g_print ("Unreffing thread\n");
90     g_object_unref (G_OBJECT (thread));
91   }
92
93   return 0;
94 }