]> 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 68e2d6948f8aa4dc4ee27e333f1398acb162e1f2..dcfbde7f3afcf5dde5bcbe44f6d99e5574b275b8 100644 (file)
 #include <common/memory/memclr.h>\r
 #include <common/memory/memshfl.h>\r
 \r
+#include <core/consumer/frame_consumer.h>\r
+\r
 #include <tbb/concurrent_queue.h>\r
+#include <tbb/cache_aligned_allocator.h>\r
 \r
 #include <boost/circular_buffer.hpp>\r
 #include <boost/timer.hpp>\r
 \r
-namespace caspar { \r
+namespace caspar { namespace decklink { \r
        \r
 struct configuration\r
 {\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
@@ -54,37 +57,70 @@ 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(CONSUMER_BUFFER_DEPTH){}\r
+               , buffer_depth(core::consumer_buffer_depth()){}\r
 };\r
 \r
-class decklink_frame_adapter : public IDeckLinkVideoFrame\r
+class decklink_frame : public IDeckLinkVideoFrame\r
 {\r
-       const safe_ptr<const core::read_frame>  frame_;\r
-       const core::video_format_desc                   format_desc_;\r
+       tbb::atomic<int>                                                                                        ref_count_;\r
+       std::shared_ptr<core::read_frame>                                                       frame_;\r
+       const core::video_format_desc                                                           format_desc_;\r
+\r
+       bool                                                                                                            key_only_;\r
+       std::vector<uint8_t, tbb::cache_aligned_allocator<uint8_t>> key_data_;\r
 public:\r
-       decklink_frame_adapter(const safe_ptr<const 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, bool key_only)\r
                : frame_(frame)\r
-               , format_desc_(format_desc){}\r
+               , format_desc_(format_desc)\r
+               , key_only_(key_only)\r
+       {\r
+               ref_count_ = 0;\r
+       }\r
        \r
-       STDMETHOD (QueryInterface(REFIID, LPVOID*))     {return E_NOINTERFACE;}\r
-       STDMETHOD_(ULONG, AddRef())                                     {return 1;}\r
-       STDMETHOD_(ULONG, Release())                            {return 1;}\r
+       STDMETHOD (QueryInterface(REFIID, LPVOID*))             {return E_NOINTERFACE;}\r
+       STDMETHOD_(ULONG,                       AddRef())                       \r
+       {\r
+               return ++ref_count_;\r
+       }\r
+       STDMETHOD_(ULONG,                       Release())                      \r
+       {\r
+               --ref_count_;\r
+               if(ref_count_ == 0)\r
+                       delete this;\r
+               return ref_count_;\r
+       }\r
 \r
-       STDMETHOD_(long, GetWidth())                            {return format_desc_.width;}        \r
-    STDMETHOD_(long, GetHeight())                              {return format_desc_.height;}        \r
-    STDMETHOD_(long, GetRowBytes())                            {return format_desc_.width*4;}        \r
-       STDMETHOD_(BMDPixelFormat, GetPixelFormat()){return bmdFormat8BitBGRA;}        \r
-    STDMETHOD_(BMDFrameFlags, GetFlags())              {return bmdFrameFlagDefault;}\r
+       STDMETHOD_(long,                        GetWidth())                     {return format_desc_.width;}        \r
+    STDMETHOD_(long,                   GetHeight())            {return format_desc_.height;}        \r
+    STDMETHOD_(long,                   GetRowBytes())          {return format_desc_.width*4;}        \r
+       STDMETHOD_(BMDPixelFormat,      GetPixelFormat())       {return bmdFormat8BitBGRA;}        \r
+    STDMETHOD_(BMDFrameFlags,  GetFlags())                     {return bmdFrameFlagDefault;}\r
         \r
     STDMETHOD(GetBytes(void** buffer))\r
        {\r
-               static std::vector<unsigned char> zeros(1920*1080*4, 0);\r
-               *buffer = const_cast<unsigned char*>(frame_->image_data().begin());\r
+               static std::vector<uint8_t, tbb::cache_aligned_allocator<uint8_t>> zeros(1920*1080*4, 0);\r
                if(static_cast<size_t>(frame_->image_data().size()) != format_desc_.size)\r
+               {\r
                        *buffer = zeros.data();\r
+                       return S_OK;\r
+               }\r
+\r
+               if(!key_only_)\r
+                       *buffer = const_cast<uint8_t*>(frame_->image_data().begin());\r
+               else\r
+               {\r
+                       if(key_data_.empty())\r
+                       {\r
+                               key_data_.resize(frame_->image_data().size());\r
+                               fast_memshfl(key_data_.data(), frame_->image_data().begin(), frame_->image_data().size(), 0x0F0F0F0F, 0x0B0B0B0B, 0x07070707, 0x03030303);\r
+                               frame_.reset();\r
+                       }\r
+                       *buffer = key_data_.data();\r
+               }\r
+\r
                return S_OK;\r
        }\r
         \r
@@ -94,31 +130,31 @@ public:
 \r
 struct decklink_consumer : public IDeckLinkVideoOutputCallback, public IDeckLinkAudioOutputCallback, boost::noncopyable\r
 {              \r
-       const configuration config_;\r
+       const configuration                                     config_;\r
 \r
        CComPtr<IDeckLink>                                      decklink_;\r
        CComQIPtr<IDeckLinkOutput>                      output_;\r
        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<short>> audio_container_;\r
+       boost::circular_buffer<std::vector<int32_t>>    audio_container_;\r
 \r
-       tbb::concurrent_bounded_queue<std::shared_ptr<const core::read_frame>> video_frame_buffer_;\r
-       tbb::concurrent_bounded_queue<std::shared_ptr<const core::read_frame>> audio_frame_buffer_;\r
+       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
        boost::timer tick_timer_;\r
@@ -145,10 +181,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
@@ -156,7 +192,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
@@ -166,8 +202,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
@@ -204,9 +238,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
@@ -228,7 +262,7 @@ public:
        \r
        void enable_audio()\r
        {\r
-               if(FAILED(output_->EnableAudioOutput(bmdAudioSampleRate48kHz, bmdAudioSampleType16bitInteger, 2, bmdAudioOutputStreamTimestamped)))\r
+               if(FAILED(output_->EnableAudioOutput(bmdAudioSampleRate48kHz, bmdAudioSampleType32bitInteger, 2, bmdAudioOutputStreamTimestamped)))\r
                                BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Could not enable audio output."));\r
                                \r
                if(FAILED(output_->SetAudioCallback(this)))\r
@@ -243,7 +277,9 @@ public:
                        BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Could not enable video output."));\r
                \r
                if(FAILED(output_->SetScheduledFrameCompletionCallback(this)))\r
-                       BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Failed to set playback completion callback."));\r
+                       BOOST_THROW_EXCEPTION(caspar_exception() \r
+                                                                       << msg_info(narrow(print()) + " Failed to set playback completion callback.")\r
+                                                                       << boost::errinfo_api_function("SetScheduledFrameCompletionCallback"));\r
        }\r
 \r
        void start_playback()\r
@@ -271,23 +307,23 @@ public:
                try\r
                {\r
                        if(result == bmdOutputFrameDisplayedLate)\r
+                       {\r
                                graph_->add_tag("late-frame");\r
+                               ++frames_scheduled_;\r
+                               ++audio_scheduled_;\r
+                       }\r
                        else if(result == bmdOutputFrameDropped)\r
                                graph_->add_tag("dropped-frame");\r
                        else if(result == bmdOutputFrameFlushed)\r
                                graph_->add_tag("flushed-frame");\r
 \r
-                       frame_container_.erase(std::find_if(frame_container_.begin(), frame_container_.end(), [&](const std::shared_ptr<IDeckLinkVideoFrame>& frame)\r
-                       {\r
-                               return frame.get() == completed_frame;\r
-                       }));\r
-\r
-                       std::shared_ptr<const core::read_frame> frame;  \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
                        return E_FAIL;\r
                }\r
@@ -314,13 +350,14 @@ public:
                        }\r
                        else\r
                        {\r
-                               std::shared_ptr<const core::read_frame> frame;\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
                        return E_FAIL;\r
                }\r
@@ -328,34 +365,33 @@ public:
                return S_OK;\r
        }\r
 \r
-       void schedule_next_audio(const safe_ptr<const core::read_frame>& frame)\r
+       void schedule_next_audio(const safe_ptr<core::read_frame>& frame)\r
        {\r
-               static std::vector<short> silence(48000, 0);\r
-               \r
-               const int sample_count = format_desc_.audio_samples_per_frame;\r
-               const int sample_frame_count = sample_count/2;\r
+               const int sample_frame_count = frame->audio_data().size()/format_desc_.audio_channels;\r
 \r
-               const int16_t* frame_audio_data = frame->audio_data().size() == sample_count ? frame->audio_data().begin() : silence.data();\r
-               audio_container_.push_back(std::vector<int16_t>(frame_audio_data, frame_audio_data+sample_count));\r
+               audio_container_.push_back(std::vector<int32_t>(frame->audio_data().begin(), frame->audio_data().end()));\r
 \r
                if(FAILED(output_->ScheduleAudioSamples(audio_container_.back().data(), sample_frame_count, (audio_scheduled_++) * sample_frame_count, format_desc_.audio_sample_rate, nullptr)))\r
                        CASPAR_LOG(error) << print() << L" Failed to schedule audio.";\r
        }\r
                        \r
-       void schedule_next_video(const safe_ptr<const core::read_frame>& frame)\r
+       void schedule_next_video(const safe_ptr<core::read_frame>& frame)\r
        {\r
-               frame_container_.push_back(std::make_shared<decklink_frame_adapter>(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
+               CComPtr<IDeckLinkVideoFrame> frame2(new decklink_frame(frame, format_desc_, config_.key_only));\r
+               if(FAILED(output_->ScheduleVideoFrame(frame2, (frames_scheduled_++) * format_desc_.duration, format_desc_.duration, format_desc_.time_scale)))\r
                        CASPAR_LOG(error) << print() << L" Failed to schedule video.";\r
 \r
                graph_->update_value("tick-time", tick_timer_.elapsed()*format_desc_.fps*0.5);\r
                tick_timer_.restart();\r
        }\r
 \r
-       void send(const safe_ptr<const core::read_frame>& frame)\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
@@ -373,47 +409,50 @@ 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
 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
+       {\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 void send(const safe_ptr<const core::read_frame>& frame)\r
+       virtual bool send(const safe_ptr<core::read_frame>& frame)\r
        {\r
                context_->send(frame);\r
+               return true;\r
        }\r
        \r
        virtual std::wstring print() const\r
        {\r
-               return context_->print();\r
-       }\r
-\r
-       virtual bool key_only() const\r
-       {\r
-               return config_.key_only;\r
+               return context_ ? context_->print() : L"decklink_consumer";\r
        }\r
-               \r
+                       \r
        virtual const core::video_format_desc& get_video_format_desc() const\r
        {\r
-               return context_->get_video_format_desc();\r
-       }\r
-\r
-       virtual size_t buffer_depth() const\r
-       {\r
-               return context_->buffer_size_;\r
+               return format_desc_;\r
        }\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
@@ -423,7 +462,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
@@ -431,11 +470,11 @@ 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
-       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
@@ -444,7 +483,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