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);
154 g_clear_error (&error);
155 g_option_context_free (context);
158 g_option_context_free (context);
160 replace = gst_replace_new ();
164 if (gst_uri_is_valid (argv[1])) {
165 uri = g_strdup (argv[1]);
167 uri = g_filename_to_uri (argv[1], NULL, NULL);
169 gst_replace_create_pipeline_playbin (replace, uri);
172 gst_replace_create_pipeline (replace);
175 gst_replace_start (replace);
177 main_loop = g_main_loop_new (NULL, TRUE);
178 replace->main_loop = main_loop;
180 g_main_loop_run (main_loop);
187 gst_replace_new (void)
191 replace = g_new0 (GstReplace, 1);
197 gst_replace_free (GstReplace *replace)
199 if (replace->source_element) {
200 gst_object_unref (replace->source_element);
201 replace->source_element = NULL;
203 if (replace->sink_element) {
204 gst_object_unref (replace->sink_element);
205 replace->sink_element = NULL;
208 if (replace->pipeline) {
209 gst_element_set_state (replace->pipeline, GST_STATE_NULL);
210 gst_object_unref (replace->pipeline);
211 replace->pipeline = NULL;
217 gst_replace_create_pipeline_playbin (GstReplace *replace, const char *uri)
219 GstElement *pipeline;
220 GError *error = NULL;
222 pipeline = gst_pipeline_new (NULL);
223 gst_bin_add (GST_BIN(pipeline),
224 gst_element_factory_make ("playbin", "source"));
227 g_print("pipeline parsing error: %s\n", error->message);
228 g_clear_error (&error);
229 gst_object_unref (pipeline);
233 replace->pipeline = pipeline;
235 gst_pipeline_set_auto_flush_bus (GST_PIPELINE(pipeline), FALSE);
236 replace->bus = gst_pipeline_get_bus (GST_PIPELINE(pipeline));
237 gst_bus_add_watch (replace->bus, gst_replace_handle_message, replace);
239 replace->source_element = gst_bin_get_by_name (GST_BIN(pipeline), "source");
240 g_print("source_element is %p\n", replace->source_element);
242 g_print("setting uri to %s\n", uri);
243 g_object_set (replace->source_element, "uri", uri, NULL);
247 gst_replace_create_pipeline (GstReplace *replace)
250 GstElement *pipeline;
251 GError *error = NULL;
253 pipe_desc = g_string_new ("");
255 g_string_append (pipe_desc, "videotestsrc name=source num-buffers=100 ! ");
256 g_string_append (pipe_desc, "timeoverlay ! ");
257 g_string_append (pipe_desc, "xvimagesink name=sink ");
258 g_string_append (pipe_desc, "audiotestsrc samplesperbuffer=1600 num-buffers=100 ! ");
259 g_string_append (pipe_desc, "alsasink ");
261 if (verbose) g_print ("pipeline: %s\n", pipe_desc->str);
263 pipeline = (GstElement *) gst_parse_launch (pipe_desc->str, &error);
264 g_string_free (pipe_desc, FALSE);
267 g_print("pipeline parsing error: %s\n", error->message);
268 g_clear_error (&error);
269 gst_object_unref (pipeline);
273 replace->pipeline = pipeline;
275 gst_pipeline_set_auto_flush_bus (GST_PIPELINE(pipeline), FALSE);
276 replace->bus = gst_pipeline_get_bus (GST_PIPELINE(pipeline));
277 gst_bus_add_watch (replace->bus, gst_replace_handle_message, replace);
279 replace->source_element = gst_bin_get_by_name (GST_BIN(pipeline), "source");
280 replace->sink_element = gst_bin_get_by_name (GST_BIN(pipeline), "sink");
284 gst_replace_start (GstReplace *replace)
286 gst_element_set_state (replace->pipeline, GST_STATE_READY);
288 replace->timer_id = g_timeout_add (1000, onesecond_timer, replace);
292 gst_replace_stop (GstReplace *replace)
294 gst_element_set_state (replace->pipeline, GST_STATE_NULL);
296 g_source_remove (replace->timer_id);
300 gst_replace_handle_eos (GstReplace *replace)
302 gst_replace_stop (replace);
306 gst_replace_handle_error (GstReplace *replace, GError *error,
309 g_print ("error: %s\n", error->message);
310 gst_replace_stop (replace);
314 gst_replace_handle_warning (GstReplace *replace, GError *error,
317 g_print ("warning: %s\n", error->message);
321 gst_replace_handle_info (GstReplace *replace, GError *error,
324 g_print ("info: %s\n", error->message);
328 gst_replace_handle_null_to_ready (GstReplace *replace)
330 gst_element_set_state (replace->pipeline, GST_STATE_PAUSED);
335 gst_replace_handle_ready_to_paused (GstReplace *replace)
337 if (!replace->paused_for_buffering) {
338 gst_element_set_state (replace->pipeline, GST_STATE_PLAYING);
343 gst_replace_handle_paused_to_playing (GstReplace *replace)
349 gst_replace_handle_playing_to_paused (GstReplace *replace)
355 gst_replace_handle_paused_to_ready (GstReplace *replace)
361 gst_replace_handle_ready_to_null (GstReplace *replace)
363 g_main_loop_quit (replace->main_loop);
369 gst_replace_handle_message (GstBus *bus, GstMessage *message,
372 GstReplace *replace = (GstReplace *) data;
374 switch (GST_MESSAGE_TYPE(message)) {
375 case GST_MESSAGE_EOS:
376 gst_replace_handle_eos (replace);
378 case GST_MESSAGE_ERROR:
380 GError *error = NULL;
383 gst_message_parse_error (message, &error, &debug);
384 gst_replace_handle_error (replace, error, debug);
385 g_clear_error (&error);
388 case GST_MESSAGE_WARNING:
390 GError *error = NULL;
393 gst_message_parse_warning (message, &error, &debug);
394 gst_replace_handle_warning (replace, error, debug);
395 g_clear_error (&error);
398 case GST_MESSAGE_INFO:
400 GError *error = NULL;
403 gst_message_parse_info (message, &error, &debug);
404 gst_replace_handle_info (replace, error, debug);
405 g_clear_error (&error);
408 case GST_MESSAGE_TAG:
410 GstTagList *tag_list;
412 gst_message_parse_tag (message, &tag_list);
413 if (verbose) g_print("tag\n");
416 case GST_MESSAGE_STATE_CHANGED:
418 GstState oldstate, newstate, pending;
420 gst_message_parse_state_changed (message, &oldstate, &newstate,
422 if (GST_ELEMENT(message->src) == replace->pipeline) {
423 if (verbose) g_print("state change from %s to %s\n",
424 gst_element_state_get_name (oldstate),
425 gst_element_state_get_name (newstate));
426 switch (GST_STATE_TRANSITION(oldstate, newstate)) {
427 case GST_STATE_CHANGE_NULL_TO_READY:
428 gst_replace_handle_null_to_ready (replace);
430 case GST_STATE_CHANGE_READY_TO_PAUSED:
431 gst_replace_handle_ready_to_paused (replace);
433 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
434 gst_replace_handle_paused_to_playing (replace);
436 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
437 gst_replace_handle_playing_to_paused (replace);
439 case GST_STATE_CHANGE_PAUSED_TO_READY:
440 gst_replace_handle_paused_to_ready (replace);
442 case GST_STATE_CHANGE_READY_TO_NULL:
443 gst_replace_handle_ready_to_null (replace);
446 if (verbose) g_print("unknown state change from %s to %s\n",
447 gst_element_state_get_name (oldstate),
448 gst_element_state_get_name (newstate));
453 case GST_MESSAGE_BUFFERING:
456 gst_message_parse_buffering (message, &percent);
457 //g_print("buffering %d\n", percent);
458 if (!replace->paused_for_buffering && percent < 100) {
459 g_print ("pausing for buffing\n");
460 replace->paused_for_buffering = TRUE;
461 gst_element_set_state (replace->pipeline, GST_STATE_PAUSED);
462 } else if (replace->paused_for_buffering && percent == 100) {
463 g_print ("unpausing for buffing\n");
464 replace->paused_for_buffering = FALSE;
465 gst_element_set_state (replace->pipeline, GST_STATE_PLAYING);
469 case GST_MESSAGE_STATE_DIRTY:
470 case GST_MESSAGE_CLOCK_PROVIDE:
471 case GST_MESSAGE_CLOCK_LOST:
472 case GST_MESSAGE_NEW_CLOCK:
473 case GST_MESSAGE_STRUCTURE_CHANGE:
474 case GST_MESSAGE_STREAM_STATUS:
476 case GST_MESSAGE_STEP_DONE:
477 case GST_MESSAGE_APPLICATION:
478 case GST_MESSAGE_ELEMENT:
479 case GST_MESSAGE_SEGMENT_START:
480 case GST_MESSAGE_SEGMENT_DONE:
481 case GST_MESSAGE_DURATION:
482 case GST_MESSAGE_LATENCY:
483 case GST_MESSAGE_ASYNC_START:
484 case GST_MESSAGE_ASYNC_DONE:
485 case GST_MESSAGE_REQUEST_STATE:
486 case GST_MESSAGE_STEP_START:
487 case GST_MESSAGE_QOS:
490 g_print ("message: %s\n", GST_MESSAGE_TYPE_NAME (message));
501 onesecond_timer (gpointer priv)
503 //GstReplace *replace = (GstReplace *)priv;
512 /* helper functions */
516 have_element (const gchar *element_name)
518 GstPluginFeature *feature;
520 feature = gst_default_registry_find_feature (element_name,
521 GST_TYPE_ELEMENT_FACTORY);
523 g_object_unref (feature);
535 -e "s/GST_BASE_REPLACE/$GST_BASE_REPLACE/g" \
536 -e "s/GST_TYPE_BASE_REPLACE/$GST_TYPE_BASE_REPLACE/g" \
537 -e "s/GstBaseReplace/$GstBaseReplace/g" \
538 -e "s/GST_IS_REPLACE/$GST_IS_REPLACE/g" \
539 -e "s/GST_REPLACE/$GST_REPLACE/g" \
540 -e "s/GST_TYPE_REPLACE/$GST_TYPE_REPLACE/g" \
541 -e "s/GstReplace/$GstReplace/g" \
542 -e "s/gst_replace/$gst_replace/g" \
543 -e "s/gstreplace/$gstreplace/g" \
544 -e "s/replace/$replace/g" >$gstreplace.c
546 gst-indent $gstreplace.c
548 gcc -O2 -Wall $(pkg-config --cflags gstreamer-1.0) -c -o $gstreplace.o $gstreplace.c
549 gcc -o $gstreplace $gstreplace.o $(pkg-config --libs gstreamer-1.0)