]> git.sesse.net Git - casparcg/blob - core/producer/scene/scene_producer.cpp
Merge branch '2.1.0' of https://github.com/CasparCG/Server into 2.1.0
[casparcg] / core / producer / scene / 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 <common/future.h>
25 #include <boost/algorithm/string/split.hpp>
26 #include <boost/algorithm/string.hpp>
27
28 #include "scene_producer.h"
29
30 #include "../../frame/draw_frame.h"
31 #include "../../interaction/interaction_aggregator.h"
32 #include "../text/text_producer.h"
33
34 namespace caspar { namespace core { namespace scene {
35
36 layer::layer(const std::wstring& name, const spl::shared_ptr<frame_producer>& producer)
37         : name(name), producer(producer)
38 {
39         clipping.width.bind(producer.get()->pixel_constraints().width);
40         clipping.height.bind(producer.get()->pixel_constraints().height);
41 }
42
43 adjustments::adjustments()
44         : opacity(1.0)
45 {
46 }
47
48 struct timeline
49 {
50         std::map<int64_t, keyframe> keyframes;
51
52         void on_frame(int64_t frame)
53         {
54                 auto before = --keyframes.upper_bound(frame);
55                 bool found_before = before != keyframes.end() && before->first < frame;
56                 auto after = keyframes.upper_bound(frame);
57                 bool found_after = after != keyframes.end() && after->first > frame;
58                 auto exact_frame = keyframes.find(frame);
59                 bool found_exact_frame = exact_frame != keyframes.end();
60
61                 if (found_exact_frame)
62                 {
63                         exact_frame->second.on_destination_frame();
64
65                         auto next_frame = ++exact_frame;
66
67                         if (next_frame != keyframes.end() && next_frame->second.on_start_animate)
68                                 next_frame->second.on_start_animate();
69                 }
70                 else if (found_after)
71                 {
72                         int64_t start_frame = 0;
73
74                         if (found_before)
75                         {
76                                 start_frame = before->first;
77                         }
78
79                         if (after->second.on_start_animate && frame == 0)
80                                 after->second.on_start_animate();
81                         else if (after->second.on_animate_to)
82                                 after->second.on_animate_to(start_frame, frame);
83                 }
84         }
85 };
86
87 struct scene_producer::impl
88 {
89         constraints pixel_constraints_;
90         std::list<layer> layers_;
91         interaction_aggregator aggregator_;
92         binding<int64_t> frame_number_;
93         binding<double> speed_;
94         double frame_fraction_;
95         std::map<void*, timeline> timelines_;
96         std::map<std::wstring, std::shared_ptr<core::variable>> variables_;
97
98         impl(int width, int height)
99                 : pixel_constraints_(width, height)
100                 , aggregator_([=] (double x, double y) { return collission_detect(x, y); })
101                 , frame_fraction_(0)
102         {
103                 auto speed_variable = std::make_shared<core::variable_impl<double>>(L"", true, 1.0);
104                 store_variable(L"scene_speed", speed_variable);
105                 speed_ = speed_variable->value();
106         }
107
108         layer& create_layer(
109                         const spl::shared_ptr<frame_producer>& producer, int x, int y, const std::wstring& name)
110         {
111                 layer layer(name, producer);
112
113                 layer.position.x.set(x);
114                 layer.position.y.set(y);
115
116                 layers_.push_back(layer);
117
118                 return layers_.back();
119         }
120
121         void store_keyframe(void* timeline_identity, const keyframe& k)
122         {
123                 timelines_[timeline_identity].keyframes.insert(std::make_pair(k.destination_frame, k));
124         }
125
126         void store_variable(
127                         const std::wstring& name, const std::shared_ptr<core::variable>& var)
128         {
129                 variables_.insert(std::make_pair(name, var));
130         }
131
132         core::variable& get_variable(const std::wstring& name)
133         {
134                 auto found = variables_.find(name);
135
136                 if (found == variables_.end())
137                         CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(name + L" not found in scene"));
138
139                 return *found->second;
140         }
141
142         binding<int64_t> frame()
143         {
144                 return frame_number_;
145         }
146
147         frame_transform get_transform(const layer& layer) const
148         {
149                 frame_transform transform;
150
151                 auto& pos = transform.image_transform.fill_translation;
152                 auto& scale = transform.image_transform.fill_scale;
153
154                 pos[0] = static_cast<double>(layer.position.x.get()) / static_cast<double>(pixel_constraints_.width.get());
155                 pos[1] = static_cast<double>(layer.position.y.get()) / static_cast<double>(pixel_constraints_.height.get());
156                 scale[0] = static_cast<double>(layer.producer.get()->pixel_constraints().width.get())
157                                 / static_cast<double>(pixel_constraints_.width.get());
158                 scale[1] = static_cast<double>(layer.producer.get()->pixel_constraints().height.get())
159                                 / static_cast<double>(pixel_constraints_.height.get());
160
161                 transform.image_transform.opacity = layer.adjustments.opacity.get();
162                 transform.image_transform.is_key = layer.is_key.get();
163
164                 return transform;
165         }
166
167         draw_frame render_frame()
168         {
169                 BOOST_FOREACH(auto& timeline, timelines_)
170                         timeline.second.on_frame(frame_number_.get());
171
172                 std::vector<draw_frame> frames;
173
174                 BOOST_FOREACH(auto& layer, layers_)
175                 {
176                         if (layer.hidden.get())
177                                 continue;
178
179                         draw_frame frame(layer.producer.get()->receive());
180                         frame.transform() = get_transform(layer);;
181                         frames.push_back(frame);
182                 }
183
184                 frame_fraction_ += speed_.get();
185
186                 if (std::abs(frame_fraction_) >= 1.0)
187                 {
188                         int64_t delta = static_cast<int64_t>(frame_fraction_);
189                         frame_number_.set(frame_number_.get() + delta);
190                         frame_fraction_ -= delta;
191                 }
192
193                 return draw_frame(frames);
194         }
195
196         void on_interaction(const interaction_event::ptr& event)
197         {
198                 aggregator_.translate_and_send(event);
199         }
200
201         bool collides(double x, double y) const
202         {
203                 return collission_detect(x, y);
204         }
205
206         boost::optional<interaction_target> collission_detect(double x, double y) const
207         {
208                 BOOST_FOREACH(auto& layer, layers_ | boost::adaptors::reversed)
209                 {
210                         if (layer.hidden.get())
211                                 continue;
212
213                         auto transform = get_transform(layer);
214                         auto translated = translate(x, y, transform);
215
216                         if (translated.first >= 0.0
217                                 && translated.first <= 1.0
218                                 && translated.second >= 0.0
219                                 && translated.second <= 1.0
220                                 && layer.producer.get()->collides(translated.first, translated.second))
221                         {
222                                 return std::make_pair(transform, layer.producer.get().get());
223                         }
224                 }
225
226                 return boost::optional<interaction_target>();
227         }
228
229         boost::unique_future<std::wstring> call(const std::vector<std::wstring>& params) 
230         {
231                 for (int i = 0; i + 1 < params.size(); i += 2)
232                 {
233                         auto found = variables_.find(boost::to_lower_copy(params[i]));
234
235                         if (found != variables_.end() && found->second->is_public())
236                                 found->second->from_string(params[i + 1]);
237                 }
238
239                 return wrap_as_future(std::wstring(L""));
240         }
241
242         std::wstring print() const
243         {
244                 return L"scene[]";
245         }
246
247         std::wstring name() const
248         {
249                 return L"scene";
250         }
251         
252         boost::property_tree::wptree info() const
253         {
254                 boost::property_tree::wptree info;
255                 info.add(L"type", L"scene");
256                 return info;
257         }
258
259         void subscribe(const monitor::observable::observer_ptr& o)
260         {
261         }
262
263         void unsubscribe(const monitor::observable::observer_ptr& o)
264         {
265         }
266 };
267
268 scene_producer::scene_producer(int width, int height)
269         : impl_(new impl(width, height))
270 {
271 }
272
273 scene_producer::~scene_producer()
274 {
275 }
276
277 layer& scene_producer::create_layer(
278                 const spl::shared_ptr<frame_producer>& producer, int x, int y, const std::wstring& name)
279 {
280         return impl_->create_layer(producer, x, y, name);
281 }
282
283 layer& scene_producer::create_layer(
284                 const spl::shared_ptr<frame_producer>& producer, const std::wstring& name)
285 {
286         return impl_->create_layer(producer, 0, 0, name);
287 }
288
289 binding<int64_t> scene_producer::frame()
290 {
291         return impl_->frame();
292 }
293
294 draw_frame scene_producer::receive_impl()
295 {
296         return impl_->render_frame();
297 }
298
299 constraints& scene_producer::pixel_constraints() { return impl_->pixel_constraints_; }
300
301 void scene_producer::on_interaction(const interaction_event::ptr& event)
302 {
303         impl_->on_interaction(event);
304 }
305
306 bool scene_producer::collides(double x, double y) const
307 {
308         return impl_->collides(x, y);
309 }
310
311 std::wstring scene_producer::print() const
312 {
313         return impl_->print();
314 }
315
316 std::wstring scene_producer::name() const
317 {
318         return impl_->name();
319 }
320
321 boost::property_tree::wptree scene_producer::info() const
322 {
323         return impl_->info();
324 }
325
326 boost::unique_future<std::wstring> scene_producer::call(const std::vector<std::wstring>& params) 
327 {
328         return impl_->call(params);
329 }
330
331 void scene_producer::subscribe(const monitor::observable::observer_ptr& o)
332 {
333         impl_->subscribe(o);
334 }
335
336 void scene_producer::unsubscribe(const monitor::observable::observer_ptr& o)
337 {
338         impl_->unsubscribe(o);
339 }
340
341 void scene_producer::store_keyframe(void* timeline_identity, const keyframe& k)
342 {
343         impl_->store_keyframe(timeline_identity, k);
344 }
345
346 void scene_producer::store_variable(
347                 const std::wstring& name, const std::shared_ptr<core::variable>& var)
348 {
349         impl_->store_variable(name, var);
350 }
351
352 core::variable& scene_producer::get_variable(const std::wstring& name)
353 {
354         return impl_->get_variable(name);
355 }
356
357 spl::shared_ptr<core::frame_producer> create_dummy_scene_producer(const spl::shared_ptr<core::frame_factory>& frame_factory, const video_format_desc& format_desc, const std::vector<std::wstring>& params)
358 {
359         if (params.size() < 1 || !boost::iequals(params.at(0), L"[SCENE]"))
360                 return core::frame_producer::empty();
361
362         auto scene = spl::make_shared<scene_producer>(format_desc.width, format_desc.height);
363
364         text::text_info text_info;
365         text_info.font = L"Arial";
366         text_info.size = 62;
367         text_info.color.r = 1;
368         text_info.color.g = 1;
369         text_info.color.b = 1;
370         text_info.color.a = 0.5;
371         auto text_area = text_producer::create(frame_factory, 0, 0, L"a", text_info, 1280, 720, false);
372
373         auto text_width = text_area->pixel_constraints().width;
374         binding<double> padding(1);
375         binding<double> panel_width = padding + text_width + padding;
376         binding<double> panel_height = padding + text_area->pixel_constraints().height + padding;
377
378         auto subscription = panel_width.on_change([&]
379         {
380                 CASPAR_LOG(info) << "Panel width: " << panel_width.get();
381         });
382
383         padding.set(2);
384
385         auto create_param = [](std::wstring elem) -> std::vector<std::wstring>
386         {
387                 std::vector<std::wstring> result;
388                 result.push_back(elem);
389                 return result;
390         };
391
392         //auto& car_layer = scene->create_layer(create_producer(frame_factory, format_desc, create_param(L"car")));
393         //car_layer.hidden = scene->frame() % 50 > 25 || !(scene->frame() < 1000);
394         std::vector<std::wstring> sub_params;
395         sub_params.push_back(L"[FREEHAND]");
396         sub_params.push_back(L"640");
397         sub_params.push_back(L"360");
398         scene->create_layer(create_producer(frame_factory, format_desc, sub_params), 10, 10, L"freehand");
399         sub_params.clear();
400
401         auto& color_layer = scene->create_layer(create_producer(frame_factory, format_desc, create_param(L"RED")), 110, 10, L"color");
402         color_layer.producer.get()->pixel_constraints().width.set(1000);
403         color_layer.producer.get()->pixel_constraints().height.set(550);
404
405         //scene->create_layer(create_producer(frame_factory, format_desc, create_param(L"SP")), 50, 50);
406
407         auto& upper_left = scene->create_layer(create_producer(frame_factory, format_desc, create_param(L"scene/upper_left")), L"upper_left");
408         auto& upper_right = scene->create_layer(create_producer(frame_factory, format_desc, create_param(L"scene/upper_right")), L"upper_right");
409         auto& lower_left = scene->create_layer(create_producer(frame_factory, format_desc, create_param(L"scene/lower_left")), L"lower_left");
410         auto& lower_right = scene->create_layer(create_producer(frame_factory, format_desc, create_param(L"scene/lower_right")), L"lower_right");
411         auto& text_layer = scene->create_layer(text_area, L"text_area");
412         upper_left.adjustments.opacity.bind(text_layer.adjustments.opacity);
413         upper_right.adjustments.opacity.bind(text_layer.adjustments.opacity);
414         lower_left.adjustments.opacity.bind(text_layer.adjustments.opacity);
415         lower_right.adjustments.opacity.bind(text_layer.adjustments.opacity);
416
417         /*
418         binding<double> panel_x = (scene->frame()
419                         .as<double>()
420                         .transformed([](double v) { return std::sin(v / 20.0); })
421                         * 20.0
422                         + 40.0)
423                         .transformed([](double v) { return std::floor(v); }); // snap to pixels instead of subpixels
424                         */
425         tweener tween(L"easeoutbounce");
426         binding<double> panel_x(0);
427
428         scene->add_keyframe(panel_x, -panel_width, 0);
429         scene->add_keyframe(panel_x, 300.0, 50, L"easeinoutsine");
430         scene->add_keyframe(panel_x, 300.0, 50 * 4);
431         scene->add_keyframe(panel_x, 1000.0, 50 * 5, L"easeinoutsine");
432         //panel_x = delay(panel_x, add_tween(panel_x, scene->frame(), 200.0, int64_t(50), L"linear"), scene->frame(), int64_t(100));
433         /*binding<double> panel_x = when(scene->frame() < 50)
434                 .then(scene->frame().as<double>().transformed([tween](double t) { return tween(t, 0.0, 200, 50); }))
435                 .otherwise(200.0);*/
436         //binding<double> panel_y = when(car_layer.hidden).then(500.0).otherwise(-panel_x + 300);
437         binding<double> panel_y(500.0);
438         scene->add_keyframe(panel_y, panel_y.get(), 50 * 4);
439         scene->add_keyframe(panel_y, 720.0, 50 * 5, L"easeinexpo");
440
441         scene->add_keyframe(text_layer.adjustments.opacity, 1.0, 100);
442         scene->add_keyframe(text_layer.adjustments.opacity, 0.0, 125, L"linear");
443         scene->add_keyframe(text_layer.adjustments.opacity, 1.0, 150, L"linear");
444
445         upper_left.position.x = panel_x;
446         upper_left.position.y = panel_y;
447         upper_right.position.x = upper_left.position.x + upper_left.producer.get()->pixel_constraints().width + panel_width;
448         upper_right.position.y = upper_left.position.y;
449         lower_left.position.x = upper_left.position.x;
450         lower_left.position.y = upper_left.position.y + upper_left.producer.get()->pixel_constraints().height + panel_height;
451         lower_right.position.x = upper_right.position.x;
452         lower_right.position.y = lower_left.position.y;
453         text_layer.position.x = upper_left.position.x + upper_left.producer.get()->pixel_constraints().width + padding;
454         text_layer.position.y = upper_left.position.y + upper_left.producer.get()->pixel_constraints().height + padding + text_area->current_bearing_y().as<double>();
455
456         text_area->text().bind(scene->create_variable<std::wstring>(L"text", true));
457
458         auto params2 = params;
459         params2.erase(params2.begin());
460
461         scene->call(params2);
462
463         return scene;
464 }
465
466 }}}