X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=modules%2Fflash%2Fproducer%2Fflash_producer.cpp;h=e9f81c795ff18a2ee0fa3a0bfe6e8ab739fd126a;hb=1bfb89a242d5bcf9bb1517e5cb385c2a79bd3f16;hp=2afaf13c5627910faf41d18c9a058f42b356a9d8;hpb=9b159a2497e154deb2f67580b802b702b136a53d;p=casparcg diff --git a/modules/flash/producer/flash_producer.cpp b/modules/flash/producer/flash_producer.cpp index 2afaf13c5..e9f81c795 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 { @@ -83,6 +85,45 @@ 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_; @@ -114,9 +155,9 @@ public: , height_(height) { graph_->add_guide("frame-time", 0.5f); - graph_->set_color("frame-time", diagnostics::color(0.8f, 0.3f, 0.4f)); + graph_->set_color("frame-time", diagnostics::color(0.1f, 1.0f, 0.1f)); 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_->set_color("param", diagnostics::color(1.0f, 0.5f, 0.0f)); graph_->set_color("underflow", diagnostics::color(0.6f, 0.3f, 0.9f)); @@ -144,7 +185,7 @@ public: ax_->SetSize(width_, height_); CASPAR_LOG(info) << print() << L" Thread started."; - CASPAR_LOG(info) << print() << L" Successfully initialized to, width: " << width_ << L" height: " << height_ << L"."; + CASPAR_LOG(info) << print() << L" Successfully initialized with template-host: " << filename << L" width: " << width_ << L" height: " << height_ << L"."; } ~flash_renderer() @@ -160,19 +201,12 @@ public: void param(const std::wstring& param) { if(!ax_->FlashCall(param)) - CASPAR_LOG(warning) << print() << " Flash function call failed. Param: " << param << "."; + BOOST_THROW_EXCEPTION(invalid_operation() << msg_info("Flash function call failed.") << arg_name_info("param") << arg_value_info(narrow(param))); graph_->add_tag("param"); } safe_ptr render_frame(bool has_underflow) { - //if(format_desc_ != frame_factory_->get_video_format_desc()) - //{ - // format_desc_ = frame_factory_->get_video_format_desc(); - // bmp_ = bitmap(format_desc_.width, format_desc_.height); - // ax_->SetFormat(format_desc_); - //} - float frame_time = 1.0f/ax_->GetFPS(); graph_->update_value("tick-time", static_cast(tick_timer_.elapsed()/frame_time)*0.5f); @@ -230,6 +264,9 @@ struct flash_producer : public core::frame_producer com_context context_; + tbb::spin_mutex exception_mutex_; + std::exception_ptr exception_; + int width_; int height_; public: @@ -247,11 +284,10 @@ public: fps_ = 0; graph_ = diagnostics::create_graph([this]{return print();}); - graph_->set_color("output-buffer", diagnostics::color(0.0f, 1.0f, 0.0f)); - - frame_buffer_.set_capacity(1); - - initialize(); + graph_->set_color("buffer-count", diagnostics::color(0.4f, 0.8f, 0.8f)); + + frame_buffer_.set_capacity(1); + initialize(); } ~flash_producer() @@ -259,11 +295,25 @@ public: frame_buffer_.clear(); } + void initialize() + { + context_.reset(nullptr); + context_.reset([&]{return new flash_renderer(safe_ptr(graph_), frame_factory_, filename_, width_, height_);}); + while(frame_buffer_.try_push(core::basic_frame::empty())){} + render(); + } + // frame_producer virtual safe_ptr receive(int) - { - graph_->set_value("output-buffer", static_cast(frame_buffer_.size())/static_cast(frame_buffer_.capacity())); + { + { + tbb::spin_mutex::scoped_lock lock(exception_mutex_); + if(exception_ != nullptr) + std::rethrow_exception(exception_); + } + + graph_->set_value("buffer-count", static_cast(frame_buffer_.size())/static_cast(frame_buffer_.capacity())); auto frame = core::basic_frame::late(); if(frame_buffer_.try_pop(frame)) @@ -279,50 +329,56 @@ public: 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) > 0.01 && abs(context_->fps()/2.0 - format_desc.fps) > 0.01) - 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()); + try + { + CASPAR_LOG_CURRENT_EXCEPTION(); + CASPAR_LOG(error) << print() << L" param failed. Trying to re-initialize flash and re-execute param."; + + initialize(); + context_->param(param); + } + catch(...) + { + CASPAR_LOG_CURRENT_EXCEPTION(); + + context_.reset(nullptr); + + tbb::spin_mutex::scoped_lock lock(exception_mutex_); + exception_ = std::current_exception(); + } } + + 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"."; }); } 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_, width_, height_);}); - 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(); @@ -345,16 +401,18 @@ public: frame_buffer_.push(frame); } - graph_->set_value("output-buffer", static_cast(frame_buffer_.size())/static_cast(frame_buffer_.capacity())); + graph_->set_value("buffer-count", 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(); } }); } @@ -362,18 +420,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]; - - size_t width = frame_factory->get_video_format_desc().width; - size_t height = frame_factory->get_video_format_desc().height; - - if(params.size() >= 3) - { - width = boost::lexical_cast(params[1]); - height = boost::lexical_cast(params[2]); - } + auto template_host = get_template_host(frame_factory->get_video_format_desc()); - return make_safe(frame_factory, filename, width, height); + 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)