Added a thread test case.
[platform/upstream/gstreamer.git] / tests / old / testsuite / threads / thread.c
1 #include <gst/gst.h>
2
3 void
4 usage (void)
5 {
6   g_print ("usage: thread <testnum>  \n"
7            "   available testnums:   \n"
8            "          1: stress test state change      \n"
9            "          2: iterate once                  \n"
10            "          3: iterate twice                 \n"
11            "          4: state change while running    \n"
12            "          5: state change in thread context\n");
13 }
14
15 static void
16 construct_pipeline (GstElement *pipeline) 
17 {
18   GstElement *src, *sink, *queue, *thread;
19
20   src    = gst_elementfactory_make ("fakesrc",  "src");
21   sink   = gst_elementfactory_make ("fakesink", "sink");
22   queue  = gst_elementfactory_make ("queue",    "queue");
23   thread = gst_elementfactory_make ("thread",   "thread");
24
25   gst_element_connect (src, "src", queue, "sink");
26   gst_element_connect (queue, "src", sink, "sink");
27
28   gst_bin_add (GST_BIN (pipeline), src);
29   gst_bin_add (GST_BIN (pipeline), queue);
30   gst_bin_add (GST_BIN (pipeline), thread);
31   
32   gst_bin_add (GST_BIN (thread), sink);
33
34   g_object_set (G_OBJECT (src), "num_buffers", 5, NULL);
35 }
36
37 void
38 change_state (GstElement *element, GstBuffer *buf, GstElement *pipeline) 
39 {
40   gst_element_set_state (pipeline, GST_STATE_NULL);
41 }
42
43 gint
44 main (gint argc, gchar *argv[])
45 {
46   GstElement *pipeline;
47   gint testnum;
48   
49   gst_init (&argc, &argv);
50
51   if (argc < 2) {
52     usage();
53     return 0;
54   }
55
56   testnum = atoi (argv[1]);
57   
58   pipeline = gst_pipeline_new ("main_pipeline");
59   construct_pipeline (pipeline);
60
61   if (testnum == 1) {
62     g_print ("stress test state changes...\n");
63
64     gst_element_set_state (pipeline, GST_STATE_NULL);
65     gst_element_set_state (pipeline, GST_STATE_READY);
66     gst_element_set_state (pipeline, GST_STATE_NULL);
67     gst_element_set_state (pipeline, GST_STATE_PAUSED);
68     gst_element_set_state (pipeline, GST_STATE_READY);
69     gst_element_set_state (pipeline, GST_STATE_PAUSED);
70     gst_element_set_state (pipeline, GST_STATE_PLAYING);
71     gst_element_set_state (pipeline, GST_STATE_PAUSED);
72     gst_element_set_state (pipeline, GST_STATE_PLAYING);
73     gst_element_set_state (pipeline, GST_STATE_READY);
74     gst_element_set_state (pipeline, GST_STATE_NULL);
75   }
76
77   if (testnum == 2 || testnum == 3) {
78     gst_element_set_state (pipeline, GST_STATE_PLAYING);
79     g_print ("running ...\n");
80     while (gst_bin_iterate (GST_BIN (pipeline)));
81     gst_element_set_state (pipeline, GST_STATE_NULL);
82   }
83   if (testnum == 3) {
84     gst_element_set_state (pipeline, GST_STATE_PLAYING);
85     g_print ("running2 ...\n");
86     while (gst_bin_iterate (GST_BIN (pipeline)));
87     gst_element_set_state (pipeline, GST_STATE_NULL);
88   }
89   if (testnum == 4) {
90     gint run;
91
92     gst_element_set_state (pipeline, GST_STATE_PLAYING);
93     g_print ("running3 ...\n");
94     for (run = 0; run < 3; run++) {
95       gst_bin_iterate (GST_BIN (pipeline));
96     }
97     gst_element_set_state (pipeline, GST_STATE_NULL);
98   }
99   if (testnum == 5) {
100     GstElement *sink;
101
102     sink = gst_bin_get_by_name (GST_BIN (pipeline), "sink");
103
104     g_signal_connect (G_OBJECT (sink), "handoff", change_state, pipeline);
105     gst_element_set_state (pipeline, GST_STATE_PLAYING);
106     g_print ("running3 ...\n");
107     while (gst_bin_iterate (GST_BIN (pipeline)));
108     gst_element_set_state (pipeline, GST_STATE_NULL);
109   }
110   
111   return 0;
112 }