summaryrefslogtreecommitdiff
path: root/ext/pango/gstpangotimeoverlay.c
blob: 50b8739dddf043c164266b32a30ed27b6dd2d2a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/* GStreamer
 * Copyright (C) 2009 Benjamin Otte <otte@gnome.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

/**
 * SECTION:element-pango_time_overlay
 *
 * Convert video frames between a great variety of colorspace formats.
 *
 * <refsect2>
 * <title>Example launch line</title>
 * |[
 * gst-launch -v videotestsrc ! video/x-raw-yuv,format=\(fourcc\)YUY2 ! pango_time_overlay ! ximagesink
 * ]|
 * </refsect2>
 */

#ifdef HAVE_CONFIG_H
#  include "config.h"
#endif

#include "gstpangotimeoverlay.h"

#include <pango/pangocairo.h>

GST_BOILERPLATE (GstPangoTimeOverlay, gst_pango_time_overlay, GstElement,
    GST_TYPE_BASE_TRANSFORM);

static gboolean
gst_pango_time_overlay_set_caps (GstBaseTransform * btrans, GstCaps * incaps,
    GstCaps * outcaps)
{
  GstPangoTimeOverlay *overlay = GST_PANGO_TIME_OVERLAY (btrans);
  GstCairoFormat *compare;
  gboolean equal;

  gst_cairo_format_free (overlay->format);
  overlay->format = gst_cairo_format_new (incaps);
  compare = gst_cairo_format_new (outcaps);

  equal = gst_cairo_format_equal (overlay->format, compare, 0);
  gst_cairo_format_free (compare);
  return equal;
}

static gboolean
gst_pango_time_overlay_get_unit_size (GstBaseTransform * btrans, GstCaps * caps,
    guint * size)
{
  GstCairoFormat *format;

  format = gst_cairo_format_new (caps);
  *size = gst_cairo_format_get_buffer_size (format);
  gst_cairo_format_free (format);
  return TRUE;
}

static gchar *
gst_pango_time_overlay_format_time (GstClockTime clocktime)
{
  guint hours, mins, secs, msecs;

  if (!GST_CLOCK_TIME_IS_VALID (clocktime))
    return g_strdup ("");

  hours = (guint) (clocktime / (GST_SECOND * 60 * 60));
  mins = (guint) ((clocktime / (GST_SECOND * 60)) % 60);
  secs = (guint) ((clocktime / GST_SECOND) % 60);
  msecs = (guint) ((clocktime % GST_SECOND) / (1000 * 1000));

  return g_strdup_printf ("%u:%02u:%02u.%03u", hours, mins, secs, msecs);
}

static GstFlowReturn
gst_pango_time_overlay_transform_ip (GstBaseTransform * btrans, GstBuffer * buf)
{
  GstPangoTimeOverlay *overlay = GST_PANGO_TIME_OVERLAY (btrans);
  PangoLayout *layout;
  cairo_surface_t *surface;
  cairo_t *cr;
  char *time_text;

  surface = gst_cairo_create_surface (buf, overlay->format);
  cr = cairo_create (surface);
  layout = pango_cairo_create_layout (cr);

  pango_layout_set_width (layout,
      gst_cairo_format_get_width (overlay->format) * PANGO_SCALE);
  pango_layout_set_alignment (layout, PANGO_ALIGN_RIGHT);

  time_text = gst_pango_time_overlay_format_time (GST_BUFFER_TIMESTAMP (buf));
  pango_layout_set_text (layout, time_text, -1);
  g_free (time_text);

  cairo_translate (cr, 1, 1);
  cairo_set_source_rgb (cr, 0, 0, 0);
  pango_cairo_show_layout (cr, layout);

  cairo_translate (cr, -1, -1);
  cairo_set_source_rgb (cr, 1, 1, 1);
  pango_cairo_show_layout (cr, layout);

  g_object_unref (layout);
  cairo_destroy (cr);
  cairo_surface_destroy (surface);

  return GST_FLOW_OK;
}

static gboolean
gst_pango_time_overlay_stop (GstBaseTransform * btrans)
{
  GstPangoTimeOverlay *overlay = GST_PANGO_TIME_OVERLAY (btrans);

  gst_cairo_format_free (overlay->format);
  overlay->format = NULL;

  return TRUE;
}

static void
gst_pango_time_overlay_base_init (gpointer klass)
{
  GstElementClass *element_class = GST_ELEMENT_CLASS (klass);

  gst_element_class_add_pad_template (element_class,
      gst_cairo_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS));
  gst_element_class_add_pad_template (element_class,
      gst_cairo_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS));
  gst_element_class_set_details_simple (element_class, "Time overlay",
      "Filter/Editor/Video",
      "Overlays buffer time stamps on a video stream",
      "Benjamin Otte <otte@gnome.org>");
}

static void
gst_pango_time_overlay_class_init (GstPangoTimeOverlayClass * klass)
{
  GstBaseTransformClass *basetransform_class = GST_BASE_TRANSFORM_CLASS (klass);

  parent_class = g_type_class_peek_parent (klass);

  basetransform_class->set_caps = gst_pango_time_overlay_set_caps;
  basetransform_class->get_unit_size = gst_pango_time_overlay_get_unit_size;
  basetransform_class->transform_ip = gst_pango_time_overlay_transform_ip;
  basetransform_class->stop = gst_pango_time_overlay_stop;

  basetransform_class->passthrough_on_same_caps = TRUE;
}

static void
gst_pango_time_overlay_init (GstPangoTimeOverlay * overlay,
    GstPangoTimeOverlayClass * klass)
{
  gst_base_transform_set_qos_enabled (GST_BASE_TRANSFORM (overlay), TRUE);
}