]> git.sesse.net Git - casparcg/blobdiff - modules/flash/producer/flash_producer.cpp
set svn:eol-style native on .h and .cpp files
[casparcg] / modules / flash / producer / flash_producer.cpp
index 07c0d63340fd170c8a0220357963bf89e028b420..1af0ff3c5a97d6da8af8876b74707dbe84ff8c93 100644 (file)
-/*\r
-* copyright (c) 2010 Sveriges Television AB <info@casparcg.com>\r
-*\r
-*  This file is part of CasparCG.\r
-*\r
-*    CasparCG is free software: you can redistribute it and/or modify\r
-*    it under the terms of the GNU General Public License as published by\r
-*    the Free Software Foundation, either version 3 of the License, or\r
-*    (at your option) any later version.\r
-*\r
-*    CasparCG is distributed in the hope that it will be useful,\r
-*    but WITHOUT ANY WARRANTY; without even the implied warranty of\r
-*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
-*    GNU General Public License for more details.\r
-\r
-*    You should have received a copy of the GNU General Public License\r
-*    along with CasparCG.  If not, see <http://www.gnu.org/licenses/>.\r
-*\r
-*/\r
\r
-#include "../stdafx.h"\r
-\r
-#if defined(_MSC_VER)\r
-#pragma warning (disable : 4146)\r
-#pragma warning (disable : 4244)\r
-#endif\r
-\r
-#include "flash_producer.h"\r
-#include "FlashAxContainer.h"\r
-\r
-#include <core/video_format.h>\r
-\r
-#include <core/producer/frame/basic_frame.h>\r
-#include <core/producer/frame/frame_factory.h>\r
-#include <core/mixer/write_frame.h>\r
-\r
-#include <common/env.h>\r
-#include <common/diagnostics/graph.h>\r
-#include <common/memory/memcpy.h>\r
-#include <common/memory/memclr.h>\r
-#include <common/utility/timer.h>\r
-\r
-#include <boost/filesystem.hpp>\r
-#include <boost/thread.hpp>\r
-#include <boost/timer.hpp>\r
-\r
-#include <functional>\r
-\r
-#include <agents.h>\r
-#include <agents_extras.h>\r
-#include <concrt_extras.h>\r
-\r
-using namespace Concurrency;\r
-\r
-namespace caspar { namespace flash {\r
-               \r
-class bitmap\r
-{\r
-public:\r
-       bitmap(size_t width, size_t height)\r
-               : bmp_data_(nullptr)\r
-               , hdc_(CreateCompatibleDC(0), DeleteDC)\r
-       {       \r
-               BITMAPINFO info;\r
-               memset(&info, 0, sizeof(BITMAPINFO));\r
-               info.bmiHeader.biBitCount = 32;\r
-               info.bmiHeader.biCompression = BI_RGB;\r
-               info.bmiHeader.biHeight = -height;\r
-               info.bmiHeader.biPlanes = 1;\r
-               info.bmiHeader.biSize = sizeof(BITMAPINFO);\r
-               info.bmiHeader.biWidth = width;\r
-\r
-               bmp_.reset(CreateDIBSection(static_cast<HDC>(hdc_.get()), &info, DIB_RGB_COLORS, reinterpret_cast<void**>(&bmp_data_), 0, 0), DeleteObject);\r
-               SelectObject(static_cast<HDC>(hdc_.get()), bmp_.get()); \r
-\r
-               if(!bmp_data_)\r
-                       BOOST_THROW_EXCEPTION(std::bad_alloc());\r
-       }\r
-\r
-       operator HDC() {return static_cast<HDC>(hdc_.get());}\r
-\r
-       BYTE* data() { return bmp_data_;}\r
-       const BYTE* data() const { return bmp_data_;}\r
-\r
-private:\r
-       BYTE* bmp_data_;        \r
-       std::shared_ptr<void> hdc_;\r
-       std::shared_ptr<void> bmp_;\r
-};\r
-\r
-struct template_host\r
-{\r
-       std::string  field_mode;\r
-       std::string  filename;\r
-       size_t           width;\r
-       size_t           height;\r
-};\r
-\r
-template_host get_template_host(const core::video_format_desc& desc)\r
-{\r
-       std::vector<template_host> template_hosts;\r
-       BOOST_FOREACH(auto& xml_mapping, env::properties().get_child("configuration.producers.template-hosts"))\r
-       {\r
-               try\r
-               {\r
-                       template_host template_host;\r
-                       template_host.field_mode                = xml_mapping.second.get("video-mode", narrow(desc.name));\r
-                       template_host.filename                  = xml_mapping.second.get("filename", "cg.fth");\r
-                       template_host.width                             = xml_mapping.second.get("width", desc.width);\r
-                       template_host.height                    = xml_mapping.second.get("height", desc.height);\r
-                       template_hosts.push_back(template_host);\r
-               }\r
-               catch(...){}\r
-       }\r
-\r
-       auto template_host_it = boost::find_if(template_hosts, [&](template_host template_host){return template_host.field_mode == narrow(desc.name);});\r
-       if(template_host_it == template_hosts.end())\r
-               template_host_it = boost::find_if(template_hosts, [&](template_host template_host){return template_host.field_mode == "";});\r
-\r
-       if(template_host_it != template_hosts.end())\r
-               return *template_host_it;\r
-       \r
-       template_host template_host;\r
-       template_host.filename = "cg.fth";\r
-       template_host.width = desc.width;\r
-       template_host.height = desc.height;\r
-       return template_host;\r
-}\r
-\r
-class flash_renderer\r
-{      \r
-       const std::wstring                                                              filename_;\r
-\r
-       const safe_ptr<core::frame_factory>                             frame_factory_;\r
-       safe_ptr<diagnostics::graph>                                    graph_;\r
-\r
-       high_prec_timer                                                                 timer_;\r
-       safe_ptr<core::basic_frame>                                             head_;\r
-       bitmap                                                                                  bmp_;\r
-\r
-       const size_t                                                                    width_;\r
-       const size_t                                                                    height_;\r
-                       \r
-       boost::timer                                                                    frame_timer_;\r
-       boost::timer                                                                    tick_timer_;\r
-\r
-       CComObject<caspar::flash::FlashAxContainer>*    ax_;\r
-       \r
-public:\r
-       flash_renderer(const safe_ptr<diagnostics::graph>& graph, const safe_ptr<core::frame_factory>& frame_factory, const std::wstring& filename, int width, int height) \r
-               : filename_(filename)\r
-               , frame_factory_(frame_factory)\r
-               , graph_(graph)\r
-               , ax_(nullptr)\r
-               , head_(core::basic_frame::empty())\r
-               , bmp_(width, height)\r
-               , width_(width)\r
-               , height_(height)\r
-       {               \r
-               graph_->add_guide("frame-time", 0.5f);\r
-               graph_->set_color("frame-time", diagnostics::color(0.1f, 1.0f, 0.1f));\r
-               graph_->add_guide("tick-time", 0.5);\r
-               graph_->set_color("tick-time", diagnostics::color(0.0f, 0.6f, 0.9f));\r
-               graph_->set_color("param", diagnostics::color(1.0f, 0.5f, 0.0f));               \r
-               \r
-               if(FAILED(CComObject<caspar::flash::FlashAxContainer>::CreateInstance(&ax_)))\r
-                       BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Failed to create FlashAxContainer"));\r
-               \r
-               ax_->set_print([this]{return L"flash_renderer";});\r
-\r
-               if(FAILED(ax_->CreateAxControl()))\r
-                       BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Failed to Create FlashAxControl"));\r
-               \r
-               CComPtr<IShockwaveFlash> spFlash;\r
-               if(FAILED(ax_->QueryControl(&spFlash)))\r
-                       BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Failed to Query FlashAxControl"));\r
-                                                                                               \r
-               if(FAILED(spFlash->put_Playing(true)) )\r
-                       BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Failed to start playing Flash"));\r
-\r
-               if(FAILED(spFlash->put_Movie(CComBSTR(filename.c_str()))))\r
-                       BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Failed to Load Template Host"));\r
-                                                                               \r
-               if(FAILED(spFlash->put_ScaleMode(2)))  //Exact fit. Scale without respect to the aspect ratio.\r
-                       BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Failed to Set Scale Mode"));\r
-                                               \r
-               ax_->SetSize(width_, height_);          \r
-       \r
-               CASPAR_LOG(info) << print() << L" Thread started.";\r
-               CASPAR_LOG(info) << print() << L" Successfully initialized with template-host: " << filename << L" width: " << width_ << L" height: " << height_ << L".";\r
-       }\r
-\r
-       ~flash_renderer()\r
-       {               \r
-               if(ax_)\r
-               {\r
-                       ax_->DestroyAxControl();\r
-                       ax_->Release();\r
-               }\r
-               CASPAR_LOG(info) << print() << L" Thread ended.";\r
-       }\r
-       \r
-       void param(const std::wstring& param)\r
-       {               \r
-               if(!ax_->FlashCall(param))\r
-                       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)));\r
-               graph_->add_tag("param");\r
-       }\r
-       \r
-       safe_ptr<core::basic_frame> operator()()\r
-       {\r
-               const float frame_time = 1.0f/ax_->GetFPS();\r
-\r
-               graph_->update_value("tick-time", static_cast<float>(tick_timer_.elapsed()/frame_time)*0.5f);\r
-               tick_timer_.restart();\r
-\r
-               if(ax_->IsEmpty())\r
-                       return core::basic_frame::empty();              \r
-               \r
-               Concurrency::scoped_oversubcription_token oversubscribe;\r
-               timer_.tick(frame_time); // This will block the thread.\r
-               //Concurrency::wait(std::max<int>(0, frame_time-3));\r
-                       \r
-               frame_timer_.restart();\r
-\r
-               ax_->Tick();\r
-               if(ax_->InvalidRect())\r
-               {                       \r
-                       fast_memclr(bmp_.data(), width_*height_*4);\r
-                       ax_->DrawControl(bmp_);\r
-               \r
-                       auto frame = frame_factory_->create_frame(this, width_, height_);\r
-                       fast_memcpy(frame->image_data().begin(), bmp_.data(), width_*height_*4);\r
-                       frame->commit();\r
-                       head_ = frame;\r
-               }               \r
-                               \r
-               graph_->update_value("frame-time", static_cast<float>(frame_timer_.elapsed()/frame_time)*0.5f);\r
-               return head_;\r
-       }\r
-\r
-       double fps() const\r
-       {\r
-               return ax_->GetFPS();   \r
-       }\r
-       \r
-       std::wstring print()\r
-       {\r
-               return L"flash[" + boost::filesystem::wpath(filename_).filename() + L"]";               \r
-       }\r
-};\r
-\r
-struct flash_producer : public Concurrency::agent, public core::frame_producer\r
-{      \r
-       unbounded_buffer<std::wstring>                                                          params_;\r
-       bounded_buffer<safe_ptr<core::basic_frame>>                                     frames_;\r
-\r
-       tbb::atomic<bool>                                                                                       is_running_;\r
-               \r
-       const safe_ptr<core::frame_factory>                                                     frame_factory_;\r
-       const std::wstring                                                                                      filename_;      \r
-       tbb::atomic<int>                                                                                        fps_;\r
-       const int                                                                                                       width_;\r
-       const int                                                                                                       height_;\r
-               \r
-       mutable overwrite_buffer<safe_ptr<core::basic_frame>>           last_frame_;\r
-\r
-       safe_ptr<diagnostics::graph>                                                            graph_;\r
-                               \r
-public:\r
-       flash_producer(const safe_ptr<core::frame_factory>& frame_factory, const std::wstring& filename, size_t width, size_t height) \r
-               : frames_(frame_factory->get_video_format_desc().fps > 30.0 ? 2 : 1)\r
-               , frame_factory_(frame_factory)\r
-               , filename_(filename)           \r
-               , width_(width > 0 ? width : frame_factory->get_video_format_desc().width)\r
-               , height_(height > 0 ? height : frame_factory->get_video_format_desc().height)\r
-               , graph_(diagnostics::create_graph("flash", false))\r
-       {       \r
-               if(!boost::filesystem::exists(filename))\r
-                       BOOST_THROW_EXCEPTION(file_not_found() << boost::errinfo_file_name(narrow(filename)));  \r
-               \r
-               graph_->set_color("underflow", diagnostics::color(0.6f, 0.3f, 0.9f));   \r
-\r
-               Concurrency::send(last_frame_, core::basic_frame::empty());\r
-               \r
-               fps_            = 0;\r
-               is_running_ = true;\r
-\r
-               graph_->start();\r
-               start();\r
-       }\r
-\r
-       ~flash_producer()\r
-       {\r
-               is_running_ = false;\r
-               auto frame = core::basic_frame::empty();\r
-               while(try_receive(frames_, frame)){}\r
-               agent::wait(this);\r
-       }\r
-       \r
-       virtual void run()\r
-       {               \r
-               try\r
-               {\r
-                       struct co_init\r
-                       {\r
-                               co_init()  {CoInitialize(NULL);}\r
-                               ~co_init() {CoUninitialize();}\r
-                       } init;\r
-\r
-                       flash_renderer renderer(graph_, frame_factory_, filename_, width_, height_);\r
-\r
-                       is_running_ = true;\r
-                       while(is_running_)\r
-                       {\r
-                               std::wstring param;\r
-                               while(is_running_ && Concurrency::try_receive(params_, param))\r
-                                       renderer.param(param);\r
-                       \r
-                               const auto& format_desc = frame_factory_->get_video_format_desc();\r
-\r
-                               if(abs(renderer.fps()/2.0 - format_desc.fps) < 2.0) // flash == 2 * format -> interlace\r
-                               {\r
-                                       auto frame1 = renderer();\r
-                                       auto frame2 = renderer();\r
-                                       send(last_frame_, frame2);\r
-                                       send(frames_, core::basic_frame::interlace(frame1, frame2, format_desc.field_mode));\r
-                               }\r
-                               else if(abs(renderer.fps()- format_desc.fps/2.0) < 2.0) // format == 2 * flash -> duplicate\r
-                               {\r
-                                       auto frame = renderer();\r
-                                       send(last_frame_, frame);\r
-                                       send(frames_, frame);\r
-                                       send(frames_, frame);\r
-                               }\r
-                               else //if(abs(renderer_->fps() - format_desc_.fps) < 0.1) // format == flash -> simple\r
-                               {\r
-                                       auto frame = renderer();\r
-                                       send(last_frame_, frame);\r
-                                       send(frames_, frame);\r
-                               }\r
-\r
-                               fps_ = static_cast<int>(renderer.fps()*100.0);\r
-                               graph_->update_text(narrow(print()));\r
-                       }\r
-               }\r
-               catch(...)\r
-               {\r
-                       CASPAR_LOG_CURRENT_EXCEPTION();\r
-               }\r
-               \r
-               is_running_ = false;\r
-               done();\r
-       }\r
-       \r
-       // frame_producer\r
-               \r
-       virtual safe_ptr<core::basic_frame> receive(int)\r
-       {                                               \r
-               auto frame = core::basic_frame::late();\r
-\r
-               try\r
-               {\r
-                       frame = Concurrency::receive(frames_, 5);\r
-               }\r
-               catch(operation_timed_out&)\r
-               {                       \r
-                       graph_->add_tag("underflow");\r
-               }\r
-\r
-               return frame;\r
-       }\r
-\r
-       virtual safe_ptr<core::basic_frame> last_frame() const\r
-       {\r
-               return last_frame_.value();\r
-       }               \r
-       \r
-       virtual void param(const std::wstring& param) \r
-       {       \r
-               if(!is_running_.fetch_and_store(true))\r
-               {\r
-                       agent::wait(this);\r
-                       start();\r
-               }\r
-               asend(params_, param);\r
-       }\r
-               \r
-       virtual std::wstring print() const\r
-       { \r
-               return L"flash[" + boost::filesystem::wpath(filename_).filename() + L"|" + boost::lexical_cast<std::wstring>(fps_) + L"]";              \r
-       }       \r
-};\r
-\r
-safe_ptr<core::frame_producer> create_flash_producer(const safe_ptr<core::frame_factory> frame_factory, const std::vector<std::wstring>& params)\r
-{              \r
-       static const std::vector<std::wstring> extensions = boost::assign::list_of\r
-               (L"swf");\r
-\r
-       std::wstring filename = env::media_folder() + L"\\" + params[0];\r
-       \r
-       auto ext = boost::find_if(extensions, [&](const std::wstring& ex)\r
-       {                                       \r
-               return boost::filesystem::is_regular_file(boost::filesystem::wpath(filename + L"." + ex));\r
-       });\r
-\r
-       if(ext == extensions.end())\r
-               return core::frame_producer::empty();\r
-               \r
-       auto path = filename + L"." + *ext;\r
-       return make_safe<flash_producer>(frame_factory, path, 0, 0);\r
-}\r
-\r
-safe_ptr<core::frame_producer> create_producer(const safe_ptr<core::frame_factory>& frame_factory, const std::vector<std::wstring>& params)\r
-{\r
-       auto template_host = get_template_host(frame_factory->get_video_format_desc());\r
-       \r
-       return make_safe<flash_producer>(frame_factory, env::template_folder() + L"\\" + widen(template_host.filename), template_host.width, template_host.height);\r
-}\r
-\r
-std::wstring find_template(const std::wstring& template_name)\r
-{\r
-       if(boost::filesystem::exists(template_name + L".ft")) \r
-               return template_name + L".ft";\r
-       \r
-       if(boost::filesystem::exists(template_name + L".ct"))\r
-               return template_name + L".ct";\r
-\r
-       return L"";\r
-}\r
-\r
+/*
+* Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
+*
+* 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 <http://www.gnu.org/licenses/>.
+*
+* 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 <core/video_format.h>
+
+#include <core/frame/frame.h>
+#include <core/frame/draw_frame.h>
+#include <core/frame/frame_factory.h>
+#include <core/frame/pixel_format.h>
+#include <core/monitor/monitor.h>
+
+#include <common/env.h>
+#include <common/executor.h>
+#include <common/lock.h>
+#include <common/diagnostics/graph.h>
+#include <common/prec_timer.h>
+#include <common/array.h>
+
+#include <boost/filesystem.hpp>
+#include <boost/property_tree/ptree.hpp>
+#include <boost/thread.hpp>
+#include <boost/timer.hpp>
+#include <boost/algorithm/string.hpp>
+
+#include <tbb/spin_mutex.h>
+
+#include <asmlib.h>
+
+#include <functional>
+
+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<LONG>(-height);
+               info.bmiHeader.biPlanes = 1;
+               info.bmiHeader.biSize = sizeof(BITMAPINFO);
+               info.bmiHeader.biWidth = static_cast<LONG>(width);
+
+               bmp_.reset(CreateDIBSection(static_cast<HDC>(hdc_.get()), &info, DIB_RGB_COLORS, reinterpret_cast<void**>(&bmp_data_), 0, 0), DeleteObject);
+               SelectObject(static_cast<HDC>(hdc_.get()), bmp_.get()); 
+
+               if(!bmp_data_)
+                       CASPAR_THROW_EXCEPTION(bad_alloc());
+       }
+
+       operator HDC() {return static_cast<HDC>(hdc_.get());}
+
+       BYTE* data() { return bmp_data_;}
+       const BYTE* data() const { return bmp_data_;}
+
+private:
+       BYTE* bmp_data_;        
+       std::shared_ptr<void> hdc_;
+       std::shared_ptr<void> 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_host> template_hosts;
+               BOOST_FOREACH(auto& xml_mapping, env::properties().get_child(L"configuration.template-hosts"))
+               {
+                       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;
+}
+
+class flash_renderer
+{      
+       struct com_init
+       {
+               HRESULT result_;
+
+               com_init()
+                       : result_(CoInitialize(nullptr))
+               {
+                       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_;
+
+       monitor::basic_subject&                                         event_subject_;
+
+       const std::wstring                                                      filename_;
+
+       const std::shared_ptr<core::frame_factory>      frame_factory_;
+       
+       CComObject<caspar::flash::FlashAxContainer>* ax_;
+       core::draw_frame                                                        head_;
+       bitmap                                                                          bmp_;
+       
+       spl::shared_ptr<diagnostics::graph>                     graph_;
+       
+       prec_timer                                                                      timer_;
+
+       const int                                                                       width_;
+       const int                                                                       height_;
+       
+public:
+       flash_renderer(monitor::basic_subject& event_subject, const spl::shared_ptr<diagnostics::graph>& graph, const std::shared_ptr<core::frame_factory>& frame_factory, const std::wstring& filename, int width, int height) 
+               : event_subject_(event_subject)
+               , graph_(graph)
+               , filename_(filename)
+               , frame_factory_(frame_factory)
+               , ax_(nullptr)
+               , head_(core::draw_frame::empty())
+               , bmp_(width, height)
+               , width_(width)
+               , height_(height)
+       {               
+               graph_->set_color("frame-time", diagnostics::color(0.1f, 1.0f, 0.1f));
+               graph_->set_color("param", diagnostics::color(1.0f, 0.5f, 0.0f));       
+               graph_->set_color("sync", diagnostics::color(0.8f, 0.3f, 0.2f));                        
+               
+               if(FAILED(CComObject<caspar::flash::FlashAxContainer>::CreateInstance(&ax_)))
+                       CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(print() + L" Failed to create FlashAxContainer"));
+               
+               ax_->set_print([this]{return print();});
+
+               if(FAILED(ax_->CreateAxControl()))
+                       CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(print() + L" Failed to Create FlashAxControl"));
+               
+               CComPtr<IShockwaveFlash> 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"));
+
+               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_);          
+
+               tick(false);
+               render();
+
+               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(trace) << 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)));
+               graph_->set_tag("param");
+
+               return result;
+       }
+
+       void tick(double sync)
+       {               
+               const float frame_time = 1.0f/ax_->GetFPS();
+
+               if(sync > 0.00001)                      
+                       timer_.tick(frame_time*sync); // This will block the thread.
+               else
+                       graph_->set_tag("sync");
+
+               graph_->set_value("sync", sync);
+               event_subject_ << monitor::event("sync") % sync;
+               
+               ax_->Tick();
+                                       
+               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);                  
+               }
+       }
+       
+       core::draw_frame render()
+       {                       
+               const float frame_time = 1.0f/fps();
+
+               boost::timer frame_timer;
+
+               if(ax_->InvalidRect())
+               {                       
+                       A_memset(bmp_.data(), 0, width_*height_*4);
+                       ax_->DrawControl(bmp_);
+               
+                       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);
+
+                       A_memcpy(frame.image_data(0).begin(), bmp_.data(), width_*height_*4);
+                       head_ = core::draw_frame(std::move(frame));     
+               }               
+                                                                               
+               graph_->set_value("frame-time", static_cast<float>(frame_timer.elapsed()/frame_time)*0.5f);
+               event_subject_ << monitor::event("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::wpath(filename_).filename().wstring() 
+                                 + L"|" + boost::lexical_cast<std::wstring>(width_)
+                                 + L"x" + boost::lexical_cast<std::wstring>(height_)
+                                 + L"]";               
+       }
+};
+
+struct flash_producer : public core::frame_producer_base
+{      
+       monitor::basic_subject                                                  event_subject_;
+       const std::wstring                                                              filename_;      
+       const spl::shared_ptr<core::frame_factory>              frame_factory_;
+       const core::video_format_desc                                   format_desc_;
+       const int                                                                               width_;
+       const int                                                                               height_;
+       const int                                                                               buffer_size_;
+
+       tbb::atomic<int>                                                                fps_;
+
+       spl::shared_ptr<diagnostics::graph>                             graph_;
+
+       std::queue<core::draw_frame>                                    frame_buffer_;
+       tbb::concurrent_bounded_queue<core::draw_frame> output_buffer_;
+                               
+       boost::timer                                                                    tick_timer_;
+       std::unique_ptr<flash_renderer>                                 renderer_;
+       
+       executor                                                                                executor_;      
+public:
+       flash_producer(const spl::shared_ptr<core::frame_factory>& 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)
+               , buffer_size_(env::properties().get(L"configuration.flash.buffer-depth", format_desc.fps > 30.0 ? 4 : 2))
+               , executor_(L"flash_producer")
+       {       
+               fps_ = 0;
+        
+               graph_->set_color("buffer-size", diagnostics::color(1.0f, 1.0f, 0.0f));
+               graph_->set_color("tick-time", diagnostics::color(0.0f, 0.6f, 0.9f));
+               graph_->set_color("late-frame", diagnostics::color(0.6f, 0.3f, 0.9f));
+               graph_->set_text(print());
+               diagnostics::register_graph(graph_);
+
+               CASPAR_LOG(info) << print() << L" Initialized";
+       }
+
+       ~flash_producer()
+       {
+               executor_.invoke([this]
+               {
+                       renderer_.reset();
+               }, task_priority::high_priority);
+       }
+
+       // frame_producer
+               
+       core::draw_frame receive_impl() override
+       {                                       
+               auto frame = core::draw_frame::late();
+               
+               if(output_buffer_.try_pop(frame))                       
+                       executor_.begin_invoke(std::bind(&flash_producer::next, this));         
+               else            
+                       graph_->set_tag("late-frame");          
+                               
+               event_subject_ << monitor::event("host/path")   % filename_
+                                          << monitor::event("host/width")      % width_
+                                          << monitor::event("host/height") % height_
+                                          << monitor::event("host/fps")        % fps_
+                                          << monitor::event("buffer")          % output_buffer_.size() % buffer_size_;
+
+               return frame;
+       }
+               
+       boost::unique_future<std::wstring> call(const std::wstring& param) override
+       {       
+               return executor_.begin_invoke([this, param]() -> std::wstring
+               {                       
+                       try
+                       {
+                               if(!renderer_)
+                               {
+                                       renderer_.reset(new flash_renderer(event_subject_, graph_, frame_factory_, filename_, width_, height_));
+
+                                       while(output_buffer_.size() < buffer_size_)
+                                               output_buffer_.push(core::draw_frame::empty());
+                               }
+
+                               return renderer_->call(param);  
+                       }
+                       catch(...)
+                       {
+                               CASPAR_LOG_CURRENT_EXCEPTION();
+                               renderer_.reset(nullptr);
+                       }
+
+                       return L"";
+               });
+       }
+               
+       std::wstring print() const override
+       { 
+               return L"flash[" + boost::filesystem::path(filename_).wstring() + L"|" + boost::lexical_cast<std::wstring>(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;
+       }
+
+       void subscribe(const monitor::observable::observer_ptr& o) override
+       {
+               event_subject_.subscribe(o);
+       }
+
+       void unsubscribe(const monitor::observable::observer_ptr& o) override
+       {
+               event_subject_.unsubscribe(o);
+       }
+
+       // flash_producer
+       
+       void tick()
+       {
+               double ratio = std::min(1.0, static_cast<double>(output_buffer_.size())/static_cast<double>(std::max(1, buffer_size_ - 1)));
+               double sync  = 2*ratio - ratio*ratio;
+               renderer_->tick(sync);
+       }
+       
+       void next()
+       {       
+               if(!renderer_)
+                       frame_buffer_.push(core::draw_frame::empty());
+
+               tick_timer_.restart();                          
+
+               if(frame_buffer_.empty())
+               {                                       
+                       tick();
+                       auto frame = renderer_->render();
+
+                       if(abs(renderer_->fps()/2.0 - format_desc_.fps) < 2.0) // flash == 2 * format -> interlace
+                       {                                       
+                               tick();
+                               if(format_desc_.field_mode != core::field_mode::progressive)
+                                       frame = core::draw_frame::interlace(frame, renderer_->render(), format_desc_.field_mode);
+                               
+                               frame_buffer_.push(frame);
+                       }
+                       else if(abs(renderer_->fps() - format_desc_.fps/2.0) < 2.0) // format == 2 * flash -> duplicate
+                       {
+                               frame_buffer_.push(frame);
+                               frame_buffer_.push(frame);
+                       }
+                       else //if(abs(renderer_->fps() - format_desc_.fps) < 0.1) // format == flash -> simple
+                       {
+                               frame_buffer_.push(frame);
+                       }
+                                               
+                       fps_.fetch_and_store(static_cast<int>(renderer_->fps()*100.0));                         
+                       graph_->set_text(print());
+                       
+                       if(renderer_->is_empty())                       
+                               renderer_.reset();
+               }
+
+               graph_->set_value("tick-time", static_cast<float>(tick_timer_.elapsed()/fps_)*0.5f);
+               event_subject_ << monitor::event("profiler/time") % tick_timer_.elapsed() % fps_;
+
+               output_buffer_.push(std::move(frame_buffer_.front()));
+               frame_buffer_.pop();
+       }
+};
+
+spl::shared_ptr<core::frame_producer> create_producer(const spl::shared_ptr<core::frame_factory>& frame_factory, const core::video_format_desc& format_desc, const std::vector<std::wstring>& params)
+{
+       auto template_host = get_template_host(format_desc);
+       
+       auto filename = env::template_folder() + L"\\" + template_host.filename;
+       
+       if(!boost::filesystem::exists(filename))
+               CASPAR_THROW_EXCEPTION(file_not_found() << boost::errinfo_file_name(u8(filename)));     
+
+       return create_destroy_proxy(spl::make_shared<flash_producer>(frame_factory, format_desc, 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"";
+}
+
 }}
\ No newline at end of file