gmyth-stream/gmemcoder/tests/main.c
author renatofilho
Tue May 01 22:19:50 2007 +0100 (2007-05-01)
branchtrunk
changeset 615 4fda44a11929
child 752 b7f71ba285da
permissions -rw-r--r--
[svn r621] added test file
     1 #include <sys/stat.h>
     2 #include <fcntl.h>
     3 #include <unistd.h>
     4 #include <string.h>
     5 
     6 #include <gst/gst.h>
     7 #include <glib.h> 
     8 
     9 
    10 static GMainLoop *mainloop = NULL;
    11 static gint64 d = 0;
    12 static gint64 gap = 10;
    13 
    14 typedef enum {
    15     MY_STREAM_TYPE_AUDIO = 0,
    16     MY_STREAM_TYPE_VIDEO = 1
    17 } MyStreamType;
    18 
    19 typedef struct _StreamData StreamData;
    20 struct _StreamData {
    21     GstElement *bin;
    22     MyStreamType type;
    23 };
    24 
    25 static void
    26 _stream_decode_pad_added_cb (GstElement *decode, 
    27                             GstPad *pad,
    28                             gboolean arg1,
    29                             gpointer user_data)
    30 {
    31     StreamData *data = (StreamData *) user_data;
    32     GstElement *queue;
    33     GstPad* sink_pad;
    34     GstCaps* caps = gst_pad_get_caps (pad);
    35     gchar *str_caps = gst_caps_to_string (caps);
    36 
    37     g_debug ("decode caps: [%d] [%s]", data->type, str_caps);
    38 
    39     switch (data->type)
    40     {
    41         case MY_STREAM_TYPE_AUDIO:
    42             g_debug ("Audio");
    43             if (strstr (str_caps, "audio") == NULL) 
    44                 goto done;
    45             break;
    46         case MY_STREAM_TYPE_VIDEO:
    47             g_debug ("Video");
    48             if (strstr (str_caps, "video") == NULL)
    49                 goto done;
    50             break;
    51     }
    52 
    53     queue = gst_bin_get_by_name (GST_BIN (data->bin), "queue");
    54     sink_pad = gst_element_get_pad (queue, "sink");
    55 
    56     if (gst_pad_link (pad, sink_pad) != GST_PAD_LINK_OK) {
    57         g_warning ("Failed to link decode");
    58     }
    59 
    60     gst_object_unref (queue);
    61     gst_object_unref (sink_pad);
    62     //g_free (data);
    63     g_debug ("Linked");
    64 
    65 done:
    66     gst_caps_unref (caps);
    67     g_free (str_caps);
    68 }
    69 
    70 
    71 static GstElement*
    72 _create_src_element (const gchar* name,
    73                      const gchar* uri,                 
    74                      MyStreamType type,
    75                      guint priority)
    76 {
    77     StreamData *data;
    78     GstElement *bin;
    79     GstElement *src;
    80     GstElement *decode;
    81     GstElement *queue;
    82     GstPad *src_pad;
    83 
    84     GstElement *gnl_src;
    85 
    86     g_debug ("element from uri: %s", uri);
    87 
    88     bin = gst_bin_new ("bin");
    89     src = gst_element_make_from_uri (GST_URI_SRC, uri, "src");
    90     g_return_val_if_fail (src != NULL, NULL);
    91 
    92     decode = gst_element_factory_make ("decodebin", NULL);
    93     g_return_val_if_fail (decode != NULL, NULL);
    94 
    95     queue = gst_element_factory_make ("queue", "queue");
    96     g_return_val_if_fail (queue != NULL, NULL);
    97 
    98     gst_bin_add_many (GST_BIN (bin), src, decode, queue, NULL);
    99     gst_element_link (src, decode);
   100 
   101     data = g_new0 (StreamData, 1);
   102     data->bin = bin;
   103     data->type = type;
   104     g_debug ("Type : %d = %d", type, data->type);
   105 
   106     g_signal_connect (G_OBJECT (decode), "new-decoded-pad",
   107                       G_CALLBACK (_stream_decode_pad_added_cb), 
   108                       data);
   109 
   110     
   111     src_pad = gst_element_get_pad (queue, "src");
   112     g_return_val_if_fail (src_pad != NULL, NULL);
   113 
   114     gst_element_add_pad (bin,
   115         gst_ghost_pad_new ("src", src_pad));
   116 
   117     gst_object_unref (src_pad);
   118 
   119     gnl_src = gst_element_factory_make ("gnlsource", name);
   120     g_return_val_if_fail (gnl_src != NULL, NULL);
   121     gst_bin_add (GST_BIN (gnl_src), bin);
   122 
   123     g_debug ("ADDING WITH: START [%lli] DUR [%lli]", d, gap);
   124     if (d == 0) {
   125         g_object_set (G_OBJECT (gnl_src),
   126                         //"start", 0L,
   127                         "duration", 10 * GST_SECOND,
   128                         //"media-start", 0L,
   129                         //"media-duration", 10 * GST_SECOND,
   130                         "priority", priority,
   131                         NULL);
   132 
   133     } else {
   134         g_object_set (G_OBJECT (gnl_src),
   135                         "start", 10 * GST_SECOND,
   136                         "duration", 10 * GST_SECOND,
   137                         ///"media-start", 10 * GST_SECOND,
   138                         //"media-duration", 10 * GST_SECOND,
   139                         "priority", priority,
   140                         NULL);
   141 
   142     }
   143     d++;
   144 
   145     return gnl_src;
   146 }
   147 
   148 static void
   149 _composition_pad_added_cb (GstElement *composition, 
   150                            GstPad *pad,
   151                            gpointer data)
   152 {
   153     GstPad *sink_pad = gst_element_get_pad (GST_ELEMENT (data), "sink");
   154     g_debug ("compose pad added");
   155 
   156     if (gst_pad_link (pad, sink_pad) != GST_PAD_LINK_OK) {
   157         g_warning ("Failed to link decode");
   158     }
   159 
   160     g_debug ("Linked ok");
   161 }
   162 
   163 static void
   164 _compose_add_file (GstElement *compose,
   165                    const gchar* e_name,
   166                    const gchar* uri,
   167                    MyStreamType type, 
   168                    guint priority)
   169 {
   170     GstElement *src;
   171 
   172     src = _create_src_element (e_name, uri, type, priority);
   173     gst_bin_add (GST_BIN (compose), src);
   174 }
   175 
   176 
   177 int 
   178 main (int argc, char** argv)
   179 {
   180     GstElement* pipe;
   181     GstElement* gnl_compose_a;
   182     GstElement* gnl_compose_v;
   183     GstElement* asink;
   184     GstElement* vsink;
   185     GstElement* aqueue;
   186     GstElement* vqueue;
   187 
   188     g_type_init ();
   189     gst_init (&argc, &argv);
   190 
   191 	mainloop = g_main_loop_new (NULL, FALSE);
   192 
   193     pipe = gst_pipeline_new ("test_pipeline");
   194 
   195     gnl_compose_a = gst_element_factory_make ("gnlcomposition", "acompose");
   196     g_return_val_if_fail (gnl_compose_a != NULL, 1);
   197 
   198     gnl_compose_v = gst_element_factory_make ("gnlcomposition", "vcompose");
   199     g_return_val_if_fail (gnl_compose_v != NULL, 1);
   200 
   201 
   202     //_compose_add_file (gnl_compose_a, "src0", argv[1], MY_STREAM_TYPE_AUDIO, 1);
   203     //_compose_add_file (gnl_compose_a, "src1", argv[2], MY_STREAM_TYPE_AUDIO, 1);
   204 
   205     d = 0;
   206 
   207     _compose_add_file (gnl_compose_v, "src2", argv[1], MY_STREAM_TYPE_VIDEO, 1);
   208     _compose_add_file (gnl_compose_v, "src3", argv[2], MY_STREAM_TYPE_VIDEO, 1);
   209 
   210 
   211     //aqueue = gst_element_factory_make ("queue", "aqueue");
   212     //asink = gst_element_factory_make ("alsasink", "asink");
   213 
   214     vqueue = gst_element_factory_make ("queue", "vqueue");
   215     vsink = gst_element_factory_make ("xvimagesink", "vsink");
   216 
   217     gst_bin_add_many (GST_BIN (pipe), gnl_compose_a, gnl_compose_v, 
   218             vqueue, vsink, 
   219             //aqueue, asink, 
   220             NULL);
   221 
   222     gst_element_link (vqueue, vsink);
   223     //gst_element_link (aqueue, asink);
   224 
   225     //g_signal_connect (G_OBJECT (gnl_compose_a), "pad-added",
   226     //                  G_CALLBACK (_composition_pad_added_cb), aqueue);
   227 
   228     g_signal_connect (G_OBJECT (gnl_compose_v), "pad-added",
   229                       G_CALLBACK (_composition_pad_added_cb), vqueue);
   230 
   231 
   232     //g_idle_add (_play, pipe);
   233     gst_element_set_state (GST_ELEMENT (pipe), GST_STATE_PLAYING);
   234 	g_main_loop_run (mainloop);
   235 
   236 	return 0;
   237 }