gst_plugins_base/gst/playback/test7.c
changeset 0 0e761a78d257
child 8 4a7fac7dd34a
equal deleted inserted replaced
-1:000000000000 0:0e761a78d257
       
     1 /* GStreamer
       
     2  * Copyright (C) <2007> Wim Taymans <wim.taymans@gmail.com>
       
     3  *
       
     4  * This library is free software; you can redistribute it and/or
       
     5  * modify it under the terms of the GNU Library General Public
       
     6  * License as published by the Free Software Foundation; either
       
     7  * version 2 of the License, or (at your option) any later version.
       
     8  *
       
     9  * This library is distributed in the hope that it will be useful,
       
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    12  * Library General Public License for more details.
       
    13  *
       
    14  * You should have received a copy of the GNU Library General Public
       
    15  * License along with this library; if not, write to the
       
    16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
       
    17  * Boston, MA 02111-1307, USA.
       
    18  */
       
    19 #include <stdlib.h>
       
    20 #include <gst/gst.h>
       
    21 
       
    22 #define UPDATE_INTERVAL 500
       
    23 
       
    24 static int arg_count;
       
    25 static int max_count;
       
    26 
       
    27 static gboolean
       
    28 update_scale (GstElement * element)
       
    29 {
       
    30   gint64 duration = -1;
       
    31   gint64 position = -1;
       
    32   GstFormat format = GST_FORMAT_TIME;
       
    33   gchar dur_str[32], pos_str[32];
       
    34 
       
    35   if (gst_element_query_position (element, &format, &position) &&
       
    36       position != -1) {
       
    37     g_snprintf (pos_str, 32, "%" GST_TIME_FORMAT, GST_TIME_ARGS (position));
       
    38   } else {
       
    39     g_snprintf (pos_str, 32, "-:--:--.---------");
       
    40   }
       
    41 
       
    42   if (gst_element_query_duration (element, &format, &duration) &&
       
    43       duration != -1) {
       
    44     g_snprintf (dur_str, 32, "%" GST_TIME_FORMAT, GST_TIME_ARGS (duration));
       
    45   } else {
       
    46     g_snprintf (dur_str, 32, "-:--:--.---------");
       
    47   }
       
    48 
       
    49   g_print ("%s / %s\n", pos_str, dur_str);
       
    50 
       
    51   return TRUE;
       
    52 }
       
    53 
       
    54 static void
       
    55 warning_cb (GstBus * bus, GstMessage * msg, gpointer foo)
       
    56 {
       
    57   GError *err = NULL;
       
    58   gchar *dbg = NULL;
       
    59 
       
    60   gst_message_parse_warning (msg, &err, &dbg);
       
    61 
       
    62   g_printerr ("WARNING: %s (%s)\n", err->message, (dbg) ? dbg : "no details");
       
    63 
       
    64   g_error_free (err);
       
    65   g_free (dbg);
       
    66 }
       
    67 
       
    68 static void
       
    69 error_cb (GstBus * bus, GstMessage * msg, GMainLoop * main_loop)
       
    70 {
       
    71   GError *err = NULL;
       
    72   gchar *dbg = NULL;
       
    73 
       
    74   gst_message_parse_error (msg, &err, &dbg);
       
    75 
       
    76   g_printerr ("ERROR: %s (%s)\n", err->message, (dbg) ? dbg : "no details");
       
    77 
       
    78   g_main_loop_quit (main_loop);
       
    79 
       
    80   g_error_free (err);
       
    81   g_free (dbg);
       
    82 }
       
    83 
       
    84 static void
       
    85 eos_cb (GstBus * bus, GstMessage * msg, GMainLoop * main_loop)
       
    86 {
       
    87   g_print ("EOS\n");
       
    88   g_main_loop_quit (main_loop);
       
    89 }
       
    90 
       
    91 static void
       
    92 about_to_finish_cb (GstElement * element, gchar * uri[])
       
    93 {
       
    94   if (arg_count < max_count) {
       
    95     g_object_set (G_OBJECT (element), "uri", uri[arg_count], NULL);
       
    96     arg_count++;
       
    97   }
       
    98 }
       
    99 
       
   100 gint
       
   101 main (gint argc, gchar * argv[])
       
   102 {
       
   103   GstStateChangeReturn res;
       
   104   GstElement *player;
       
   105   GMainLoop *loop;
       
   106   GstBus *bus;
       
   107 
       
   108   gst_init (&argc, &argv);
       
   109 
       
   110   loop = g_main_loop_new (NULL, TRUE);
       
   111 
       
   112   if (argc < 2) {
       
   113     g_print ("usage: %s <uri> [<uri> ... ]\n", argv[0]);
       
   114     exit (-1);
       
   115   }
       
   116 
       
   117   player = gst_element_factory_make ("playbin2", "player");
       
   118   g_assert (player);
       
   119 
       
   120   bus = gst_pipeline_get_bus (GST_PIPELINE (player));
       
   121   gst_bus_add_signal_watch (bus);
       
   122 
       
   123   g_signal_connect (bus, "message::eos", G_CALLBACK (eos_cb), loop);
       
   124   g_signal_connect (bus, "message::error", G_CALLBACK (error_cb), loop);
       
   125   g_signal_connect (bus, "message::warning", G_CALLBACK (warning_cb), NULL);
       
   126 
       
   127   g_object_set (G_OBJECT (player), "uri", argv[1], NULL);
       
   128 
       
   129   arg_count = 2;
       
   130   max_count = argc;
       
   131   g_signal_connect (player, "about-to-finish", G_CALLBACK (about_to_finish_cb),
       
   132       argv);
       
   133 
       
   134   res = gst_element_set_state (player, GST_STATE_PLAYING);
       
   135   if (res == GST_STATE_CHANGE_FAILURE) {
       
   136     g_print ("could not play\n");
       
   137     return -1;
       
   138   }
       
   139 
       
   140   g_timeout_add (UPDATE_INTERVAL, (GSourceFunc) update_scale, player);
       
   141 
       
   142   g_main_loop_run (loop);
       
   143 
       
   144   /* tidy up */
       
   145   gst_element_set_state (player, GST_STATE_NULL);
       
   146   gst_object_unref (player);
       
   147   gst_object_unref (bus);
       
   148 
       
   149   return 0;
       
   150 }