]> git.sesse.net Git - casparcg/blobdiff - modules/decklink/consumer/decklink_consumer.cpp
2.0. flash_producer: Improved error handling.
[casparcg] / modules / decklink / consumer / decklink_consumer.cpp
index 68e2d6948f8aa4dc4ee27e333f1398acb162e1f2..b4c63c6552044f5b34117cc96049fb802b8bb57f 100644 (file)
@@ -35,6 +35,8 @@
 #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
 \r
 #include <boost/circular_buffer.hpp>\r
@@ -57,15 +59,15 @@ struct configuration
                , external_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
+       const safe_ptr<core::read_frame>        frame_;\r
+       const core::video_format_desc           format_desc_;\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)\r
                : frame_(frame)\r
                , format_desc_(format_desc){}\r
        \r
@@ -81,8 +83,8 @@ public:
         \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> zeros(1920*1080*4, 0);\r
+               *buffer = const_cast<uint8_t*>(frame_->image_data().begin());\r
                if(static_cast<size_t>(frame_->image_data().size()) != format_desc_.size)\r
                        *buffer = zeros.data();\r
                return S_OK;\r
@@ -101,24 +103,25 @@ struct decklink_consumer : public IDeckLinkVideoOutputCallback, public IDeckLink
        CComQIPtr<IDeckLinkConfiguration>       configuration_;\r
        CComQIPtr<IDeckLinkKeyer>                       keyer_;\r
 \r
-       std::exception_ptr exception_;\r
+       tbb::spin_mutex                                         exception_mutex_;\r
+       std::exception_ptr                                      exception_;\r
 \r
-       tbb::atomic<bool> is_running_;\r
+       tbb::atomic<bool>                                       is_running_;\r
                \r
-       const std::wstring model_name_;\r
-       const core::video_format_desc format_desc_;\r
-       const size_t buffer_size_;\r
+       const std::wstring                                      model_name_;\r
+       const core::video_format_desc           format_desc_;\r
+       const size_t                                            buffer_size_;\r
 \r
-       unsigned long frames_scheduled_;\r
-       unsigned long audio_scheduled_;\r
+       long long                                                       frames_scheduled_;\r
+       long long                                                       audio_scheduled_;\r
 \r
-       size_t preroll_count_;\r
+       size_t                                                          preroll_count_;\r
                \r
        std::list<std::shared_ptr<IDeckLinkVideoFrame>> frame_container_; // Must be std::list in order to guarantee that pointers are always valid.\r
-       boost::circular_buffer<std::vector<short>> audio_container_;\r
+       boost::circular_buffer<std::vector<int16_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
@@ -243,7 +246,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,7 +276,11 @@ 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
@@ -282,12 +291,13 @@ public:
                                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
                }\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 +324,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
                        }\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,23 +339,19 @@ 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<int16_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
+               frame_container_.push_back(std::make_shared<decklink_frame>(frame, format_desc_));\r
                if(FAILED(output_->ScheduleVideoFrame(frame_container_.back().get(), (frames_scheduled_++) * format_desc_.duration, format_desc_.duration, format_desc_.time_scale)))\r
                        CASPAR_LOG(error) << print() << L" Failed to schedule video.";\r
 \r
@@ -352,10 +359,13 @@ public:
                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
@@ -387,9 +397,10 @@ public:
                context_.reset([&]{return new decklink_consumer(config_, format_desc);});\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
@@ -406,11 +417,6 @@ public:
        {\r
                return context_->get_video_format_desc();\r
        }\r
-\r
-       virtual size_t buffer_depth() const\r
-       {\r
-               return context_->buffer_size_;\r
-       }\r
 };     \r
 \r
 safe_ptr<core::frame_consumer> create_decklink_consumer(const std::vector<std::wstring>& params) \r