\r
#if defined(_MSC_VER)\r
#pragma warning (disable : 4146)\r
+#pragma warning (disable : 4244)\r
#endif\r
\r
#include "flash_producer.h"\r
\r
#include <core/video_format.h>\r
\r
-#include <core/producer/frame/frame_factory.h>\r
#include <core/producer/frame/basic_frame.h>\r
-#include <core/producer/frame/write_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/concurrency/executor.h>\r
-#include <common/utility/timer.h>\r
+#include <common/concurrency/com_context.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
-#include <boost/thread.hpp>\r
\r
-namespace caspar {\r
+#include <tbb/spin_mutex.h>\r
+\r
+namespace caspar { namespace flash {\r
\r
-bool interlaced(double fps, const core::video_format_desc& format_desc)\r
+class bitmap\r
{\r
- return abs(fps/2.0 - format_desc.fps) < 0.1;\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
-class flash_renderer\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
- struct co_init\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
- co_init(){CoInitialize(nullptr);}\r
- ~co_init(){CoUninitialize();}\r
- } co_;\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
- const core::video_format_desc format_desc_;\r
\r
- std::shared_ptr<core::frame_factory> frame_factory_;\r
+ const std::shared_ptr<core::frame_factory> frame_factory_;\r
\r
- BYTE* bmp_data_; \r
- std::shared_ptr<void> hdc_;\r
- std::shared_ptr<void> bmp_;\r
-\r
CComObject<caspar::flash::FlashAxContainer>* ax_;\r
safe_ptr<core::basic_frame> head_;\r
+ bitmap bmp_;\r
\r
safe_ptr<diagnostics::graph> graph_;\r
- timer perf_timer_;\r
+ boost::timer frame_timer_;\r
+ boost::timer tick_timer_;\r
+\r
+ high_prec_timer timer_;\r
+\r
+ const size_t width_;\r
+ const size_t height_;\r
\r
public:\r
- flash_renderer(const safe_ptr<diagnostics::graph>& graph, const std::shared_ptr<core::frame_factory>& frame_factory, const std::wstring& filename) \r
+ flash_renderer(const safe_ptr<diagnostics::graph>& graph, const std::shared_ptr<core::frame_factory>& frame_factory, const std::wstring& filename, int width, int height) \r
: graph_(graph)\r
, filename_(filename)\r
- , format_desc_(frame_factory->get_video_format_desc())\r
, frame_factory_(frame_factory)\r
- , bmp_data_(nullptr)\r
- , hdc_(CreateCompatibleDC(0), DeleteDC)\r
, ax_(nullptr)\r
, head_(core::basic_frame::empty())\r
- {\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(1.0f, 0.0f, 0.0f)); \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
- graph_->set_color("underflow", diagnostics::color(0.6f, 0.3f, 0.9f)); \r
+ graph_->set_color("skip-sync", diagnostics::color(0.8f, 0.3f, 0.2f)); \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"";});\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
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_->SetFormat(format_desc_);\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 = -format_desc_.height;\r
- info.bmiHeader.biPlanes = 1;\r
- info.bmiHeader.biSize = sizeof(BITMAPINFO);\r
- info.bmiHeader.biWidth = format_desc_.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
+ 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
void param(const std::wstring& param)\r
{ \r
if(!ax_->FlashCall(param))\r
- CASPAR_LOG(warning) << print() << " Flash function call failed. Param: " << 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
- if(abs(ax_->GetFPS() - format_desc_.fps) > 0.001 && abs(ax_->GetFPS()/2.0 - format_desc_.fps) > 0.001)\r
- CASPAR_LOG(warning) << print() << " Invalid frame-rate: " << ax_->GetFPS() << L". Should be either " << format_desc_.fps << L" or " << format_desc_.fps*2.0 << L".";\r
}\r
\r
safe_ptr<core::basic_frame> render_frame(bool has_underflow)\r
{\r
+ 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
+ return core::basic_frame::empty(); \r
+ \r
+ if(!has_underflow) \r
+ timer_.tick(frame_time); // This will block the thread.\r
+ else\r
+ graph_->add_tag("skip-sync");\r
+ \r
+ frame_timer_.restart();\r
\r
- auto frame = render_simple_frame(has_underflow);\r
- if(interlaced(ax_->GetFPS(), format_desc_))\r
- frame = core::basic_frame::interlace(frame, render_simple_frame(has_underflow), format_desc_.mode);\r
- return frame;\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 L"flash[" + boost::filesystem::wpath(filename_).filename() + L"]"; \r
}\r
- \r
-private:\r
-\r
- safe_ptr<core::basic_frame> render_simple_frame(bool underflow)\r
- {\r
- if(underflow)\r
- graph_->add_tag("underflow");\r
-\r
- double frame_time = 1.0/ax_->GetFPS();\r
-\r
- perf_timer_.reset();\r
- ax_->Tick();\r
-\r
- if(ax_->InvalidRect())\r
- { \r
- std::fill_n(bmp_data_, format_desc_.size, 0);\r
- ax_->DrawControl(static_cast<HDC>(hdc_.get()));\r
- \r
- auto frame = frame_factory_->create_frame();\r
- std::copy_n(bmp_data_, format_desc_.size, frame->image_data().begin());\r
- head_ = frame;\r
- } \r
- \r
- graph_->update_value("frame-time", static_cast<float>(perf_timer_.elapsed()/frame_time));\r
- return head_;\r
- }\r
};\r
\r
struct flash_producer : public core::frame_producer\r
{ \r
const std::wstring filename_; \r
+ const safe_ptr<core::frame_factory> frame_factory_;\r
+\r
tbb::atomic<int> fps_;\r
\r
std::shared_ptr<diagnostics::graph> graph_;\r
\r
- safe_ptr<core::basic_frame> tail_;\r
tbb::concurrent_bounded_queue<safe_ptr<core::basic_frame>> frame_buffer_;\r
\r
- std::shared_ptr<flash_renderer> renderer_;\r
- std::shared_ptr<core::frame_factory> frame_factory_;\r
- core::video_format_desc format_desc_;\r
- \r
- executor executor_;\r
- \r
+ mutable tbb::spin_mutex last_frame_mutex_;\r
+ safe_ptr<core::basic_frame> last_frame_;\r
+ \r
+ com_context<flash_renderer> context_; \r
+\r
+ int width_;\r
+ int height_;\r
public:\r
- flash_producer(const std::wstring& filename) \r
+ flash_producer(const safe_ptr<core::frame_factory>& frame_factory, const std::wstring& filename, size_t width, size_t height) \r
: filename_(filename) \r
- , tail_(core::basic_frame::empty()) \r
- , executor_(L"flash_producer")\r
+ , frame_factory_(frame_factory)\r
+ , context_(L"flash_producer")\r
+ , last_frame_(core::basic_frame::empty())\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
{ \r
if(!boost::filesystem::exists(filename))\r
BOOST_THROW_EXCEPTION(file_not_found() << boost::errinfo_file_name(narrow(filename))); \r
- \r
+\r
fps_ = 0;\r
+\r
+ graph_ = diagnostics::create_graph(narrow(print()));\r
+ graph_->set_color("output-buffer-count", diagnostics::color(1.0f, 1.0f, 0.0f)); \r
+ graph_->set_color("underflow", diagnostics::color(0.6f, 0.3f, 0.9f)); \r
+ \r
+ frame_buffer_.set_capacity(frame_factory_->get_video_format_desc().fps > 30.0 ? 2 : 1);\r
+\r
+ initialize(); \r
}\r
\r
~flash_producer()\r
{\r
- executor_.clear();\r
- executor_.invoke([=]\r
- {\r
- renderer_ = nullptr;\r
- });\r
- }\r
- \r
- virtual void set_frame_factory(const safe_ptr<core::frame_factory>& frame_factory)\r
- { \r
- frame_factory_ = frame_factory;\r
- format_desc_ = frame_factory->get_video_format_desc();\r
- frame_buffer_.set_capacity(4);\r
- graph_ = diagnostics::create_graph([this]{return print();});\r
- graph_->set_color("output-buffer", diagnostics::color(0.0f, 1.0f, 0.0f));\r
- \r
- executor_.start();\r
- executor_.begin_invoke([=]\r
- {\r
- init_renderer();\r
- });\r
+ frame_buffer_.clear();\r
}\r
- \r
- virtual safe_ptr<core::basic_frame> receive()\r
- { \r
- if(!renderer_)\r
- return core::basic_frame::empty();\r
- \r
- graph_->set_value("output-buffer", static_cast<float>(frame_buffer_.size())/static_cast<float>(frame_buffer_.capacity()));\r
- if(!frame_buffer_.try_pop(tail_))\r
- return tail_;\r
\r
- executor_.begin_invoke([=]\r
- {\r
- if(!renderer_)\r
- return;\r
+ // frame_producer\r
+ \r
+ virtual safe_ptr<core::basic_frame> receive(int)\r
+ { \r
+ graph_->set_value("output-buffer-count", static_cast<float>(frame_buffer_.size())/static_cast<float>(frame_buffer_.capacity()));\r
\r
- try\r
- {\r
- auto frame = core::basic_frame::empty();\r
- do{frame = renderer_->render_frame(frame_buffer_.size() < frame_buffer_.capacity()-2);}\r
- while(frame_buffer_.try_push(frame) && frame == core::basic_frame::empty());\r
- graph_->set_value("output-buffer", static_cast<float>(frame_buffer_.size())/static_cast<float>(frame_buffer_.capacity())); \r
- fps_.fetch_and_store(static_cast<int>(renderer_->fps()*100.0));\r
- }\r
- catch(...)\r
- {\r
- CASPAR_LOG_CURRENT_EXCEPTION();\r
- renderer_ = nullptr;\r
- }\r
- }); \r
+ auto frame = core::basic_frame::late();\r
+ if(!frame_buffer_.try_pop(frame))\r
+ graph_->add_tag("underflow");\r
\r
- return tail_;\r
+ return frame;\r
}\r
+\r
+ virtual safe_ptr<core::basic_frame> last_frame() const\r
+ {\r
+ tbb::spin_mutex::scoped_lock lock(last_frame_mutex_);\r
+ return last_frame_;\r
+ } \r
\r
virtual void param(const std::wstring& param) \r
{ \r
- executor_.begin_invoke([=]\r
+ context_.begin_invoke([=]\r
{\r
- if(!renderer_)\r
- init_renderer();\r
+ if(!context_)\r
+ initialize();\r
\r
try\r
{\r
- renderer_->param(param); \r
+ context_->param(param); \r
+\r
+ //const auto& format_desc = frame_factory_->get_video_format_desc();\r
+ //if(abs(context_->fps() - format_desc.fps) > 0.01 && abs(context_->fps()/2.0 - format_desc.fps) > 0.01)\r
+ // CASPAR_LOG(warning) << print() << " Invalid frame-rate: " << context_->fps() << L". Should be either " << format_desc.fps << L" or " << format_desc.fps*2.0 << L".";\r
}\r
catch(...)\r
{\r
CASPAR_LOG_CURRENT_EXCEPTION();\r
- renderer_ = nullptr;\r
+ context_.reset(nullptr);\r
+ frame_buffer_.push(core::basic_frame::empty());\r
}\r
});\r
}\r
\r
virtual std::wstring print() const\r
{ \r
- return L"flash[" + boost::filesystem::wpath(filename_).filename() + L", " + \r
- boost::lexical_cast<std::wstring>(fps_) + \r
- (interlaced(fps_, format_desc_) ? L"i" : L"p") + L"]"; \r
+ return L"flash[" + boost::filesystem::wpath(filename_).filename() + L"|" + boost::lexical_cast<std::wstring>(fps_) + L"]"; \r
} \r
\r
- void init_renderer()\r
+ // flash_producer\r
+\r
+ void initialize()\r
{\r
- renderer_.reset(new flash_renderer(safe_ptr<diagnostics::graph>(graph_), frame_factory_, filename_));\r
+ context_.reset([&]{return new flash_renderer(safe_ptr<diagnostics::graph>(graph_), frame_factory_, filename_, width_, height_);});\r
while(frame_buffer_.try_push(core::basic_frame::empty())){} \r
+ render(context_.get());\r
+ }\r
+\r
+ safe_ptr<core::basic_frame> render_frame()\r
+ {\r
+ auto frame = context_->render_frame(frame_buffer_.size() < frame_buffer_.capacity()); \r
+ tbb::spin_mutex::scoped_lock lock(last_frame_mutex_);\r
+ last_frame_ = make_safe<core::basic_frame>(frame);\r
+ return frame;\r
+ }\r
+\r
+ void render(const flash_renderer* renderer)\r
+ { \r
+ context_.begin_invoke([=]\r
+ {\r
+ if(context_.get() != renderer) // Since initialize will start a new recursive call make sure the recursive calls are only for a specific instance.\r
+ return;\r
+\r
+ try\r
+ { \r
+ const auto& format_desc = frame_factory_->get_video_format_desc();\r
+\r
+ if(abs(context_->fps()/2.0 - format_desc.fps) < 2.0) // flash == 2 * format -> interlace\r
+ {\r
+ auto frame1 = render_frame();\r
+ auto frame2 = render_frame();\r
+ frame_buffer_.push(core::basic_frame::interlace(frame1, frame2, format_desc.field_mode));\r
+ }\r
+ else if(abs(context_->fps()- format_desc.fps/2.0) < 2.0) // format == 2 * flash -> duplicate\r
+ {\r
+ auto frame = render_frame();\r
+ frame_buffer_.push(frame);\r
+ frame_buffer_.push(frame);\r
+ }\r
+ else //if(abs(renderer_->fps() - format_desc_.fps) < 0.1) // format == flash -> simple\r
+ {\r
+ auto frame = render_frame();\r
+ frame_buffer_.push(frame);\r
+ }\r
+\r
+ graph_->set_value("output-buffer-count", static_cast<float>(frame_buffer_.size())/static_cast<float>(frame_buffer_.capacity())); \r
+ fps_.fetch_and_store(static_cast<int>(context_->fps()*100.0)); \r
+ graph_->update_text(narrow(print()));\r
+\r
+ render(renderer);\r
+ }\r
+ catch(...)\r
+ {\r
+ CASPAR_LOG_CURRENT_EXCEPTION();\r
+ context_.reset(nullptr);\r
+ frame_buffer_.push(core::basic_frame::empty());\r
+ }\r
+ });\r
}\r
};\r
\r
-safe_ptr<core::frame_producer> create_flash_producer(const std::vector<std::wstring>& params)\r
+safe_ptr<core::frame_producer> create_producer(const safe_ptr<core::frame_factory>& frame_factory, const std::vector<std::wstring>& params)\r
{\r
- std::wstring filename = env::template_folder() + L"\\" + params[0];\r
+ auto template_host = get_template_host(frame_factory->get_video_format_desc());\r
\r
- return make_safe<flash_producer>(filename);\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_flash_template(const std::wstring& template_name)\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
return L"";\r
}\r
\r
-}
\ No newline at end of file
+}}
\ No newline at end of file