]> git.sesse.net Git - casparcg/blob - core/producer/scene/xml_scene_producer.cpp
a13a2b624a0f66675e08319b29b7b1da591f7818
[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 #include <vector>
42
43 #include "scene_producer.h"
44 #include "scene_cg_proxy.h"
45
46 namespace caspar { namespace core { namespace scene {
47
48 void deduce_expression(variable& var, const variable_repository& repo)
49 {
50         auto expr_str = var.original_expr();
51         auto trimmed = boost::trim_copy(expr_str);
52
53         if (boost::starts_with(trimmed, L"${") && boost::ends_with(trimmed, L"}"))
54         {
55                 expr_str = trimmed.substr(2, expr_str.length() - 3);
56
57                 if (var.is<double>())
58                         var.as<double>().bind(parse_expression<double>(expr_str, repo));
59                 else if (var.is<bool>())
60                         var.as<bool>().bind(parse_expression<bool>(expr_str, repo));
61                 else if (var.is<std::wstring>())
62                         var.as<std::wstring>().bind(parse_expression<std::wstring>(expr_str, repo));
63         }
64         else if (!expr_str.empty())
65         {
66                 var.from_string(expr_str);
67         }
68 }
69
70 void describe_xml_scene_producer(core::help_sink& sink, const core::help_repository& repo)
71 {
72         sink.short_description(L"A simple producer for dynamic graphics using .scene files.");
73         sink.syntax(L"[.scene_filename:string] {[param1:string] [value1:string]} {[param2:string] [value2:string]} ...");
74         sink.para()->text(L"A simple producer that looks in the ")->code(L"templates")->text(L" folder for .scene files.");
75         sink.para()->text(L"The .scene file is a simple XML document containing variables, layers and timelines.");
76         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.");
77         sink.para()->text(L"The scene producer also supports setting the variables while playing via the CALL command:");
78         sink.example(L">> CALL 1-10 FIRSTNAME \"Jane\"", L"changes the variable FIRSTNAME on an already loaded scene.");
79 }
80
81 spl::shared_ptr<core::frame_producer> create_xml_scene_producer(
82                 const core::frame_producer_dependencies& dependencies,
83                 const std::vector<std::wstring>& params)
84 {
85         if (params.empty())
86                 return core::frame_producer::empty();
87
88         auto found = find_case_insensitive(env::template_folder() + L"/" + params.at(0) + L".scene");
89         if (!found)
90                 return core::frame_producer::empty();
91
92         auto filename           = *found;
93         auto template_name      = get_relative(filename, env::template_folder()).wstring();
94
95         CASPAR_SCOPED_CONTEXT_MSG(template_name + L": ");
96
97         boost::property_tree::wptree root;
98         boost::filesystem::wifstream file(filename);
99         boost::property_tree::read_xml(
100                         file,
101                         root,
102                         boost::property_tree::xml_parser::trim_whitespace
103                                         | boost::property_tree::xml_parser::no_comments);
104
105         int width = ptree_get<int>(root, L"scene.<xmlattr>.width");
106         int height = ptree_get<int>(root, L"scene.<xmlattr>.height");
107
108         auto scene = spl::make_shared<scene_producer>(L"scene", template_name, width, height, dependencies.format_desc);
109
110         for (auto elem : root | witerate_children(L"scene.variables") | welement_context_iteration)
111         {
112                 ptree_verify_element_name(elem, L"variable");
113
114                 auto type               = ptree_get<std::wstring>(elem.second, L"<xmlattr>.type");
115                 auto id                 = ptree_get<std::wstring>(elem.second, L"<xmlattr>.id");
116                 auto is_public  = elem.second.get(L"<xmlattr>.public", false);
117                 auto expr               = ptree_get_value<std::wstring>(elem.second);
118
119                 if (!is_public)
120                         id = L"variable." + id;
121
122                 if (type == L"number")
123                         scene->create_variable<double>(id, is_public, expr);
124                 else if (type == L"string")
125                         scene->create_variable<std::wstring>(id, is_public, expr);
126                 else if (type == L"bool")
127                         scene->create_variable<bool>(id, is_public, expr);
128         }
129
130         for (auto& elem : root | witerate_children(L"scene.layers") | welement_context_iteration)
131         {
132                 ptree_verify_element_name(elem, L"layer");
133
134                 auto id                                                 = ptree_get<std::wstring>(elem.second, L"<xmlattr>.id");
135                 auto producer_string                    = ptree_get<std::wstring>(elem.second, L"producer");
136                 auto producer                                   = [&]
137                 {
138                         CASPAR_SCOPED_CONTEXT_MSG(" -> ");
139                         auto adjusted_dependencies = dependencies;
140                         auto& adjusted_format_desc = adjusted_dependencies.format_desc;
141
142                         adjusted_format_desc.field_mode          = field_mode::progressive;
143                         adjusted_format_desc.fps                        *= adjusted_format_desc.field_count;
144                         adjusted_format_desc.duration           /= adjusted_format_desc.field_count;
145                         adjusted_format_desc.field_count         = 1;
146
147                         return dependencies.producer_registry->create_producer(adjusted_dependencies, producer_string);
148                 }();
149                 auto& layer                                             = scene->create_layer(producer, 0, 0, id);
150                 auto variable_prefix                    = L"layer." + id + L".";
151
152                 auto overridden_width = elem.second.get<std::wstring>(L"width", L"");
153                 if (!overridden_width.empty())
154                         layer.producer.get()->pixel_constraints().width = scene->create_variable<double>(variable_prefix + L"width", false, overridden_width);
155                 else
156                         scene->create_variable<double>(variable_prefix + L"width", false) = layer.producer.get()->pixel_constraints().width;
157
158                 auto overridden_height = elem.second.get<std::wstring>(L"height", L"");
159                 if (!overridden_height.empty())
160                         layer.producer.get()->pixel_constraints().height = scene->create_variable<double>(variable_prefix + L"height", false, overridden_height);
161                 else
162                         scene->create_variable<double>(variable_prefix + L"height", false) = layer.producer.get()->pixel_constraints().height;
163
164                 layer.hidden                                                            = scene->create_variable<bool>(variable_prefix + L"hidden", false, elem.second.get(L"hidden", L"false"));
165                 layer.position.x                                                        = scene->create_variable<double>(variable_prefix + L"x", false, ptree_get<std::wstring>(elem.second, L"x"));
166                 layer.position.y                                                        = scene->create_variable<double>(variable_prefix + L"y", false, ptree_get<std::wstring>(elem.second, L"y"));
167                 layer.anchor.x                                                          = scene->create_variable<double>(variable_prefix + L"anchor_x", false, elem.second.get<std::wstring>(L"anchor_x", L"0.0"));
168                 layer.anchor.y                                                          = scene->create_variable<double>(variable_prefix + L"anchor_y", false, elem.second.get<std::wstring>(L"anchor_y", L"0.0"));
169                 layer.clip.upper_left.x                                         = scene->create_variable<double>(variable_prefix + L"clip.upper_left_x", false, elem.second.get<std::wstring>(L"clip.upper_left_x", L"0.0"));
170                 layer.clip.upper_left.y                                         = scene->create_variable<double>(variable_prefix + L"clip.upper_left_y", false, elem.second.get<std::wstring>(L"clip.upper_left_y", L"0.0"));
171                 layer.clip.lower_right.x                                        = scene->create_variable<double>(variable_prefix + L"clip.lower_right_x", false, elem.second.get<std::wstring>(L"clip.lower_right_x", L"${scene_width}"));
172                 layer.clip.lower_right.y                                        = scene->create_variable<double>(variable_prefix + L"clip.lower_right_y", false, elem.second.get<std::wstring>(L"clip.lower_right_y", L"${scene_height}"));
173                 layer.rotation                                                          = scene->create_variable<double>(variable_prefix + L"rotation", false, elem.second.get<std::wstring>(L"rotation", L"0.0"));
174                 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"));
175                 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"));
176                 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}"));
177                 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}"));
178                 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"));
179                 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"));
180                 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}"));
181                 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"));
182                 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}"));
183                 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}"));
184                 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"));
185                 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}"));
186
187                 layer.adjustments.opacity                                       = scene->create_variable<double>(variable_prefix + L"adjustment.opacity", false, elem.second.get(L"adjustments.opacity", L"1.0"));
188                 layer.adjustments.contrast                                      = scene->create_variable<double>(variable_prefix + L"adjustment.contrast", false, elem.second.get(L"adjustments.contrast", L"1.0"));
189                 layer.adjustments.saturation                            = scene->create_variable<double>(variable_prefix + L"adjustment.saturation", false, elem.second.get(L"adjustments.saturation", L"1.0"));
190                 layer.adjustments.brightness                            = scene->create_variable<double>(variable_prefix + L"adjustment.brightness", false, elem.second.get(L"adjustments.brightness", L"1.0"));
191                 layer.levels.min_input                                          = scene->create_variable<double>(variable_prefix + L"level.min_input", false, elem.second.get(L"levels.min_input", L"0.0"));
192                 layer.levels.max_input                                          = scene->create_variable<double>(variable_prefix + L"level.max_input", false, elem.second.get(L"levels.max_input", L"1.0"));
193                 layer.levels.gamma                                                      = scene->create_variable<double>(variable_prefix + L"level.gamma", false, elem.second.get(L"levels.gamma", L"1.0"));
194                 layer.levels.min_output                                         = scene->create_variable<double>(variable_prefix + L"level.min_output", false, elem.second.get(L"levels.min_output", L"0.0"));
195                 layer.levels.max_output                                         = scene->create_variable<double>(variable_prefix + L"level.max_output", false, elem.second.get(L"levels.max_output", L"1.0"));
196                 layer.volume                                                            = scene->create_variable<double>(variable_prefix + L"volume", false, elem.second.get(L"volume", L"1.0"));
197                 layer.is_key                                                            = scene->create_variable<bool>(variable_prefix + L"is_key", false, elem.second.get(L"is_key", L"false"));
198                 layer.use_mipmap                                                        = scene->create_variable<bool>(variable_prefix + L"use_mipmap", false, elem.second.get(L"use_mipmap", L"false"));
199                 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); });
200                 layer.chroma_key.enable                                         = scene->create_variable<bool>(variable_prefix + L"chroma_key.enable", false, elem.second.get(L"chroma_key.enable", L"false"));
201                 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"));
202                 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"));
203                 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"));
204                 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"));
205                 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"));
206                 layer.chroma_key.spill_suppress                         = scene->create_variable<double>(variable_prefix + L"chroma_key.spill_suppress", false, elem.second.get(L"chroma_key.spill_suppress", L"0.0"));
207                 layer.chroma_key.spill_suppress_saturation      = scene->create_variable<double>(variable_prefix + L"chroma_key.spill_suppress_saturation", false, elem.second.get(L"chroma_key.spill_suppress_saturation", L"1.0"));
208
209                 for (auto& var_name : producer->get_variables())
210                 {
211                         auto& var = producer->get_variable(var_name);
212                         auto expr = elem.second.get<std::wstring>(L"parameters." + var_name, L"");
213
214                         if (var.is<double>())
215                                 scene->create_variable<double>(variable_prefix + L"parameter." + var_name, false, expr) = var.as<double>();
216                         else if (var.is<std::wstring>())
217                                 scene->create_variable<std::wstring>(variable_prefix + L"parameter." + var_name, false, expr) = var.as<std::wstring>();
218                         else if (var.is<bool>())
219                                 scene->create_variable<bool>(variable_prefix + L"parameter." + var_name, false, expr) = var.as<bool>();
220                 }
221         }
222
223         if (root.get_child_optional(L"scene.marks"))
224         {
225                 for (auto& mark : root | witerate_children(L"scene.marks") | welement_context_iteration)
226                 {
227                         ptree_verify_element_name(mark, L"mark");
228
229                         auto at         = ptree_get<int64_t>(mark.second, L"<xmlattr>.at");
230                         auto type       = get_mark_action(ptree_get<std::wstring>(mark.second, L"<xmlattr>.type"));
231                         auto label      = mark.second.get(L"<xmlattr>.label", L"");
232
233                         scene->add_mark(at, type, label);
234                 }
235         }
236
237         if (root.get_child_optional(L"scene.timelines"))
238         {
239                 for (auto& elem : root | witerate_children(L"scene.timelines") | welement_context_iteration)
240                 {
241                         ptree_verify_element_name(elem, L"timeline");
242
243                         auto variable_name      = ptree_get<std::wstring>(elem.second, L"<xmlattr>.variable");
244                         auto& variable          = scene->get_variable(variable_name);
245
246                         for (auto& k : elem.second)
247                         {
248                                 if (k.first == L"<xmlattr>")
249                                         continue;
250
251                                 ptree_verify_element_name(k, L"keyframe");
252
253                                 auto easing     = k.second.get(L"<xmlattr>.easing", L"");
254                                 auto at         = ptree_get<int64_t>(k.second, L"<xmlattr>.at");
255
256                                 auto keyframe_variable_name = L"timeline." + variable_name + L"." + boost::lexical_cast<std::wstring>(at);
257
258                                 if (variable.is<double>())
259                                         scene->add_keyframe(variable.as<double>(), scene->create_variable<double>(keyframe_variable_name, false, ptree_get_value<std::wstring>(k.second)), at, easing);
260                                 else if (variable.is<int>())
261                                         scene->add_keyframe(variable.as<int>(), scene->create_variable<int>(keyframe_variable_name, false, ptree_get_value<std::wstring>(k.second)), at, easing);
262                         }
263                 }
264         }
265
266         auto repo = [&scene](const std::wstring& name) -> variable&
267         {
268                 return scene->get_variable(name);
269         };
270
271         int task_id = 0;
272         if (root.get_child_optional(L"scene.tasks"))
273         {
274                 for (auto& task : root | witerate_children(L"scene.tasks") | welement_context_iteration)
275                 {
276                         auto at_frame = task.second.get_optional<int64_t>(L"<xmlattr>.at");
277                         auto when = task.second.get_optional<std::wstring>(L"<xmlattr>.when");
278
279                         binding<bool> condition;
280
281                         if (at_frame)
282                                 condition = scene->timeline_frame() == *at_frame;
283                         else if (when)
284                                 condition = scene->create_variable<bool>(L"tasks.task_" + boost::lexical_cast<std::wstring>(task_id), false, *when);
285                         else
286                                 CASPAR_THROW_EXCEPTION(user_error() << msg_info(L"Task elements must have either an at attribute or a when attribute"));
287
288                         auto& element_name = task.first;
289
290                         std::vector<std::wstring> call_params;
291
292                         if (element_name == L"call")
293                         {
294                                 for (auto& arg : task.second)
295                                 {
296                                         if (arg.first == L"<xmlattr>")
297                                                 continue;
298
299                                         ptree_verify_element_name(arg, L"arg");
300                                         call_params.push_back(ptree_get_value<std::wstring>(arg.second));
301                                 }
302                         }
303                         else if (element_name == L"cg_play")
304                                 call_params.push_back(L"play()");
305                         else if (element_name == L"cg_stop")
306                                 call_params.push_back(L"stop()");
307                         else if (element_name == L"cg_next")
308                                 call_params.push_back(L"next()");
309                         else if (element_name == L"cg_invoke")
310                         {
311                                 call_params.push_back(L"invoke()");
312                                 call_params.push_back(ptree_get<std::wstring>(task.second, L"<xmlattr>.label"));
313                         }
314                         else if (element_name == L"goto_mark")
315                         {
316                                 auto mark_label = ptree_get<std::wstring>(task.second, L"<xmlattr>.label");
317                                 auto weak_scene = static_cast<std::weak_ptr<scene_producer>>(scene);
318
319                                 scene->add_task(std::move(condition), [=]
320                                 {
321                                         auto strong = weak_scene.lock();
322
323                                         if (strong)
324                                                 strong->call({ L"play()", mark_label });
325                                 });
326                         }
327                         else
328                                 CASPAR_THROW_EXCEPTION(user_error() << msg_info(L"Only valid element names in <tasks /> are call, cg_play, cg_stop, cg_next, cg_invoke and goto_mark"));
329
330                         if (!call_params.empty())
331                         {
332                                 auto to_layer = ptree_get<std::wstring>(task.second, L"<xmlattr>.to_layer");
333                                 auto producer = scene->get_layer(to_layer).producer.get();
334
335                                 auto weak_producer = static_cast<std::weak_ptr<frame_producer>>(producer);
336
337                                 scene->add_task(std::move(condition), [=]
338                                 {
339                                         auto strong = weak_producer.lock();
340
341                                         if (strong)
342                                                 strong->call(call_params);
343                                 });
344                         }
345
346                         ++task_id;
347                 }
348         }
349
350         for (auto& var_name : scene->get_variables())
351         {
352                 CASPAR_SCOPED_CONTEXT_MSG(L"/scene/variables/variable[@id=" + var_name + L"]/text()");
353                 deduce_expression(scene->get_variable(var_name), repo);
354         }
355
356         auto params2 = params;
357         params2.erase(params2.begin());
358
359         scene->call(params2);
360
361         return scene;
362 }
363
364 void init(module_dependencies dependencies)
365 {
366         dependencies.producer_registry->register_producer_factory(L"XML Scene Producer", create_xml_scene_producer, describe_xml_scene_producer);
367         dependencies.cg_registry->register_cg_producer(
368                         L"scene",
369                         { L".scene" },
370                         [](const std::wstring& filename) { return ""; },
371                         [](const spl::shared_ptr<core::frame_producer>& producer)
372                         {
373                                 return spl::make_shared<core::scene::scene_cg_proxy>(producer);
374                         },
375                         [](
376                         const core::frame_producer_dependencies& dependencies,
377                         const std::wstring& filename)
378                         {
379                                 return create_xml_scene_producer(dependencies, { filename });
380                         },
381                         false);
382 }
383
384 }}}