Blame tests/icles/playback/test.c

Packit 971217
/* GStreamer
Packit 971217
 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
Packit 971217
 * Copyright (C) <2007> Wim Taymans <wim.taymans@gmail.com>
Packit 971217
 *
Packit 971217
 * This library is free software; you can redistribute it and/or
Packit 971217
 * modify it under the terms of the GNU Library General Public
Packit 971217
 * License as published by the Free Software Foundation; either
Packit 971217
 * version 2 of the License, or (at your option) any later version.
Packit 971217
 *
Packit 971217
 * This library is distributed in the hope that it will be useful,
Packit 971217
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 971217
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 971217
 * Library General Public License for more details.
Packit 971217
 *
Packit 971217
 * You should have received a copy of the GNU Library General Public
Packit 971217
 * License along with this library; if not, write to the
Packit 971217
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
Packit 971217
 * Boston, MA 02110-1301, USA.
Packit 971217
 */
Packit 971217
#ifdef HAVE_CONFIG_H
Packit 971217
#include "config.h"
Packit 971217
#endif
Packit 971217
#include <gst/gst.h>
Packit 971217
Packit 971217
#include <stdlib.h>
Packit 971217
Packit 971217
static GMainLoop *loop;
Packit 971217
Packit 971217
static GstElement *
Packit 971217
gen_video_element (void)
Packit 971217
{
Packit 971217
  GstElement *element;
Packit 971217
  GstElement *conv;
Packit 971217
  GstElement *sink;
Packit 971217
  GstPad *pad;
Packit 971217
Packit 971217
  element = gst_bin_new ("vbin");
Packit 971217
  conv = gst_element_factory_make ("videoconvert", "conv");
Packit 971217
  sink = gst_element_factory_make (DEFAULT_VIDEOSINK, "sink");
Packit 971217
  g_assert (sink);
Packit 971217
Packit 971217
  gst_bin_add (GST_BIN (element), conv);
Packit 971217
  gst_bin_add (GST_BIN (element), sink);
Packit 971217
  gst_element_link_pads (conv, "src", sink, "sink");
Packit 971217
Packit 971217
  pad = gst_element_get_static_pad (conv, "sink");
Packit 971217
  gst_element_add_pad (element, gst_ghost_pad_new ("sink", pad));
Packit 971217
  gst_object_unref (pad);
Packit 971217
Packit 971217
  return element;
Packit 971217
}
Packit 971217
Packit 971217
static GstElement *
Packit 971217
gen_audio_element (void)
Packit 971217
{
Packit 971217
  GstElement *element;
Packit 971217
  GstElement *conv;
Packit 971217
  GstElement *sink;
Packit 971217
  GstPad *pad;
Packit 971217
Packit 971217
  element = gst_bin_new ("abin");
Packit 971217
  conv = gst_element_factory_make ("audioconvert", "conv");
Packit 971217
  sink = gst_element_factory_make (DEFAULT_AUDIOSINK, "sink");
Packit 971217
  g_assert (sink);
Packit 971217
Packit 971217
  gst_bin_add (GST_BIN (element), conv);
Packit 971217
  gst_bin_add (GST_BIN (element), sink);
Packit 971217
  gst_element_link_pads (conv, "src", sink, "sink");
Packit 971217
Packit 971217
  pad = gst_element_get_static_pad (conv, "sink");
Packit 971217
  gst_element_add_pad (element, gst_ghost_pad_new ("sink", pad));
Packit 971217
  gst_object_unref (pad);
Packit 971217
Packit 971217
  return element;
Packit 971217
}
Packit 971217
Packit 971217
static void
Packit 971217
pad_added_cb (GstElement * decodebin, GstPad * pad, gpointer data)
Packit 971217
{
Packit 971217
  GstCaps *caps;
Packit 971217
  GstStructure *str;
Packit 971217
  GstPad *sinkpad;
Packit 971217
  GstElement *sink;
Packit 971217
  GstElement *pipeline;
Packit 971217
  const gchar *name;
Packit 971217
  GstStateChangeReturn ret;
Packit 971217
  GstPadLinkReturn lret;
Packit 971217
Packit 971217
  /* check media type */
Packit 971217
  caps = gst_pad_query_caps (pad, NULL);
Packit 971217
  str = gst_caps_get_structure (caps, 0);
Packit 971217
Packit 971217
  name = gst_structure_get_name (str);
Packit 971217
  g_print ("name: %s\n", name);
Packit 971217
Packit 971217
  if (g_strrstr (name, "audio")) {
Packit 971217
    sink = gen_audio_element ();
Packit 971217
  } else if (g_strrstr (name, "video")) {
Packit 971217
    sink = gen_video_element ();
Packit 971217
  } else {
Packit 971217
    sink = NULL;
Packit 971217
  }
Packit 971217
  gst_caps_unref (caps);
Packit 971217
Packit 971217
  if (sink) {
Packit 971217
    pipeline = GST_ELEMENT_CAST (data);
Packit 971217
Packit 971217
    /* add new sink to the pipeline */
Packit 971217
    gst_bin_add (GST_BIN_CAST (pipeline), sink);
Packit 971217
Packit 971217
    /* set the new sink tp PAUSED as well */
Packit 971217
    ret = gst_element_set_state (sink, GST_STATE_PAUSED);
Packit 971217
    if (ret == GST_STATE_CHANGE_FAILURE)
Packit 971217
      goto state_error;
Packit 971217
Packit 971217
    /* get the ghostpad of the sink bin */
Packit 971217
    sinkpad = gst_element_get_static_pad (sink, "sink");
Packit 971217
Packit 971217
    /* link'n'play */
Packit 971217
    lret = gst_pad_link (pad, sinkpad);
Packit 971217
    if (lret != GST_PAD_LINK_OK)
Packit 971217
      goto link_failed;
Packit 971217
Packit 971217
    gst_object_unref (sinkpad);
Packit 971217
  }
Packit 971217
  return;
Packit 971217
Packit 971217
  /* ERRORS */
Packit 971217
state_error:
Packit 971217
  {
Packit 971217
    gst_bin_remove (GST_BIN_CAST (pipeline), sink);
Packit 971217
    g_warning ("could not change state of new sink (%d)", ret);
Packit 971217
    return;
Packit 971217
  }
Packit 971217
link_failed:
Packit 971217
  {
Packit 971217
    g_warning ("could not link pad and sink (%d)", lret);
Packit 971217
    return;
Packit 971217
  }
Packit 971217
}
Packit 971217
Packit 971217
static void
Packit 971217
error_eos_cb (GstBus * bus, GstMessage * msg, GMainLoop * main_loop)
Packit 971217
{
Packit 971217
  g_main_loop_quit (main_loop);
Packit 971217
}
Packit 971217
Packit 971217
gint
Packit 971217
main (gint argc, gchar * argv[])
Packit 971217
{
Packit 971217
  GstElement *pipeline, *filesrc, *decodebin;
Packit 971217
  GstStateChangeReturn res;
Packit 971217
  GstBus *bus;
Packit 971217
Packit 971217
  gst_init (&argc, &argv);
Packit 971217
Packit 971217
  pipeline = gst_pipeline_new ("pipeline");
Packit 971217
Packit 971217
  filesrc = gst_element_factory_make ("filesrc", "filesrc");
Packit 971217
  g_assert (filesrc);
Packit 971217
  decodebin = gst_element_factory_make ("decodebin", "decodebin");
Packit 971217
  g_assert (decodebin);
Packit 971217
Packit 971217
  loop = g_main_loop_new (NULL, TRUE);
Packit 971217
  bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
Packit 971217
  gst_bus_add_signal_watch (bus);
Packit 971217
Packit 971217
  g_signal_connect (bus, "message::eos", G_CALLBACK (error_eos_cb), loop);
Packit 971217
  g_signal_connect (bus, "message::error", G_CALLBACK (error_eos_cb), loop);
Packit 971217
Packit 971217
  g_signal_connect (G_OBJECT (decodebin), "pad-added",
Packit 971217
      G_CALLBACK (pad_added_cb), pipeline);
Packit 971217
Packit 971217
  gst_bin_add_many (GST_BIN (pipeline), filesrc, decodebin, NULL);
Packit 971217
  gst_element_link (filesrc, decodebin);
Packit 971217
Packit 971217
  if (argc < 2) {
Packit 971217
    g_print ("usage: %s <uri>\n", argv[0]);
Packit 971217
    exit (-1);
Packit 971217
  }
Packit 971217
Packit 971217
  if (!g_str_has_prefix (argv[1], "file://")) {
Packit 971217
    g_object_set (G_OBJECT (filesrc), "location", argv[1], NULL);
Packit 971217
  } else {
Packit 971217
    g_object_set (G_OBJECT (filesrc), "location", argv[1] + 7, NULL);
Packit 971217
  }
Packit 971217
Packit 971217
  /* set to paused, decodebin will autoplug and signal new_pad callbacks */
Packit 971217
  res = gst_element_set_state (pipeline, GST_STATE_PAUSED);
Packit 971217
  if (res == GST_STATE_CHANGE_FAILURE) {
Packit 971217
    g_print ("could not pause\n");
Packit 971217
    return -1;
Packit 971217
  }
Packit 971217
  /* wait for paused to complete */
Packit 971217
  res = gst_element_get_state (pipeline, NULL, NULL, GST_CLOCK_TIME_NONE);
Packit 971217
  if (res == GST_STATE_CHANGE_FAILURE) {
Packit 971217
    g_print ("could not pause\n");
Packit 971217
    return -1;
Packit 971217
  }
Packit 971217
Packit 971217
  /* play, now all the sinks are added to the pipeline and are prerolled and
Packit 971217
   * ready to play. */
Packit 971217
  res = gst_element_set_state (pipeline, GST_STATE_PLAYING);
Packit 971217
  if (res == GST_STATE_CHANGE_FAILURE) {
Packit 971217
    g_print ("could not play\n");
Packit 971217
    return -1;
Packit 971217
  }
Packit 971217
Packit 971217
  /* go in the mainloop now */
Packit 971217
  g_main_loop_run (loop);
Packit 971217
Packit 971217
  return 0;
Packit 971217
}