]> git.sesse.net Git - casparcg/commitdiff
2.0.0.2:
authorronag <ronag@362d55ac-95cf-4e76-9f9a-cbaa9c17b72d>
Sun, 8 May 2011 10:06:23 +0000 (10:06 +0000)
committerronag <ronag@362d55ac-95cf-4e76-9f9a-cbaa9c17b72d>
Sun, 8 May 2011 10:06:23 +0000 (10:06 +0000)
 - decklink_consumer: Refactored.
 - flash_producer: Refactored. Output empty-frames after error.

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

common/common.vcxproj
common/common.vcxproj.filters
common/concurrency/com_context.h [new file with mode: 0644]
modules/decklink/consumer/decklink_consumer.cpp
modules/decklink/consumer/decklink_consumer.h
modules/decklink/decklink.cpp
modules/flash/producer/flash_producer.cpp
shell/server.cpp

index 9007bee22463cc6ed3471dfde7c352d76dd6ee5b..235708c9de8dab6effe8c3ee2712f6a5c8e99ea2 100644 (file)
   </ItemDefinitionGroup>\r
   <ItemGroup>\r
     <ClInclude Include="compiler\vs\disable_silly_warnings.h" />\r
+    <ClInclude Include="concurrency\com_context.h" />\r
     <ClInclude Include="concurrency\executor.h" />\r
     <ClInclude Include="diagnostics\graph.h" />\r
     <ClInclude Include="exception\exceptions.h" />\r
index 65a703a9a9ccb99efbd0bbc1514a42e52bc63c99..b0b0d26adc1002932f6611460f113ebcf75ab45c 100644 (file)
     <ClInclude Include="os\windows\system_info.h">\r
       <Filter>os\windows</Filter>\r
     </ClInclude>\r
+    <ClInclude Include="concurrency\com_context.h">\r
+      <Filter>concurrency</Filter>\r
+    </ClInclude>\r
   </ItemGroup>\r
 </Project>
\ No newline at end of file
diff --git a/common/concurrency/com_context.h b/common/concurrency/com_context.h
new file mode 100644 (file)
index 0000000..e940bb0
--- /dev/null
@@ -0,0 +1,55 @@
+#pragma once\r
+\r
+#include "executor.h"\r
+\r
+#include <Windows.h>\r
+\r
+#include <boost/noncopyable.hpp>\r
+\r
+#include <functional>\r
+\r
+namespace caspar {\r
+\r
+template<typename T>\r
+class com_context : public executor\r
+{\r
+       std::unique_ptr<T> instance_;\r
+public:\r
+       com_context(const std::wstring& name)\r
+               : executor(name, true)\r
+       {\r
+               executor::begin_invoke([]\r
+               {\r
+                       ::CoInitialize(nullptr);\r
+               });\r
+       }\r
+\r
+       ~com_context()\r
+       {\r
+               executor::clear();\r
+               executor::invoke([&]\r
+               {\r
+                       instance_.reset(nullptr);\r
+                       ::CoUninitialize();\r
+               });\r
+       }\r
+       \r
+       void reset(const std::function<T*()>& factory)\r
+       {\r
+               executor::invoke([&]\r
+               {\r
+                       if(factory)\r
+                               instance_.reset(factory());\r
+                       else\r
+                               instance_.reset(nullptr);\r
+               });\r
+       }\r
+\r
+       T& operator*() const { return *instance_.get();}  // noexcept\r
+\r
+       T* operator->() const { return instance_.get();}  // noexcept\r
+\r
+       T* get() const { return instance_.get();}  // noexcept\r
+};\r
+\r
+}
\ No newline at end of file
index 8cdffcbb28b47308bdedb5be948161bddda31b75..a1f9b80f2f5ba26d5d77a36418575d90eb982315 100644 (file)
@@ -30,7 +30,7 @@
 \r
 #include <core/consumer/frame/read_frame.h>\r
 \r
-#include <common/concurrency/executor.h>\r
+#include <common/concurrency/com_context.h>\r
 #include <common/diagnostics/graph.h>\r
 #include <common/exception/exceptions.h>\r
 #include <common/memory/memcpy.h>\r
@@ -84,16 +84,10 @@ struct configuration
                , latency(default_latency){}\r
 };\r
 \r
-struct decklink_output : public IDeckLinkVideoOutputCallback, public IDeckLinkAudioOutputCallback, boost::noncopyable\r
+struct decklink_consumer : public IDeckLinkVideoOutputCallback, public IDeckLinkAudioOutputCallback, boost::noncopyable\r
 {              \r
        static const size_t BUFFER_SIZE = 4;\r
-       \r
-       struct co_init\r
-       {\r
-               co_init(){CoInitialize(nullptr);}\r
-               ~co_init(){CoUninitialize();}\r
-       } co_;\r
-       \r
+               \r
        const configuration config_;\r
 \r
        CComPtr<IDeckLink>                                      decklink_;\r
@@ -123,7 +117,7 @@ struct decklink_output : public IDeckLinkVideoOutputCallback, public IDeckLinkAu
        boost::timer tick_timer_;\r
 \r
 public:\r
-       decklink_output(const configuration& config, const core::video_format_desc& format_desc) \r
+       decklink_consumer(const configuration& config, const core::video_format_desc& format_desc) \r
                : config_(config)\r
                , decklink_(get_device(config.device_index))\r
                , output_(decklink_)\r
@@ -235,7 +229,7 @@ public:
                CASPAR_LOG(info) << print() << L" Successfully initialized for " << format_desc_.name;  \r
        }\r
 \r
-       ~decklink_output()\r
+       ~decklink_consumer()\r
        {               \r
                is_running_ = false;\r
                video_frame_buffer_.clear();\r
@@ -356,42 +350,30 @@ public:
        }\r
 };\r
 \r
-struct decklink_consumer : public core::frame_consumer\r
+struct decklink_consumer_proxy : public core::frame_consumer\r
 {\r
-       std::unique_ptr<decklink_output> input_;\r
-       configuration config_;\r
+       const configuration config_;\r
 \r
-       executor executor_;\r
+       com_context<decklink_consumer> context_;\r
 public:\r
 \r
-       decklink_consumer(const configuration& config)\r
+       decklink_consumer_proxy(const configuration& config)\r
                : config_(config)\r
-               , executor_(L"decklink_consumer[" + boost::lexical_cast<std::wstring>(config.device_index) + L"]", true){}\r
-\r
-       ~decklink_consumer()\r
-       {\r
-               executor_.invoke([&]\r
-               {\r
-                       input_ = nullptr;\r
-               });\r
-       }\r
-\r
+               , context_(L"decklink_consumer[" + boost::lexical_cast<std::wstring>(config.device_index) + L"]"){}\r
+       \r
        void initialize(const core::video_format_desc& format_desc)\r
        {\r
-               executor_.invoke([&]\r
-               {\r
-                       input_.reset(new decklink_output(config_, format_desc));\r
-               });\r
+               context_.reset([&]{return new decklink_consumer(config_, format_desc);});\r
        }\r
        \r
        void send(const safe_ptr<const core::read_frame>& frame)\r
        {\r
-               input_->send(frame);\r
+               context_->send(frame);\r
        }\r
        \r
        std::wstring print() const\r
        {\r
-               return input_->print();\r
+               return context_->print();\r
        }\r
 };     \r
 \r
@@ -405,22 +387,22 @@ safe_ptr<core::frame_consumer> create_decklink_consumer(const std::vector<std::w
        if(params.size() > 1)\r
                config.device_index = lexical_cast_or_default<int>(params[1], config.device_index);\r
                \r
-       auto it = std::find(params.begin(), params.end(), L"INTERNAL_KEY");\r
-       if(it != params.end())\r
+       if(std::find(params.begin(), params.end(), L"INTERNAL_KEY") != params.end())\r
                config.keyer = internal_key;\r
-       else\r
-       {\r
-               auto it = std::find(params.begin(), params.end(), L"EXTERNAL_KEY");\r
-               if(it != params.end())\r
-                       config.keyer = external_key;\r
-       }\r
+       else if(std::find(params.begin(), params.end(), L"EXTERNAL_KEY") != params.end())\r
+               config.keyer = external_key;\r
+       \r
+       if(std::find(params.begin(), params.end(), L"LOW_LATENCY") != params.end())\r
+               config.latency = low_latency;\r
+       else if(std::find(params.begin(), params.end(), L"NORMAL_LATENCY") != params.end())\r
+               config.latency = normal_latency;\r
                \r
        config.embedded_audio = std::find(params.begin(), params.end(), L"EMBEDDED_AUDIO") != params.end();\r
 \r
-       return make_safe<decklink_consumer>(config);\r
+       return make_safe<decklink_consumer_proxy>(config);\r
 }\r
 \r
-safe_ptr<core::frame_consumer> create_decklink_consumer_ptree(const boost::property_tree::ptree& ptree) \r
+safe_ptr<core::frame_consumer> create_decklink_consumer(const boost::property_tree::ptree& ptree) \r
 {\r
        configuration config;\r
 \r
@@ -439,7 +421,7 @@ safe_ptr<core::frame_consumer> create_decklink_consumer_ptree(const boost::prope
        config.device_index = ptree.get("device", 0);\r
        config.embedded_audio  = ptree.get("embedded-audio", false);\r
 \r
-       return make_safe<decklink_consumer>(config);\r
+       return make_safe<decklink_consumer_proxy>(config);\r
 }\r
 \r
 }
\ No newline at end of file
index 0d1400a9946d6644d43099a6ec0e97e3472178fc..41d49bb7669e715bcb99803a7d217ca08fd04156 100644 (file)
@@ -30,6 +30,6 @@
 namespace caspar { \r
        \r
 safe_ptr<core::frame_consumer> create_decklink_consumer(const std::vector<std::wstring>& params);\r
-safe_ptr<core::frame_consumer> create_decklink_consumer_ptree(const boost::property_tree::ptree& ptree);\r
+safe_ptr<core::frame_consumer> create_decklink_consumer(const boost::property_tree::ptree& ptree);\r
 \r
 }
\ No newline at end of file
index 22d164959360f13683c9ca6438606d337ce45e3d..cfcb4c8507cdb826d1266eeda9b1bcf3ba26834a 100644 (file)
@@ -44,7 +44,7 @@ namespace caspar{
 \r
 void init_decklink()\r
 {\r
-       core::register_consumer_factory(create_decklink_consumer);\r
+       core::register_consumer_factory([](const std::vector<std::wstring>& params){return create_decklink_consumer(params);});\r
        core::register_producer_factory(create_decklink_producer);\r
 }\r
 \r
index 65b2e4321fc77eff21363f5918525e4c4cbfc1a6..3fe4ce3d558737888d4b58461d4ce2a58f2b0c8a 100644 (file)
@@ -35,7 +35,7 @@
 #include <core/producer/frame/write_frame.h>\r
 \r
 #include <common/env.h>\r
-#include <common/concurrency/executor.h>\r
+#include <common/concurrency/com_context.h>\r
 #include <common/diagnostics/graph.h>\r
 #include <common/memory/memcpy.h>\r
 #include <common/memory/memclr.h>\r
 namespace caspar {\r
                \r
 class flash_renderer\r
-{\r
-       struct co_init\r
-       {\r
-               co_init(){CoInitialize(nullptr);}\r
-               ~co_init(){CoUninitialize();}\r
-       } co_;\r
-       \r
+{      \r
        const std::wstring filename_;\r
        const core::video_format_desc format_desc_;\r
 \r
-       std::shared_ptr<core::frame_factory> frame_factory_;\r
+       const std::shared_ptr<core::frame_factory> frame_factory_;\r
        \r
        BYTE* bmp_data_;        \r
        std::shared_ptr<void> hdc_;\r
@@ -96,7 +90,7 @@ public:
                if(FAILED(CComObject<caspar::flash::FlashAxContainer>::CreateInstance(&ax_)))\r
                        BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Failed to create FlashAxContainer"));\r
                \r
-               ax_->set_print([this]{return L"";});\r
+               ax_->set_print([this]{return L"flash_renderer";});\r
 \r
                if(FAILED(ax_->CreateAxControl()))\r
                        BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Failed to Create FlashAxControl"));\r
@@ -161,7 +155,7 @@ public:
                        return core::basic_frame::empty();              \r
                \r
                if(!has_underflow)                      \r
-                       timer_.tick(frame_time);        \r
+                       timer_.tick(frame_time); // This will block the thread.\r
                else\r
                        graph_->add_tag("underflow");\r
                        \r
@@ -196,51 +190,39 @@ public:
 struct flash_producer : public core::frame_producer\r
 {      \r
        const std::wstring filename_;   \r
+       const safe_ptr<core::frame_factory> frame_factory_;\r
+\r
        tbb::atomic<int> fps_;\r
-       bool underfow_;\r
 \r
        std::shared_ptr<diagnostics::graph> graph_;\r
 \r
        safe_ptr<core::basic_frame> tail_;\r
        tbb::concurrent_bounded_queue<safe_ptr<core::basic_frame>> frame_buffer_;\r
-\r
-       std::shared_ptr<flash_renderer> renderer_;\r
-       safe_ptr<core::frame_factory> frame_factory_;\r
-       const core::video_format_desc format_desc_;\r
-                       \r
-       executor executor_;\r
-               \r
+                               \r
+       com_context<flash_renderer> context_;           \r
 public:\r
        flash_producer(const safe_ptr<core::frame_factory>& frame_factory, const std::wstring& filename) \r
                : filename_(filename)           \r
-               , tail_(core::basic_frame::empty())             \r
                , frame_factory_(frame_factory)\r
-               , format_desc_(frame_factory->get_video_format_desc())\r
-               , executor_(L"flash_producer", true)\r
+               , tail_(core::basic_frame::empty())             \r
+               , context_(L"flash_producer")\r
        {       \r
                if(!boost::filesystem::exists(filename))\r
                        BOOST_THROW_EXCEPTION(file_not_found() << boost::errinfo_file_name(narrow(filename)));  \r
                 \r
-               frame_buffer_.set_capacity(2);\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
                \r
-               executor_.begin_invoke([=]\r
-               {\r
-                       init_renderer();\r
-               });\r
-                               \r
-               fps_ = 0;\r
+               frame_buffer_.set_capacity(2);\r
+\r
+               initialize();                           \r
        }\r
 \r
        ~flash_producer()\r
        {\r
-               executor_.clear();\r
                frame_buffer_.clear();\r
-               executor_.invoke([=]\r
-               {\r
-                       renderer_ = nullptr;\r
-               });             \r
        }\r
 \r
        // frame_producer\r
@@ -248,27 +230,27 @@ public:
        virtual safe_ptr<core::basic_frame> receive()\r
        {                               \r
                graph_->set_value("output-buffer", static_cast<float>(frame_buffer_.size())/static_cast<float>(frame_buffer_.capacity()));\r
-               if(!frame_buffer_.try_pop(tail_))\r
-                       return tail_;\r
-               \r
+\r
+               frame_buffer_.try_pop(tail_);           \r
                return tail_;\r
        }\r
        \r
        virtual void param(const std::wstring& param) \r
        {       \r
-               executor_.begin_invoke([=]\r
+               context_.begin_invoke([=]\r
                {\r
-                       if(!renderer_)\r
-                               init_renderer();\r
+                       if(!context_.get())\r
+                               initialize();\r
 \r
                        try\r
                        {\r
-                               renderer_->param(param);        \r
+                               context_->param(param); \r
                        }\r
                        catch(...)\r
                        {\r
                                CASPAR_LOG_CURRENT_EXCEPTION();\r
-                               renderer_ = nullptr;\r
+                               context_.reset(nullptr);\r
+                               frame_buffer_.push(core::basic_frame::empty());\r
                        }\r
                });\r
        }\r
@@ -281,59 +263,55 @@ public:
 \r
        // flash_producer\r
 \r
-       void init_renderer()\r
+       void initialize()\r
        {\r
-               renderer_.reset(new flash_renderer(safe_ptr<diagnostics::graph>(graph_), frame_factory_, filename_));\r
+               context_.reset([&]{return new flash_renderer(safe_ptr<diagnostics::graph>(graph_), frame_factory_, filename_);});\r
                while(frame_buffer_.try_push(core::basic_frame::empty())){}             \r
-               executor_.begin_invoke([=]\r
-               {\r
-                       render(renderer_);\r
-               });             \r
+               render(context_.get());\r
        }\r
 \r
-       void render(const std::shared_ptr<flash_renderer>& renderer)\r
-       {\r
-               if(renderer_ != renderer) // Make sure the recursive calls are only for a specific instance.\r
+       void render(const flash_renderer* renderer)\r
+       {               \r
+               context_.begin_invoke([=]\r
                {\r
-                       frame_buffer_.push(core::basic_frame::empty());\r
-                       return;\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
-                       auto frame = core::basic_frame::empty();\r
-                       if(abs(renderer_->fps()/2.0 - format_desc_.fps) < 0.1) //flash 50, format 50i\r
-                       {\r
-                               auto frame1 = renderer_->render_frame(frame_buffer_.size() < frame_buffer_.capacity());\r
-                               auto frame2 = renderer_->render_frame(frame_buffer_.size() < frame_buffer_.capacity());\r
-                               frame_buffer_.push(core::basic_frame::interlace(frame1, frame2, format_desc_.mode));\r
-                               frame = frame2;\r
-                       }\r
-                       else if(abs(renderer_->fps()- format_desc_.fps/2.0 ) < 0.1) //flash 25, format 50p\r
-                       {\r
-                               frame = renderer_->render_frame(frame_buffer_.size() < frame_buffer_.capacity());\r
-                               frame_buffer_.push(frame);\r
-                               frame_buffer_.push(frame);\r
+                       try\r
+                       {               \r
+                               const auto& format_desc = frame_factory_->get_video_format_desc();\r
+                               auto frame = core::basic_frame::empty();\r
+                               if(abs(context_->fps()/2.0 - format_desc.fps) < 0.1) // flash 50fps, format 50i\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
+                                       frame = frame2;\r
+                               }\r
+                               else if(abs(context_->fps()- format_desc.fps/2.0 ) < 0.1) // flash 25fps, format 50p\r
+                               {\r
+                                       frame = context_->render_frame(frame_buffer_.size() < frame_buffer_.capacity());\r
+                                       frame_buffer_.push(frame);\r
+                                       frame_buffer_.push(frame);\r
+                               }\r
+                               else //if(abs(renderer_->fps() - format_desc_.fps) < 0.1) // flash 25fps, format 50i or flash 50fps, format 50p\r
+                               {\r
+                                       frame = context_->render_frame(frame_buffer_.size() < frame_buffer_.capacity());\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
+\r
+                               render(renderer);\r
                        }\r
-                       else //if(abs(renderer_->fps() - format_desc_.fps) < 0.1) // flash 25, format 50i or flash 50, format 50p\r
+                       catch(...)\r
                        {\r
-                               frame = renderer_->render_frame(frame_buffer_.size() < frame_buffer_.capacity());\r
-                               frame_buffer_.push(frame);\r
+                               CASPAR_LOG_CURRENT_EXCEPTION();\r
+                               context_.reset(nullptr);\r
+                               frame_buffer_.push(core::basic_frame::empty());\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>(renderer_->fps()*100.0));\r
-\r
-                       executor_.begin_invoke([=]\r
-                       {\r
-                               render(renderer);\r
-                       });                     \r
-               }\r
-               catch(...)\r
-               {\r
-                       CASPAR_LOG_CURRENT_EXCEPTION();\r
-                       renderer_ = nullptr;\r
-               }\r
+               });\r
        }\r
 };\r
 \r
index 7f8b23043eb139a5b771a3e44f3c725be7ca43e2..5ed616e69414ca0b36989d6455815d9c6a874be2 100644 (file)
@@ -121,7 +121,7 @@ struct server::implementation : boost::noncopyable
                                                channels_.back()->consumer()->add(index++, bluefish_consumer(xml_consumer.second.get("device", 0), \r
                                                                                                                                                                        xml_consumer.second.get("embedded-audio", true)));                                      \r
                                        else if(name == "decklink")                                     \r
-                                               channels_.back()->consumer()->add(index++, create_decklink_consumer_ptree(xml_consumer.second));                                        \r
+                                               channels_.back()->consumer()->add(index++, create_decklink_consumer(xml_consumer.second));                                      \r
                                        else if(name == "audio")\r
                                                channels_.back()->consumer()->add(index++, oal_consumer());                     \r
                                }\r