1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/gmyth-stream/gmencoder/tests/main.c Thu Jan 31 20:02:39 2008 +0000
1.3 @@ -0,0 +1,230 @@
1.4 +#include <sys/stat.h>
1.5 +#include <fcntl.h>
1.6 +#include <unistd.h>
1.7 +#include <string.h>
1.8 +
1.9 +#include <gst/gst.h>
1.10 +#include <glib.h>
1.11 +
1.12 +
1.13 +static GMainLoop *mainloop = NULL;
1.14 +static gint64 d = 0;
1.15 +static gint64 gap = 10;
1.16 +
1.17 +typedef enum {
1.18 + MY_STREAM_TYPE_AUDIO = 0,
1.19 + MY_STREAM_TYPE_VIDEO = 1
1.20 +} MyStreamType;
1.21 +
1.22 +typedef struct _StreamData StreamData;
1.23 +struct _StreamData {
1.24 + GstElement *bin;
1.25 + MyStreamType type;
1.26 +};
1.27 +
1.28 +static void
1.29 +_stream_decode_pad_added_cb(GstElement * decode,
1.30 + GstPad * pad, gboolean arg1,
1.31 + gpointer user_data)
1.32 +{
1.33 + StreamData *data = (StreamData *) user_data;
1.34 + GstElement *queue;
1.35 + GstPad *sink_pad;
1.36 + GstCaps *caps = gst_pad_get_caps(pad);
1.37 + gchar *str_caps = gst_caps_to_string(caps);
1.38 +
1.39 + g_debug("decode caps: [%d] [%s]", data->type, str_caps);
1.40 +
1.41 + switch (data->type) {
1.42 + case MY_STREAM_TYPE_AUDIO:
1.43 + g_debug("Audio");
1.44 + if (strstr(str_caps, "audio") == NULL)
1.45 + goto done;
1.46 + break;
1.47 + case MY_STREAM_TYPE_VIDEO:
1.48 + g_debug("Video");
1.49 + if (strstr(str_caps, "video") == NULL)
1.50 + goto done;
1.51 + break;
1.52 + }
1.53 +
1.54 + queue = gst_bin_get_by_name(GST_BIN(data->bin), "queue");
1.55 + sink_pad = gst_element_get_pad(queue, "sink");
1.56 +
1.57 + if (gst_pad_link(pad, sink_pad) != GST_PAD_LINK_OK) {
1.58 + g_warning("Failed to link decode");
1.59 + }
1.60 +
1.61 + gst_object_unref(queue);
1.62 + gst_object_unref(sink_pad);
1.63 + // g_free (data);
1.64 + g_debug("Linked");
1.65 +
1.66 + done:
1.67 + gst_caps_unref(caps);
1.68 + g_free(str_caps);
1.69 +}
1.70 +
1.71 +
1.72 +static GstElement *
1.73 +_create_src_element(const gchar * name,
1.74 + const gchar * uri, MyStreamType type, guint priority)
1.75 +{
1.76 + StreamData *data;
1.77 + GstElement *bin;
1.78 + GstElement *src;
1.79 + GstElement *decode;
1.80 + GstElement *queue;
1.81 + GstPad *src_pad;
1.82 +
1.83 + GstElement *gnl_src;
1.84 +
1.85 + g_debug("element from uri: %s", uri);
1.86 +
1.87 + bin = gst_bin_new("bin");
1.88 + src = gst_element_make_from_uri(GST_URI_SRC, uri, "src");
1.89 + g_return_val_if_fail(src != NULL, NULL);
1.90 +
1.91 + decode = gst_element_factory_make("decodebin", NULL);
1.92 + g_return_val_if_fail(decode != NULL, NULL);
1.93 +
1.94 + queue = gst_element_factory_make("queue", "queue");
1.95 + g_return_val_if_fail(queue != NULL, NULL);
1.96 +
1.97 + gst_bin_add_many(GST_BIN(bin), src, decode, queue, NULL);
1.98 + gst_element_link(src, decode);
1.99 +
1.100 + data = g_new0(StreamData, 1);
1.101 + data->bin = bin;
1.102 + data->type = type;
1.103 + g_debug("Type : %d = %d", type, data->type);
1.104 +
1.105 + g_signal_connect(G_OBJECT(decode), "new-decoded-pad",
1.106 + G_CALLBACK(_stream_decode_pad_added_cb), data);
1.107 +
1.108 +
1.109 + src_pad = gst_element_get_pad(queue, "src");
1.110 + g_return_val_if_fail(src_pad != NULL, NULL);
1.111 +
1.112 + gst_element_add_pad(bin, gst_ghost_pad_new("src", src_pad));
1.113 +
1.114 + gst_object_unref(src_pad);
1.115 +
1.116 + gnl_src = gst_element_factory_make("gnlsource", name);
1.117 + g_return_val_if_fail(gnl_src != NULL, NULL);
1.118 + gst_bin_add(GST_BIN(gnl_src), bin);
1.119 +
1.120 + g_debug("ADDING WITH: START [%lli] DUR [%lli]", d, gap);
1.121 + if (d == 0) {
1.122 + g_object_set(G_OBJECT(gnl_src),
1.123 + // "start", 0L,
1.124 + "duration", 10 * GST_SECOND,
1.125 + // "media-start", 0L,
1.126 + // "media-duration", 10 * GST_SECOND,
1.127 + "priority", priority, NULL);
1.128 +
1.129 + } else {
1.130 + g_object_set(G_OBJECT(gnl_src),
1.131 + "start", 10 * GST_SECOND, "duration", 10 * GST_SECOND,
1.132 + // /"media-start", 10 * GST_SECOND,
1.133 + // "media-duration", 10 * GST_SECOND,
1.134 + "priority", priority, NULL);
1.135 +
1.136 + }
1.137 + d++;
1.138 +
1.139 + return gnl_src;
1.140 +}
1.141 +
1.142 +static void
1.143 +_composition_pad_added_cb(GstElement * composition,
1.144 + GstPad * pad, gpointer data)
1.145 +{
1.146 + GstPad *sink_pad =
1.147 + gst_element_get_pad(GST_ELEMENT(data), "sink");
1.148 + g_debug("compose pad added");
1.149 +
1.150 + if (gst_pad_link(pad, sink_pad) != GST_PAD_LINK_OK) {
1.151 + g_warning("Failed to link decode");
1.152 + }
1.153 +
1.154 + g_debug("Linked ok");
1.155 +}
1.156 +
1.157 +static void
1.158 +_compose_add_file(GstElement * compose,
1.159 + const gchar * e_name,
1.160 + const gchar * uri, MyStreamType type, guint priority)
1.161 +{
1.162 + GstElement *src;
1.163 +
1.164 + src = _create_src_element(e_name, uri, type, priority);
1.165 + gst_bin_add(GST_BIN(compose), src);
1.166 +}
1.167 +
1.168 +
1.169 +int
1.170 +main(int argc, char **argv)
1.171 +{
1.172 + GstElement *pipe;
1.173 + GstElement *gnl_compose_a;
1.174 + GstElement *gnl_compose_v;
1.175 + GstElement *asink;
1.176 + GstElement *vsink;
1.177 + GstElement *aqueue;
1.178 + GstElement *vqueue;
1.179 +
1.180 + g_type_init();
1.181 + gst_init(&argc, &argv);
1.182 +
1.183 + mainloop = g_main_loop_new(NULL, FALSE);
1.184 +
1.185 + pipe = gst_pipeline_new("test_pipeline");
1.186 +
1.187 + gnl_compose_a = gst_element_factory_make("gnlcomposition", "acompose");
1.188 + g_return_val_if_fail(gnl_compose_a != NULL, 1);
1.189 +
1.190 + gnl_compose_v = gst_element_factory_make("gnlcomposition", "vcompose");
1.191 + g_return_val_if_fail(gnl_compose_v != NULL, 1);
1.192 +
1.193 +
1.194 + // _compose_add_file (gnl_compose_a, "src0", argv[1],
1.195 + // MY_STREAM_TYPE_AUDIO, 1);
1.196 + // _compose_add_file (gnl_compose_a, "src1", argv[2],
1.197 + // MY_STREAM_TYPE_AUDIO, 1);
1.198 +
1.199 + d = 0;
1.200 +
1.201 + _compose_add_file(gnl_compose_v, "src2", argv[1], MY_STREAM_TYPE_VIDEO,
1.202 + 1);
1.203 + _compose_add_file(gnl_compose_v, "src3", argv[2], MY_STREAM_TYPE_VIDEO,
1.204 + 1);
1.205 +
1.206 +
1.207 + // aqueue = gst_element_factory_make ("queue", "aqueue");
1.208 + // asink = gst_element_factory_make ("alsasink", "asink");
1.209 +
1.210 + vqueue = gst_element_factory_make("queue", "vqueue");
1.211 + vsink = gst_element_factory_make("xvimagesink", "vsink");
1.212 +
1.213 + gst_bin_add_many(GST_BIN(pipe), gnl_compose_a, gnl_compose_v, vqueue,
1.214 + vsink,
1.215 + // aqueue, asink,
1.216 + NULL);
1.217 +
1.218 + gst_element_link(vqueue, vsink);
1.219 + // gst_element_link (aqueue, asink);
1.220 +
1.221 + // g_signal_connect (G_OBJECT (gnl_compose_a), "pad-added",
1.222 + // G_CALLBACK (_composition_pad_added_cb), aqueue);
1.223 +
1.224 + g_signal_connect(G_OBJECT(gnl_compose_v), "pad-added",
1.225 + G_CALLBACK(_composition_pad_added_cb), vqueue);
1.226 +
1.227 +
1.228 + // g_idle_add (_play, pipe);
1.229 + gst_element_set_state(GST_ELEMENT(pipe), GST_STATE_PLAYING);
1.230 + g_main_loop_run(mainloop);
1.231 +
1.232 + return 0;
1.233 +}