Querying stream position/duration with gstmm



I worked a little on querying stream positions and duration so I modified the player example to print a "status line" showing the position/duration of the ogg stream. Would you be able to look at/check in this patch if it's ok? Thanks.

-Jose
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 105)
+++ ChangeLog	(working copy)
@@ -1,3 +1,16 @@
+2007-12-12  José Alburquerque  <jaalburquerque cox com>
+
+	* examples/ogg_player/main.cc: modified example to query and show
+	stream position
+	* gst/src/clock.ccg: added get_hours(), get_minutes(), get_seconds()
+	and get_milliseconds() methods for the Gst::ClockTime enum type
+	* gst/src/clock.hg: added get_hours(), get_minutes(), get_seconds()
+	and get_milliseconds declarations
+	* gst/src/element.hg: modified query_position() and query_duration()
+	methods to use a Gst::ClockTime instead of a gint64 and remove wrapped
+	methods from IGNORE list
+	* tools/m4/convert_gst.m4: added conversion from ClockTime& to gint64*
+
 2007-12-12  Murray Cumming  <murrayc murrayc com>
 
 	* examples/ogg_player/main.cc: Use dynamic_cast<> instead of static_cast<> 
Index: gst/src/element.hg
===================================================================
--- gst/src/element.hg	(revision 105)
+++ gst/src/element.hg	(working copy)
@@ -85,16 +85,14 @@
 
   _WRAP_METHOD(bool query_convert(Format src_format, gint64 src_value, Format& dst_format, gint64& dst_value), gst_element_query_convert)
   bool query_position(Format& format);
-  _WRAP_METHOD(bool query_position(Format& format, gint64& position), gst_element_query_position)
+  _WRAP_METHOD(bool query_position(Format& format, ClockTime& position), gst_element_query_position)
   bool query_duration(Format& format);
-  _WRAP_METHOD(bool query_duration(Format& format, gint64& duration), gst_element_query_duration)
+  _WRAP_METHOD(bool query_duration(Format& format, ClockTime& duration), gst_element_query_duration)
 
   _WRAP_METHOD(bool seek_simple(Format format, SeekFlags flags, const gint64& position), gst_element_seek_simple)
   _WRAP_METHOD(bool seek(const double& rate, Format format, SeekFlags flags, SeekType current_type, const gint64& current_position, SeekType stop_type, const gint64& stop_position), gst_element_seek)
 
-  _IGNORE(gst_element_link, gst_element_get_compatible_pad,
-          gst_element_query_convert, gst_element_query_position,
-          gst_element_query_duration)
+  _IGNORE(gst_element_link, gst_element_get_compatible_pad, gst_element_link_many)
 
   _WRAP_SIGNAL(void no_more_pads(), "no-more-pads")
 
Index: gst/src/clock.ccg
===================================================================
--- gst/src/clock.ccg	(revision 105)
+++ gst/src/clock.ccg	(working copy)
@@ -4,4 +4,28 @@
 namespace Gst
 {
 
+unsigned get_hours(ClockTime time)
+{
+  return GST_CLOCK_TIME_IS_VALID (time) ? \
+        (guint) (((GstClockTime)(time)) / (GST_SECOND * 60 * 60)) : 99;
+}
+
+unsigned get_minutes(ClockTime time)
+{
+  return GST_CLOCK_TIME_IS_VALID (time) ? \
+        (guint) ((((GstClockTime)(time)) / (GST_SECOND * 60)) % 60) : 99;
+}
+
+unsigned get_seconds(ClockTime time)
+{
+  return GST_CLOCK_TIME_IS_VALID (time) ? \
+        (guint) ((((GstClockTime)(time)) / GST_SECOND) % 60) : 99;
+}
+
+unsigned get_milliseconds(ClockTime time)
+{
+  return GST_CLOCK_TIME_IS_VALID (time) ? \
+        (guint) (((GstClockTime)(time)) % GST_SECOND) : 999999999;
+}
+
 } //namespace Gst
Index: gst/src/clock.hg
===================================================================
--- gst/src/clock.hg	(revision 105)
+++ gst/src/clock.hg	(working copy)
@@ -20,6 +20,13 @@
 typedef GstClockTimeDiff ClockTimeDiff;
 const ClockTime CLOCK_TIME_NONE = GST_CLOCK_TIME_NONE;
 
+//Note that ClockTime is an enum.  That's why these are not member functions of a class
+
+unsigned get_hours(ClockTime time);
+unsigned get_minutes(ClockTime time);
+unsigned get_seconds(ClockTime time);
+unsigned get_milliseconds(ClockTime time);
+
 class Clock : public Object
 {
 
Index: tools/m4/convert_gst.m4
===================================================================
--- tools/m4/convert_gst.m4	(revision 105)
+++ tools/m4/convert_gst.m4	(working copy)
@@ -17,6 +17,7 @@
 _CONVERSION(`State&',`GstState*',`((GstState*) (&($3)))')
 _CONVERSION(`GstClockTime',`ClockTime',`$3')
 _CONVERSION(`ClockTime',`GstClockTime',`(GstClockTime ($3))')
+_CONVERSION(`ClockTime&',`gint64*',`((gint64*) &(($3)))')
 _CONVERSION(`GstClockID',`Glib::RefPtr<ClockID>',`$3')
 _CONVERSION(`Glib::RefPtr<ClockID>',`GstClockID',`(GstClockID ($3))')
 _CONVERSION(`GstClock*',`Clock',`Glib::wrap($3)')
Index: examples/ogg_player/main.cc
===================================================================
--- examples/ogg_player/main.cc	(revision 105)
+++ examples/ogg_player/main.cc	(working copy)
@@ -1,14 +1,39 @@
 #include <gstmm.h>
 #include <iostream>
+#include <iomanip>
 
 Glib::RefPtr<Glib::MainLoop> mainloop;
+Glib::RefPtr<Gst::Pipeline> pipeline;
 Glib::RefPtr<Gst::Element> decoder;
 
+bool print_stream_position(void)
+{
+ Gst::Format fmt = Gst::FORMAT_TIME;
+ Gst::ClockTime pos, len;
+
+ if (pipeline->query_position(fmt, pos)
+   && pipeline->query_duration(fmt, len)) {
+
+   std::cout << "Time: " << std::setfill('0') << 
+     std::setw(3) << Gst::get_hours(pos) << ":" <<
+       std::setw(2) << Gst::get_minutes(pos) << ":" <<
+         std::setw(2) << Gst::get_seconds(pos) << "." <<
+           std::setw(9) << Gst::get_milliseconds(pos);
+
+   std::cout << "/" << std::setw(3) << Gst::get_hours(len) << ":" <<
+     std::setw(2) << Gst::get_minutes(len) << ":" <<
+       std::setw(2) << Gst::get_seconds(len) << "." <<
+         std::setw(9) << Gst::get_milliseconds(len) << "\r" << std::flush;
+ }
+
+ return true;
+}
+
 bool on_bus_message(const Glib::RefPtr<Gst::Bus>& bus, const Glib::RefPtr<Gst::Message>& message)
 {
   switch (message->get_message_type()) {
     case Gst::MESSAGE_EOS:
-      std::cout << "End of stream" << std::endl;
+      std::cout << "\nEnd of stream" << std::endl;
       mainloop->quit();
       return false;
     case Gst::MESSAGE_ERROR:
@@ -56,7 +81,7 @@
   mainloop = Glib::MainLoop::create();
 
   // Create elements:
-  Glib::RefPtr<Gst::Pipeline> pipeline = Gst::Pipeline::create("audio-player");
+  pipeline = Gst::Pipeline::create("audio-player");
   std::cout << "pipeline=" << pipeline << std::endl;
   Glib::RefPtr<Gst::Element> source = Gst::Element::create("filesrc", "file-source");
   std::cout << "source=" << source << std::endl;
@@ -103,6 +128,9 @@
 
   decoder->link(conv)->link(sink);
 
+  // Call print_stream_position function at a 200ms
+  // interval to regularly print the position of the stream
+  Glib::signal_timeout().connect(sigc::ptr_fun(&print_stream_position), 200);
 
   // Now set to playing and iterate:
   std::cout << "Setting to PLAYING." << std::endl;


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]