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