gst_plugins_base/gst/playback/test.c
changeset 0 0e761a78d257
child 8 4a7fac7dd34a
equal deleted inserted replaced
-1:000000000000 0:0e761a78d257
       
     1 /* GStreamer
       
     2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
       
     3  * Copyright (C) <2007> Wim Taymans <wim.taymans@gmail.com>
       
     4  *
       
     5  * This library is free software; you can redistribute it and/or
       
     6  * modify it under the terms of the GNU Library General Public
       
     7  * License as published by the Free Software Foundation; either
       
     8  * version 2 of the License, or (at your option) any later version.
       
     9  *
       
    10  * This library is distributed in the hope that it will be useful,
       
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    13  * Library General Public License for more details.
       
    14  *
       
    15  * You should have received a copy of the GNU Library General Public
       
    16  * License along with this library; if not, write to the
       
    17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
       
    18  * Boston, MA 02111-1307, USA.
       
    19  */
       
    20 #ifdef HAVE_CONFIG_H
       
    21 #include "config.h"
       
    22 #endif
       
    23 #include <gst/gst.h>
       
    24 
       
    25 #include <stdlib.h>
       
    26 
       
    27 static GMainLoop *loop;
       
    28 
       
    29 static GstElement *
       
    30 gen_video_element (void)
       
    31 {
       
    32   GstElement *element;
       
    33   GstElement *conv;
       
    34   GstElement *sink;
       
    35   GstPad *pad;
       
    36 
       
    37   element = gst_bin_new ("vbin");
       
    38   conv = gst_element_factory_make ("ffmpegcolorspace", "conv");
       
    39   sink = gst_element_factory_make (DEFAULT_VIDEOSINK, "sink");
       
    40 
       
    41   gst_bin_add (GST_BIN (element), conv);
       
    42   gst_bin_add (GST_BIN (element), sink);
       
    43   gst_element_link_pads (conv, "src", sink, "sink");
       
    44 
       
    45   pad = gst_element_get_pad (conv, "sink");
       
    46   gst_element_add_pad (element, gst_ghost_pad_new ("sink", pad));
       
    47   gst_object_unref (pad);
       
    48 
       
    49   return element;
       
    50 }
       
    51 
       
    52 static GstElement *
       
    53 gen_audio_element (void)
       
    54 {
       
    55   GstElement *element;
       
    56   GstElement *conv;
       
    57   GstElement *sink;
       
    58   GstPad *pad;
       
    59 
       
    60   element = gst_bin_new ("abin");
       
    61   conv = gst_element_factory_make ("audioconvert", "conv");
       
    62   sink = gst_element_factory_make (DEFAULT_AUDIOSINK, "sink");
       
    63 
       
    64   gst_bin_add (GST_BIN (element), conv);
       
    65   gst_bin_add (GST_BIN (element), sink);
       
    66   gst_element_link_pads (conv, "src", sink, "sink");
       
    67 
       
    68   pad = gst_element_get_pad (conv, "sink");
       
    69   gst_element_add_pad (element, gst_ghost_pad_new ("sink", pad));
       
    70   gst_object_unref (pad);
       
    71 
       
    72   return element;
       
    73 }
       
    74 
       
    75 static void
       
    76 cb_newpad (GstElement * decodebin, GstPad * pad, gboolean last, gpointer data)
       
    77 {
       
    78   GstCaps *caps;
       
    79   GstStructure *str;
       
    80   GstPad *sinkpad;
       
    81   GstElement *sink;
       
    82   GstElement *pipeline;
       
    83   const gchar *name;
       
    84   GstStateChangeReturn ret;
       
    85   GstPadLinkReturn lret;
       
    86 
       
    87   /* check media type */
       
    88   caps = gst_pad_get_caps (pad);
       
    89   str = gst_caps_get_structure (caps, 0);
       
    90 
       
    91   name = gst_structure_get_name (str);
       
    92   g_print ("name: %s\n", name);
       
    93 
       
    94   if (g_strrstr (name, "audio")) {
       
    95     sink = gen_audio_element ();
       
    96   } else if (g_strrstr (name, "video")) {
       
    97     sink = gen_video_element ();
       
    98   } else {
       
    99     sink = NULL;
       
   100   }
       
   101   gst_caps_unref (caps);
       
   102 
       
   103   if (sink) {
       
   104     pipeline = GST_ELEMENT_CAST (data);
       
   105 
       
   106     /* add new sink to the pipeline */
       
   107     gst_bin_add (GST_BIN_CAST (pipeline), sink);
       
   108 
       
   109     /* set the new sink tp PAUSED as well */
       
   110     ret = gst_element_set_state (sink, GST_STATE_PAUSED);
       
   111     if (ret == GST_STATE_CHANGE_FAILURE)
       
   112       goto state_error;
       
   113 
       
   114     /* get the ghostpad of the sink bin */
       
   115     sinkpad = gst_element_get_pad (sink, "sink");
       
   116 
       
   117     /* link'n'play */
       
   118     lret = gst_pad_link (pad, sinkpad);
       
   119     if (lret != GST_PAD_LINK_OK)
       
   120       goto link_failed;
       
   121 
       
   122     gst_object_unref (sinkpad);
       
   123   }
       
   124   return;
       
   125 
       
   126   /* ERRORS */
       
   127 state_error:
       
   128   {
       
   129     gst_bin_remove (GST_BIN_CAST (pipeline), sink);
       
   130     g_warning ("could not change state of new sink (%d)", ret);
       
   131     return;
       
   132   }
       
   133 link_failed:
       
   134   {
       
   135     g_warning ("could not link pad and sink (%d)", lret);
       
   136     return;
       
   137   }
       
   138 }
       
   139 
       
   140 gint
       
   141 main (gint argc, gchar * argv[])
       
   142 {
       
   143   GstElement *pipeline, *filesrc, *decodebin;
       
   144   GstStateChangeReturn res;
       
   145 
       
   146   gst_init (&argc, &argv);
       
   147 
       
   148   pipeline = gst_pipeline_new ("pipeline");
       
   149 
       
   150   filesrc = gst_element_factory_make ("filesrc", "filesrc");
       
   151   g_assert (filesrc);
       
   152   decodebin = gst_element_factory_make ("decodebin", "decodebin");
       
   153   g_assert (decodebin);
       
   154 
       
   155   g_signal_connect (G_OBJECT (decodebin), "new-decoded-pad",
       
   156       G_CALLBACK (cb_newpad), pipeline);
       
   157 
       
   158   gst_bin_add_many (GST_BIN (pipeline), filesrc, decodebin, NULL);
       
   159   gst_element_link (filesrc, decodebin);
       
   160 
       
   161   if (argc < 2) {
       
   162     g_print ("usage: %s <uri>\n", argv[0]);
       
   163     exit (-1);
       
   164   }
       
   165   g_object_set (G_OBJECT (filesrc), "location", argv[1], NULL);
       
   166 
       
   167   /* set to paused, decodebin will autoplug and signal new_pad callbacks */
       
   168   res = gst_element_set_state (pipeline, GST_STATE_PAUSED);
       
   169   if (res == GST_STATE_CHANGE_FAILURE) {
       
   170     g_print ("could not pause\n");
       
   171     return -1;
       
   172   }
       
   173   /* wait for paused to complete */
       
   174   res = gst_element_get_state (pipeline, NULL, NULL, GST_CLOCK_TIME_NONE);
       
   175   if (res == GST_STATE_CHANGE_FAILURE) {
       
   176     g_print ("could not pause\n");
       
   177     return -1;
       
   178   }
       
   179 
       
   180   /* play, now all the sinks are added to the pipeline and are prerolled and
       
   181    * ready to play. */
       
   182   res = gst_element_set_state (pipeline, GST_STATE_PLAYING);
       
   183   if (res == GST_STATE_CHANGE_FAILURE) {
       
   184     g_print ("could not play\n");
       
   185     return -1;
       
   186   }
       
   187 
       
   188   /* go in the mainloop now */
       
   189   loop = g_main_loop_new (NULL, TRUE);
       
   190   g_main_loop_run (loop);
       
   191 
       
   192   return 0;
       
   193 }