Skip to content

Commit

Permalink
fix rare race condition in debug vid src
Browse files Browse the repository at this point in the history
  • Loading branch information
Green-Sky committed Dec 22, 2024
1 parent ac951b2 commit 586b49f
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/debug_video_tap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ struct DebugVideoTapSink : public FrameStream2SinkI<SDLVideoFrame> {
};

struct DebugVideoTestSource : public FrameStream2SourceI<SDLVideoFrame> {
std::mutex _readers_mutex;
std::vector<std::shared_ptr<LockedFrameStream2<SDLVideoFrame>>> _readers;

std::atomic_bool _stop {false};
Expand All @@ -94,6 +95,9 @@ struct DebugVideoTestSource : public FrameStream2SourceI<SDLVideoFrame> {
std::cout << "DVTS: starting new test video source\n";
_thread = std::thread([this](void) {
while (!_stop) {
std::this_thread::sleep_for(std::chrono::milliseconds(50));

std::lock_guard lg{_readers_mutex};
if (!_readers.empty()) {
auto* surf = SDL_CreateSurface(960, 720, SDL_PIXELFORMAT_RGBA32);

Expand All @@ -113,8 +117,6 @@ struct DebugVideoTestSource : public FrameStream2SourceI<SDLVideoFrame> {

SDL_DestroySurface(surf);
}

std::this_thread::sleep_for(std::chrono::milliseconds(50));
}
});
}
Expand All @@ -124,10 +126,12 @@ struct DebugVideoTestSource : public FrameStream2SourceI<SDLVideoFrame> {
}

std::shared_ptr<FrameStream2I<SDLVideoFrame>> subscribe(void) override {
std::lock_guard lg{_readers_mutex};
return _readers.emplace_back(std::make_shared<LockedFrameStream2<SDLVideoFrame>>());
}

bool unsubscribe(const std::shared_ptr<FrameStream2I<SDLVideoFrame>>& sub) override {
std::lock_guard lg{_readers_mutex};
for (auto it = _readers.cbegin(); it != _readers.cend(); it++) {
if (it->get() == sub.get()) {
_readers.erase(it);
Expand Down

0 comments on commit 586b49f

Please sign in to comment.