Skip to content
Snippets Groups Projects
Commit 2c4aa515 authored by Vivien Didelot's avatar Vivien Didelot Committed by Tristan Matthews
Browse files

video_device_monitor: restore preferences

Rework the way preferences are stored and restored to a device, when
they are unserialized or when a new device is added.

rationales:

  * getters should not modify a device.
  * when unserializing preferences:
    - reset the corresponding device, if it is present.
    - assign the default to the first preference, or the first plugged.
  * when adding a device:
    - reset its preferences (if any) or store them.
    - assign it as default, if there's no default yet.

Issue: #51243
Change-Id: I5423e43f988cf9b7240dd1271cdf5e5b7f79bfce
parent c98ee4b0
No related branches found
No related tags found
No related merge requests found
...@@ -77,14 +77,6 @@ VideoDeviceMonitor::getSettings(const string& name) ...@@ -77,14 +77,6 @@ VideoDeviceMonitor::getSettings(const string& name)
if (itd == devices_.end()) if (itd == devices_.end())
return VideoSettings(); return VideoSettings();
/*
* Unserialization of device preferences is done after plugged devices were
* added. If preferences are stored for this device, apply them first.
*/
auto itp = findPreferencesByName(name);
if (itp != preferences_.end())
itd->applySettings(*itp);
return itd->getSettings(); return itd->getSettings();
} }
...@@ -97,7 +89,7 @@ VideoDeviceMonitor::applySettings(const string& name, VideoSettings settings) ...@@ -97,7 +89,7 @@ VideoDeviceMonitor::applySettings(const string& name, VideoSettings settings)
return; return;
iter->applySettings(settings); iter->applySettings(settings);
storePreferences(iter->getSettings()); overwritePreferences(iter->getSettings());
} }
string string
...@@ -173,9 +165,21 @@ VideoDeviceMonitor::addDevice(const string& node) ...@@ -173,9 +165,21 @@ VideoDeviceMonitor::addDevice(const string& node)
if (findDeviceByNode(node) != devices_.end()) if (findDeviceByNode(node) != devices_.end())
return; return;
// instantiate a new unique device
VideoDevice dev(node); VideoDevice dev(node);
giveUniqueName(dev, devices_); giveUniqueName(dev, devices_);
// restore its preferences if any, or store the defaults
auto it = findPreferencesByName(dev.name);
if (it != preferences_.end())
dev.applySettings(*it);
else
preferences_.push_back(dev.getSettings());
// in case there is no default device on a fresh run
if (defaultDevice_.empty())
defaultDevice_ = dev.name;
devices_.push_back(dev); devices_.push_back(dev);
notify(); notify();
} }
...@@ -256,7 +260,7 @@ VideoDeviceMonitor::findPreferencesByName(const string& name) ...@@ -256,7 +260,7 @@ VideoDeviceMonitor::findPreferencesByName(const string& name)
} }
void void
VideoDeviceMonitor::storePreferences(VideoSettings settings) VideoDeviceMonitor::overwritePreferences(VideoSettings settings)
{ {
auto it = findPreferencesByName(settings["name"]); auto it = findPreferencesByName(settings["name"]);
if (it != preferences_.end()) if (it != preferences_.end())
...@@ -305,13 +309,17 @@ VideoDeviceMonitor::unserialize(const Conf::YamlNode &node) ...@@ -305,13 +309,17 @@ VideoDeviceMonitor::unserialize(const Conf::YamlNode &node)
if (!devicesNode || devicesNode->getType() != SEQUENCE) { if (!devicesNode || devicesNode->getType() != SEQUENCE) {
ERROR("No 'devices' sequence node! Old config?"); ERROR("No 'devices' sequence node! Old config?");
} else { return;
}
SequenceNode *seqNode = static_cast<SequenceNode *>(devicesNode); SequenceNode *seqNode = static_cast<SequenceNode *>(devicesNode);
Sequence *seq = seqNode->getSequence(); Sequence *seq = seqNode->getSequence();
if (seq->empty()) { if (seq->empty()) {
WARN("Empty video device list"); WARN("Empty video device list");
} else { return;
}
for (const auto &iter : *seq) { for (const auto &iter : *seq) {
MappingNode *devnode = static_cast<MappingNode *>(iter); MappingNode *devnode = static_cast<MappingNode *>(iter);
VideoSettings pref; VideoSettings pref;
...@@ -321,21 +329,21 @@ VideoDeviceMonitor::unserialize(const Conf::YamlNode &node) ...@@ -321,21 +329,21 @@ VideoDeviceMonitor::unserialize(const Conf::YamlNode &node)
devnode->getValue("size", &pref["size"]); devnode->getValue("size", &pref["size"]);
devnode->getValue("rate", &pref["rate"]); devnode->getValue("rate", &pref["rate"]);
storePreferences(pref); overwritePreferences(pref);
// Update the default device if it is plugged // Restore the device preferences if present
if (defaultDevice_.empty()) { auto itd = findDeviceByName(pref["name"]);
auto it = findDeviceByName(pref["name"]); if (itd != devices_.end())
if (it != devices_.end()) itd->applySettings(pref);
defaultDevice_ = it->name;
}
} }
// If no default device, use to the first plugged one // Restore the default device if present, or select the first one
if (defaultDevice_.empty() && !devices_.empty()) const string pref = preferences_.empty() ? "" : preferences_[0]["name"];
defaultDevice_ = devices_[0].name; const string first = devices_.empty() ? "" : devices_[0].name;
} if (findDeviceByName(pref) != devices_.end())
} defaultDevice_ = pref;
else
defaultDevice_ = first;
} }
} // namespace sfl_video } // namespace sfl_video
...@@ -82,7 +82,7 @@ class VideoDeviceMonitor : public Serializable ...@@ -82,7 +82,7 @@ class VideoDeviceMonitor : public Serializable
*/ */
std::vector<VideoSettings> preferences_; std::vector<VideoSettings> preferences_;
void storePreferences(VideoSettings settings); void overwritePreferences(VideoSettings settings);
std::vector<VideoSettings>::iterator findPreferencesByName(const std::string& name); std::vector<VideoSettings>::iterator findPreferencesByName(const std::string& name);
/* /*
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment