]> git.sesse.net Git - casparcg/blob - core/producer/stage.cpp
- Implemented real-time state notification using OSC-UDP.
[casparcg] / core / producer / stage.cpp
1 /*\r
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>\r
3 *\r
4 * This file is part of CasparCG (www.casparcg.com).\r
5 *\r
6 * CasparCG is free software: you can redistribute it and/or modify\r
7 * it under the terms of the GNU General Public License as published by\r
8 * the Free Software Foundation, either version 3 of the License, or\r
9 * (at your option) any later version.\r
10 *\r
11 * CasparCG is distributed in the hope that it will be useful,\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14 * GNU General Public License for more details.\r
15 *\r
16 * You should have received a copy of the GNU General Public License\r
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.\r
18 *\r
19 * Author: Robert Nagy, ronag89@gmail.com\r
20 */\r
21 \r
22 #include "../StdAfx.h"\r
23 \r
24 #include "stage.h"\r
25 \r
26 #include "layer.h"\r
27 \r
28 #include "frame/basic_frame.h"\r
29 #include "frame/frame_factory.h"\r
30 \r
31 #include <common/concurrency/executor.h>\r
32 \r
33 #include <core/producer/frame/frame_transform.h>\r
34 \r
35 #include <boost/foreach.hpp>\r
36 #include <boost/timer.hpp>\r
37 \r
38 #include <tbb/parallel_for_each.h>\r
39 #include <tbb/concurrent_unordered_map.h>\r
40 \r
41 #include <boost/property_tree/ptree.hpp>\r
42 \r
43 #include <map>\r
44 \r
45 namespace caspar { namespace core {\r
46         \r
47 template<typename T>\r
48 class tweened_transform\r
49 {\r
50         T source_;\r
51         T dest_;\r
52         int duration_;\r
53         int time_;\r
54         tweener_t tweener_;\r
55 public: \r
56         tweened_transform()\r
57                 : duration_(0)\r
58                 , time_(0)\r
59                 , tweener_(get_tweener(L"linear")){}\r
60         tweened_transform(const T& source, const T& dest, int duration, const std::wstring& tween = L"linear")\r
61                 : source_(source)\r
62                 , dest_(dest)\r
63                 , duration_(duration)\r
64                 , time_(0)\r
65                 , tweener_(get_tweener(tween)){}\r
66         \r
67         const T& source() const\r
68         {\r
69                 return source_;\r
70         }\r
71         \r
72         const T& dest() const\r
73         {\r
74                 return dest_;\r
75         }\r
76 \r
77         T fetch()\r
78         {\r
79                 return time_ == duration_ ? dest_ : tween(static_cast<double>(time_), source_, dest_, static_cast<double>(duration_), tweener_);\r
80         }\r
81 \r
82         T fetch_and_tick(int num)\r
83         {                                               \r
84                 time_ = std::min(time_+num, duration_);\r
85                 return fetch();\r
86         }\r
87 };\r
88 \r
89 struct stage::implementation : public std::enable_shared_from_this<implementation>\r
90                                                          , boost::noncopyable\r
91 {               \r
92         safe_ptr<diagnostics::graph>                                                                                             graph_;\r
93         safe_ptr<stage::target_t>                                                                                                        target_;\r
94         video_format_desc                                                                                                                        format_desc_;\r
95                                                                                                                                                                  \r
96         boost::timer                                                                                                                             produce_timer_;\r
97         boost::timer                                                                                                                             tick_timer_;\r
98                                                                                                                                                                  \r
99         std::map<int, std::shared_ptr<layer>>                                                                            layers_;       \r
100         tbb::concurrent_unordered_map<int, tweened_transform<core::frame_transform>> transforms_;       \r
101         \r
102         monitor::subject                                                                                                                         monitor_subject_;\r
103 \r
104         executor                                                                                                                                         executor_;\r
105 public:\r
106         implementation(const safe_ptr<diagnostics::graph>& graph, const safe_ptr<stage::target_t>& target, const video_format_desc& format_desc)  \r
107                 : graph_(graph)\r
108                 , format_desc_(format_desc)\r
109                 , target_(target)\r
110                 , monitor_subject_("/stage")\r
111                 , executor_(L"stage")\r
112         {\r
113                 graph_->set_color("tick-time", diagnostics::color(0.0f, 0.6f, 0.9f, 0.8));      \r
114                 graph_->set_color("produce-time", diagnostics::color(0.0f, 1.0f, 0.0f));\r
115         }\r
116 \r
117         void spawn_token()\r
118         {\r
119                 std::weak_ptr<implementation> self = shared_from_this();\r
120                 executor_.begin_invoke([=]{tick(self);});\r
121         }\r
122                                                         \r
123         void tick(const std::weak_ptr<implementation>& self)\r
124         {               \r
125                 try\r
126                 {\r
127                         produce_timer_.restart();\r
128 \r
129                         std::map<int, safe_ptr<basic_frame>> frames;\r
130                 \r
131                         for(auto it = layers_.begin(); it != layers_.end(); ++it)\r
132                                 frames[it->first] = basic_frame::empty();       \r
133 \r
134                         tbb::parallel_for_each(layers_.begin(), layers_.end(), [&](std::map<int, std::shared_ptr<layer>>::value_type& layer) \r
135                         {\r
136                                 auto transform = transforms_[layer.first].fetch_and_tick(1);\r
137 \r
138                                 int hints = frame_producer::NO_HINT;\r
139                                 if(format_desc_.field_mode != field_mode::progressive)\r
140                                 {\r
141                                         hints |= std::abs(transform.fill_scale[1]  - 1.0) > 0.0001 ? frame_producer::DEINTERLACE_HINT : frame_producer::NO_HINT;\r
142                                         hints |= std::abs(transform.fill_translation[1]) > 0.0001 ? frame_producer::DEINTERLACE_HINT : frame_producer::NO_HINT;\r
143                                 }\r
144 \r
145                                 if(transform.is_key)\r
146                                         hints |= frame_producer::ALPHA_HINT;\r
147 \r
148                                 auto frame = layer.second->receive(hints);      \r
149                                 \r
150                                 auto frame1 = make_safe<core::basic_frame>(frame);\r
151                                 frame1->get_frame_transform() = transform;\r
152 \r
153                                 if(format_desc_.field_mode != core::field_mode::progressive)\r
154                                 {                               \r
155                                         auto frame2 = make_safe<core::basic_frame>(frame);\r
156                                         frame2->get_frame_transform() = transforms_[layer.first].fetch_and_tick(1);\r
157                                         frame1 = core::basic_frame::interlace(frame1, frame2, format_desc_.field_mode);\r
158                                 }\r
159 \r
160                                 frames[layer.first] = frame1;\r
161                         });\r
162                         \r
163                         graph_->set_value("produce-time", produce_timer_.elapsed()*format_desc_.fps*0.5);\r
164                         \r
165                         std::shared_ptr<void> ticket(nullptr, [self](void*)\r
166                         {\r
167                                 auto self2 = self.lock();\r
168                                 if(self2)                               \r
169                                         self2->executor_.begin_invoke([=]{tick(self);});                                \r
170                         });\r
171 \r
172                         target_->send(std::make_pair(frames, ticket));\r
173 \r
174                         graph_->set_value("tick-time", tick_timer_.elapsed()*format_desc_.fps*0.5);\r
175                         tick_timer_.restart();\r
176                 }\r
177                 catch(...)\r
178                 {\r
179                         layers_.clear();\r
180                         CASPAR_LOG_CURRENT_EXCEPTION();\r
181                 }               \r
182         }\r
183                 \r
184         void set_transform(int index, const frame_transform& transform, unsigned int mix_duration, const std::wstring& tween)\r
185         {\r
186                 executor_.begin_invoke([=]\r
187                 {\r
188                         auto src = transforms_[index].fetch();\r
189                         auto dst = transform;\r
190                         transforms_[index] = tweened_transform<frame_transform>(src, dst, mix_duration, tween);\r
191                 }, high_priority);\r
192         }\r
193                                         \r
194         void apply_transforms(const std::vector<std::tuple<int, stage::transform_func_t, unsigned int, std::wstring>>& transforms)\r
195         {\r
196                 executor_.begin_invoke([=]\r
197                 {\r
198                         BOOST_FOREACH(auto& transform, transforms)\r
199                         {\r
200                                 auto& tween = transforms_[std::get<0>(transform)];\r
201                                 auto src = tween.fetch();\r
202                                 auto dst = std::get<1>(transform)(tween.dest());\r
203                                 transforms_[std::get<0>(transform)] = tweened_transform<frame_transform>(src, dst, std::get<2>(transform), std::get<3>(transform));\r
204                         }\r
205                 }, high_priority);\r
206         }\r
207                                                 \r
208         void apply_transform(int index, const stage::transform_func_t& transform, unsigned int mix_duration, const std::wstring& tween)\r
209         {\r
210                 executor_.begin_invoke([=]\r
211                 {\r
212                         auto src = transforms_[index].fetch();\r
213                         auto dst = transform(src);\r
214                         transforms_[index] = tweened_transform<frame_transform>(src, dst, mix_duration, tween);\r
215                 }, high_priority);\r
216         }\r
217 \r
218         void clear_transforms(int index)\r
219         {\r
220                 executor_.begin_invoke([=]\r
221                 {\r
222                         transforms_[index] = tweened_transform<core::frame_transform>();\r
223                 }, high_priority);\r
224         }\r
225 \r
226         void clear_transforms()\r
227         {\r
228                 executor_.begin_invoke([=]\r
229                 {\r
230                         transforms_.clear();\r
231                 }, high_priority);\r
232         }\r
233                 \r
234         layer& get_layer(int index)\r
235         {\r
236                 auto it = layers_.find(index);\r
237                 if(it == std::end(layers_))\r
238                 {\r
239                         it = layers_.insert(std::make_pair(index, std::make_shared<layer>(index))).first;\r
240                         it->second->monitor_output().link_target(&monitor_subject_);\r
241                 }\r
242                 return *it->second;\r
243         }\r
244 \r
245         void load(int index, const safe_ptr<frame_producer>& producer, bool preview, int auto_play_delta)\r
246         {\r
247                 executor_.begin_invoke([=]\r
248                 {\r
249                         get_layer(index).load(producer, preview, auto_play_delta);\r
250                 }, high_priority);\r
251         }\r
252 \r
253         void pause(int index)\r
254         {               \r
255                 executor_.begin_invoke([=]\r
256                 {\r
257                         get_layer(index).pause();\r
258                 }, high_priority);\r
259         }\r
260 \r
261         void play(int index)\r
262         {               \r
263                 executor_.begin_invoke([=]\r
264                 {\r
265                         get_layer(index).play();\r
266                 }, high_priority);\r
267         }\r
268 \r
269         void stop(int index)\r
270         {               \r
271                 executor_.begin_invoke([=]\r
272                 {\r
273                         get_layer(index).stop();\r
274                 }, high_priority);\r
275         }\r
276 \r
277         void clear(int index)\r
278         {\r
279                 executor_.begin_invoke([=]\r
280                 {\r
281                         layers_.erase(index);\r
282                 }, high_priority);\r
283         }\r
284                 \r
285         void clear()\r
286         {\r
287                 executor_.begin_invoke([=]\r
288                 {\r
289                         layers_.clear();\r
290                 }, high_priority);\r
291         }       \r
292         \r
293         boost::unique_future<std::wstring> call(int index, bool foreground, const std::wstring& param)\r
294         {\r
295                 return std::move(*executor_.invoke([=]\r
296                 {\r
297                         return std::make_shared<boost::unique_future<std::wstring>>(std::move(get_layer(index).call(foreground, param)));\r
298                 }, high_priority));\r
299         }\r
300         \r
301         void swap_layers(stage& other)\r
302         {\r
303                 auto other_impl = other.impl_;\r
304 \r
305                 if(other_impl.get() == this)\r
306                         return;\r
307                 \r
308                 auto func = [=]\r
309                 {\r
310                         auto layers                     = layers_ | boost::adaptors::map_values;\r
311                         auto other_layers       = other_impl->layers_ | boost::adaptors::map_values;\r
312 \r
313                         BOOST_FOREACH(auto& layer, layers)\r
314                                 layer->monitor_output().unlink_target(&monitor_subject_);\r
315                         \r
316                         BOOST_FOREACH(auto& layer, other_layers)\r
317                                 layer->monitor_output().unlink_target(&monitor_subject_);\r
318                         \r
319                         std::swap(layers_, other_impl->layers_);\r
320                                                 \r
321                         BOOST_FOREACH(auto& layer, layers)\r
322                                 layer->monitor_output().link_target(&monitor_subject_);\r
323                         \r
324                         BOOST_FOREACH(auto& layer, other_layers)\r
325                                 layer->monitor_output().link_target(&monitor_subject_);\r
326                 };              \r
327 \r
328                 executor_.begin_invoke([=]\r
329                 {\r
330                         other_impl->executor_.invoke(func, task_priority::high_priority);\r
331                 }, task_priority::high_priority);\r
332         }\r
333 \r
334         void swap_layer(int index, int other_index)\r
335         {\r
336                 executor_.begin_invoke([=]\r
337                 {\r
338                         std::swap(get_layer(index), get_layer(other_index));\r
339                 }, task_priority::high_priority);\r
340         }\r
341 \r
342         void swap_layer(int index, int other_index, stage& other)\r
343         {\r
344                 auto other_impl = other.impl_;\r
345 \r
346                 if(other_impl.get() == this)\r
347                         swap_layer(index, other_index);\r
348                 else\r
349                 {\r
350                         auto func = [=]\r
351                         {\r
352                                 auto& my_layer          = get_layer(index);\r
353                                 auto& other_layer       = other_impl->get_layer(other_index);\r
354 \r
355                                 my_layer.monitor_output().unlink_target(&monitor_subject_);\r
356                                 other_layer.monitor_output().unlink_target(&other_impl->monitor_subject_);\r
357 \r
358                                 std::swap(my_layer, other_layer);\r
359 \r
360                                 my_layer.monitor_output().link_target(&monitor_subject_);\r
361                                 other_layer.monitor_output().link_target(&other_impl->monitor_subject_);\r
362                         };              \r
363 \r
364                         executor_.begin_invoke([=]\r
365                         {\r
366                                 other_impl->executor_.invoke(func, task_priority::high_priority);\r
367                         }, task_priority::high_priority);\r
368                 }\r
369         }\r
370                 \r
371         boost::unique_future<safe_ptr<frame_producer>> foreground(int index)\r
372         {\r
373                 return executor_.begin_invoke([=]\r
374                 {\r
375                         return get_layer(index).foreground();\r
376                 }, high_priority);\r
377         }\r
378         \r
379         boost::unique_future<safe_ptr<frame_producer>> background(int index)\r
380         {\r
381                 return executor_.begin_invoke([=]\r
382                 {\r
383                         return get_layer(index).background();\r
384                 }, high_priority);\r
385         }\r
386         \r
387         void set_video_format_desc(const video_format_desc& format_desc)\r
388         {\r
389                 executor_.begin_invoke([=]\r
390                 {\r
391                         format_desc_ = format_desc;\r
392                 }, high_priority);\r
393         }\r
394 \r
395         boost::unique_future<boost::property_tree::wptree> info()\r
396         {\r
397                 return std::move(executor_.begin_invoke([this]() -> boost::property_tree::wptree\r
398                 {\r
399                         boost::property_tree::wptree info;\r
400                         BOOST_FOREACH(auto& layer, layers_)                     \r
401                                 info.add_child(L"layers.layer", layer.second->info())\r
402                                         .add(L"index", layer.first);    \r
403                         return info;\r
404                 }, high_priority));\r
405         }\r
406 \r
407         boost::unique_future<boost::property_tree::wptree> info(int index)\r
408         {\r
409                 return std::move(executor_.begin_invoke([=]() -> boost::property_tree::wptree\r
410                 {\r
411                         return get_layer(index).info();\r
412                 }, high_priority));\r
413         }\r
414 };\r
415 \r
416 stage::stage(const safe_ptr<diagnostics::graph>& graph, const safe_ptr<target_t>& target, const video_format_desc& format_desc) \r
417         : impl_(new implementation(graph, target, format_desc)){}\r
418 void stage::apply_transforms(const std::vector<stage::transform_tuple_t>& transforms){impl_->apply_transforms(transforms);}\r
419 void stage::apply_transform(int index, const std::function<core::frame_transform(core::frame_transform)>& transform, unsigned int mix_duration, const std::wstring& tween){impl_->apply_transform(index, transform, mix_duration, tween);}\r
420 void stage::clear_transforms(int index){impl_->clear_transforms(index);}\r
421 void stage::clear_transforms(){impl_->clear_transforms();}\r
422 void stage::spawn_token(){impl_->spawn_token();}\r
423 void stage::load(int index, const safe_ptr<frame_producer>& producer, bool preview, int auto_play_delta){impl_->load(index, producer, preview, auto_play_delta);}\r
424 void stage::pause(int index){impl_->pause(index);}\r
425 void stage::play(int index){impl_->play(index);}\r
426 void stage::stop(int index){impl_->stop(index);}\r
427 void stage::clear(int index){impl_->clear(index);}\r
428 void stage::clear(){impl_->clear();}\r
429 void stage::swap_layers(const safe_ptr<stage>& other){impl_->swap_layers(*other);}\r
430 void stage::swap_layer(int index, size_t other_index){impl_->swap_layer(index, other_index);}\r
431 void stage::swap_layer(int index, size_t other_index, const safe_ptr<stage>& other){impl_->swap_layer(index, other_index, *other);}\r
432 boost::unique_future<safe_ptr<frame_producer>> stage::foreground(int index) {return impl_->foreground(index);}\r
433 boost::unique_future<safe_ptr<frame_producer>> stage::background(int index) {return impl_->background(index);}\r
434 boost::unique_future<std::wstring> stage::call(int index, bool foreground, const std::wstring& param){return impl_->call(index, foreground, param);}\r
435 void stage::set_video_format_desc(const video_format_desc& format_desc){impl_->set_video_format_desc(format_desc);}\r
436 boost::unique_future<boost::property_tree::wptree> stage::info() const{return impl_->info();}\r
437 boost::unique_future<boost::property_tree::wptree> stage::info(int index) const{return impl_->info(index);}\r
438 monitor::source& stage::monitor_output(){return impl_->monitor_subject_;}\r
439 }}