2 * copyright (c) 2010 Sveriges Television AB <info@casparcg.com>
\r
4 * This file is part of CasparCG.
\r
6 * CasparCG is free software: you can redistribute it and/or modify
\r
7 * it under the terms of the GNU General Public License as published by
\r
8 * the Free Software Foundation, either version 3 of the License, or
\r
9 * (at your option) any later version.
\r
11 * CasparCG is distributed in the hope that it will be useful,
\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 * GNU General Public License for more details.
\r
16 * You should have received a copy of the GNU General Public License
\r
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
\r
21 #include "../stdafx.h"
\r
23 #if defined(_MSC_VER)
\r
24 #pragma warning (disable : 4146)
\r
25 #pragma warning (disable : 4244)
\r
28 #include "flash_producer.h"
\r
29 #include "FlashAxContainer.h"
\r
31 #include <core/video_format.h>
\r
33 #include <core/producer/frame/basic_frame.h>
\r
34 #include <core/producer/frame/frame_factory.h>
\r
35 #include <core/mixer/write_frame.h>
\r
37 #include <common/env.h>
\r
38 #include <common/concurrency/com_context.h>
\r
39 #include <common/diagnostics/graph.h>
\r
40 #include <common/memory/memcpy.h>
\r
41 #include <common/memory/memclr.h>
\r
42 #include <common/utility/timer.h>
\r
44 #include <boost/filesystem.hpp>
\r
45 #include <boost/thread.hpp>
\r
46 #include <boost/timer.hpp>
\r
48 #include <functional>
\r
55 bitmap(size_t width, size_t height)
\r
56 : bmp_data_(nullptr)
\r
57 , hdc_(CreateCompatibleDC(0), DeleteDC)
\r
60 memset(&info, 0, sizeof(BITMAPINFO));
\r
61 info.bmiHeader.biBitCount = 32;
\r
62 info.bmiHeader.biCompression = BI_RGB;
\r
63 info.bmiHeader.biHeight = -height;
\r
64 info.bmiHeader.biPlanes = 1;
\r
65 info.bmiHeader.biSize = sizeof(BITMAPINFO);
\r
66 info.bmiHeader.biWidth = width;
\r
68 bmp_.reset(CreateDIBSection(static_cast<HDC>(hdc_.get()), &info, DIB_RGB_COLORS, reinterpret_cast<void**>(&bmp_data_), 0, 0), DeleteObject);
\r
69 SelectObject(static_cast<HDC>(hdc_.get()), bmp_.get());
\r
72 BOOST_THROW_EXCEPTION(std::bad_alloc());
\r
75 operator HDC() {return static_cast<HDC>(hdc_.get());}
\r
77 BYTE* data() { return bmp_data_;}
\r
78 const BYTE* data() const { return bmp_data_;}
\r
82 std::shared_ptr<void> hdc_;
\r
83 std::shared_ptr<void> bmp_;
\r
86 struct template_host
\r
88 std::string video_mode;
\r
89 std::string filename;
\r
94 template_host get_template_host(const core::video_format_desc& desc)
\r
96 std::vector<template_host> template_hosts;
\r
97 BOOST_FOREACH(auto& xml_mapping, env::properties().get_child("configuration.producers.template-hosts"))
\r
101 template_host template_host;
\r
102 template_host.video_mode = xml_mapping.second.get("video-mode", narrow(desc.name));
\r
103 template_host.filename = xml_mapping.second.get("filename", "cg.fth");
\r
104 template_host.width = xml_mapping.second.get("width", desc.width);
\r
105 template_host.height = xml_mapping.second.get("height", desc.height);
\r
106 template_hosts.push_back(template_host);
\r
111 auto template_host_it = boost::find_if(template_hosts, [&](template_host template_host){return template_host.video_mode == narrow(desc.name);});
\r
112 if(template_host_it == template_hosts.end())
\r
113 template_host_it = boost::find_if(template_hosts, [&](template_host template_host){return template_host.video_mode == "";});
\r
115 if(template_host_it != template_hosts.end())
\r
116 return *template_host_it;
\r
118 template_host template_host;
\r
119 template_host.filename = "cg.fth";
\r
120 template_host.width = desc.width;
\r
121 template_host.height = desc.height;
\r
122 return template_host;
\r
125 class flash_renderer
\r
127 const std::wstring filename_;
\r
129 const std::shared_ptr<core::frame_factory> frame_factory_;
\r
131 CComObject<caspar::flash::FlashAxContainer>* ax_;
\r
132 safe_ptr<core::basic_frame> head_;
\r
135 safe_ptr<diagnostics::graph> graph_;
\r
136 boost::timer frame_timer_;
\r
137 boost::timer tick_timer_;
\r
139 high_prec_timer timer_;
\r
141 const size_t width_;
\r
142 const size_t height_;
\r
145 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
147 , filename_(filename)
\r
148 , frame_factory_(frame_factory)
\r
150 , head_(core::basic_frame::empty())
\r
151 , bmp_(width, height)
\r
155 graph_->add_guide("frame-time", 0.5f);
\r
156 graph_->set_color("frame-time", diagnostics::color(0.1f, 1.0f, 0.1f));
\r
157 graph_->add_guide("tick-time", 0.5);
\r
158 graph_->set_color("tick-time", diagnostics::color(0.0f, 0.6f, 0.9f));
\r
159 graph_->set_color("param", diagnostics::color(1.0f, 0.5f, 0.0f));
\r
160 graph_->set_color("underflow", diagnostics::color(0.6f, 0.3f, 0.9f));
\r
162 if(FAILED(CComObject<caspar::flash::FlashAxContainer>::CreateInstance(&ax_)))
\r
163 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Failed to create FlashAxContainer"));
\r
165 ax_->set_print([this]{return L"flash_renderer";});
\r
167 if(FAILED(ax_->CreateAxControl()))
\r
168 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Failed to Create FlashAxControl"));
\r
170 CComPtr<IShockwaveFlash> spFlash;
\r
171 if(FAILED(ax_->QueryControl(&spFlash)))
\r
172 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Failed to Query FlashAxControl"));
\r
174 if(FAILED(spFlash->put_Playing(true)) )
\r
175 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Failed to start playing Flash"));
\r
177 if(FAILED(spFlash->put_Movie(CComBSTR(filename.c_str()))))
\r
178 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Failed to Load Template Host"));
\r
180 if(FAILED(spFlash->put_ScaleMode(2))) //Exact fit. Scale without respect to the aspect ratio.
\r
181 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Failed to Set Scale Mode"));
\r
183 ax_->SetSize(width_, height_);
\r
185 CASPAR_LOG(info) << print() << L" Thread started.";
\r
186 CASPAR_LOG(info) << print() << L" Successfully initialized with template-host: " << filename << L" width: " << width_ << L" height: " << height_ << L".";
\r
193 ax_->DestroyAxControl();
\r
196 CASPAR_LOG(info) << print() << L" Thread ended.";
\r
199 void param(const std::wstring& param)
\r
201 if(!ax_->FlashCall(param))
\r
202 BOOST_THROW_EXCEPTION(invalid_operation() << msg_info("Flash function call failed.") << arg_name_info("param") << arg_value_info(narrow(param)));
\r
203 graph_->add_tag("param");
\r
206 safe_ptr<core::basic_frame> render_frame(bool has_underflow)
\r
208 float frame_time = 1.0f/ax_->GetFPS();
\r
210 graph_->update_value("tick-time", static_cast<float>(tick_timer_.elapsed()/frame_time)*0.5f);
\r
211 tick_timer_.restart();
\r
214 return core::basic_frame::empty();
\r
216 if(!has_underflow)
\r
217 timer_.tick(frame_time); // This will block the thread.
\r
219 graph_->add_tag("underflow");
\r
221 frame_timer_.restart();
\r
224 if(ax_->InvalidRect())
\r
226 fast_memclr(bmp_.data(), width_*height_*4);
\r
227 ax_->DrawControl(bmp_);
\r
229 auto frame = frame_factory_->create_frame(this, width_, height_);
\r
230 fast_memcpy(frame->image_data().begin(), bmp_.data(), width_*height_*4);
\r
235 graph_->update_value("frame-time", static_cast<float>(frame_timer_.elapsed()/frame_time)*0.5f);
\r
241 return ax_->GetFPS();
\r
244 std::wstring print()
\r
246 return L"flash[" + boost::filesystem::wpath(filename_).filename() + L"]";
\r
250 struct flash_producer : public core::frame_producer
\r
252 const std::wstring filename_;
\r
253 const safe_ptr<core::frame_factory> frame_factory_;
\r
255 tbb::atomic<int> fps_;
\r
257 std::shared_ptr<diagnostics::graph> graph_;
\r
259 tbb::concurrent_bounded_queue<safe_ptr<core::basic_frame>> frame_buffer_;
\r
261 safe_ptr<core::basic_frame> last_frame_;
\r
263 com_context<flash_renderer> context_;
\r
268 flash_producer(const safe_ptr<core::frame_factory>& frame_factory, const std::wstring& filename, size_t width, size_t height)
\r
269 : filename_(filename)
\r
270 , frame_factory_(frame_factory)
\r
271 , context_(L"flash_producer")
\r
272 , last_frame_(core::basic_frame::empty())
\r
273 , width_(width > 0 ? width : frame_factory->get_video_format_desc().width)
\r
274 , height_(height > 0 ? height : frame_factory->get_video_format_desc().height)
\r
276 if(!boost::filesystem::exists(filename))
\r
277 BOOST_THROW_EXCEPTION(file_not_found() << boost::errinfo_file_name(narrow(filename)));
\r
281 graph_ = diagnostics::create_graph([this]{return print();});
\r
282 graph_->set_color("output-buffer", diagnostics::color(0.0f, 1.0f, 0.0f));
\r
284 frame_buffer_.set_capacity(frame_factory_->get_video_format_desc().fps > 30.0 ? 2 : 1);
\r
291 frame_buffer_.clear();
\r
296 virtual safe_ptr<core::basic_frame> receive(int)
\r
298 graph_->set_value("output-buffer", static_cast<float>(frame_buffer_.size())/static_cast<float>(frame_buffer_.capacity()));
\r
300 auto frame = core::basic_frame::late();
\r
301 if(frame_buffer_.try_pop(frame))
\r
302 last_frame_ = frame;
\r
307 virtual safe_ptr<core::basic_frame> last_frame() const
\r
309 return last_frame_;
\r
312 virtual void param(const std::wstring& param)
\r
314 context_.begin_invoke([=]
\r
321 context_->param(param);
\r
323 //const auto& format_desc = frame_factory_->get_video_format_desc();
\r
324 //if(abs(context_->fps() - format_desc.fps) > 0.01 && abs(context_->fps()/2.0 - format_desc.fps) > 0.01)
\r
325 // 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
329 CASPAR_LOG_CURRENT_EXCEPTION();
\r
330 context_.reset(nullptr);
\r
331 frame_buffer_.push(core::basic_frame::empty());
\r
336 virtual std::wstring print() const
\r
338 return L"flash[" + boost::filesystem::wpath(filename_).filename() + L", " +
\r
339 boost::lexical_cast<std::wstring>(fps_) + L"]";
\r
346 context_.reset([&]{return new flash_renderer(safe_ptr<diagnostics::graph>(graph_), frame_factory_, filename_, width_, height_);});
\r
347 while(frame_buffer_.try_push(core::basic_frame::empty())){}
\r
348 render(context_.get());
\r
351 void render(const flash_renderer* renderer)
\r
353 context_.begin_invoke([=]
\r
355 if(context_.get() != renderer) // Since initialize will start a new recursive call make sure the recursive calls are only for a specific instance.
\r
360 const auto& format_desc = frame_factory_->get_video_format_desc();
\r
362 if(abs(context_->fps()/2.0 - format_desc.fps) < 2.0) // flash == 2 * format -> interlace
\r
364 auto frame1 = context_->render_frame(frame_buffer_.size() < frame_buffer_.capacity());
\r
365 auto frame2 = context_->render_frame(frame_buffer_.size() < frame_buffer_.capacity());
\r
366 frame_buffer_.push(core::basic_frame::interlace(frame1, frame2, format_desc.mode));
\r
368 else if(abs(context_->fps()- format_desc.fps/2.0) < 2.0) // format == 2 * flash -> duplicate
\r
370 auto frame = context_->render_frame(frame_buffer_.size() < frame_buffer_.capacity());
\r
371 frame_buffer_.push(frame);
\r
372 frame_buffer_.push(frame);
\r
374 else //if(abs(renderer_->fps() - format_desc_.fps) < 0.1) // format == flash -> simple
\r
376 auto frame = context_->render_frame(frame_buffer_.size() < frame_buffer_.capacity());
\r
377 frame_buffer_.push(frame);
\r
380 graph_->set_value("output-buffer", static_cast<float>(frame_buffer_.size())/static_cast<float>(frame_buffer_.capacity()));
\r
381 fps_.fetch_and_store(static_cast<int>(context_->fps()*100.0));
\r
387 CASPAR_LOG_CURRENT_EXCEPTION();
\r
388 context_.reset(nullptr);
\r
389 frame_buffer_.push(core::basic_frame::empty());
\r
395 safe_ptr<core::frame_producer> create_flash_producer(const safe_ptr<core::frame_factory>& frame_factory, const std::vector<std::wstring>& params)
\r
397 auto template_host = get_template_host(frame_factory->get_video_format_desc());
\r
399 return make_safe<flash_producer>(frame_factory, env::template_folder() + L"\\" + widen(template_host.filename), template_host.width, template_host.height);
\r
402 std::wstring find_flash_template(const std::wstring& template_name)
\r
404 if(boost::filesystem::exists(template_name + L".ft"))
\r
405 return template_name + L".ft";
\r
407 if(boost::filesystem::exists(template_name + L".ct"))
\r
408 return template_name + L".ct";
\r