X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=modules%2Fflash%2Fproducer%2Fflash_producer.cpp;h=cbbae1aa2286c8dcee28a132523619ebaaac08f6;hb=48e6b878823d7a118df54b91ee8d6bd7ce84c8e8;hp=65457bfc5afbde8551ff1b2f64d473c6079be3b9;hpb=623472e30864f882aa703c04ca916148d33a77cd;p=casparcg diff --git a/modules/flash/producer/flash_producer.cpp b/modules/flash/producer/flash_producer.cpp index 65457bfc5..cbbae1aa2 100644 --- a/modules/flash/producer/flash_producer.cpp +++ b/modules/flash/producer/flash_producer.cpp @@ -1,475 +1,673 @@ -/* -* copyright (c) 2010 Sveriges Television AB -* -* This file is part of CasparCG. -* -* 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 . -* -*/ - -#include "../stdafx.h" - -#if defined(_MSC_VER) -#pragma warning (disable : 4146) -#pragma warning (disable : 4244) -#endif - -#include "flash_producer.h" -#include "FlashAxContainer.h" - -#include - -#include -#include -#include - -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include - -#include - -#include - -namespace caspar { namespace flash { - -class bitmap -{ -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; - - 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());} - - 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.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_; - 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, int width, int height) - : graph_(graph) - , filename_(filename) - , frame_factory_(frame_factory) - , 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(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("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([this]{return L"flash_renderer";}); - - if(FAILED(ax_->CreateAxControl())) - BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Failed to Create FlashAxControl")); - - CComPtr spFlash; - if(FAILED(ax_->QueryControl(&spFlash))) - BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Failed to Query FlashAxControl")); - - if(FAILED(spFlash->put_Playing(true)) ) - BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Failed to start playing Flash")); - - if(FAILED(spFlash->put_Movie(CComBSTR(filename.c_str())))) - BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Failed to Load Template Host")); - - 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_->SetSize(width_, height_); - - CASPAR_LOG(info) << print() << L" Successfully initialized with template-host: " << filename << L" width: " << width_ << L" height: " << height_ << L"."; - } - - ~flash_renderer() - { - if(ax_) - { - ax_->DestroyAxControl(); - ax_->Release(); - } - CASPAR_LOG(info) << print() << L" Uninitialized."; - } - - std::wstring call(const std::wstring& param) - { - std::wstring result; - - if(!ax_->FlashCall(param, result)) - 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"); - - return result; - } - - safe_ptr render_frame(bool has_underflow) - { - float frame_time = 1.0f/ax_->GetFPS(); - - graph_->update_value("tick-time", static_cast(tick_timer_.elapsed()/frame_time)*0.5f); - tick_timer_.restart(); - - 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(); - - ax_->Tick(); - if(ax_->InvalidRect()) - { - fast_memclr(bmp_.data(), width_*height_*4); - ax_->DrawControl(bmp_); - - core::pixel_format_desc desc; - desc.pix_fmt = core::pixel_format::bgra; - desc.planes.push_back(core::pixel_format_desc::plane(width_, height_, 4)); - auto frame = frame_factory_->create_frame(this, desc); - - fast_memcpy(frame->image_data().begin(), bmp_.data(), width_*height_*4); - frame->commit(); - head_ = frame; - } - - 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 : public core::frame_producer -{ - const std::wstring filename_; - const safe_ptr frame_factory_; - - tbb::atomic fps_; - - safe_ptr graph_; - - tbb::concurrent_bounded_queue> frame_buffer_; - - mutable tbb::spin_mutex last_frame_mutex_; - safe_ptr last_frame_; - - com_context context_; - - int width_; - int height_; -public: - 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))); - - fps_ = 0; - - 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(); - } - - ~flash_producer() - { - frame_buffer_.clear(); - } - - // frame_producer - - virtual safe_ptr receive(int) override - { - 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"); - - return frame; - } - - virtual safe_ptr last_frame() const override - { - tbb::spin_mutex::scoped_lock lock(last_frame_mutex_); - return last_frame_; - } - - virtual boost::unique_future call(const std::wstring& param) override - { - return context_.begin_invoke([=]() -> std::wstring - { - if(!context_) - initialize(); - - try - { - return context_->call(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()); - } - - return L""; - }); - } - - virtual std::wstring print() const override - { - return L"flash[" + boost::filesystem::wpath(filename_).filename() + L"|" + boost::lexical_cast(fps_) + L"]"; - } - - virtual boost::property_tree::wptree info() const override - { - boost::property_tree::wptree info; - info.add(L"type", L"flash-producer"); - return info; - } - - // 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()); - } - - 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 = 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(); - context_.reset(nullptr); - frame_buffer_.push(core::basic_frame::empty()); - } - }); - } -}; - -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 create_producer_destroy_proxy(make_safe(frame_factory, env::template_folder() + L"\\" + widen(template_host.filename), template_host.width, template_host.height)); -} - -std::wstring find_template(const std::wstring& template_name) -{ - if(boost::filesystem::exists(template_name + L".ft")) - return template_name + L".ft"; - - if(boost::filesystem::exists(template_name + L".ct")) - return template_name + L".ct"; - - if(boost::filesystem::exists(template_name + L".swf")) - return template_name + L".swf"; - - return L""; -} - +/* +* Copyright (c) 2011 Sveriges Television AB +* +* 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 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" + +#if defined(_MSC_VER) +#pragma warning (disable : 4146) +#pragma warning (disable : 4244) +#endif + +#include "flash_producer.h" +#include "FlashAxContainer.h" + +#include "../util/swf.h" + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include + +#include + +namespace caspar { namespace flash { + +class bitmap +{ +public: + bitmap(int width, int 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 = static_cast(-height); + info.bmiHeader.biPlanes = 1; + info.bmiHeader.biSize = sizeof(BITMAPINFO); + info.bmiHeader.biWidth = static_cast(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()); + + if(!bmp_data_) + CASPAR_THROW_EXCEPTION(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::wstring video_mode; + std::wstring filename; + int width; + int height; +}; + +template_host get_template_host(const core::video_format_desc& desc) +{ + try + { + std::vector template_hosts; + auto template_hosts_element = env::properties().get_child_optional( + L"configuration.template-hosts"); + + if (template_hosts_element) + { + for (auto& xml_mapping : *template_hosts_element) + { + try + { + template_host template_host; + template_host.video_mode = xml_mapping.second.get(L"video-mode", L""); + template_host.filename = xml_mapping.second.get(L"filename", L"cg.fth"); + template_host.width = xml_mapping.second.get(L"width", desc.width); + template_host.height = xml_mapping.second.get(L"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 == 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 == L"";}); + + if(template_host_it != template_hosts.end()) + return *template_host_it; + } + catch(...){} + + template_host template_host; + template_host.filename = L"cg.fth"; + + for(auto it = boost::filesystem::directory_iterator(env::template_folder()); it != boost::filesystem::directory_iterator(); ++it) + { + if(boost::iequals(it->path().extension().wstring(), L"." + desc.name)) + { + template_host.filename = it->path().filename().wstring(); + break; + } + } + + template_host.width = desc.square_width; + template_host.height = desc.square_height; + return template_host; +} + +boost::mutex& get_global_init_destruct_mutex() +{ + static boost::mutex m; + + return m; +} + +class flash_renderer +{ + struct com_init + { + HRESULT result_ = CoInitialize(nullptr); + + com_init() + { + if(FAILED(result_)) + CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info("Failed to initialize com-context for flash-player")); + } + + ~com_init() + { + if(SUCCEEDED(result_)) + ::CoUninitialize(); + } + } com_init_; + + core::monitor::subject& monitor_subject_; + + const std::wstring filename_; + const int width_; + const int height_; + + const std::shared_ptr frame_factory_; + + CComObject* ax_ = nullptr; + core::draw_frame head_ = core::draw_frame::late(); + bitmap bmp_ { width_, height_ }; + prec_timer timer_; + caspar::timer tick_timer_; + + spl::shared_ptr graph_; + +public: + flash_renderer(core::monitor::subject& monitor_subject, const spl::shared_ptr& graph, const std::shared_ptr& frame_factory, const std::wstring& filename, int width, int height) + : monitor_subject_(monitor_subject) + , graph_(graph) + , filename_(filename) + , width_(width) + , height_(height) + , frame_factory_(frame_factory) + , bmp_(width, height) + { + graph_->set_color("frame-time", diagnostics::color(0.1f, 1.0f, 0.1f)); + 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)); + + if(FAILED(CComObject::CreateInstance(&ax_))) + CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(print() + L" Failed to create FlashAxContainer")); + + if(FAILED(ax_->CreateAxControl())) + CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(print() + L" Failed to Create FlashAxControl")); + + ax_->set_print([this]{return print(); }); + + CComPtr spFlash; + if(FAILED(ax_->QueryControl(&spFlash))) + CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(print() + L" Failed to Query FlashAxControl")); + + if(FAILED(spFlash->put_Playing(true)) ) + CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(print() + L" Failed to start playing Flash")); + + // Concurrent initialization of two template hosts causes a + // SecurityException later when CG ADD is performed. Initialization is + // therefore serialized via a global mutex. + lock(get_global_init_destruct_mutex(), [&] + { + if (FAILED(spFlash->put_Movie(CComBSTR(filename.c_str())))) + CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(print() + L" Failed to Load Template Host")); + }); + + if(FAILED(spFlash->put_ScaleMode(2))) //Exact fit. Scale without respect to the aspect ratio. + CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(print() + L" Failed to Set Scale Mode")); + + ax_->SetSize(width_, height_); + render_frame(0.0); + + CASPAR_LOG(info) << print() << L" Initialized."; + } + + ~flash_renderer() + { + if(ax_) + { + ax_->DestroyAxControl(); + ax_->Release(); + } + graph_->set_value("tick-time", 0.0f); + graph_->set_value("frame-time", 0.0f); + CASPAR_LOG(info) << print() << L" Uninitialized."; + } + + std::wstring call(const std::wstring& param) + { + std::wstring result; + + CASPAR_LOG(debug) << print() << " Call: " << param; + + if(!ax_->FlashCall(param, result)) + CASPAR_LOG(warning) << print() << L" Flash call failed:" << param;//CASPAR_THROW_EXCEPTION(invalid_operation() << msg_info("Flash function call failed.") << arg_name_info("param") << arg_value_info(narrow(param))); + + if (boost::starts_with(result, L"")) + CASPAR_LOG(warning) << print() << L" Flash call failed:" << result; + + graph_->set_tag(diagnostics::tag_severity::INFO, "param"); + + return result; + } + + core::draw_frame render_frame(double sync) + { + const float frame_time = 1.0f/fps(); + + if (!ax_->IsReadyToRender()) + return head_; + + if (is_empty()) + return core::draw_frame::empty(); + + if (sync > 0.00001) + timer_.tick(frame_time*sync); // This will block the thread. + + graph_->set_value("tick-time", static_cast(tick_timer_.elapsed() / frame_time) * 0.5f); + tick_timer_.restart(); + boost::timer frame_timer; + ax_->Tick(); + + if (ax_->InvalidRect()) + { + core::pixel_format_desc desc = core::pixel_format::bgra; + desc.planes.push_back(core::pixel_format_desc::plane(width_, height_, 4)); + auto frame = frame_factory_->create_frame(this, desc, core::audio_channel_layout::invalid()); + + fast_memset(bmp_.data(), 0, width_ * height_ * 4); + ax_->DrawControl(bmp_); + + fast_memcpy(frame.image_data(0).begin(), bmp_.data(), width_*height_*4); + head_ = core::draw_frame(std::move(frame)); + } + + MSG msg; + while (PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE)) // DO NOT REMOVE THE MESSAGE DISPATCH LOOP. Without this some stuff doesn't work! + { + if (msg.message == WM_TIMER && msg.wParam == 3 && msg.lParam == 0) // We tick this inside FlashAxContainer + continue; + + TranslateMessage(&msg); + DispatchMessage(&msg); + } + + graph_->set_value("frame-time", static_cast(frame_timer.elapsed()/frame_time)*0.5f); + monitor_subject_ << core::monitor::message("/renderer/profiler/time") % frame_timer.elapsed() % frame_time; + return head_; + } + + bool is_empty() const + { + return ax_->IsEmpty(); + } + + double fps() const + { + return ax_->GetFPS(); + } + + std::wstring print() + { + return L"flash-player[" + boost::filesystem::path(filename_).filename().wstring() + + L"|" + boost::lexical_cast(width_) + + L"x" + boost::lexical_cast(height_) + + L"]"; + } +}; + +struct flash_producer : public core::frame_producer_base +{ + core::monitor::subject monitor_subject_; + const std::wstring filename_; + const spl::shared_ptr frame_factory_; + const core::video_format_desc format_desc_; + const int width_; + const int height_; + core::constraints constraints_ { static_cast(width_), static_cast(height_) }; + const int buffer_size_ = env::properties().get(L"configuration.flash.buffer-depth", format_desc_.fps > 30.0 ? 4 : 2); + + tbb::atomic fps_; + + spl::shared_ptr graph_; + + std::queue frame_buffer_; + tbb::concurrent_bounded_queue output_buffer_; + + core::draw_frame last_frame_ = core::draw_frame::empty(); + + std::unique_ptr renderer_; + tbb::atomic has_renderer_; + + executor executor_ = L"flash_producer"; +public: + flash_producer(const spl::shared_ptr& frame_factory, const core::video_format_desc& format_desc, const std::wstring& filename, int width, int height) + : filename_(filename) + , frame_factory_(frame_factory) + , format_desc_(format_desc) + , width_(width > 0 ? width : format_desc.width) + , height_(height > 0 ? height : format_desc.height) + { + fps_ = 0; + + graph_->set_color("buffered", diagnostics::color(1.0f, 1.0f, 0.0f)); + graph_->set_color("late-frame", diagnostics::color(0.6f, 0.3f, 0.9f)); + graph_->set_text(print()); + diagnostics::register_graph(graph_); + + has_renderer_ = false; + + CASPAR_LOG(info) << print() << L" Initialized"; + } + + ~flash_producer() + { + executor_.invoke([this] + { + renderer_.reset(); + }, task_priority::high_priority); + } + + // frame_producer + + void log_buffered() + { + double buffered = output_buffer_.size(); + auto ratio = buffered / buffer_size_; + graph_->set_value("buffered", ratio); + } + + core::draw_frame receive_impl() override + { + auto frame = last_frame_; + + double buffered = output_buffer_.size(); + auto ratio = buffered / buffer_size_; + graph_->set_value("buffered", ratio); + + if (output_buffer_.try_pop(frame)) + last_frame_ = frame; + else + graph_->set_tag(diagnostics::tag_severity::WARNING, "late-frame"); + + fill_buffer(); + + monitor_subject_ << core::monitor::message("/host/path") % filename_ + << core::monitor::message("/host/width") % width_ + << core::monitor::message("/host/height") % height_ + << core::monitor::message("/host/fps") % fps_ + << core::monitor::message("/buffer") % output_buffer_.size() % buffer_size_; + + return frame; + } + + core::constraints& pixel_constraints() override + { + return constraints_; + } + + std::future call(const std::vector& params) override + { + auto param = boost::algorithm::join(params, L" "); + + if (param == L"?") + return make_ready_future(std::wstring(has_renderer_ ? L"1" : L"0")); + + return executor_.begin_invoke([this, param]() -> std::wstring + { + try + { + bool initialize_renderer = !renderer_; + + if (initialize_renderer) + { + renderer_.reset(new flash_renderer(monitor_subject_, graph_, frame_factory_, filename_, width_, height_)); + + has_renderer_ = true; + } + + std::wstring result = param == L"start_rendering" + ? L"" : renderer_->call(param); + + if (initialize_renderer) + { + do_fill_buffer(true); + } + + return result; + } + catch(...) + { + CASPAR_LOG_CURRENT_EXCEPTION(); + renderer_.reset(nullptr); + has_renderer_ = false; + } + + return L""; + }, task_priority::high_priority); + } + + std::wstring print() const override + { + return L"flash[" + boost::filesystem::path(filename_).wstring() + L"|" + boost::lexical_cast(fps_) + L"]"; + } + + std::wstring name() const override + { + return L"flash"; + } + + boost::property_tree::wptree info() const override + { + boost::property_tree::wptree info; + info.add(L"type", L"flash"); + return info; + } + + core::monitor::subject& monitor_output() + { + return monitor_subject_; + } + + // flash_producer + + void fill_buffer() + { + if (executor_.size() > 0) + return; + + executor_.begin_invoke([this] + { + do_fill_buffer(false); + }); + } + + void do_fill_buffer(bool initial_buffer_fill) + { + int nothing_rendered = 0; + const int MAX_NOTHING_RENDERED_RETRIES = 4; + + auto to_render = buffer_size_ - output_buffer_.size(); + bool allow_faster_rendering = !initial_buffer_fill; + int rendered = 0; + + while (rendered < to_render) + { + bool was_rendered = next(allow_faster_rendering); + log_buffered(); + + if (was_rendered) + { + ++rendered; + } + else + { + if (nothing_rendered++ < MAX_NOTHING_RENDERED_RETRIES) + { + // Flash player not ready with first frame, sleep to not busy-loop; + boost::this_thread::sleep(boost::posix_time::milliseconds(10)); + boost::this_thread::yield(); + } + else + return; + } + + executor_.yield(task_priority::high_priority); + } + } + + bool next(bool allow_faster_rendering) + { + if (!renderer_) + frame_buffer_.push(core::draw_frame::empty()); + + if (frame_buffer_.empty()) + { + if (abs(renderer_->fps() / 2.0 - format_desc_.fps) < 2.0) // flash == 2 * format -> interlace + { + auto frame1 = render_frame(allow_faster_rendering); + + if (frame1 != core::draw_frame::late()) + { + auto frame2 = render_frame(allow_faster_rendering); + frame_buffer_.push(core::draw_frame::interlace(frame1, frame2, format_desc_.field_mode)); + } + } + else if (abs(renderer_->fps() - format_desc_.fps / 2.0) < 2.0) // format == 2 * flash -> duplicate + { + auto frame = render_frame(allow_faster_rendering); + + if (frame != core::draw_frame::late()) + { + 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(allow_faster_rendering); + + if (frame != core::draw_frame::late()) + frame_buffer_.push(frame); + } + + fps_.fetch_and_store(static_cast(renderer_->fps()*100.0)); + graph_->set_text(print()); + + if (renderer_->is_empty()) + { + renderer_.reset(); + has_renderer_ = false; + } + } + + if (frame_buffer_.empty()) + { + return false; + } + else + { + output_buffer_.push(std::move(frame_buffer_.front())); + frame_buffer_.pop(); + return true; + } + } + + core::draw_frame render_frame(bool allow_faster_rendering) + { + double sync; + + if (allow_faster_rendering) + { + double ratio = std::min( + 1.0, + static_cast(output_buffer_.size()) + / static_cast(std::max(1, buffer_size_ - 1))); + sync = 2 * ratio - ratio * ratio; + } + else + { + sync = 1.0; + } + + return renderer_->render_frame(sync); + } +}; + +spl::shared_ptr create_producer(const core::frame_producer_dependencies& dependencies, const std::vector& params) +{ + auto template_host = get_template_host(dependencies.format_desc); + + auto filename = env::template_folder() + L"\\" + template_host.filename; + + if(!boost::filesystem::exists(filename)) + CASPAR_THROW_EXCEPTION(file_not_found() << msg_info(L"Could not open flash movie " + filename)); + + return create_destroy_proxy(spl::make_shared(dependencies.frame_factory, dependencies.format_desc, filename, template_host.width, template_host.height)); +} + +void describe_swf_producer(core::help_sink& sink, const core::help_repository& repo) +{ + sink.short_description(L"Plays flash files (.swf files)."); + sink.syntax(L"[swf_file:string]"); + sink.para()->text(L"Plays flash files (.swf files). The file should reside under the media folder."); + sink.para()->text(L"Examples:"); + sink.example(L">> PLAY 1-10 folder/swf_file"); +} + +spl::shared_ptr create_swf_producer(const core::frame_producer_dependencies& dependencies, const std::vector& params) +{ + auto filename = env::media_folder() + L"\\" + params.at(0) + L".swf"; + + if (!boost::filesystem::exists(filename)) + return core::frame_producer::empty(); + + swf_t::header_t header(filename); + + auto producer = spl::make_shared( + dependencies.frame_factory, dependencies.format_desc, filename, header.frame_width, header.frame_height); + + producer->call({ L"start_rendering" }).get(); + + return create_destroy_proxy(producer); +} + +std::wstring find_template(const std::wstring& template_name) +{ + if(boost::filesystem::exists(template_name + L".ft")) + return template_name + L".ft"; + + if(boost::filesystem::exists(template_name + L".ct")) + return template_name + L".ct"; + + if(boost::filesystem::exists(template_name + L".swf")) + return template_name + L".swf"; + + return L""; +} + }} \ No newline at end of file