X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=modules%2Fbluefish%2Fconsumer%2Fbluefish_consumer.cpp;h=f0bb889a40545b17164232c51f899b1097833b3b;hb=9bd249256b11caa7912fb7cdbc7710251e8cd6f4;hp=4902850d8f3509096bfeed729705762ebba3caf5;hpb=ff6d30a547b05bcc55d2c8201e70bb016c7c8afd;p=casparcg diff --git a/modules/bluefish/consumer/bluefish_consumer.cpp b/modules/bluefish/consumer/bluefish_consumer.cpp index 4902850d8..f0bb889a4 100644 --- a/modules/bluefish/consumer/bluefish_consumer.cpp +++ b/modules/bluefish/consumer/bluefish_consumer.cpp @@ -24,14 +24,17 @@ #include "../util/util.h" #include "../util/memory.h" -#include +#include #include #include +#include #include #include +#include + #include #include @@ -79,67 +82,49 @@ void blue_initialize() blue_hanc_initialize(); } -struct bluefish_consumer::implementation : boost::noncopyable +struct bluefish_consumer : boost::noncopyable { std::wstring model_name_; const unsigned int device_index_; std::shared_ptr graph_; - timer perf_timer_; + boost::timer frame_timer_; + boost::timer tick_timer_; + boost::timer sync_timer_; boost::unique_future active_; std::shared_ptr blue_; - core::video_format_desc format_desc_; + const core::video_format_desc format_desc_; - unsigned long mem_fmt_; - unsigned long upd_fmt_; - EVideoMode vid_fmt_; - unsigned long res_fmt_; - unsigned long engine_mode_; + const unsigned long mem_fmt_; + const unsigned long upd_fmt_; + const unsigned long res_fmt_; + unsigned long engine_mode_; + EVideoMode vid_fmt_; std::array reserved_frames_; - const bool embed_audio_; + const bool embedded_audio_; executor executor_; public: - implementation::implementation(unsigned int device_index, bool embed_audio) + bluefish_consumer(const core::video_format_desc& format_desc, unsigned int device_index, bool embedded_audio) : model_name_(L"BLUEFISH") , device_index_(device_index) + , format_desc_(format_desc) , mem_fmt_(MEM_FMT_ARGB_PC) , upd_fmt_(UPD_FMT_FRAME) - , vid_fmt_(VID_FMT_INVALID) , res_fmt_(RES_FMT_NORMAL) , engine_mode_(VIDEO_ENGINE_FRAMESTORE) - , embed_audio_(embed_audio) - , executor_(print()) - { - if(!BlueVelvetFactory4 || (embed_audio_ && (!encode_hanc_frame || !encode_hanc_frame))) - BOOST_THROW_EXCEPTION(bluefish_exception() << msg_info("Bluefish drivers not found.")); - } - - ~implementation() + , vid_fmt_(VID_FMT_INVALID) + , embedded_audio_(embedded_audio) + , executor_(print(), true) { - if(executor_.is_running()) - { - executor_.invoke([&] - { - disable_video_output(); - - if(blue_) - blue_->device_detach(); - }); - } + if(!BlueVelvetFactory4 || (embedded_audio_ && (!encode_hanc_frame || !encode_hanc_frame))) + BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("Bluefish drivers not found.")); - CASPAR_LOG(info) << print() << L" Shutting down."; - } - - void initialize(const core::video_format_desc& format_desc) - { - format_desc_ = format_desc; - blue_.reset(BlueVelvetFactory4()); if(BLUE_FAIL(blue_->device_attach(device_index_, FALSE))) @@ -151,6 +136,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_->add_guide("frame-time", 0.5f); + graph_->set_color("frame-time", diagnostics::color(1.0f, 0.0f, 0.0f)); + graph_->add_guide("frame-time", 0.5f); + graph_->set_color("sync-time", diagnostics::color(0.5f, 1.0f, 0.2f)); //void* pBlueDevice = blue_attach_to_device(1); //EBlueConnectorPropertySetting video_routing[1]; @@ -208,7 +197,7 @@ public: if(BLUE_FAIL(set_card_property(blue_, VIDEO_PREDEFINED_COLOR_MATRIX, vid_fmt_ == VID_FMT_PAL ? MATRIX_601_CGR : MATRIX_709_CGR))) CASPAR_LOG(warning) << print() << TEXT(" Failed to set colormatrix to ") << (vid_fmt_ == VID_FMT_PAL ? TEXT("601 CGR") : TEXT("709 CGR")) << TEXT("."); - if(!embed_audio_) + if(!embedded_audio_) { if(BLUE_FAIL(set_card_property(blue_, EMBEDEDDED_AUDIO_OUTPUT, 0))) CASPAR_LOG(warning) << TEXT("BLUECARD ERROR: Failed to disable embedded audio."); @@ -237,13 +226,26 @@ public: for(size_t n = 0; n < reserved_frames_.size(); ++n) reserved_frames_[n] = std::make_shared(format_desc_.size, n); - - executor_.start(); - active_ = executor_.begin_invoke([]{}); - CASPAR_LOG(info) << print() << TEXT(" Successfully initialized for ") << format_desc_ << TEXT("."); + active_ = executor_.begin_invoke([]{}); + + CASPAR_LOG(info) << print() << L" Successfully Initialized."; } + + ~bluefish_consumer() + { + executor_.clear(); + executor_.invoke([&] + { + disable_video_output(); + + if(blue_) + blue_->device_detach(); + }); + CASPAR_LOG(info) << print() << L" Shutting down."; + } + void enable_video_output() { if(!BLUE_PASS(set_card_property(blue_, VIDEO_BLACKGENERATOR, 0))) @@ -256,21 +258,28 @@ public: CASPAR_LOG(error)<< print() << TEXT(" Failed to disable video output."); } - virtual void send(const safe_ptr& frame) + void send(const safe_ptr& frame) { static std::vector silence(MAX_HANC_BUFFER_SIZE, 0); - - size_t audio_samples = static_cast(48000.0 / format_desc_.fps); - size_t audio_nchannels = 2; - + active_.get(); active_ = executor_.begin_invoke([=] { try { - std::copy_n(frame->image_data().begin(), frame->image_data().size(), reserved_frames_.front()->image_data()); + frame_timer_.restart(); + + const size_t audio_samples = static_cast(48000.0 / format_desc_.fps); + const size_t audio_nchannels = 2; + + fast_memcpy(reserved_frames_.front()->image_data(), frame->image_data().begin(), frame->image_data().size()); - if(embed_audio_) + sync_timer_.restart(); + unsigned long n_field = 0; + blue_->wait_output_video_synch(UPD_FMT_FRAME, n_field); + graph_->update_value("sync-time", static_cast(sync_timer_.elapsed()*format_desc_.fps*0.5)); + + if(embedded_audio_) { auto frame_audio_data = frame->audio_data().empty() ? silence.data() : const_cast(frame->audio_data().begin()); @@ -300,12 +309,12 @@ public: CASPAR_LOG(warning) << print() << TEXT(" render_buffer_update failed."); } - unsigned long n_field = 0; - blue_->wait_output_video_synch(UPD_FMT_FRAME, n_field); - std::rotate(reserved_frames_.begin(), reserved_frames_.begin() + 1, reserved_frames_.end()); - graph_->update_value("tick-time", static_cast(perf_timer_.elapsed()/format_desc_.interval*0.5)); - perf_timer_.reset(); + + graph_->update_value("frame-time", static_cast(frame_timer_.elapsed()*format_desc_.fps*0.5)); + + graph_->update_value("tick-time", static_cast(tick_timer_.elapsed()*format_desc_.fps*0.5)); + tick_timer_.restart(); } catch(...) { @@ -314,8 +323,6 @@ public: }); } - virtual size_t buffer_depth() const{return 1;} - void encode_hanc(BLUE_UINT32* hanc_data, void* audio_data, size_t audio_samples, size_t audio_nchannels) { auto card_type = blue_->has_video_cardtype(); @@ -341,20 +348,43 @@ public: std::wstring print() const { - return model_name_ + L" [" + boost::lexical_cast(device_index_) + L"]"; + return model_name_ + L" [" + boost::lexical_cast(device_index_) + L"|" + format_desc_.name + L"]"; } }; -bluefish_consumer::bluefish_consumer(unsigned int device_index, bool embed_audio) : impl_(new implementation(device_index, embed_audio)){} -bluefish_consumer::bluefish_consumer(bluefish_consumer&& other) : impl_(std::move(other.impl_)){} -void bluefish_consumer::initialize(const core::video_format_desc& format_desc) +struct bluefish_consumer_proxy : public core::frame_consumer { - impl_.reset(new implementation(impl_->device_index_, impl_->embed_audio_)); - impl_->initialize(format_desc); -} -void bluefish_consumer::send(const safe_ptr& frame){impl_->send(frame);} -size_t bluefish_consumer::buffer_depth() const{return impl_->buffer_depth();} -std::wstring bluefish_consumer::print() const {return impl_->print();} + std::unique_ptr consumer_; + const size_t device_index_; + const bool embedded_audio_; + bool key_only_; +public: + + bluefish_consumer_proxy(size_t device_index, bool embedded_audio, bool key_only) + : device_index_(device_index) + , embedded_audio_(embedded_audio) + , key_only_(key_only){} + + virtual void initialize(const core::video_format_desc& format_desc) + { + consumer_.reset(new bluefish_consumer(format_desc, device_index_, embedded_audio_)); + } + + virtual void send(const safe_ptr& frame) + { + consumer_->send(frame); + } + + virtual std::wstring print() const + { + return consumer_->print(); + } + + virtual bool key_only() const + { + return key_only_; + } +}; std::wstring get_bluefish_version() { @@ -385,7 +415,7 @@ std::vector get_bluefish_device_list() for(int n = 1; BLUE_PASS(blue->device_attach(n, FALSE)); ++n) { - devices.push_back(L"[" + boost::lexical_cast(n) + L"] " + get_card_desc(blue->has_video_cardtype())); + devices.push_back(std::wstring(get_card_desc(blue->has_video_cardtype())) + L" [" + boost::lexical_cast(n) + L"]"); blue->device_detach(); } } @@ -400,15 +430,22 @@ safe_ptr create_bluefish_consumer(const std::vector 1) - device_index = lexical_cast_or_default(params[2]); + device_index = lexical_cast_or_default(params[1], 1); + + bool embedded_audio = std::find(params.begin(), params.end(), L"EMBEDDED_AUDIO") != params.end(); + bool key_only = std::find(params.begin(), params.end(), L"KEY_ONLY") != params.end(); + + return make_safe(device_index, embedded_audio, key_only); +} - if(params.size() > 2) - embed_audio = lexical_cast_or_default(params[3]); +safe_ptr create_bluefish_consumer(const boost::property_tree::ptree& ptree) +{ + auto device_index = ptree.get("device", 0); + auto embedded_audio = ptree.get("embedded-audio", false); + bool key_only = (ptree.get("output", "fill_and_key") == "key_only"); - return make_safe(device_index, embed_audio); + return make_safe(device_index, embedded_audio, key_only); } } \ No newline at end of file