X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=modules%2Fdecklink%2Fconsumer%2Fdecklink_consumer.cpp;h=9ead7d9bbf1ace07d8975cb2e7591d27ad60a771;hb=7c931281ee7e32ead77e77bda55eecc84699816e;hp=71da7b33fadace2748709f66d8ef465ca2f07208;hpb=beea2625dcb4ed6e7b95e716baaa4220ee4e448e;p=casparcg diff --git a/modules/decklink/consumer/decklink_consumer.cpp b/modules/decklink/consumer/decklink_consumer.cpp index 71da7b33f..9ead7d9bb 100644 --- a/modules/decklink/consumer/decklink_consumer.cpp +++ b/modules/decklink/consumer/decklink_consumer.cpp @@ -48,7 +48,7 @@ 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; @@ -56,18 +56,18 @@ struct configuration 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()){} }; -class decklink_frame_muxer : public IDeckLinkVideoFrame +class decklink_frame : public IDeckLinkVideoFrame { const safe_ptr frame_; - const core::video_format_desc format_desc_; + const core::video_format_desc format_desc_; 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) : frame_(frame) , format_desc_(format_desc){} @@ -103,18 +103,19 @@ struct decklink_consumer : public IDeckLinkVideoOutputCallback, public IDeckLink 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_; - 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_; @@ -147,10 +148,10 @@ 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)); @@ -158,7 +159,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(); @@ -206,9 +207,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."; @@ -292,10 +293,11 @@ public: std::shared_ptr frame; video_frame_buffer_.pop(frame); - schedule_next_video(make_safe(frame)); + schedule_next_video(make_safe(frame)); } catch(...) { + tbb::spin_mutex::scoped_lock lock(exception_mutex_); exception_ = std::current_exception(); return E_FAIL; } @@ -329,6 +331,7 @@ public: } catch(...) { + tbb::spin_mutex::scoped_lock lock(exception_mutex_); exception_ = std::current_exception(); return E_FAIL; } @@ -348,7 +351,7 @@ public: void schedule_next_video(const safe_ptr& frame) { - frame_container_.push_back(std::make_shared(frame, format_desc_)); + 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))) CASPAR_LOG(error) << print() << L" Failed to schedule video."; @@ -358,8 +361,11 @@ public: 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.")); @@ -377,23 +383,45 @@ public: 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_; + size_t fail_count_; 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"]") + , fail_count_(0) + { + } 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_);}); } virtual bool send(const safe_ptr& frame) { - context_->send(frame); + if(!context_) + context_.reset([&]{return new decklink_consumer(config_, format_desc_);}); + + try + { + context_->send(frame); + fail_count_ = 0; + } + catch(...) + { + context_.reset(); + + if(fail_count_++ > 3) + return false; // Outside didn't handle exception properly, just give up. + + throw; + } + return true; } @@ -409,7 +437,7 @@ public: virtual const core::video_format_desc& get_video_format_desc() const { - return context_->get_video_format_desc(); + return format_desc_; } }; @@ -423,7 +451,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,7 +463,7 @@ safe_ptr create_decklink_consumer(const boost::property_tr { 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);