Blame tests/icles/playback/decodetest.c

Packit 971217
/* GStreamer
Packit 971217
 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
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
#include <gst/gst.h>
Packit 971217
#include <string.h>
Packit 971217
Packit 971217
static void
Packit 971217
warning_cb (GstBus * bus, GstMessage * msg, gpointer foo)
Packit 971217
{
Packit 971217
  GError *err = NULL;
Packit 971217
  gchar *dbg = NULL;
Packit 971217
Packit 971217
  gst_message_parse_warning (msg, &err, &dbg;;
Packit 971217
Packit 971217
  g_printerr ("WARNING: %s (%s)\n", err->message, (dbg) ? dbg : "no details");
Packit 971217
Packit 971217
  g_error_free (err);
Packit 971217
  g_free (dbg);
Packit 971217
}
Packit 971217
Packit 971217
static void
Packit 971217
error_cb (GstBus * bus, GstMessage * msg, GMainLoop * main_loop)
Packit 971217
{
Packit 971217
  GError *err = NULL;
Packit 971217
  gchar *dbg = NULL;
Packit 971217
Packit 971217
  gst_message_parse_error (msg, &err, &dbg;;
Packit 971217
Packit 971217
  g_printerr ("ERROR: %s (%s)\n", err->message, (dbg) ? dbg : "no details");
Packit 971217
Packit 971217
  g_main_loop_quit (main_loop);
Packit 971217
Packit 971217
  g_error_free (err);
Packit 971217
  g_free (dbg);
Packit 971217
}
Packit 971217
Packit 971217
static void
Packit 971217
eos_cb (GstBus * bus, GstMessage * msg, GMainLoop * main_loop)
Packit 971217
{
Packit 971217
  g_print ("EOS\n");
Packit 971217
  g_main_loop_quit (main_loop);
Packit 971217
}
Packit 971217
Packit 971217
static void
Packit 971217
state_cb (GstBus * bus, GstMessage * msg, GstElement * pipeline)
Packit 971217
{
Packit 971217
  if (msg->src == GST_OBJECT (pipeline)) {
Packit 971217
    GstState old_state, new_state, pending_state;
Packit 971217
Packit 971217
    gst_message_parse_state_changed (msg, &old_state, &new_state,
Packit 971217
        &pending_state);
Packit 971217
    if (new_state == GST_STATE_PLAYING) {
Packit 971217
      g_print ("Decoding ...\n");
Packit 971217
    }
Packit 971217
  }
Packit 971217
}
Packit 971217
Packit 971217
static void
Packit 971217
pad_added_cb (GstElement * decodebin, GstPad * pad, GstElement * pipeline)
Packit 971217
{
Packit 971217
  GstPadLinkReturn ret;
Packit 971217
  GstElement *fakesink;
Packit 971217
  GstPad *fakesink_pad;
Packit 971217
Packit 971217
  fakesink = gst_element_factory_make ("fakesink", NULL);
Packit 971217
  fakesink_pad = gst_element_get_static_pad (fakesink, "sink");
Packit 971217
Packit 971217
  gst_bin_add (GST_BIN (pipeline), fakesink);
Packit 971217
Packit 971217
  /* this doesn't really seem right, but it makes things work for me */
Packit 971217
  gst_element_set_state (fakesink, GST_STATE_PLAYING);
Packit 971217
Packit 971217
  ret = gst_pad_link (pad, fakesink_pad);
Packit 971217
  if (!GST_PAD_LINK_SUCCESSFUL (ret)) {
Packit 971217
    g_printerr ("Failed to link %s:%s to %s:%s (ret = %d)\n",
Packit 971217
        GST_DEBUG_PAD_NAME (pad), GST_DEBUG_PAD_NAME (fakesink_pad), ret);
Packit 971217
  } else {
Packit 971217
    g_printerr ("Linked %s:%s to %s:%s\n", GST_DEBUG_PAD_NAME (pad),
Packit 971217
        GST_DEBUG_PAD_NAME (fakesink_pad));
Packit 971217
  }
Packit 971217
Packit 971217
  gst_object_unref (fakesink_pad);
Packit 971217
}
Packit 971217
Packit 971217
gint
Packit 971217
main (gint argc, gchar * argv[])
Packit 971217
{
Packit 971217
  GstElement *decoder;
Packit 971217
  GstElement *source;
Packit 971217
  GstElement *pipeline;
Packit 971217
  GstStateChangeReturn res;
Packit 971217
  GMainLoop *loop;
Packit 971217
  GstBus *bus;
Packit 971217
Packit 971217
  gst_init (&argc, &argv);
Packit 971217
Packit 971217
  if (argc != 2) {
Packit 971217
    g_printerr ("Decode file from start to end.\n");
Packit 971217
    g_printerr ("Usage: %s URI\n\n", argv[0]);
Packit 971217
    return 1;
Packit 971217
  }
Packit 971217
Packit 971217
  loop = g_main_loop_new (NULL, TRUE);
Packit 971217
Packit 971217
  pipeline = gst_pipeline_new ("pipeline");
Packit 971217
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 (eos_cb), loop);
Packit 971217
  g_signal_connect (bus, "message::error", G_CALLBACK (error_cb), loop);
Packit 971217
  g_signal_connect (bus, "message::warning", G_CALLBACK (warning_cb), NULL);
Packit 971217
  g_signal_connect (bus, "message::state-changed", G_CALLBACK (state_cb),
Packit 971217
      pipeline);
Packit 971217
Packit 971217
  source = gst_element_factory_make ("giosrc", "source");
Packit 971217
  g_assert (source);
Packit 971217
Packit 971217
  if (argv[1] && strstr (argv[1], "://") != NULL) {
Packit 971217
    g_object_set (G_OBJECT (source), "location", argv[1], NULL);
Packit 971217
  } else if (argv[1]) {
Packit 971217
    gchar *uri = g_strdup_printf ("file://%s", argv[1]);
Packit 971217
Packit 971217
    g_object_set (G_OBJECT (source), "location", uri, NULL);
Packit 971217
    g_free (uri);
Packit 971217
  }
Packit 971217
Packit 971217
  decoder = gst_element_factory_make ("decodebin", "decoder");
Packit 971217
  g_assert (decoder);
Packit 971217
Packit 971217
  gst_bin_add (GST_BIN (pipeline), source);
Packit 971217
  gst_bin_add (GST_BIN (pipeline), decoder);
Packit 971217
Packit 971217
  gst_element_link_pads (source, "src", decoder, "sink");
Packit 971217
Packit 971217
  g_signal_connect (decoder, "pad-added", G_CALLBACK (pad_added_cb), pipeline);
Packit 971217
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
  g_main_loop_run (loop);
Packit 971217
Packit 971217
  /* tidy up */
Packit 971217
  gst_element_set_state (pipeline, GST_STATE_NULL);
Packit 971217
  gst_object_unref (pipeline);
Packit 971217
  gst_object_unref (bus);
Packit 971217
Packit 971217
  return 0;
Packit 971217
}