whitespace fixes
[platform/upstream/gstreamer.git] / examples / cutter / cutter.c
1 /*
2  * cutter.c - cut audio into pieces based on silence  - thomas@apestaart.org
3  *
4  * construct a simple pipeline osssrc ! cutter ! filesink
5  * pause when necessary, change output
6  *
7  * Latest change :      03/06/2001
8  *
9  * Version :            0.3
10  */
11
12 #include <stdlib.h>
13 #include <gst/gst.h>
14 #include <unistd.h>
15 #include <time.h>
16
17 #define DEBUG
18
19 gboolean playing = TRUE;
20 gboolean cut_start_signalled = FALSE;
21 gboolean cut_stop_signalled = FALSE;
22
23 int id = 0;                     /* increment this for each new cut */
24 GstElement *main_bin;
25 GstElement *audiosrc;
26 GstElement *queue;
27 GstElement *thread;
28 GstElement *cutter;
29 GstElement *filesink;
30 GstElement *encoder;
31 char buffer[255];
32
33 /* signal callbacks */
34
35 void
36 cut_start (GstElement * element)
37 {
38   g_print ("\nDEBUG: main: cut start\n");
39   /* we should pause the pipeline, unlink cutter and filesink
40    * create a new filesink to a real file, relink, and set to play
41    */
42   g_print ("DEBUG: cut_start: main_bin pausing\n");
43   gst_element_set_state (main_bin, GST_STATE_PAUSED);
44   g_print ("DEBUG: cut_start: main_bin paused\n");
45
46   {
47     time_t seconds;
48     struct tm *ct;
49
50     time (&seconds);
51     ct = localtime (&seconds);
52 /*    sprintf (buffer, "/news/incoming/audio/cutter.%06d.wav", id); */
53     sprintf (buffer,
54         "/news/incoming/audio/cutter.%04d%02d%02d.%02d%02d%02d.wav",
55         ct->tm_year + 1900, ct->tm_mon, ct->tm_mday, ct->tm_hour, ct->tm_min,
56         ct->tm_sec);
57   }
58   g_print ("DEBUG: cut_start: setting new location to %s\n", buffer);
59   g_object_set (G_OBJECT (filesink), "location", buffer, NULL);
60   g_object_set (G_OBJECT (filesink), "type", 4, NULL);
61
62   gst_element_set_state (main_bin, GST_STATE_PLAYING);
63   ++id;
64   g_print ("start_cut_signal done\n");
65   return;
66 }
67
68 void
69 cut_start_signal (GstElement * element)
70 {
71   g_print ("\nDEBUG: main: cut start signal\n");
72   cut_start_signalled = TRUE;
73 }
74
75 void
76 cut_stop (GstElement * element)
77 {
78   g_print ("\nDEBUG: main: cut stop\n");
79   /* we should pause the pipeline, unlink filesink, create a fake filesink,
80    * link to pipeline, and set to play
81    */
82   g_print ("DEBUG: cut_stop: main_bin paused\n");
83   gst_element_set_state (main_bin, GST_STATE_PAUSED);
84
85   g_print ("DEBUG: cut_stop: setting new location\n");
86   g_object_set (G_OBJECT (filesink), "location", "/dev/null", NULL);
87
88   gst_element_set_state (main_bin, GST_STATE_PLAYING);
89   g_print ("stop_cut_signal done\n");
90   return;
91 }
92
93 void
94 cut_stop_signal (GstElement * element)
95 {
96   g_print ("\nDEBUG: main: cut stop signal\n");
97   cut_stop_signalled = TRUE;
98 }
99
100 int
101 main (int argc, char *argv[])
102 {
103   /*int i, j; */
104   /*gboolean done; */
105
106   /*char buffer[20]; */
107
108   /*output_channel_t *channel_out; */
109
110   GstElement *audiosrc;
111
112   gst_init (&argc, &argv);
113 /*
114   if (argc == 1)
115   {
116     g_print("usage: %s <filename1> <filename2> <...>\n", argv[0]);
117     exit(-1);
118   }*/
119
120   /* set up input channel and main bin */
121
122   g_print ("creating main bin\n");
123   /* create cutter */
124   cutter = gst_element_factory_make ("cutter", "cutter");
125
126   g_object_set (G_OBJECT (cutter),
127       "threshold_dB", -40.0, "runlength", 0.5, "prelength", 1.0, NULL);
128
129   /* create an audio src */
130   if (!(audiosrc = gst_element_factory_make ("osssrc", "audio_src")))
131     g_error ("Could not create 'osssrc' element !\n");
132
133   /* set params */
134
135   g_object_set (G_OBJECT (audiosrc), "frequency", 44100,
136       "channels", 1, "format", 16, NULL);
137
138   if (!(encoder = gst_element_factory_make ("passthrough", "encoder")))
139     g_error ("Could not create 'passthrough' element !\n");
140
141   if (!(filesink = gst_element_factory_make ("afsink", "disk_sink")))
142     g_error ("Could not create 'afsink' element !\n");
143
144   g_object_set (G_OBJECT (filesink), "location", "/dev/null", NULL);
145
146   thread = gst_thread_new ("thread");
147   g_assert (thread != NULL);
148
149   /* create main bin */
150   main_bin = gst_pipeline_new ("bin");
151   g_assert (main_bin != NULL);
152
153   queue = gst_element_factory_make ("queue", "queue");
154   g_assert (queue);
155
156   /* add elements to bin */
157   gst_bin_add (GST_BIN (main_bin), audiosrc);
158   gst_bin_add (GST_BIN (thread), queue);
159
160   gst_bin_add_many (GST_BIN (thread), cutter, encoder, filesink, NULL);
161
162   gst_element_link_many (audiosrc, queue, cutter, encoder, filesink, NULL);
163   gst_bin_add (GST_BIN (main_bin), thread);
164
165   /* set signal handlers */
166   g_print ("setting signal handlers\n");
167   g_signal_connect (G_OBJECT (cutter), "cut_start",
168       (GCallback) cut_start_signal, NULL);
169   g_signal_connect (G_OBJECT (cutter), "cut_stop",
170       (GCallback) cut_stop_signal, NULL);
171
172   /* start playing */
173   g_print ("setting to play\n");
174   gst_element_set_state (main_bin, GST_STATE_PLAYING);
175 /*
176   g_print ("setting thread to play\n");
177   gst_element_set_state (GST_ELEMENT (thread), GST_STATE_PLAYING);
178 */
179   while (playing) {
180 /*      g_print ("> "); */
181     gst_bin_iterate (GST_BIN (main_bin));
182 /*      g_print (" <"); */
183     if (cut_start_signalled) {
184       g_print ("\nDEBUG: main: cut_start_signalled true !\n");
185       cut_start (cutter);
186       cut_start_signalled = FALSE;
187     }
188     if (cut_stop_signalled) {
189       g_print ("\nDEBUG: main: cut_stop_signalled true !\n");
190       cut_stop (cutter);
191       cut_stop_signalled = FALSE;
192     }
193   }
194   g_print ("we're done iterating.\n");
195   /* stop the bin */
196
197   gst_element_set_state (main_bin, GST_STATE_NULL);
198
199   gst_object_unref (filesink);
200   gst_object_unref (main_bin);
201
202   exit (0);
203 }