Blame tests/icles/playback/test7.c

Packit 971217
/* GStreamer
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
#ifdef HAVE_STDLIB_H
Packit 971217
#include <stdlib.h>             /* exit() */
Packit 971217
#endif
Packit 971217
#include <gst/gst.h>
Packit 971217
Packit 971217
#define UPDATE_INTERVAL 500
Packit 971217
Packit 971217
static int arg_count;
Packit 971217
static int max_count;
Packit 971217
Packit 971217
static gboolean
Packit 971217
update_scale (GstElement * element)
Packit 971217
{
Packit 971217
  gint64 duration = -1;
Packit 971217
  gint64 position = -1;
Packit 971217
  gchar dur_str[32], pos_str[32];
Packit 971217
Packit 971217
  if (gst_element_query_position (element, GST_FORMAT_TIME, &position) &&
Packit 971217
      position != -1) {
Packit 971217
    g_snprintf (pos_str, 32, "%" GST_TIME_FORMAT, GST_TIME_ARGS (position));
Packit 971217
  } else {
Packit 971217
    g_snprintf (pos_str, 32, "-:--:--.---------");
Packit 971217
  }
Packit 971217
Packit 971217
  if (gst_element_query_duration (element, GST_FORMAT_TIME, &duration) &&
Packit 971217
      duration != -1) {
Packit 971217
    g_snprintf (dur_str, 32, "%" GST_TIME_FORMAT, GST_TIME_ARGS (duration));
Packit 971217
  } else {
Packit 971217
    g_snprintf (dur_str, 32, "-:--:--.---------");
Packit 971217
  }
Packit 971217
Packit 971217
  g_print ("%s / %s\n", pos_str, dur_str);
Packit 971217
Packit 971217
  return TRUE;
Packit 971217
}
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
new_clock_cb (GstBus * bus, GstMessage * msg, gpointer nothing)
Packit 971217
{
Packit 971217
  GstClock *clock;
Packit 971217
Packit 971217
  gst_message_parse_new_clock (msg, &clock);
Packit 971217
  g_print ("NEW CLOCK: %s\n", GST_OBJECT_NAME (clock));
Packit 971217
}
Packit 971217
Packit 971217
static void
Packit 971217
clock_lost_cb (GstBus * bus, GstMessage * msg, GstElement * playbin)
Packit 971217
{
Packit 971217
  GstClock *clock;
Packit 971217
Packit 971217
  gst_message_parse_clock_lost (msg, &clock);
Packit 971217
  g_print ("CLOCK LOST: %s\n", GST_OBJECT_NAME (clock));
Packit 971217
Packit 971217
  gst_element_set_state (playbin, GST_STATE_PAUSED);
Packit 971217
  gst_element_set_state (playbin, GST_STATE_PLAYING);
Packit 971217
}
Packit 971217
Packit 971217
static void
Packit 971217
about_to_finish_cb (GstElement * element, gchar * uri[])
Packit 971217
{
Packit 971217
  if (arg_count < max_count) {
Packit 971217
    g_object_set (G_OBJECT (element), "uri", uri[arg_count], NULL);
Packit 971217
    arg_count++;
Packit 971217
  }
Packit 971217
}
Packit 971217
Packit 971217
gint
Packit 971217
main (gint argc, gchar * argv[])
Packit 971217
{
Packit 971217
  GstStateChangeReturn res;
Packit 971217
  GstElement *player;
Packit 971217
  GMainLoop *loop;
Packit 971217
  GstBus *bus;
Packit 971217
Packit 971217
  gst_init (&argc, &argv);
Packit 971217
Packit 971217
  loop = g_main_loop_new (NULL, TRUE);
Packit 971217
Packit 971217
  if (argc < 2) {
Packit 971217
    g_print ("usage: %s <uri> [<uri> ... ]\n", argv[0]);
Packit 971217
    exit (-1);
Packit 971217
  }
Packit 971217
Packit 971217
  player = gst_element_factory_make ("playbin", "player");
Packit 971217
  g_assert (player);
Packit 971217
Packit 971217
  bus = gst_pipeline_get_bus (GST_PIPELINE (player));
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::new-clock", G_CALLBACK (new_clock_cb), NULL);
Packit 971217
  g_signal_connect (bus, "message::clock-lost", G_CALLBACK (clock_lost_cb),
Packit 971217
      player);
Packit 971217
Packit 971217
  g_object_set (G_OBJECT (player), "uri", argv[1], NULL);
Packit 971217
Packit 971217
  arg_count = 2;
Packit 971217
  max_count = argc;
Packit 971217
  g_signal_connect (player, "about-to-finish", G_CALLBACK (about_to_finish_cb),
Packit 971217
      argv);
Packit 971217
Packit 971217
  res = gst_element_set_state (player, 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_timeout_add (UPDATE_INTERVAL, (GSourceFunc) update_scale, player);
Packit 971217
Packit 971217
  g_main_loop_run (loop);
Packit 971217
Packit 971217
  /* tidy up */
Packit 971217
  gst_element_set_state (player, GST_STATE_NULL);
Packit 971217
  gst_object_unref (player);
Packit 971217
  gst_object_unref (bus);
Packit 971217
Packit 971217
  return 0;
Packit 971217
}