X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=modules%2Fbluefish%2Fconsumer%2Fbluefish_consumer.cpp;h=ab73cac358894d64ae3f9cf412d7e29f65185888;hb=ef45ae47b4f50ef057041b5122d0f34c1659cc11;hp=4fac6d84fec32f593a46dace3e7b2150095c4c56;hpb=ff60b8649021d4d41bc15af633a1b4848ee7588c;p=casparcg diff --git a/modules/bluefish/consumer/bluefish_consumer.cpp b/modules/bluefish/consumer/bluefish_consumer.cpp index 4fac6d84f..ab73cac35 100644 --- a/modules/bluefish/consumer/bluefish_consumer.cpp +++ b/modules/bluefish/consumer/bluefish_consumer.cpp @@ -21,98 +21,29 @@ #include "../StdAfx.h" #include "bluefish_consumer.h" -#include "../util/util.h" +#include "../util/blue_velvet.h" #include "../util/memory.h" #include #include #include -#include #include +#include +#include #include +#include + #include #include -#include -#include - #include #include namespace caspar { - -CBlueVelvet4* (*BlueVelvetFactory4)() = nullptr; -const char* (*BlueVelvetVersion)() = nullptr; -BLUE_UINT32 (*encode_hanc_frame)(struct hanc_stream_info_struct * hanc_stream_ptr, void * audio_pcm_ptr,BLUE_UINT32 no_audio_ch,BLUE_UINT32 no_audio_samples,BLUE_UINT32 nTypeOfSample,BLUE_UINT32 emb_audio_flag) = nullptr; -BLUE_UINT32 (*encode_hanc_frame_ex)(BLUE_UINT32 card_type, struct hanc_stream_info_struct * hanc_stream_ptr, void * audio_pcm_ptr, BLUE_UINT32 no_audio_ch, BLUE_UINT32 no_audio_samples, BLUE_UINT32 nTypeOfSample, BLUE_UINT32 emb_audio_flag) = nullptr; - -void blue_velvet_initialize() -{ -#ifdef _DEBUG - auto module = LoadLibrary(L"BlueVelvet3_d.dll"); -#else - auto module = LoadLibrary(L"BlueVelvet3.dll"); -#endif - if(!module) - BOOST_THROW_EXCEPTION(file_not_found() << msg_info("Could not find BlueVelvet3.dll")); - static std::shared_ptr lib(module, FreeLibrary); - BlueVelvetFactory4 = reinterpret_cast(GetProcAddress(module, "BlueVelvetFactory4")); - BlueVelvetVersion = reinterpret_cast(GetProcAddress(module, "BlueVelvetVersion")); -} - -void blue_hanc_initialize() -{ -#ifdef _DEBUG - auto module = LoadLibrary(L"BlueHancUtils_d.dll"); -#else - auto module = LoadLibrary(L"BlueHancUtils.dll"); -#endif - if(!module) - BOOST_THROW_EXCEPTION(file_not_found() << msg_info("Could not find BlueHancUtils.dll")); - static std::shared_ptr lib(module, FreeLibrary); - encode_hanc_frame = reinterpret_cast(GetProcAddress(module, "encode_hanc_frame")); - encode_hanc_frame_ex = reinterpret_cast(GetProcAddress(module, "encode_hanc_frame_ex")); -} - -void blue_initialize() -{ - blue_velvet_initialize(); - blue_hanc_initialize(); -} - -safe_ptr create_blue(size_t device_index) -{ - if(!BlueVelvetFactory4 || !encode_hanc_frame || !encode_hanc_frame) - BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("Bluefish drivers not found.")); - - auto blue = safe_ptr(BlueVelvetFactory4()); - - if(BLUE_FAIL(blue->device_attach(device_index, FALSE))) - BOOST_THROW_EXCEPTION(bluefish_exception() << msg_info("Failed to attach device.")); - - return blue; -} - -EVideoMode get_video_mode(CBlueVelvet4& blue, const core::video_format_desc& format_desc) -{ - EVideoMode vid_fmt = VID_FMT_INVALID; - auto desiredVideoFormat = vid_fmt_from_video_format(format_desc.format); - int videoModeCount = blue.count_video_mode(); - for(int videoModeIndex = 1; videoModeIndex <= videoModeCount; ++videoModeIndex) - { - EVideoMode videoMode = blue.enum_video_mode(videoModeIndex); - if(videoMode == desiredVideoFormat) - vid_fmt = videoMode; - } - if(vid_fmt == VID_FMT_INVALID) - BOOST_THROW_EXCEPTION(bluefish_exception() << msg_info("Failed get videomode.") << arg_value_info(narrow(format_desc.name))); - - return vid_fmt; -} - + struct bluefish_consumer : boost::noncopyable { safe_ptr blue_; @@ -125,41 +56,35 @@ struct bluefish_consumer : boost::noncopyable boost::timer frame_timer_; boost::timer tick_timer_; boost::timer sync_timer_; - - const EVideoMode vid_fmt_; - const EMemoryFormat mem_fmt_; - const EUpdateMethod upd_fmt_; - const EResoFormat res_fmt_; - EEngineMode engine_mode_; - - std::array reserved_frames_; - tbb::concurrent_bounded_queue> frame_buffer_; + + unsigned int vid_fmt_; + + std::array reserved_frames_; + tbb::concurrent_bounded_queue> frame_buffer_; - int preroll_count_; + int preroll_count_; - const bool embedded_audio_; + const bool embedded_audio_; + const bool key_only_; - executor executor_; + executor executor_; public: - bluefish_consumer(const core::video_format_desc& format_desc, unsigned int device_index, bool embedded_audio) + bluefish_consumer(const core::video_format_desc& format_desc, unsigned int device_index, bool embedded_audio, bool key_only) : blue_(create_blue(device_index)) , device_index_(device_index) , format_desc_(format_desc) - , model_name_(get_card_desc(blue_->has_video_cardtype())) - , vid_fmt_(get_video_mode(*blue_, format_desc)) - , mem_fmt_(MEM_FMT_ARGB_PC) - , upd_fmt_(UPD_FMT_FRAME) - , res_fmt_(RES_FMT_NORMAL) - , engine_mode_(VIDEO_ENGINE_FRAMESTORE) + , model_name_(get_card_desc(*blue_)) + , vid_fmt_(get_video_mode(*blue_, format_desc)) , preroll_count_(0) , embedded_audio_(embedded_audio) + , key_only_(key_only) , executor_(print()) { - executor_.set_capacity(CONSUMER_BUFFER_DEPTH); + executor_.set_capacity(core::consumer_buffer_depth()); 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_->add_guide("frame-time", 0.5f); graph_->set_color("frame-time", diagnostics::color(1.0f, 0.0f, 0.0f)); graph_->set_color("sync-time", diagnostics::color(0.5f, 1.0f, 0.2f)); @@ -167,24 +92,24 @@ public: //Setting output Video mode if(BLUE_FAIL(set_card_property(blue_, VIDEO_MODE, vid_fmt_))) - BOOST_THROW_EXCEPTION(bluefish_exception() << msg_info(narrow(print()) + " Failed to set videomode.")); + BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Failed to set videomode.")); //Select Update Mode for output - if(BLUE_FAIL(set_card_property(blue_, VIDEO_UPDATE_TYPE, upd_fmt_))) - BOOST_THROW_EXCEPTION(bluefish_exception() << msg_info(narrow(print()) + " Failed to set update type.")); + if(BLUE_FAIL(set_card_property(blue_, VIDEO_UPDATE_TYPE, UPD_FMT_FRAME))) + BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Failed to set update type.")); disable_video_output(); //Enable dual link output if(BLUE_FAIL(set_card_property(blue_, VIDEO_DUAL_LINK_OUTPUT, 1))) - BOOST_THROW_EXCEPTION(bluefish_exception() << msg_info(narrow(print()) + " Failed to enable dual link.")); + BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Failed to enable dual link.")); if(BLUE_FAIL(set_card_property(blue_, VIDEO_DUAL_LINK_OUTPUT_SIGNAL_FORMAT_TYPE, Signal_FormatType_4224))) - BOOST_THROW_EXCEPTION(bluefish_exception() << msg_info(narrow(print()) + " Failed to set dual link format type to 4:2:2:4.")); + BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Failed to set dual link format type to 4:2:2:4.")); //Select output memory format - if(BLUE_FAIL(set_card_property(blue_, VIDEO_MEMORY_FORMAT, mem_fmt_))) - BOOST_THROW_EXCEPTION(bluefish_exception() << msg_info(narrow(print()) + " Failed to set memory format.")); + if(BLUE_FAIL(set_card_property(blue_, VIDEO_MEMORY_FORMAT, MEM_FMT_ARGB_PC))) + BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Failed to set memory format.")); //Select image orientation if(BLUE_FAIL(set_card_property(blue_, VIDEO_IMAGE_ORIENTATION, ImageOrientation_Normal))) @@ -219,24 +144,35 @@ public: if(blue_->GetHDCardType(device_index_) != CRD_HD_INVALID) blue_->Set_DownConverterSignalType(vid_fmt_ == VID_FMT_PAL ? SD_SDI : HD_SDI); - if(BLUE_FAIL(blue_->set_video_engine(*reinterpret_cast(&engine_mode_)))) - BOOST_THROW_EXCEPTION(bluefish_exception() << msg_info(narrow(print()) + " Failed to set video engine.")); + unsigned long engine_mode = VIDEO_ENGINE_FRAMESTORE; + if(BLUE_FAIL(blue_->set_video_engine(engine_mode))) + BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Failed to set video engine.")); enable_video_output(); - for(size_t n = 0; n < reserved_frames_.size(); ++n) - reserved_frames_[n] = std::make_shared(format_desc_.size, n); + int n = 0; + std::generate(reserved_frames_.begin(), reserved_frames_.end(), [&] + { + return std::make_shared(format_desc_.size, n++); + }); CASPAR_LOG(info) << print() << L" Successfully Initialized."; } ~bluefish_consumer() { - executor_.invoke([&] + try { - disable_video_output(); - blue_->device_detach(); - }); + executor_.invoke([&] + { + disable_video_output(); + blue_->device_detach(); + }); + } + catch(...) + { + CASPAR_LOG_CURRENT_EXCEPTION(); + } CASPAR_LOG(info) << print() << L" Shutting down."; } @@ -258,20 +194,20 @@ public: CASPAR_LOG(error)<< print() << TEXT(" Failed to disable video output."); } - void send(const safe_ptr& frame) + void send(const safe_ptr& frame) { if(preroll_count_ < executor_.capacity()) { while(preroll_count_++ < executor_.capacity()) - schedule_next_video(core::read_frame::empty()); + schedule_next_video(make_safe()); } schedule_next_video(frame); } - void schedule_next_video(const safe_ptr& frame) + void schedule_next_video(const safe_ptr& frame) { - static std::vector silence(MAX_HANC_BUFFER_SIZE, 0); + static std::vector silence(MAX_HANC_BUFFER_SIZE, 0); executor_.begin_invoke([=] { @@ -282,23 +218,34 @@ public: frame_timer_.restart(); + // Copy to local buffers + if(!frame->image_data().empty()) - fast_memcpy(reserved_frames_.front()->image_data(), frame->image_data().begin(), frame->image_data().size()); + { + if(key_only_) + fast_memshfl(reserved_frames_.front()->image_data(), frame->image_data().begin(), frame->image_data().size(), 0x0F0F0F0F, 0x0B0B0B0B, 0x07070707, 0x03030303); + else + fast_memcpy(reserved_frames_.front()->image_data(), frame->image_data().begin(), frame->image_data().size()); + } else fast_memclr(reserved_frames_.front()->image_data(), reserved_frames_.front()->image_size()); + + // Sync 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)); + // Send and display + if(embedded_audio_) { - auto frame_audio_data = frame->audio_data().empty() ? silence.data() : const_cast(frame->audio_data().begin()); + auto frame_audio_data = frame->audio_data().empty() ? silence.data() : const_cast(frame->audio_data().begin()); encode_hanc(reinterpret_cast(reserved_frames_.front()->hanc_data()), frame_audio_data, audio_samples, audio_nchannels); - blue_->system_buffer_write_async(const_cast(reserved_frames_.front()->image_data()), + blue_->system_buffer_write_async(const_cast(reserved_frames_.front()->image_data()), reserved_frames_.front()->image_size(), nullptr, BlueImage_HANC_DMABuffer(reserved_frames_.front()->id(), BLUE_DATA_IMAGE)); @@ -313,7 +260,7 @@ public: } else { - blue_->system_buffer_write_async(const_cast(reserved_frames_.front()->image_data()), + blue_->system_buffer_write_async(const_cast(reserved_frames_.front()->image_data()), reserved_frames_.front()->image_size(), nullptr, BlueImage_DMABuffer(reserved_frames_.front()->id(), BLUE_DATA_IMAGE)); @@ -340,8 +287,8 @@ public: 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(); - auto sample_type = (AUDIO_CHANNEL_16BIT | AUDIO_CHANNEL_LITTLEENDIAN); + const auto sample_type = AUDIO_CHANNEL_16BIT | AUDIO_CHANNEL_LITTLEENDIAN; + const auto emb_audio_flag = blue_emb_audio_enable | blue_emb_audio_group1_enable; hanc_stream_info_struct hanc_stream_info; memset(&hanc_stream_info, 0, sizeof(hanc_stream_info)); @@ -350,15 +297,13 @@ public: hanc_stream_info.AudioDBNArray[1] = -1; hanc_stream_info.AudioDBNArray[2] = -1; hanc_stream_info.AudioDBNArray[3] = -1; - hanc_stream_info.hanc_data_ptr = hanc_data; - hanc_stream_info.video_mode = vid_fmt_; + hanc_stream_info.hanc_data_ptr = hanc_data; + hanc_stream_info.video_mode = vid_fmt_; - auto emb_audio_flag = (blue_emb_audio_enable | blue_emb_audio_group1_enable); - - if (!is_epoch_card(card_type)) + if (!is_epoch_card(*blue_)) encode_hanc_frame(&hanc_stream_info, audio_data, audio_nchannels, audio_samples, sample_type, emb_audio_flag); else - encode_hanc_frame_ex(card_type, &hanc_stream_info, audio_data, audio_nchannels, audio_samples, sample_type, emb_audio_flag); + encode_hanc_frame_ex(blue_->has_video_cardtype(), &hanc_stream_info, audio_data, audio_nchannels, audio_samples, sample_type, emb_audio_flag); } std::wstring print() const @@ -372,22 +317,46 @@ struct bluefish_consumer_proxy : public core::frame_consumer std::unique_ptr consumer_; const size_t device_index_; const bool embedded_audio_; - bool key_only_; + const bool key_only_; + core::video_format_desc format_desc_; + size_t fail_count_; 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){} + , key_only_(key_only) + , fail_count_(0) + { + } virtual void initialize(const core::video_format_desc& format_desc) { - consumer_.reset(new bluefish_consumer(format_desc, device_index_, embedded_audio_)); + format_desc_ = format_desc; + consumer_.reset(new bluefish_consumer(format_desc, device_index_, embedded_audio_, key_only_)); } - virtual void send(const safe_ptr& frame) + virtual bool send(const safe_ptr& frame) { - consumer_->send(frame); + if(!consumer_) + consumer_.reset(new bluefish_consumer(format_desc_, device_index_, embedded_audio_, key_only_)); + + try + { + consumer_->send(frame); + fail_count_ = 0; + } + catch(...) + { + consumer_.reset(); + + if(fail_count_++ > 3) + return false; // Outside didn't handle exception properly, just give up. + + throw; + } + + return true; } virtual const core::video_format_desc& get_video_format_desc() const @@ -397,73 +366,31 @@ public: virtual std::wstring print() const { - return consumer_->print(); - } + if(consumer_) + consumer_->print(); - virtual bool key_only() const - { - return key_only_; + return L"bluefish [" + boost::lexical_cast(device_index_) + L"]"; } }; -std::wstring get_bluefish_version() -{ - try - { - blue_initialize(); - } - catch(...) - { - return L"Not found"; - } - if(!BlueVelvetVersion) - return L"Unknown"; - - return widen(std::string(BlueVelvetVersion())); -} - -std::vector get_bluefish_device_list() -{ - std::vector devices; - - try - { - if(!BlueVelvetFactory4) - return devices; - - std::shared_ptr blue(BlueVelvetFactory4()); - - for(int n = 1; BLUE_PASS(blue->device_attach(n, FALSE)); ++n) - { - devices.push_back(std::wstring(get_card_desc(blue->has_video_cardtype())) + L" [" + boost::lexical_cast(n) + L"]"); - blue->device_detach(); - } - } - catch(...){} - - return devices; -} - safe_ptr create_bluefish_consumer(const std::vector& params) { if(params.size() < 1 || params[0] != L"BLUEFISH") return core::frame_consumer::empty(); - int device_index = 1; - if(params.size() > 1) - device_index = lexical_cast_or_default(params[1], 1); + const auto device_index = params.size() > 1 ? lexical_cast_or_default(params[1], 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(); + const auto embedded_audio = std::find(params.begin(), params.end(), L"EMBEDDED_AUDIO") != params.end(); + const auto key_only = std::find(params.begin(), params.end(), L"KEY_ONLY") != params.end(); return make_safe(device_index, embedded_audio, key_only); } safe_ptr create_bluefish_consumer(const boost::property_tree::ptree& ptree) { - auto device_index = ptree.get("device", 1); - auto embedded_audio = ptree.get("embedded-audio", false); - bool key_only = ptree.get("key-only", false); + const auto device_index = ptree.get("device", 1); + const auto embedded_audio = ptree.get("embedded-audio", false); + const auto key_only = ptree.get("key-only", false); return make_safe(device_index, embedded_audio, key_only); }