]> git.sesse.net Git - casparcg/blob - core/producer/scene/xml_scene_producer.cpp
* Fixed bug where lexical_cast from double to string in Linux where swprintf was...
[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 <core/producer/frame_producer.h>
35 #include <core/help/help_repository.h>
36 #include <core/help/help_sink.h>
37
38 #include <future>
39
40 #include "scene_producer.h"
41 #include "scene_cg_proxy.h"
42
43 namespace caspar { namespace core { namespace scene {
44
45 void deduce_expression(variable& var, const variable_repository& repo)
46 {
47         auto expr_str = var.original_expr();
48         auto trimmed = boost::trim_copy(expr_str);
49
50         if (boost::starts_with(trimmed, L"${") && boost::ends_with(trimmed, L"}"))
51         {
52                 expr_str = trimmed.substr(2, expr_str.length() - 3);
53
54                 if (var.is<double>())
55                         var.as<double>().bind(parse_expression<double>(expr_str, repo));
56                 else if (var.is<bool>())
57                         var.as<bool>().bind(parse_expression<bool>(expr_str, repo));
58                 else if (var.is<std::wstring>())
59                         var.as<std::wstring>().bind(parse_expression<std::wstring>(expr_str, repo));
60         }
61         else if (!expr_str.empty())
62         {
63                 var.from_string(expr_str);
64         }
65 }
66
67 void describe_xml_scene_producer(core::help_sink& sink, const core::help_repository& repo)
68 {
69         sink.short_description(L"A simple producer for dynamic graphics using .scene files.");
70         sink.syntax(L"[.scene_filename:string] {[param1:string] [value1:string]} {[param2:string] [value2:string]} ...");
71         sink.para()->text(L"A simple producer that looks in the ")->code(L"templates")->text(L" folder for .scene files.");
72         sink.para()->text(L"The .scene file is a simple XML document containing variables, layers and timelines.");
73         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.");
74         sink.para()->text(L"The scene producer also supports setting the variables while playing via the CALL command:");
75         sink.example(L">> CALL 1-10 FIRSTNAME \"Jane\"", L"changes the variable FIRSTNAME on an already loaded scene.");
76 }
77
78 spl::shared_ptr<core::frame_producer> create_xml_scene_producer(
79                 const core::frame_producer_dependencies& dependencies,
80                 const std::vector<std::wstring>& params)
81 {
82         if (params.empty())
83                 return core::frame_producer::empty();
84
85         auto found = find_case_insensitive(env::template_folder() + L"/" + params.at(0) + L".scene");
86         if (!found)
87                 return core::frame_producer::empty();
88
89         std::wstring filename = *found;
90
91         boost::property_tree::wptree root;
92         boost::filesystem::wifstream file(filename);
93         boost::property_tree::read_xml(
94                         file,
95                         root,
96                         boost::property_tree::xml_parser::trim_whitespace
97                                         | boost::property_tree::xml_parser::no_comments);
98
99         int width = root.get<int>(L"scene.<xmlattr>.width");
100         int height = root.get<int>(L"scene.<xmlattr>.height");
101
102         auto scene = spl::make_shared<scene_producer>(width, height, dependencies.format_desc);
103
104         for (auto elem : root.get_child(L"scene.variables"))
105         {
106                 auto type = elem.second.get<std::wstring>(L"<xmlattr>.type");
107                 auto id = elem.second.get<std::wstring>(L"<xmlattr>.id");
108                 auto is_public = elem.second.get(L"<xmlattr>.public", false);
109                 auto expr = elem.second.get_value<std::wstring>();
110
111                 if (!is_public)
112                         id = L"variable." + id;
113
114                 if (type == L"number")
115                         scene->create_variable<double>(id, is_public, expr);
116                 else if (type == L"string")
117                         scene->create_variable<std::wstring>(id, is_public, expr);
118                 else if (type == L"bool")
119                         scene->create_variable<bool>(id, is_public, expr);
120         }
121
122         for (auto& elem : root.get_child(L"scene.layers"))
123         {
124                 auto id = elem.second.get<std::wstring>(L"<xmlattr>.id");
125                 auto producer = dependencies.producer_registry->create_producer(dependencies, elem.second.get<std::wstring>(L"producer"));
126                 auto& layer = scene->create_layer(producer, 0, 0, id);
127                 auto variable_prefix = L"layer." + id + L".";
128
129                 layer.hidden = scene->create_variable<bool>(variable_prefix + L"hidden", false, elem.second.get(L"hidden", L"false"));
130                 layer.position.x = scene->create_variable<double>(variable_prefix + L"x", false, elem.second.get<std::wstring>(L"x"));
131                 layer.position.y = scene->create_variable<double>(variable_prefix + L"y", false, elem.second.get<std::wstring>(L"y"));
132                 layer.anchor.x = scene->create_variable<double>(variable_prefix + L"anchor_x", false, elem.second.get<std::wstring>(L"anchor_x", L"0.0"));
133                 layer.anchor.y = scene->create_variable<double>(variable_prefix + L"anchor_y", false, elem.second.get<std::wstring>(L"anchor_y", L"0.0"));
134                 layer.rotation = scene->create_variable<double>(variable_prefix + L"rotation", false, elem.second.get<std::wstring>(L"rotation", L"0.0"));
135                 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"));
136                 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"));
137                 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()));
138                 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()));
139                 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"));
140                 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"));
141                 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()));
142                 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"));
143                 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()));
144                 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()));
145                 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"));
146                 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()));
147
148                 layer.adjustments.opacity = scene->create_variable<double>(variable_prefix + L"adjustment.opacity", false, elem.second.get(L"adjustments.opacity", L"1.0"));
149                 layer.is_key = scene->create_variable<bool>(variable_prefix + L"is_key", false, elem.second.get(L"is_key", L"false"));
150                 layer.use_mipmap = scene->create_variable<bool>(variable_prefix + L"use_mipmap", false, elem.second.get(L"use_mipmap", L"false"));
151                 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); });
152                 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); });
153                 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"));
154                 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"));
155                 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"));
156
157                 scene->create_variable<double>(variable_prefix + L"width", false) = layer.producer.get()->pixel_constraints().width;
158                 scene->create_variable<double>(variable_prefix + L"height", false) = layer.producer.get()->pixel_constraints().height;
159
160                 for (auto& var_name : producer->get_variables())
161                 {
162                         auto& var = producer->get_variable(var_name);
163                         auto expr = elem.second.get<std::wstring>(L"parameters." + var_name, L"");
164
165                         if (var.is<double>())
166                                 scene->create_variable<double>(variable_prefix + L"parameter." + var_name, false, expr) = var.as<double>();
167                         else if (var.is<std::wstring>())
168                                 scene->create_variable<std::wstring>(variable_prefix + L"parameter." + var_name, false, expr) = var.as<std::wstring>();
169                         else if (var.is<bool>())
170                                 scene->create_variable<bool>(variable_prefix + L"parameter." + var_name, false, expr) = var.as<bool>();
171                 }
172         }
173
174         if (root.get_child_optional(L"scene.marks"))
175         {
176                 for (auto& mark : root.get_child(L"scene.marks"))
177                 {
178                         auto at = mark.second.get<int64_t>(L"<xmlattr>.at");
179                         auto type = get_mark_action(mark.second.get<std::wstring>(L"<xmlattr>.type"));
180                         auto label = mark.second.get(L"<xmlattr>.label", L"");
181
182                         scene->add_mark(at, type, label);
183                 }
184         }
185
186         for (auto& elem : root.get_child(L"scene.timelines"))
187         {
188                 auto& variable = scene->get_variable(elem.second.get<std::wstring>(L"<xmlattr>.variable"));
189
190                 for (auto& k : elem.second)
191                 {
192                         if (k.first == L"<xmlattr>")
193                                 continue;
194
195                         auto easing = k.second.get(L"<xmlattr>.easing", L"");
196                         auto at = k.second.get<int64_t>(L"<xmlattr>.at");
197
198                         if (variable.is<double>())
199                                 scene->add_keyframe(variable.as<double>(), k.second.get_value<double>(), at, easing);
200                         else if (variable.is<int>())
201                                 scene->add_keyframe(variable.as<int>(), k.second.get_value<int>(), at, easing);
202                 }
203         }
204
205         auto repo = [&scene](const std::wstring& name) -> variable&
206         {
207                 return scene->get_variable(name); 
208         };
209
210         for (auto& var_name : scene->get_variables())
211         {
212                 deduce_expression(scene->get_variable(var_name), repo);
213         }
214
215         auto params2 = params;
216         params2.erase(params2.begin());
217
218         scene->call(params2);
219
220         return scene;
221 }
222
223 void init(module_dependencies dependencies)
224 {
225         dependencies.producer_registry->register_producer_factory(L"XML Scene Producer", create_xml_scene_producer, describe_xml_scene_producer);
226         dependencies.cg_registry->register_cg_producer(
227                         L"scene",
228                         { L".scene" },
229                         [](const std::wstring& filename) { return ""; },
230                         [](const spl::shared_ptr<core::frame_producer>& producer)
231                         {
232                                 return spl::make_shared<core::scene::scene_cg_proxy>(producer);
233                         },
234                         [](
235                         const core::frame_producer_dependencies& dependencies,
236                         const std::wstring& filename)
237                         {
238                                 return create_xml_scene_producer(dependencies, { filename });
239                         },
240                         false);
241 }
242
243 }}}