X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=modules%2Fflash%2Fproducer%2Fflash_producer.cpp;h=c14158d2b21ebc2dd83b4a9db5bfeb62c39b5da1;hb=65199f18ae5ec831d809ea73b6e9dd01f530b4c5;hp=a7132c23b8682765498d340e7af328f49d6b3301;hpb=69c89fd3ef41155fb4502091f4b3a68490cafda8;p=casparcg diff --git a/modules/flash/producer/flash_producer.cpp b/modules/flash/producer/flash_producer.cpp index a7132c23b..c14158d2b 100644 --- a/modules/flash/producer/flash_producer.cpp +++ b/modules/flash/producer/flash_producer.cpp @@ -22,6 +22,7 @@ #if defined(_MSC_VER) #pragma warning (disable : 4146) +#pragma warning (disable : 4244) #endif #include "flash_producer.h" @@ -30,74 +31,157 @@ #include #include -#include +#include +#include -#include -#include +#include +#include #include +#include +#include +#include #include +#include +#include +#include #include -#include -namespace caspar { +#include + +namespace caspar { namespace flash { -bool interlaced(double fps, const core::video_format_desc& format_desc) +class bitmap { - return abs(fps/2.0 - format_desc.fps) < 0.1; -} +public: + bitmap(size_t width, size_t height) + : bmp_data_(nullptr) + , hdc_(CreateCompatibleDC(0), DeleteDC) + { + BITMAPINFO info; + memset(&info, 0, sizeof(BITMAPINFO)); + info.bmiHeader.biBitCount = 32; + info.bmiHeader.biCompression = BI_RGB; + info.bmiHeader.biHeight = -height; + info.bmiHeader.biPlanes = 1; + info.bmiHeader.biSize = sizeof(BITMAPINFO); + info.bmiHeader.biWidth = width; -class flash_renderer -{ - struct co_init - { - co_init(){CoInitialize(nullptr);} - ~co_init(){CoUninitialize();} - } co_; - - printer parent_printer_; - const std::wstring filename_; - const core::video_format_desc format_desc_; + 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()); - std::shared_ptr frame_factory_; - + if(!bmp_data_) + BOOST_THROW_EXCEPTION(std::bad_alloc()); + } + + operator HDC() {return static_cast(hdc_.get());} + + BYTE* data() { return bmp_data_;} + const BYTE* data() const { return bmp_data_;} + +private: BYTE* bmp_data_; std::shared_ptr hdc_; std::shared_ptr bmp_; +}; + +struct template_host +{ + std::string field_mode; + std::string filename; + size_t width; + size_t height; +}; + +template_host get_template_host(const core::video_format_desc& desc) +{ + try + { + std::vector template_hosts; + BOOST_FOREACH(auto& xml_mapping, env::properties().get_child("configuration.producers.template-hosts")) + { + try + { + template_host template_host; + 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); + template_hosts.push_back(template_host); + } + catch(...){} + } + + 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.field_mode == "";}); + if(template_host_it != template_hosts.end()) + return *template_host_it; + } + catch(...) + { + } + + template_host template_host; + template_host.filename = "cg.fth"; + + for(auto it = boost::filesystem2::wdirectory_iterator(env::template_folder()); it != boost::filesystem2::wdirectory_iterator(); ++it) + { + if(boost::iequals(it->path().extension(), L"." + desc.name)) + { + template_host.filename = narrow(it->filename()); + break; + } + } + + template_host.width = desc.square_width; + template_host.height = desc.square_height; + return template_host; +} + +class flash_renderer +{ + const std::wstring filename_; + + const std::shared_ptr frame_factory_; + CComObject* ax_; safe_ptr head_; + bitmap bmp_; safe_ptr graph_; - timer perf_timer_; - - std::wstring print() - { - return parent_printer_(); - } + boost::timer frame_timer_; + 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, const printer& parent_printer) - : parent_printer_(parent_printer) - , graph_(graph) + 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) - , bmp_data_(nullptr) - , hdc_(CreateCompatibleDC(0), DeleteDC) , ax_(nullptr) , head_(core::basic_frame::empty()) - { + , 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.1f, 1.0f, 0.1f)); + graph_->add_guide("tick-time", 0.5); + 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")); - ax_->set_print(parent_printer_); + ax_->set_print([this]{return L"flash_renderer";}); if(FAILED(ax_->CreateAxControl())) BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Failed to Create FlashAxControl")); @@ -115,20 +199,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_); - - BITMAPINFO info; - memset(&info, 0, sizeof(BITMAPINFO)); - info.bmiHeader.biBitCount = 32; - info.bmiHeader.biCompression = BI_RGB; - info.bmiHeader.biHeight = -format_desc_.height; - info.bmiHeader.biPlanes = 1; - info.bmiHeader.biSize = sizeof(BITMAPINFO); - info.bmiHeader.biWidth = format_desc_.width; - - 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()); - CASPAR_LOG(info) << parent_printer_() << L" Thread started."; + 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() @@ -144,180 +218,230 @@ 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"); - - if(abs(ax_->GetFPS() / format_desc_.fps) > 0.001) - 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) { - if(ax_->IsEmpty()) - return core::basic_frame::empty(); + float frame_time = 1.0f/ax_->GetFPS(); - auto frame = render_simple_frame(has_underflow); - if(interlaced(ax_->GetFPS(), format_desc_)) - frame = core::basic_frame::interlace(frame, render_simple_frame(has_underflow), format_desc_.mode); - return frame; - } - - double fps() const - { - return ax_->GetFPS(); - } - -private: - - safe_ptr render_simple_frame(bool underflow) - { - if(underflow) - graph_->add_tag("underflow"); + graph_->update_value("tick-time", static_cast(tick_timer_.elapsed()/frame_time)*0.5f); + tick_timer_.restart(); - double frame_time = 1.0/ax_->GetFPS(); + if(ax_->IsEmpty()) + return core::basic_frame::empty(); + + if(!has_underflow) + timer_.tick(frame_time); // This will block the thread. + else + graph_->add_tag("skip-sync"); + + frame_timer_.restart(); - perf_timer_.reset(); ax_->Tick(); - if(ax_->InvalidRect()) { - std::fill_n(bmp_data_, format_desc_.size, 0); - ax_->DrawControl(static_cast(hdc_.get())); + fast_memclr(bmp_.data(), width_*height_*4); + ax_->DrawControl(bmp_); - auto frame = frame_factory_->create_frame(); - std::copy_n(bmp_data_, format_desc_.size, frame->image_data().begin()); + auto frame = frame_factory_->create_frame(this, width_, height_); + fast_memcpy(frame->image_data().begin(), bmp_.data(), width_*height_*4); + frame->commit(); head_ = frame; } - - graph_->update_value("frame-time", static_cast(perf_timer_.elapsed()/frame_time)); + + graph_->update_value("frame-time", static_cast(frame_timer_.elapsed()/frame_time)*0.5f); return head_; } + + bool is_empty() const + { + return ax_->IsEmpty(); + } + + double fps() const + { + return ax_->GetFPS(); + } + + std::wstring print() + { + return L"flash[" + boost::filesystem::wpath(filename_).filename() + L"]"; + } }; -struct flash_producer::implementation +struct flash_producer : public core::frame_producer { - printer parent_printer_; const std::wstring filename_; + const safe_ptr frame_factory_; + tbb::atomic fps_; - std::shared_ptr graph_; + safe_ptr graph_; - safe_ptr tail_; tbb::concurrent_bounded_queue> frame_buffer_; - std::shared_ptr renderer_; - std::shared_ptr frame_factory_; - core::video_format_desc format_desc_; - - executor executor_; - - std::wstring print() const - { - return (parent_printer_ ? parent_printer_() + L"/" : L"") + L"flash[" + boost::filesystem::wpath(filename_).filename() + L", " + - boost::lexical_cast(fps_) + - (interlaced(fps_, format_desc_) ? L"i" : L"p") + L"]"; - } - + mutable tbb::spin_mutex last_frame_mutex_; + safe_ptr last_frame_; + + com_context context_; + + int width_; + int height_; public: - implementation(const std::wstring& filename) + flash_producer(const safe_ptr& frame_factory, const std::wstring& filename, size_t width, size_t height) : filename_(filename) - , tail_(core::basic_frame::empty()) - , executor_(print()) + , 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))); - - graph_ = diagnostics::create_graph(boost::bind(&implementation::print, this)); + fps_ = 0; - graph_->set_color("output-buffer", diagnostics::color(0.0f, 1.0f, 0.0f)); - } - ~implementation() - { - executor_.clear(); - executor_.invoke([=] - { - renderer_ = nullptr; - }); - } - - virtual void initialize(const safe_ptr& frame_factory) - { - frame_factory_ = frame_factory; - format_desc_ = frame_factory->get_video_format_desc(); - frame_buffer_.set_capacity(static_cast(format_desc_.fps/4.0)); - executor_.start(); + 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)); + graph_->set_text(print()); + diagnostics::register_graph(graph_); + + frame_buffer_.set_capacity(frame_factory_->get_video_format_desc().fps > 30.0 ? 2 : 1); + + initialize(); } - virtual void set_parent_printer(const printer& parent_printer) + ~flash_producer() { - parent_printer_ = parent_printer; + frame_buffer_.clear(); } - virtual safe_ptr receive() - { - if(!renderer_) - return core::basic_frame::empty(); + // frame_producer - graph_->set_value("output-buffer", static_cast(frame_buffer_.size())/static_cast(frame_buffer_.capacity())); - if(!frame_buffer_.try_pop(tail_)) - return tail_; + virtual safe_ptr receive(int) + { + 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) && context_) + graph_->add_tag("underflow"); - executor_.begin_invoke([=] + 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) + { + context_.begin_invoke([=] { - if(!renderer_) - return; + if(!context_) + initialize(); try { - auto frame = core::basic_frame::empty(); - do{frame = renderer_->render_frame(frame_buffer_.size() < frame_buffer_.capacity()-2);} - while(frame_buffer_.try_push(frame) && frame == core::basic_frame::empty()); - graph_->set_value("output-buffer", static_cast(frame_buffer_.size())/static_cast(frame_buffer_.capacity())); - fps_.fetch_and_store(static_cast(renderer_->fps()*100.0)); + 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(); - renderer_ = nullptr; + context_.reset(nullptr); + 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 tail_; + // 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 param(const std::wstring& param) - { - executor_.begin_invoke([=] + + 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(!renderer_) - { - renderer_.reset(new flash_renderer(safe_ptr(graph_), frame_factory_, filename_, [=]{return print();})); - while(frame_buffer_.try_push(core::basic_frame::empty())){} - } + 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 - { - renderer_->param(param); + { + 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 = 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 = 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 = render_frame(); + frame_buffer_.push(frame); + } + + if(context_->is_empty()) + { + context_.reset(nullptr); + return; + } + + 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)); + graph_->set_text(narrow(print())); + + render(renderer); } catch(...) { CASPAR_LOG_CURRENT_EXCEPTION(); - renderer_ = nullptr; + context_.reset(nullptr); + frame_buffer_.push(core::basic_frame::empty()); } }); } }; -flash_producer::flash_producer(flash_producer&& other) : impl_(std::move(other.impl_)){} -flash_producer::flash_producer(const std::wstring& filename) : impl_(new implementation(filename)){} -safe_ptr flash_producer::receive(){return impl_->receive();} -void flash_producer::param(const std::wstring& param){impl_->param(param);} -void flash_producer::initialize(const safe_ptr& frame_factory) { impl_->initialize(frame_factory);} -void flash_producer::set_parent_printer(const printer& parent_printer){impl_->set_parent_printer(parent_printer);} -std::wstring flash_producer::print() const {return impl_->print();} +safe_ptr create_producer(const safe_ptr& frame_factory, const std::vector& params) +{ + auto template_host = get_template_host(frame_factory->get_video_format_desc()); + + return make_safe(frame_factory, env::template_folder() + L"\\" + widen(template_host.filename), template_host.width, template_host.height); +} -std::wstring flash_producer::find_template(const std::wstring& template_name) +std::wstring find_template(const std::wstring& template_name) { if(boost::filesystem::exists(template_name + L".ft")) return template_name + L".ft"; @@ -328,4 +452,4 @@ std::wstring flash_producer::find_template(const std::wstring& template_name) return L""; } -} \ No newline at end of file +}} \ No newline at end of file