timeoverlay: Add elapsed-running-time mode
authorSebastian Dröge <sebastian@centricular.com>
Thu, 8 Oct 2020 14:10:30 +0000 (17:10 +0300)
committerSebastian Dröge <sebastian@centricular.com>
Thu, 8 Oct 2020 20:51:02 +0000 (23:51 +0300)
This takes the first running time and the first after each flush-stop as
an offset for the running time that is rendered.

Part-of: <https://gitlab.freedesktop.org/gstreamer/gst-plugins-base/-/merge_requests/737>

docs/plugins/gst_plugins_cache.json
ext/pango/gsttimeoverlay.c
ext/pango/gsttimeoverlay.h

index 0ef4561..387700a 100644 (file)
                         "desc": "time-code",
                         "name": "time-code",
                         "value": "3"
+                    },
+                    {
+                        "desc": "elapsed-running-time",
+                        "name": "elapsed-running-time",
+                        "value": "4"
                     }
                 ]
             }
index db05220..66f49ab 100644 (file)
@@ -73,6 +73,14 @@ static void gst_time_overlay_set_property (GObject * object, guint prop_id,
 static void gst_time_overlay_get_property (GObject * object, guint prop_id,
     GValue * value, GParamSpec * pspec);
 
+/**
+ * GstTimeOverlayTimeLine::elapsed-running-time:
+ *
+ * Overlay elapsed running time since the first observed running time.
+ *
+ * Since: 1.20
+ */
+
 #define GST_TYPE_TIME_OVERLAY_TIME_LINE (gst_time_overlay_time_line_type())
 static GType
 gst_time_overlay_time_line_type (void)
@@ -83,6 +91,8 @@ gst_time_overlay_time_line_type (void)
     {GST_TIME_OVERLAY_TIME_LINE_STREAM_TIME, "stream-time", "stream-time"},
     {GST_TIME_OVERLAY_TIME_LINE_RUNNING_TIME, "running-time", "running-time"},
     {GST_TIME_OVERLAY_TIME_LINE_TIME_CODE, "time-code", "time-code"},
+    {GST_TIME_OVERLAY_TIME_LINE_ELAPSED_RUNNING_TIME,
+        "elapsed-running-time", "elapsed-running-time"},
     {0, NULL, NULL},
   };
 
@@ -150,6 +160,12 @@ gst_time_overlay_get_text (GstBaseTextOverlay * overlay,
       case GST_TIME_OVERLAY_TIME_LINE_RUNNING_TIME:
         ts = gst_segment_to_running_time (segment, GST_FORMAT_TIME, ts_buffer);
         break;
+      case GST_TIME_OVERLAY_TIME_LINE_ELAPSED_RUNNING_TIME:
+        ts = gst_segment_to_running_time (segment, GST_FORMAT_TIME, ts_buffer);
+        if (self->first_running_time == GST_CLOCK_TIME_NONE)
+          self->first_running_time = ts;
+        ts -= self->first_running_time;
+        break;
       case GST_TIME_OVERLAY_TIME_LINE_BUFFER_TIME:
       default:
         ts = ts_buffer;
@@ -184,6 +200,22 @@ gst_time_overlay_get_text (GstBaseTextOverlay * overlay,
   return ret;
 }
 
+static GstStateChangeReturn
+gst_time_overlay_change_state (GstElement * element, GstStateChange transition)
+{
+  GstTimeOverlay *self = GST_TIME_OVERLAY (element);
+
+  switch (transition) {
+    case GST_STATE_CHANGE_READY_TO_PAUSED:
+      self->first_running_time = GST_CLOCK_TIME_NONE;
+      break;
+    default:
+      break;
+  }
+
+  return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
+}
+
 static void
 gst_time_overlay_finalize (GObject * gobject)
 {
@@ -212,6 +244,8 @@ gst_time_overlay_class_init (GstTimeOverlayClass * klass)
 
   gsttextoverlay_class->get_text = gst_time_overlay_get_text;
 
+  gstelement_class->change_state = gst_time_overlay_change_state;
+
   gobject_class->finalize = gst_time_overlay_finalize;
   gobject_class->set_property = gst_time_overlay_set_property;
   gobject_class->get_property = gst_time_overlay_get_property;
@@ -241,12 +275,30 @@ gst_time_overlay_class_init (GstTimeOverlayClass * klass)
   gst_type_mark_as_plugin_api (GST_TYPE_TIME_OVERLAY_TIME_LINE, 0);
 }
 
+static gboolean
+gst_time_overlay_video_event (GstPad * pad, GstObject * parent,
+    GstEvent * event)
+{
+  GstTimeOverlay *overlay = GST_TIME_OVERLAY (parent);
+
+  switch (GST_EVENT_TYPE (event)) {
+    case GST_EVENT_FLUSH_STOP:
+      overlay->first_running_time = GST_CLOCK_TIME_NONE;
+      break;
+    default:
+      break;
+  }
+
+  return overlay->orig_video_event (pad, parent, event);
+}
+
 static void
 gst_time_overlay_init (GstTimeOverlay * overlay)
 {
   GstBaseTextOverlay *textoverlay;
   PangoContext *context;
   PangoFontDescription *font_description;
+  GstPad *video_sink;
 
   textoverlay = GST_BASE_TEXT_OVERLAY (overlay);
 
@@ -272,6 +324,10 @@ gst_time_overlay_init (GstTimeOverlay * overlay)
   pango_font_description_set_size (font_description, 18 * PANGO_SCALE);
   pango_context_set_font_description (context, font_description);
   pango_font_description_free (font_description);
+
+  video_sink = gst_element_get_static_pad (GST_ELEMENT (overlay), "video_sink");
+  overlay->orig_video_event = GST_PAD_EVENTFUNC (video_sink);
+  gst_pad_set_event_function (video_sink, gst_time_overlay_video_event);
 }
 
 static void
index cd1d625..d6cbde6 100644 (file)
@@ -46,6 +46,7 @@ typedef enum {
   GST_TIME_OVERLAY_TIME_LINE_STREAM_TIME,
   GST_TIME_OVERLAY_TIME_LINE_RUNNING_TIME,
   GST_TIME_OVERLAY_TIME_LINE_TIME_CODE,
+  GST_TIME_OVERLAY_TIME_LINE_ELAPSED_RUNNING_TIME,
 } GstTimeOverlayTimeLine;
 
 /**
@@ -63,6 +64,10 @@ struct _GstTimeOverlay {
   gboolean show_times_as_dates;
   gchar *datetime_format;
   GDateTime *datetime_epoch;
+
+  /* For GST_TIME_OVERLAY_TIME_LINE_ELAPSED_RUNNING_TIME mode */
+  GstClockTime first_running_time;
+  GstPadEventFunction orig_video_event;
 };
 
 struct _GstTimeOverlayClass {