]> git.sesse.net Git - casparcg/blob - core/producer/scene/xml_scene_producer.cpp
Added basic support for image sequences.
[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/property_tree/ptree.hpp>
29 #include <boost/property_tree/xml_parser.hpp>
30
31 #include <common/env.h>
32 #include <core/producer/frame_producer.h>
33
34 #include "scene_producer.h"
35
36 namespace caspar { namespace core { namespace scene {
37
38 void deduce_expression(variable& var, const variable_repository& repo)
39 {
40         auto expr_str = var.original_expr();
41         auto trimmed = boost::trim_copy(expr_str);
42
43         if (boost::starts_with(trimmed, L"${") && boost::ends_with(trimmed, L"}"))
44         {
45                 expr_str = trimmed.substr(2, expr_str.length() - 3);
46
47                 if (var.is<double>())
48                         var.as<double>().bind(parse_expression<double>(expr_str, repo));
49                 else if (var.is<bool>())
50                         var.as<bool>().bind(parse_expression<bool>(expr_str, repo));
51                 else if (var.is<std::wstring>())
52                         var.as<std::wstring>().bind(parse_expression<std::wstring>(expr_str, repo));
53         }
54         else if (!expr_str.empty())
55         {
56                 var.from_string(expr_str);
57         }
58 }
59
60 spl::shared_ptr<core::frame_producer> create_xml_scene_producer(
61                 const spl::shared_ptr<core::frame_factory>& frame_factory,
62                 const core::video_format_desc& format_desc,
63                 const std::vector<std::wstring>& params)
64 {
65         if (params.empty())
66                 return core::frame_producer::empty();
67
68         std::wstring filename = env::media_folder() + L"\\" + params[0] + L".xml";
69         
70         if (!boost::filesystem::is_regular_file(boost::filesystem::path(filename)))
71                 return core::frame_producer::empty();
72
73         boost::property_tree::wptree root;
74         std::wifstream file(filename);
75         boost::property_tree::read_xml(
76                         file,
77                         root,
78                         boost::property_tree::xml_parser::trim_whitespace
79                                         | boost::property_tree::xml_parser::no_comments);
80
81         int width = root.get<int>(L"scene.<xmlattr>.width");
82         int height = root.get<int>(L"scene.<xmlattr>.height");
83
84         auto scene = spl::make_shared<scene_producer>(width, height);
85
86         BOOST_FOREACH(auto elem, root.get_child(L"scene.variables"))
87         {
88                 auto type = elem.second.get<std::wstring>(L"<xmlattr>.type");
89                 auto id = elem.second.get<std::wstring>(L"<xmlattr>.id");
90                 auto is_public = elem.second.get(L"<xmlattr>.public", false);
91                 auto expr = elem.second.get_value<std::wstring>();
92
93                 if (!is_public)
94                         id = L"variable." + id;
95
96                 if (type == L"number")
97                         scene->create_variable<double>(id, is_public, expr);
98                 else if (type == L"string")
99                         scene->create_variable<std::wstring>(id, is_public, expr);
100                 else if (type == L"bool")
101                         scene->create_variable<bool>(id, is_public, expr);
102         }
103
104         BOOST_FOREACH(auto elem, root.get_child(L"scene.layers"))
105         {
106                 auto id = elem.second.get<std::wstring>(L"<xmlattr>.id");
107                 auto producer = create_producer(frame_factory, format_desc, elem.second.get<std::wstring>(L"producer"));
108                 auto& layer = scene->create_layer(producer, 0, 0, id);
109                 auto variable_prefix = L"layer." + id + L".";
110
111                 layer.hidden = scene->create_variable<bool>(variable_prefix + L"hidden", false, elem.second.get(L"hidden", L"false"));
112                 layer.position.x = scene->create_variable<double>(variable_prefix + L"x", false, elem.second.get<std::wstring>(L"x"));
113                 layer.position.y = scene->create_variable<double>(variable_prefix + L"y", false, elem.second.get<std::wstring>(L"y"));
114
115                 layer.adjustments.opacity = scene->create_variable<double>(variable_prefix + L"adjustment.opacity", false, elem.second.get(L"adjustments.opacity", L"1.0"));
116
117                 scene->create_variable<double>(variable_prefix + L"width", false) = layer.producer.get()->pixel_constraints().width;
118                 scene->create_variable<double>(variable_prefix + L"height", false) = layer.producer.get()->pixel_constraints().height;
119
120                 BOOST_FOREACH(auto& var_name, producer->get_variables())
121                 {
122                         auto& var = producer->get_variable(var_name);
123                         auto expr = elem.second.get<std::wstring>(L"parameters." + var_name, L"");
124
125                         if (var.is<double>())
126                                 scene->create_variable<double>(variable_prefix + L"parameter." + var_name, false, expr) = var.as<double>();
127                         else if (var.is<std::wstring>())
128                                 scene->create_variable<std::wstring>(variable_prefix + L"parameter." + var_name, false, expr) = var.as<std::wstring>();
129                         else if (var.is<bool>())
130                                 scene->create_variable<bool>(variable_prefix + L"parameter." + var_name, false, expr) = var.as<bool>();
131                 }
132         }
133
134         BOOST_FOREACH(auto& elem, root.get_child(L"scene.timelines"))
135         {
136                 auto& variable = scene->get_variable(elem.second.get<std::wstring>(L"<xmlattr>.variable"));
137
138                 BOOST_FOREACH(auto& k, elem.second)
139                 {
140                         if (k.first == L"<xmlattr>")
141                                 continue;
142
143                         auto easing = k.second.get(L"<xmlattr>.easing", L"");
144                         auto at = k.second.get<int64_t>(L"<xmlattr>.at");
145
146                         if (variable.is<double>())
147                                 scene->add_keyframe(variable.as<double>(), k.second.get_value<double>(), at, easing);
148                         else if (variable.is<int>())
149                                 scene->add_keyframe(variable.as<int>(), k.second.get_value<int>(), at, easing);
150                 }
151         }
152
153         auto repo = [&scene](const std::wstring& name) -> variable&
154         {
155                 return scene->get_variable(name); 
156         };
157
158         BOOST_FOREACH(auto& var_name, scene->get_variables())
159         {
160                 deduce_expression(scene->get_variable(var_name), repo);
161         }
162
163         auto params2 = params;
164         params2.erase(params2.begin());
165
166         scene->call(params2);
167
168         return scene;
169 }
170
171 }}}