X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=modules%2Fbluefish%2Fconsumer%2Fbluefish_consumer.cpp;h=aa354394429b853cc3412091ed601dc05fecc6c9;hb=d07d65aa2999fac4c8aa3f9e8ec8819179a97c84;hp=d2e5a7f07386e1a382b6c380c5b55a2daaf9bddd;hpb=6d0f9227b505b479513b8220fc23ddc68a22bdbd;p=casparcg diff --git a/modules/bluefish/consumer/bluefish_consumer.cpp b/modules/bluefish/consumer/bluefish_consumer.cpp index d2e5a7f07..aa3543944 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 2013 Sveriges Television AB http://casparcg.com/ * -* 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,6 +25,8 @@ #include "../util/blue_velvet.h" #include "../util/memory.h" +#include +#include #include #include @@ -37,23 +40,29 @@ #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 core::channel_layout channel_layout_; const std::wstring model_name_; - std::shared_ptr graph_; + safe_ptr graph_; boost::timer frame_timer_; boost::timer tick_timer_; boost::timer sync_timer_; @@ -62,34 +71,40 @@ struct bluefish_consumer : boost::noncopyable std::array reserved_frames_; tbb::concurrent_bounded_queue> frame_buffer_; - - int preroll_count_; - + tbb::atomic presentation_delay_millis_; + std::shared_ptr previous_frame_; + 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, bool key_only) + bluefish_consumer( + const core::video_format_desc& format_desc, + unsigned int device_index, + bool embedded_audio, + bool key_only, + int channel_index, + const core::channel_layout& channel_layout) : blue_(create_blue(device_index)) , device_index_(device_index) , format_desc_(format_desc) + , channel_index_(channel_index) + , channel_layout_(channel_layout) , 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(core::consumer_buffer_depth()); + executor_.set_capacity(1); + presentation_delay_millis_ = 0; - graph_ = diagnostics::create_graph(narrow(print())); - graph_->add_guide("tick-time", 0.5); 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)); - graph_->set_color("input-buffer", diagnostics::color(1.0f, 1.0f, 0.0f)); + 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_))) @@ -131,7 +146,19 @@ public: } else { - if(BLUE_FAIL(set_card_property(blue_, EMBEDEDDED_AUDIO_OUTPUT, blue_emb_audio_enable | blue_emb_audio_group1_enable))) + ULONG audio_value = + EMBEDDED_AUDIO_OUTPUT | blue_emb_audio_group1_enable; + + if (channel_layout_.num_channels > 4) + audio_value |= blue_emb_audio_group2_enable; + + if (channel_layout_.num_channels > 8) + audio_value |= blue_emb_audio_group3_enable; + + if (channel_layout_.num_channels > 12) + audio_value |= blue_emb_audio_group4_enable; + + if(BLUE_FAIL(set_card_property(blue_, EMBEDEDDED_AUDIO_OUTPUT, audio_value))) CASPAR_LOG(warning) << print() << TEXT(" Failed to enable embedded audio."); CASPAR_LOG(info) << print() << TEXT(" Enabled embedded-audio."); } @@ -145,19 +172,13 @@ 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(caspar_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() @@ -174,15 +195,8 @@ public: { CASPAR_LOG_CURRENT_EXCEPTION(); } - - CASPAR_LOG(info) << print() << L" Shutting down."; } - 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))) @@ -191,106 +205,117 @@ 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) + boost::unique_future send(const safe_ptr& frame) { - static std::vector silence(MAX_HANC_BUFFER_SIZE, 0); - - executor_.begin_invoke([=] + return executor_.begin_invoke([=]() -> bool { 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()) - { - 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 + CASPAR_LOG_CURRENT_EXCEPTION(); + } - 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)); + return true; + }); + } + + 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_->set_value("sync-time", sync_timer_.elapsed()*format_desc_.fps*0.5); + + frame_timer_.restart(); - // Send and display + if (previous_frame_) + presentation_delay_millis_ = previous_frame_->get_age_millis(); - if(embedded_audio_) - { - auto frame_audio = core::audio_32_to_16_sse(frame->audio_data()); - auto frame_audio_data = frame_audio.size() != audio_samples ? silence.data() : frame_audio.data(); + previous_frame_ = frame; - encode_hanc(reinterpret_cast(reserved_frames_.front()->hanc_data()), frame_audio_data, audio_samples, audio_nchannels); + // 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()); - 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)); + // Send and display - 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())); + if(embedded_audio_) + { + auto src_view = frame->multichannel_view(); + auto frame_audio = core::audio_32_to_24( + core::get_rearranged_and_mixed( + src_view, + channel_layout_, + channel_layout_.num_channels)); + encode_hanc( + reinterpret_cast(reserved_frames_.front()->hanc_data()), + frame_audio.data(), + src_view.num_samples(), + channel_layout_.num_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)); + + if(BLUE_FAIL(blue_->render_buffer_update(BlueBuffer_Image(reserved_frames_.front()->id())))) + CASPAR_LOG(warning) << print() << TEXT(" render_buffer_update failed."); + } + + 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) { - const auto sample_type = AUDIO_CHANNEL_16BIT | AUDIO_CHANNEL_LITTLEENDIAN; - const auto emb_audio_flag = blue_emb_audio_enable | blue_emb_audio_group1_enable; + const auto sample_type = AUDIO_CHANNEL_24BIT | AUDIO_CHANNEL_LITTLEENDIAN; + auto emb_audio_flag = blue_emb_audio_enable | blue_emb_audio_group1_enable; + + if (audio_nchannels > 4) + emb_audio_flag |= blue_emb_audio_group2_enable; + + if (audio_nchannels > 8) + emb_audio_flag |= blue_emb_audio_group3_enable; + + if (audio_nchannels > 12) + emb_audio_flag |= blue_emb_audio_group4_enable; hanc_stream_info_struct hanc_stream_info; memset(&hanc_stream_info, 0, sizeof(hanc_stream_info)); @@ -310,7 +335,13 @@ 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"]"; + } + + int64_t presentation_delay_millis() const + { + return presentation_delay_millis_; } }; @@ -320,62 +351,119 @@ 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_; core::video_format_desc format_desc_; + core::channel_layout channel_layout_; + public: - bluefish_consumer_proxy(size_t device_index, bool embedded_audio, bool key_only) + bluefish_consumer_proxy( + size_t device_index, + bool embedded_audio, + bool key_only, + const core::channel_layout& channel_layout) : device_index_(device_index) , embedded_audio_(embedded_audio) , key_only_(key_only) + , channel_layout_(channel_layout) { } - virtual void initialize(const core::video_format_desc& format_desc) + ~bluefish_consumer_proxy() { - format_desc_ = format_desc; - consumer_.reset(new bluefish_consumer(format_desc, device_index_, embedded_audio_, key_only_)); + if(consumer_) + { + auto str = print(); + consumer_.reset(); + CASPAR_LOG(info) << str << L" Successfully Uninitialized."; + } } + + // frame_consumer - virtual bool send(const safe_ptr& frame) + virtual void initialize( + const core::video_format_desc& format_desc, + const core::channel_layout& audio_channel_layout, + int channel_index) override { - consumer_->send(frame); - return true; + consumer_.reset(new bluefish_consumer( + format_desc, + device_index_, + embedded_audio_, + key_only_, + channel_index, + channel_layout_)); + audio_cadence_ = format_desc.audio_cadence; + format_desc_ = format_desc; + CASPAR_LOG(info) << print() << L" Successfully Initialized."; } - virtual const core::video_format_desc& get_video_format_desc() const + virtual boost::unique_future send(const safe_ptr& frame) override + { + CASPAR_VERIFY(audio_cadence_.front() * frame->num_channels() == static_cast(frame->audio_data().size())); + boost::range::rotate(audio_cadence_, std::begin(audio_cadence_)+1); + + return consumer_->send(frame); + } + + virtual std::wstring print() const override { - return consumer_->get_video_format_desc(); + return consumer_ ? consumer_->print() : L"[bluefish_consumer]"; + } + + 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_); + info.add(L"presentation-frame-age", presentation_frame_age_millis()); + return info; + } + + virtual size_t buffer_depth() const override + { + return 1; } - virtual std::wstring print() const + virtual int index() const override { - if(consumer_) - consumer_->print(); + return 400 + device_index_; + } - return L"bluefish [" + boost::lexical_cast(device_index_) + L"]"; + virtual int64_t presentation_frame_age_millis() const override + { + return consumer_ ? consumer_->presentation_delay_millis() : 0; } }; -safe_ptr create_bluefish_consumer(const std::vector& params) +safe_ptr create_consumer(const core::parameters& params) { if(params.size() < 1 || params[0] != L"BLUEFISH") return core::frame_consumer::empty(); const auto device_index = params.size() > 1 ? lexical_cast_or_default(params[1], 1) : 1; - 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(); + 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(); + const auto audio_layout = core::default_channel_layout_repository().get_by_name( + params.get(L"CHANNEL_LAYOUT", L"STEREO")); - return make_safe(device_index, embedded_audio, key_only); + return make_safe(device_index, embedded_audio, key_only, audio_layout); } -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); - - return make_safe(device_index, embedded_audio, key_only); + 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); + const auto audio_layout = + core::default_channel_layout_repository().get_by_name( + boost::to_upper_copy(ptree.get(L"channel-layout", L"STEREO"))); + + return make_safe( + device_index, embedded_audio, key_only, audio_layout); } -} \ No newline at end of file +}} \ No newline at end of file