]> git.sesse.net Git - casparcg/blobdiff - core/producer/stage.cpp
Merge pull request #131 from Julusian/master
[casparcg] / core / producer / stage.cpp
index 3422cf4c2bfcc109f3c92c446c2ec2a56f550644..59fafd065d9ffb70ce8b52f32c504f9a2b6f6aee 100644 (file)
@@ -96,15 +96,18 @@ struct stage::implementation : public std::enable_shared_from_this<implementatio
        boost::timer                                                                                                                             produce_timer_;\r
        boost::timer                                                                                                                             tick_timer_;\r
                                                                                                                                                                 \r
-       std::map<int, layer>                                                                                                             layers_;       \r
+       std::map<int, std::shared_ptr<layer>>                                                                            layers_;       \r
        tbb::concurrent_unordered_map<int, tweened_transform<core::frame_transform>> transforms_;       \r
+       \r
+       monitor::subject                                                                                                                         monitor_subject_;\r
 \r
-       executor                                                executor_;\r
+       executor                                                                                                                                         executor_;\r
 public:\r
        implementation(const safe_ptr<diagnostics::graph>& graph, const safe_ptr<stage::target_t>& target, const video_format_desc& format_desc)  \r
                : graph_(graph)\r
                , format_desc_(format_desc)\r
                , target_(target)\r
+               , monitor_subject_("/stage")\r
                , executor_(L"stage")\r
        {\r
                graph_->set_color("tick-time", diagnostics::color(0.0f, 0.6f, 0.9f, 0.8));      \r
@@ -125,10 +128,10 @@ public:
 \r
                        std::map<int, safe_ptr<basic_frame>> frames;\r
                \r
-                       BOOST_FOREACH(auto& layer, layers_)                     \r
-                               frames[layer.first] = basic_frame::empty();     \r
+                       for(auto it = layers_.begin(); it != layers_.end(); ++it)\r
+                               frames[it->first] = basic_frame::empty();       \r
 \r
-                       tbb::parallel_for_each(layers_.begin(), layers_.end(), [&](std::map<int, layer>::value_type& layer) \r
+                       tbb::parallel_for_each(layers_.begin(), layers_.end(), [&](std::map<int, std::shared_ptr<layer>>::value_type& layer) \r
                        {\r
                                auto transform = transforms_[layer.first].fetch_and_tick(1);\r
 \r
@@ -142,7 +145,7 @@ public:
                                if(transform.is_key)\r
                                        hints |= frame_producer::ALPHA_HINT;\r
 \r
-                               auto frame = layer.second.receive(hints);       \r
+                               auto frame = layer.second->receive(hints);      \r
                                \r
                                auto frame1 = make_safe<core::basic_frame>(frame);\r
                                frame1->get_frame_transform() = transform;\r
@@ -228,11 +231,22 @@ public:
                }, high_priority);\r
        }\r
                \r
+       layer& get_layer(int index)\r
+       {\r
+               auto it = layers_.find(index);\r
+               if(it == std::end(layers_))\r
+               {\r
+                       it = layers_.insert(std::make_pair(index, std::make_shared<layer>(index))).first;\r
+                       it->second->monitor_output().link_target(&monitor_subject_);\r
+               }\r
+               return *it->second;\r
+       }\r
+\r
        void load(int index, const safe_ptr<frame_producer>& producer, bool preview, int auto_play_delta)\r
        {\r
                executor_.begin_invoke([=]\r
                {\r
-                       layers_[index].load(producer, preview, auto_play_delta);\r
+                       get_layer(index).load(producer, preview, auto_play_delta);\r
                }, high_priority);\r
        }\r
 \r
@@ -240,7 +254,7 @@ public:
        {               \r
                executor_.begin_invoke([=]\r
                {\r
-                       layers_[index].pause();\r
+                       get_layer(index).pause();\r
                }, high_priority);\r
        }\r
 \r
@@ -248,7 +262,7 @@ public:
        {               \r
                executor_.begin_invoke([=]\r
                {\r
-                       layers_[index].play();\r
+                       get_layer(index).play();\r
                }, high_priority);\r
        }\r
 \r
@@ -256,7 +270,7 @@ public:
        {               \r
                executor_.begin_invoke([=]\r
                {\r
-                       layers_[index].stop();\r
+                       get_layer(index).stop();\r
                }, high_priority);\r
        }\r
 \r
@@ -280,47 +294,77 @@ public:
        {\r
                return std::move(*executor_.invoke([=]\r
                {\r
-                       return std::make_shared<boost::unique_future<std::wstring>>(std::move(layers_[index].call(foreground, param)));\r
+                       return std::make_shared<boost::unique_future<std::wstring>>(std::move(get_layer(index).call(foreground, param)));\r
                }, high_priority));\r
        }\r
        \r
-       void swap_layers(const safe_ptr<stage>& other)\r
+       void swap_layers(stage& other)\r
        {\r
-               if(other->impl_.get() == this)\r
+               auto other_impl = other.impl_;\r
+\r
+               if(other_impl.get() == this)\r
                        return;\r
                \r
                auto func = [=]\r
                {\r
-                       std::swap(layers_, other->impl_->layers_);\r
+                       auto layers                     = layers_ | boost::adaptors::map_values;\r
+                       auto other_layers       = other_impl->layers_ | boost::adaptors::map_values;\r
+\r
+                       BOOST_FOREACH(auto& layer, layers)\r
+                               layer->monitor_output().unlink_target(&monitor_subject_);\r
+                       \r
+                       BOOST_FOREACH(auto& layer, other_layers)\r
+                               layer->monitor_output().unlink_target(&monitor_subject_);\r
+                       \r
+                       std::swap(layers_, other_impl->layers_);\r
+                                               \r
+                       BOOST_FOREACH(auto& layer, layers)\r
+                               layer->monitor_output().link_target(&monitor_subject_);\r
+                       \r
+                       BOOST_FOREACH(auto& layer, other_layers)\r
+                               layer->monitor_output().link_target(&monitor_subject_);\r
                };              \r
+\r
                executor_.begin_invoke([=]\r
                {\r
-                       other->impl_->executor_.invoke(func, high_priority);\r
-               }, high_priority);\r
+                       other_impl->executor_.invoke(func, task_priority::high_priority);\r
+               }, task_priority::high_priority);\r
        }\r
 \r
-       void swap_layer(int index, size_t other_index)\r
+       void swap_layer(int index, int other_index)\r
        {\r
                executor_.begin_invoke([=]\r
                {\r
-                       std::swap(layers_[index], layers_[other_index]);\r
-               }, high_priority);\r
+                       std::swap(get_layer(index), get_layer(other_index));\r
+               }, task_priority::high_priority);\r
        }\r
 \r
-       void swap_layer(int index, size_t other_index, const safe_ptr<stage>& other)\r
+       void swap_layer(int index, int other_index, stage& other)\r
        {\r
-               if(other->impl_.get() == this)\r
+               auto other_impl = other.impl_;\r
+\r
+               if(other_impl.get() == this)\r
                        swap_layer(index, other_index);\r
                else\r
                {\r
                        auto func = [=]\r
                        {\r
-                               std::swap(layers_[index], other->impl_->layers_[other_index]);\r
+                               auto& my_layer          = get_layer(index);\r
+                               auto& other_layer       = other_impl->get_layer(other_index);\r
+\r
+                               my_layer.monitor_output().unlink_target(&monitor_subject_);\r
+                               other_layer.monitor_output().unlink_target(&other_impl->monitor_subject_);\r
+\r
+                               std::swap(my_layer, other_layer);\r
+\r
+                               my_layer.monitor_output().link_target(&monitor_subject_);\r
+                               other_layer.monitor_output().link_target(&other_impl->monitor_subject_);\r
                        };              \r
+\r
                        executor_.begin_invoke([=]\r
                        {\r
-                               other->impl_->executor_.invoke(func, high_priority);\r
-                       }, high_priority);\r
+                               other_impl->executor_.invoke(func, task_priority::high_priority);\r
+                       }, task_priority::high_priority);\r
                }\r
        }\r
                \r
@@ -328,7 +372,7 @@ public:
        {\r
                return executor_.begin_invoke([=]\r
                {\r
-                       return layers_[index].foreground();\r
+                       return get_layer(index).foreground();\r
                }, high_priority);\r
        }\r
        \r
@@ -336,7 +380,7 @@ public:
        {\r
                return executor_.begin_invoke([=]\r
                {\r
-                       return layers_[index].background();\r
+                       return get_layer(index).background();\r
                }, high_priority);\r
        }\r
        \r
@@ -354,7 +398,7 @@ public:
                {\r
                        boost::property_tree::wptree info;\r
                        BOOST_FOREACH(auto& layer, layers_)                     \r
-                               info.add_child(L"layers.layer", layer.second.info())\r
+                               info.add_child(L"layers.layer", layer.second->info())\r
                                        .add(L"index", layer.first);    \r
                        return info;\r
                }, high_priority));\r
@@ -364,12 +408,13 @@ public:
        {\r
                return std::move(executor_.begin_invoke([=]() -> boost::property_tree::wptree\r
                {\r
-                       return layers_[index].info();\r
+                       return get_layer(index).info();\r
                }, high_priority));\r
        }\r
 };\r
 \r
-stage::stage(const safe_ptr<diagnostics::graph>& graph, const safe_ptr<target_t>& target, const video_format_desc& format_desc) : impl_(new implementation(graph, target, format_desc)){}\r
+stage::stage(const safe_ptr<diagnostics::graph>& graph, const safe_ptr<target_t>& target, const video_format_desc& format_desc) \r
+       : impl_(new implementation(graph, target, format_desc)){}\r
 void stage::apply_transforms(const std::vector<stage::transform_tuple_t>& transforms){impl_->apply_transforms(transforms);}\r
 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
 void stage::clear_transforms(int index){impl_->clear_transforms(index);}\r
@@ -381,13 +426,14 @@ void stage::play(int index){impl_->play(index);}
 void stage::stop(int index){impl_->stop(index);}\r
 void stage::clear(int index){impl_->clear(index);}\r
 void stage::clear(){impl_->clear();}\r
-void stage::swap_layers(const safe_ptr<stage>& other){impl_->swap_layers(other);}\r
+void stage::swap_layers(const safe_ptr<stage>& other){impl_->swap_layers(*other);}\r
 void stage::swap_layer(int index, size_t other_index){impl_->swap_layer(index, other_index);}\r
-void stage::swap_layer(int index, size_t other_index, const safe_ptr<stage>& other){impl_->swap_layer(index, other_index, other);}\r
+void stage::swap_layer(int index, size_t other_index, const safe_ptr<stage>& other){impl_->swap_layer(index, other_index, *other);}\r
 boost::unique_future<safe_ptr<frame_producer>> stage::foreground(int index) {return impl_->foreground(index);}\r
 boost::unique_future<safe_ptr<frame_producer>> stage::background(int index) {return impl_->background(index);}\r
 boost::unique_future<std::wstring> stage::call(int index, bool foreground, const std::wstring& param){return impl_->call(index, foreground, param);}\r
 void stage::set_video_format_desc(const video_format_desc& format_desc){impl_->set_video_format_desc(format_desc);}\r
 boost::unique_future<boost::property_tree::wptree> stage::info() const{return impl_->info();}\r
 boost::unique_future<boost::property_tree::wptree> stage::info(int index) const{return impl_->info(index);}\r
+monitor::source& stage::monitor_output(){return impl_->monitor_subject_;}\r
 }}
\ No newline at end of file