]> git.sesse.net Git - casparcg/blobdiff - modules/flash/producer/flash_producer.cpp
2.0. flash_producer: Improved error handling.
[casparcg] / modules / flash / producer / flash_producer.cpp
index aa21c1232e91d194277032cce17109baf0417e82..f24d6ef2fb03b1de414963cdd799f8ad4bab6961 100644 (file)
@@ -45,6 +45,8 @@
 #include <boost/thread.hpp>\r
 #include <boost/timer.hpp>\r
 \r
+#include <tbb/spin_mutex.h>\r
+\r
 #include <functional>\r
 \r
 namespace caspar {\r
@@ -262,6 +264,9 @@ struct flash_producer : public core::frame_producer
                                \r
        com_context<flash_renderer> context_;   \r
 \r
+       tbb::spin_mutex    exception_mutex_;\r
+       std::exception_ptr exception_;\r
+\r
        int width_;\r
        int height_;\r
 public:\r
@@ -282,8 +287,10 @@ public:
                graph_->set_color("output-buffer", diagnostics::color(0.0f, 1.0f, 0.0f));\r
                \r
                frame_buffer_.set_capacity(1);\r
-\r
-               initialize();                           \r
+               \r
+               context_.reset([&]{return new flash_renderer(safe_ptr<diagnostics::graph>(graph_), frame_factory_, filename_, width_, height_);});\r
+               while(frame_buffer_.try_push(core::basic_frame::empty())){}             \r
+               render();                       \r
        }\r
 \r
        ~flash_producer()\r
@@ -294,7 +301,13 @@ public:
        // frame_producer\r
                \r
        virtual safe_ptr<core::basic_frame> receive(int)\r
-       {                               \r
+       {               \r
+               {                       \r
+                       tbb::spin_mutex::scoped_lock lock(exception_mutex_);\r
+                       if(exception_ != nullptr)\r
+                               std::rethrow_exception(exception_);\r
+               }\r
+\r
                graph_->set_value("output-buffer", static_cast<float>(frame_buffer_.size())/static_cast<float>(frame_buffer_.capacity()));\r
 \r
                auto frame = core::basic_frame::late();\r
@@ -311,11 +324,14 @@ public:
        \r
        virtual void param(const std::wstring& param) \r
        {       \r
+               {                       \r
+                       tbb::spin_mutex::scoped_lock lock(exception_mutex_);\r
+                       if(exception_ != nullptr)\r
+                               std::rethrow_exception(exception_);\r
+               }\r
+\r
                context_.begin_invoke([=]\r
                {\r
-                       if(!context_)\r
-                               initialize();\r
-\r
                        try\r
                        {\r
                                context_->param(param); \r
@@ -328,33 +344,24 @@ public:
                        {\r
                                CASPAR_LOG_CURRENT_EXCEPTION();\r
                                context_.reset(nullptr);\r
-                               frame_buffer_.push(core::basic_frame::empty());\r
+\r
+                               tbb::spin_mutex::scoped_lock lock(exception_mutex_);\r
+                               exception_ = std::current_exception();\r
                        }\r
                });\r
        }\r
                \r
        virtual std::wstring print() const\r
        { \r
-               return L"flash[" + boost::filesystem::wpath(filename_).filename() + L", " + \r
-                                       boost::lexical_cast<std::wstring>(fps_) + L"]";         \r
+               return L"flash[" + boost::filesystem::wpath(filename_).filename() + L", " + boost::lexical_cast<std::wstring>(fps_) + L"]";             \r
        }       \r
 \r
        // flash_producer\r
 \r
-       void initialize()\r
-       {\r
-               context_.reset([&]{return new flash_renderer(safe_ptr<diagnostics::graph>(graph_), frame_factory_, filename_, width_, height_);});\r
-               while(frame_buffer_.try_push(core::basic_frame::empty())){}             \r
-               render(context_.get());\r
-       }\r
-\r
-       void render(const flash_renderer* renderer)\r
-       {               \r
+       void render()\r
+       {       \r
                context_.begin_invoke([=]\r
                {\r
-                       if(context_.get() != renderer) // Since initialize will start a new recursive call make sure the recursive calls are only for a specific instance.\r
-                               return;\r
-\r
                        try\r
                        {               \r
                                const auto& format_desc = frame_factory_->get_video_format_desc();\r
@@ -380,13 +387,15 @@ public:
                                graph_->set_value("output-buffer", static_cast<float>(frame_buffer_.size())/static_cast<float>(frame_buffer_.capacity()));      \r
                                fps_.fetch_and_store(static_cast<int>(context_->fps()*100.0));\r
 \r
-                               render(renderer);\r
+                               render();\r
                        }\r
                        catch(...)\r
                        {\r
                                CASPAR_LOG_CURRENT_EXCEPTION();\r
                                context_.reset(nullptr);\r
-                               frame_buffer_.push(core::basic_frame::empty());\r
+                               \r
+                               tbb::spin_mutex::scoped_lock lock(exception_mutex_);\r
+                               exception_ = std::current_exception();\r
                        }\r
                });\r
        }\r