First THREADED backport attempt, focusing on adding locks and making sure the API...
[platform/upstream/gstreamer.git] / tests / threadstate / threadstate2.c
1 #include <stdlib.h>
2 #include <gst/gst.h>
3
4 /* this pipeline is:
5  * { filesrc ! mad ! osssink }
6  */
7
8 /* eos will be called when the src element has an end of stream */
9 void
10 eos (GstElement * element, gpointer data)
11 {
12   GstThread *thread = GST_THREAD (data);
13
14   g_print ("have eos, quitting\n");
15
16   /* stop the bin */
17   gst_element_set_state (GST_ELEMENT (thread), GST_STATE_NULL);
18 }
19
20 int
21 main (int argc, char *argv[])
22 {
23   GstElement *filesrc, *osssink;
24   GstElement *thread;
25   GstElement *mad;
26   gint x;
27
28   gst_init (&argc, &argv);
29
30   if (argc != 2) {
31     g_print ("usage: %s <filename>\n", argv[0]);
32     exit (-1);
33   }
34
35   /* create a new thread to hold the elements */
36   thread = gst_thread_new ("thread");
37   g_assert (thread != NULL);
38
39   /* create a disk reader */
40   filesrc = gst_element_factory_make ("filesrc", "disk_source");
41   g_assert (filesrc != NULL);
42   g_object_set (G_OBJECT (filesrc), "location", argv[1], NULL);
43   g_signal_connect (G_OBJECT (filesrc), "eos", G_CALLBACK (eos), thread);
44
45   /* and an audio sink */
46   osssink = gst_element_factory_make ("osssink", "play_audio");
47   g_assert (osssink != NULL);
48
49   /* did i mention that this is an mp3 player? */
50   mad = gst_element_factory_make ("mad", "mp3_decoder");
51   g_assert (mad != NULL);
52
53   gst_bin_add_many (GST_BIN (thread), filesrc, mad, osssink, NULL);
54   gst_element_link_many (filesrc, mad, osssink, NULL);
55
56   for (x = 0; x < 10; x++) {
57     g_print ("playing %d\n", x);
58     gst_element_set_state (GST_ELEMENT (thread), GST_STATE_PLAYING);
59     g_usleep (G_USEC_PER_SEC * 2);
60
61     g_print ("pausing %d\n", x);
62     gst_element_set_state (GST_ELEMENT (thread), GST_STATE_PAUSED);
63     g_usleep (G_USEC_PER_SEC * 2);
64   }
65
66   exit (0);
67 }