gmyth-stream/gmemcoder/tests/main.c
author renatofilho
Thu Jun 14 18:21:53 2007 +0100 (2007-06-14)
branchtrunk
changeset 752 b7f71ba285da
parent 615 4fda44a11929
child 754 cb885ee44618
permissions -rw-r--r--
[svn r758] fixed indent using GNU Style
     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 {
    16   MY_STREAM_TYPE_AUDIO = 0,
    17   MY_STREAM_TYPE_VIDEO = 1
    18 } MyStreamType;
    19 
    20 typedef struct _StreamData StreamData;
    21 struct _StreamData
    22 {
    23   GstElement *bin;
    24   MyStreamType type;
    25 };
    26 
    27 static void
    28 _stream_decode_pad_added_cb(GstElement * decode,
    29 							GstPad * pad, gboolean arg1, 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 	{
    58 	  g_warning("Failed to link decode");
    59 	}
    60 
    61   gst_object_unref(queue);
    62   gst_object_unref(sink_pad);
    63  //g_free (data);
    64   g_debug("Linked");
    65 
    66 done:
    67   gst_caps_unref(caps);
    68   g_free(str_caps);
    69 }
    70 
    71 
    72 static GstElement *
    73 _create_src_element(const gchar * name,
    74 					const gchar * uri, MyStreamType type, guint priority)
    75 {
    76   StreamData *data;
    77   GstElement *bin;
    78   GstElement *src;
    79   GstElement *decode;
    80   GstElement *queue;
    81   GstPad *src_pad;
    82 
    83   GstElement *gnl_src;
    84 
    85   g_debug("element from uri: %s", uri);
    86 
    87   bin = gst_bin_new("bin");
    88   src = gst_element_make_from_uri(GST_URI_SRC, uri, "src");
    89   g_return_val_if_fail(src != NULL, NULL);
    90 
    91   decode = gst_element_factory_make("decodebin", NULL);
    92   g_return_val_if_fail(decode != NULL, NULL);
    93 
    94   queue = gst_element_factory_make("queue", "queue");
    95   g_return_val_if_fail(queue != NULL, NULL);
    96 
    97   gst_bin_add_many(GST_BIN(bin), src, decode, queue, NULL);
    98   gst_element_link(src, decode);
    99 
   100   data = g_new0(StreamData, 1);
   101   data->bin = bin;
   102   data->type = type;
   103   g_debug("Type : %d = %d", type, data->type);
   104 
   105   g_signal_connect(G_OBJECT(decode), "new-decoded-pad",
   106 				   G_CALLBACK(_stream_decode_pad_added_cb), data);
   107 
   108 
   109   src_pad = gst_element_get_pad(queue, "src");
   110   g_return_val_if_fail(src_pad != NULL, NULL);
   111 
   112   gst_element_add_pad(bin, gst_ghost_pad_new("src", src_pad));
   113 
   114   gst_object_unref(src_pad);
   115 
   116   gnl_src = gst_element_factory_make("gnlsource", name);
   117   g_return_val_if_fail(gnl_src != NULL, NULL);
   118   gst_bin_add(GST_BIN(gnl_src), bin);
   119 
   120   g_debug("ADDING WITH: START [%lli] DUR [%lli]", d, gap);
   121   if (d == 0)
   122 	{
   123 	  g_object_set(G_OBJECT(gnl_src),
   124 				  //"start", 0L,
   125 				   "duration", 10 * GST_SECOND,
   126 				  //"media-start", 0L,
   127 				  //"media-duration", 10 * GST_SECOND,
   128 				   "priority", priority, NULL);
   129 
   130 	}
   131   else
   132 	{
   133 	  g_object_set(G_OBJECT(gnl_src),
   134 				   "start", 10 * GST_SECOND, "duration", 10 * GST_SECOND,
   135 				  ///"media-start", 10 * GST_SECOND,
   136 				  //"media-duration", 10 * GST_SECOND,
   137 				   "priority", priority, NULL);
   138 
   139 	}
   140   d++;
   141 
   142   return gnl_src;
   143 }
   144 
   145 static void
   146 _composition_pad_added_cb(GstElement * composition,
   147 						  GstPad * pad, gpointer data)
   148 {
   149   GstPad *sink_pad = gst_element_get_pad(GST_ELEMENT(data), "sink");
   150   g_debug("compose pad added");
   151 
   152   if (gst_pad_link(pad, sink_pad) != GST_PAD_LINK_OK)
   153 	{
   154 	  g_warning("Failed to link decode");
   155 	}
   156 
   157   g_debug("Linked ok");
   158 }
   159 
   160 static void
   161 _compose_add_file(GstElement * compose,
   162 				  const gchar * e_name,
   163 				  const gchar * uri, MyStreamType type, guint priority)
   164 {
   165   GstElement *src;
   166 
   167   src = _create_src_element(e_name, uri, type, priority);
   168   gst_bin_add(GST_BIN(compose), src);
   169 }
   170 
   171 
   172 int
   173 main(int argc, char **argv)
   174 {
   175   GstElement *pipe;
   176   GstElement *gnl_compose_a;
   177   GstElement *gnl_compose_v;
   178   GstElement *asink;
   179   GstElement *vsink;
   180   GstElement *aqueue;
   181   GstElement *vqueue;
   182 
   183   g_type_init();
   184   gst_init(&argc, &argv);
   185 
   186   mainloop = g_main_loop_new(NULL, FALSE);
   187 
   188   pipe = gst_pipeline_new("test_pipeline");
   189 
   190   gnl_compose_a = gst_element_factory_make("gnlcomposition", "acompose");
   191   g_return_val_if_fail(gnl_compose_a != NULL, 1);
   192 
   193   gnl_compose_v = gst_element_factory_make("gnlcomposition", "vcompose");
   194   g_return_val_if_fail(gnl_compose_v != NULL, 1);
   195 
   196 
   197  //_compose_add_file (gnl_compose_a, "src0", argv[1], MY_STREAM_TYPE_AUDIO, 1);
   198  //_compose_add_file (gnl_compose_a, "src1", argv[2], MY_STREAM_TYPE_AUDIO, 1);
   199 
   200   d = 0;
   201 
   202   _compose_add_file(gnl_compose_v, "src2", argv[1], MY_STREAM_TYPE_VIDEO, 1);
   203   _compose_add_file(gnl_compose_v, "src3", argv[2], MY_STREAM_TYPE_VIDEO, 1);
   204 
   205 
   206  //aqueue = gst_element_factory_make ("queue", "aqueue");
   207  //asink = gst_element_factory_make ("alsasink", "asink");
   208 
   209   vqueue = gst_element_factory_make("queue", "vqueue");
   210   vsink = gst_element_factory_make("xvimagesink", "vsink");
   211 
   212   gst_bin_add_many(GST_BIN(pipe), gnl_compose_a, gnl_compose_v, vqueue, vsink,
   213 				  //aqueue, asink, 
   214 				   NULL);
   215 
   216   gst_element_link(vqueue, vsink);
   217  //gst_element_link (aqueue, asink);
   218 
   219  //g_signal_connect (G_OBJECT (gnl_compose_a), "pad-added",
   220  //                  G_CALLBACK (_composition_pad_added_cb), aqueue);
   221 
   222   g_signal_connect(G_OBJECT(gnl_compose_v), "pad-added",
   223 				   G_CALLBACK(_composition_pad_added_cb), vqueue);
   224 
   225 
   226  //g_idle_add (_play, pipe);
   227   gst_element_set_state(GST_ELEMENT(pipe), GST_STATE_PLAYING);
   228   g_main_loop_run(mainloop);
   229 
   230   return 0;
   231 }