]> git.sesse.net Git - casparcg/commitdiff
flash_producer:
authorronag <ronag@362d55ac-95cf-4e76-9f9a-cbaa9c17b72d>
Mon, 23 Jan 2012 19:45:47 +0000 (19:45 +0000)
committerronag <ronag@362d55ac-95cf-4e76-9f9a-cbaa9c17b72d>
Mon, 23 Jan 2012 19:45:47 +0000 (19:45 +0000)
 - Fixed potential data-race.
 - Fixed potential deadlock when running 25p template on 50p channel.
 - Reinitialize COM Apartement with flash-player.
 - Don't unitialize COM Aprtement if initialization failed.
 - Increased default buffering by 1 frame (2/3).
 - Remove uneccessarily long locking around last_frame, which could degrade performance when lagging.

git-svn-id: https://casparcg.svn.sourceforge.net/svnroot/casparcg/server/trunk@2153 362d55ac-95cf-4e76-9f9a-cbaa9c17b72d

modules/flash/producer/flash_producer.cpp

index dc7bc853b9972e42320a224ca86ada71a93f339d..cd659b47dc81402f03a86704be42c7344c5363b8 100644 (file)
@@ -144,33 +144,49 @@ template_host get_template_host(const core::video_format_desc& desc)
 \r
 class flash_renderer\r
 {      \r
-       const std::wstring filename_;\r
+       struct com_init\r
+       {\r
+               HRESULT result_;\r
 \r
-       const std::shared_ptr<core::frame_factory> frame_factory_;\r
+               com_init()\r
+                       : result_(CoInitialize(nullptr))\r
+               {\r
+                       if(FAILED(result_))\r
+                               BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("Failed to initialize com-context for flash-player"));\r
+               }\r
+\r
+               ~com_init()\r
+               {\r
+                       if(SUCCEEDED(result_))\r
+                               ::CoUninitialize();\r
+               }\r
+       } com_init_;\r
        \r
-       CComObject<caspar::flash::FlashAxContainer>* ax_;\r
-       safe_ptr<core::basic_frame> head_;\r
-       bitmap bmp_;\r
+       const safe_ptr<diagnostics::graph>                              graph_;\r
+       const size_t                                                                    width_;\r
+       const size_t                                                                    height_;\r
+       const std::wstring                                                              filename_;\r
+       const std::shared_ptr<core::frame_factory>              frame_factory_;\r
        \r
-       safe_ptr<diagnostics::graph> graph_;\r
-       boost::timer frame_timer_;\r
-       boost::timer tick_timer_;\r
-\r
-       high_prec_timer timer_;\r
+       CComObject<caspar::flash::FlashAxContainer>*    ax_;\r
+       safe_ptr<core::basic_frame>                                             head_;\r
+       bitmap                                                                                  bmp_;\r
+       \r
+       boost::timer                                                                    frame_timer_;\r
+       boost::timer                                                                    tick_timer_;\r
 \r
-       const size_t width_;\r
-       const size_t height_;\r
+       high_prec_timer                                                                 timer_;\r
        \r
 public:\r
        flash_renderer(const safe_ptr<diagnostics::graph>& graph, const std::shared_ptr<core::frame_factory>& frame_factory, const std::wstring& filename, int width, int height) \r
                : graph_(graph)\r
+               , width_(width)\r
+               , height_(height)\r
                , filename_(filename)\r
                , frame_factory_(frame_factory)\r
                , ax_(nullptr)\r
                , head_(core::basic_frame::empty())\r
                , bmp_(width, height)\r
-               , width_(width)\r
-               , height_(height)\r
        {               \r
                graph_->set_color("frame-time", diagnostics::color(0.1f, 1.0f, 0.1f));\r
                graph_->set_color("tick-time", diagnostics::color(0.0f, 0.6f, 0.9f));\r
@@ -199,6 +215,7 @@ public:
                        BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Failed to Set Scale Mode"));\r
                                                \r
                ax_->SetSize(width_, height_);          \r
+               render_frame(false);\r
        \r
                CASPAR_LOG(info) << print() << L" Initialized.";\r
        }\r
@@ -210,6 +227,8 @@ public:
                        ax_->DestroyAxControl();\r
                        ax_->Release();\r
                }\r
+               graph_->set_value("tick-time", 0.0f);\r
+               graph_->set_value("frame-time", 0.0f);\r
                CASPAR_LOG(info) << print() << L" Uninitialized.";\r
        }\r
        \r
@@ -226,7 +245,7 @@ public:
                return result;\r
        }\r
        \r
-       safe_ptr<core::basic_frame> render_frame(bool has_underflow)\r
+       safe_ptr<core::basic_frame> render_frame(bool sync)\r
        {\r
                float frame_time = 1.0f/ax_->GetFPS();\r
 \r
@@ -236,7 +255,7 @@ public:
                if(ax_->IsEmpty())\r
                        return core::basic_frame::empty();              \r
                \r
-               if(!has_underflow)                      \r
+               if(sync)                        \r
                        timer_.tick(frame_time); // This will block the thread.\r
                else\r
                        graph_->set_tag("skip-sync");\r
@@ -258,7 +277,7 @@ public:
                        frame->commit();\r
                        head_ = frame;\r
                }               \r
-                               \r
+                                                                               \r
                graph_->set_value("frame-time", static_cast<float>(frame_timer_.elapsed()/frame_time)*0.5f);\r
                return head_;\r
        }\r
@@ -286,20 +305,21 @@ struct flash_producer : public core::frame_producer
 {      \r
        const std::wstring                                                                                      filename_;      \r
        const safe_ptr<core::frame_factory>                                                     frame_factory_;\r
+       const int                                                                                                       width_;\r
+       const int                                                                                                       height_;\r
+       const int                                                                                                       buffer_size_;\r
 \r
        tbb::atomic<int>                                                                                        fps_;\r
+       tbb::atomic<bool>                                                                                       sync_;\r
 \r
-       safe_ptr<diagnostics::graph> graph_;\r
-\r
-       tbb::concurrent_bounded_queue<safe_ptr<core::basic_frame>>      frame_buffer_;\r
+       safe_ptr<diagnostics::graph>                                                            graph_;\r
 \r
+       std::queue<safe_ptr<core::basic_frame>>                                         frame_buffer_;\r
+       tbb::concurrent_bounded_queue<safe_ptr<core::basic_frame>>      output_buffer_;\r
+       \r
        mutable tbb::spin_mutex                                                                         last_frame_mutex_;\r
        safe_ptr<core::basic_frame>                                                                     last_frame_;\r
                \r
-       int                                                                                                                     width_;\r
-       int                                                                                                                     height_;\r
-\r
-       tbb::atomic<bool>                                                                                       is_running_;\r
        std::unique_ptr<flash_renderer>                                                         renderer_;\r
 \r
        executor                                                                                                        executor_;      \r
@@ -310,50 +330,40 @@ public:
                , last_frame_(core::basic_frame::empty())\r
                , width_(width > 0 ? width : frame_factory->get_video_format_desc().width)\r
                , height_(height > 0 ? height : frame_factory->get_video_format_desc().height)\r
+               , buffer_size_(env::properties().get(L"configuration.flash.buffer-depth", frame_factory_->get_video_format_desc().fps > 30.0 ? 3 : 2))\r
                , executor_(L"flash_producer")\r
        {       \r
+               sync_ = true;\r
                fps_ = 0;\r
-               is_running_ = true;\r
-\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
+               graph_->set_color("buffer-size", diagnostics::color(1.0f, 1.0f, 0.0f));\r
+               graph_->set_color("late-frame", diagnostics::color(0.6f, 0.3f, 0.9f));\r
                graph_->set_text(print());\r
                diagnostics::register_graph(graph_);\r
-               \r
-               auto flash_buffer = env::properties().get(L"configuration.flash.buffer-depth", frame_factory_->get_video_format_desc().fps > 30.0 ? 2 : 1);\r
-               frame_buffer_.set_capacity(flash_buffer);\r
-\r
-               executor_.begin_invoke([]\r
-               {\r
-                       ::CoInitialize(nullptr);\r
-               });                     \r
        }\r
 \r
        ~flash_producer()\r
        {\r
-               is_running_ = false;\r
-\r
-               safe_ptr<core::basic_frame> frame;\r
-               for(int n = 0; n < 3; ++n)\r
-                       frame_buffer_.try_pop(frame);\r
-\r
                executor_.invoke([this]\r
                {\r
                        renderer_.reset();\r
-                       ::CoUninitialize();\r
-               });\r
+               }, high_priority);\r
        }\r
 \r
        // frame_producer\r
                \r
        virtual safe_ptr<core::basic_frame> receive(int) override\r
-       {                               \r
-               graph_->set_value("output-buffer-count", static_cast<float>(frame_buffer_.size())/static_cast<float>(frame_buffer_.capacity()));\r
-\r
+       {                                       \r
                auto frame = core::basic_frame::late();\r
-               if(!frame_buffer_.try_pop(frame) && renderer_)\r
-                       graph_->set_tag("underflow");\r
+               \r
+               graph_->set_value("buffer-size", static_cast<float>(output_buffer_.size())/static_cast<float>(buffer_size_));\r
+               sync_ = output_buffer_.size() == buffer_size_;\r
 \r
+               if(output_buffer_.try_pop(frame))       \r
+                       next();\r
+               else\r
+                       graph_->set_tag("late-frame");\r
+                                               \r
                return frame;\r
        }\r
 \r
@@ -367,29 +377,18 @@ public:
        \r
        virtual boost::unique_future<std::wstring> call(const std::wstring& param) override\r
        {       \r
-               executor_.begin_invoke([=]()\r
-               {\r
-                       if(!is_running_)\r
-                               return;\r
-\r
-                       if(!renderer_)\r
+               return executor_.begin_invoke([this, param]() -> std::wstring\r
+               {                       \r
+                       try\r
                        {\r
-                               renderer_.reset(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(renderer_.get());\r
-                       }\r
-               });\r
+                               if(!renderer_)\r
+                               {\r
+                                       renderer_.reset(new flash_renderer(graph_, frame_factory_, filename_, width_, height_));\r
 \r
-               return executor_.begin_invoke([=]() -> std::wstring\r
-               {\r
-                       if(!is_running_)\r
-                               return L"";\r
-                       \r
-                       if(!renderer_)\r
-                               BOOST_THROW_EXCEPTION(invalid_operation() << msg_info("No renderer"));\r
+                                       while(output_buffer_.size() < buffer_size_)\r
+                                               output_buffer_.push(core::basic_frame::empty());\r
+                               }\r
 \r
-                       try\r
-                       {\r
                                return renderer_->call(param);  \r
 \r
                                //const auto& format_desc = frame_factory_->get_video_format_desc();\r
@@ -400,7 +399,6 @@ public:
                        {\r
                                CASPAR_LOG_CURRENT_EXCEPTION();\r
                                renderer_.reset(nullptr);\r
-                               frame_buffer_.push(core::basic_frame::empty());\r
                        }\r
 \r
                        return L"";\r
@@ -421,34 +419,24 @@ public:
 \r
        // flash_producer\r
        \r
-       safe_ptr<core::basic_frame> render_frame()\r
-       {\r
-               auto frame = renderer_->render_frame(frame_buffer_.size() < frame_buffer_.capacity());          \r
-               lock(last_frame_mutex_, [&]\r
-               {\r
-                       last_frame_ = make_safe<core::basic_frame>(frame);\r
-               });\r
-               return frame;\r
-       }\r
-\r
-       void render(const flash_renderer* renderer)\r
-       {               \r
-               executor_.begin_invoke([=]\r
+       void next()\r
+       {       \r
+               executor_.begin_invoke([this]\r
                {\r
-                       if(!is_running_ || renderer_.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
+                       if(!renderer_)\r
+                               frame_buffer_.push(core::basic_frame::empty());\r
 \r
+                       if(frame_buffer_.empty())\r
+                       {\r
+                               auto format_desc = frame_factory_->get_video_format_desc();\r
+                                       \r
                                if(abs(renderer_->fps()/2.0 - format_desc.fps) < 2.0) // flash == 2 * format -> interlace\r
                                {\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(renderer_->fps()- format_desc.fps/2.0) < 2.0) // format == 2 * flash -> duplicate\r
+                               else if(abs(renderer_->fps() - format_desc.fps/2.0) < 2.0) // format == 2 * flash -> duplicate\r
                                {\r
                                        auto frame = render_frame();\r
                                        frame_buffer_.push(frame);\r
@@ -459,27 +447,28 @@ public:
                                        auto frame = render_frame();\r
                                        frame_buffer_.push(frame);\r
                                }\r
-\r
-                               if(renderer_->is_empty())\r
-                               {\r
-                                       renderer_.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
+                                               \r
                                fps_.fetch_and_store(static_cast<int>(renderer_->fps()*100.0));                         \r
                                graph_->set_text(print());\r
-\r
-                               render(renderer);\r
-                       }\r
-                       catch(...)\r
-                       {\r
-                               CASPAR_LOG_CURRENT_EXCEPTION();\r
-                               renderer_.reset(nullptr);\r
-                               frame_buffer_.push(core::basic_frame::empty());\r
+                       \r
+                               if(renderer_->is_empty())                       \r
+                                       renderer_.reset();\r
                        }\r
+\r
+                       output_buffer_.push(std::move(frame_buffer_.front()));\r
+                       frame_buffer_.pop();\r
                });\r
        }\r
+\r
+       safe_ptr<core::basic_frame> render_frame()\r
+       {       \r
+               auto frame = renderer_->render_frame(sync_);\r
+               lock(last_frame_mutex_, [&]\r
+               {\r
+                       last_frame_ = frame;\r
+               });\r
+               return frame;\r
+       }\r
 };\r
 \r
 safe_ptr<core::frame_producer> create_producer(const safe_ptr<core::frame_factory>& frame_factory, const std::vector<std::wstring>& params)\r
@@ -491,8 +480,8 @@ safe_ptr<core::frame_producer> create_producer(const safe_ptr<core::frame_factor
        if(!boost::filesystem::exists(filename))\r
                BOOST_THROW_EXCEPTION(file_not_found() << boost::errinfo_file_name(narrow(filename)));  \r
 \r
-       return create_producer_print_proxy(\r
-                  create_producer_destroy_proxy(\r
+       return create_producer_destroy_proxy(\r
+                  create_producer_print_proxy(\r
                        make_safe<flash_producer>(frame_factory, filename, template_host.width, template_host.height)));\r
 }\r
 \r