Cleanup Session loading (prepare for session history)

This commit is contained in:
brunoherbelin
2020-10-03 18:45:30 +02:00
parent d563ee14a9
commit 5421b5e926
6 changed files with 81 additions and 79 deletions

View File

@@ -677,10 +677,10 @@ void Mixer::load(const std::string& filename)
if (sessionLoaders_.empty()) { if (sessionLoaders_.empty()) {
// Start async thread for loading the session // Start async thread for loading the session
// Will be obtained in the future in update() // Will be obtained in the future in update()
sessionLoaders_.emplace_back( std::async(std::launch::async, loadSession_, filename) ); sessionLoaders_.emplace_back( std::async(std::launch::async, Session::load, filename) );
} }
#else #else
set( loadSession_(filename) ); set( Session::load(filename) );
#endif #endif
} }
@@ -715,10 +715,10 @@ void Mixer::import(const std::string& filename)
if (sessionImporters_.empty()) { if (sessionImporters_.empty()) {
// Start async thread for loading the session // Start async thread for loading the session
// Will be obtained in the future in update() // Will be obtained in the future in update()
sessionImporters_.emplace_back( std::async(std::launch::async, loadSession_, filename) ); sessionImporters_.emplace_back( std::async(std::launch::async, Session::load, filename) );
} }
#else #else
merge( loadSession_(filename) ); merge( Session::load(filename) );
#endif #endif
} }

View File

@@ -340,25 +340,11 @@ void Session::unlock()
} }
Session *loadSession_(const std::string& filename) Session *Session::load(const std::string& filename)
{ {
Session *s = new Session; SessionCreator creator;
creator.load(filename);
if (s) { return creator.session();
// actual loading of xml file
SessionCreator creator( s );
if (creator.load(filename)) {
// loaded ok
s->setFilename(filename);
}
else {
// error loading
delete s;
s = nullptr;
}
}
return s;
} }

View File

@@ -14,6 +14,8 @@ public:
Session(); Session();
~Session(); ~Session();
static Session *load(const std::string& filename);
// add given source into the session // add given source into the session
SourceList::iterator addSource (Source *s); SourceList::iterator addSource (Source *s);
@@ -92,6 +94,4 @@ protected:
}; };
Session *loadSession_(const std::string& filename);
#endif // SESSION_H #endif // SESSION_H

View File

@@ -42,25 +42,21 @@ std::string SessionCreator::info(const std::string& filename)
return ret; return ret;
} }
SessionCreator::SessionCreator(Session *session): Visitor(), session_(session) SessionCreator::SessionCreator(): SessionLoader(nullptr)
{ {
} }
SessionCreator::~SessionCreator() void SessionCreator::load(const std::string& filename)
{
}
bool SessionCreator::load(const std::string& filename)
{ {
XMLError eResult = xmlDoc_.LoadFile(filename.c_str()); XMLError eResult = xmlDoc_.LoadFile(filename.c_str());
if ( XMLResultError(eResult)) if ( XMLResultError(eResult))
return false; return;
XMLElement *header = xmlDoc_.FirstChildElement(APP_NAME); XMLElement *header = xmlDoc_.FirstChildElement(APP_NAME);
if (header == nullptr) { if (header == nullptr) {
Log::Warning("%s is not a %s session file.", filename.c_str(), APP_NAME); Log::Warning("%s is not a %s session file.", filename.c_str(), APP_NAME);
return false; return;
} }
int version_major = -1, version_minor = -1; int version_major = -1, version_minor = -1;
@@ -68,25 +64,41 @@ bool SessionCreator::load(const std::string& filename)
header->QueryIntAttribute("minor", &version_minor); header->QueryIntAttribute("minor", &version_minor);
if (version_major != XML_VERSION_MAJOR || version_minor != XML_VERSION_MINOR){ if (version_major != XML_VERSION_MAJOR || version_minor != XML_VERSION_MINOR){
Log::Warning("%s is in a different versions of session file. Loading might fail.", filename.c_str()); Log::Warning("%s is in a different versions of session file. Loading might fail.", filename.c_str());
return false; return;
} }
session_ = new Session;
// ok, ready to read sources // ok, ready to read sources
loadSession( xmlDoc_.FirstChildElement("Session") ); SessionLoader::load( xmlDoc_.FirstChildElement("Session") );
// excellent, session was created: load optionnal config
if (session_){
loadConfig( xmlDoc_.FirstChildElement("Views") );
}
return true; // load optionnal config
loadConfig( xmlDoc_.FirstChildElement("Views") );
session_->setFilename(filename);
} }
void SessionCreator::loadSession(XMLElement *sessionNode)
void SessionCreator::loadConfig(XMLElement *viewsNode)
{ {
if (sessionNode != nullptr) { if (viewsNode != nullptr) {
// create a session if not provided // ok, ready to read views
if (!session_) SessionLoader::XMLToNode( viewsNode->FirstChildElement("Mixing"), *session_->config(View::MIXING));
session_ = new Session; SessionLoader::XMLToNode( viewsNode->FirstChildElement("Geometry"), *session_->config(View::GEOMETRY));
SessionLoader::XMLToNode( viewsNode->FirstChildElement("Layer"), *session_->config(View::LAYER));
SessionLoader::XMLToNode( viewsNode->FirstChildElement("Rendering"), *session_->config(View::RENDERING));
}
}
SessionLoader::SessionLoader(Session *session): Visitor(), session_(session)
{
}
void SessionLoader::load(XMLElement *sessionNode)
{
if (sessionNode != nullptr && session_ != nullptr) {
int counter = 0; int counter = 0;
XMLElement* sourceNode = sessionNode->FirstChildElement("Source"); XMLElement* sourceNode = sessionNode->FirstChildElement("Source");
@@ -124,6 +136,11 @@ void SessionCreator::loadSession(XMLElement *sessionNode)
else if ( std::string(pType) == "DeviceSource") { else if ( std::string(pType) == "DeviceSource") {
load_source = new DeviceSource; load_source = new DeviceSource;
} }
// avoid non recognized types
if (!load_source)
continue;
// add source to session // add source to session
session_->addSource(load_source); session_->addSource(load_source);
} }
@@ -164,18 +181,8 @@ void SessionCreator::loadSession(XMLElement *sessionNode)
Log::Warning("Session seems empty."); Log::Warning("Session seems empty.");
} }
void SessionCreator::loadConfig(XMLElement *viewsNode)
{
if (viewsNode != nullptr) {
// ok, ready to read views
SessionCreator::XMLToNode( viewsNode->FirstChildElement("Mixing"), *session_->config(View::MIXING));
SessionCreator::XMLToNode( viewsNode->FirstChildElement("Geometry"), *session_->config(View::GEOMETRY));
SessionCreator::XMLToNode( viewsNode->FirstChildElement("Layer"), *session_->config(View::LAYER));
SessionCreator::XMLToNode( viewsNode->FirstChildElement("Rendering"), *session_->config(View::RENDERING));
}
}
void SessionCreator::XMLToNode(tinyxml2::XMLElement *xml, Node &n) void SessionLoader::XMLToNode(tinyxml2::XMLElement *xml, Node &n)
{ {
if (xml != nullptr){ if (xml != nullptr){
XMLElement *node = xml->FirstChildElement("Node"); XMLElement *node = xml->FirstChildElement("Node");
@@ -194,12 +201,12 @@ void SessionCreator::XMLToNode(tinyxml2::XMLElement *xml, Node &n)
} }
} }
void SessionCreator::visit(Node &n) void SessionLoader::visit(Node &n)
{ {
XMLToNode(xmlCurrent_, n); XMLToNode(xmlCurrent_, n);
} }
void SessionCreator::visit(MediaPlayer &n) void SessionLoader::visit(MediaPlayer &n)
{ {
XMLElement* mediaplayerNode = xmlCurrent_->FirstChildElement("MediaPlayer"); XMLElement* mediaplayerNode = xmlCurrent_->FirstChildElement("MediaPlayer");
if (mediaplayerNode) { if (mediaplayerNode) {
@@ -239,7 +246,7 @@ void SessionCreator::visit(MediaPlayer &n)
} }
} }
void SessionCreator::visit(Shader &n) void SessionLoader::visit(Shader &n)
{ {
XMLElement* color = xmlCurrent_->FirstChildElement("color"); XMLElement* color = xmlCurrent_->FirstChildElement("color");
if ( color ) { if ( color ) {
@@ -253,7 +260,7 @@ void SessionCreator::visit(Shader &n)
} }
} }
void SessionCreator::visit(ImageShader &n) void SessionLoader::visit(ImageShader &n)
{ {
const char *pType = xmlCurrent_->Attribute("type"); const char *pType = xmlCurrent_->Attribute("type");
if ( std::string(pType) != "ImageShader" ) if ( std::string(pType) != "ImageShader" )
@@ -266,7 +273,7 @@ void SessionCreator::visit(ImageShader &n)
} }
} }
void SessionCreator::visit(ImageProcessingShader &n) void SessionLoader::visit(ImageProcessingShader &n)
{ {
const char *pType = xmlCurrent_->Attribute("type"); const char *pType = xmlCurrent_->Attribute("type");
if ( std::string(pType) != "ImageProcessingShader" ) if ( std::string(pType) != "ImageProcessingShader" )
@@ -297,7 +304,7 @@ void SessionCreator::visit(ImageProcessingShader &n)
tinyxml2::XMLElementToGLM( chromakey->FirstChildElement("vec4"), n.chromakey); tinyxml2::XMLElementToGLM( chromakey->FirstChildElement("vec4"), n.chromakey);
} }
void SessionCreator::visit (Source& s) void SessionLoader::visit (Source& s)
{ {
XMLElement* sourceNode = xmlCurrent_; XMLElement* sourceNode = xmlCurrent_;
const char *pName = sourceNode->Attribute("name"); const char *pName = sourceNode->Attribute("name");
@@ -324,7 +331,7 @@ void SessionCreator::visit (Source& s)
xmlCurrent_ = sourceNode; xmlCurrent_ = sourceNode;
} }
void SessionCreator::visit (MediaSource& s) void SessionLoader::visit (MediaSource& s)
{ {
// set uri // set uri
XMLElement* uriNode = xmlCurrent_->FirstChildElement("uri"); XMLElement* uriNode = xmlCurrent_->FirstChildElement("uri");
@@ -337,7 +344,7 @@ void SessionCreator::visit (MediaSource& s)
s.mediaplayer()->accept(*this); s.mediaplayer()->accept(*this);
} }
void SessionCreator::visit (SessionSource& s) void SessionLoader::visit (SessionSource& s)
{ {
// set uri // set uri
XMLElement* pathNode = xmlCurrent_->FirstChildElement("path"); XMLElement* pathNode = xmlCurrent_->FirstChildElement("path");
@@ -348,7 +355,7 @@ void SessionCreator::visit (SessionSource& s)
} }
void SessionCreator::visit (PatternSource& s) void SessionLoader::visit (PatternSource& s)
{ {
uint p = xmlCurrent_->UnsignedAttribute("pattern"); uint p = xmlCurrent_->UnsignedAttribute("pattern");
@@ -360,7 +367,7 @@ void SessionCreator::visit (PatternSource& s)
s.setPattern(p, resolution); s.setPattern(p, resolution);
} }
void SessionCreator::visit (DeviceSource& s) void SessionLoader::visit (DeviceSource& s)
{ {
const char *devname = xmlCurrent_->Attribute("device"); const char *devname = xmlCurrent_->Attribute("device");
s.setDevice(devname); s.setDevice(devname);

View File

@@ -6,23 +6,15 @@
class Session; class Session;
class SessionCreator : public Visitor {
tinyxml2::XMLDocument xmlDoc_;
tinyxml2::XMLElement *xmlCurrent_;
Session *session_;
void loadSession(tinyxml2::XMLElement *sessionNode);
void loadConfig(tinyxml2::XMLElement *viewsNode);
class SessionLoader : public Visitor {
public: public:
SessionCreator(Session *session = nullptr); SessionLoader(Session *session);
~SessionCreator();
bool load(const std::string& filename);
inline Session *session() const { return session_; } inline Session *session() const { return session_; }
void load(tinyxml2::XMLElement *sessionNode);
// Elements of Scene // Elements of Scene
void visit(Node& n) override; void visit(Node& n) override;
@@ -51,9 +43,26 @@ public:
void visit (PatternSource& s) override; void visit (PatternSource& s) override;
void visit (DeviceSource& s) override; void visit (DeviceSource& s) override;
static std::string info(const std::string& filename);
static void XMLToNode(tinyxml2::XMLElement *xml, Node &n); static void XMLToNode(tinyxml2::XMLElement *xml, Node &n);
protected:
tinyxml2::XMLElement *xmlCurrent_;
Session *session_;
}; };
class SessionCreator : public SessionLoader {
tinyxml2::XMLDocument xmlDoc_;
void loadConfig(tinyxml2::XMLElement *viewsNode);
public:
SessionCreator();
void load(const std::string& filename);
static std::string info(const std::string& filename);
};
#endif // SESSIONCREATOR_H #endif // SESSIONCREATOR_H

View File

@@ -77,7 +77,7 @@ void SessionSource::load(const std::string &p)
session_ = new Session; session_ = new Session;
else else
// launch a thread to load the session file // launch a thread to load the session file
sessionLoader_ = std::async(std::launch::async, loadSession_, path_); sessionLoader_ = std::async(std::launch::async, Session::load, path_);
Log::Notify("Opening %s", p.c_str()); Log::Notify("Opening %s", p.c_str());
} }