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