]> git.sesse.net Git - casparcg/blobdiff - core/producer/stage.cpp
2.0 image_mixer: Refactored, core: Fixed destruction proxy usage.
[casparcg] / core / producer / stage.cpp
index 13c547aa4790ec65d635a4c88d898d594403fff4..9fe30bbd45b917d03a901138cba211c987a16e9f 100644 (file)
@@ -30,6 +30,7 @@
 #include <core/producer/frame/frame_factory.h>\r
 \r
 #include <common/concurrency/executor.h>\r
+#include <common/utility/move_on_copy.h>\r
 \r
 #include <boost/foreach.hpp>\r
 \r
 #include <set>\r
 \r
 namespace caspar { namespace core {\r
-               \r
-void destroy_producer(safe_ptr<frame_producer>& producer)\r
-{\r
-       if(!producer.unique())\r
-               CASPAR_LOG(debug) << producer->print() << L" Not destroyed on safe asynchronous destruction thread.";\r
-       \r
-       producer = frame_producer::empty();\r
-}\r
-\r
-class destroy_producer_proxy : public frame_producer\r
-{\r
-       safe_ptr<frame_producer> producer_;\r
-       executor& destroy_context_;\r
-public:\r
-       destroy_producer_proxy(executor& destroy_context, const safe_ptr<frame_producer>& producer) \r
-               : producer_(producer)\r
-               , destroy_context_(destroy_context){}\r
-\r
-       ~destroy_producer_proxy()\r
-       {               \r
-               if(destroy_context_.size() > 4)\r
-                       CASPAR_LOG(error) << L" Potential destroyer deadlock.";\r
-\r
-               destroy_context_.begin_invoke(std::bind(&destroy_producer, std::move(producer_)));\r
-       }\r
-\r
-       virtual safe_ptr<basic_frame>           receive(int hints)                                                                                              {return producer_->receive(hints);}\r
-       virtual safe_ptr<basic_frame>           last_frame() const                                                                                              {return producer_->last_frame();}\r
-       virtual std::wstring                            print() const                                                                                                   {return producer_->print();}\r
-       virtual void                                            param(const std::wstring& str)                                                                  {producer_->param(str);}\r
-       virtual safe_ptr<frame_producer>        get_following_producer() const                                                                  {return producer_->get_following_producer();}\r
-       virtual void                                            set_leading_producer(const safe_ptr<frame_producer>& producer)  {producer_->set_leading_producer(producer);}\r
-       virtual int64_t                                         nb_frames() const                                                                                               {return producer_->nb_frames();}\r
-};\r
 \r
 struct stage::implementation : boost::noncopyable\r
 {              \r
@@ -85,11 +52,11 @@ public:
        }\r
                                                \r
        std::map<int, safe_ptr<basic_frame>> execute()\r
-       {                       \r
-               std::map<int, safe_ptr<basic_frame>> frames;\r
-\r
+       {               \r
                try\r
-               {                       \r
+               {\r
+                       std::map<int, safe_ptr<basic_frame>> frames;\r
+               \r
                        BOOST_FOREACH(auto& layer, layers_)                     \r
                                frames[layer.first] = basic_frame::empty();     \r
 \r
@@ -97,20 +64,21 @@ public:
                        {\r
                                frames[layer.first] = layer.second.receive();   \r
                        });\r
+\r
+                       return frames;\r
                }\r
                catch(...)\r
                {\r
-                       CASPAR_LOG_CURRENT_EXCEPTION();\r
-               }\r
-               \r
-               return frames;\r
+                       CASPAR_LOG(error) << L"[stage] Error detected";\r
+                       throw;\r
+               }               \r
        }\r
 \r
        void load(int index, const safe_ptr<frame_producer>& producer, bool preview, int auto_play_delta)\r
        {\r
                channel_.execution().invoke([&]\r
                {\r
-                       layers_[index].load(make_safe<destroy_producer_proxy>(channel_.destruction(), producer), preview, auto_play_delta);\r
+                       layers_[index].load(create_destroy_producer_proxy(channel_.destruction(), producer), preview, auto_play_delta);\r
                }, high_priority);\r
        }\r
 \r
@@ -158,7 +126,7 @@ public:
        {\r
                channel_.execution().invoke([&]\r
                {\r
-                       layers_[index].swap(layers_[other_index]);\r
+                       std::swap(layers_[index], layers_[other_index]);\r
                }, high_priority);\r
        }\r
 \r
@@ -168,11 +136,10 @@ public:
                        swap_layer(index, other_index);\r
                else\r
                {\r
-                       if(channel_.get_format_desc() != other.impl_->channel_.get_format_desc() || &channel_.ogl() != &other.impl_->channel_.ogl())\r
-                               BOOST_THROW_EXCEPTION(not_supported() << msg_info("Cannot swap between incompatible channels."));\r
-\r
-                       auto func = [&]{layers_[index].swap(other.impl_->layers_[other_index]);};\r
-               \r
+                       auto func = [&]\r
+                       {\r
+                               std::swap(layers_[index], other.impl_->layers_[other_index]);\r
+                       };              \r
                        channel_.execution().invoke([&]{other.impl_->channel_.execution().invoke(func, high_priority);}, high_priority);\r
                }\r
        }\r
@@ -182,34 +149,29 @@ public:
                if(other.impl_.get() == this)\r
                        return;\r
                \r
-               if(channel_.get_format_desc() != other.impl_->channel_.get_format_desc() || &channel_.ogl() != &other.impl_->channel_.ogl())\r
-                       BOOST_THROW_EXCEPTION(not_supported() << msg_info("Cannot swap between incompatible channels."));\r
-\r
                auto func = [&]\r
                {\r
-                       auto sel_first = [](const std::pair<int, layer>& pair){return pair.first;};\r
-\r
-                       std::set<int> indices;\r
-                       auto inserter = std::inserter(indices, indices.begin());\r
-\r
-                       std::transform(layers_.begin(), layers_.end(), inserter, sel_first);\r
-                       std::transform(other.impl_->layers_.begin(), other.impl_->layers_.end(), inserter, sel_first);\r
+                       std::swap(layers_, other.impl_->layers_);\r
+               };              \r
+               channel_.execution().invoke([&]{other.impl_->channel_.execution().invoke(func, high_priority);}, high_priority);\r
+       }\r
 \r
-                       BOOST_FOREACH(auto index, indices)\r
-                               layers_[index].swap(other.impl_->layers_[index]);\r
-               };\r
-               \r
-               channel_.execution().invoke([&]{other.impl_->channel_.execution().invoke(func, high_priority);});\r
+       layer_status get_status(int index)\r
+       {               \r
+               return channel_.execution().invoke([&]\r
+               {\r
+                       return layers_[index].status();\r
+               }, high_priority );\r
        }\r
        \r
-       boost::unique_future<safe_ptr<frame_producer>> foreground(int index)\r
+       safe_ptr<frame_producer> foreground(int index)\r
        {\r
-               return channel_.execution().begin_invoke([=]{return layers_[index].foreground();}, high_priority);\r
+               return channel_.execution().invoke([=]{return layers_[index].foreground();}, high_priority);\r
        }\r
        \r
-       boost::unique_future<safe_ptr<frame_producer>> background(int index)\r
+       safe_ptr<frame_producer> background(int index)\r
        {\r
-               return channel_.execution().begin_invoke([=]{return layers_[index].background();}, high_priority);\r
+               return channel_.execution().invoke([=]{return layers_[index].background();}, high_priority);\r
        }\r
 \r
        std::wstring print() const\r
@@ -229,7 +191,8 @@ void stage::clear(int index){impl_->clear(index);}
 void stage::clear(){impl_->clear();}\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, stage& other){impl_->swap_layer(index, other_index, other);}\r
-boost::unique_future<safe_ptr<frame_producer>> stage::foreground(size_t index) {return impl_->foreground(index);}\r
-boost::unique_future<safe_ptr<frame_producer>> stage::background(size_t index) {return impl_->background(index);}\r
+layer_status stage::get_status(int index){return impl_->get_status(index);}\r
+safe_ptr<frame_producer> stage::foreground(size_t index) {return impl_->foreground(index);}\r
+safe_ptr<frame_producer> stage::background(size_t index) {return impl_->background(index);}\r
 std::map<int, safe_ptr<basic_frame>> stage::execute(){return impl_->execute();}\r
 }}
\ No newline at end of file