plugins: Use g_win32_error_message for HRESULT to string conversion
authorSeungha Yang <seungha@centricular.com>
Thu, 16 Jul 2020 10:26:55 +0000 (19:26 +0900)
committerSeungha Yang <seungha@centricular.com>
Sat, 18 Jul 2020 02:05:52 +0000 (11:05 +0900)
We don't need to duplicate a method for HRESULT error code to string
conversion. This patch is intended to
* Remove duplicated code
* Ensure FormatMessageW (Unicode version) and avoid FormatMessageA
  (ANSI version), as the ANSI format is not portable at all.
  Note that if "UNICODE" is not defined, FormatMessageA will be aliased
  as FormatMessage by default.

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

sys/d3d11/gstd3d11utils.c
sys/mediafoundation/gstmfutils.cpp
sys/wasapi/gstwasapiutil.c
sys/wasapi2/gstwasapi2util.c
sys/winscreencap/dxgicapture.c

index 9e342c0..6cf9a77 100644 (file)
@@ -386,29 +386,6 @@ gst_d3d11_get_device_vendor (GstD3D11Device * device)
   return vendor;
 }
 
-static gchar *
-gst_d3d11_hres_to_string (HRESULT hr)
-{
-  DWORD flags;
-  gchar *ret_text;
-  LPTSTR error_text = NULL;
-
-  flags = FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER
-      | FORMAT_MESSAGE_IGNORE_INSERTS;
-  FormatMessage (flags, NULL, hr, MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
-      (LPTSTR) & error_text, 0, NULL);
-
-#ifdef UNICODE
-  /* If UNICODE is defined, LPTSTR is LPWSTR which is UTF-16 */
-  ret_text = g_utf16_to_utf8 (error_text, 0, NULL, NULL, NULL);
-#else
-  ret_text = g_strdup (error_text);
-#endif
-
-  LocalFree (error_text);
-  return ret_text;
-}
-
 gboolean
 _gst_d3d11_result (HRESULT hr, GstD3D11Device * device, GstDebugCategory * cat,
     const gchar * file, const gchar * function, gint line)
@@ -419,9 +396,13 @@ _gst_d3d11_result (HRESULT hr, GstD3D11Device * device, GstDebugCategory * cat,
   if (FAILED (hr)) {
     gchar *error_text = NULL;
 
-    error_text = gst_d3d11_hres_to_string (hr);
+    error_text = g_win32_error_message ((guint) hr);
+    /* g_win32_error_message() doesn't cover all HERESULT return code,
+     * so it could be empty string, or null if there was an error
+     * in g_utf16_to_utf8() */
     gst_debug_log (cat, GST_LEVEL_WARNING, file, function, line,
-        NULL, "D3D11 call failed: 0x%x, %s", (guint) hr, error_text);
+        NULL, "D3D11 call failed: 0x%x, %s", (guint) hr,
+        GST_STR_NULL (error_text));
     g_free (error_text);
 
     ret = FALSE;
index cecf80c..77793a1 100644 (file)
@@ -356,29 +356,6 @@ gst_mf_media_type_release (IMFMediaType * media_type)
     media_type->Release ();
 }
 
-static gchar *
-gst_mf_hr_to_string (HRESULT hr)
-{
-  DWORD flags;
-  gchar *ret_text;
-  LPTSTR error_text = NULL;
-
-  flags = FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER
-      | FORMAT_MESSAGE_IGNORE_INSERTS;
-  FormatMessage (flags, NULL, hr, MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
-      (LPTSTR) & error_text, 0, NULL);
-
-#ifdef UNICODE
-  ret_text = g_utf16_to_utf8 ((const gunichar2  *) error_text,
-      -1, NULL, NULL, NULL);
-#else
-  ret_text = g_strdup (error_text);
-#endif
-
-  LocalFree (error_text);
-  return ret_text;
-}
-
 gboolean
 _gst_mf_result (HRESULT hr, GstDebugCategory * cat, const gchar * file,
     const gchar * function, gint line)
@@ -389,9 +366,13 @@ _gst_mf_result (HRESULT hr, GstDebugCategory * cat, const gchar * file,
   if (FAILED (hr)) {
     gchar *error_text = NULL;
 
-    error_text = gst_mf_hr_to_string (hr);
+    error_text = g_win32_error_message ((gint) hr);
+    /* g_win32_error_message() doesn't cover all HERESULT return code,
+     * so it could be empty string, or null if there was an error
+     * in g_utf16_to_utf8() */
     gst_debug_log (cat, GST_LEVEL_WARNING, file, function, line,
-        NULL, "MediaFoundation call failed: 0x%x, %s", (guint) hr, error_text);
+        NULL, "MediaFoundation call failed: 0x%x, %s", (guint) hr,
+        GST_STR_NULL (error_text));
     g_free (error_text);
 
     ret = FALSE;
index 1651bdf..61076df 100644 (file)
@@ -292,28 +292,17 @@ hresult_to_string_fallback (HRESULT hr)
 gchar *
 gst_wasapi_util_hresult_to_string (HRESULT hr)
 {
-  DWORD flags;
-  gchar *ret_text;
-  LPTSTR error_text = NULL;
-
-  flags = FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER
-      | FORMAT_MESSAGE_IGNORE_INSERTS;
-  FormatMessage (flags, NULL, hr, MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
-      (LPTSTR) & error_text, 0, NULL);
-
-  /* If we couldn't get the error msg, try the fallback switch statement */
-  if (error_text == NULL)
-    return g_strdup (hresult_to_string_fallback (hr));
-
-#ifdef UNICODE
-  /* If UNICODE is defined, LPTSTR is LPWSTR which is UTF-16 */
-  ret_text = g_utf16_to_utf8 (error_text, 0, NULL, NULL, NULL);
-#else
-  ret_text = g_strdup (error_text);
-#endif
+  gchar *error_text = NULL;
+
+  error_text = g_win32_error_message ((gint) hr);
+  /* g_win32_error_message() seems to be returning empty string for
+   * AUDCLNT_* cases */
+  if (!error_text || strlen (error_text) == 0) {
+    g_free (error_text);
+    error_text = g_strdup (hresult_to_string_fallback (hr));
+  }
 
-  LocalFree (error_text);
-  return ret_text;
+  return error_text;
 }
 
 static IMMDeviceEnumerator *
index 0bbce30..81109d0 100644 (file)
@@ -157,33 +157,6 @@ hresult_to_string_fallback (HRESULT hr)
   return s;
 }
 
-static gchar *
-gst_wasapi2_util_hresult_to_string (HRESULT hr)
-{
-  DWORD flags;
-  gchar *ret_text;
-  LPTSTR error_text = NULL;
-
-  flags = FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER
-      | FORMAT_MESSAGE_IGNORE_INSERTS;
-  FormatMessage (flags, NULL, hr, MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
-      (LPTSTR) & error_text, 0, NULL);
-
-  /* If we couldn't get the error msg, try the fallback switch statement */
-  if (error_text == NULL)
-    return g_strdup (hresult_to_string_fallback (hr));
-
-#ifdef UNICODE
-  /* If UNICODE is defined, LPTSTR is LPWSTR which is UTF-16 */
-  ret_text = g_utf16_to_utf8 (error_text, 0, NULL, NULL, NULL);
-#else
-  ret_text = g_strdup (error_text);
-#endif
-
-  LocalFree (error_text);
-  return ret_text;
-}
-
 gboolean
 _gst_wasapi2_result (HRESULT hr, GstDebugCategory * cat, const gchar * file,
     const gchar * function, gint line)
@@ -193,11 +166,23 @@ _gst_wasapi2_result (HRESULT hr, GstDebugCategory * cat, const gchar * file,
 
   if (FAILED (hr)) {
     gchar *error_text = NULL;
+    gboolean free_string = TRUE;
+
+    error_text = g_win32_error_message ((gint) hr);
+    /* g_win32_error_message() seems to be returning empty string for
+     * AUDCLNT_* cases */
+    if (!error_text || strlen (error_text) == 0) {
+      g_free (error_text);
+      error_text = (gchar *) hresult_to_string_fallback (hr);
+
+      free_string = FALSE;
+    }
 
-    error_text = gst_wasapi2_util_hresult_to_string (hr);
     gst_debug_log (cat, GST_LEVEL_WARNING, file, function, line,
         NULL, "WASAPI call failed: 0x%x, %s", (guint) hr, error_text);
-    g_free (error_text);
+
+    if (free_string)
+      g_free (error_text);
 
     ret = FALSE;
   }
index 6d2de48..07c30dc 100644 (file)
@@ -1339,26 +1339,16 @@ _hresult_to_string_fallback (HRESULT hr)
 gchar *
 get_hresult_to_string (HRESULT hr)
 {
-  DWORD flags;
-  gchar *ret_text;
-  LPTSTR error_text = NULL;
-
-  flags = FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER
-      | FORMAT_MESSAGE_IGNORE_INSERTS;
-  FormatMessage (flags, NULL, hr, MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
-      (LPTSTR) & error_text, 0, NULL);
-
-  /* If we couldn't get the error msg, try the fallback switch statement */
-  if (error_text == NULL)
-    return g_strdup (_hresult_to_string_fallback (hr));
-
-#ifdef UNICODE
-  /* If UNICODE is defined, LPTSTR is LPWSTR which is UTF-16 */
-  ret_text = g_utf16_to_utf8 (error_text, 0, NULL, NULL, NULL);
-#else
-  ret_text = g_strdup (error_text);
-#endif
+  gchar *error_text = NULL;
+
+  error_text = g_win32_error_message ((gint) hr);
+  /* g_win32_error_message() doesn't cover all HERESULT return code,
+   * so it could be empty string, or null if there was an error
+   * in g_utf16_to_utf8() */
+  if (!error_text || strlen (error_text) == 0) {
+    g_free (error_text);
+    error_text = g_strdup (_hresult_to_string_fallback (hr));
+  }
 
-  LocalFree (error_text);
-  return ret_text;
+  return error_text;
 }