]> git.sesse.net Git - casparcg/blobdiff - core/producer/scene/xml_scene_producer.cpp
* Implemented supports for timeline marks in scene_producer. A mark is set on a speci...
[casparcg] / core / producer / scene / xml_scene_producer.cpp
index 666926b6e084e8255e6e726a006146d9d83fbf89..b876e6d619eca026be970e8a2dccc60f5466f0e8 100644 (file)
 * Author: Helge Norberg, helge.norberg@svt.se
 */
 
-#include "../../stdafx.h"
+#include "../../StdAfx.h"
 
 #include "xml_scene_producer.h"
+#include "expression_parser.h"
 
 #include <boost/filesystem.hpp>
+#include <boost/filesystem/fstream.hpp>
 #include <boost/property_tree/ptree.hpp>
 #include <boost/property_tree/xml_parser.hpp>
 
 #include <common/env.h>
 #include <core/producer/frame_producer.h>
 
+#include <future>
+
 #include "scene_producer.h"
+#include "scene_cg_proxy.h"
 
 namespace caspar { namespace core { namespace scene {
 
+void deduce_expression(variable& var, const variable_repository& repo)
+{
+       auto expr_str = var.original_expr();
+       auto trimmed = boost::trim_copy(expr_str);
+
+       if (boost::starts_with(trimmed, L"${") && boost::ends_with(trimmed, L"}"))
+       {
+               expr_str = trimmed.substr(2, expr_str.length() - 3);
+
+               if (var.is<double>())
+                       var.as<double>().bind(parse_expression<double>(expr_str, repo));
+               else if (var.is<bool>())
+                       var.as<bool>().bind(parse_expression<bool>(expr_str, repo));
+               else if (var.is<std::wstring>())
+                       var.as<std::wstring>().bind(parse_expression<std::wstring>(expr_str, repo));
+       }
+       else if (!expr_str.empty())
+       {
+               var.from_string(expr_str);
+       }
+}
+
+void init(module_dependencies dependencies)
+{
+       dependencies.cg_registry->register_cg_producer(
+                       L"scene",
+                       { L".scene" },
+                       [](const std::wstring& filename) { return ""; },
+                       [](const spl::shared_ptr<core::frame_producer>& producer)
+                       {
+                               return spl::make_shared<core::scene::scene_cg_proxy>(producer);
+                       },
+                       [](
+                       const spl::shared_ptr<core::frame_factory>& factory,
+                       const core::video_format_desc& format_desc,
+                       const std::wstring& filename)
+                       {
+                               return create_xml_scene_producer(factory, format_desc, { filename });
+                       },
+                       false);
+}
+
 spl::shared_ptr<core::frame_producer> create_xml_scene_producer(
                const spl::shared_ptr<core::frame_factory>& frame_factory,
                const core::video_format_desc& format_desc,
@@ -42,13 +89,13 @@ spl::shared_ptr<core::frame_producer> create_xml_scene_producer(
        if (params.empty())
                return core::frame_producer::empty();
 
-       std::wstring filename = env::media_folder() + L"\\" + params[0] + L".xml";
+       std::wstring filename = env::template_folder() + L"/" + params[0] + L".scene";
        
        if (!boost::filesystem::is_regular_file(boost::filesystem::path(filename)))
                return core::frame_producer::empty();
 
        boost::property_tree::wptree root;
-       std::wifstream file(filename);
+       boost::filesystem::wifstream file(filename);
        boost::property_tree::read_xml(
                        file,
                        root,
@@ -58,20 +105,27 @@ spl::shared_ptr<core::frame_producer> create_xml_scene_producer(
        int width = root.get<int>(L"scene.<xmlattr>.width");
        int height = root.get<int>(L"scene.<xmlattr>.height");
 
-       auto scene = spl::make_shared<scene_producer>(width, height);
+       auto scene = spl::make_shared<scene_producer>(width, height, format_desc);
 
-       BOOST_FOREACH(auto elem, root.get_child(L"scene.variables"))
+       for (auto elem : root.get_child(L"scene.variables"))
        {
                auto type = elem.second.get<std::wstring>(L"<xmlattr>.type");
-
-               if (type == L"double")
-                       scene->create_variable<double>(
-                                       L"variable." + elem.second.get<std::wstring>(L"<xmlattr>.id"),
-                                       false,
-                                       elem.second.get_value<std::wstring>());
+               auto id = elem.second.get<std::wstring>(L"<xmlattr>.id");
+               auto is_public = elem.second.get(L"<xmlattr>.public", false);
+               auto expr = elem.second.get_value<std::wstring>();
+
+               if (!is_public)
+                       id = L"variable." + id;
+
+               if (type == L"number")
+                       scene->create_variable<double>(id, is_public, expr);
+               else if (type == L"string")
+                       scene->create_variable<std::wstring>(id, is_public, expr);
+               else if (type == L"bool")
+                       scene->create_variable<bool>(id, is_public, expr);
        }
 
-       BOOST_FOREACH(auto elem, root.get_child(L"scene.layers"))
+       for (auto& elem : root.get_child(L"scene.layers"))
        {
                auto id = elem.second.get<std::wstring>(L"<xmlattr>.id");
                auto producer = create_producer(frame_factory, format_desc, elem.second.get<std::wstring>(L"producer"));
@@ -81,16 +135,65 @@ spl::shared_ptr<core::frame_producer> create_xml_scene_producer(
                layer.hidden = scene->create_variable<bool>(variable_prefix + L"hidden", false, elem.second.get(L"hidden", L"false"));
                layer.position.x = scene->create_variable<double>(variable_prefix + L"x", false, elem.second.get<std::wstring>(L"x"));
                layer.position.y = scene->create_variable<double>(variable_prefix + L"y", false, elem.second.get<std::wstring>(L"y"));
+               layer.anchor.x = scene->create_variable<double>(variable_prefix + L"anchor_x", false, elem.second.get<std::wstring>(L"anchor_x", L"0.0"));
+               layer.anchor.y = scene->create_variable<double>(variable_prefix + L"anchor_y", false, elem.second.get<std::wstring>(L"anchor_y", L"0.0"));
+               layer.rotation = scene->create_variable<double>(variable_prefix + L"rotation", false, elem.second.get<std::wstring>(L"rotation", L"0.0"));
+               layer.crop.upper_left.x = scene->create_variable<double>(variable_prefix + L"crop_upper_left_x", false, elem.second.get<std::wstring>(L"crop_upper_left_x", L"0.0"));
+               layer.crop.upper_left.y = scene->create_variable<double>(variable_prefix + L"crop_upper_left_y", false, elem.second.get<std::wstring>(L"crop_upper_left_y", L"0.0"));
+               layer.crop.lower_right.x = scene->create_variable<double>(variable_prefix + L"crop_lower_right_x", false, elem.second.get<std::wstring>(L"crop_lower_right_x", layer.producer.get()->pixel_constraints().width.as<std::wstring>().get()));
+               layer.crop.lower_right.y = scene->create_variable<double>(variable_prefix + L"crop_lower_right_y", false, elem.second.get<std::wstring>(L"crop_lower_right_y", layer.producer.get()->pixel_constraints().height.as<std::wstring>().get()));
+               layer.perspective.upper_left.x = scene->create_variable<double>(variable_prefix + L"perspective_upper_left_x", false, elem.second.get<std::wstring>(L"perspective_upper_left_x", L"0.0"));
+               layer.perspective.upper_left.y = scene->create_variable<double>(variable_prefix + L"perspective_upper_left_y", false, elem.second.get<std::wstring>(L"perspective_upper_left_y", L"0.0"));
+               layer.perspective.upper_right.x = scene->create_variable<double>(variable_prefix + L"perspective_upper_right_x", false, elem.second.get<std::wstring>(L"perspective_upper_right_x", layer.producer.get()->pixel_constraints().width.as<std::wstring>().get()));
+               layer.perspective.upper_right.y = scene->create_variable<double>(variable_prefix + L"perspective_upper_right_y", false, elem.second.get<std::wstring>(L"perspective_upper_right_y", L"0.0"));
+               layer.perspective.lower_right.x = scene->create_variable<double>(variable_prefix + L"perspective_lower_right_x", false, elem.second.get<std::wstring>(L"perspective_lower_right_x", layer.producer.get()->pixel_constraints().width.as<std::wstring>().get()));
+               layer.perspective.lower_right.y = scene->create_variable<double>(variable_prefix + L"perspective_lower_right_y", false, elem.second.get<std::wstring>(L"perspective_lower_right_y", layer.producer.get()->pixel_constraints().height.as<std::wstring>().get()));
+               layer.perspective.lower_left.x = scene->create_variable<double>(variable_prefix + L"perspective_lower_left_x", false, elem.second.get<std::wstring>(L"perspective_lower_left_x", L"0.0"));
+               layer.perspective.lower_left.y = scene->create_variable<double>(variable_prefix + L"perspective_lower_left_y", false, elem.second.get<std::wstring>(L"perspective_lower_left_y", layer.producer.get()->pixel_constraints().height.as<std::wstring>().get()));
+
+               layer.adjustments.opacity = scene->create_variable<double>(variable_prefix + L"adjustment.opacity", false, elem.second.get(L"adjustments.opacity", L"1.0"));
+               layer.is_key = scene->create_variable<bool>(variable_prefix + L"is_key", false, elem.second.get(L"is_key", L"false"));
+               layer.use_mipmap = scene->create_variable<bool>(variable_prefix + L"use_mipmap", false, elem.second.get(L"use_mipmap", L"false"));
+               layer.blend_mode = scene->create_variable<std::wstring>(variable_prefix + L"blend_mode", false, elem.second.get(L"blend_mode", L"normal")).transformed([](const std::wstring& b) { return get_blend_mode(b); });
+               layer.chroma_key.key = scene->create_variable<std::wstring>(variable_prefix + L"chroma_key.key", false, elem.second.get(L"chroma_key.key", L"none")).transformed([](const std::wstring& k) { return get_chroma_mode(k); });
+               layer.chroma_key.threshold = scene->create_variable<double>(variable_prefix + L"chroma_key.threshold", false, elem.second.get(L"chroma_key.threshold", L"0.0"));
+               layer.chroma_key.softness = scene->create_variable<double>(variable_prefix + L"chroma_key.softness", false, elem.second.get(L"chroma_key.softness", L"0.0"));
+               layer.chroma_key.spill = scene->create_variable<double>(variable_prefix + L"chroma_key.spill", false, elem.second.get(L"chroma_key.spill", L"0.0"));
 
                scene->create_variable<double>(variable_prefix + L"width", false) = layer.producer.get()->pixel_constraints().width;
                scene->create_variable<double>(variable_prefix + L"height", false) = layer.producer.get()->pixel_constraints().height;
+
+               for (auto& var_name : producer->get_variables())
+               {
+                       auto& var = producer->get_variable(var_name);
+                       auto expr = elem.second.get<std::wstring>(L"parameters." + var_name, L"");
+
+                       if (var.is<double>())
+                               scene->create_variable<double>(variable_prefix + L"parameter." + var_name, false, expr) = var.as<double>();
+                       else if (var.is<std::wstring>())
+                               scene->create_variable<std::wstring>(variable_prefix + L"parameter." + var_name, false, expr) = var.as<std::wstring>();
+                       else if (var.is<bool>())
+                               scene->create_variable<bool>(variable_prefix + L"parameter." + var_name, false, expr) = var.as<bool>();
+               }
+       }
+
+       if (root.get_child_optional(L"scene.marks"))
+       {
+               for (auto& mark : root.get_child(L"scene.marks"))
+               {
+                       auto at = mark.second.get<int64_t>(L"<xmlattr>.at");
+                       auto type = get_mark_action(mark.second.get<std::wstring>(L"<xmlattr>.type"));
+                       auto label = mark.second.get(L"<xmlattr>.label", L"");
+
+                       scene->add_mark(at, type, label);
+               }
        }
 
-       BOOST_FOREACH(auto& elem, root.get_child(L"scene.timelines"))
+       for (auto& elem : root.get_child(L"scene.timelines"))
        {
                auto& variable = scene->get_variable(elem.second.get<std::wstring>(L"<xmlattr>.variable"));
 
-               BOOST_FOREACH(auto& k, elem.second)
+               for (auto& k : elem.second)
                {
                        if (k.first == L"<xmlattr>")
                                continue;
@@ -105,17 +208,21 @@ spl::shared_ptr<core::frame_producer> create_xml_scene_producer(
                }
        }
 
-       BOOST_FOREACH(auto& elem, root.get_child(L"scene.parameters"))
+       auto repo = [&scene](const std::wstring& name) -> variable&
        {
-               auto& variable = scene->get_variable(elem.second.get<std::wstring>(L"<xmlattr>.variable"));
-               auto id = elem.second.get<std::wstring>(L"<xmlattr>.id");
+               return scene->get_variable(name); 
+       };
 
-               if (variable.is<double>())
-                       scene->create_variable<double>(id, true) = variable.as<double>();
-               else if (variable.is<std::wstring>())
-                       scene->create_variable<std::wstring>(id, true) = variable.as<std::wstring>();
+       for (auto& var_name : scene->get_variables())
+       {
+               deduce_expression(scene->get_variable(var_name), repo);
        }
 
+       auto params2 = params;
+       params2.erase(params2.begin());
+
+       scene->call(params2);
+
        return scene;
 }