File: gstdwriteclockoverlay.cpp

package info (click to toggle)
gst-plugins-bad1.0 1.28.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 72,252 kB
  • sloc: ansic: 744,658; cpp: 300,297; objc: 3,559; xml: 3,351; sh: 1,095; python: 565; makefile: 181; java: 75
file content (193 lines) | stat: -rw-r--r-- 5,801 bytes parent folder | download | duplicates (4)
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/* GStreamer
 * Copyright (C) 2023 Seungha Yang <seungha@centricular.com>
 *
 * 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., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

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

#include "gstdwriteclockoverlay.h"
#include <mutex>
#include <time.h>

GST_DEBUG_CATEGORY_STATIC (dwrite_clock_overlay_debug);
#define GST_CAT_DEFAULT dwrite_clock_overlay_debug

enum
{
  PROP_0,
  PROP_TIME_FORMAT,
};

#define DEFAULT_TIME_FORMAT "%H:%M:%S"

struct GstDWriteClockOverlayPrivate
{
  std::mutex lock;
  std::string format;
  WString wformat;
};

struct _GstDWriteClockOverlay
{
  GstDWriteBaseOverlay parent;

  GstDWriteClockOverlayPrivate *priv;
};

static void gst_dwrite_clock_overlay_finalize (GObject * object);
static void gst_dwrite_clock_overlay_set_property (GObject * object,
    guint prop_id, const GValue * value, GParamSpec * pspec);
static void gst_dwrite_clock_overlay_get_property (GObject * object,
    guint prop_id, GValue * value, GParamSpec * pspec);

static WString
gst_dwrite_clock_overlay_get_text (GstDWriteBaseOverlay * overlay,
    const WString & default_text, GstBuffer * buffer);

#define gst_dwrite_clock_overlay_parent_class parent_class
G_DEFINE_TYPE (GstDWriteClockOverlay, gst_dwrite_clock_overlay,
    GST_TYPE_DWRITE_BASE_OVERLAY);

static void
gst_dwrite_clock_overlay_class_init (GstDWriteClockOverlayClass * klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
  GstDWriteBaseOverlayClass *overlay_class =
      GST_DWRITE_BASE_OVERLAY_CLASS (klass);

  object_class->finalize = gst_dwrite_clock_overlay_finalize;
  object_class->set_property = gst_dwrite_clock_overlay_set_property;
  object_class->get_property = gst_dwrite_clock_overlay_get_property;

  g_object_class_install_property (object_class, PROP_TIME_FORMAT,
      g_param_spec_string ("time-format", "Date/Time Format",
          "Format to use for time and date value, as in strftime.",
          DEFAULT_TIME_FORMAT,
          (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));

  gst_element_class_set_static_metadata (element_class,
      "DirectWrite Clock Overlay", "Filter/Editor/Video",
      "Overlays the current clock time on a video stream",
      "Seungha Yang <seungha@centricular.com>");

  overlay_class->get_text =
      GST_DEBUG_FUNCPTR (gst_dwrite_clock_overlay_get_text);

  GST_DEBUG_CATEGORY_INIT (dwrite_clock_overlay_debug,
      "dwriteclockoverlay", 0, "dwriteclockoverlay");
}

static void
gst_dwrite_clock_overlay_init (GstDWriteClockOverlay * self)
{
  g_object_set (self, "text-alignment", DWRITE_TEXT_ALIGNMENT_LEADING,
      "paragraph-alignment", DWRITE_PARAGRAPH_ALIGNMENT_NEAR, nullptr);

  self->priv = new GstDWriteClockOverlayPrivate ();
  self->priv->format = DEFAULT_TIME_FORMAT;
  self->priv->wformat = gst_dwrite_string_to_wstring (DEFAULT_TIME_FORMAT);
}

static void
gst_dwrite_clock_overlay_finalize (GObject * object)
{
  GstDWriteClockOverlay *self = GST_DWRITE_CLOCK_OVERLAY (object);

  delete self->priv;

  G_OBJECT_CLASS (parent_class)->finalize (object);
}

static void
gst_dwrite_clock_overlay_set_property (GObject * object, guint prop_id,
    const GValue * value, GParamSpec * pspec)
{
  GstDWriteClockOverlay *self = GST_DWRITE_CLOCK_OVERLAY (object);
  GstDWriteClockOverlayPrivate *priv = self->priv;
  std::lock_guard < std::mutex > lk (priv->lock);

  switch (prop_id) {
    case PROP_TIME_FORMAT:
    {
      const gchar *format = g_value_get_string (value);
      if (format)
        priv->format = format;
      else
        priv->format = DEFAULT_TIME_FORMAT;

      priv->wformat = gst_dwrite_string_to_wstring (priv->format);
      break;
    }
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

static void
gst_dwrite_clock_overlay_get_property (GObject * object, guint prop_id,
    GValue * value, GParamSpec * pspec)
{
  GstDWriteClockOverlay *self = GST_DWRITE_CLOCK_OVERLAY (object);
  GstDWriteClockOverlayPrivate *priv = self->priv;
  std::lock_guard < std::mutex > lk (priv->lock);

  switch (prop_id) {
    case PROP_TIME_FORMAT:
      g_value_set_string (value, priv->format.c_str ());
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

static WString
gst_dwrite_clock_overlay_render_time (GstDWriteClockOverlay * self)
{
  GstDWriteClockOverlayPrivate *priv = self->priv;
  struct tm *t;
  time_t now;
  wchar_t text[256];

  now = time (nullptr);
  t = localtime (&now);

  if (!t)
    return WString (L"--:--:--");

  if (wcsftime (text, G_N_ELEMENTS (text), priv->wformat.c_str (), t) == 0)
    return WString (L"--:--:--");

  return WString (text);
}

static WString
gst_dwrite_clock_overlay_get_text (GstDWriteBaseOverlay * overlay,
    const WString & default_text, GstBuffer * buffer)
{
  GstDWriteClockOverlay *self = GST_DWRITE_CLOCK_OVERLAY (overlay);
  WString time_str = gst_dwrite_clock_overlay_render_time (self);

  if (default_text.empty ())
    return time_str;

  return default_text + WString (L" ") + time_str;
}