X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=modules%2Fflash%2Fproducer%2Fflash_producer.cpp;h=c285ba9fcc876ab55a9843bed79fa7e899958dda;hb=04b14225786105cc6fb61bf26f8ba09f80ef9e7e;hp=f24d6ef2fb03b1de414963cdd799f8ad4bab6961;hpb=9647d954c83b0f6930e99fe5cc64b0f73f0d5385;p=casparcg diff --git a/modules/flash/producer/flash_producer.cpp b/modules/flash/producer/flash_producer.cpp index f24d6ef2f..c285ba9fc 100644 --- a/modules/flash/producer/flash_producer.cpp +++ b/modules/flash/producer/flash_producer.cpp @@ -45,10 +45,10 @@ #include #include -#include - #include +#include + namespace caspar { class bitmap @@ -87,7 +87,7 @@ private: struct template_host { - std::string video_mode; + std::string field_mode; std::string filename; size_t width; size_t height; @@ -101,7 +101,7 @@ template_host get_template_host(const core::video_format_desc& desc) try { template_host template_host; - template_host.video_mode = xml_mapping.second.get("video-mode", narrow(desc.name)); + template_host.field_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); @@ -110,9 +110,9 @@ template_host get_template_host(const core::video_format_desc& desc) catch(...){} } - auto template_host_it = boost::find_if(template_hosts, [&](template_host template_host){return template_host.video_mode == narrow(desc.name);}); + auto template_host_it = boost::find_if(template_hosts, [&](template_host template_host){return template_host.field_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 == "";}); + template_host_it = boost::find_if(template_hosts, [&](template_host template_host){return template_host.field_mode == "";}); if(template_host_it != template_hosts.end()) return *template_host_it; @@ -153,13 +153,13 @@ public: , bmp_(width, height) , width_(width) , 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)); + graph_->set_color("skip-sync", diagnostics::color(0.8f, 0.3f, 0.2f)); if(FAILED(CComObject::CreateInstance(&ax_))) BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Failed to create FlashAxContainer")); @@ -201,7 +201,7 @@ public: void param(const std::wstring& param) { if(!ax_->FlashCall(param)) - CASPAR_LOG(warning) << print() << " Flash function call failed. Param: " << param << "."; + CASPAR_LOG(warning) << print() << L" Flash call failed:" << 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"); } @@ -218,7 +218,7 @@ public: if(!has_underflow) timer_.tick(frame_time); // This will block the thread. else - graph_->add_tag("underflow"); + graph_->add_tag("skip-sync"); frame_timer_.restart(); @@ -260,13 +260,11 @@ struct flash_producer : public core::frame_producer tbb::concurrent_bounded_queue> frame_buffer_; + mutable tbb::spin_mutex last_frame_mutex_; safe_ptr last_frame_; com_context context_; - tbb::spin_mutex exception_mutex_; - std::exception_ptr exception_; - int width_; int height_; public: @@ -280,17 +278,16 @@ public: { if(!boost::filesystem::exists(filename)) BOOST_THROW_EXCEPTION(file_not_found() << boost::errinfo_file_name(narrow(filename))); - + fps_ = 0; graph_ = diagnostics::create_graph([this]{return print();}); - graph_->set_color("output-buffer", diagnostics::color(0.0f, 1.0f, 0.0f)); + graph_->set_color("output-buffer-count", diagnostics::color(1.0f, 1.0f, 0.0f)); + graph_->set_color("underflow", diagnostics::color(0.6f, 0.3f, 0.9f)); - 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(); + frame_buffer_.set_capacity(frame_factory_->get_video_format_desc().fps > 30.0 ? 2 : 1); + + initialize(); } ~flash_producer() @@ -301,37 +298,29 @@ public: // frame_producer 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())); + { + graph_->set_value("output-buffer-count", static_cast(frame_buffer_.size())/static_cast(frame_buffer_.capacity())); auto frame = core::basic_frame::late(); - if(frame_buffer_.try_pop(frame)) - last_frame_ = frame; + if(!frame_buffer_.try_pop(frame)) + graph_->add_tag("underflow"); return frame; } virtual safe_ptr last_frame() const { + tbb::spin_mutex::scoped_lock lock(last_frame_mutex_); 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); @@ -344,58 +333,72 @@ public: { CASPAR_LOG_CURRENT_EXCEPTION(); context_.reset(nullptr); - - tbb::spin_mutex::scoped_lock lock(exception_mutex_); - exception_ = std::current_exception(); + frame_buffer_.push(core::basic_frame::empty()); } }); } 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 render() - { + 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()); + } + + safe_ptr render_frame() + { + auto frame = context_->render_frame(frame_buffer_.size() < frame_buffer_.capacity()); + tbb::spin_mutex::scoped_lock lock(last_frame_mutex_); + last_frame_ = make_safe(frame); + return frame; + } + + void render(const flash_renderer* renderer) + { 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) < 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)); + auto frame1 = render_frame(); + auto frame2 = render_frame(); + frame_buffer_.push(core::basic_frame::interlace(frame1, frame2, format_desc.field_mode)); } 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()); + auto frame = render_frame(); frame_buffer_.push(frame); frame_buffer_.push(frame); } else //if(abs(renderer_->fps() - format_desc_.fps) < 0.1) // format == flash -> simple { - auto frame = context_->render_frame(frame_buffer_.size() < frame_buffer_.capacity()); + auto frame = render_frame(); frame_buffer_.push(frame); } - graph_->set_value("output-buffer", static_cast(frame_buffer_.size())/static_cast(frame_buffer_.capacity())); + graph_->set_value("output-buffer-count", static_cast(frame_buffer_.size())/static_cast(frame_buffer_.capacity())); fps_.fetch_and_store(static_cast(context_->fps()*100.0)); - render(); + render(renderer); } catch(...) { CASPAR_LOG_CURRENT_EXCEPTION(); context_.reset(nullptr); - - tbb::spin_mutex::scoped_lock lock(exception_mutex_); - exception_ = std::current_exception(); + frame_buffer_.push(core::basic_frame::empty()); } }); }