diff --git a/sys/d3d11/gstd3d11utils.c b/sys/d3d11/gstd3d11utils.c index 9e342c08f4..6cf9a77dea 100644 --- a/sys/d3d11/gstd3d11utils.c +++ b/sys/d3d11/gstd3d11utils.c @@ -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; diff --git a/sys/mediafoundation/gstmfutils.cpp b/sys/mediafoundation/gstmfutils.cpp index cecf80cf82..77793a1a92 100644 --- a/sys/mediafoundation/gstmfutils.cpp +++ b/sys/mediafoundation/gstmfutils.cpp @@ -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; diff --git a/sys/wasapi/gstwasapiutil.c b/sys/wasapi/gstwasapiutil.c index 1651bdf808..61076df842 100644 --- a/sys/wasapi/gstwasapiutil.c +++ b/sys/wasapi/gstwasapiutil.c @@ -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 * diff --git a/sys/wasapi2/gstwasapi2util.c b/sys/wasapi2/gstwasapi2util.c index 0bbce301a4..81109d020a 100644 --- a/sys/wasapi2/gstwasapi2util.c +++ b/sys/wasapi2/gstwasapi2util.c @@ -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; } diff --git a/sys/winscreencap/dxgicapture.c b/sys/winscreencap/dxgicapture.c index 6d2de48fae..07c30dc702 100644 --- a/sys/winscreencap/dxgicapture.c +++ b/sys/winscreencap/dxgicapture.c @@ -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); + 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)); + } - /* 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; }