]> git.sesse.net Git - casparcg/blobdiff - modules/decklink/consumer/decklink_consumer.cpp
2.0. ogl_consumer: Refactored
[casparcg] / modules / decklink / consumer / decklink_consumer.cpp
index 71da7b33fadace2748709f66d8ef465ca2f07208..601d875175c1a61da4864983787a1f73d062de1b 100644 (file)
@@ -48,7 +48,7 @@ struct configuration
 {\r
        size_t  device_index;\r
        bool    embedded_audio;\r
-       bool    external_key;\r
+       bool    internal_key;\r
        bool    low_latency;\r
        bool    key_only;\r
        size_t  buffer_depth;\r
@@ -56,18 +56,18 @@ struct configuration
        configuration()\r
                : device_index(1)\r
                , embedded_audio(false)\r
-               , external_key(false)\r
+               , internal_key(false)\r
                , low_latency(false)\r
                , key_only(false)\r
                , buffer_depth(core::consumer_buffer_depth()){}\r
 };\r
 \r
-class decklink_frame_muxer : public IDeckLinkVideoFrame\r
+class decklink_frame : public IDeckLinkVideoFrame\r
 {\r
        const safe_ptr<core::read_frame>        frame_;\r
-       const core::video_format_desc                   format_desc_;\r
+       const core::video_format_desc           format_desc_;\r
 public:\r
-       decklink_frame_muxer(const safe_ptr<core::read_frame>& frame, const core::video_format_desc& format_desc)\r
+       decklink_frame(const safe_ptr<core::read_frame>& frame, const core::video_format_desc& format_desc)\r
                : frame_(frame)\r
                , format_desc_(format_desc){}\r
        \r
@@ -103,18 +103,19 @@ struct decklink_consumer : public IDeckLinkVideoOutputCallback, public IDeckLink
        CComQIPtr<IDeckLinkConfiguration>       configuration_;\r
        CComQIPtr<IDeckLinkKeyer>                       keyer_;\r
 \r
-       std::exception_ptr exception_;\r
+       tbb::spin_mutex                                         exception_mutex_;\r
+       std::exception_ptr                                      exception_;\r
 \r
-       tbb::atomic<bool> is_running_;\r
+       tbb::atomic<bool>                                       is_running_;\r
                \r
-       const std::wstring model_name_;\r
-       const core::video_format_desc format_desc_;\r
-       const size_t buffer_size_;\r
+       const std::wstring                                      model_name_;\r
+       const core::video_format_desc           format_desc_;\r
+       const size_t                                            buffer_size_;\r
 \r
-       unsigned long frames_scheduled_;\r
-       unsigned long audio_scheduled_;\r
+       long long                                                       frames_scheduled_;\r
+       long long                                                       audio_scheduled_;\r
 \r
-       size_t preroll_count_;\r
+       size_t                                                          preroll_count_;\r
                \r
        std::list<std::shared_ptr<IDeckLinkVideoFrame>> frame_container_; // Must be std::list in order to guarantee that pointers are always valid.\r
        boost::circular_buffer<std::vector<int16_t>>    audio_container_;\r
@@ -147,10 +148,10 @@ public:
 \r
                graph_ = diagnostics::create_graph(narrow(print()));\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("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.3f, 0.3f, 0.6f));\r
+               graph_->set_color("flushed-frame", diagnostics::color(0.4f, 0.3f, 0.8f));\r
                \r
                enable_video(get_display_mode(output_, format_desc_.format, bmdFormat8BitBGRA, bmdVideoOutputFlagDefault));\r
                                \r
@@ -158,7 +159,7 @@ public:
                        enable_audio();\r
 \r
                set_latency(config.low_latency);                                \r
-               set_keyer(config.external_key);\r
+               set_keyer(config.internal_key);\r
                                \r
                if(config.embedded_audio)               \r
                        output_->BeginAudioPreroll();           \r
@@ -168,8 +169,6 @@ public:
 \r
                if(!config.embedded_audio)\r
                        start_playback();\r
-                               \r
-               CASPAR_LOG(info) << print() << L" Successfully Initialized.";   \r
        }\r
 \r
        ~decklink_consumer()\r
@@ -206,9 +205,9 @@ public:
                }\r
        }\r
 \r
-       void set_keyer(bool external_key)\r
+       void set_keyer(bool internal_key)\r
        {\r
-               if(!external_key) \r
+               if(internal_key) \r
                {\r
                        if(FAILED(keyer_->Enable(FALSE)))                       \r
                                CASPAR_LOG(error) << print() << L" Failed to enable internal keyer.";                   \r
@@ -292,10 +291,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(frame));  \r
                }\r
                catch(...)\r
                {\r
+                       tbb::spin_mutex::scoped_lock lock(exception_mutex_);\r
                        exception_ = std::current_exception();\r
                        return E_FAIL;\r
                }\r
@@ -329,6 +329,7 @@ public:
                }\r
                catch(...)\r
                {\r
+                       tbb::spin_mutex::scoped_lock lock(exception_mutex_);\r
                        exception_ = std::current_exception();\r
                        return E_FAIL;\r
                }\r
@@ -348,7 +349,7 @@ public:
                        \r
        void schedule_next_video(const safe_ptr<core::read_frame>& frame)\r
        {\r
-               frame_container_.push_back(std::make_shared<decklink_frame_muxer>(frame, format_desc_));\r
+               frame_container_.push_back(std::make_shared<decklink_frame>(frame, format_desc_));\r
                if(FAILED(output_->ScheduleVideoFrame(frame_container_.back().get(), (frames_scheduled_++) * format_desc_.duration, format_desc_.duration, format_desc_.time_scale)))\r
                        CASPAR_LOG(error) << print() << L" Failed to schedule video.";\r
 \r
@@ -358,8 +359,11 @@ public:
 \r
        void send(const safe_ptr<core::read_frame>& frame)\r
        {\r
-               if(exception_ != nullptr)\r
-                       std::rethrow_exception(exception_);\r
+               {\r
+                       tbb::spin_mutex::scoped_lock lock(exception_mutex_);\r
+                       if(exception_ != nullptr)\r
+                               std::rethrow_exception(exception_);\r
+               }\r
 \r
                if(!is_running_)\r
                        BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Is not running."));\r
@@ -377,29 +381,60 @@ public:
 \r
 struct decklink_consumer_proxy : public core::frame_consumer\r
 {\r
-       const configuration config_;\r
-\r
-       com_context<decklink_consumer> context_;\r
+       const configuration                             config_;\r
+       com_context<decklink_consumer>  context_;\r
+       core::video_format_desc                 format_desc_;\r
+       size_t                                                  fail_count_;\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
+               , context_(L"decklink_consumer[" + boost::lexical_cast<std::wstring>(config.device_index) + L"]")\r
+               , fail_count_(0)\r
+       {\r
+       }\r
+\r
+       ~decklink_consumer_proxy()\r
+       {\r
+               auto str = print();\r
+               context_.reset();\r
+               CASPAR_LOG(info) << str << L" Successfully Uninitialized.";     \r
+       }\r
        \r
        virtual void initialize(const core::video_format_desc& format_desc)\r
        {\r
-               context_.reset([&]{return new decklink_consumer(config_, format_desc);});\r
+               format_desc_ = format_desc;\r
+               context_.reset([&]{return new decklink_consumer(config_, format_desc_);});              \r
+                               \r
+               CASPAR_LOG(info) << print() << L" Successfully Initialized.";   \r
        }\r
        \r
        virtual bool send(const safe_ptr<core::read_frame>& frame)\r
        {\r
-               context_->send(frame);\r
+               if(!context_)\r
+                       context_.reset([&]{return new decklink_consumer(config_, format_desc_);});\r
+\r
+               try\r
+               {\r
+                       context_->send(frame);\r
+                       fail_count_ = 0;\r
+               }\r
+               catch(...)\r
+               {\r
+                       context_.reset();\r
+\r
+                       if(fail_count_++ > 3)\r
+                               return false;  // Outside didn't handle exception properly, just give up.\r
+                       \r
+                       throw;\r
+               }\r
+\r
                return true;\r
        }\r
        \r
        virtual std::wstring print() const\r
        {\r
-               return context_->print();\r
+               return context_ ? context_->print() : L"decklink_consumer";\r
        }\r
 \r
        virtual bool key_only() const\r
@@ -409,7 +444,7 @@ public:
                \r
        virtual const core::video_format_desc& get_video_format_desc() const\r
        {\r
-               return context_->get_video_format_desc();\r
+               return format_desc_;\r
        }\r
 };     \r
 \r
@@ -423,7 +458,7 @@ 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
-       config.external_key             = std::find(params.begin(), params.end(), L"EXTERNAL_KEY")       != params.end();\r
+       config.internal_key             = std::find(params.begin(), params.end(), L"INTERNAL_KEY")       != params.end();\r
        config.low_latency              = std::find(params.begin(), params.end(), L"LOW_LATENCY")        != params.end();\r
        config.embedded_audio   = std::find(params.begin(), params.end(), L"EMBEDDED_AUDIO") != params.end();\r
        config.key_only                 = std::find(params.begin(), params.end(), L"KEY_ONLY")           != params.end();\r
@@ -435,7 +470,7 @@ safe_ptr<core::frame_consumer> create_decklink_consumer(const boost::property_tr
 {\r
        configuration config;\r
 \r
-       config.external_key             = ptree.get("external-key",       config.external_key);\r
+       config.internal_key             = ptree.get("internal-key",       config.internal_key);\r
        config.low_latency              = ptree.get("low-latency",        config.low_latency);\r
        config.key_only                 = ptree.get("key-only",           config.key_only);\r
        config.device_index             = ptree.get("device",             config.device_index);\r