Blame tests/icles/playback/decodetest.c

Packit 0652a1
/* GStreamer
Packit 0652a1
 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
Packit 0652a1
 *
Packit 0652a1
 * This library is free software; you can redistribute it and/or
Packit 0652a1
 * modify it under the terms of the GNU Library General Public
Packit 0652a1
 * License as published by the Free Software Foundation; either
Packit 0652a1
 * version 2 of the License, or (at your option) any later version.
Packit 0652a1
 *
Packit 0652a1
 * This library is distributed in the hope that it will be useful,
Packit 0652a1
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 0652a1
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 0652a1
 * Library General Public License for more details.
Packit 0652a1
 *
Packit 0652a1
 * You should have received a copy of the GNU Library General Public
Packit 0652a1
 * License along with this library; if not, write to the
Packit 0652a1
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
Packit 0652a1
 * Boston, MA 02110-1301, USA.
Packit 0652a1
 */
Packit 0652a1
#include <gst/gst.h>
Packit 0652a1
#include <string.h>
Packit 0652a1
Packit 0652a1
static void
Packit 0652a1
warning_cb (GstBus * bus, GstMessage * msg, gpointer foo)
Packit 0652a1
{
Packit 0652a1
  GError *err = NULL;
Packit 0652a1
  gchar *dbg = NULL;
Packit 0652a1
Packit 0652a1
  gst_message_parse_warning (msg, &err, &dbg;;
Packit 0652a1
Packit 0652a1
  g_printerr ("WARNING: %s (%s)\n", err->message, (dbg) ? dbg : "no details");
Packit 0652a1
Packit 0652a1
  g_error_free (err);
Packit 0652a1
  g_free (dbg);
Packit 0652a1
}
Packit 0652a1
Packit 0652a1
static void
Packit 0652a1
error_cb (GstBus * bus, GstMessage * msg, GMainLoop * main_loop)
Packit 0652a1
{
Packit 0652a1
  GError *err = NULL;
Packit 0652a1
  gchar *dbg = NULL;
Packit 0652a1
Packit 0652a1
  gst_message_parse_error (msg, &err, &dbg;;
Packit 0652a1
Packit 0652a1
  g_printerr ("ERROR: %s (%s)\n", err->message, (dbg) ? dbg : "no details");
Packit 0652a1
Packit 0652a1
  g_main_loop_quit (main_loop);
Packit 0652a1
Packit 0652a1
  g_error_free (err);
Packit 0652a1
  g_free (dbg);
Packit 0652a1
}
Packit 0652a1
Packit 0652a1
static void
Packit 0652a1
eos_cb (GstBus * bus, GstMessage * msg, GMainLoop * main_loop)
Packit 0652a1
{
Packit 0652a1
  g_print ("EOS\n");
Packit 0652a1
  g_main_loop_quit (main_loop);
Packit 0652a1
}
Packit 0652a1
Packit 0652a1
static void
Packit 0652a1
state_cb (GstBus * bus, GstMessage * msg, GstElement * pipeline)
Packit 0652a1
{
Packit 0652a1
  if (msg->src == GST_OBJECT (pipeline)) {
Packit 0652a1
    GstState old_state, new_state, pending_state;
Packit 0652a1
Packit 0652a1
    gst_message_parse_state_changed (msg, &old_state, &new_state,
Packit 0652a1
        &pending_state);
Packit 0652a1
    if (new_state == GST_STATE_PLAYING) {
Packit 0652a1
      g_print ("Decoding ...\n");
Packit 0652a1
    }
Packit 0652a1
  }
Packit 0652a1
}
Packit 0652a1
Packit 0652a1
static void
Packit 0652a1
pad_added_cb (GstElement * decodebin, GstPad * pad, GstElement * pipeline)
Packit 0652a1
{
Packit 0652a1
  GstPadLinkReturn ret;
Packit 0652a1
  GstElement *fakesink;
Packit 0652a1
  GstPad *fakesink_pad;
Packit 0652a1
Packit 0652a1
  fakesink = gst_element_factory_make ("fakesink", NULL);
Packit 0652a1
  fakesink_pad = gst_element_get_static_pad (fakesink, "sink");
Packit 0652a1
Packit 0652a1
  gst_bin_add (GST_BIN (pipeline), fakesink);
Packit 0652a1
Packit 0652a1
  /* this doesn't really seem right, but it makes things work for me */
Packit 0652a1
  gst_element_set_state (fakesink, GST_STATE_PLAYING);
Packit 0652a1
Packit 0652a1
  ret = gst_pad_link (pad, fakesink_pad);
Packit 0652a1
  if (!GST_PAD_LINK_SUCCESSFUL (ret)) {
Packit 0652a1
    g_printerr ("Failed to link %s:%s to %s:%s (ret = %d)\n",
Packit 0652a1
        GST_DEBUG_PAD_NAME (pad), GST_DEBUG_PAD_NAME (fakesink_pad), ret);
Packit 0652a1
  } else {
Packit 0652a1
    g_printerr ("Linked %s:%s to %s:%s\n", GST_DEBUG_PAD_NAME (pad),
Packit 0652a1
        GST_DEBUG_PAD_NAME (fakesink_pad));
Packit 0652a1
  }
Packit 0652a1
Packit 0652a1
  gst_object_unref (fakesink_pad);
Packit 0652a1
}
Packit 0652a1
Packit 0652a1
gint
Packit 0652a1
main (gint argc, gchar * argv[])
Packit 0652a1
{
Packit 0652a1
  GstElement *decoder;
Packit 0652a1
  GstElement *source;
Packit 0652a1
  GstElement *pipeline;
Packit 0652a1
  GstStateChangeReturn res;
Packit 0652a1
  GMainLoop *loop;
Packit 0652a1
  GstBus *bus;
Packit 0652a1
Packit 0652a1
  gst_init (&argc, &argv);
Packit 0652a1
Packit 0652a1
  if (argc != 2) {
Packit 0652a1
    g_printerr ("Decode file from start to end.\n");
Packit 0652a1
    g_printerr ("Usage: %s URI\n\n", argv[0]);
Packit 0652a1
    return 1;
Packit 0652a1
  }
Packit 0652a1
Packit 0652a1
  loop = g_main_loop_new (NULL, TRUE);
Packit 0652a1
Packit 0652a1
  pipeline = gst_pipeline_new ("pipeline");
Packit 0652a1
Packit 0652a1
  bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
Packit 0652a1
  gst_bus_add_signal_watch (bus);
Packit 0652a1
Packit 0652a1
  g_signal_connect (bus, "message::eos", G_CALLBACK (eos_cb), loop);
Packit 0652a1
  g_signal_connect (bus, "message::error", G_CALLBACK (error_cb), loop);
Packit 0652a1
  g_signal_connect (bus, "message::warning", G_CALLBACK (warning_cb), NULL);
Packit 0652a1
  g_signal_connect (bus, "message::state-changed", G_CALLBACK (state_cb),
Packit 0652a1
      pipeline);
Packit 0652a1
Packit 0652a1
  source = gst_element_factory_make ("giosrc", "source");
Packit 0652a1
  g_assert (source);
Packit 0652a1
Packit 0652a1
  if (argv[1] && strstr (argv[1], "://") != NULL) {
Packit 0652a1
    g_object_set (G_OBJECT (source), "location", argv[1], NULL);
Packit 0652a1
  } else if (argv[1]) {
Packit 0652a1
    gchar *uri = g_strdup_printf ("file://%s", argv[1]);
Packit 0652a1
Packit 0652a1
    g_object_set (G_OBJECT (source), "location", uri, NULL);
Packit 0652a1
    g_free (uri);
Packit 0652a1
  }
Packit 0652a1
Packit 0652a1
  decoder = gst_element_factory_make ("decodebin", "decoder");
Packit 0652a1
  g_assert (decoder);
Packit 0652a1
Packit 0652a1
  gst_bin_add (GST_BIN (pipeline), source);
Packit 0652a1
  gst_bin_add (GST_BIN (pipeline), decoder);
Packit 0652a1
Packit 0652a1
  gst_element_link_pads (source, "src", decoder, "sink");
Packit 0652a1
Packit 0652a1
  g_signal_connect (decoder, "pad-added", G_CALLBACK (pad_added_cb), pipeline);
Packit 0652a1
Packit 0652a1
  res = gst_element_set_state (pipeline, GST_STATE_PLAYING);
Packit 0652a1
  if (res == GST_STATE_CHANGE_FAILURE) {
Packit 0652a1
    g_print ("could not play\n");
Packit 0652a1
    return -1;
Packit 0652a1
  }
Packit 0652a1
Packit 0652a1
  g_main_loop_run (loop);
Packit 0652a1
Packit 0652a1
  /* tidy up */
Packit 0652a1
  gst_element_set_state (pipeline, GST_STATE_NULL);
Packit 0652a1
  gst_object_unref (pipeline);
Packit 0652a1
  gst_object_unref (bus);
Packit 0652a1
Packit 0652a1
  return 0;
Packit 0652a1
}