X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=modules%2Fflash%2Fproducer%2Fflash_producer.cpp;h=1521b50d3bb46328d23acfaa6f0bbc1eebaaf050;hb=b31bcb48f04da7f5e222343be4b234445fbe3b24;hp=69bcd50b04479caf589b346cd64a9c59b0448ff5;hpb=dfbde5d6d0fcff021c60c0240d94fece52481cf5;p=casparcg diff --git a/modules/flash/producer/flash_producer.cpp b/modules/flash/producer/flash_producer.cpp index 69bcd50b0..1521b50d3 100644 --- a/modules/flash/producer/flash_producer.cpp +++ b/modules/flash/producer/flash_producer.cpp @@ -45,6 +45,8 @@ #include #include +#include + #include namespace caspar { @@ -67,6 +69,9 @@ public: bmp_.reset(CreateDIBSection(static_cast(hdc_.get()), &info, DIB_RGB_COLORS, reinterpret_cast(&bmp_data_), 0, 0), DeleteObject); SelectObject(static_cast(hdc_.get()), bmp_.get()); + + if(!bmp_data_) + BOOST_THROW_EXCEPTION(std::bad_alloc()); } operator HDC() {return static_cast(hdc_.get());} @@ -80,10 +85,48 @@ private: std::shared_ptr bmp_; }; +struct template_host +{ + std::string video_mode; + std::string filename; + size_t width; + size_t height; +}; + +template_host get_template_host(const core::video_format_desc& desc) +{ + std::vector template_hosts; + BOOST_FOREACH(auto& xml_mapping, env::properties().get_child("configuration.producers.template-hosts")) + { + try + { + template_host template_host; + template_host.video_mode = xml_mapping.second.get("video-mode", narrow(desc.name)); + template_host.filename = xml_mapping.second.get("filename", "cg.fth"); + template_host.width = xml_mapping.second.get("width", desc.width); + template_host.height = xml_mapping.second.get("height", desc.height); + template_hosts.push_back(template_host); + } + catch(...){} + } + + auto template_host_it = boost::find_if(template_hosts, [&](template_host template_host){return template_host.video_mode == narrow(desc.name);}); + if(template_host_it == template_hosts.end()) + template_host_it = boost::find_if(template_hosts, [&](template_host template_host){return template_host.video_mode == "";}); + + if(template_host_it != template_hosts.end()) + return *template_host_it; + + template_host template_host; + template_host.filename = "cg.fth"; + template_host.width = desc.width; + template_host.height = desc.height; + return template_host; +} + class flash_renderer { const std::wstring filename_; - const core::video_format_desc format_desc_; const std::shared_ptr frame_factory_; @@ -96,19 +139,23 @@ class flash_renderer boost::timer tick_timer_; high_prec_timer timer_; + + const size_t width_; + const size_t height_; public: - flash_renderer(const safe_ptr& graph, const std::shared_ptr& frame_factory, const std::wstring& filename) + flash_renderer(const safe_ptr& graph, const std::shared_ptr& frame_factory, const std::wstring& filename, int width, int height) : graph_(graph) , filename_(filename) - , format_desc_(frame_factory->get_video_format_desc()) , frame_factory_(frame_factory) , ax_(nullptr) , head_(core::basic_frame::empty()) - , bmp_(format_desc_.width, format_desc_.height) + , bmp_(width, height) + , width_(width) + , height_(height) { graph_->add_guide("frame-time", 0.5f); - graph_->set_color("frame-time", diagnostics::color(1.0f, 0.0f, 0.0f)); + graph_->set_color("frame-time", diagnostics::color(0.8f, 0.3f, 0.4f)); graph_->add_guide("tick-time", 0.5); graph_->set_color("tick-time", diagnostics::color(0.1f, 0.7f, 0.8f)); graph_->set_color("param", diagnostics::color(1.0f, 0.5f, 0.0f)); @@ -135,9 +182,10 @@ public: if(FAILED(spFlash->put_ScaleMode(2))) //Exact fit. Scale without respect to the aspect ratio. BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Failed to Set Scale Mode")); - ax_->SetFormat(format_desc_); + ax_->SetSize(width_, height_); CASPAR_LOG(info) << print() << L" Thread started."; + CASPAR_LOG(info) << print() << L" Successfully initialized with template-host: " << filename << L" width: " << width_ << L" height: " << height_ << L"."; } ~flash_renderer() @@ -155,9 +203,6 @@ public: if(!ax_->FlashCall(param)) CASPAR_LOG(warning) << print() << " Flash function call failed. Param: " << param << "."; graph_->add_tag("param"); - - if(abs(ax_->GetFPS() - format_desc_.fps) > 0.01 && abs(ax_->GetFPS()/2.0 - format_desc_.fps) > 0.01) - CASPAR_LOG(warning) << print() << " Invalid frame-rate: " << ax_->GetFPS() << L". Should be either " << format_desc_.fps << L" or " << format_desc_.fps*2.0 << L"."; } safe_ptr render_frame(bool has_underflow) @@ -180,11 +225,11 @@ public: ax_->Tick(); if(ax_->InvalidRect()) { - fast_memclr(bmp_.data(), format_desc_.size); + fast_memclr(bmp_.data(), width_*height_*4); ax_->DrawControl(bmp_); - auto frame = frame_factory_->create_frame(this); - fast_memcpy(frame->image_data().begin(), bmp_.data(), format_desc_.size); + auto frame = frame_factory_->create_frame(this, width_, height_); + fast_memcpy(frame->image_data().begin(), bmp_.data(), width_*height_*4); frame->commit(); head_ = frame; } @@ -214,13 +259,24 @@ struct flash_producer : public core::frame_producer std::shared_ptr graph_; tbb::concurrent_bounded_queue> frame_buffer_; + + safe_ptr last_frame_; - com_context context_; + com_context context_; + + tbb::spin_mutex exception_mutex_; + std::exception_ptr exception_; + + int width_; + int height_; public: - flash_producer(const safe_ptr& frame_factory, const std::wstring& filename) + flash_producer(const safe_ptr& frame_factory, const std::wstring& filename, size_t width, size_t height) : filename_(filename) , frame_factory_(frame_factory) , context_(L"flash_producer") + , last_frame_(core::basic_frame::empty()) + , width_(width > 0 ? width : frame_factory->get_video_format_desc().width) + , height_(height > 0 ? height : frame_factory->get_video_format_desc().height) { if(!boost::filesystem::exists(filename)) BOOST_THROW_EXCEPTION(file_not_found() << boost::errinfo_file_name(narrow(filename))); @@ -230,9 +286,11 @@ public: graph_ = diagnostics::create_graph([this]{return print();}); graph_->set_color("output-buffer", diagnostics::color(0.0f, 1.0f, 0.0f)); - frame_buffer_.set_capacity(2); - - initialize(); + frame_buffer_.set_capacity(1); + + context_.reset([&]{return new flash_renderer(safe_ptr(graph_), frame_factory_, filename_, width_, height_);}); + while(frame_buffer_.try_push(core::basic_frame::empty())){} + render(); } ~flash_producer() @@ -242,68 +300,79 @@ public: // frame_producer - virtual safe_ptr receive() - { + virtual safe_ptr receive(int) + { + { + tbb::spin_mutex::scoped_lock lock(exception_mutex_); + if(exception_ != nullptr) + std::rethrow_exception(exception_); + } + graph_->set_value("output-buffer", static_cast(frame_buffer_.size())/static_cast(frame_buffer_.capacity())); auto frame = core::basic_frame::late(); - frame_buffer_.try_pop(frame); + if(frame_buffer_.try_pop(frame)) + last_frame_ = frame; + return frame; } + + virtual safe_ptr last_frame() const + { + return last_frame_; + } virtual void param(const std::wstring& param) { + { + tbb::spin_mutex::scoped_lock lock(exception_mutex_); + if(exception_ != nullptr) + std::rethrow_exception(exception_); + } + context_.begin_invoke([=] { - if(!context_) - initialize(); - try { context_->param(param); + + const auto& format_desc = frame_factory_->get_video_format_desc(); + if(abs(context_->fps() - format_desc.fps) > 2.0 && abs(context_->fps()/2.0 - format_desc.fps) > 2.0) + CASPAR_LOG(warning) << print() << " Invalid frame-rate: " << context_->fps() << L". Should be either " << format_desc.fps << L" or " << format_desc.fps*2.0 << L"."; } catch(...) { CASPAR_LOG_CURRENT_EXCEPTION(); context_.reset(nullptr); - frame_buffer_.push(core::basic_frame::empty()); + + tbb::spin_mutex::scoped_lock lock(exception_mutex_); + exception_ = std::current_exception(); } }); } virtual std::wstring print() const { - return L"flash[" + boost::filesystem::wpath(filename_).filename() + L", " + - boost::lexical_cast(fps_) + L"]"; + return L"flash[" + boost::filesystem::wpath(filename_).filename() + L", " + boost::lexical_cast(fps_) + L"]"; } // flash_producer - void initialize() - { - context_.reset([&]{return new flash_renderer(safe_ptr(graph_), frame_factory_, filename_);}); - while(frame_buffer_.try_push(core::basic_frame::empty())){} - render(context_.get()); - } - - void render(const flash_renderer* renderer) - { + void render() + { context_.begin_invoke([=] { - if(context_.get() != renderer) // Since initialize will start a new recursive call make sure the recursive calls are only for a specific instance. - return; - try { const auto& format_desc = frame_factory_->get_video_format_desc(); - if(abs(context_->fps()/2.0 - format_desc.fps) < 0.01) // flash == 2 * format -> interlace + if(abs(context_->fps()/2.0 - format_desc.fps) < 2.0) // flash == 2 * format -> interlace { auto frame1 = context_->render_frame(frame_buffer_.size() < frame_buffer_.capacity()); auto frame2 = context_->render_frame(frame_buffer_.size() < frame_buffer_.capacity()); frame_buffer_.push(core::basic_frame::interlace(frame1, frame2, format_desc.mode)); } - else if(abs(context_->fps()- format_desc.fps/2.0) < 0.01) // format == 2 * flash -> duplicate + else if(abs(context_->fps()- format_desc.fps/2.0) < 2.0) // format == 2 * flash -> duplicate { auto frame = context_->render_frame(frame_buffer_.size() < frame_buffer_.capacity()); frame_buffer_.push(frame); @@ -318,13 +387,15 @@ public: graph_->set_value("output-buffer", static_cast(frame_buffer_.size())/static_cast(frame_buffer_.capacity())); fps_.fetch_and_store(static_cast(context_->fps()*100.0)); - render(renderer); + render(); } catch(...) { CASPAR_LOG_CURRENT_EXCEPTION(); context_.reset(nullptr); - frame_buffer_.push(core::basic_frame::empty()); + + tbb::spin_mutex::scoped_lock lock(exception_mutex_); + exception_ = std::current_exception(); } }); } @@ -332,9 +403,9 @@ public: safe_ptr create_flash_producer(const safe_ptr& frame_factory, const std::vector& params) { - std::wstring filename = env::template_folder() + L"\\" + params[0]; + auto template_host = get_template_host(frame_factory->get_video_format_desc()); - return make_safe(frame_factory, filename); + return make_safe(frame_factory, env::template_folder() + L"\\" + widen(template_host.filename), template_host.width, template_host.height); } std::wstring find_flash_template(const std::wstring& template_name)