]> git.sesse.net Git - casparcg/blobdiff - modules/flash/producer/flash_producer.cpp
git-svn-id: https://casparcg.svn.sourceforge.net/svnroot/casparcg/server/branches...
[casparcg] / modules / flash / producer / flash_producer.cpp
index 2afaf13c5627910faf41d18c9a058f42b356a9d8..e9f81c795ff18a2ee0fa3a0bfe6e8ab739fd126a 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
@@ -83,6 +85,45 @@ private:
        std::shared_ptr<void> bmp_;\r
 };\r
 \r
+struct template_host\r
+{\r
+       std::string  video_mode;\r
+       std::string  filename;\r
+       size_t           width;\r
+       size_t           height;\r
+};\r
+\r
+template_host get_template_host(const core::video_format_desc& desc)\r
+{\r
+       std::vector<template_host> template_hosts;\r
+       BOOST_FOREACH(auto& xml_mapping, env::properties().get_child("configuration.producers.template-hosts"))\r
+       {\r
+               try\r
+               {\r
+                       template_host template_host;\r
+                       template_host.video_mode                = xml_mapping.second.get("video-mode", narrow(desc.name));\r
+                       template_host.filename                  = xml_mapping.second.get("filename", "cg.fth");\r
+                       template_host.width                             = xml_mapping.second.get("width", desc.width);\r
+                       template_host.height                    = xml_mapping.second.get("height", desc.height);\r
+                       template_hosts.push_back(template_host);\r
+               }\r
+               catch(...){}\r
+       }\r
+\r
+       auto template_host_it = boost::find_if(template_hosts, [&](template_host template_host){return template_host.video_mode == narrow(desc.name);});\r
+       if(template_host_it == template_hosts.end())\r
+               template_host_it = boost::find_if(template_hosts, [&](template_host template_host){return template_host.video_mode == "";});\r
+\r
+       if(template_host_it != template_hosts.end())\r
+               return *template_host_it;\r
+       \r
+       template_host template_host;\r
+       template_host.filename = "cg.fth";\r
+       template_host.width = desc.width;\r
+       template_host.height = desc.height;\r
+       return template_host;\r
+}\r
+\r
 class flash_renderer\r
 {      \r
        const std::wstring filename_;\r
@@ -114,9 +155,9 @@ public:
                , height_(height)\r
        {\r
                graph_->add_guide("frame-time", 0.5f);\r
-               graph_->set_color("frame-time", diagnostics::color(0.8f, 0.3f, 0.4f));  \r
+               graph_->set_color("frame-time", diagnostics::color(0.1f, 1.0f, 0.1f));\r
                graph_->add_guide("tick-time", 0.5);\r
-               graph_->set_color("tick-time", diagnostics::color(0.1f, 0.7f, 0.8f));\r
+               graph_->set_color("tick-time", diagnostics::color(0.0f, 0.6f, 0.9f));           \r
                graph_->set_color("param", diagnostics::color(1.0f, 0.5f, 0.0f));       \r
                graph_->set_color("underflow", diagnostics::color(0.6f, 0.3f, 0.9f));                   \r
                \r
@@ -144,7 +185,7 @@ public:
                ax_->SetSize(width_, height_);          \r
        \r
                CASPAR_LOG(info) << print() << L" Thread started.";\r
-               CASPAR_LOG(info) << print() << L" Successfully initialized to, width: " << width_ << L" height: " << height_ << L".";\r
+               CASPAR_LOG(info) << print() << L" Successfully initialized with template-host: " << filename << L" width: " << width_ << L" height: " << height_ << L".";\r
        }\r
 \r
        ~flash_renderer()\r
@@ -160,19 +201,12 @@ public:
        void param(const std::wstring& param)\r
        {               \r
                if(!ax_->FlashCall(param))\r
-                       CASPAR_LOG(warning) << print() << " Flash function call failed. Param: " << param << ".";\r
+                       BOOST_THROW_EXCEPTION(invalid_operation() << msg_info("Flash function call failed.") << arg_name_info("param") << arg_value_info(narrow(param)));\r
                graph_->add_tag("param");\r
        }\r
        \r
        safe_ptr<core::basic_frame> render_frame(bool has_underflow)\r
        {\r
-               //if(format_desc_ != frame_factory_->get_video_format_desc())\r
-               //{\r
-               //      format_desc_ = frame_factory_->get_video_format_desc();\r
-               //      bmp_ = bitmap(format_desc_.width, format_desc_.height);\r
-               //      ax_->SetFormat(format_desc_);\r
-               //}\r
-\r
                float frame_time = 1.0f/ax_->GetFPS();\r
 \r
                graph_->update_value("tick-time", static_cast<float>(tick_timer_.elapsed()/frame_time)*0.5f);\r
@@ -230,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
@@ -247,11 +284,10 @@ public:
                fps_ = 0;\r
 \r
                graph_ = diagnostics::create_graph([this]{return print();});\r
-               graph_->set_color("output-buffer", diagnostics::color(0.0f, 1.0f, 0.0f));\r
-               \r
-               frame_buffer_.set_capacity(1);\r
-\r
-               initialize();                           \r
+               graph_->set_color("buffer-count", diagnostics::color(0.4f, 0.8f, 0.8f));\r
+                               \r
+               frame_buffer_.set_capacity(1);          \r
+               initialize();\r
        }\r
 \r
        ~flash_producer()\r
@@ -259,11 +295,25 @@ public:
                frame_buffer_.clear();\r
        }\r
 \r
+       void initialize()\r
+       {               \r
+               context_.reset(nullptr);\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
        // frame_producer\r
                \r
        virtual safe_ptr<core::basic_frame> receive(int)\r
-       {                               \r
-               graph_->set_value("output-buffer", static_cast<float>(frame_buffer_.size())/static_cast<float>(frame_buffer_.capacity()));\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("buffer-count", static_cast<float>(frame_buffer_.size())/static_cast<float>(frame_buffer_.capacity()));\r
 \r
                auto frame = core::basic_frame::late();\r
                if(frame_buffer_.try_pop(frame))\r
@@ -279,50 +329,56 @@ 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
-\r
-                               const auto& format_desc = frame_factory_->get_video_format_desc();\r
-                               if(abs(context_->fps() - format_desc.fps) > 0.01 && abs(context_->fps()/2.0 - format_desc.fps) > 0.01)\r
-                                       CASPAR_LOG(warning) << print() << " Invalid frame-rate: " << context_->fps() << L". Should be either " << format_desc.fps << L" or " << format_desc.fps*2.0 << L".";\r
                        }\r
                        catch(...)\r
                        {\r
-                               CASPAR_LOG_CURRENT_EXCEPTION();\r
-                               context_.reset(nullptr);\r
-                               frame_buffer_.push(core::basic_frame::empty());\r
+                               try\r
+                               {\r
+                                       CASPAR_LOG_CURRENT_EXCEPTION();\r
+                                       CASPAR_LOG(error) << print() << L" param failed. Trying to re-initialize flash and re-execute param.";\r
+\r
+                                       initialize();                           \r
+                                       context_->param(param); \r
+                               }\r
+                               catch(...)\r
+                               {\r
+                                       CASPAR_LOG_CURRENT_EXCEPTION();\r
+\r
+                                       context_.reset(nullptr);\r
+\r
+                                       tbb::spin_mutex::scoped_lock lock(exception_mutex_);\r
+                                       exception_ = std::current_exception();\r
+                               }\r
                        }\r
+\r
+                       const auto& format_desc = frame_factory_->get_video_format_desc();\r
+                       if(abs(context_->fps() - format_desc.fps) > 2.0 && abs(context_->fps()/2.0 - format_desc.fps) > 2.0)\r
+                               CASPAR_LOG(warning) << print() << " Invalid frame-rate: " << context_->fps() << L". Should be either " << format_desc.fps << L" or " << format_desc.fps*2.0 << L".";\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
@@ -345,16 +401,18 @@ public:
                                        frame_buffer_.push(frame);\r
                                }\r
 \r
-                               graph_->set_value("output-buffer", static_cast<float>(frame_buffer_.size())/static_cast<float>(frame_buffer_.capacity()));      \r
+                               graph_->set_value("buffer-count", 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
@@ -362,18 +420,9 @@ public:
 \r
 safe_ptr<core::frame_producer> create_flash_producer(const safe_ptr<core::frame_factory>& frame_factory, const std::vector<std::wstring>& params)\r
 {\r
-       std::wstring filename = env::template_folder() + L"\\" + params[0];\r
-\r
-       size_t width = frame_factory->get_video_format_desc().width;\r
-       size_t height = frame_factory->get_video_format_desc().height;\r
-\r
-       if(params.size() >= 3)\r
-       {\r
-               width = boost::lexical_cast<size_t>(params[1]);\r
-               height = boost::lexical_cast<size_t>(params[2]);\r
-       }\r
+       auto template_host = get_template_host(frame_factory->get_video_format_desc());\r
        \r
-       return make_safe<flash_producer>(frame_factory, filename, width, height);\r
+       return make_safe<flash_producer>(frame_factory, env::template_folder() + L"\\" + widen(template_host.filename), template_host.width, template_host.height);\r
 }\r
 \r
 std::wstring find_flash_template(const std::wstring& template_name)\r