From 5249b09e044d2eff9f756c427049e9e8310a979c Mon Sep 17 00:00:00 2001 From: Seungha Yang Date: Sat, 25 Feb 2023 20:40:42 +0900 Subject: [PATCH] examples: win32-videooverlay: Fix for window freezing Read and flush console buffer from the console thread immediately, instead of main thread. Otherwise (if main thread is busy) the console thread will keep adding idle source and then main thread will be unresponsive. Part-of: --- .../examples/overlay/win32-videooverlay.c | 29 ++++++++++--------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/subprojects/gst-plugins-base/tests/examples/overlay/win32-videooverlay.c b/subprojects/gst-plugins-base/tests/examples/overlay/win32-videooverlay.c index 622256b6fc..18c4135365 100644 --- a/subprojects/gst-plugins-base/tests/examples/overlay/win32-videooverlay.c +++ b/subprojects/gst-plugins-base/tests/examples/overlay/win32-videooverlay.c @@ -356,19 +356,13 @@ print_keyboard_help (void) } static gboolean -win32_kb_source_cb (Win32KeyHandler * handler) +win32_kb_source_cb (INPUT_RECORD * buffer) { - HANDLE h_input = handler->console_handle; - INPUT_RECORD buffer; - DWORD n; - - if (PeekConsoleInput (h_input, &buffer, 1, &n) && n == 1) { - ReadConsoleInput (h_input, &buffer, 1, &n); - - if (buffer.EventType == KEY_EVENT && buffer.Event.KeyEvent.bKeyDown) { + { + if (buffer->EventType == KEY_EVENT && buffer->Event.KeyEvent.bKeyDown) { gchar key_val[2] = { 0 }; - switch (buffer.Event.KeyEvent.wVirtualKeyCode) { + switch (buffer->Event.KeyEvent.wVirtualKeyCode) { case VK_RIGHT: gst_println ("Move xpos to %d", x++); gst_video_overlay_set_render_rectangle (GST_VIDEO_OVERLAY (sink), @@ -390,7 +384,7 @@ win32_kb_source_cb (Win32KeyHandler * handler) x, y, width, height); break; default: - key_val[0] = buffer.Event.KeyEvent.uChar.AsciiChar; + key_val[0] = buffer->Event.KeyEvent.uChar.AsciiChar; switch (key_val[0]) { case '<': gst_println ("Decrease width to %d", width--); @@ -446,7 +440,8 @@ win32_kb_thread (gpointer user_data) while (TRUE) { DWORD ret = WaitForMultipleObjects (2, handles, FALSE, INFINITE); - static guint i = 0; + INPUT_RECORD buffer; + DWORD n; if (ret == WAIT_FAILED) { g_warning ("WaitForMultipleObject Failed"); @@ -461,7 +456,15 @@ win32_kb_thread (gpointer user_data) } g_mutex_unlock (&handler->lock); - g_idle_add ((GSourceFunc) win32_kb_source_cb, handler); + if (PeekConsoleInput (handler->console_handle, &buffer, 1, &n) && n == 1) { + INPUT_RECORD *record; + + record = g_new0 (INPUT_RECORD, 1); + ReadConsoleInput (handler->console_handle, record, 1, &n); + + g_idle_add_full (G_PRIORITY_DEFAULT_IDLE, + (GSourceFunc) win32_kb_source_cb, record, (GDestroyNotify) g_free); + } } return NULL;