]> git.sesse.net Git - casparcg/blob - core/producer/scene/scene_producer.h
[scene_producer] Added possibility to CALL/CG PLAY/CG STOP/CG NEXT/CG INVOKE layers...
[casparcg] / core / producer / scene / scene_producer.h
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 #pragma once
23
24 #include <common/log.h>
25
26 #include "../frame_producer.h"
27 #include "../../fwd.h"
28
29 #include "../binding.h"
30 #include "../variable.h"
31
32 namespace caspar { namespace core { namespace scene {
33
34 struct coord
35 {
36         binding<double> x;
37         binding<double> y;
38 };
39
40 struct rect
41 {
42         coord upper_left;
43         coord lower_right;
44 };
45
46 struct corners
47 {
48         coord upper_left;
49         coord upper_right;
50         coord lower_right;
51         coord lower_left;
52 };
53
54 struct adjustments
55 {
56         binding<double> opacity;
57
58         adjustments();
59 };
60
61 struct chroma_key
62 {
63         binding<bool>                           enable;
64         binding<double>                         target_hue;
65         binding<double>                         hue_width;
66         binding<double>                         min_saturation;
67         binding<double>                         min_brightness;
68         binding<double>                         softness;
69         binding<double>                         spill;
70         binding<double>                         spill_darken;
71 };
72
73 struct layer
74 {
75         binding<std::wstring>                                           name;
76         scene::coord                                                            anchor;
77         scene::coord                                                            position;
78         scene::rect                                                                     crop;
79         scene::corners                                                          perspective;
80         binding<double>                                                         rotation;
81         scene::adjustments                                                      adjustments;
82         binding<spl::shared_ptr<frame_producer>>        producer;
83         binding<bool>                                                           hidden;
84         binding<bool>                                                           is_key;
85         binding<bool>                                                           use_mipmap;
86         binding<core::blend_mode>                                       blend_mode;
87         scene::chroma_key                                                       chroma_key;
88
89         explicit layer(const std::wstring& name, const spl::shared_ptr<frame_producer>& producer);
90 };
91
92 struct keyframe
93 {
94         std::function<void ()>                                                                                          on_start_animate;
95         std::function<void (int64_t start_frame, int64_t current_frame)>        on_animate_to;
96         std::function<void ()>                                                                                          on_destination_frame;
97         int64_t                                                                                                                         destination_frame;
98 public:
99         keyframe(int64_t destination_frame)
100                 : destination_frame(destination_frame)
101         {
102         }
103 };
104
105 enum class mark_action
106 {
107         start,
108         stop,
109         jump_to,
110         remove
111 };
112
113 mark_action get_mark_action(const std::wstring& name);
114
115 class scene_producer : public frame_producer_base
116 {
117 public:
118         scene_producer(std::wstring producer_name, std::wstring template_name, int width, int height, const video_format_desc& format_desc);
119         ~scene_producer();
120
121         draw_frame receive_impl() override;
122         constraints& pixel_constraints() override;
123         void on_interaction(const interaction_event::ptr& event) override;
124         bool collides(double x, double y) const override;
125         std::wstring print() const override;
126         std::wstring name() const override;
127         std::future<std::wstring> call(const std::vector<std::wstring>& params) override;
128         boost::property_tree::wptree info() const override;
129         monitor::subject& monitor_output();
130
131         layer& create_layer(
132                         const spl::shared_ptr<frame_producer>& producer, int x, int y, const std::wstring& name);
133         layer& create_layer(
134                         const spl::shared_ptr<frame_producer>& producer, const std::wstring& name);
135         void reverse_layers();
136         layer& get_layer(const std::wstring& name);
137
138         binding<int64_t> timeline_frame();
139         binding<double> speed();
140
141         template<typename T> binding<T>& create_variable(
142                         const std::wstring& name, bool is_public, const std::wstring& expr = L"")
143         {
144                 std::shared_ptr<core::variable> var =
145                                 std::make_shared<core::variable_impl<T>>(expr, is_public);
146
147                 store_variable(name, var);
148
149                 return var->as<T>();
150         }
151
152         template<typename T>
153         void add_keyframe(
154                         binding<T>& to_affect,
155                         T destination_value,
156                         int64_t at_frame,
157                         const std::wstring& easing)
158         {
159                 add_keyframe(to_affect, binding<T>(destination_value), at_frame, easing);
160         }
161
162         template<typename T>
163         void add_keyframe(
164                         binding<T>& to_affect,
165                         const binding<T>& destination_value,
166                         int64_t at_frame,
167                         const std::wstring& easing)
168         {
169                 if (easing.empty())
170                 {
171                         add_keyframe(to_affect, destination_value, at_frame);
172                         return;
173                 }
174
175                 tweener tween(easing);
176                 keyframe k(at_frame);
177
178                 std::shared_ptr<T> start_value(new T);
179
180                 k.on_start_animate = [=]() mutable
181                 {
182                         *start_value = to_affect.get();
183                         to_affect.unbind();
184                 };
185
186                 k.on_destination_frame = [=]() mutable
187                 {
188                         to_affect.bind(destination_value);
189                 };
190
191                 k.on_animate_to =
192                                 [=](int64_t start_frame, int64_t current_frame) mutable
193                                 {
194                                         auto relative_frame = current_frame - start_frame;
195                                         auto duration = at_frame - start_frame;
196                                         auto tweened = static_cast<T>(tween(
197                                                         static_cast<double>(relative_frame),
198                                                         *start_value,
199                                                         destination_value.get() - *start_value,
200                                                         static_cast<double>(duration)));
201
202                                         to_affect.set(tweened);
203
204                                         //CASPAR_LOG(info) << relative_frame << L" " << *start_value << L" " << duration << L" " << tweened;
205                                 };
206
207                 store_keyframe(to_affect.identity(), k);
208         }
209
210         template<typename T>
211         void add_keyframe(binding<T>& to_affect, T set_value, int64_t at_frame)
212         {
213                 add_keyframe(to_affect, binding<T>(set_value), at_frame);
214         }
215
216         template<typename T>
217         void add_keyframe(binding<T>& to_affect, const binding<T>& set_value, int64_t at_frame)
218         {
219                 keyframe k(at_frame);
220
221                 k.on_destination_frame = [=]() mutable
222                 {
223                         to_affect.bind(set_value);
224                 };
225
226                 store_keyframe(to_affect.identity(), k);
227         }
228
229         void add_mark(int64_t at_frame, mark_action action, const std::wstring& label);
230         void add_task(binding<bool> when, std::function<void ()> task);
231
232         core::variable& get_variable(const std::wstring& name) override;
233         const std::vector<std::wstring>& get_variables() const override;
234 private:
235         void store_keyframe(void* timeline_identity, const keyframe& k);
236         void store_variable(
237                         const std::wstring& name, const std::shared_ptr<core::variable>& var);
238
239         struct impl;
240         std::unique_ptr<impl> impl_;
241 };
242
243 }}}