X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=modules%2Fdecklink%2Fconsumer%2Fdecklink_consumer.cpp;h=9895fac1017938bf67154049f9399b55d997cc73;hb=51e34f2513d2a26f7da4681b53748a8b24ad8dc1;hp=78bf816c6cf05b3ad88af55b9d6173111d4d2a5e;hpb=acfc5e815529d35adca06c5fd2a1a657d2cb9dea;p=casparcg diff --git a/modules/decklink/consumer/decklink_consumer.cpp b/modules/decklink/consumer/decklink_consumer.cpp index 78bf816c6..9895fac10 100644 --- a/modules/decklink/consumer/decklink_consumer.cpp +++ b/modules/decklink/consumer/decklink_consumer.cpp @@ -28,7 +28,6 @@ #include -#include #include #include #include @@ -38,55 +37,98 @@ #include #include +#include #include #include -namespace caspar { +#include +#include + +namespace caspar { namespace decklink { struct configuration { size_t device_index; bool embedded_audio; - bool external_key; + bool internal_key; bool low_latency; bool key_only; - size_t buffer_depth; configuration() : device_index(1) , embedded_audio(false) - , external_key(false) + , internal_key(false) , low_latency(false) - , key_only(false) - , buffer_depth(core::consumer_buffer_depth()){} + , key_only(false){} + + size_t preroll_count() const + { + size_t count = 0; + count += low_latency ? 2 : 3; + count += embedded_audio ? 1 : 0; + return count; + } }; -class decklink_frame_muxer : public IDeckLinkVideoFrame +class decklink_frame : public IDeckLinkVideoFrame { - const safe_ptr frame_; - const core::video_format_desc format_desc_; + tbb::atomic ref_count_; + std::shared_ptr frame_; + const core::video_format_desc format_desc_; + + bool key_only_; + std::vector> key_data_; public: - decklink_frame_muxer(const safe_ptr& frame, const core::video_format_desc& format_desc) + decklink_frame(const safe_ptr& frame, const core::video_format_desc& format_desc, bool key_only) : frame_(frame) - , format_desc_(format_desc){} + , format_desc_(format_desc) + , key_only_(key_only) + { + ref_count_ = 0; + } - STDMETHOD (QueryInterface(REFIID, LPVOID*)) {return E_NOINTERFACE;} - STDMETHOD_(ULONG, AddRef()) {return 1;} - STDMETHOD_(ULONG, Release()) {return 1;} + STDMETHOD (QueryInterface(REFIID, LPVOID*)) {return E_NOINTERFACE;} + STDMETHOD_(ULONG, AddRef()) + { + return ++ref_count_; + } + STDMETHOD_(ULONG, Release()) + { + --ref_count_; + if(ref_count_ == 0) + delete this; + return ref_count_; + } - STDMETHOD_(long, GetWidth()) {return format_desc_.width;} - STDMETHOD_(long, GetHeight()) {return format_desc_.height;} - STDMETHOD_(long, GetRowBytes()) {return format_desc_.width*4;} - STDMETHOD_(BMDPixelFormat, GetPixelFormat()){return bmdFormat8BitBGRA;} - STDMETHOD_(BMDFrameFlags, GetFlags()) {return bmdFrameFlagDefault;} + STDMETHOD_(long, GetWidth()) {return format_desc_.width;} + STDMETHOD_(long, GetHeight()) {return format_desc_.height;} + STDMETHOD_(long, GetRowBytes()) {return format_desc_.width*4;} + STDMETHOD_(BMDPixelFormat, GetPixelFormat()) {return bmdFormat8BitBGRA;} + STDMETHOD_(BMDFrameFlags, GetFlags()) {return bmdFrameFlagDefault;} STDMETHOD(GetBytes(void** buffer)) { - static std::vector zeros(1920*1080*4, 0); - *buffer = const_cast(frame_->image_data().begin()); + static std::vector> zeros(1920*1080*4, 0); if(static_cast(frame_->image_data().size()) != format_desc_.size) + { *buffer = zeros.data(); + return S_OK; + } + + if(!key_only_) + *buffer = const_cast(frame_->image_data().begin()); + else + { + if(key_data_.empty()) + { + key_data_.resize(frame_->image_data().size()); + fast_memshfl(key_data_.data(), frame_->image_data().begin(), frame_->image_data().size(), 0x0F0F0F0F, 0x0B0B0B0B, 0x07070707, 0x03030303); + frame_.reset(); + } + *buffer = key_data_.data(); + } + return S_OK; } @@ -96,33 +138,32 @@ public: struct decklink_consumer : public IDeckLinkVideoOutputCallback, public IDeckLinkAudioOutputCallback, boost::noncopyable { - const configuration config_; + const configuration config_; CComPtr decklink_; CComQIPtr output_; CComQIPtr configuration_; CComQIPtr keyer_; - std::exception_ptr exception_; + Concurrency::overwrite_buffer exception_; - tbb::atomic is_running_; + tbb::atomic is_running_; - const std::wstring model_name_; - const core::video_format_desc format_desc_; - const size_t buffer_size_; + const std::wstring model_name_; + const core::video_format_desc format_desc_; + const size_t buffer_size_; - unsigned long frames_scheduled_; - unsigned long audio_scheduled_; + long long frames_scheduled_; + long long audio_scheduled_; - size_t preroll_count_; + size_t preroll_count_; - std::list> frame_container_; // Must be std::list in order to guarantee that pointers are always valid. - boost::circular_buffer> audio_container_; + boost::circular_buffer> audio_container_; tbb::concurrent_bounded_queue> video_frame_buffer_; tbb::concurrent_bounded_queue> audio_frame_buffer_; - std::shared_ptr graph_; + safe_ptr graph_; boost::timer tick_timer_; public: @@ -134,7 +175,7 @@ public: , keyer_(decklink_) , model_name_(get_model_name(decklink_)) , format_desc_(format_desc) - , buffer_size_(config.embedded_audio ? config.buffer_depth + 1 : config.buffer_depth) // Minimum buffer-size 3. + , buffer_size_(config.preroll_count()) , frames_scheduled_(0) , audio_scheduled_(0) , preroll_count_(0) @@ -145,12 +186,13 @@ public: video_frame_buffer_.set_capacity(1); audio_frame_buffer_.set_capacity(1); - graph_ = diagnostics::create_graph(narrow(print())); graph_->add_guide("tick-time", 0.5); - graph_->set_color("tick-time", diagnostics::color(0.1f, 0.7f, 0.8f)); + graph_->set_color("tick-time", diagnostics::color(0.0f, 0.6f, 0.9f)); graph_->set_color("late-frame", diagnostics::color(0.6f, 0.3f, 0.3f)); graph_->set_color("dropped-frame", diagnostics::color(0.3f, 0.6f, 0.3f)); - graph_->set_color("flushed-frame", diagnostics::color(0.3f, 0.3f, 0.6f)); + graph_->set_color("flushed-frame", diagnostics::color(0.4f, 0.3f, 0.8f)); + graph_->set_text(print()); + diagnostics::register_graph(graph_); enable_video(get_display_mode(output_, format_desc_.format, bmdFormat8BitBGRA, bmdVideoOutputFlagDefault)); @@ -158,7 +200,7 @@ public: enable_audio(); set_latency(config.low_latency); - set_keyer(config.external_key); + set_keyer(config.internal_key); if(config.embedded_audio) output_->BeginAudioPreroll(); @@ -168,8 +210,6 @@ public: if(!config.embedded_audio) start_playback(); - - CASPAR_LOG(info) << print() << L" Successfully Initialized."; } ~decklink_consumer() @@ -206,9 +246,9 @@ public: } } - void set_keyer(bool external_key) + void set_keyer(bool internal_key) { - if(!external_key) + if(internal_key) { if(FAILED(keyer_->Enable(FALSE))) CASPAR_LOG(error) << print() << L" Failed to enable internal keyer."; @@ -230,7 +270,7 @@ public: void enable_audio() { - if(FAILED(output_->EnableAudioOutput(bmdAudioSampleRate48kHz, bmdAudioSampleType16bitInteger, 2, bmdAudioOutputStreamTimestamped))) + if(FAILED(output_->EnableAudioOutput(bmdAudioSampleRate48kHz, bmdAudioSampleType32bitInteger, 2, bmdAudioOutputStreamTimestamped))) BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Could not enable audio output.")); if(FAILED(output_->SetAudioCallback(this))) @@ -285,18 +325,13 @@ public: else if(result == bmdOutputFrameFlushed) graph_->add_tag("flushed-frame"); - frame_container_.erase(std::find_if(frame_container_.begin(), frame_container_.end(), [&](const std::shared_ptr& frame) - { - return frame.get() == completed_frame; - })); - std::shared_ptr frame; video_frame_buffer_.pop(frame); - schedule_next_video(make_safe(frame)); + schedule_next_video(make_safe_ptr(frame)); } catch(...) { - exception_ = std::current_exception(); + Concurrency::send(exception_, std::current_exception()); return E_FAIL; } @@ -324,12 +359,12 @@ public: { std::shared_ptr frame; audio_frame_buffer_.pop(frame); - schedule_next_audio(make_safe(frame)); + schedule_next_audio(make_safe_ptr(frame)); } } catch(...) { - exception_ = std::current_exception(); + Concurrency::send(exception_, std::current_exception()); return E_FAIL; } @@ -340,7 +375,7 @@ public: { const int sample_frame_count = frame->audio_data().size()/format_desc_.audio_channels; - audio_container_.push_back(std::vector(frame->audio_data().begin(), frame->audio_data().end())); + audio_container_.push_back(std::vector(frame->audio_data().begin(), frame->audio_data().end())); if(FAILED(output_->ScheduleAudioSamples(audio_container_.back().data(), sample_frame_count, (audio_scheduled_++) * sample_frame_count, format_desc_.audio_sample_rate, nullptr))) CASPAR_LOG(error) << print() << L" Failed to schedule audio."; @@ -348,8 +383,8 @@ public: void schedule_next_video(const safe_ptr& frame) { - frame_container_.push_back(std::make_shared(frame, format_desc_)); - if(FAILED(output_->ScheduleVideoFrame(frame_container_.back().get(), (frames_scheduled_++) * format_desc_.duration, format_desc_.duration, format_desc_.time_scale))) + CComPtr frame2(new decklink_frame(frame, format_desc_, config_.key_only)); + if(FAILED(output_->ScheduleVideoFrame(frame2, (frames_scheduled_++) * format_desc_.duration, format_desc_.duration, format_desc_.time_scale))) CASPAR_LOG(error) << print() << L" Failed to schedule video."; graph_->update_value("tick-time", tick_timer_.elapsed()*format_desc_.fps*0.5); @@ -358,12 +393,13 @@ public: void send(const safe_ptr& frame) { - if(exception_ != nullptr) - std::rethrow_exception(exception_); + if(exception_.has_value()) + std::rethrow_exception(exception_.value()); if(!is_running_) BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Is not running.")); + Concurrency::scoped_oversubcription_token oversubscribe; if(config_.embedded_audio) audio_frame_buffer_.push(frame); video_frame_buffer_.push(frame); @@ -377,47 +413,56 @@ public: struct decklink_consumer_proxy : public core::frame_consumer { - const configuration config_; - - com_context context_; + const configuration config_; + std::unique_ptr context_; + core::video_format_desc format_desc_; public: decklink_consumer_proxy(const configuration& config) : config_(config) - , context_(L"decklink_consumer[" + boost::lexical_cast(config.device_index) + L"]"){} + { + } + + ~decklink_consumer_proxy() + { + auto str = print(); + context_.reset(); + CASPAR_LOG(info) << str << L" Successfully Uninitialized."; + } virtual void initialize(const core::video_format_desc& format_desc) { - context_.reset([&]{return new decklink_consumer(config_, format_desc);}); + Concurrency::scoped_oversubcription_token oversubscribe; + format_desc_ = format_desc; + struct co_init + { + co_init(){CoInitialize(nullptr);} + ~co_init(){CoUninitialize();} + } init; + context_ = nullptr; + context_.reset(new decklink_consumer(config_, format_desc_)); + + CASPAR_LOG(info) << print() << L" Successfully Initialized."; } - virtual void send(const safe_ptr& frame) + virtual bool send(const safe_ptr& frame) { context_->send(frame); + return true; } virtual std::wstring print() const { - return context_->print(); - } - - virtual bool key_only() const - { - return config_.key_only; + return context_ ? context_->print() : L"decklink_consumer"; } - + virtual const core::video_format_desc& get_video_format_desc() const { - return context_->get_video_format_desc(); - } - - virtual size_t buffer_depth() const - { - return context_->buffer_size_; + return format_desc_; } }; -safe_ptr create_decklink_consumer(const std::vector& params) +safe_ptr create_consumer(const std::vector& params) { if(params.size() < 1 || params[0] != L"DECKLINK") return core::frame_consumer::empty(); @@ -427,7 +472,7 @@ safe_ptr create_decklink_consumer(const std::vector 1) config.device_index = lexical_cast_or_default(params[1], config.device_index); - config.external_key = std::find(params.begin(), params.end(), L"EXTERNAL_KEY") != params.end(); + config.internal_key = std::find(params.begin(), params.end(), L"INTERNAL_KEY") != params.end(); config.low_latency = std::find(params.begin(), params.end(), L"LOW_LATENCY") != params.end(); config.embedded_audio = std::find(params.begin(), params.end(), L"EMBEDDED_AUDIO") != params.end(); config.key_only = std::find(params.begin(), params.end(), L"KEY_ONLY") != params.end(); @@ -435,11 +480,11 @@ safe_ptr create_decklink_consumer(const std::vector(config); } -safe_ptr create_decklink_consumer(const boost::property_tree::ptree& ptree) +safe_ptr create_consumer(const boost::property_tree::ptree& ptree) { configuration config; - config.external_key = ptree.get("external-key", config.external_key); + config.internal_key = ptree.get("internal-key", config.internal_key); config.low_latency = ptree.get("low-latency", config.low_latency); config.key_only = ptree.get("key-only", config.key_only); config.device_index = ptree.get("device", config.device_index); @@ -448,7 +493,7 @@ safe_ptr create_decklink_consumer(const boost::property_tr return make_safe(config); } -} +}} /* ##############################################################################