]> git.sesse.net Git - casparcg/blobdiff - modules/decklink/consumer/decklink_consumer.cpp
decklink_consumer: Minor refactoring
[casparcg] / modules / decklink / consumer / decklink_consumer.cpp
index 5218dc834c757afa5532310c3c13d5db6ea03514..bddcf909850bdbe5678e184ae0b00be654cefedb 100644 (file)
@@ -1,21 +1,22 @@
 /*\r
-* copyright (c) 2010 Sveriges Television AB <info@casparcg.com>\r
+* Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>\r
 *\r
-*  This file is part of CasparCG.\r
+* This file is part of CasparCG (www.casparcg.com).\r
 *\r
-*    CasparCG is free software: you can redistribute it and/or modify\r
-*    it under the terms of the GNU General Public License as published by\r
-*    the Free Software Foundation, either version 3 of the License, or\r
-*    (at your option) any later version.\r
+* CasparCG is free software: you can redistribute it and/or modify\r
+* it under the terms of the GNU General Public License as published by\r
+* the Free Software Foundation, either version 3 of the License, or\r
+* (at your option) any later version.\r
 *\r
-*    CasparCG is distributed in the hope that it will be useful,\r
-*    but WITHOUT ANY WARRANTY; without even the implied warranty of\r
-*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
-*    GNU General Public License for more details.\r
-\r
-*    You should have received a copy of the GNU General Public License\r
-*    along with CasparCG.  If not, see <http://www.gnu.org/licenses/>.\r
+* CasparCG is distributed in the hope that it will be useful,\r
+* but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
+* GNU General Public License for more details.\r
+*\r
+* You should have received a copy of the GNU General Public License\r
+* along with CasparCG. If not, see <http://www.gnu.org/licenses/>.\r
 *\r
+* Author: Robert Nagy, ronag89@gmail.com\r
 */\r
 \r
 #include "../StdAfx.h"\r
 \r
 #include "../interop/DeckLinkAPI_h.h"\r
 \r
-#include <core/video_format.h>\r
+#include <core/mixer/read_frame.h>\r
 \r
-#include <core/consumer/frame/read_frame.h>\r
-\r
-#include <common/concurrency/com_context.h>\r
+#include <common/concurrency/executor.h>\r
+#include <common/concurrency/lock.h>\r
 #include <common/diagnostics/graph.h>\r
 #include <common/exception/exceptions.h>\r
 #include <common/memory/memcpy.h>\r
 #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
+#include <boost/property_tree/ptree.hpp>\r
 \r
-#include <vector>\r
-\r
-#pragma warning(push)\r
-#pragma warning(disable : 4996)\r
-\r
-       #include <atlbase.h>\r
-\r
-       #include <atlcom.h>\r
-       #include <atlhost.h>\r
-\r
-#pragma warning(push)\r
-\r
-namespace caspar { \r
+namespace caspar { namespace decklink { \r
        \r
-enum key\r
-{\r
-       external_key,\r
-       internal_key,\r
-       default_key\r
-};\r
-\r
-enum latency\r
-{\r
-       low_latency,\r
-       normal_latency,\r
-       default_latency\r
-};\r
-\r
 struct configuration\r
 {\r
-       size_t device_index;\r
-       bool embedded_audio;\r
-       key keyer;\r
-       latency latency;\r
+       size_t  device_index;\r
+       bool    embedded_audio;\r
+       bool    internal_key;\r
+       bool    low_latency;\r
+       bool    key_only;\r
+       size_t  base_buffer_depth;\r
+       size_t  buffer_depth;\r
        \r
        configuration()\r
                : device_index(1)\r
                , embedded_audio(false)\r
-               , keyer(default_key)\r
-               , latency(default_latency){}\r
+               , internal_key(false)\r
+               , low_latency(true)\r
+               , key_only(false)\r
+               , base_buffer_depth(3)\r
+               , buffer_depth(base_buffer_depth + (low_latency ? 0 : 1) + (embedded_audio ? 1 : 0)){}\r
 };\r
 \r
-class decklink_frame_adapter : public IDeckLinkVideoFrame\r
+class decklink_frame : public IDeckLinkVideoFrame\r
 {\r
-       safe_ptr<const core::read_frame> frame_;\r
-       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
+       const bool                                                                                                      key_only_;\r
+       std::vector<uint8_t, tbb::cache_aligned_allocator<uint8_t>> 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
+       // IUnknown\r
+\r
+       STDMETHOD (QueryInterface(REFIID, LPVOID*))             \r
+       {\r
+               return E_NOINTERFACE;\r
+       }\r
+       \r
+       STDMETHOD_(ULONG,                       AddRef())                       \r
+       {\r
+               return ++ref_count_;\r
+       }\r
+\r
+       STDMETHOD_(ULONG,                       Release())                      \r
+       {\r
+               if(--ref_count_ == 0)\r
+                       delete this;\r
+               return ref_count_;\r
+       }\r
+\r
+       // IDecklinkVideoFrame\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
-               if(static_cast<size_t>(frame_->image_data().size()) != format_desc_.size)\r
-                       *buffer = zeros.data();\r
+               try\r
+               {\r
+                       if(static_cast<size_t>(frame_->image_data().size()) != format_desc_.size)\r
+                       {\r
+                               data_.resize(format_desc_.size, 0);\r
+                               *buffer = data_.data();\r
+                       }\r
+                       else if(key_only_)\r
+                       {\r
+                               if(data_.empty())\r
+                               {\r
+                                       data_.resize(frame_->image_data().size());\r
+                                       fast_memshfl(data_.data(), frame_->image_data().begin(), frame_->image_data().size(), 0x0F0F0F0F, 0x0B0B0B0B, 0x07070707, 0x03030303);\r
+                                       frame_.reset();\r
+                               }\r
+                               *buffer = data_.data();\r
+                       }\r
+                       else\r
+                               *buffer = const_cast<uint8_t*>(frame_->image_data().begin());\r
+               }\r
+               catch(...)\r
+               {\r
+                       CASPAR_LOG_CURRENT_EXCEPTION();\r
+                       return E_FAIL;\r
+               }\r
+\r
                return S_OK;\r
        }\r
         \r
     STDMETHOD(GetTimecode(BMDTimecodeFormat format, IDeckLinkTimecode** timecode)){return S_FALSE;}        \r
     STDMETHOD(GetAncillaryData(IDeckLinkVideoFrameAncillary** ancillary))                {return S_FALSE;}\r
+\r
+       // decklink_frame       \r
+\r
+       const boost::iterator_range<const int32_t*> audio_data()\r
+       {\r
+               return frame_->audio_data();\r
+       }\r
 };\r
 \r
 struct decklink_consumer : public IDeckLinkVideoOutputCallback, public IDeckLinkAudioOutputCallback, boost::noncopyable\r
 {              \r
-       const configuration config_;\r
+       const int                                                       channel_index_;\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
+       long long                                                       video_scheduled_;\r
+       long long                                                       audio_scheduled_;\r
 \r
-       unsigned long frames_scheduled_;\r
-       unsigned long audio_scheduled_;\r
+       size_t                                                          preroll_count_;\r
                \r
-       std::list<decklink_frame_adapter> 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
+       safe_ptr<diagnostics::graph> graph_;\r
        boost::timer tick_timer_;\r
 \r
-\r
 public:\r
-       decklink_consumer(const configuration& config, const core::video_format_desc& format_desc) \r
-               : config_(config)\r
+       decklink_consumer(const configuration& config, const core::video_format_desc& format_desc, int channel_index) \r
+               : channel_index_(channel_index)\r
+               , config_(config)\r
                , decklink_(get_device(config.device_index))\r
                , output_(decklink_)\r
                , configuration_(decklink_)\r
                , keyer_(decklink_)\r
                , model_name_(get_model_name(decklink_))\r
                , format_desc_(format_desc)\r
-               , buffer_size_(config.embedded_audio ? 5 : 4) // Minimum buffer-size (3 + 1 tolerance).\r
-               , frames_scheduled_(0)\r
+               , buffer_size_(config.buffer_depth) // Minimum buffer-size 3.\r
+               , video_scheduled_(0)\r
                , audio_scheduled_(0)\r
+               , preroll_count_(0)\r
                , audio_container_(buffer_size_+1)\r
        {\r
                is_running_ = true;\r
@@ -164,43 +207,38 @@ 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.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
+               graph_->set_color("buffered-audio", diagnostics::color(0.9f, 0.9f, 0.5f));\r
+               graph_->set_color("buffered-video", diagnostics::color(0.2f, 0.9f, 0.9f));\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
                if(config.embedded_audio)\r
                        enable_audio();\r
 \r
-               set_latency(config.latency);                            \r
-               set_keyer(config.keyer);\r
-                                                               \r
-               for(size_t n = 0; n < buffer_size_; ++n)\r
-                       schedule_next_video(core::read_frame::empty());\r
-               \r
-               if(config.embedded_audio)\r
-                       output_->BeginAudioPreroll();\r
-               else\r
-               {\r
-                       if(FAILED(output_->StartScheduledPlayback(0, format_desc_.time_scale, 1.0))) \r
-                               BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Failed to schedule playback."));\r
-               }\r
+               set_latency(config.low_latency);                                \r
+               set_keyer(config.internal_key);\r
+                               \r
+               if(config.embedded_audio)               \r
+                       output_->BeginAudioPreroll();           \r
                \r
-               CASPAR_LOG(info) << print() << L" Buffer depth: " << buffer_size_;              \r
-               CASPAR_LOG(info) << print() << L" Successfully initialized for " << format_desc_.name;  \r
+               for(size_t n = 0; n < buffer_size_; ++n)\r
+                       schedule_next_video(make_safe<core::read_frame>());\r
+\r
+               if(!config.embedded_audio)\r
+                       start_playback();\r
        }\r
 \r
        ~decklink_consumer()\r
        {               \r
                is_running_ = false;\r
-               video_frame_buffer_.clear();\r
-               audio_frame_buffer_.clear();\r
-               video_frame_buffer_.try_push(core::read_frame::empty());\r
-               audio_frame_buffer_.try_push(core::read_frame::empty());\r
+               video_frame_buffer_.try_push(std::make_shared<core::read_frame>());\r
+               audio_frame_buffer_.try_push(std::make_shared<core::read_frame>());\r
 \r
                if(output_ != nullptr) \r
                {\r
@@ -211,25 +249,23 @@ public:
                }\r
        }\r
                        \r
-       void set_latency(latency latency)\r
+       void set_latency(bool low_latency)\r
        {               \r
-               if(latency == normal_latency)\r
+               if(!low_latency)\r
                {\r
                        configuration_->SetFlag(bmdDeckLinkConfigLowLatencyVideoOutput, false);\r
                        CASPAR_LOG(info) << print() << L" Enabled normal-latency mode";\r
                }\r
-               else if(latency == low_latency)\r
+               else\r
                {                       \r
                        configuration_->SetFlag(bmdDeckLinkConfigLowLatencyVideoOutput, true);\r
                        CASPAR_LOG(info) << print() << L" Enabled low-latency mode";\r
                }\r
-               else\r
-                       CASPAR_LOG(info) << print() << L" Uses driver latency settings.";       \r
        }\r
 \r
-       void set_keyer(key keyer)\r
+       void set_keyer(bool internal_key)\r
        {\r
-               if(keyer == internal_key) \r
+               if(internal_key) \r
                {\r
                        if(FAILED(keyer_->Enable(FALSE)))                       \r
                                CASPAR_LOG(error) << print() << L" Failed to enable internal keyer.";                   \r
@@ -238,7 +274,7 @@ public:
                        else\r
                                CASPAR_LOG(info) << print() << L" Enabled internal keyer.";             \r
                }\r
-               else if(keyer == external_key)\r
+               else\r
                {\r
                        if(FAILED(keyer_->Enable(TRUE)))                        \r
                                CASPAR_LOG(error) << print() << L" Failed to enable external keyer.";   \r
@@ -247,13 +283,11 @@ public:
                        else\r
                                CASPAR_LOG(info) << print() << L" Enabled external keyer.";                     \r
                }\r
-               else\r
-                       CASPAR_LOG(info) << print() << L" Uses driver keyer settings."; \r
        }\r
        \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
@@ -268,13 +302,28 @@ 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
+       {\r
+               if(FAILED(output_->StartScheduledPlayback(0, format_desc_.time_scale, 1.0))) \r
+                       BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Failed to schedule playback."));\r
        }\r
        \r
        STDMETHOD (QueryInterface(REFIID, LPVOID*))     {return E_NOINTERFACE;}\r
        STDMETHOD_(ULONG, AddRef())                                     {return 1;}\r
        STDMETHOD_(ULONG, Release())                            {return 1;}\r
        \r
+       STDMETHOD(ScheduledPlaybackHasStopped())\r
+       {\r
+               is_running_ = false;\r
+               CASPAR_LOG(info) << print() << L" Scheduled playback has stopped.";\r
+               return S_OK;\r
+       }\r
+\r
        STDMETHOD(ScheduledFrameCompleted(IDeckLinkVideoFrame* completed_frame, BMDOutputFrameCompletionResult result))\r
        {\r
                if(!is_running_)\r
@@ -283,36 +332,38 @@ public:
                try\r
                {\r
                        if(result == bmdOutputFrameDisplayedLate)\r
-                               graph_->add_tag("late-frame");\r
+                       {\r
+                               graph_->set_tag("late-frame");\r
+                               video_scheduled_ += format_desc_.duration;\r
+                               audio_scheduled_ += reinterpret_cast<decklink_frame*>(completed_frame)->audio_data().size()/format_desc_.audio_channels;\r
+                               //++video_scheduled_;\r
+                               //audio_scheduled_ += format_desc_.audio_cadence[0];\r
+                               //++audio_scheduled_;\r
+                       }\r
                        else if(result == bmdOutputFrameDropped)\r
-                               graph_->add_tag("dropped-frame");\r
+                               graph_->set_tag("dropped-frame");\r
                        else if(result == bmdOutputFrameFlushed)\r
-                               graph_->add_tag("flushed-frame");\r
-\r
-                       frame_container_.erase(std::remove_if(frame_container_.begin(), frame_container_.end(), [&](const decklink_frame_adapter& frame)\r
-                       {\r
-                               return &frame == completed_frame;\r
-                       }), frame_container_.end());\r
+                               graph_->set_tag("flushed-frame");\r
 \r
-                       std::shared_ptr<const core::read_frame> frame;  \r
-                       video_frame_buffer_.pop(frame);         \r
-                       schedule_next_video(safe_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_ptr(frame));      \r
+                       \r
+                       unsigned long buffered;\r
+                       output_->GetBufferedVideoFrameCount(&buffered);\r
+                       graph_->set_value("buffered-video", static_cast<double>(buffered)/format_desc_.fps);\r
                }\r
                catch(...)\r
                {\r
-                       exception_ = std::current_exception();\r
+                       lock(exception_mutex_, [this]\r
+                       {\r
+                               exception_ = std::current_exception();\r
+                       });\r
                        return E_FAIL;\r
                }\r
 \r
                return S_OK;\r
        }\r
-\r
-       STDMETHOD(ScheduledPlaybackHasStopped())\r
-       {\r
-               is_running_ = false;\r
-               CASPAR_LOG(info) << print() << L" Scheduled playback has stopped.";\r
-               return S_OK;\r
-       }\r
                \r
        STDMETHOD(RenderAudioSamples(BOOL preroll))\r
        {\r
@@ -320,54 +371,75 @@ public:
                        return E_FAIL;\r
                \r
                try\r
-               {\r
+               {       \r
                        if(preroll)\r
                        {\r
-                               if(FAILED(output_->StartScheduledPlayback(0, format_desc_.time_scale, 1.0)))\r
-                                       BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Failed to schedule playback."));\r
+                               if(++preroll_count_ >= buffer_size_)\r
+                               {\r
+                                       output_->EndAudioPreroll();\r
+                                       start_playback();                               \r
+                               }\r
+                               else\r
+                                       schedule_next_audio(core::audio_buffer(format_desc_.audio_cadence[preroll % format_desc_.audio_cadence.size()], 0));    \r
+                       }\r
+                       else\r
+                       {\r
+                               std::shared_ptr<core::read_frame> frame;\r
+                               audio_frame_buffer_.pop(frame);\r
+                               schedule_next_audio(frame->audio_data());\r
                        }\r
 \r
-                       std::shared_ptr<const core::read_frame> frame;\r
-                       audio_frame_buffer_.pop(frame);\r
-                       schedule_next_audio(safe_ptr<const core::read_frame>(frame));           \r
+                       unsigned long buffered;\r
+                       output_->GetBufferedAudioSampleFrameCount(&buffered);\r
+                       graph_->set_value("buffered-audio", static_cast<double>(buffered)/(format_desc_.audio_cadence[0]*2));\r
                }\r
                catch(...)\r
                {\r
-                       exception_ = std::current_exception();\r
+                       lock(exception_mutex_, [this]\r
+                       {\r
+                               exception_ = std::current_exception();\r
+                       });\r
                        return E_FAIL;\r
                }\r
 \r
                return S_OK;\r
        }\r
 \r
-       void schedule_next_audio(const safe_ptr<const core::read_frame>& frame)\r
+       template<typename T>\r
+       void schedule_next_audio(const T& audio_data)\r
        {\r
-               static std::vector<short> silence(48000, 0);\r
-\r
-               int audio_samples = static_cast<size_t>(48000.0 / format_desc_.fps)*2; // Audio samples per channel\r
+               const int sample_frame_count = audio_data.size()/format_desc_.audio_channels;\r
 \r
-               const short* frame_audio_data = frame->audio_data().size() == audio_samples ? frame->audio_data().begin() : silence.data();\r
-               audio_container_.push_back(std::vector<short>(frame_audio_data, frame_audio_data+audio_samples));\r
+               audio_container_.push_back(std::vector<int32_t>(audio_data.begin(), audio_data.end()));\r
 \r
-               if(FAILED(output_->ScheduleAudioSamples(audio_container_.back().data(), audio_samples/2, (audio_scheduled_++) * audio_samples/2, 48000, nullptr)))\r
+               if(FAILED(output_->ScheduleAudioSamples(audio_container_.back().data(), sample_frame_count, audio_scheduled_, format_desc_.audio_sample_rate, nullptr)))\r
                        CASPAR_LOG(error) << print() << L" Failed to schedule audio.";\r
+\r
+               audio_scheduled_ += sample_frame_count;\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(decklink_frame_adapter(frame, format_desc_));\r
-               if(FAILED(output_->ScheduleVideoFrame(&frame_container_.back(), (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, video_scheduled_, 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
+               video_scheduled_ += format_desc_.duration;\r
+\r
+               graph_->set_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
+               auto exception = lock(exception_mutex_, [this]\r
+               {\r
+                       return exception_;\r
+               });\r
 \r
+               if(exception != nullptr)\r
+                       std::rethrow_exception(exception);\r
+               \r
                if(!is_running_)\r
                        BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Is not running."));\r
                \r
@@ -378,38 +450,99 @@ public:
        \r
        std::wstring print() const\r
        {\r
-               return model_name_ + L" [" + boost::lexical_cast<std::wstring>(config_.device_index) + L"]";\r
+               return model_name_ + L" [" + boost::lexical_cast<std::wstring>(channel_index_) + L"-" +\r
+                       boost::lexical_cast<std::wstring>(config_.device_index) + L"|" +  format_desc_.name + L"]";\r
        }\r
 };\r
 \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
+       std::unique_ptr<decklink_consumer>      consumer_;\r
+       std::vector<size_t>                                     audio_cadence_;\r
+       executor                                                        executor_;\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
+               , executor_(L"decklink_consumer[" + boost::lexical_cast<std::wstring>(config.device_index) + L"]")\r
+       {\r
+               executor_.begin_invoke([=]\r
+               {\r
+                       ::CoInitialize(nullptr);\r
+               });\r
+       }\r
+\r
+       ~decklink_consumer_proxy()\r
+       {\r
+               executor_.invoke([=]\r
+               {\r
+                       if(consumer_)\r
+                       {\r
+                               auto str = print();\r
+                               consumer_.reset();\r
+                               CASPAR_LOG(info) << str << L" Successfully Uninitialized.";     \r
+                       }\r
+               });\r
+               \r
+               executor_.invoke([=]\r
+               {\r
+                       ::CoUninitialize();\r
+               });\r
+       }\r
+\r
+       // frame_consumer\r
        \r
-       virtual void initialize(const core::video_format_desc& format_desc)\r
+       virtual void initialize(const core::video_format_desc& format_desc, int channel_index) override\r
        {\r
-               context_.reset([&]{return new decklink_consumer(config_, format_desc);});\r
+               executor_.invoke([=]\r
+               {\r
+                       consumer_.reset(new decklink_consumer(config_, format_desc, channel_index));            \r
+                       audio_cadence_ = format_desc.audio_cadence;             \r
+\r
+                       CASPAR_LOG(info) << print() << L" Successfully Initialized.";\r
+               });\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) override\r
        {\r
-               context_->send(frame);\r
+               CASPAR_VERIFY(audio_cadence_.front() == static_cast<size_t>(frame->audio_data().size()));\r
+               boost::range::rotate(audio_cadence_, std::begin(audio_cadence_)+1);\r
+\r
+               consumer_->send(frame);\r
+               return true;\r
        }\r
        \r
-       virtual std::wstring print() const\r
+       virtual std::wstring print() const override\r
+       {\r
+               return consumer_ ? consumer_->print() : L"[decklink_consumer]";\r
+       }               \r
+\r
+       virtual boost::property_tree::wptree info() const override\r
        {\r
-               return context_->print();\r
+               boost::property_tree::wptree info;\r
+               info.add(L"type", L"decklink-consumer");\r
+               info.add(L"key-only", config_.key_only);\r
+               info.add(L"device", config_.device_index);\r
+               info.add(L"low-latency", config_.low_latency);\r
+               info.add(L"embedded-audio", config_.embedded_audio);\r
+               info.add(L"low-latency", config_.low_latency);\r
+               info.add(L"internal-key", config_.internal_key);\r
+               return info;\r
+       }\r
+\r
+       virtual size_t buffer_depth() const override\r
+       {\r
+               return config_.buffer_depth;\r
+       }\r
+\r
+       virtual int index() const override\r
+       {\r
+               return 300 + config_.device_index;\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
@@ -418,45 +551,30 @@ safe_ptr<core::frame_consumer> create_decklink_consumer(const std::vector<std::w
                \r
        if(params.size() > 1)\r
                config.device_index = lexical_cast_or_default<int>(params[1], config.device_index);\r
-               \r
-       if(std::find(params.begin(), params.end(), L"INTERNAL_KEY") != params.end())\r
-               config.keyer = internal_key;\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
+       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
 \r
        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::wptree& ptree) \r
 {\r
        configuration config;\r
 \r
-       auto key_str = ptree.get("key", "default");\r
-       if(key_str == "internal")\r
-               config.keyer = internal_key;\r
-       else if(key_str == "external")\r
-               config.keyer = external_key;\r
-\r
-       auto latency_str = ptree.get("latency", "default");\r
-       if(latency_str == "normal")\r
-               config.latency = normal_latency;\r
-       else if(latency_str == "low")\r
-               config.latency = low_latency;\r
-\r
-       config.device_index = ptree.get("device", 0);\r
-       config.embedded_audio  = ptree.get("embedded-audio", false);\r
+       config.internal_key                     = ptree.get(L"internal-key",    config.internal_key);\r
+       config.low_latency                      = ptree.get(L"low-latency",             config.low_latency);\r
+       config.key_only                         = ptree.get(L"key-only",                config.key_only);\r
+       config.device_index                     = ptree.get(L"device",                  config.device_index);\r
+       config.embedded_audio           = ptree.get(L"embedded-audio",  config.embedded_audio);\r
+       config.base_buffer_depth        = ptree.get(L"buffer-depth",    config.base_buffer_depth);\r
 \r
        return make_safe<decklink_consumer_proxy>(config);\r
 }\r
 \r
-}\r
+}}\r
 \r
 /*\r
 ##############################################################################\r