]> git.sesse.net Git - casparcg/blob - core/producer/scene/xml_scene_producer.cpp
Merge pull request #519 from krzyc/patch-1
[casparcg] / core / producer / scene / xml_scene_producer.cpp
1 /*
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
3 *
4 * This file is part of CasparCG (www.casparcg.com).
5 *
6 * CasparCG is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * CasparCG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * Author: Helge Norberg, helge.norberg@svt.se
20 */
21
22 #include "../../StdAfx.h"
23
24 #include "xml_scene_producer.h"
25 #include "expression_parser.h"
26
27 #include <boost/filesystem.hpp>
28 #include <boost/filesystem/fstream.hpp>
29 #include <boost/property_tree/ptree.hpp>
30 #include <boost/property_tree/xml_parser.hpp>
31
32 #include <common/env.h>
33 #include <common/os/filesystem.h>
34 #include <common/filesystem.h>
35 #include <common/ptree.h>
36 #include <core/producer/frame_producer.h>
37 #include <core/help/help_repository.h>
38 #include <core/help/help_sink.h>
39
40 #include <future>
41
42 #include "scene_producer.h"
43 #include "scene_cg_proxy.h"
44
45 namespace caspar { namespace core { namespace scene {
46
47 void deduce_expression(variable& var, const variable_repository& repo)
48 {
49         auto expr_str = var.original_expr();
50         auto trimmed = boost::trim_copy(expr_str);
51
52         if (boost::starts_with(trimmed, L"${") && boost::ends_with(trimmed, L"}"))
53         {
54                 expr_str = trimmed.substr(2, expr_str.length() - 3);
55
56                 if (var.is<double>())
57                         var.as<double>().bind(parse_expression<double>(expr_str, repo));
58                 else if (var.is<bool>())
59                         var.as<bool>().bind(parse_expression<bool>(expr_str, repo));
60                 else if (var.is<std::wstring>())
61                         var.as<std::wstring>().bind(parse_expression<std::wstring>(expr_str, repo));
62         }
63         else if (!expr_str.empty())
64         {
65                 var.from_string(expr_str);
66         }
67 }
68
69 void describe_xml_scene_producer(core::help_sink& sink, const core::help_repository& repo)
70 {
71         sink.short_description(L"A simple producer for dynamic graphics using .scene files.");
72         sink.syntax(L"[.scene_filename:string] {[param1:string] [value1:string]} {[param2:string] [value2:string]} ...");
73         sink.para()->text(L"A simple producer that looks in the ")->code(L"templates")->text(L" folder for .scene files.");
74         sink.para()->text(L"The .scene file is a simple XML document containing variables, layers and timelines.");
75         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.");
76         sink.para()->text(L"The scene producer also supports setting the variables while playing via the CALL command:");
77         sink.example(L">> CALL 1-10 FIRSTNAME \"Jane\"", L"changes the variable FIRSTNAME on an already loaded scene.");
78 }
79
80 spl::shared_ptr<core::frame_producer> create_xml_scene_producer(
81                 const core::frame_producer_dependencies& dependencies,
82                 const std::vector<std::wstring>& params)
83 {
84         if (params.empty())
85                 return core::frame_producer::empty();
86
87         auto found = find_case_insensitive(env::template_folder() + L"/" + params.at(0) + L".scene");
88         if (!found)
89                 return core::frame_producer::empty();
90
91         std::wstring filename = *found;
92
93         CASPAR_SCOPED_CONTEXT_MSG(get_relative(filename, env::template_folder()).string() + ": ");
94
95         boost::property_tree::wptree root;
96         boost::filesystem::wifstream file(filename);
97         boost::property_tree::read_xml(
98                         file,
99                         root,
100                         boost::property_tree::xml_parser::trim_whitespace
101                                         | boost::property_tree::xml_parser::no_comments);
102
103         int width = ptree_get<int>(root, L"scene.<xmlattr>.width");
104         int height = ptree_get<int>(root, L"scene.<xmlattr>.height");
105
106         auto scene = spl::make_shared<scene_producer>(L"scene", width, height, dependencies.format_desc);
107
108         for (auto elem : root | witerate_children(L"scene.variables") | welement_context_iteration)
109         {
110                 ptree_verify_element_name(elem, L"variable");
111
112                 auto type               = ptree_get<std::wstring>(elem.second, L"<xmlattr>.type");
113                 auto id                 = ptree_get<std::wstring>(elem.second, L"<xmlattr>.id");
114                 auto is_public  = elem.second.get(L"<xmlattr>.public", false);
115                 auto expr               = ptree_get_value<std::wstring>(elem.second);
116
117                 if (!is_public)
118                         id = L"variable." + id;
119
120                 if (type == L"number")
121                         scene->create_variable<double>(id, is_public, expr);
122                 else if (type == L"string")
123                         scene->create_variable<std::wstring>(id, is_public, expr);
124                 else if (type == L"bool")
125                         scene->create_variable<bool>(id, is_public, expr);
126         }
127
128         for (auto& elem : root | witerate_children(L"scene.layers") | welement_context_iteration)
129         {
130                 ptree_verify_element_name(elem, L"layer");
131
132                 auto id                                                 = ptree_get<std::wstring>(elem.second, L"<xmlattr>.id");
133                 auto producer_string                    = ptree_get<std::wstring>(elem.second, L"producer");
134                 auto producer                                   = [&]
135                 {
136                         CASPAR_SCOPED_CONTEXT_MSG(" -> ");
137                         auto adjusted_dependencies = dependencies;
138                         auto& adjusted_format_desc = adjusted_dependencies.format_desc;
139
140                         adjusted_format_desc.field_mode          = field_mode::progressive;
141                         adjusted_format_desc.fps                        *= adjusted_format_desc.field_count;
142                         adjusted_format_desc.duration           /= adjusted_format_desc.field_count;
143                         adjusted_format_desc.field_count         = 1;
144
145                         return dependencies.producer_registry->create_producer(adjusted_dependencies, producer_string);
146                 }();
147                 auto& layer                                             = scene->create_layer(producer, 0, 0, id);
148                 auto variable_prefix                    = L"layer." + id + L".";
149
150                 layer.hidden                                    = scene->create_variable<bool>(variable_prefix + L"hidden", false, elem.second.get(L"hidden", L"false"));
151                 layer.position.x                                = scene->create_variable<double>(variable_prefix + L"x", false, ptree_get<std::wstring>(elem.second, L"x"));
152                 layer.position.y                                = scene->create_variable<double>(variable_prefix + L"y", false, ptree_get<std::wstring>(elem.second, L"y"));
153                 layer.anchor.x                                  = scene->create_variable<double>(variable_prefix + L"anchor_x", false, elem.second.get<std::wstring>(L"anchor_x", L"0.0"));
154                 layer.anchor.y                                  = scene->create_variable<double>(variable_prefix + L"anchor_y", false, elem.second.get<std::wstring>(L"anchor_y", L"0.0"));
155                 layer.rotation                                  = scene->create_variable<double>(variable_prefix + L"rotation", false, elem.second.get<std::wstring>(L"rotation", L"0.0"));
156                 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"));
157                 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"));
158                 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()));
159                 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()));
160                 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"));
161                 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"));
162                 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()));
163                 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"));
164                 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()));
165                 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()));
166                 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"));
167                 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()));
168
169                 layer.adjustments.opacity               = scene->create_variable<double>(variable_prefix + L"adjustment.opacity", false, elem.second.get(L"adjustments.opacity", L"1.0"));
170                 layer.is_key                                    = scene->create_variable<bool>(variable_prefix + L"is_key", false, elem.second.get(L"is_key", L"false"));
171                 layer.use_mipmap                                = scene->create_variable<bool>(variable_prefix + L"use_mipmap", false, elem.second.get(L"use_mipmap", L"false"));
172                 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); });
173                 layer.chroma_key.enable                 = scene->create_variable<bool>(variable_prefix + L"chroma_key.enable", false, elem.second.get(L"chroma_key.enable", L"false"));
174                 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"));
175                 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"));
176                 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"));
177                 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"));
178                 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"));
179                 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"));
180                 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"));
181
182                 scene->create_variable<double>(variable_prefix + L"width", false) = layer.producer.get()->pixel_constraints().width;
183                 scene->create_variable<double>(variable_prefix + L"height", false) = layer.producer.get()->pixel_constraints().height;
184
185                 for (auto& var_name : producer->get_variables())
186                 {
187                         auto& var = producer->get_variable(var_name);
188                         auto expr = elem.second.get<std::wstring>(L"parameters." + var_name, L"");
189
190                         if (var.is<double>())
191                                 scene->create_variable<double>(variable_prefix + L"parameter." + var_name, false, expr) = var.as<double>();
192                         else if (var.is<std::wstring>())
193                                 scene->create_variable<std::wstring>(variable_prefix + L"parameter." + var_name, false, expr) = var.as<std::wstring>();
194                         else if (var.is<bool>())
195                                 scene->create_variable<bool>(variable_prefix + L"parameter." + var_name, false, expr) = var.as<bool>();
196                 }
197         }
198
199         if (root.get_child_optional(L"scene.marks"))
200         {
201                 for (auto& mark : root | witerate_children(L"scene.marks") | welement_context_iteration)
202                 {
203                         ptree_verify_element_name(mark, L"mark");
204
205                         auto at         = ptree_get<int64_t>(mark.second, L"<xmlattr>.at");
206                         auto type       = get_mark_action(ptree_get<std::wstring>(mark.second, L"<xmlattr>.type"));
207                         auto label      = mark.second.get(L"<xmlattr>.label", L"");
208
209                         scene->add_mark(at, type, label);
210                 }
211         }
212
213         if (root.get_child_optional(L"scene.timelines"))
214         {
215                 for (auto& elem : root | witerate_children(L"scene.timelines") | welement_context_iteration)
216                 {
217                         ptree_verify_element_name(elem, L"timeline");
218
219                         auto& variable = scene->get_variable(ptree_get<std::wstring>(elem.second, L"<xmlattr>.variable"));
220
221                         for (auto& k : elem.second)
222                         {
223                                 if (k.first == L"<xmlattr>")
224                                         continue;
225
226                                 ptree_verify_element_name(k, L"keyframe");
227
228                                 auto easing = k.second.get(L"<xmlattr>.easing", L"");
229                                 auto at = ptree_get<int64_t>(k.second, L"<xmlattr>.at");
230
231                                 if (variable.is<double>())
232                                         scene->add_keyframe(variable.as<double>(), ptree_get_value<double>(k.second), at, easing);
233                                 else if (variable.is<int>())
234                                         scene->add_keyframe(variable.as<int>(), ptree_get_value<int>(k.second), at, easing);
235                         }
236                 }
237         }
238
239         auto repo = [&scene](const std::wstring& name) -> variable&
240         {
241                 return scene->get_variable(name);
242         };
243
244         for (auto& var_name : scene->get_variables())
245         {
246                 CASPAR_SCOPED_CONTEXT_MSG(L"/scene/variables/variable[@id=" + var_name + L"]/text()");
247                 deduce_expression(scene->get_variable(var_name), repo);
248         }
249
250         auto params2 = params;
251         params2.erase(params2.begin());
252
253         scene->call(params2);
254
255         return scene;
256 }
257
258 void init(module_dependencies dependencies)
259 {
260         dependencies.producer_registry->register_producer_factory(L"XML Scene Producer", create_xml_scene_producer, describe_xml_scene_producer);
261         dependencies.cg_registry->register_cg_producer(
262                         L"scene",
263                         { L".scene" },
264                         [](const std::wstring& filename) { return ""; },
265                         [](const spl::shared_ptr<core::frame_producer>& producer)
266                         {
267                                 return spl::make_shared<core::scene::scene_cg_proxy>(producer);
268                         },
269                         [](
270                         const core::frame_producer_dependencies& dependencies,
271                         const std::wstring& filename)
272                         {
273                                 return create_xml_scene_producer(dependencies, { filename });
274                         },
275                         false);
276 }
277
278 }}}