]> git.sesse.net Git - casparcg/blobdiff - modules/decklink/producer/decklink_producer.cpp
git-svn-id: https://casparcg.svn.sourceforge.net/svnroot/casparcg/server/branches...
[casparcg] / modules / decklink / producer / decklink_producer.cpp
index c8304bddf07cc2973fddc0fb7e39d50183039eed..176e79f56ee79dd6394a27c1b285581d480702d4 100644 (file)
@@ -36,7 +36,7 @@
 #include <common/memory/memclr.h>\r
 \r
 #include <core/mixer/write_frame.h>\r
-#include <core/producer/frame/audio_transform.h>\r
+#include <core/producer/frame/frame_transform.h>\r
 #include <core/producer/frame/frame_factory.h>\r
 \r
 #include <tbb/concurrent_queue.h>\r
@@ -71,7 +71,7 @@ extern "C"
 \r
 #include <functional>\r
 \r
-namespace caspar { \r
+namespace caspar { namespace decklink {\r
                \r
 class decklink_producer : boost::noncopyable, public IDeckLinkInputCallback\r
 {      \r
@@ -82,7 +82,7 @@ class decklink_producer : boost::noncopyable, public IDeckLinkInputCallback
        const core::video_format_desc                                                           format_desc_;\r
        const size_t                                                                                            device_index_;\r
 \r
-       std::shared_ptr<diagnostics::graph>                                                     graph_;\r
+       safe_ptr<diagnostics::graph>                                                            graph_;\r
        boost::timer                                                                                            tick_timer_;\r
        boost::timer                                                                                            frame_timer_;\r
                \r
@@ -91,9 +91,9 @@ class decklink_producer : boost::noncopyable, public IDeckLinkInputCallback
        tbb::concurrent_bounded_queue<safe_ptr<core::basic_frame>>      frame_buffer_;\r
 \r
        std::exception_ptr                                                                                      exception_;\r
-       filter                                                                                                          filter_;\r
+       ffmpeg::filter                                                                                          filter_;\r
                \r
-       frame_muxer                                                                                                     muxer_;\r
+       ffmpeg::frame_muxer                                                                                     muxer_;\r
 \r
 public:\r
        decklink_producer(const core::video_format_desc& format_desc, size_t device_index, const safe_ptr<core::frame_factory>& frame_factory, const std::wstring& filter)\r
@@ -104,17 +104,18 @@ public:
                , device_index_(device_index)\r
                , frame_factory_(frame_factory)\r
                , filter_(filter)\r
-               , muxer_(double_rate(filter) ? format_desc.fps * 2.0 : format_desc.fps, frame_factory)\r
+               , muxer_(ffmpeg::double_rate(filter) ? format_desc.fps * 2.0 : format_desc.fps, frame_factory)\r
        {\r
                frame_buffer_.set_capacity(2);\r
                \r
-               graph_ = diagnostics::create_graph(boost::bind(&decklink_producer::print, this));\r
                graph_->add_guide("tick-time", 0.5);\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("frame-time", diagnostics::color(1.0f, 0.0f, 0.0f));\r
                graph_->set_color("dropped-frame", diagnostics::color(0.3f, 0.6f, 0.3f));\r
                graph_->set_color("output-buffer", diagnostics::color(0.0f, 1.0f, 0.0f));\r
+               graph_->set_text(print());\r
+               diagnostics::register_graph(graph_);\r
                \r
                auto display_mode = get_display_mode(input_, format_desc_.format, bmdFormat8BitYUV, bmdVideoInputFlagDefault);\r
                \r
@@ -124,7 +125,7 @@ public:
                                                                        << msg_info(narrow(print()) + " Could not enable video input.")\r
                                                                        << boost::errinfo_api_function("EnableVideoInput"));\r
 \r
-               if(FAILED(input_->EnableAudioInput(bmdAudioSampleRate48kHz, bmdAudioSampleType16bitInteger, 2))) \r
+               if(FAILED(input_->EnableAudioInput(bmdAudioSampleRate48kHz, bmdAudioSampleType32bitInteger, format_desc_.audio_channels))) \r
                        BOOST_THROW_EXCEPTION(caspar_exception() \r
                                                                        << msg_info(narrow(print()) + " Could not enable audio input.")\r
                                                                        << boost::errinfo_api_function("EnableAudioInput"));\r
@@ -184,8 +185,8 @@ public:
                        av_frame->format                        = PIX_FMT_UYVY422;\r
                        av_frame->width                         = video->GetWidth();\r
                        av_frame->height                        = video->GetHeight();\r
-                       av_frame->interlaced_frame      = format_desc_.mode != core::field_mode::progressive;\r
-                       av_frame->top_field_first       = format_desc_.mode == core::field_mode::upper ? 1 : 0;\r
+                       av_frame->interlaced_frame      = format_desc_.field_mode != core::field_mode::progressive;\r
+                       av_frame->top_field_first       = format_desc_.field_mode == core::field_mode::upper ? 1 : 0;\r
                                        \r
                        BOOST_FOREACH(auto& av_frame2, filter_.execute(av_frame))\r
                                muxer_.push(av_frame2);         \r
@@ -194,11 +195,11 @@ public:
                        if(audio && SUCCEEDED(audio->GetBytes(&bytes)))\r
                        {\r
                                auto sample_frame_count = audio->GetSampleFrameCount();\r
-                               auto audio_data = reinterpret_cast<short*>(bytes);\r
-                               muxer_.push(std::make_shared<std::vector<int16_t>>(audio_data, audio_data + sample_frame_count*2));\r
+                               auto audio_data = reinterpret_cast<int32_t*>(bytes);\r
+                               muxer_.push(std::make_shared<core::audio_buffer>(audio_data, audio_data + sample_frame_count*format_desc_.audio_channels));\r
                        }\r
                        else\r
-                               muxer_.push(std::make_shared<std::vector<int16_t>>(frame_factory_->get_video_format_desc().audio_samples_per_frame, 0));\r
+                               muxer_.push(std::make_shared<core::audio_buffer>(frame_factory_->get_video_format_desc().audio_samples_per_frame, 0));\r
                                        \r
                        muxer_.commit();\r
 \r
@@ -241,16 +242,25 @@ public:
        \r
 class decklink_producer_proxy : public core::frame_producer\r
 {              \r
-       safe_ptr<core::basic_frame>     last_frame_;\r
-       com_context<decklink_producer> context_;\r
+       safe_ptr<core::basic_frame>             last_frame_;\r
+       com_context<decklink_producer>  context_;\r
+       const int64_t                                   length_;\r
 public:\r
 \r
-       explicit decklink_producer_proxy(const safe_ptr<core::frame_factory>& frame_factory, const core::video_format_desc& format_desc, size_t device_index, const std::wstring& filter_str = L"")\r
+       explicit decklink_producer_proxy(const safe_ptr<core::frame_factory>& frame_factory, const core::video_format_desc& format_desc, size_t device_index, const std::wstring& filter_str, int64_t length)\r
                : context_(L"decklink_producer[" + boost::lexical_cast<std::wstring>(device_index) + L"]")\r
                , last_frame_(core::basic_frame::empty())\r
+               , length_(length)\r
        {\r
                context_.reset([&]{return new decklink_producer(format_desc, device_index, frame_factory, filter_str);}); \r
        }\r
+\r
+       ~decklink_producer_proxy()\r
+       {\r
+               auto str = print();\r
+               context_.reset();\r
+               CASPAR_LOG(info) << str << L" Successfully Uninitialized.";     \r
+       }\r
                                \r
        virtual safe_ptr<core::basic_frame> receive(int)\r
        {\r
@@ -265,39 +275,35 @@ public:
                return disable_audio(last_frame_);\r
        }\r
        \r
+       virtual int64_t nb_frames() const \r
+       {\r
+               return length_;\r
+       }\r
+       \r
        std::wstring print() const\r
        {\r
                return context_->print();\r
        }\r
 };\r
 \r
-safe_ptr<core::frame_producer> create_decklink_producer(const safe_ptr<core::frame_factory>& frame_factory, const std::vector<std::wstring>& params)\r
+safe_ptr<core::frame_producer> create_producer(const safe_ptr<core::frame_factory>& frame_factory, const std::vector<std::wstring>& params)\r
 {\r
        if(params.empty() || !boost::iequals(params[0], "decklink"))\r
                return core::frame_producer::empty();\r
 \r
-       size_t device_index = 1;\r
-       if(params.size() > 1)\r
-               device_index = lexical_cast_or_default(params[1], 1);\r
-\r
-       core::video_format_desc format_desc = core::video_format_desc::get(L"PAL");\r
-       if(params.size() > 2)\r
-       {\r
-               auto desc = core::video_format_desc::get(params[2]);\r
-               if(desc.format != core::video_format::invalid)\r
-                       format_desc = desc;\r
-       }\r
+       auto device_index       = core::get_param(L"DEVICE", params, 1);\r
+       auto filter_str         = core::get_param<std::wstring>(L"FILTER", params, L"");        \r
+       auto length                     = core::get_param(L"LENGTH", params, std::numeric_limits<int64_t>::max());      \r
        \r
-       std::wstring filter_str = L"";\r
+       boost::replace_all(filter_str, L"DEINTERLACE", L"YADIF=0:-1");\r
+       boost::replace_all(filter_str, L"DEINTERLACE_BOB", L"YADIF=1:-1");\r
 \r
-       auto filter_it = std::find(params.begin(), params.end(), L"FILTER");\r
-       if(filter_it != params.end())\r
-       {\r
-               if(++filter_it != params.end())\r
-                       filter_str = *filter_it;\r
-       }\r
+       auto format_desc        = core::video_format_desc::get(core::get_param<std::wstring>(L"FORMAT", params, L"INVALID"));\r
 \r
-       return make_safe<decklink_producer_proxy>(frame_factory, format_desc, device_index, filter_str);\r
+       if(format_desc.format == core::video_format::invalid)\r
+               format_desc = frame_factory->get_video_format_desc();\r
+                       \r
+       return make_safe<decklink_producer_proxy>(frame_factory, format_desc, device_index, filter_str, length);\r
 }\r
 \r
-}
\ No newline at end of file
+}}
\ No newline at end of file