]> git.sesse.net Git - casparcg/blobdiff - core/producer/scene/xml_scene_producer.cpp
[scene_producer] Added possibility to CALL/CG PLAY/CG STOP/CG NEXT/CG INVOKE layers...
[casparcg] / core / producer / scene / xml_scene_producer.cpp
index 666926b6e084e8255e6e726a006146d9d83fbf89..10c4aca6db751395504ac21c4bad8dc005423b86 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 <common/os/filesystem.h>
+#include <common/filesystem.h>
+#include <common/ptree.h>
 #include <core/producer/frame_producer.h>
+#include <core/help/help_repository.h>
+#include <core/help/help_sink.h>
+
+#include <future>
+#include <vector>
 
 #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 describe_xml_scene_producer(core::help_sink& sink, const core::help_repository& repo)
+{
+       sink.short_description(L"A simple producer for dynamic graphics using .scene files.");
+       sink.syntax(L"[.scene_filename:string] {[param1:string] [value1:string]} {[param2:string] [value2:string]} ...");
+       sink.para()->text(L"A simple producer that looks in the ")->code(L"templates")->text(L" folder for .scene files.");
+       sink.para()->text(L"The .scene file is a simple XML document containing variables, layers and timelines.");
+       sink.example(L">> PLAY 1-10 scene_name_sign FIRSTNAME \"John\" LASTNAME \"Doe\"", L"loads and plays templates/scene_name_sign.scene and sets the variables FIRSTNAME and LASTNAME.");
+       sink.para()->text(L"The scene producer also supports setting the variables while playing via the CALL command:");
+       sink.example(L">> CALL 1-10 FIRSTNAME \"Jane\"", L"changes the variable FIRSTNAME on an already loaded scene.");
+}
+
 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,
+               const core::frame_producer_dependencies& dependencies,
                const std::vector<std::wstring>& params)
 {
        if (params.empty())
                return core::frame_producer::empty();
 
-       std::wstring filename = env::media_folder() + L"\\" + params[0] + L".xml";
-       
-       if (!boost::filesystem::is_regular_file(boost::filesystem::path(filename)))
+       auto found = find_case_insensitive(env::template_folder() + L"/" + params.at(0) + L".scene");
+       if (!found)
                return core::frame_producer::empty();
 
+       auto filename           = *found;
+       auto template_name      = get_relative(filename, env::template_folder()).wstring();
+
+       CASPAR_SCOPED_CONTEXT_MSG(template_name + L": ");
+
        boost::property_tree::wptree root;
-       std::wifstream file(filename);
+       boost::filesystem::wifstream file(filename);
        boost::property_tree::read_xml(
                        file,
                        root,
                        boost::property_tree::xml_parser::trim_whitespace
                                        | boost::property_tree::xml_parser::no_comments);
 
-       int width = root.get<int>(L"scene.<xmlattr>.width");
-       int height = root.get<int>(L"scene.<xmlattr>.height");
+       int width = ptree_get<int>(root, L"scene.<xmlattr>.width");
+       int height = ptree_get<int>(root, L"scene.<xmlattr>.height");
 
-       auto scene = spl::make_shared<scene_producer>(width, height);
+       auto scene = spl::make_shared<scene_producer>(L"scene", template_name, width, height, dependencies.format_desc);
 
-       BOOST_FOREACH(auto elem, root.get_child(L"scene.variables"))
+       for (auto elem : root | witerate_children(L"scene.variables") | welement_context_iteration)
        {
-               auto type = elem.second.get<std::wstring>(L"<xmlattr>.type");
+               ptree_verify_element_name(elem, L"variable");
 
-               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 type               = ptree_get<std::wstring>(elem.second, L"<xmlattr>.type");
+               auto id                 = ptree_get<std::wstring>(elem.second, L"<xmlattr>.id");
+               auto is_public  = elem.second.get(L"<xmlattr>.public", false);
+               auto expr               = ptree_get_value<std::wstring>(elem.second);
+
+               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 | witerate_children(L"scene.layers") | welement_context_iteration)
        {
-               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"));
-               auto& layer = scene->create_layer(producer, 0, 0, id);
-               auto variable_prefix = L"layer." + id + L".";
+               ptree_verify_element_name(elem, L"layer");
+
+               auto id                                                 = ptree_get<std::wstring>(elem.second, L"<xmlattr>.id");
+               auto producer_string                    = ptree_get<std::wstring>(elem.second, L"producer");
+               auto producer                                   = [&]
+               {
+                       CASPAR_SCOPED_CONTEXT_MSG(" -> ");
+                       auto adjusted_dependencies = dependencies;
+                       auto& adjusted_format_desc = adjusted_dependencies.format_desc;
+
+                       adjusted_format_desc.field_mode          = field_mode::progressive;
+                       adjusted_format_desc.fps                        *= adjusted_format_desc.field_count;
+                       adjusted_format_desc.duration           /= adjusted_format_desc.field_count;
+                       adjusted_format_desc.field_count         = 1;
+
+                       return dependencies.producer_registry->create_producer(adjusted_dependencies, producer_string);
+               }();
+               auto& layer                                             = scene->create_layer(producer, 0, 0, id);
+               auto variable_prefix                    = L"layer." + id + L".";
+
+               auto overridden_width = elem.second.get<std::wstring>(L"width", L"");
+               if (!overridden_width.empty())
+                       layer.producer.get()->pixel_constraints().width = scene->create_variable<double>(variable_prefix + L"width", false, overridden_width);
+               else
+                       scene->create_variable<double>(variable_prefix + L"width", false) = layer.producer.get()->pixel_constraints().width;
 
-               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"));
+               auto overridden_height = elem.second.get<std::wstring>(L"height", L"");
+               if (!overridden_height.empty())
+                       layer.producer.get()->pixel_constraints().height = scene->create_variable<double>(variable_prefix + L"height", false, overridden_height);
+               else
+                       scene->create_variable<double>(variable_prefix + L"height", false) = layer.producer.get()->pixel_constraints().height;
 
-               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;
+               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, ptree_get<std::wstring>(elem.second, L"x"));
+               layer.position.y                                = scene->create_variable<double>(variable_prefix + L"y", false, ptree_get<std::wstring>(elem.second, 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", L"${" + variable_prefix + L"width}"));
+               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", L"${" + variable_prefix + L"height}"));
+               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", L"${" + variable_prefix + L"width}"));
+               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", L"${" + variable_prefix + L"width}"));
+               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", L"${" + variable_prefix + L"height}"));
+               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", L"${" + variable_prefix + L"height}"));
+
+               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.enable                 = scene->create_variable<bool>(variable_prefix + L"chroma_key.enable", false, elem.second.get(L"chroma_key.enable", L"false"));
+               layer.chroma_key.target_hue             = scene->create_variable<double>(variable_prefix + L"chroma_key.target_hue", false, elem.second.get(L"chroma_key.target_hue", L"120.0"));
+               layer.chroma_key.hue_width              = scene->create_variable<double>(variable_prefix + L"chroma_key.hue_width", false, elem.second.get(L"chroma_key.hue_width", L"0.1"));
+               layer.chroma_key.min_saturation = scene->create_variable<double>(variable_prefix + L"chroma_key.min_saturation", false, elem.second.get(L"chroma_key.min_saturation", L"0.0"));
+               layer.chroma_key.min_brightness = scene->create_variable<double>(variable_prefix + L"chroma_key.min_brightness", false, elem.second.get(L"chroma_key.min_brightness", 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"1.0"));
+               layer.chroma_key.spill_darken   = scene->create_variable<double>(variable_prefix + L"chroma_key.spill_darken", false, elem.second.get(L"chroma_key.spill_darken", L"2.0"));
+
+               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>();
+               }
        }
 
-       BOOST_FOREACH(auto& elem, root.get_child(L"scene.timelines"))
+       if (root.get_child_optional(L"scene.marks"))
        {
-               auto& variable = scene->get_variable(elem.second.get<std::wstring>(L"<xmlattr>.variable"));
+               for (auto& mark : root | witerate_children(L"scene.marks") | welement_context_iteration)
+               {
+                       ptree_verify_element_name(mark, L"mark");
+
+                       auto at         = ptree_get<int64_t>(mark.second, L"<xmlattr>.at");
+                       auto type       = get_mark_action(ptree_get<std::wstring>(mark.second, L"<xmlattr>.type"));
+                       auto label      = mark.second.get(L"<xmlattr>.label", L"");
 
-               BOOST_FOREACH(auto& k, elem.second)
+                       scene->add_mark(at, type, label);
+               }
+       }
+
+       if (root.get_child_optional(L"scene.timelines"))
+       {
+               for (auto& elem : root | witerate_children(L"scene.timelines") | welement_context_iteration)
                {
-                       if (k.first == L"<xmlattr>")
-                               continue;
+                       ptree_verify_element_name(elem, L"timeline");
+
+                       auto variable_name      = ptree_get<std::wstring>(elem.second, L"<xmlattr>.variable");
+                       auto& variable          = scene->get_variable(variable_name);
+
+                       for (auto& k : elem.second)
+                       {
+                               if (k.first == L"<xmlattr>")
+                                       continue;
+
+                               ptree_verify_element_name(k, L"keyframe");
+
+                               auto easing     = k.second.get(L"<xmlattr>.easing", L"");
+                               auto at         = ptree_get<int64_t>(k.second, L"<xmlattr>.at");
 
-                       auto easing = k.second.get(L"<xmlattr>.easing", L"");
-                       auto at = k.second.get<int64_t>(L"<xmlattr>.at");
+                               auto keyframe_variable_name = L"timeline." + variable_name + L"." + boost::lexical_cast<std::wstring>(at);
 
-                       if (variable.is<double>())
-                               scene->add_keyframe(variable.as<double>(), k.second.get_value<double>(), at, easing);
-                       else if (variable.is<int>())
-                               scene->add_keyframe(variable.as<int>(), k.second.get_value<int>(), at, easing);
+                               if (variable.is<double>())
+                                       scene->add_keyframe(variable.as<double>(), scene->create_variable<double>(keyframe_variable_name, false, ptree_get_value<std::wstring>(k.second)), at, easing);
+                               else if (variable.is<int>())
+                                       scene->add_keyframe(variable.as<int>(), scene->create_variable<int>(keyframe_variable_name, false, ptree_get_value<std::wstring>(k.second)), at, easing);
+                       }
                }
        }
 
-       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);
+       };
+
+       int task_id = 0;
+       if (root.get_child_optional(L"scene.tasks"))
+       {
+               for (auto& task : root | witerate_children(L"scene.tasks") | welement_context_iteration)
+               {
+                       auto& element_name = task.first;
+
+                       std::vector<std::wstring> call_params;
+
+                       if (element_name == L"call")
+                       {
+                               for (auto& arg : task.second)
+                               {
+                                       if (arg.first == L"<xmlattr>")
+                                               continue;
+
+                                       ptree_verify_element_name(arg, L"arg");
+                                       call_params.push_back(ptree_get_value<std::wstring>(arg.second));
+                               }
+                       }
+                       else if (element_name == L"cg_play")
+                               call_params.push_back(L"play()");
+                       else if (element_name == L"cg_stop")
+                               call_params.push_back(L"stop()");
+                       else if (element_name == L"cg_next")
+                               call_params.push_back(L"next()");
+                       else if (element_name == L"cg_invoke")
+                       {
+                               call_params.push_back(L"invoke()");
+                               call_params.push_back(ptree_get<std::wstring>(task.second, L"<xmlattr>.label"));
+                       }
+                       else
+                               CASPAR_THROW_EXCEPTION(user_error() << msg_info(L"Only valid element names in <tasks /> are call, cg_play, cg_stop, cg_next and cg_invoke"));
+
+                       auto at_frame   = task.second.get_optional<int64_t>(L"<xmlattr>.at");
+                       auto when               = task.second.get_optional<std::wstring>(L"<xmlattr>.when");
+                       auto to_layer   = ptree_get<std::wstring>(task.second, L"<xmlattr>.to_layer");
+                       auto producer   = scene->get_layer(to_layer).producer.get();
 
-               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>();
+                       binding<bool> condition;
+
+                       if (at_frame)
+                               condition = scene->timeline_frame() == *at_frame;
+                       else if (when)
+                               condition = scene->create_variable<bool>(L"tasks.task_" + boost::lexical_cast<std::wstring>(task_id), false, *when);
+                       else
+                               CASPAR_THROW_EXCEPTION(user_error() << msg_info(L"Task elements must have either an at attribute or a when attribute"));
+
+                       auto weak_producer = static_cast<std::weak_ptr<frame_producer>>(producer);
+
+                       scene->add_task(std::move(condition), [=]
+                       {
+                               auto strong = weak_producer.lock();
+
+                               if (strong)
+                                       strong->call(call_params);
+                       });
+
+                       ++task_id;
+               }
+       }
+
+       for (auto& var_name : scene->get_variables())
+       {
+               CASPAR_SCOPED_CONTEXT_MSG(L"/scene/variables/variable[@id=" + var_name + L"]/text()");
+               deduce_expression(scene->get_variable(var_name), repo);
        }
 
+       auto params2 = params;
+       params2.erase(params2.begin());
+
+       scene->call(params2);
+
        return scene;
 }
 
+void init(module_dependencies dependencies)
+{
+       dependencies.producer_registry->register_producer_factory(L"XML Scene Producer", create_xml_scene_producer, describe_xml_scene_producer);
+       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 core::frame_producer_dependencies& dependencies,
+                       const std::wstring& filename)
+                       {
+                               return create_xml_scene_producer(dependencies, { filename });
+                       },
+                       false);
+}
+
 }}}