wang-bin / QtAV

A cross-platform multimedia framework based on Qt and FFmpeg. 基于Qt和FFmpeg的跨平台高性能音视频播放框架. Recommand to use new sdk https://github.com/wang-bin/mdk-sdk
http://qtav.org
3.94k stars 1.5k forks source link

VideoFrameExtractor: Fix race condition in synchronous extraction mode #1390

Open Tereius opened 2 years ago

Tereius commented 2 years ago

There is a race condition between the ExtractThread running p->releaseResourceInternal() (and unloading the demuxer) and the main thread calling extract() and loading the demuxer inside checkAndOpen() if run in synchronous extraction mode.

// Run in main thread
auto mpExtractor = new QtAV::VideoFrameExtractor();
mpExtractor->setAsync(false);
mpExtractor->setSource("file://whatever.mp4"); // This call will unnecessarily start the ExtractThread which will unload the demuxer in p->releaseResourceInternal()
mpExtractor->setPosition(0);
mpExtractor->extract(); // The frame extraction is only successful if the demuxer is loaded. Racing happens because the ExtractThread simultaneously unloads the demuxer while the main thread loads the demuxer in checkAndOpen()

The solution is not to start the ExtractThread at all if async equals false.