]> git.sesse.net Git - casparcg/blobdiff - core/producer/frame_producer_device.cpp
2.0.0.2: bluefish_consumer: Added proper prerolling.
[casparcg] / core / producer / frame_producer_device.cpp
index 2498370da820eeb3a74768278c317e80067896cf..4d351ce19d6a2c400576fed5bffe57b4e1c5bc9c 100644 (file)
 #include "../StdAfx.h"\r
 \r
 #include "frame_producer_device.h"\r
-#include "destroy_producer_proxy.h"\r
+\r
+#include "../video_channel_context.h"\r
+\r
+#include "layer.h"\r
 \r
 #include <core/producer/frame/basic_frame.h>\r
 #include <core/producer/frame/frame_factory.h>\r
 \r
 #include <common/diagnostics/graph.h>\r
-\r
-#include "layer.h"\r
-\r
 #include <common/concurrency/executor.h>\r
 \r
-#include <boost/range/algorithm_ext/erase.hpp>\r
-#include <boost/lexical_cast.hpp>\r
 #include <boost/timer.hpp>\r
 \r
 #include <tbb/parallel_for.h>\r
-#include <tbb/mutex.h>\r
 \r
-#include <array>\r
-#include <memory>\r
 #include <map>\r
 \r
 namespace caspar { namespace core {\r
+               \r
+void destroy_producer(safe_ptr<frame_producer>& producer)\r
+{\r
+       if(!producer.unique())\r
+               CASPAR_LOG(warning) << 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()                                                                                                               {return core::receive(producer_);}\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
+};\r
 \r
 struct frame_producer_device::implementation : boost::noncopyable\r
 {              \r
-       std::map<int, layer> layers_;           \r
+       std::map<int, layer>                                            layers_;                \r
+       typedef std::map<int, layer>::value_type        layer_t;\r
        \r
-       const video_format_desc format_desc_;\r
+       safe_ptr<diagnostics::graph>                            diag_;\r
+       boost::timer                                                            frame_timer_;\r
+       boost::timer                                                            tick_timer_;\r
+       boost::timer                                                            output_timer_;\r
        \r
-       safe_ptr<diagnostics::graph> diag_;\r
-\r
-       const std::function<void(const std::map<int, safe_ptr<basic_frame>>&)> output_;\r
-\r
-       boost::timer frame_timer_;\r
-       boost::timer tick_timer_;\r
-       boost::timer output_timer_;\r
-       \r
-       mutable executor executor_;\r
+       video_channel_context&                                          channel_;\r
 public:\r
-       implementation(const video_format_desc& format_desc, const std::function<void(const std::map<int, safe_ptr<basic_frame>>&)>& output)  \r
-               : format_desc_(format_desc)\r
-               , diag_(diagnostics::create_graph(std::string("frame_producer_device")))\r
-               , executor_(L"frame_producer_device")\r
-               , output_(output)\r
+       implementation(video_channel_context& video_channel)  \r
+               : diag_(diagnostics::create_graph(std::string("frame_producer_device")))\r
+               , channel_(video_channel)\r
        {\r
                diag_->add_guide("frame-time", 0.5f);   \r
                diag_->set_color("frame-time", diagnostics::color(1.0f, 0.0f, 0.0f));\r
-               diag_->set_color("tick-time", diagnostics::color(0.1f, 0.7f, 0.8f));\r
-               diag_->set_color("output-time", diagnostics::color(0.5f, 1.0f, 0.2f));\r
-               executor_.begin_invoke([]\r
-               {\r
-                       SetThreadPriority(GetCurrentThread(), ABOVE_NORMAL_PRIORITY_CLASS);\r
-               });\r
-               executor_.begin_invoke([=]{tick();});           \r
+               diag_->set_color("tick-time", diagnostics::color(0.1f, 0.7f, 0.8f));    \r
        }\r
-                       \r
-       void tick()\r
-       {                               \r
-               auto frame = draw();\r
-               output_timer_.restart();\r
-               output_(frame);\r
-               diag_->update_value("output-time", static_cast<float>(output_timer_.elapsed()*format_desc_.fps*0.5));\r
-\r
-               executor_.begin_invoke([=]{tick();});\r
-       }\r
-                       \r
-       std::map<int, safe_ptr<basic_frame>> draw()\r
+                                               \r
+       std::map<int, safe_ptr<basic_frame>> operator()()\r
        {       \r
                frame_timer_.restart();\r
-\r
+               \r
                std::map<int, safe_ptr<basic_frame>> frames;\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(), [&](decltype(*layers_.begin())& pair)\r
+               // Allocate placeholders.\r
+               std::for_each(layers_.begin(), layers_.end(), [&](layer_t& layer)\r
                {\r
-                       auto frame = pair.second.receive();\r
-                       if(is_concrete_frame(frame))\r
-                               frames[pair.first] = frame;             \r
+                       frames[layer.first] = basic_frame::empty();\r
+               });\r
+\r
+               // Render layers\r
+               tbb::parallel_for_each(layers_.begin(), layers_.end(), [&](layer_t& layer)\r
+               {\r
+                       frames[layer.first] = layer.second.receive();\r
                });\r
                \r
-               diag_->update_value("frame-time", static_cast<float>(frame_timer_.elapsed()*format_desc_.fps*0.5));\r
+               diag_->update_value("frame-time", frame_timer_.elapsed()*channel_.format_desc.fps*0.5);\r
 \r
-               diag_->update_value("tick-time", static_cast<float>(tick_timer_.elapsed()*format_desc_.fps*0.5));\r
+               diag_->update_value("tick-time", tick_timer_.elapsed()*channel_.format_desc.fps*0.5);\r
                tick_timer_.restart();\r
 \r
                return frames;\r
@@ -113,37 +121,37 @@ public:
 \r
        void load(int index, const safe_ptr<frame_producer>& producer, bool preview)\r
        {\r
-               executor_.invoke([&]{layers_[index].load(make_safe<destroy_producer_proxy>(producer), preview);});\r
+               channel_.execution.invoke([&]{layers_[index].load(make_safe<destroy_producer_proxy>(channel_.destruction, producer), preview);});\r
        }\r
 \r
        void pause(int index)\r
        {               \r
-               executor_.invoke([&]{layers_[index].pause();});\r
+               channel_.execution.invoke([&]{layers_[index].pause();});\r
        }\r
 \r
        void play(int index)\r
        {               \r
-               executor_.invoke([&]{layers_[index].play();});\r
+               channel_.execution.invoke([&]{layers_[index].play();});\r
        }\r
 \r
        void stop(int index)\r
        {               \r
-               executor_.invoke([&]{layers_[index].stop();});\r
+               channel_.execution.invoke([&]{layers_[index].stop();});\r
        }\r
 \r
        void clear(int index)\r
        {\r
-               executor_.invoke([&]{layers_.erase(index);});\r
+               channel_.execution.invoke([&]{layers_.erase(index);});\r
        }\r
                \r
        void clear()\r
        {\r
-               executor_.invoke([&]{layers_.clear();});\r
+               channel_.execution.invoke([&]{layers_.clear();});\r
        }       \r
        \r
        void swap_layer(int index, size_t other_index)\r
        {\r
-               executor_.invoke([&]{layers_[index].swap(layers_[other_index]);});\r
+               channel_.execution.invoke([&]{layers_[index].swap(layers_[other_index]);});\r
        }\r
 \r
        void swap_layer(int index, size_t other_index, frame_producer_device& other)\r
@@ -152,12 +160,12 @@ public:
                        swap_layer(index, other_index);\r
                else\r
                {\r
-                       if(format_desc_ != other.impl_->format_desc_)\r
-                               BOOST_THROW_EXCEPTION(invalid_operation() << msg_info("Cannot swap between channels with different formats."));\r
+                       if(channel_.format_desc != other.impl_->channel_.format_desc)\r
+                               BOOST_THROW_EXCEPTION(not_supported() << msg_info("Cannot swap between channels with different formats."));\r
 \r
                        auto func = [&]{layers_[index].swap(other.impl_->layers_[other_index]);};\r
                \r
-                       executor_.invoke([&]{other.impl_->executor_.invoke(func);});\r
+                       channel_.execution.invoke([&]{other.impl_->channel_.execution.invoke(func);});\r
                }\r
        }\r
 \r
@@ -166,8 +174,8 @@ public:
                if(other.impl_.get() == this)\r
                        return;\r
 \r
-               if(format_desc_ != other.impl_->format_desc_)\r
-                       BOOST_THROW_EXCEPTION(invalid_operation() << msg_info("Cannot swap between channels with different formats."));\r
+               if(channel_.format_desc != other.impl_->channel_.format_desc)\r
+                       BOOST_THROW_EXCEPTION(not_supported() << msg_info("Cannot swap between channels with different formats."));\r
 \r
                auto func = [&]\r
                {\r
@@ -178,28 +186,28 @@ public:
 \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
+\r
                        std::for_each(indices.begin(), indices.end(), [&](int index)\r
                        {\r
                                layers_[index].swap(other.impl_->layers_[index]);\r
                        });                                     \r
                };\r
                \r
-               executor_.invoke([&]{other.impl_->executor_.invoke(func);});\r
+               channel_.execution.invoke([&]{other.impl_->channel_.execution.invoke(func);});\r
        }\r
        \r
        boost::unique_future<safe_ptr<frame_producer>> foreground(int index)\r
        {\r
-               return executor_.begin_invoke([=]{return layers_[index].foreground();});\r
+               return channel_.execution.begin_invoke([=]{return layers_[index].foreground();});\r
        }\r
        \r
        boost::unique_future<safe_ptr<frame_producer>> background(int index)\r
        {\r
-               return executor_.begin_invoke([=]{return layers_[index].background();});\r
+               return channel_.execution.begin_invoke([=]{return layers_[index].background();});\r
        }\r
 };\r
 \r
-frame_producer_device::frame_producer_device(const video_format_desc& format_desc, const std::function<void(const std::map<int, safe_ptr<basic_frame>>&)>& output) : impl_(new implementation(format_desc, output)){}\r
-frame_producer_device::frame_producer_device(frame_producer_device&& other) : impl_(std::move(other.impl_)){}\r
+frame_producer_device::frame_producer_device(video_channel_context& video_channel) : impl_(new implementation(video_channel)){}\r
 void frame_producer_device::swap(frame_producer_device& other){impl_->swap(other);}\r
 void frame_producer_device::load(int index, const safe_ptr<frame_producer>& producer, bool preview){impl_->load(index, producer, preview);}\r
 void frame_producer_device::pause(int index){impl_->pause(index);}\r
@@ -211,4 +219,5 @@ void frame_producer_device::swap_layer(int index, size_t other_index){impl_->swa
 void frame_producer_device::swap_layer(int index, size_t other_index, frame_producer_device& other){impl_->swap_layer(index, other_index, other);}\r
 boost::unique_future<safe_ptr<frame_producer>> frame_producer_device::foreground(size_t index) {return impl_->foreground(index);}\r
 boost::unique_future<safe_ptr<frame_producer>> frame_producer_device::background(size_t index) {return impl_->background(index);}\r
+std::map<int, safe_ptr<basic_frame>> frame_producer_device::operator()(){return (*impl_)();}\r
 }}
\ No newline at end of file