[the-board/time-label-fix: 2/4] [tb] Export recording duration in TbSoundRecorder



commit 43e3057424de89756830fdaba511fe5789eea558
Author: Lucas Rocha <lucasr gnome org>
Date:   Thu Jan 27 00:47:44 2011 +0000

    [tb] Export recording duration in TbSoundRecorder

 src/tb/tb-sound-recorder.c |   75 +++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 74 insertions(+), 1 deletions(-)
---
diff --git a/src/tb/tb-sound-recorder.c b/src/tb/tb-sound-recorder.c
index 658af99..b16ce4c 100644
--- a/src/tb/tb-sound-recorder.c
+++ b/src/tb/tb-sound-recorder.c
@@ -15,7 +15,8 @@ enum
   PROP_0,
 
   PROP_STATE,
-  PROP_FILENAME
+  PROP_FILENAME,
+  PROP_DURATION
 };
 
 struct _TbSoundRecorderPrivate
@@ -24,6 +25,8 @@ struct _TbSoundRecorderPrivate
   GstBus                *bus;
   TbSoundRecorderState   state;
   char                  *filename;
+  gint                   duration;
+  guint                  tick_timeout_id;
 };
 
 static void tb_sound_recorder_destroy_pipeline (TbSoundRecorder *recorder);
@@ -126,6 +129,44 @@ tb_sound_recorder_destroy_pipeline (TbSoundRecorder *recorder)
       gst_object_unref (priv->pipeline);
       priv->pipeline = NULL;
     }
+
+  if (priv->tick_timeout_id != 0)
+    {
+      g_source_remove (priv->tick_timeout_id);
+      priv->tick_timeout_id = 0;
+    }
+
+  g_object_notify (G_OBJECT (recorder), "duration");
+}
+
+static gboolean
+tb_sound_recorder_tick_timeout (gpointer user_data)
+{
+  TbSoundRecorderPrivate *priv;
+  TbSoundRecorder *recorder;
+  GstFormat format = GST_FORMAT_TIME;
+  gint64 val = -1;
+  gint secs;
+
+  recorder = TB_SOUND_RECORDER (user_data);
+  priv = TB_SOUND_RECORDER_GET_PRIVATE (recorder);
+
+  /* This check stops us from doing an unnecessary query */
+  if (priv->state != TB_SOUND_RECORDER_STATE_RECORDING)
+    return FALSE;
+
+  if (gst_element_query_position (priv->pipeline, &format, &val) && val != -1)
+    {
+      secs = val / GST_SECOND;
+
+      if (priv->duration != secs)
+        {
+          priv->duration = secs;
+          g_object_notify (G_OBJECT (recorder), "duration");
+        }
+    }
+
+  return TRUE;
 }
 
 static void
@@ -149,11 +190,26 @@ tb_sound_recorder_on_state_changed (GstBus          *bus,
     {
     case GST_STATE_PLAYING:
       tb_sound_recorder_set_state (recorder, TB_SOUND_RECORDER_STATE_RECORDING);
+
+      if (priv->tick_timeout_id == 0)
+        {
+          priv->tick_timeout_id =
+            g_timeout_add (200,
+                           tb_sound_recorder_tick_timeout,
+                           recorder);
+        }
+
       break;
 
     case GST_STATE_READY:
     case GST_STATE_PAUSED:
       tb_sound_recorder_set_state (recorder, TB_SOUND_RECORDER_STATE_IDLE);
+
+      if (priv->tick_timeout_id != 0)
+        {
+          g_source_remove (priv->tick_timeout_id);
+          priv->tick_timeout_id = 0;
+        }
       break;
 
     default:
@@ -284,6 +340,10 @@ tb_sound_recorder_get_property (GObject    *gobject,
       g_value_set_string (value, priv->filename);
       break;
 
+    case PROP_DURATION:
+      g_value_set_double (value, priv->duration);
+      break;
+
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
       break;
@@ -344,6 +404,17 @@ tb_sound_recorder_class_init (TbSoundRecorderClass *klass)
                                        NULL,
                                        G_PARAM_READWRITE |
                                        G_PARAM_CONSTRUCT));
+
+  g_object_class_install_property
+                 (gobject_class,
+                  PROP_DURATION,
+                  g_param_spec_double ("duration",
+                                       "Duration",
+                                       "Sound recording duration",
+                                       0.0,
+                                       G_MAXDOUBLE,
+                                       0.0,
+                                       G_PARAM_READABLE));
 }
 
 static void
@@ -357,6 +428,8 @@ tb_sound_recorder_init (TbSoundRecorder *recorder)
   recorder->priv->filename = NULL;
   recorder->priv->pipeline = NULL;
   recorder->priv->bus = NULL;
+  recorder->priv->duration = 0;
+  recorder->priv->tick_timeout_id = 0;
 }
 
 void



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