gst_plugins_base/ext/pango/gsttimeoverlay.c
changeset 0 0e761a78d257
equal deleted inserted replaced
-1:000000000000 0:0e761a78d257
       
     1 /* GStreamer
       
     2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
       
     3  * Copyright (C) <2005> Tim-Philipp Müller <tim@centricular.net>
       
     4  *
       
     5  * This library is free software; you can redistribute it and/or
       
     6  * modify it under the terms of the GNU Library General Public
       
     7  * License as published by the Free Software Foundation; either
       
     8  * version 2 of the License, or (at your option) any later version.
       
     9  *
       
    10  * This library is distributed in the hope that it will be useful,
       
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    13  * Library General Public License for more details.
       
    14  *
       
    15  * You should have received a copy of the GNU Library General Public
       
    16  * License along with this library; if not, write to the
       
    17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
       
    18  * Boston, MA 02111-1307, USA.
       
    19  */
       
    20 
       
    21 /**
       
    22  * SECTION:element-timeoverlay
       
    23  * @see_also: #GstTextOverlay, #GstClockOverlay
       
    24  *
       
    25  * <refsect2>
       
    26  * <para>
       
    27  * This element overlays the buffer time stamps of a video stream on
       
    28  * top of itself. You can position the text and configure the font details
       
    29  * using the properties of the #GstTextOverlay class. By default, the
       
    30  * time stamp is displayed in the top left corner of the picture, with some
       
    31  * padding to the left and to the top.
       
    32  * </para>
       
    33  * <para>
       
    34  * Here is a simple pipeline that displays the time stamps in the top left
       
    35  * corner of the video picture:
       
    36  * <programlisting>
       
    37  * gst-launch -v videotestsrc ! timeoverlay ! xvimagesink
       
    38  * </programlisting>
       
    39  * </para>
       
    40  * <para>
       
    41  * Here is another pipeline that displays the time stamps with some leading
       
    42  * text in the bottom right corner of the video picture, with the background
       
    43  * of the text being shaded in order to make it more legible on top of a
       
    44  * bright video background:
       
    45  * <programlisting>
       
    46  * gst-launch -v videotestsrc ! timeoverlay halign=right valign=bottom text="Stream time:" shaded-background=true ! xvimagesink
       
    47  * </programlisting>
       
    48  * </para>
       
    49  * </refsect2>
       
    50  */
       
    51 
       
    52 #ifdef HAVE_CONFIG_H
       
    53 #include "config.h"
       
    54 #endif
       
    55 
       
    56 #include <gst/video/video.h>
       
    57 
       
    58 #include <gsttimeoverlay.h>
       
    59 
       
    60 static const GstElementDetails time_overlay_details =
       
    61 GST_ELEMENT_DETAILS ("Time overlay",
       
    62     "Filter/Editor/Video",
       
    63     "Overlays buffer time stamps on a video stream",
       
    64     "Tim-Philipp Müller <tim@centricular.net>");
       
    65 
       
    66 GST_BOILERPLATE (GstTimeOverlay, gst_time_overlay, GstTextOverlay,
       
    67     GST_TYPE_TEXT_OVERLAY)
       
    68 
       
    69      static void gst_time_overlay_base_init (gpointer g_class)
       
    70 {
       
    71   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
       
    72 
       
    73   gst_element_class_set_details (element_class, &time_overlay_details);
       
    74 }
       
    75 
       
    76 static gchar *
       
    77 gst_time_overlay_render_time (GstTimeOverlay * overlay, GstClockTime time)
       
    78 {
       
    79   guint hours, mins, secs, msecs;
       
    80 
       
    81   if (!GST_CLOCK_TIME_IS_VALID (time))
       
    82     return g_strdup ("");
       
    83 
       
    84   hours = (guint) (time / (GST_SECOND * 60 * 60));
       
    85   mins = (guint) ((time / (GST_SECOND * 60)) % 60);
       
    86   secs = (guint) ((time / GST_SECOND) % 60);
       
    87   msecs = (guint) ((time % GST_SECOND) / (1000 * 1000));
       
    88 
       
    89   return g_strdup_printf ("%u:%02u:%02u.%03u", hours, mins, secs, msecs);
       
    90 }
       
    91 
       
    92 /* Called with lock held */
       
    93 static gchar *
       
    94 gst_time_overlay_get_text (GstTextOverlay * overlay, GstBuffer * video_frame)
       
    95 {
       
    96   GstClockTime time = GST_BUFFER_TIMESTAMP (video_frame);
       
    97   gchar *time_str, *txt, *ret;
       
    98 
       
    99   overlay->need_render = TRUE;
       
   100 
       
   101   if (!GST_CLOCK_TIME_IS_VALID (time)) {
       
   102     GST_DEBUG ("buffer without valid timestamp");
       
   103     return g_strdup ("");
       
   104   }
       
   105 
       
   106   GST_DEBUG ("buffer with timestamp %" GST_TIME_FORMAT, GST_TIME_ARGS (time));
       
   107 
       
   108   txt = g_strdup (overlay->default_text);
       
   109 
       
   110   time_str = gst_time_overlay_render_time (GST_TIME_OVERLAY (overlay), time);
       
   111   if (txt != NULL && *txt != '\0') {
       
   112     ret = g_strdup_printf ("%s %s", txt, time_str);
       
   113   } else {
       
   114     ret = time_str;
       
   115     time_str = NULL;
       
   116   }
       
   117 
       
   118   g_free (txt);
       
   119   g_free (time_str);
       
   120 
       
   121   return ret;
       
   122 }
       
   123 
       
   124 static void
       
   125 gst_time_overlay_class_init (GstTimeOverlayClass * klass)
       
   126 {
       
   127   GstTextOverlayClass *gsttextoverlay_class;
       
   128 
       
   129   gsttextoverlay_class = (GstTextOverlayClass *) klass;
       
   130 
       
   131   gsttextoverlay_class->get_text = gst_time_overlay_get_text;
       
   132 }
       
   133 
       
   134 static void
       
   135 gst_time_overlay_init (GstTimeOverlay * overlay, GstTimeOverlayClass * klass)
       
   136 {
       
   137   PangoFontDescription *font_description;
       
   138   GstTextOverlay *textoverlay;
       
   139   PangoContext *context;
       
   140 
       
   141   textoverlay = GST_TEXT_OVERLAY (overlay);
       
   142 
       
   143   context = GST_TEXT_OVERLAY_CLASS (klass)->pango_context;
       
   144 
       
   145   pango_context_set_language (context, pango_language_from_string ("en_US"));
       
   146   pango_context_set_base_dir (context, PANGO_DIRECTION_LTR);
       
   147 
       
   148   font_description = pango_font_description_new ();
       
   149   pango_font_description_set_family_static (font_description, "Monospace");
       
   150   pango_font_description_set_style (font_description, PANGO_STYLE_NORMAL);
       
   151   pango_font_description_set_variant (font_description, PANGO_VARIANT_NORMAL);
       
   152   pango_font_description_set_weight (font_description, PANGO_WEIGHT_NORMAL);
       
   153   pango_font_description_set_stretch (font_description, PANGO_STRETCH_NORMAL);
       
   154   pango_font_description_set_size (font_description, 18 * PANGO_SCALE);
       
   155   pango_context_set_font_description (context, font_description);
       
   156   pango_font_description_free (font_description);
       
   157 
       
   158   textoverlay->valign = GST_TEXT_OVERLAY_VALIGN_TOP;
       
   159   textoverlay->halign = GST_TEXT_OVERLAY_HALIGN_LEFT;
       
   160 }