1 #include <glib.h>
2 #include <gst/gst.h>
4 typedef struct _launch_delayed_pad launch_delayed_pad;
5 struct _launch_delayed_pad {
6 gchar *name;
7 GstPad *peer;
8 };
10 void launch_newpad(GstElement *element,GstPad *pad,launch_delayed_pad *peer) {
11 gst_info("have NEW_PAD signal\n");
12 // if it matches, connect it
13 if (!strcmp(gst_pad_get_name(pad),peer->name)) {
14 gst_pad_connect(pad,peer->peer);
15 gst_info("delayed connect of '%s' to '%s'\n",
16 gst_pad_get_name(pad),gst_pad_get_name(peer->peer));
17 }
18 }
20 gint parse(int argc,char *argv[],GstElement *parent,gint offset,gchar endchar) {
21 gint i = offset;
22 gchar *plugin;
23 GstElement *element, *prevelement;
24 GstPad *prevpad,*nextpad;
25 gchar *prevpadname = NULL,*nextpadname = NULL;
26 gchar *ptr;
27 gint len;
28 launch_delayed_pad *delayed;
30 gst_info("at offset %d, argc is %d\n",i,argc);
32 // loop through all the arguments
33 while (i < argc) {
34 // first is the plugin name
35 plugin = argv[i++];
36 gst_info("plugin is \"%s\"\n",plugin);
37 // record previous element
38 prevelement = element;
39 // create the element and add it to the parent
40 element = gst_elementfactory_make(plugin,plugin);
41 gst_bin_add(GST_BIN(parent),element);
42 // connect it to the previous if there is one
43 if (nextpadname != NULL) {
44 // grab the pad of this element
45 nextpad = gst_element_get_pad(element,nextpadname);
46 g_return_if_fail(nextpad != NULL);
47 // check to see if the pad exists yet, connect it if it does
48 if (prevpad != NULL) {
49 gst_pad_connect(prevpad,nextpad);
50 gst_info("wired '%s' to '%s'\n",
51 gst_pad_get_name(prevpad),gst_pad_get_name(nextpad));
52 }
53 // otherwise we have to attach and wait for it to show
54 else {
55 delayed = g_new0(launch_delayed_pad,1);
56 delayed->name = prevpadname;
57 delayed->peer = nextpad;
58 gtk_signal_connect(GTK_OBJECT(prevelement),"new_pad",
59 launch_newpad,delayed);
60 }
61 }
62 // then come all the other things
63 while (i < argc) {
64 // snag the length in advance;
65 len = strlen(argv[i]);
66 // if it's just a connection, pick the 'src' pad and move on
67 if (ptr = strchr(argv[i],'|')) {
68 // if there's a previous pad name
69 if (ptr != argv[i]) {
70 ptr[0] = '\0';
71 prevpadname = argv[i];
72 prevpad = gst_element_get_pad(element,prevpadname);
73 } else
74 prevpad = gst_element_get_pad(element,"src");
75 // if there's a next pad name
76 if (((ptr - argv[i]) + 1) < len) {
77 nextpadname = ptr + 1;
78 } else
79 nextpadname = "sink";
80 i++;
81 break;
82 } else {
83 gst_info("have unknown argument '%s'\n",argv[i]);
84 gtk_object_set(GTK_OBJECT(element),"location",argv[i],NULL);
85 i++;
86 }
87 }
88 }
89 }
91 int main(int argc,char *argv[]) {
92 int t;
93 GstElement *pipeline;
95 gst_init(&argc,&argv);
96 gst_plugin_load_all();
98 gst_info("\n\n");
100 pipeline = gst_elementfactory_make("thread","launch");
101 if (t = atoi(argv[1]))
102 parse(argc,argv,pipeline,2,0);
103 else
104 parse(argc,argv,pipeline,1,0);
106 xmlSaveFile("launch.xml",gst_xml_write(pipeline));
108 gst_element_set_state(pipeline,GST_STATE_RUNNING);
109 gst_element_set_state(pipeline,GST_STATE_PLAYING);
111 if (t)
112 sleep(t);
113 else
114 sleep(5);
115 }