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.
102 #define GETTEXT_PACKAGE "replace"
105 typedef struct _GstReplace GstReplace;
107 GstElement *pipeline;
109 GMainLoop *main_loop;
111 GstElement *source_element;
112 GstElement *sink_element;
114 gboolean paused_for_buffering;
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);
125 static gboolean gst_replace_handle_message (GstBus *bus, GstMessage *message,
127 static gboolean onesecond_timer (gpointer priv);
132 static GOptionEntry entries[] =
134 { "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose, "Be verbose", NULL },
141 main (int argc, char *argv[])
143 GError *error = NULL;
144 GOptionContext *context;
146 GMainLoop *main_loop;
148 if (!g_thread_supported ()) g_thread_init(NULL);
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);
157 g_option_context_free (context);
159 replace = gst_replace_new ();
163 if (gst_uri_is_valid (argv[1])) {
164 uri = g_strdup (argv[1]);
166 uri = g_filename_to_uri (argv[1], NULL, NULL);
168 gst_replace_create_pipeline_playbin (replace, uri);
171 gst_replace_create_pipeline (replace);
174 gst_replace_start (replace);
176 main_loop = g_main_loop_new (NULL, TRUE);
177 replace->main_loop = main_loop;
179 g_main_loop_run (main_loop);
186 gst_replace_new (void)
190 replace = g_new0 (GstReplace, 1);
196 gst_replace_free (GstReplace *replace)
198 if (replace->source_element) {
199 gst_object_unref (replace->source_element);
200 replace->source_element = NULL;
202 if (replace->sink_element) {
203 gst_object_unref (replace->sink_element);
204 replace->sink_element = NULL;
207 if (replace->pipeline) {
208 gst_element_set_state (replace->pipeline, GST_STATE_NULL);
209 gst_object_unref (replace->pipeline);
210 replace->pipeline = NULL;
216 gst_replace_create_pipeline_playbin (GstReplace *replace, const char *uri)
218 GstElement *pipeline;
219 GError *error = NULL;
221 pipeline = gst_pipeline_new (NULL);
222 gst_bin_add (GST_BIN(pipeline),
223 gst_element_factory_make ("playbin2", "source"));
226 g_print("pipeline parsing error: %s\n", error->message);
227 gst_object_unref (pipeline);
231 replace->pipeline = pipeline;
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);
237 replace->source_element = gst_bin_get_by_name (GST_BIN(pipeline), "source");
238 g_print("source_element is %p\n", replace->source_element);
240 g_print("setting uri to %s\n", uri);
241 g_object_set (replace->source_element, "uri", uri, NULL);
245 gst_replace_create_pipeline (GstReplace *replace)
248 GstElement *pipeline;
249 GError *error = NULL;
251 pipe_desc = g_string_new ("");
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 ");
259 if (verbose) g_print ("pipeline: %s\n", pipe_desc->str);
261 pipeline = (GstElement *) gst_parse_launch (pipe_desc->str, &error);
262 g_string_free (pipe_desc, FALSE);
265 g_print("pipeline parsing error: %s\n", error->message);
266 gst_object_unref (pipeline);
270 replace->pipeline = pipeline;
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);
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");
281 gst_replace_start (GstReplace *replace)
283 gst_element_set_state (replace->pipeline, GST_STATE_READY);
285 replace->timer_id = g_timeout_add (1000, onesecond_timer, replace);
289 gst_replace_stop (GstReplace *replace)
291 gst_element_set_state (replace->pipeline, GST_STATE_NULL);
293 g_source_remove (replace->timer_id);
297 gst_replace_handle_eos (GstReplace *replace)
299 gst_replace_stop (replace);
303 gst_replace_handle_error (GstReplace *replace, GError *error,
306 g_print ("error: %s\n", error->message);
307 gst_replace_stop (replace);
311 gst_replace_handle_warning (GstReplace *replace, GError *error,
314 g_print ("warning: %s\n", error->message);
318 gst_replace_handle_info (GstReplace *replace, GError *error,
321 g_print ("info: %s\n", error->message);
325 gst_replace_handle_null_to_ready (GstReplace *replace)
327 gst_element_set_state (replace->pipeline, GST_STATE_PAUSED);
332 gst_replace_handle_ready_to_paused (GstReplace *replace)
334 if (!replace->paused_for_buffering) {
335 gst_element_set_state (replace->pipeline, GST_STATE_PLAYING);
340 gst_replace_handle_paused_to_playing (GstReplace *replace)
346 gst_replace_handle_playing_to_paused (GstReplace *replace)
352 gst_replace_handle_paused_to_ready (GstReplace *replace)
358 gst_replace_handle_ready_to_null (GstReplace *replace)
360 g_main_loop_quit (replace->main_loop);
366 gst_replace_handle_message (GstBus *bus, GstMessage *message,
369 GstReplace *replace = (GstReplace *) data;
371 switch (GST_MESSAGE_TYPE(message)) {
372 case GST_MESSAGE_EOS:
373 gst_replace_handle_eos (replace);
375 case GST_MESSAGE_ERROR:
377 GError *error = NULL;
380 gst_message_parse_error (message, &error, &debug);
381 gst_replace_handle_error (replace, error, debug);
384 case GST_MESSAGE_WARNING:
386 GError *error = NULL;
389 gst_message_parse_warning (message, &error, &debug);
390 gst_replace_handle_warning (replace, error, debug);
393 case GST_MESSAGE_INFO:
395 GError *error = NULL;
398 gst_message_parse_info (message, &error, &debug);
399 gst_replace_handle_info (replace, error, debug);
402 case GST_MESSAGE_TAG:
404 GstTagList *tag_list;
406 gst_message_parse_tag (message, &tag_list);
407 if (verbose) g_print("tag\n");
410 case GST_MESSAGE_STATE_CHANGED:
412 GstState oldstate, newstate, pending;
414 gst_message_parse_state_changed (message, &oldstate, &newstate,
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);
424 case GST_STATE_CHANGE_READY_TO_PAUSED:
425 gst_replace_handle_ready_to_paused (replace);
427 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
428 gst_replace_handle_paused_to_playing (replace);
430 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
431 gst_replace_handle_playing_to_paused (replace);
433 case GST_STATE_CHANGE_PAUSED_TO_READY:
434 gst_replace_handle_paused_to_ready (replace);
436 case GST_STATE_CHANGE_READY_TO_NULL:
437 gst_replace_handle_ready_to_null (replace);
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));
447 case GST_MESSAGE_BUFFERING:
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);
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:
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:
484 g_print ("message: %s\n", GST_MESSAGE_TYPE_NAME (message));
495 onesecond_timer (gpointer priv)
497 //GstReplace *replace = (GstReplace *)priv;
506 /* helper functions */
510 have_element (const gchar *element_name)
512 GstPluginFeature *feature;
514 feature = gst_default_registry_find_feature (element_name,
515 GST_TYPE_ELEMENT_FACTORY);
517 g_object_unref (feature);
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
540 gst-indent $gstreplace.c
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)