Opened 20 months ago

Closed 5 months ago

#11539 closed defect (fixed)

ffmpeg hangs when using gdigrab with show_region on Windows

Reported by: elem Owned by: michaelni <michael@niedermayer.cc>
Priority: minor Component: ffmpeg
Version: 7.1 Keywords: gdigrab show_region hang
Cc: elem Blocked By:
Blocking: Reproduced by developer: no
Analyzed by developer: no

Description

Summary of the bug:
How to reproduce:

% ffmpeg -y -f gdigrab -show_region 1 -offset_x 20 -offset_y 20 -video_size 400x80 -i desktop -vcodec libx264 -preset ultrafast -pix_fmt yuv420p "C:\temp\test.mp4"
ffmpeg version 7.1.1-full_build-www.gyan.dev
built with gcc 14.2.0 (Rev1, Built by MSYS2 project)

Then if you move your cursor on the border of the show_region window, Windows will show a "working in background" cursor. If you click it multiple times, Windows will show an "app not responding" window.

ffmpeg doesn't hang on versions 5.x so I did a bisect and the issue appeared with commit d8bf53710f. I guess the big CLI multithreading refactoring done in 6.x and ffmpeg having to draw UI for show_region don't work well together.

Using ffmpeg built with commit d8bf53710f, I have this warning: "Thread message queue blocking; consider raising the thread_queue_size option". With the latest non hanging commit cc2b7f4625, no warning, because thread_queue_size was 0, but if I understand correctly since the CLI multithreading refactoring, thread_queue_size is always at least 1.

I'm not sure if this is reproducible on Linux. Let me know if I can help in any way.

Change History (2)

comment:1 by kvog, 5 months ago

Your guess that this was caused by an interaction between WinAPI and the multithreading refactor is correct. Long story short, CreateWindow and GetMessage are being called on different threads, which prevents the window message queue from being drained.

I opened the following PR to try and resolve this: ​https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/20148

comment:2 by michaelni <michael@niedermayer.cc>, 5 months ago

Owner: set to michaelni <michael@niedermayer.cc>
Resolution: → fixed
Status: new → closed

In dc1128e4/ffmpeg:

avdevice/gdigrab: process window in a separate thread

Move window creation and event processing to a dedicated thread.

GetMessage only processes events from the calling thread's message
queue. Because gdigrab_read_header and gdigrab_read_packet don't run on
the same thread, the message queue was not being drained.

Fixes: #11539

Note: See TracTickets for help on using tickets.