From 40ee25ff8e793eedb21d8216c1f33a911a08e762 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Blin?=
 <sebastien.blin@savoirfairelinux.com>
Date: Wed, 15 Dec 2021 15:36:53 -0500
Subject: [PATCH] videomanager: protect double video input creation

getVideoInput is called in two different place:
+ openVideoInput from the client
+ VideoRtpSession::startSender() from the daemon
Nothing is blocking both sides to do it at the same time.
In this case, two VideoInputs can be created, causing the second
one to fail because the device is busy (used by the first one)
and causing weird results (i.e. sometimes the video is not shown).

Change-Id: I23e34a95efe4972fbe111c19f0f0989f5e8ae8a9
---
 src/client/videomanager.cpp | 1 +
 src/client/videomanager.h   | 1 +
 2 files changed, 2 insertions(+)

diff --git a/src/client/videomanager.cpp b/src/client/videomanager.cpp
index 69aa47a0be..a6ef30ed63 100644
--- a/src/client/videomanager.cpp
+++ b/src/client/videomanager.cpp
@@ -699,6 +699,7 @@ std::shared_ptr<video::VideoInput>
 getVideoInput(const std::string& id, video::VideoInputMode inputMode)
 {
     auto& vmgr = Manager::instance().getVideoManager();
+    std::lock_guard<std::mutex> lk(vmgr.videoMutex);
     auto it = vmgr.videoInputs.find(id);
     if (it != vmgr.videoInputs.end()) {
         if (auto input = it->second.lock()) {
diff --git a/src/client/videomanager.h b/src/client/videomanager.h
index 34767eefdd..c910baba10 100644
--- a/src/client/videomanager.h
+++ b/src/client/videomanager.h
@@ -56,6 +56,7 @@ public:
     std::map<std::string, std::weak_ptr<AudioInput>, std::less<>> audioInputs;
     std::map<std::string, std::weak_ptr<video::VideoInput>, std::less<>> videoInputs;
     std::mutex audioMutex;
+    std::mutex videoMutex;
     bool hasRunningPlayers();
     std::shared_ptr<video::VideoInput> getVideoInput(std::string_view id) const
     {
-- 
GitLab