]> git.sesse.net Git - casparcg/blobdiff - modules/decklink/consumer/decklink_consumer.cpp
git-svn-id: https://casparcg.svn.sourceforge.net/svnroot/casparcg/server/branches...
[casparcg] / modules / decklink / consumer / decklink_consumer.cpp
index 03131d31031bba151af2b1aa9199bc420945a9be..9895fac1017938bf67154049f9399b55d997cc73 100644 (file)
@@ -28,7 +28,6 @@
 \r
 #include <core/mixer/read_frame.h>\r
 \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
 #include <boost/circular_buffer.hpp>\r
 #include <boost/timer.hpp>\r
 \r
-namespace caspar { \r
+#include <agents.h>\r
+#include <concrt_extras.h>\r
+\r
+namespace caspar { namespace decklink { \r
        \r
 struct configuration\r
 {\r
@@ -52,15 +54,21 @@ struct configuration
        bool    internal_key;\r
        bool    low_latency;\r
        bool    key_only;\r
-       size_t  buffer_depth;\r
        \r
        configuration()\r
                : device_index(1)\r
                , embedded_audio(false)\r
                , internal_key(false)\r
                , low_latency(false)\r
-               , key_only(false)\r
-               , buffer_depth(core::consumer_buffer_depth()){}\r
+               , key_only(false){}\r
+\r
+       size_t preroll_count() const\r
+       {\r
+               size_t count = 0;\r
+               count += low_latency ? 2 : 3;\r
+               count += embedded_audio ? 1 : 0;\r
+               return count;\r
+       }\r
 };\r
 \r
 class decklink_frame : public IDeckLinkVideoFrame\r
@@ -137,8 +145,7 @@ struct decklink_consumer : public IDeckLinkVideoOutputCallback, public IDeckLink
        CComQIPtr<IDeckLinkConfiguration>       configuration_;\r
        CComQIPtr<IDeckLinkKeyer>                       keyer_;\r
 \r
-       tbb::spin_mutex                                         exception_mutex_;\r
-       std::exception_ptr                                      exception_;\r
+       Concurrency::overwrite_buffer<std::exception_ptr>       exception_;\r
 \r
        tbb::atomic<bool>                                       is_running_;\r
                \r
@@ -156,7 +163,7 @@ struct decklink_consumer : public IDeckLinkVideoOutputCallback, public IDeckLink
        tbb::concurrent_bounded_queue<std::shared_ptr<core::read_frame>> video_frame_buffer_;\r
        tbb::concurrent_bounded_queue<std::shared_ptr<core::read_frame>> audio_frame_buffer_;\r
        \r
-       std::shared_ptr<diagnostics::graph> graph_;\r
+       safe_ptr<diagnostics::graph> graph_;\r
        boost::timer tick_timer_;\r
 \r
 public:\r
@@ -168,7 +175,7 @@ public:
                , keyer_(decklink_)\r
                , model_name_(get_model_name(decklink_))\r
                , format_desc_(format_desc)\r
-               , buffer_size_(config.embedded_audio ? config.buffer_depth + 1 : config.buffer_depth) // Minimum buffer-size 3.\r
+               , buffer_size_(config.preroll_count())\r
                , frames_scheduled_(0)\r
                , audio_scheduled_(0)\r
                , preroll_count_(0)\r
@@ -179,12 +186,13 @@ public:
                video_frame_buffer_.set_capacity(1);\r
                audio_frame_buffer_.set_capacity(1);\r
 \r
-               graph_ = diagnostics::create_graph(narrow(print()));\r
                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("late-frame", diagnostics::color(0.6f, 0.3f, 0.3f));\r
                graph_->set_color("dropped-frame", diagnostics::color(0.3f, 0.6f, 0.3f));\r
                graph_->set_color("flushed-frame", diagnostics::color(0.4f, 0.3f, 0.8f));\r
+               graph_->set_text(print());\r
+               diagnostics::register_graph(graph_);\r
                \r
                enable_video(get_display_mode(output_, format_desc_.format, bmdFormat8BitBGRA, bmdVideoOutputFlagDefault));\r
                                \r
@@ -319,12 +327,11 @@ public:
 \r
                        std::shared_ptr<core::read_frame> frame;        \r
                        video_frame_buffer_.pop(frame);                                 \r
-                       schedule_next_video(make_safe(frame));  \r
+                       schedule_next_video(make_safe_ptr(frame));      \r
                }\r
                catch(...)\r
                {\r
-                       tbb::spin_mutex::scoped_lock lock(exception_mutex_);\r
-                       exception_ = std::current_exception();\r
+                       Concurrency::send(exception_, std::current_exception());\r
                        return E_FAIL;\r
                }\r
 \r
@@ -352,13 +359,12 @@ public:
                        {\r
                                std::shared_ptr<core::read_frame> frame;\r
                                audio_frame_buffer_.pop(frame);\r
-                               schedule_next_audio(make_safe(frame));  \r
+                               schedule_next_audio(make_safe_ptr(frame));      \r
                        }\r
                }\r
                catch(...)\r
                {\r
-                       tbb::spin_mutex::scoped_lock lock(exception_mutex_);\r
-                       exception_ = std::current_exception();\r
+                       Concurrency::send(exception_, std::current_exception());\r
                        return E_FAIL;\r
                }\r
 \r
@@ -387,15 +393,13 @@ public:
 \r
        void send(const safe_ptr<core::read_frame>& frame)\r
        {\r
-               {\r
-                       tbb::spin_mutex::scoped_lock lock(exception_mutex_);\r
-                       if(exception_ != nullptr)\r
-                               std::rethrow_exception(exception_);\r
-               }\r
+               if(exception_.has_value())\r
+                       std::rethrow_exception(exception_.value());\r
 \r
                if(!is_running_)\r
                        BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Is not running."));\r
                \r
+               Concurrency::scoped_oversubcription_token oversubscribe;                \r
                if(config_.embedded_audio)\r
                        audio_frame_buffer_.push(frame);        \r
                video_frame_buffer_.push(frame);        \r
@@ -409,14 +413,13 @@ public:
 \r
 struct decklink_consumer_proxy : public core::frame_consumer\r
 {\r
-       const configuration                             config_;\r
-       com_context<decklink_consumer>  context_;\r
-       core::video_format_desc                 format_desc_;\r
+       const configuration                                     config_;\r
+       std::unique_ptr<decklink_consumer>      context_;\r
+       core::video_format_desc                         format_desc_;\r
 public:\r
 \r
        decklink_consumer_proxy(const configuration& config)\r
                : config_(config)\r
-               , context_(L"decklink_consumer[" + boost::lexical_cast<std::wstring>(config.device_index) + L"]")\r
        {\r
        }\r
 \r
@@ -429,8 +432,15 @@ public:
        \r
        virtual void initialize(const core::video_format_desc& format_desc)\r
        {\r
+               Concurrency::scoped_oversubcription_token oversubscribe;\r
                format_desc_ = format_desc;\r
-               context_.reset([&]{return new decklink_consumer(config_, format_desc_);});              \r
+               struct co_init\r
+               {\r
+                       co_init(){CoInitialize(nullptr);}\r
+                       ~co_init(){CoUninitialize();}\r
+               } init;         \r
+               context_ = nullptr;\r
+               context_.reset(new decklink_consumer(config_, format_desc_));           \r
                                \r
                CASPAR_LOG(info) << print() << L" Successfully Initialized.";   \r
        }\r
@@ -452,7 +462,7 @@ public:
        }\r
 };     \r
 \r
-safe_ptr<core::frame_consumer> create_decklink_consumer(const std::vector<std::wstring>& params) \r
+safe_ptr<core::frame_consumer> create_consumer(const std::vector<std::wstring>& params) \r
 {\r
        if(params.size() < 1 || params[0] != L"DECKLINK")\r
                return core::frame_consumer::empty();\r
@@ -470,7 +480,7 @@ safe_ptr<core::frame_consumer> create_decklink_consumer(const std::vector<std::w
        return make_safe<decklink_consumer_proxy>(config);\r
 }\r
 \r
-safe_ptr<core::frame_consumer> create_decklink_consumer(const boost::property_tree::ptree& ptree) \r
+safe_ptr<core::frame_consumer> create_consumer(const boost::property_tree::ptree& ptree) \r
 {\r
        configuration config;\r
 \r
@@ -483,7 +493,7 @@ safe_ptr<core::frame_consumer> create_decklink_consumer(const boost::property_tr
        return make_safe<decklink_consumer_proxy>(config);\r
 }\r
 \r
-}\r
+}}\r
 \r
 /*\r
 ##############################################################################\r