Commit b70e83c4 authored by Seungha Yang's avatar Seungha Yang Committed by GStreamer Marge Bot

clockoverlay: Fix broken string formatting by strftime() on Windows

Like other foobarA variant APIs on Windows, formatted string
by strftime() is ANSI string, not unicode encoded one.
It would be problematic for non-english locale systems.
We should use unicode version API (wcsftime in this case)
whenever it's possible on Windows.

Part-of: <https://gitlab.freedesktop.org/gstreamer/gst-plugins-base/-/merge_requests/1093>
parent 1d5ad7d1
......@@ -83,7 +83,11 @@ gst_clock_overlay_render_time (GstClockOverlay * overlay)
#endif
struct tm *t;
time_t now;
#ifdef G_OS_WIN32
gunichar2 buf[256];
#else
gchar buf[256];
#endif
now = time (NULL);
......@@ -100,9 +104,17 @@ gst_clock_overlay_render_time (GstClockOverlay * overlay)
if (t == NULL)
return g_strdup ("--:--:--");
#ifdef G_OS_WIN32
if (wcsftime (buf, sizeof (buf), (wchar_t *) overlay->wformat, t) == 0)
return g_strdup ("");
return g_utf16_to_utf8 (buf, -1, NULL, NULL, NULL);
#else
if (strftime (buf, sizeof (buf), overlay->format, t) == 0)
return g_strdup ("");
return g_strdup (buf);
#endif
}
/* Called with lock held */
......@@ -115,7 +127,10 @@ gst_clock_overlay_get_text (GstBaseTextOverlay * overlay,
txt = g_strdup (overlay->default_text);
GST_OBJECT_LOCK (overlay);
time_str = gst_clock_overlay_render_time (clock_overlay);
GST_OBJECT_UNLOCK (overlay);
if (txt != NULL && *txt != '\0') {
ret = g_strdup_printf ("%s %s", txt, time_str);
} else {
......@@ -173,6 +188,8 @@ gst_clock_overlay_finalize (GObject * object)
g_free (overlay->text);
overlay->format = NULL;
g_free (overlay->wformat);
G_OBJECT_CLASS (parent_class)->finalize (object);
}
......@@ -191,6 +208,11 @@ gst_clock_overlay_init (GstClockOverlay * overlay)
overlay->format = g_strdup (DEFAULT_PROP_TIMEFORMAT);
#ifdef G_OS_WIN32
overlay->wformat =
g_utf8_to_utf16 (DEFAULT_PROP_TIMEFORMAT, -1, NULL, NULL, NULL);
#endif
context = textoverlay->pango_context;
pango_context_set_language (context, pango_language_from_string ("en_US"));
......@@ -218,6 +240,13 @@ gst_clock_overlay_set_property (GObject * object, guint prop_id,
case PROP_TIMEFORMAT:
g_free (overlay->format);
overlay->format = g_value_dup_string (value);
if (!overlay->format)
overlay->format = g_strdup (DEFAULT_PROP_TIMEFORMAT);
#ifdef G_OS_WIN32
g_free (overlay->wformat);
overlay->wformat =
g_utf8_to_utf16 (overlay->format, -1, NULL, NULL, NULL);
#endif
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
......
......@@ -48,6 +48,10 @@ typedef struct _GstClockOverlayClass GstClockOverlayClass;
struct _GstClockOverlay {
GstBaseTextOverlay textoverlay;
gchar *format; /* as in strftime () */
/* for wcsftime */
gunichar2 *wformat;
gchar *text;
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment