]> git.sesse.net Git - casparcg/blobdiff - modules/flash/producer/flash_producer.cpp
2.0.0: flash_producer: Use square-pixel dimensions.
[casparcg] / modules / flash / producer / flash_producer.cpp
index cfc4ca7394e6a38cab75c768679a341e67110a41..67a556423fc72cae525bf742bed4f0ed157facf0 100644 (file)
 #include <boost/filesystem.hpp>\r
 #include <boost/thread.hpp>\r
 #include <boost/timer.hpp>\r
+#include <boost/algorithm/string.hpp>\r
 \r
 #include <functional>\r
 \r
-namespace caspar {\r
+#include <tbb/spin_mutex.h>\r
+\r
+namespace caspar { namespace flash {\r
                \r
 class bitmap\r
 {\r
@@ -85,7 +88,7 @@ private:
 \r
 struct template_host\r
 {\r
-       std::string  video_mode;\r
+       std::string  field_mode;\r
        std::string  filename;\r
        size_t           width;\r
        size_t           height;\r
@@ -93,32 +96,48 @@ struct template_host
 \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
+       try\r
        {\r
-               try\r
+               std::vector<template_host> template_hosts;\r
+               BOOST_FOREACH(auto& xml_mapping, env::properties().get_child("configuration.producers.template-hosts"))\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
+                       try\r
+                       {\r
+                               template_host template_host;\r
+                               template_host.field_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
-               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
+               auto template_host_it = boost::find_if(template_hosts, [&](template_host template_host){return template_host.field_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.field_mode == "";});\r
 \r
-       if(template_host_it != template_hosts.end())\r
-               return *template_host_it;\r
-       \r
+               if(template_host_it != template_hosts.end())\r
+                       return *template_host_it;\r
+       }\r
+       catch(...)\r
+       {\r
+       }\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
+\r
+       for(auto it = boost::filesystem2::wdirectory_iterator(env::template_folder()); it != boost::filesystem2::wdirectory_iterator(); ++it)\r
+       {\r
+               if(boost::iequals(it->path().extension(), L"." + desc.name))\r
+               {\r
+                       template_host.filename = narrow(it->filename());\r
+                       break;\r
+               }\r
+       }\r
+\r
+       template_host.width = desc.square_width;\r
+       template_host.height = desc.square_height;\r
        return template_host;\r
 }\r
 \r
@@ -157,7 +176,7 @@ public:
                graph_->add_guide("tick-time", 0.5);\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
+               graph_->set_color("skip-sync", diagnostics::color(0.8f, 0.3f, 0.2f));                   \r
                \r
                if(FAILED(CComObject<caspar::flash::FlashAxContainer>::CreateInstance(&ax_)))\r
                        BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Failed to create FlashAxContainer"));\r
@@ -199,7 +218,7 @@ public:
        void param(const std::wstring& param)\r
        {               \r
                if(!ax_->FlashCall(param))\r
-                       BOOST_THROW_EXCEPTION(invalid_operation() << msg_info("Flash function call failed.") << arg_name_info("param") << arg_value_info(narrow(param)));\r
+                       CASPAR_LOG(warning) << print() << L" Flash call failed:" << param;//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
@@ -216,7 +235,7 @@ public:
                if(!has_underflow)                      \r
                        timer_.tick(frame_time); // This will block the thread.\r
                else\r
-                       graph_->add_tag("underflow");\r
+                       graph_->add_tag("skip-sync");\r
                        \r
                frame_timer_.restart();\r
 \r
@@ -236,6 +255,11 @@ public:
                return head_;\r
        }\r
 \r
+       bool is_empty() const\r
+       {\r
+               return ax_->IsEmpty();\r
+       }\r
+\r
        double fps() const\r
        {\r
                return ax_->GetFPS();   \r
@@ -258,6 +282,7 @@ struct flash_producer : public core::frame_producer
 \r
        tbb::concurrent_bounded_queue<safe_ptr<core::basic_frame>> frame_buffer_;\r
 \r
+       mutable tbb::spin_mutex         last_frame_mutex_;\r
        safe_ptr<core::basic_frame>     last_frame_;\r
                                \r
        com_context<flash_renderer> context_;   \r
@@ -275,11 +300,12 @@ public:
        {       \r
                if(!boost::filesystem::exists(filename))\r
                        BOOST_THROW_EXCEPTION(file_not_found() << boost::errinfo_file_name(narrow(filename)));  \r
-                \r
+\r
                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
+               graph_ = diagnostics::create_graph(narrow(print()));\r
+               graph_->set_color("output-buffer-count", diagnostics::color(1.0f, 1.0f, 0.0f));          \r
+               graph_->set_color("underflow", diagnostics::color(0.6f, 0.3f, 0.9f));   \r
                \r
                frame_buffer_.set_capacity(frame_factory_->get_video_format_desc().fps > 30.0 ? 2 : 1);\r
 \r
@@ -295,17 +321,18 @@ public:
                \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
+               graph_->set_value("output-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
-                       last_frame_ = frame;\r
+               if(!frame_buffer_.try_pop(frame) && context_)\r
+                       graph_->add_tag("underflow");\r
 \r
                return frame;\r
        }\r
 \r
        virtual safe_ptr<core::basic_frame> last_frame() const\r
        {\r
+               tbb::spin_mutex::scoped_lock lock(last_frame_mutex_);\r
                return last_frame_;\r
        }               \r
        \r
@@ -347,6 +374,14 @@ public:
                render(context_.get());\r
        }\r
 \r
+       safe_ptr<core::basic_frame> render_frame()\r
+       {\r
+               auto frame = context_->render_frame(frame_buffer_.size() < frame_buffer_.capacity());           \r
+               tbb::spin_mutex::scoped_lock lock(last_frame_mutex_);\r
+               last_frame_ = make_safe<core::basic_frame>(frame);\r
+               return frame;\r
+       }\r
+\r
        void render(const flash_renderer* renderer)\r
        {               \r
                context_.begin_invoke([=]\r
@@ -360,24 +395,31 @@ public:
 \r
                                if(abs(context_->fps()/2.0 - format_desc.fps) < 2.0) // flash == 2 * format -> interlace\r
                                {\r
-                                       auto frame1 = context_->render_frame(frame_buffer_.size() < frame_buffer_.capacity());\r
-                                       auto frame2 = context_->render_frame(frame_buffer_.size() < frame_buffer_.capacity());\r
-                                       frame_buffer_.push(core::basic_frame::interlace(frame1, frame2, format_desc.mode));\r
+                                       auto frame1 = render_frame();\r
+                                       auto frame2 = render_frame();\r
+                                       frame_buffer_.push(core::basic_frame::interlace(frame1, frame2, format_desc.field_mode));\r
                                }\r
                                else if(abs(context_->fps()- format_desc.fps/2.0) < 2.0) // format == 2 * flash -> duplicate\r
                                {\r
-                                       auto frame = context_->render_frame(frame_buffer_.size() < frame_buffer_.capacity());\r
+                                       auto frame = render_frame();\r
                                        frame_buffer_.push(frame);\r
                                        frame_buffer_.push(frame);\r
                                }\r
                                else //if(abs(renderer_->fps() - format_desc_.fps) < 0.1) // format == flash -> simple\r
                                {\r
-                                       auto frame = context_->render_frame(frame_buffer_.size() < frame_buffer_.capacity());\r
+                                       auto frame = render_frame();\r
                                        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
-                               fps_.fetch_and_store(static_cast<int>(context_->fps()*100.0));\r
+                               if(context_->is_empty())\r
+                               {\r
+                                       context_.reset(nullptr);\r
+                                       return;\r
+                               }\r
+\r
+                               graph_->set_value("output-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
+                               graph_->update_text(narrow(print()));\r
 \r
                                render(renderer);\r
                        }\r
@@ -391,14 +433,14 @@ public:
        }\r
 };\r
 \r
-safe_ptr<core::frame_producer> create_flash_producer(const safe_ptr<core::frame_factory>& frame_factory, const std::vector<std::wstring>& params)\r
+safe_ptr<core::frame_producer> create_producer(const safe_ptr<core::frame_factory>& frame_factory, const std::vector<std::wstring>& params)\r
 {\r
        auto template_host = get_template_host(frame_factory->get_video_format_desc());\r
        \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
+std::wstring find_template(const std::wstring& template_name)\r
 {\r
        if(boost::filesystem::exists(template_name + L".ft")) \r
                return template_name + L".ft";\r
@@ -409,4 +451,4 @@ std::wstring find_flash_template(const std::wstring& template_name)
        return L"";\r
 }\r
 \r
-}
\ No newline at end of file
+}}
\ No newline at end of file