plugins: Use g_win32_error_message for HRESULT to string conversion

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>
This commit is contained in:
Seungha Yang 2020-07-16 19:26:55 +09:00
parent f68c8d853b
commit c1093e3481
5 changed files with 45 additions and 119 deletions

View 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;

View 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;

View 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;
gchar *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);
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));
}
/* 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;
return error_text;
}
static IMMDeviceEnumerator *

View 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,10 +166,22 @@ _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);
if (free_string)
g_free (error_text);
ret = FALSE;

View 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;
gchar *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;
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));
}
return error_text;
}