]> git.sesse.net Git - casparcg/blobdiff - modules/decklink/consumer/decklink_consumer.cpp
2.0. Optimized key-only.
[casparcg] / modules / decklink / consumer / decklink_consumer.cpp
index 86019eb4c37dd82f3da5922bfa73b117d7da4f45..7699f7fd846894dcb91150690de41776013fca30 100644 (file)
 \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/executor.h>\r
+#include <common/concurrency/com_context.h>\r
 #include <common/diagnostics/graph.h>\r
 #include <common/exception/exceptions.h>\r
-#include <common/utility/timer.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
 \r
-#include <array>\r
+namespace caspar { \r
+       \r
+struct configuration\r
+{\r
+       size_t  device_index;\r
+       bool    embedded_audio;\r
+       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
+};\r
 \r
-#pragma warning(push)\r
-#pragma warning(disable : 4996)\r
+class decklink_frame : public IDeckLinkVideoFrame\r
+{\r
+       const std::shared_ptr<core::read_frame>                                         frame_;\r
+       const core::video_format_desc                                                           format_desc_;\r
 \r
-       #include <atlbase.h>\r
+       bool                                                                                                            key_only_;\r
+       std::vector<uint8_t, tbb::cache_aligned_allocator<uint8_t>> key_data_;\r
+public:\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
+               , key_only_(key_only){}\r
+       \r
+       STDMETHOD (QueryInterface(REFIID, LPVOID*))             {return E_NOINTERFACE;}\r
+       STDMETHOD_(ULONG,                       AddRef())                       {return 1;}\r
+       STDMETHOD_(ULONG,                       Release())                      {return 1;}\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
+        \r
+    STDMETHOD(GetBytes(void** buffer))\r
+       {\r
+               static std::vector<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
-       #include <atlcom.h>\r
-       #include <atlhost.h>\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
+                       }\r
+                       *buffer = key_data_.data();\r
+               }\r
 \r
-#pragma warning(push)\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
 \r
-namespace caspar { \r
-       \r
-struct decklink_output : public IDeckLinkVideoOutputCallback, public IDeckLinkAudioOutputCallback, boost::noncopyable\r
+struct decklink_consumer : public IDeckLinkVideoOutputCallback, public IDeckLinkAudioOutputCallback, boost::noncopyable\r
 {              \r
-       struct co_init\r
-       {\r
-               co_init(){CoInitialize(nullptr);}\r
-               ~co_init(){CoUninitialize();}\r
-       } co_;\r
-       \r
-       const size_t    device_index_;\r
-       const bool              embed_audio_;\r
-       const decklink_consumer::key            key_;\r
+       const configuration                                     config_;\r
 \r
-       std::wstring    model_name_;\r
-       tbb::atomic<bool> is_running_;\r
+       CComPtr<IDeckLink>                                      decklink_;\r
+       CComQIPtr<IDeckLinkOutput>                      output_;\r
+       CComQIPtr<IDeckLinkConfiguration>       configuration_;\r
+       CComQIPtr<IDeckLinkKeyer>                       keyer_;\r
 \r
-       std::shared_ptr<diagnostics::graph> graph_;\r
-       boost::timer perf_timer_;\r
+       tbb::spin_mutex                                         exception_mutex_;\r
+       std::exception_ptr                                      exception_;\r
 \r
-       std::array<std::pair<void*, CComPtr<IDeckLinkMutableVideoFrame>>, 3> reserved_frames_;\r
-       boost::circular_buffer<std::vector<short>> audio_container_;\r
-       \r
-       CComPtr<IDeckLink>                      decklink_;\r
-       CComQIPtr<IDeckLinkOutput>      output_;\r
-       \r
-       core::video_format_desc format_desc_;\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
+\r
+       long long                                                       frames_scheduled_;\r
+       long long                                                       audio_scheduled_;\r
+\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
 \r
-       BMDTimeScale frame_time_scale_;\r
-       BMDTimeValue frame_duration_;\r
-       unsigned long frames_scheduled_;\r
-       unsigned long audio_scheduled_;\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
-       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
+       std::shared_ptr<diagnostics::graph> graph_;\r
+       boost::timer tick_timer_;\r
 \r
 public:\r
-       decklink_output(const core::video_format_desc& format_desc,size_t device_index, bool embed_audio, decklink_consumer::key key) \r
-               :  model_name_(L"DECKLINK")\r
-               , device_index_(device_index)\r
-               , audio_container_(5)\r
-               , embed_audio_(embed_audio)\r
-               , key_(key)\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
+               , configuration_(decklink_)\r
+               , 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
                , frames_scheduled_(0)\r
                , audio_scheduled_(0)\r
-               , format_desc_(format_desc)\r
+               , preroll_count_(0)\r
+               , audio_container_(buffer_size_+1)\r
        {\r
                is_running_ = true;\r
-               format_desc_ = format_desc;\r
-               CComPtr<IDeckLinkIterator> pDecklinkIterator;\r
-               if(FAILED(pDecklinkIterator.CoCreateInstance(CLSID_CDeckLinkIterator)))\r
-                       BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " No Decklink drivers installed."));\r
-               \r
-               size_t n = 0;\r
-               while(n < device_index_ && pDecklinkIterator->Next(&decklink_) == S_OK){++n;}   \r
-\r
-               if(n != device_index_ || !decklink_)\r
-                       BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Decklink card not found.") << arg_name_info("device_index") << arg_value_info(boost::lexical_cast<std::string>(device_index_)));\r
-               \r
-               BSTR pModelName;\r
-               decklink_->GetModelName(&pModelName);\r
-               model_name_ = std::wstring(pModelName);\r
                                \r
+               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.4f, 0.3f, 0.8f));\r
                \r
-               output_ = decklink_;\r
+               enable_video(get_display_mode(output_, format_desc_.format, bmdFormat8BitBGRA, bmdVideoOutputFlagDefault));\r
+                               \r
+               if(config.embedded_audio)\r
+                       enable_audio();\r
 \r
-               auto display_mode = get_display_mode(output_.p, format_desc_.format);\r
-               if(display_mode == nullptr) \r
-                       BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Card does not support requested videoformat."));\r
+               set_latency(config.low_latency);                                \r
+               set_keyer(config.internal_key);\r
+                               \r
+               if(config.embedded_audio)               \r
+                       output_->BeginAudioPreroll();           \r
                \r
-               display_mode->GetFrameRate(&frame_duration_, &frame_time_scale_);\r
+               for(size_t n = 0; n < buffer_size_; ++n)\r
+                       schedule_next_video(make_safe<core::read_frame>());\r
 \r
-               BMDDisplayModeSupport displayModeSupport;\r
-               if(FAILED(output_->DoesSupportVideoMode(display_mode->GetDisplayMode(), bmdFormat8BitBGRA, bmdVideoOutputFlagDefault, &displayModeSupport, nullptr)))\r
-                       BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Card does not support requested videoformat."));\r
-               \r
-               if(embed_audio_)\r
-               {\r
-                       if(FAILED(output_->EnableAudioOutput(bmdAudioSampleRate48kHz, bmdAudioSampleType16bitInteger, 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
-                               BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Could not set audio callback."));\r
+               if(!config.embedded_audio)\r
+                       start_playback();\r
+       }\r
 \r
-                       CASPAR_LOG(info) << print() << L" Enabled embedded-audio.";\r
+       ~decklink_consumer()\r
+       {               \r
+               is_running_ = false;\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
+                       output_->StopScheduledPlayback(0, nullptr, 0);\r
+                       if(config_.embedded_audio)\r
+                               output_->DisableAudioOutput();\r
+                       output_->DisableVideoOutput();\r
                }\r
+       }\r
+                       \r
+       const core::video_format_desc& get_video_format_desc() const\r
+       {\r
+               return format_desc_;\r
+       }\r
 \r
-               if(FAILED(output_->EnableVideoOutput(display_mode->GetDisplayMode(), bmdVideoOutputFlagDefault))) \r
-                       BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Could not enable video output."));\r
+       void set_latency(bool low_latency)\r
+       {               \r
+               if(!low_latency)\r
+               {\r
+                       configuration_->SetFlag(bmdDeckLinkConfigLowLatencyVideoOutput, false);\r
+                       CASPAR_LOG(info) << print() << L" Enabled normal-latency mode";\r
+               }\r
+               else\r
+               {                       \r
+                       configuration_->SetFlag(bmdDeckLinkConfigLowLatencyVideoOutput, true);\r
+                       CASPAR_LOG(info) << print() << L" Enabled low-latency mode";\r
+               }\r
+       }\r
 \r
-               if(FAILED(output_->SetScheduledFrameCompletionCallback(this)))\r
-                       BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Failed to set playback completion callback."));\r
-                       \r
-               CComQIPtr<IDeckLinkKeyer> keyer = decklink_;\r
-               if(key_ == decklink_consumer::internal_key) \r
+       void set_keyer(bool internal_key)\r
+       {\r
+               if(internal_key) \r
                {\r
-                       if(FAILED(keyer->Enable(FALSE)))                        \r
+                       if(FAILED(keyer_->Enable(FALSE)))                       \r
                                CASPAR_LOG(error) << print() << L" Failed to enable internal keyer.";                   \r
-                       else if(FAILED(keyer->SetLevel(255)))                   \r
+                       else if(FAILED(keyer_->SetLevel(255)))                  \r
                                CASPAR_LOG(error) << print() << L" Failed to set key-level to max.";\r
                        else\r
-                               CASPAR_LOG(info) << print() << L" Successfully configured internal keyer.";             \r
+                               CASPAR_LOG(info) << print() << L" Enabled internal keyer.";             \r
                }\r
-               else if(key_ == decklink_consumer::external_key)\r
+               else\r
                {\r
-                       if(FAILED(keyer->Enable(TRUE)))                 \r
+                       if(FAILED(keyer_->Enable(TRUE)))                        \r
                                CASPAR_LOG(error) << print() << L" Failed to enable external keyer.";   \r
-                       else if(FAILED(keyer->SetLevel(255)))                   \r
+                       else if(FAILED(keyer_->SetLevel(255)))                  \r
                                CASPAR_LOG(error) << print() << L" Failed to set key-level to max.";\r
                        else\r
-                               CASPAR_LOG(info) << print() << L" Successfully configured external keyer.";                     \r
+                               CASPAR_LOG(info) << print() << L" Enabled external keyer.";                     \r
                }\r
-               else\r
-                               CASPAR_LOG(info) << print() << L" Uses default keyer settings.";        \r
+       }\r
+       \r
+       void enable_audio()\r
+       {\r
+               if(FAILED(output_->EnableAudioOutput(bmdAudioSampleRate48kHz, bmdAudioSampleType16bitInteger, 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
+                       BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Could not set audio callback."));\r
 \r
-               \r
-               for(size_t n = 0; n < reserved_frames_.size(); ++n)\r
-               {\r
-                       if(FAILED(output_->CreateVideoFrame(format_desc_.width, format_desc_.height, format_desc_.size/format_desc_.height, bmdFormat8BitBGRA, bmdFrameFlagDefault, &reserved_frames_[n].second)))\r
-                               BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Failed to create frame."));\r
+               CASPAR_LOG(info) << print() << L" Enabled embedded-audio.";\r
+       }\r
 \r
-                       if(FAILED(reserved_frames_[n].second->GetBytes(&reserved_frames_[n].first)))\r
-                               BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Failed to get frame bytes."));\r
-               }\r
-                                       \r
-               auto buffer_size = static_cast<size_t>(frame_time_scale_/frame_duration_)/4;\r
-               for(size_t n = 0; n < buffer_size; ++n)\r
-                       schedule_next_video(core::read_frame::empty());\r
-\r
-               video_frame_buffer_.set_capacity(buffer_size);\r
-               audio_frame_buffer_.set_capacity(buffer_size);\r
-               for(size_t n = 0; n < std::max<size_t>(2, buffer_size-2); ++n)\r
-               {\r
-                       video_frame_buffer_.try_push(core::read_frame::empty());\r
-                       if(embed_audio_)\r
-                               audio_frame_buffer_.try_push(core::read_frame::empty());\r
-               }\r
-               \r
-               if(FAILED(output_->StartScheduledPlayback(0, frame_time_scale_, 1.0))) \r
-                       BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Failed to schedule playback."));\r
+       void enable_video(BMDDisplayMode display_mode)\r
+       {\r
+               if(FAILED(output_->EnableVideoOutput(display_mode, bmdVideoOutputFlagDefault))) \r
+                       BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Could not enable video output."));\r
                \r
-               CASPAR_LOG(info) << print() << L" Successfully initialized for " << format_desc_.name;  \r
+               if(FAILED(output_->SetScheduledFrameCompletionCallback(this)))\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
-       ~decklink_output()\r
-       {               \r
-               is_running_ = false;\r
-               video_frame_buffer_.try_push(core::read_frame::empty());\r
-               audio_frame_buffer_.try_push(core::read_frame::empty());\r
-\r
-               if(output_ != nullptr) \r
-               {\r
-                       output_->StopScheduledPlayback(0, nullptr, 0);\r
-                       if(embed_audio_)\r
-                               output_->DisableAudioOutput();\r
-                       output_->DisableVideoOutput();\r
-               }\r
-               CASPAR_LOG(info) << print() << L" Shutting down.";      \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
-       virtual HRESULT STDMETHODCALLTYPE       QueryInterface (REFIID, LPVOID*)        {return E_NOINTERFACE;}\r
-       virtual ULONG STDMETHODCALLTYPE         AddRef ()                                                       {return 1;}\r
-       virtual ULONG STDMETHODCALLTYPE         Release ()                                                      {return 1;}\r
        \r
-       virtual HRESULT STDMETHODCALLTYPE ScheduledFrameCompleted (IDeckLinkVideoFrame* /*completedFrame*/, BMDOutputFrameCompletionResult /*result*/)\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
-               if(!is_running_)\r
-                       return S_OK;\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
-\r
+               is_running_ = false;\r
+               CASPAR_LOG(info) << print() << L" Scheduled playback has stopped.";\r
                return S_OK;\r
        }\r
 \r
-       virtual HRESULT STDMETHODCALLTYPE ScheduledPlaybackHasStopped (void)\r
+       STDMETHOD(ScheduledFrameCompleted(IDeckLinkVideoFrame* completed_frame, BMDOutputFrameCompletionResult result))\r
        {\r
+               if(!is_running_)\r
+                       return E_FAIL;\r
+               \r
+               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<core::read_frame> frame;        \r
+                       video_frame_buffer_.pop(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
+\r
                return S_OK;\r
        }\r
                \r
-       virtual HRESULT STDMETHODCALLTYPE RenderAudioSamples (BOOL /*preroll*/)\r
+       STDMETHOD(RenderAudioSamples(BOOL preroll))\r
        {\r
                if(!is_running_)\r
-                       return S_OK;\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
+                       return E_FAIL;\r
+               \r
+               try\r
+               {       \r
+                       if(preroll)\r
+                       {\r
+                               if(++preroll_count_ >= buffer_size_)\r
+                               {\r
+                                       output_->EndAudioPreroll();\r
+                                       start_playback();                               \r
+                               }\r
+                               else\r
+                                       schedule_next_audio(make_safe<core::read_frame>());     \r
+                       }\r
+                       else\r
+                       {\r
+                               std::shared_ptr<core::read_frame> frame;\r
+                               audio_frame_buffer_.pop(frame);\r
+                               schedule_next_audio(make_safe(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
 \r
                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
-               int audio_samples = static_cast<size_t>(48000.0 / format_desc_.fps);\r
-\r
-               auto frame_audio_data = frame->audio_data().empty() ? silence.data() : const_cast<short*>(frame->audio_data().begin());\r
+               const int sample_frame_count = frame->audio_data().size()/format_desc_.audio_channels;\r
 \r
-               audio_container_.push_back(std::vector<short>(frame_audio_data, frame_audio_data+audio_samples*2));\r
+               audio_container_.push_back(std::vector<int16_t>(frame->audio_data().begin(), frame->audio_data().end()));\r
 \r
-               if(FAILED(output_->ScheduleAudioSamples(audio_container_.back().data(), audio_samples, (audio_scheduled_++) * audio_samples, 48000, nullptr)))\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
-               if(!frame->image_data().empty())\r
-                       std::copy(frame->image_data().begin(), frame->image_data().end(), static_cast<char*>(reserved_frames_.front().first));\r
-               else\r
-                       std::fill_n(static_cast<int*>(reserved_frames_.front().first), 0, format_desc_.size/4);\r
-\r
-               if(FAILED(output_->ScheduleVideoFrame(reserved_frames_.front().second, (frames_scheduled_++) * frame_duration_, frame_duration_, frame_time_scale_)))\r
+               frame_container_.push_back(std::make_shared<decklink_frame>(frame, format_desc_, config_.key_only));\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
-               std::rotate(reserved_frames_.begin(), reserved_frames_.begin() + 1, reserved_frames_.end());\r
-               graph_->update_value("tick-time", static_cast<float>(perf_timer_.elapsed()/format_desc_.interval*0.5));\r
-               perf_timer_.restart();\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
-               video_frame_buffer_.push(frame);\r
-               if(embed_audio_)\r
-                       audio_frame_buffer_.push(frame);\r
-       }\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
+               \r
+               if(config_.embedded_audio)\r
+                       audio_frame_buffer_.push(frame);        \r
+               video_frame_buffer_.push(frame);        \r
+       }\r
+       \r
        std::wstring print() const\r
        {\r
-               return model_name_ + L" [" + boost::lexical_cast<std::wstring>(device_index_) + L"]";\r
+               return model_name_ + L" [" + boost::lexical_cast<std::wstring>(config_.device_index) + L"|" +  format_desc_.name + L"]";\r
        }\r
 };\r
 \r
-struct decklink_consumer::implementation\r
+struct decklink_consumer_proxy : public core::frame_consumer\r
 {\r
-       std::unique_ptr<decklink_output> input_;\r
-       size_t device_index_;\r
-       bool embed_audio_;\r
-       decklink_consumer::key key_;\r
-\r
-       executor executor_;\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
-       implementation(size_t device_index, bool embed_audio, decklink_consumer::key key)\r
-               : device_index_(device_index)\r
-               , embed_audio_(embed_audio)\r
-               , key_(key)\r
-               , executor_(L"DECKLINK[" + boost::lexical_cast<std::wstring>(device_index) + L"]")\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
+               , fail_count_(0)\r
        {\r
-               executor_.start();\r
        }\r
 \r
-       ~implementation()\r
+       ~decklink_consumer_proxy()\r
        {\r
-               executor_.invoke([&]\r
-               {\r
-                       input_ = nullptr;\r
-               });\r
+               auto str = print();\r
+               context_.reset();\r
+               CASPAR_LOG(info) << str << L" Successfully Uninitialized.";     \r
        }\r
-\r
-       void initialize(const core::video_format_desc& format_desc)\r
+       \r
+       virtual void initialize(const core::video_format_desc& format_desc)\r
        {\r
-               executor_.invoke([&]\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
+               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
-                       input_.reset(new decklink_output(format_desc, device_index_, embed_audio_, key_));\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
-       void send(const safe_ptr<const core::read_frame>& frame)\r
+       virtual std::wstring print() const\r
        {\r
-               input_->send(frame);\r
+               return context_ ? context_->print() : L"decklink_consumer";\r
        }\r
 \r
-       size_t buffer_depth() const\r
+       virtual bool key_only() const\r
        {\r
-               return 1;\r
+               return config_.key_only;\r
        }\r
-\r
-       std::wstring print() const\r
+               \r
+       virtual const core::video_format_desc& get_video_format_desc() const\r
        {\r
-               return input_->print();\r
+               return format_desc_;\r
        }\r
-};\r
+};     \r
 \r
-decklink_consumer::decklink_consumer(size_t device_index, bool embed_audio, key key) : impl_(new implementation(device_index, embed_audio, key)){}\r
-decklink_consumer::decklink_consumer(decklink_consumer&& other) : impl_(std::move(other.impl_)){}\r
-void decklink_consumer::initialize(const core::video_format_desc& format_desc){impl_->initialize(format_desc);}\r
-void decklink_consumer::send(const safe_ptr<const core::read_frame>& frame){impl_->send(frame);}\r
-size_t decklink_consumer::buffer_depth() const{return impl_->buffer_depth();}\r
-std::wstring decklink_consumer::print() const{return impl_->print();}\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(const std::vector<std::wstring>& params) \r
 {\r
        if(params.size() < 1 || params[0] != L"DECKLINK")\r
                return core::frame_consumer::empty();\r
        \r
-       int device_index = 1;\r
-       bool embed_audio = false;\r
-       auto key = decklink_consumer::default_key;\r
+       configuration config;\r
+               \r
+       if(params.size() > 1)\r
+               config.device_index = lexical_cast_or_default<int>(params[1], config.device_index);\r
+       \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
-       if(params.size() > 1) \r
-               device_index = lexical_cast_or_default<int>(params[2], device_index);\r
+       return make_safe<decklink_consumer_proxy>(config);\r
+}\r
 \r
-       if(params.size() > 2)\r
-               embed_audio = lexical_cast_or_default<bool>(params[3], embed_audio);\r
-       \r
+safe_ptr<core::frame_consumer> create_decklink_consumer(const boost::property_tree::ptree& ptree) \r
+{\r
+       configuration config;\r
 \r
-       if(params.size() > 3) \r
-       {\r
-               if(params[4] == L"INTERNAL_KEY")\r
-                       key = decklink_consumer::internal_key;\r
-               else if(params[4] == L"EXTERNAL_KEY")\r
-                       key = decklink_consumer::external_key;\r
-       }\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
+       config.embedded_audio   = ptree.get("embedded-audio", config.embedded_audio);\r
 \r
-       return make_safe<decklink_consumer>(device_index, embed_audio, key);\r
+       return make_safe<decklink_consumer_proxy>(config);\r
 }\r
 \r
-}
\ No newline at end of file
+}\r
+\r
+/*\r
+##############################################################################\r
+Pre-rolling\r
+\r
+Mail: 2011-05-09\r
+\r
+Yoshan\r
+BMD Developer Support\r
+developer@blackmagic-design.com\r
+\r
+-----------------------------------------------------------------------------\r
+\r
+Thanks for your inquiry. The minimum number of frames that you can preroll \r
+for scheduled playback is three frames for video and four frames for audio. \r
+As you mentioned if you preroll less frames then playback will not start or\r
+playback will be very sporadic. From our experience with Media Express, we \r
+recommended that at least seven frames are prerolled for smooth playback. \r
+\r
+Regarding the bmdDeckLinkConfigLowLatencyVideoOutput flag:\r
+There can be around 3 frames worth of latency on scheduled output.\r
+When the bmdDeckLinkConfigLowLatencyVideoOutput flag is used this latency is\r
+reduced  or removed for scheduled playback. If the DisplayVideoFrameSync() \r
+method is used, the bmdDeckLinkConfigLowLatencyVideoOutput setting will \r
+guarantee that the provided frame will be output as soon the previous \r
+frame output has been completed.\r
+################################################################################\r
+*/\r
+\r
+/*\r
+##############################################################################\r
+Async DMA Transfer without redundant copying\r
+\r
+Mail: 2011-05-10\r
+\r
+Yoshan\r
+BMD Developer Support\r
+developer@blackmagic-design.com\r
+\r
+-----------------------------------------------------------------------------\r
+\r
+Thanks for your inquiry. You could try subclassing IDeckLinkMutableVideoFrame \r
+and providing a pointer to your video buffer when GetBytes() is called. \r
+This may help to keep copying to a minimum. Please ensure that the pixel \r
+format is in bmdFormat10BitYUV, otherwise the DeckLink API / driver will \r
+have to colourspace convert which may result in additional copying.\r
+################################################################################\r
+*/
\ No newline at end of file