X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=modules%2Fdecklink%2Fconsumer%2Fdecklink_consumer.cpp;h=03131d31031bba151af2b1aa9199bc420945a9be;hb=6016fd682b267d2886babd51b1faa9cdd1812d54;hp=01b222a76b2f4e3b0a9e3d64a348f3c12340c17b;hpb=59918650af56790611de115111e6f0605ec284e6;p=casparcg diff --git a/modules/decklink/consumer/decklink_consumer.cpp b/modules/decklink/consumer/decklink_consumer.cpp index 01b222a76..03131d310 100644 --- a/modules/decklink/consumer/decklink_consumer.cpp +++ b/modules/decklink/consumer/decklink_consumer.cpp @@ -26,179 +26,135 @@ #include "../interop/DeckLinkAPI_h.h" -#include +#include #include #include #include #include #include +#include + +#include #include +#include #include #include namespace caspar { -enum key -{ - external_key, - internal_key, - default_key -}; - -enum latency -{ - low_latency, - normal_latency, - default_latency -}; - -enum output_pixels -{ - fill_and_key, - fill_only, - key_only -}; - struct configuration { size_t device_index; bool embedded_audio; - key keyer; - latency latency; - output_pixels output; + bool internal_key; + bool low_latency; + bool key_only; + size_t buffer_depth; configuration() : device_index(1) , embedded_audio(false) - , keyer(default_key) - , latency(default_latency) - , output(fill_and_key){} + , internal_key(false) + , low_latency(false) + , key_only(false) + , buffer_depth(core::consumer_buffer_depth()){} }; -class decklink_frame_adapter : 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_adapter(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; - } - - STDMETHOD(GetTimecode(BMDTimecodeFormat format, IDeckLinkTimecode** timecode)){return S_FALSE;} - STDMETHOD(GetAncillaryData(IDeckLinkVideoFrameAncillary** ancillary)) {return S_FALSE;} -}; - -std::shared_ptr make_alpha_only_frame(const CComQIPtr& decklink, const safe_ptr& frame, const core::video_format_desc& format_desc) -{ - IDeckLinkMutableVideoFrame* result; - - if(FAILED(decklink->CreateVideoFrame(format_desc.width, format_desc.height, format_desc.size/format_desc.height, bmdFormat8BitBGRA, bmdFrameFlagDefault, &result))) - BOOST_THROW_EXCEPTION(caspar_exception()); - - void* bytes = nullptr; - if(FAILED(result->GetBytes(&bytes))) - BOOST_THROW_EXCEPTION(caspar_exception()); - - unsigned char* data = reinterpret_cast(bytes); + return S_OK; + } - if(static_cast(frame->image_data().size()) == format_desc.size) - { - tbb::parallel_for(tbb::blocked_range(0, frame->image_data().size()/4), [&](const tbb::blocked_range& r) + if(!key_only_) + *buffer = const_cast(frame_->image_data().begin()); + else { - for(int n = r.begin(); n != r.end(); ++n) + if(key_data_.empty()) { - data[n*4+0] = frame->image_data()[n*4+3]; - data[n*4+1] = frame->image_data()[n*4+3]; - data[n*4+2] = frame->image_data()[n*4+3]; - data[n*4+3] = 255; + 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(); } - }); - } - else - memset(data, 0, format_desc.size); - - return std::shared_ptr(result, [](IDeckLinkMutableVideoFrame* p) {p->Release();}); -} - -std::shared_ptr make_fill_only_frame(const CComQIPtr& decklink, const safe_ptr& frame, const core::video_format_desc& format_desc) -{ - IDeckLinkMutableVideoFrame* result; - - if(FAILED(decklink->CreateVideoFrame(format_desc.width, format_desc.height, format_desc.size/format_desc.height, bmdFormat8BitBGRA, bmdFrameFlagDefault, &result))) - BOOST_THROW_EXCEPTION(caspar_exception()); - - void* bytes = nullptr; - if(FAILED(result->GetBytes(&bytes))) - BOOST_THROW_EXCEPTION(caspar_exception()); - - unsigned char* data = reinterpret_cast(bytes); + *buffer = key_data_.data(); + } - if(static_cast(frame->image_data().size()) == format_desc.size) - { - tbb::parallel_for(tbb::blocked_range(0, frame->image_data().size()/4), [&](const tbb::blocked_range& r) - { - for(int n = r.begin(); n != r.end(); ++n) - { - data[n*4+0] = frame->image_data()[n*4+0]; - data[n*4+1] = frame->image_data()[n*4+1]; - data[n*4+2] = frame->image_data()[n*4+2]; - data[n*4+3] = 255; - } - }); + return S_OK; } - else - memset(data, 0, format_desc.size); - - return std::shared_ptr(result, [](IDeckLinkMutableVideoFrame* p) {p->Release();}); -} + + STDMETHOD(GetTimecode(BMDTimecodeFormat format, IDeckLinkTimecode** timecode)){return S_FALSE;} + STDMETHOD(GetAncillaryData(IDeckLinkVideoFrameAncillary** ancillary)) {return S_FALSE;} +}; 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_; + tbb::spin_mutex exception_mutex_; + std::exception_ptr 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_; + + long long frames_scheduled_; + long long audio_scheduled_; - unsigned long frames_scheduled_; - unsigned long audio_scheduled_; + 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_; + tbb::concurrent_bounded_queue> video_frame_buffer_; + tbb::concurrent_bounded_queue> audio_frame_buffer_; std::shared_ptr graph_; boost::timer tick_timer_; @@ -212,9 +168,10 @@ public: , keyer_(decklink_) , model_name_(get_model_name(decklink_)) , format_desc_(format_desc) - , buffer_size_(config.embedded_audio ? 5 : 4) // Minimum buffer-size (3 + 1 tolerance). + , buffer_size_(config.embedded_audio ? config.buffer_depth + 1 : config.buffer_depth) // Minimum buffer-size 3. , frames_scheduled_(0) , audio_scheduled_(0) + , preroll_count_(0) , audio_container_(buffer_size_+1) { is_running_ = true; @@ -224,36 +181,34 @@ public: 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)); enable_video(get_display_mode(output_, format_desc_.format, bmdFormat8BitBGRA, bmdVideoOutputFlagDefault)); if(config.embedded_audio) enable_audio(); - set_latency(config.latency); - set_keyer(config.keyer); - - for(size_t n = 0; n < buffer_size_; ++n) - schedule_next_video(core::read_frame::empty()); + set_latency(config.low_latency); + set_keyer(config.internal_key); + + if(config.embedded_audio) + output_->BeginAudioPreroll(); - if(config.embedded_audio) - output_->BeginAudioPreroll(); - else + for(size_t n = 0; n < buffer_size_; ++n) + schedule_next_video(make_safe()); + + if(!config.embedded_audio) start_playback(); - - CASPAR_LOG(info) << print() << L" Buffer depth: " << buffer_size_; - CASPAR_LOG(info) << print() << L" Successfully initialized for " << format_desc_.name; } ~decklink_consumer() { is_running_ = false; - video_frame_buffer_.try_push(core::read_frame::empty()); - audio_frame_buffer_.try_push(core::read_frame::empty()); + video_frame_buffer_.try_push(std::make_shared()); + audio_frame_buffer_.try_push(std::make_shared()); if(output_ != nullptr) { @@ -264,25 +219,28 @@ public: } } - void set_latency(latency latency) + const core::video_format_desc& get_video_format_desc() const + { + return format_desc_; + } + + void set_latency(bool low_latency) { - if(latency == normal_latency) + if(!low_latency) { configuration_->SetFlag(bmdDeckLinkConfigLowLatencyVideoOutput, false); CASPAR_LOG(info) << print() << L" Enabled normal-latency mode"; } - else if(latency == low_latency) + else { configuration_->SetFlag(bmdDeckLinkConfigLowLatencyVideoOutput, true); CASPAR_LOG(info) << print() << L" Enabled low-latency mode"; } - else - CASPAR_LOG(info) << print() << L" Uses driver latency settings."; } - void set_keyer(key keyer) + void set_keyer(bool internal_key) { - if(keyer == internal_key) + if(internal_key) { if(FAILED(keyer_->Enable(FALSE))) CASPAR_LOG(error) << print() << L" Failed to enable internal keyer."; @@ -291,7 +249,7 @@ public: else CASPAR_LOG(info) << print() << L" Enabled internal keyer."; } - else if(keyer == external_key) + else { if(FAILED(keyer_->Enable(TRUE))) CASPAR_LOG(error) << print() << L" Failed to enable external keyer."; @@ -300,13 +258,11 @@ public: else CASPAR_LOG(info) << print() << L" Enabled external keyer."; } - else - CASPAR_LOG(info) << print() << L" Uses driver keyer settings."; } 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))) @@ -321,7 +277,9 @@ public: BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Could not enable video output.")); if(FAILED(output_->SetScheduledFrameCompletionCallback(this))) - BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Failed to set playback completion callback.")); + BOOST_THROW_EXCEPTION(caspar_exception() + << msg_info(narrow(print()) + " Failed to set playback completion callback.") + << boost::errinfo_api_function("SetScheduledFrameCompletionCallback")); } void start_playback() @@ -349,23 +307,23 @@ public: try { if(result == bmdOutputFrameDisplayedLate) + { graph_->add_tag("late-frame"); + ++frames_scheduled_; + ++audio_scheduled_; + } else if(result == bmdOutputFrameDropped) graph_->add_tag("dropped-frame"); 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(safe_ptr(frame)); + std::shared_ptr frame; + video_frame_buffer_.pop(frame); + schedule_next_video(make_safe(frame)); } catch(...) { + tbb::spin_mutex::scoped_lock lock(exception_mutex_); exception_ = std::current_exception(); return E_FAIL; } @@ -379,16 +337,27 @@ public: return E_FAIL; try - { - std::shared_ptr frame; - audio_frame_buffer_.pop(frame); - schedule_next_audio(safe_ptr(frame)); - + { if(preroll) - start_playback(); + { + if(++preroll_count_ >= buffer_size_) + { + output_->EndAudioPreroll(); + start_playback(); + } + else + schedule_next_audio(make_safe()); + } + else + { + std::shared_ptr frame; + audio_frame_buffer_.pop(frame); + schedule_next_audio(make_safe(frame)); + } } catch(...) { + tbb::spin_mutex::scoped_lock lock(exception_mutex_); exception_ = std::current_exception(); return E_FAIL; } @@ -396,42 +365,33 @@ public: return S_OK; } - void schedule_next_audio(const safe_ptr& frame) + void schedule_next_audio(const safe_ptr& frame) { - static std::vector silence(48000, 0); - - const int sample_count = format_desc_.audio_samples_per_frame; - const int sample_frame_count = sample_count/2; + const int sample_frame_count = frame->audio_data().size()/format_desc_.audio_channels; - const short* frame_audio_data = frame->audio_data().size() == sample_count ? frame->audio_data().begin() : silence.data(); - audio_container_.push_back(std::vector(frame_audio_data, frame_audio_data+sample_count)); + 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."; } - void schedule_next_video(const safe_ptr& frame) + void schedule_next_video(const safe_ptr& frame) { - std::shared_ptr deck_frame; - if(config_.output == key_only) - deck_frame = make_alpha_only_frame(output_, frame, format_desc_); - else if(config_.output == fill_only) - deck_frame = make_fill_only_frame(output_, frame, format_desc_); - else - deck_frame = std::make_shared(frame, format_desc_); - - frame_container_.push_back(deck_frame); - 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); tick_timer_.restart(); } - void send(const safe_ptr& frame) + void send(const safe_ptr& frame) { - if(exception_ != nullptr) - std::rethrow_exception(exception_); + { + tbb::spin_mutex::scoped_lock lock(exception_mutex_); + if(exception_ != nullptr) + std::rethrow_exception(exception_); + } if(!is_running_) BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Is not running.")); @@ -443,34 +403,52 @@ public: std::wstring print() const { - return model_name_ + L" [" + boost::lexical_cast(config_.device_index) + L"]"; + return model_name_ + L" [" + boost::lexical_cast(config_.device_index) + L"|" + format_desc_.name + L"]"; } }; struct decklink_consumer_proxy : public core::frame_consumer { - const configuration config_; - - com_context context_; + const configuration config_; + com_context 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"]"){} + , 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);}); + format_desc_ = format_desc; + context_.reset([&]{return 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(); + return context_ ? context_->print() : L"decklink_consumer"; + } + + virtual const core::video_format_desc& get_video_format_desc() const + { + return format_desc_; } }; @@ -483,18 +461,11 @@ safe_ptr create_decklink_consumer(const std::vector 1) config.device_index = lexical_cast_or_default(params[1], config.device_index); - - if(std::find(params.begin(), params.end(), L"INTERNAL_KEY") != params.end()) - config.keyer = internal_key; - else if(std::find(params.begin(), params.end(), L"EXTERNAL_KEY") != params.end()) - config.keyer = external_key; - if(std::find(params.begin(), params.end(), L"LOW_LATENCY") != params.end()) - config.latency = low_latency; - else if(std::find(params.begin(), params.end(), L"NORMAL_LATENCY") != params.end()) - config.latency = normal_latency; - - config.embedded_audio = std::find(params.begin(), params.end(), L"EMBEDDED_AUDIO") != 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(); return make_safe(config); } @@ -503,26 +474,11 @@ safe_ptr create_decklink_consumer(const boost::property_tr { configuration config; - auto key_str = ptree.get("key", "default"); - if(key_str == "internal") - config.keyer = internal_key; - else if(key_str == "external") - config.keyer = external_key; - - auto latency_str = ptree.get("latency", "default"); - if(latency_str == "normal") - config.latency = normal_latency; - else if(latency_str == "low") - config.latency = low_latency; - - auto output_str = ptree.get("output", "fill_and_key"); - if(output_str == "fill_only") - config.output = fill_only; - else if(output_str == "key_only") - config.output = key_only; - - config.device_index = ptree.get("device", 0); - config.embedded_audio = ptree.get("embedded-audio", false); + 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); + config.embedded_audio = ptree.get("embedded-audio", config.embedded_audio); return make_safe(config); }