gst: Update for GST_PLUGIN_DEFINE() API changes
[platform/upstream/gstreamer.git] / tools / gst-app-maker
1 #!/bin/sh
2
3
4 prefix=gst
5 templatedir=element-templates
6
7 while [ "$1" ] ; do
8   case $1 in
9     --help)
10       cat <<-EOF
11 Usage: element-maker [OPTIONS] _NAME BASE_CLASS
12 Create a GStreamer application from a template.
13 Options:
14   --help             Print this information
15   --prefix PREFIX    Use PREFIX instead of "gst"
16 Example: 'gst-app-maker my_app' will create the file gstmyapp.c.
17 EOF
18       exit 0
19       ;;
20     --prefix)
21       shift
22       prefix=$1
23       ;;
24     -*)
25       echo Unknown option: $1
26       exit 1
27       ;;
28     *)
29       if [ "$name" = "" ]; then
30         name=$1
31       else
32         echo Ignored: $1
33       fi
34   esac
35   shift
36 done
37
38 if [ "$name" = "" ] ; then
39   echo "Usage: element-maker [OPTIONS] ELEMENT_NAME BASE_CLASS"
40   exit 1
41 fi
42
43
44 PREFIX=$(echo $prefix | sed -e 's/\(.*\)/\U\1/')
45 NAME=$(echo $name | sed -e 's/\(.*\)/\U\1/')
46 Prefix=$(echo $prefix | sed -e 's/_\(.\)/\U\1/g' -e 's/^\(.\)/\U\1/')
47 Name=$(echo $name | sed -e 's/_\(.\)/\U\1/g' -e 's/^\(.\)/\U\1/')
48
49 GST_IS_REPLACE=${PREFIX}_IS_${NAME}
50 GST_REPLACE=${PREFIX}_${NAME}
51 GST_TYPE_REPLACE=${PREFIX}_TYPE_${NAME}
52 GstReplace=${Prefix}${Name}
53 gst_replace=${prefix}_${name}
54 gstreplace=${prefix}$(echo $name | sed -e 's/_//g')
55 replace=$(echo $name | sed -e 's/_//g')
56
57 if [ "$REAL_NAME" = "" ] ; then
58   REAL_NAME=FIXME
59 fi
60 if [ "$EMAIL_ADDRESS" = "" ] ; then
61   EMAIL_ADDRESS=fixme@example.com
62 fi
63
64
65
66 generate ()
67 {
68
69 cat <<-EOF
70 /* GstReplace
71  * Copyright (C) $(date +%Y) $REAL_NAME <$EMAIL_ADDRESS>
72  * Copyright (C) 2010 Entropy Wave Inc
73  *
74  * Redistribution and use in source and binary forms, with or without
75  * modification, are permitted provided that the following conditions
76  * are met:
77  * 1. Redistributions of source code must retain the above copyright
78  *    notice, this list of conditions and the following disclaimer.
79  * 2. Redistributions in binary form must reproduce the above copyright
80  *    notice, this list of conditions and the following disclaimer in the
81  *    documentation and/or other materials provided with the distribution.
82  *
83  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
84  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
85  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
86  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
87  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
88  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
89  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
90  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
91  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
92  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
93  * POSSIBILITY OF SUCH DAMAGE.
94  */
95
96 #ifdef HAVE_CONFIG_H
97 #include "config.h"
98 #endif
99
100 #include <gst/gst.h>
101
102 #define GETTEXT_PACKAGE "replace"
103
104
105 typedef struct _GstReplace GstReplace;
106 struct _GstReplace {
107   GstElement *pipeline;
108   GstBus *bus;
109   GMainLoop *main_loop;
110
111   GstElement *source_element;
112   GstElement *sink_element;
113
114   gboolean paused_for_buffering;
115   guint timer_id;
116 };
117
118 GstReplace * gst_replace_new (void);
119 void gst_replace_free (GstReplace *replace);
120 void gst_replace_create_pipeline (GstReplace *replace);
121 void gst_replace_create_pipeline_playbin (GstReplace *replace, const char *uri);
122 void gst_replace_start (GstReplace *replace);
123 void gst_replace_stop (GstReplace *replace);
124
125 static gboolean gst_replace_handle_message (GstBus *bus, GstMessage *message,
126     gpointer data);
127 static gboolean onesecond_timer (gpointer priv);
128
129
130 gboolean verbose;
131
132 static GOptionEntry entries[] =
133 {
134   { "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose, "Be verbose", NULL },
135
136   { NULL }
137
138 };
139
140 int
141 main (int argc, char *argv[])
142 {
143   GError *error = NULL;
144   GOptionContext *context;
145   GstReplace *replace;
146   GMainLoop *main_loop;
147
148   if (!g_thread_supported ()) g_thread_init(NULL);
149
150   context = g_option_context_new ("- FIXME");
151   g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
152   g_option_context_add_group (context, gst_init_get_option_group ());
153   if (!g_option_context_parse (context, &argc, &argv, &error)) {
154     g_print ("option parsing failed: %s\n", error->message);
155     exit (1);
156   }
157   g_option_context_free (context);
158
159   replace = gst_replace_new ();
160
161   if (argc > 1) {
162     gchar *uri;
163     if (gst_uri_is_valid (argv[1])) {
164       uri = g_strdup (argv[1]);
165     } else {
166       uri = g_filename_to_uri (argv[1], NULL, NULL);
167     }
168     gst_replace_create_pipeline_playbin (replace, uri);
169     g_free (uri);
170   } else {
171     gst_replace_create_pipeline (replace);
172   }
173
174   gst_replace_start (replace);
175
176   main_loop = g_main_loop_new (NULL, TRUE);
177   replace->main_loop = main_loop;
178
179   g_main_loop_run (main_loop);
180
181   exit (0);
182 }
183
184
185 GstReplace *
186 gst_replace_new (void)
187 {
188   GstReplace *replace;
189
190   replace = g_new0 (GstReplace, 1);
191
192   return replace;
193 }
194
195 void
196 gst_replace_free (GstReplace *replace)
197 {
198   if (replace->source_element) {
199     gst_object_unref (replace->source_element);
200     replace->source_element = NULL;
201   }
202   if (replace->sink_element) {
203     gst_object_unref (replace->sink_element);
204     replace->sink_element = NULL;
205   }
206
207   if (replace->pipeline) {
208     gst_element_set_state (replace->pipeline, GST_STATE_NULL);
209     gst_object_unref (replace->pipeline);
210     replace->pipeline = NULL;
211   }
212   g_free (replace);
213 }
214
215 void
216 gst_replace_create_pipeline_playbin (GstReplace *replace, const char *uri)
217 {
218   GstElement *pipeline;
219   GError *error = NULL;
220   
221   pipeline = gst_pipeline_new (NULL);
222   gst_bin_add (GST_BIN(pipeline),
223       gst_element_factory_make ("playbin2", "source"));
224
225   if (error) {
226     g_print("pipeline parsing error: %s\n", error->message);
227     gst_object_unref (pipeline);
228     return;
229   }
230
231   replace->pipeline = pipeline;
232
233   gst_pipeline_set_auto_flush_bus (GST_PIPELINE(pipeline), FALSE);
234   replace->bus = gst_pipeline_get_bus (GST_PIPELINE(pipeline));
235   gst_bus_add_watch (replace->bus, gst_replace_handle_message, replace);
236
237   replace->source_element = gst_bin_get_by_name (GST_BIN(pipeline), "source");
238   g_print("source_element is %p\n", replace->source_element);
239
240   g_print("setting uri to %s\n", uri);
241   g_object_set (replace->source_element, "uri", uri, NULL);
242 }
243
244 void
245 gst_replace_create_pipeline (GstReplace *replace)
246 {
247   GString *pipe_desc;
248   GstElement *pipeline;
249   GError *error = NULL;
250   
251   pipe_desc = g_string_new ("");
252
253   g_string_append (pipe_desc, "videotestsrc name=source num-buffers=100 ! ");
254   g_string_append (pipe_desc, "timeoverlay ! ");
255   g_string_append (pipe_desc, "xvimagesink name=sink ");
256   g_string_append (pipe_desc, "audiotestsrc samplesperbuffer=1600 num-buffers=100 ! ");
257   g_string_append (pipe_desc, "alsasink ");
258
259   if (verbose) g_print ("pipeline: %s\n", pipe_desc->str);
260
261   pipeline = (GstElement *) gst_parse_launch (pipe_desc->str, &error);
262   g_string_free (pipe_desc, FALSE);
263
264   if (error) {
265     g_print("pipeline parsing error: %s\n", error->message);
266     gst_object_unref (pipeline);
267     return;
268   }
269
270   replace->pipeline = pipeline;
271
272   gst_pipeline_set_auto_flush_bus (GST_PIPELINE(pipeline), FALSE);
273   replace->bus = gst_pipeline_get_bus (GST_PIPELINE(pipeline));
274   gst_bus_add_watch (replace->bus, gst_replace_handle_message, replace);
275
276   replace->source_element = gst_bin_get_by_name (GST_BIN(pipeline), "source");
277   replace->sink_element = gst_bin_get_by_name (GST_BIN(pipeline), "sink");
278 }
279
280 void
281 gst_replace_start (GstReplace *replace)
282 {
283   gst_element_set_state (replace->pipeline, GST_STATE_READY);
284
285   replace->timer_id = g_timeout_add (1000, onesecond_timer, replace);
286 }
287
288 void
289 gst_replace_stop (GstReplace *replace)
290 {
291   gst_element_set_state (replace->pipeline, GST_STATE_NULL);
292
293   g_source_remove (replace->timer_id);
294 }
295
296 static void
297 gst_replace_handle_eos (GstReplace *replace)
298 {
299   gst_replace_stop (replace);
300 }
301
302 static void
303 gst_replace_handle_error (GstReplace *replace, GError *error,
304     const char *debug)
305 {
306   g_print ("error: %s\n", error->message);
307   gst_replace_stop (replace);
308 }
309
310 static void
311 gst_replace_handle_warning (GstReplace *replace, GError *error,
312     const char *debug)
313 {
314   g_print ("warning: %s\n", error->message);
315 }
316
317 static void
318 gst_replace_handle_info (GstReplace *replace, GError *error,
319     const char *debug)
320 {
321   g_print ("info: %s\n", error->message);
322 }
323
324 static void
325 gst_replace_handle_null_to_ready (GstReplace *replace)
326 {
327   gst_element_set_state (replace->pipeline, GST_STATE_PAUSED);
328
329 }
330
331 static void
332 gst_replace_handle_ready_to_paused (GstReplace *replace)
333 {
334   if (!replace->paused_for_buffering) {
335     gst_element_set_state (replace->pipeline, GST_STATE_PLAYING);
336   }
337 }
338
339 static void
340 gst_replace_handle_paused_to_playing (GstReplace *replace)
341 {
342
343 }
344
345 static void
346 gst_replace_handle_playing_to_paused (GstReplace *replace)
347 {
348
349 }
350
351 static void
352 gst_replace_handle_paused_to_ready (GstReplace *replace)
353 {
354
355 }
356
357 static void
358 gst_replace_handle_ready_to_null (GstReplace *replace)
359 {
360   g_main_loop_quit (replace->main_loop);
361
362 }
363
364
365 static gboolean
366 gst_replace_handle_message (GstBus *bus, GstMessage *message,
367     gpointer data)
368 {
369   GstReplace *replace = (GstReplace *) data;
370
371   switch (GST_MESSAGE_TYPE(message)) {
372     case GST_MESSAGE_EOS:
373       gst_replace_handle_eos (replace);
374       break;
375     case GST_MESSAGE_ERROR:
376       {
377         GError *error = NULL;
378         gchar *debug;
379
380         gst_message_parse_error (message, &error, &debug);
381         gst_replace_handle_error (replace, error, debug);
382       }
383       break;
384     case GST_MESSAGE_WARNING:
385       {
386         GError *error = NULL;
387         gchar *debug;
388
389         gst_message_parse_warning (message, &error, &debug);
390         gst_replace_handle_warning (replace, error, debug);
391       }
392       break;
393     case GST_MESSAGE_INFO:
394       {
395         GError *error = NULL;
396         gchar *debug;
397
398         gst_message_parse_info (message, &error, &debug);
399         gst_replace_handle_info (replace, error, debug);
400       }
401       break;
402     case GST_MESSAGE_TAG:
403       {
404         GstTagList *tag_list;
405
406         gst_message_parse_tag (message, &tag_list);
407         if (verbose) g_print("tag\n");
408       }
409       break;
410     case GST_MESSAGE_STATE_CHANGED:
411       {
412         GstState oldstate, newstate, pending;
413
414         gst_message_parse_state_changed (message, &oldstate, &newstate,
415             &pending);
416         if (GST_ELEMENT(message->src) == replace->pipeline) {
417           if (verbose) g_print("state change from %s to %s\n",
418               gst_element_state_get_name (oldstate),
419               gst_element_state_get_name (newstate));
420           switch (GST_STATE_TRANSITION(oldstate, newstate)) {
421             case GST_STATE_CHANGE_NULL_TO_READY:
422               gst_replace_handle_null_to_ready (replace);
423               break;
424             case GST_STATE_CHANGE_READY_TO_PAUSED:
425               gst_replace_handle_ready_to_paused (replace);
426               break;
427             case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
428               gst_replace_handle_paused_to_playing (replace);
429               break;
430             case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
431               gst_replace_handle_playing_to_paused (replace);
432               break;
433             case GST_STATE_CHANGE_PAUSED_TO_READY:
434               gst_replace_handle_paused_to_ready (replace);
435               break;
436             case GST_STATE_CHANGE_READY_TO_NULL:
437               gst_replace_handle_ready_to_null (replace);
438               break;
439             default:
440               if (verbose) g_print("unknown state change from %s to %s\n",
441                   gst_element_state_get_name (oldstate),
442                   gst_element_state_get_name (newstate));
443           }
444         }
445       }
446       break;
447     case GST_MESSAGE_BUFFERING:
448       {
449         int percent;
450         gst_message_parse_buffering (message, &percent);
451         //g_print("buffering %d\n", percent);
452         if (!replace->paused_for_buffering && percent < 100) {
453           g_print ("pausing for buffing\n");
454           replace->paused_for_buffering = TRUE;
455           gst_element_set_state (replace->pipeline, GST_STATE_PAUSED);
456         } else if (replace->paused_for_buffering && percent == 100) {
457           g_print ("unpausing for buffing\n");
458           replace->paused_for_buffering = FALSE;
459           gst_element_set_state (replace->pipeline, GST_STATE_PLAYING);
460         }
461       }
462       break;
463     case GST_MESSAGE_STATE_DIRTY:
464     case GST_MESSAGE_CLOCK_PROVIDE:
465     case GST_MESSAGE_CLOCK_LOST:
466     case GST_MESSAGE_NEW_CLOCK:
467     case GST_MESSAGE_STRUCTURE_CHANGE:
468     case GST_MESSAGE_STREAM_STATUS:
469       break;
470     case GST_MESSAGE_STEP_DONE:
471     case GST_MESSAGE_APPLICATION:
472     case GST_MESSAGE_ELEMENT:
473     case GST_MESSAGE_SEGMENT_START:
474     case GST_MESSAGE_SEGMENT_DONE:
475     case GST_MESSAGE_DURATION:
476     case GST_MESSAGE_LATENCY:
477     case GST_MESSAGE_ASYNC_START:
478     case GST_MESSAGE_ASYNC_DONE:
479     case GST_MESSAGE_REQUEST_STATE:
480     case GST_MESSAGE_STEP_START:
481     case GST_MESSAGE_QOS:
482     default:
483       if (verbose) {
484         g_print ("message: %s\n", GST_MESSAGE_TYPE_NAME (message));
485       }
486       break;
487   }
488
489   return TRUE;
490 }
491
492
493
494 static gboolean
495 onesecond_timer (gpointer priv)
496 {
497   //GstReplace *replace = (GstReplace *)priv;
498
499   g_print(".\n");
500
501   return TRUE;
502 }
503
504
505
506 /* helper functions */
507
508 #if 0
509 gboolean
510 have_element (const gchar *element_name)
511 {
512   GstPluginFeature *feature;
513
514   feature = gst_default_registry_find_feature (element_name,
515       GST_TYPE_ELEMENT_FACTORY);
516   if (feature) {
517     g_object_unref (feature);
518     return TRUE;
519   }
520   return FALSE;
521 }
522 #endif
523
524 EOF
525
526 }
527
528 generate | sed \
529   -e "s/GST_BASE_REPLACE/$GST_BASE_REPLACE/g" \
530   -e "s/GST_TYPE_BASE_REPLACE/$GST_TYPE_BASE_REPLACE/g" \
531   -e "s/GstBaseReplace/$GstBaseReplace/g" \
532   -e "s/GST_IS_REPLACE/$GST_IS_REPLACE/g" \
533   -e "s/GST_REPLACE/$GST_REPLACE/g" \
534   -e "s/GST_TYPE_REPLACE/$GST_TYPE_REPLACE/g" \
535   -e "s/GstReplace/$GstReplace/g" \
536   -e "s/gst_replace/$gst_replace/g" \
537   -e "s/gstreplace/$gstreplace/g" \
538   -e "s/replace/$replace/g" >$gstreplace.c
539
540 gst-indent $gstreplace.c
541
542 gcc -Wall $(pkg-config --cflags gstreamer-0.10) -c -o $gstreplace.o $gstreplace.c
543 gcc -o $gstreplace $gstreplace.o $(pkg-config --libs gstreamer-0.10)
544
545