X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=modules%2Fbluefish%2Fconsumer%2Fbluefish_consumer.cpp;h=162a3d4128f49989fa79791f71c418dec5d41f9d;hb=efa58f1b76624ca8dd0064a9154414d08b52c8b6;hp=afcac5196a95e40e00658a0a5e2d4f9d461d7a71;hpb=f6f9b178229996940c3da7461720051f5b82370d;p=casparcg diff --git a/modules/bluefish/consumer/bluefish_consumer.cpp b/modules/bluefish/consumer/bluefish_consumer.cpp index afcac5196..162a3d412 100644 --- a/modules/bluefish/consumer/bluefish_consumer.cpp +++ b/modules/bluefish/consumer/bluefish_consumer.cpp @@ -1,21 +1,22 @@ /* -* copyright (c) 2010 Sveriges Television AB +* Copyright (c) 2011 Sveriges Television AB * -* This file is part of CasparCG. +* This file is part of CasparCG (www.casparcg.com). * -* CasparCG is free software: you can redistribute it and/or modify -* it under the terms of the GNU General Public License as published by -* the Free Software Foundation, either version 3 of the License, or -* (at your option) any later version. +* CasparCG is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. * -* CasparCG is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -* GNU General Public License for more details. - -* You should have received a copy of the GNU General Public License -* along with CasparCG. If not, see . +* CasparCG is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with CasparCG. If not, see . * +* Author: Robert Nagy, ronag89@gmail.com */ #include "../StdAfx.h" @@ -24,32 +25,40 @@ #include "../util/blue_velvet.h" #include "../util/memory.h" +#include #include #include #include -#include #include +#include +#include #include +#include +#include + #include #include +#include +#include #include #include -namespace caspar { +namespace caspar { namespace bluefish { struct bluefish_consumer : boost::noncopyable { safe_ptr blue_; const unsigned int device_index_; const core::video_format_desc format_desc_; + const int channel_index_; const std::wstring model_name_; - std::shared_ptr graph_; + safe_ptr graph_; boost::timer frame_timer_; boost::timer tick_timer_; boost::timer sync_timer_; @@ -57,57 +66,52 @@ struct bluefish_consumer : boost::noncopyable unsigned int vid_fmt_; std::array reserved_frames_; - tbb::concurrent_bounded_queue> frame_buffer_; - - int preroll_count_; - - const bool embedded_audio_; + tbb::concurrent_bounded_queue> frame_buffer_; + const bool embedded_audio_; + const bool key_only_; + executor executor_; public: - bluefish_consumer(const core::video_format_desc& format_desc, unsigned int device_index, bool embedded_audio) - : blue_(create_blue()) + bluefish_consumer(const core::video_format_desc& format_desc, unsigned int device_index, bool embedded_audio, bool key_only, int channel_index) + : blue_(create_blue(device_index)) , device_index_(device_index) , format_desc_(format_desc) + , channel_index_(channel_index) , 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()) { - if(BLUE_FAIL(blue_->device_attach(device_index, FALSE))) - BOOST_THROW_EXCEPTION(bluefish_exception() << msg_info("Failed to attach device.")); - - executor_.set_capacity(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_->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)); - graph_->set_color("input-buffer", diagnostics::color(1.0f, 1.0f, 0.0f)); + executor_.set_capacity(1); + + graph_->set_color("tick-time", diagnostics::color(0.0f, 0.6f, 0.9f)); + graph_->set_color("sync-time", diagnostics::color(1.0f, 0.0f, 0.0f)); + graph_->set_color("frame-time", diagnostics::color(0.5f, 1.0f, 0.2f)); + graph_->set_text(print()); + diagnostics::register_graph(graph_); //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_FRAME))) - BOOST_THROW_EXCEPTION(bluefish_exception() << msg_info(narrow(print()) + " Failed to set update type.")); + 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_ARGB_PC))) - BOOST_THROW_EXCEPTION(bluefish_exception() << msg_info(narrow(print()) + " Failed to set memory format.")); + 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))) @@ -142,37 +146,31 @@ public: if(blue_->GetHDCardType(device_index_) != CRD_HD_INVALID) blue_->Set_DownConverterSignalType(vid_fmt_ == VID_FMT_PAL ? SD_SDI : HD_SDI); - unsigned long engine_mode = VIDEO_ENGINE_FRAMESTORE; - if(BLUE_FAIL(blue_->set_video_engine(engine_mode))) - BOOST_THROW_EXCEPTION(bluefish_exception() << msg_info(narrow(print()) + " Failed to set video engine.")); - + if(BLUE_FAIL(set_card_property(blue_, VIDEO_OUTPUT_ENGINE, VIDEO_ENGINE_FRAMESTORE))) + CASPAR_LOG(warning) << print() << TEXT(" Failed to set video engine."); + enable_video_output(); 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."; + boost::range::generate(reserved_frames_, [&]{return std::make_shared(format_desc_.size, n++);}); } ~bluefish_consumer() { - executor_.invoke([&] + try { - disable_video_output(); - blue_->device_detach(); - }); - - CASPAR_LOG(info) << print() << L" Shutting down."; + executor_.invoke([&] + { + disable_video_output(); + blue_->device_detach(); + }); + } + catch(...) + { + CASPAR_LOG_CURRENT_EXCEPTION(); + } } - const core::video_format_desc& get_video_format_desc() const - { - return format_desc_; - } - void enable_video_output() { if(!BLUE_PASS(set_card_property(blue_, VIDEO_BLACKGENERATOR, 0))) @@ -181,100 +179,92 @@ public: void disable_video_output() { + blue_->video_playback_stop(0,0); if(!BLUE_PASS(set_card_property(blue_, VIDEO_BLACKGENERATOR, 1))) CASPAR_LOG(error)<< print() << TEXT(" Failed to disable video output."); } - void send(const safe_ptr& frame) - { - if(preroll_count_ < executor_.capacity()) - { - while(preroll_count_++ < executor_.capacity()) - schedule_next_video(make_safe()); - } - - schedule_next_video(frame); - } - - void schedule_next_video(const safe_ptr& frame) - { - static std::vector silence(MAX_HANC_BUFFER_SIZE, 0); - + void send(const safe_ptr& frame) + { executor_.begin_invoke([=] { try + { + display_frame(frame); + graph_->set_value("tick-time", static_cast(tick_timer_.elapsed()*format_desc_.fps*0.5)); + tick_timer_.restart(); + } + catch(...) { - const size_t audio_samples = format_desc_.audio_samples_per_frame; - const size_t audio_nchannels = format_desc_.audio_channels; - - 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()); - else - fast_memclr(reserved_frames_.front()->image_data(), reserved_frames_.front()->image_size()); + CASPAR_LOG_CURRENT_EXCEPTION(); + } + }); + } - // Sync + void display_frame(const safe_ptr& frame) + { + // 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)); + sync_timer_.restart(); + unsigned long n_field = 0; + blue_->wait_output_video_synch(UPD_FMT_FRAME, n_field); + graph_->set_value("sync-time", sync_timer_.elapsed()*format_desc_.fps*0.5); + + frame_timer_.restart(); - // Send and display + // Copy to local buffers + + if(!frame->image_data().empty()) + { + if(key_only_) + fast_memshfl(reserved_frames_.front()->image_data(), std::begin(frame->image_data()), frame->image_data().size(), 0x0F0F0F0F, 0x0B0B0B0B, 0x07070707, 0x03030303); + else + fast_memcpy(reserved_frames_.front()->image_data(), std::begin(frame->image_data()), frame->image_data().size()); + } + else + fast_memclr(reserved_frames_.front()->image_data(), reserved_frames_.front()->image_size()); + - if(embedded_audio_) - { - auto frame_audio_data = frame->audio_data().empty() ? silence.data() : const_cast(frame->audio_data().begin()); + // Send and display - encode_hanc(reinterpret_cast(reserved_frames_.front()->hanc_data()), frame_audio_data, audio_samples, audio_nchannels); + if(embedded_audio_) + { + auto frame_audio = core::audio_32_to_24(frame->audio_data()); + encode_hanc(reinterpret_cast(reserved_frames_.front()->hanc_data()), frame_audio.data(), frame->audio_data().size()/format_desc_.audio_channels, format_desc_.audio_channels); - 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)); - - blue_->system_buffer_write_async(reserved_frames_.front()->hanc_data(), - reserved_frames_.front()->hanc_size(), - nullptr, - BlueImage_HANC_DMABuffer(reserved_frames_.front()->id(), BLUE_DATA_HANC)); - - if(BLUE_FAIL(blue_->render_buffer_update(BlueBuffer_Image_HANC(reserved_frames_.front()->id())))) - CASPAR_LOG(warning) << print() << TEXT(" render_buffer_update failed."); - } - else - { - 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)); + 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)); + + blue_->system_buffer_write_async(reserved_frames_.front()->hanc_data(), + reserved_frames_.front()->hanc_size(), + nullptr, + BlueImage_HANC_DMABuffer(reserved_frames_.front()->id(), BLUE_DATA_HANC)); + + if(BLUE_FAIL(blue_->render_buffer_update(BlueBuffer_Image_HANC(reserved_frames_.front()->id())))) + CASPAR_LOG(warning) << print() << TEXT(" render_buffer_update failed."); + } + else + { + 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)); - if(BLUE_FAIL(blue_->render_buffer_update(BlueBuffer_Image(reserved_frames_.front()->id())))) - CASPAR_LOG(warning) << print() << TEXT(" render_buffer_update failed."); - } - - std::rotate(reserved_frames_.begin(), reserved_frames_.begin() + 1, reserved_frames_.end()); - - graph_->update_value("frame-time", static_cast(frame_timer_.elapsed()*format_desc_.fps*0.5)); + if(BLUE_FAIL(blue_->render_buffer_update(BlueBuffer_Image(reserved_frames_.front()->id())))) + CASPAR_LOG(warning) << print() << TEXT(" render_buffer_update failed."); + } - graph_->update_value("tick-time", static_cast(tick_timer_.elapsed()*format_desc_.fps*0.5)); - tick_timer_.restart(); - } - catch(...) - { - CASPAR_LOG_CURRENT_EXCEPTION(); - } - graph_->set_value("input-buffer", static_cast(executor_.size())/static_cast(executor_.capacity())); - }); - graph_->set_value("input-buffer", static_cast(executor_.size())/static_cast(executor_.capacity())); + boost::range::rotate(reserved_frames_, std::begin(reserved_frames_)+1); + + graph_->set_value("frame-time", static_cast(frame_timer_.elapsed()*format_desc_.fps*0.5)); } void encode_hanc(BLUE_UINT32* hanc_data, void* audio_data, size_t audio_samples, size_t audio_nchannels) { - static const auto sample_type = AUDIO_CHANNEL_16BIT | AUDIO_CHANNEL_LITTLEENDIAN; - static const auto emb_audio_flag = blue_emb_audio_enable | blue_emb_audio_group1_enable; + const auto sample_type = AUDIO_CHANNEL_24BIT | 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)); @@ -294,7 +284,8 @@ public: std::wstring print() const { - return model_name_ + L" [" + boost::lexical_cast(device_index_) + L"|" + format_desc_.name + L"]"; + return model_name_ + L" [" + boost::lexical_cast(channel_index_) + L"-" + + boost::lexical_cast(device_index_) + L"|" + format_desc_.name + L"]"; } }; @@ -304,40 +295,71 @@ struct bluefish_consumer_proxy : public core::frame_consumer const size_t device_index_; const bool embedded_audio_; const bool key_only_; + std::vector audio_cadence_; 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) + , key_only_(key_only) { - consumer_.reset(new bluefish_consumer(format_desc, device_index_, embedded_audio_)); } - virtual void send(const safe_ptr& frame) + ~bluefish_consumer_proxy() { - consumer_->send(frame); + if(consumer_) + { + auto str = print(); + consumer_.reset(); + CASPAR_LOG(info) << str << L" Successfully Uninitialized."; + } } - virtual const core::video_format_desc& get_video_format_desc() const + // frame_consumer + + virtual void initialize(const core::video_format_desc& format_desc, int channel_index) override { - return consumer_->get_video_format_desc(); + consumer_.reset(new bluefish_consumer(format_desc, device_index_, embedded_audio_, key_only_, channel_index)); + audio_cadence_ = format_desc.audio_cadence; + CASPAR_LOG(info) << print() << L" Successfully Initialized."; } - virtual std::wstring print() const + virtual bool send(const safe_ptr& frame) override { - return consumer_->print(); + CASPAR_VERIFY(audio_cadence_.front() == static_cast(frame->audio_data().size())); + boost::range::rotate(audio_cadence_, std::begin(audio_cadence_)+1); + + consumer_->send(frame); + return true; + } + + virtual std::wstring print() const override + { + return consumer_ ? consumer_->print() : L"[bluefish_consumer]"; } - virtual bool key_only() const + virtual boost::property_tree::wptree info() const override + { + boost::property_tree::wptree info; + info.add(L"type", L"bluefish-consumer"); + info.add(L"key-only", key_only_); + info.add(L"device", device_index_); + info.add(L"embedded-audio", embedded_audio_); + return info; + } + + size_t buffer_depth() const override + { + return 1; + } + + virtual int index() const override { - return key_only_; + return 400 + device_index_; } }; -safe_ptr create_bluefish_consumer(const std::vector& params) +safe_ptr create_consumer(const std::vector& params) { if(params.size() < 1 || params[0] != L"BLUEFISH") return core::frame_consumer::empty(); @@ -350,13 +372,13 @@ safe_ptr create_bluefish_consumer(const std::vector(device_index, embedded_audio, key_only); } -safe_ptr create_bluefish_consumer(const boost::property_tree::ptree& ptree) +safe_ptr create_consumer(const boost::property_tree::wptree& ptree) { - 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); + const auto device_index = ptree.get(L"device", 1); + const auto embedded_audio = ptree.get(L"embedded-audio", false); + const auto key_only = ptree.get(L"key-only", false); return make_safe(device_index, embedded_audio, key_only); } -} \ No newline at end of file +}} \ No newline at end of file