5 templatedir=element-templates
11 Usage: element-maker [OPTIONS] _NAME BASE_CLASS
12 Create a GStreamer application from a template.
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.
25 echo Unknown option: $1
29 if [ "$name" = "" ]; then
38 if [ "$name" = "" ] ; then
39 echo "Usage: element-maker [OPTIONS] ELEMENT_NAME BASE_CLASS"
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/')
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')
57 if [ "$REAL_NAME" = "" ] ; then
60 if [ "$EMAIL_ADDRESS" = "" ] ; then
61 EMAIL_ADDRESS=fixme@example.com
71 * Copyright (C) $(date +%Y) $REAL_NAME <$EMAIL_ADDRESS>
72 * Copyright (C) 2010 Entropy Wave Inc
74 * Redistribution and use in source and binary forms, with or without
75 * modification, are permitted provided that the following conditions
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.
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.
103 #define GETTEXT_PACKAGE "replace"
106 typedef struct _GstReplace GstReplace;
108 GstElement *pipeline;
110 GMainLoop *main_loop;
112 GstElement *source_element;
113 GstElement *sink_element;
115 gboolean paused_for_buffering;
119 GstReplace * gst_replace_new (void);
120 void gst_replace_free (GstReplace *replace);
121 void gst_replace_create_pipeline (GstReplace *replace);
122 void gst_replace_create_pipeline_playbin (GstReplace *replace, const char *uri);
123 void gst_replace_start (GstReplace *replace);
124 void gst_replace_stop (GstReplace *replace);
126 static gboolean gst_replace_handle_message (GstBus *bus, GstMessage *message,
128 static gboolean onesecond_timer (gpointer priv);
133 static GOptionEntry entries[] =
135 { "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose, "Be verbose", NULL },
142 main (int argc, char *argv[])
144 GError *error = NULL;
145 GOptionContext *context;
147 GMainLoop *main_loop;
149 context = g_option_context_new ("- FIXME");
150 g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
151 g_option_context_add_group (context, gst_init_get_option_group ());
152 if (!g_option_context_parse (context, &argc, &argv, &error)) {
153 g_print ("option parsing failed: %s\n", error->message);
156 g_option_context_free (context);
158 replace = gst_replace_new ();
162 if (gst_uri_is_valid (argv[1])) {
163 uri = g_strdup (argv[1]);
165 uri = g_filename_to_uri (argv[1], NULL, NULL);
167 gst_replace_create_pipeline_playbin (replace, uri);
170 gst_replace_create_pipeline (replace);
173 gst_replace_start (replace);
175 main_loop = g_main_loop_new (NULL, TRUE);
176 replace->main_loop = main_loop;
178 g_main_loop_run (main_loop);
185 gst_replace_new (void)
189 replace = g_new0 (GstReplace, 1);
195 gst_replace_free (GstReplace *replace)
197 if (replace->source_element) {
198 gst_object_unref (replace->source_element);
199 replace->source_element = NULL;
201 if (replace->sink_element) {
202 gst_object_unref (replace->sink_element);
203 replace->sink_element = NULL;
206 if (replace->pipeline) {
207 gst_element_set_state (replace->pipeline, GST_STATE_NULL);
208 gst_object_unref (replace->pipeline);
209 replace->pipeline = NULL;
215 gst_replace_create_pipeline_playbin (GstReplace *replace, const char *uri)
217 GstElement *pipeline;
218 GError *error = NULL;
220 pipeline = gst_pipeline_new (NULL);
221 gst_bin_add (GST_BIN(pipeline),
222 gst_element_factory_make ("playbin", "source"));
225 g_print("pipeline parsing error: %s\n", error->message);
226 gst_object_unref (pipeline);
230 replace->pipeline = pipeline;
232 gst_pipeline_set_auto_flush_bus (GST_PIPELINE(pipeline), FALSE);
233 replace->bus = gst_pipeline_get_bus (GST_PIPELINE(pipeline));
234 gst_bus_add_watch (replace->bus, gst_replace_handle_message, replace);
236 replace->source_element = gst_bin_get_by_name (GST_BIN(pipeline), "source");
237 g_print("source_element is %p\n", replace->source_element);
239 g_print("setting uri to %s\n", uri);
240 g_object_set (replace->source_element, "uri", uri, NULL);
244 gst_replace_create_pipeline (GstReplace *replace)
247 GstElement *pipeline;
248 GError *error = NULL;
250 pipe_desc = g_string_new ("");
252 g_string_append (pipe_desc, "videotestsrc name=source num-buffers=100 ! ");
253 g_string_append (pipe_desc, "timeoverlay ! ");
254 g_string_append (pipe_desc, "xvimagesink name=sink ");
255 g_string_append (pipe_desc, "audiotestsrc samplesperbuffer=1600 num-buffers=100 ! ");
256 g_string_append (pipe_desc, "alsasink ");
258 if (verbose) g_print ("pipeline: %s\n", pipe_desc->str);
260 pipeline = (GstElement *) gst_parse_launch (pipe_desc->str, &error);
261 g_string_free (pipe_desc, FALSE);
264 g_print("pipeline parsing error: %s\n", error->message);
265 gst_object_unref (pipeline);
269 replace->pipeline = pipeline;
271 gst_pipeline_set_auto_flush_bus (GST_PIPELINE(pipeline), FALSE);
272 replace->bus = gst_pipeline_get_bus (GST_PIPELINE(pipeline));
273 gst_bus_add_watch (replace->bus, gst_replace_handle_message, replace);
275 replace->source_element = gst_bin_get_by_name (GST_BIN(pipeline), "source");
276 replace->sink_element = gst_bin_get_by_name (GST_BIN(pipeline), "sink");
280 gst_replace_start (GstReplace *replace)
282 gst_element_set_state (replace->pipeline, GST_STATE_READY);
284 replace->timer_id = g_timeout_add (1000, onesecond_timer, replace);
288 gst_replace_stop (GstReplace *replace)
290 gst_element_set_state (replace->pipeline, GST_STATE_NULL);
292 g_source_remove (replace->timer_id);
296 gst_replace_handle_eos (GstReplace *replace)
298 gst_replace_stop (replace);
302 gst_replace_handle_error (GstReplace *replace, GError *error,
305 g_print ("error: %s\n", error->message);
306 gst_replace_stop (replace);
310 gst_replace_handle_warning (GstReplace *replace, GError *error,
313 g_print ("warning: %s\n", error->message);
317 gst_replace_handle_info (GstReplace *replace, GError *error,
320 g_print ("info: %s\n", error->message);
324 gst_replace_handle_null_to_ready (GstReplace *replace)
326 gst_element_set_state (replace->pipeline, GST_STATE_PAUSED);
331 gst_replace_handle_ready_to_paused (GstReplace *replace)
333 if (!replace->paused_for_buffering) {
334 gst_element_set_state (replace->pipeline, GST_STATE_PLAYING);
339 gst_replace_handle_paused_to_playing (GstReplace *replace)
345 gst_replace_handle_playing_to_paused (GstReplace *replace)
351 gst_replace_handle_paused_to_ready (GstReplace *replace)
357 gst_replace_handle_ready_to_null (GstReplace *replace)
359 g_main_loop_quit (replace->main_loop);
365 gst_replace_handle_message (GstBus *bus, GstMessage *message,
368 GstReplace *replace = (GstReplace *) data;
370 switch (GST_MESSAGE_TYPE(message)) {
371 case GST_MESSAGE_EOS:
372 gst_replace_handle_eos (replace);
374 case GST_MESSAGE_ERROR:
376 GError *error = NULL;
379 gst_message_parse_error (message, &error, &debug);
380 gst_replace_handle_error (replace, error, debug);
383 case GST_MESSAGE_WARNING:
385 GError *error = NULL;
388 gst_message_parse_warning (message, &error, &debug);
389 gst_replace_handle_warning (replace, error, debug);
392 case GST_MESSAGE_INFO:
394 GError *error = NULL;
397 gst_message_parse_info (message, &error, &debug);
398 gst_replace_handle_info (replace, error, debug);
401 case GST_MESSAGE_TAG:
403 GstTagList *tag_list;
405 gst_message_parse_tag (message, &tag_list);
406 if (verbose) g_print("tag\n");
409 case GST_MESSAGE_STATE_CHANGED:
411 GstState oldstate, newstate, pending;
413 gst_message_parse_state_changed (message, &oldstate, &newstate,
415 if (GST_ELEMENT(message->src) == replace->pipeline) {
416 if (verbose) g_print("state change from %s to %s\n",
417 gst_element_state_get_name (oldstate),
418 gst_element_state_get_name (newstate));
419 switch (GST_STATE_TRANSITION(oldstate, newstate)) {
420 case GST_STATE_CHANGE_NULL_TO_READY:
421 gst_replace_handle_null_to_ready (replace);
423 case GST_STATE_CHANGE_READY_TO_PAUSED:
424 gst_replace_handle_ready_to_paused (replace);
426 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
427 gst_replace_handle_paused_to_playing (replace);
429 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
430 gst_replace_handle_playing_to_paused (replace);
432 case GST_STATE_CHANGE_PAUSED_TO_READY:
433 gst_replace_handle_paused_to_ready (replace);
435 case GST_STATE_CHANGE_READY_TO_NULL:
436 gst_replace_handle_ready_to_null (replace);
439 if (verbose) g_print("unknown state change from %s to %s\n",
440 gst_element_state_get_name (oldstate),
441 gst_element_state_get_name (newstate));
446 case GST_MESSAGE_BUFFERING:
449 gst_message_parse_buffering (message, &percent);
450 //g_print("buffering %d\n", percent);
451 if (!replace->paused_for_buffering && percent < 100) {
452 g_print ("pausing for buffing\n");
453 replace->paused_for_buffering = TRUE;
454 gst_element_set_state (replace->pipeline, GST_STATE_PAUSED);
455 } else if (replace->paused_for_buffering && percent == 100) {
456 g_print ("unpausing for buffing\n");
457 replace->paused_for_buffering = FALSE;
458 gst_element_set_state (replace->pipeline, GST_STATE_PLAYING);
462 case GST_MESSAGE_STATE_DIRTY:
463 case GST_MESSAGE_CLOCK_PROVIDE:
464 case GST_MESSAGE_CLOCK_LOST:
465 case GST_MESSAGE_NEW_CLOCK:
466 case GST_MESSAGE_STRUCTURE_CHANGE:
467 case GST_MESSAGE_STREAM_STATUS:
469 case GST_MESSAGE_STEP_DONE:
470 case GST_MESSAGE_APPLICATION:
471 case GST_MESSAGE_ELEMENT:
472 case GST_MESSAGE_SEGMENT_START:
473 case GST_MESSAGE_SEGMENT_DONE:
474 case GST_MESSAGE_DURATION:
475 case GST_MESSAGE_LATENCY:
476 case GST_MESSAGE_ASYNC_START:
477 case GST_MESSAGE_ASYNC_DONE:
478 case GST_MESSAGE_REQUEST_STATE:
479 case GST_MESSAGE_STEP_START:
480 case GST_MESSAGE_QOS:
483 g_print ("message: %s\n", GST_MESSAGE_TYPE_NAME (message));
494 onesecond_timer (gpointer priv)
496 //GstReplace *replace = (GstReplace *)priv;
505 /* helper functions */
509 have_element (const gchar *element_name)
511 GstPluginFeature *feature;
513 feature = gst_default_registry_find_feature (element_name,
514 GST_TYPE_ELEMENT_FACTORY);
516 g_object_unref (feature);
528 -e "s/GST_BASE_REPLACE/$GST_BASE_REPLACE/g" \
529 -e "s/GST_TYPE_BASE_REPLACE/$GST_TYPE_BASE_REPLACE/g" \
530 -e "s/GstBaseReplace/$GstBaseReplace/g" \
531 -e "s/GST_IS_REPLACE/$GST_IS_REPLACE/g" \
532 -e "s/GST_REPLACE/$GST_REPLACE/g" \
533 -e "s/GST_TYPE_REPLACE/$GST_TYPE_REPLACE/g" \
534 -e "s/GstReplace/$GstReplace/g" \
535 -e "s/gst_replace/$gst_replace/g" \
536 -e "s/gstreplace/$gstreplace/g" \
537 -e "s/replace/$replace/g" >$gstreplace.c
539 gst-indent $gstreplace.c
541 gcc -O2 -Wall $(pkg-config --cflags gstreamer-1.0) -c -o $gstreplace.o $gstreplace.c
542 gcc -o $gstreplace $gstreplace.o $(pkg-config --libs gstreamer-1.0)