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 <tbb/spin_mutex.h>
\r
50 #include <functional>
\r
57 bitmap(size_t width, size_t height)
\r
58 : bmp_data_(nullptr)
\r
59 , hdc_(CreateCompatibleDC(0), DeleteDC)
\r
62 memset(&info, 0, sizeof(BITMAPINFO));
\r
63 info.bmiHeader.biBitCount = 32;
\r
64 info.bmiHeader.biCompression = BI_RGB;
\r
65 info.bmiHeader.biHeight = -height;
\r
66 info.bmiHeader.biPlanes = 1;
\r
67 info.bmiHeader.biSize = sizeof(BITMAPINFO);
\r
68 info.bmiHeader.biWidth = width;
\r
70 bmp_.reset(CreateDIBSection(static_cast<HDC>(hdc_.get()), &info, DIB_RGB_COLORS, reinterpret_cast<void**>(&bmp_data_), 0, 0), DeleteObject);
\r
71 SelectObject(static_cast<HDC>(hdc_.get()), bmp_.get());
\r
74 BOOST_THROW_EXCEPTION(std::bad_alloc());
\r
77 operator HDC() {return static_cast<HDC>(hdc_.get());}
\r
79 BYTE* data() { return bmp_data_;}
\r
80 const BYTE* data() const { return bmp_data_;}
\r
84 std::shared_ptr<void> hdc_;
\r
85 std::shared_ptr<void> bmp_;
\r
88 struct template_host
\r
90 std::string video_mode;
\r
91 std::string filename;
\r
96 template_host get_template_host(const core::video_format_desc& desc)
\r
98 std::vector<template_host> template_hosts;
\r
99 BOOST_FOREACH(auto& xml_mapping, env::properties().get_child("configuration.producers.template-hosts"))
\r
103 template_host template_host;
\r
104 template_host.video_mode = xml_mapping.second.get("video-mode", narrow(desc.name));
\r
105 template_host.filename = xml_mapping.second.get("filename", "cg.fth");
\r
106 template_host.width = xml_mapping.second.get("width", desc.width);
\r
107 template_host.height = xml_mapping.second.get("height", desc.height);
\r
108 template_hosts.push_back(template_host);
\r
113 auto template_host_it = boost::find_if(template_hosts, [&](template_host template_host){return template_host.video_mode == narrow(desc.name);});
\r
114 if(template_host_it == template_hosts.end())
\r
115 template_host_it = boost::find_if(template_hosts, [&](template_host template_host){return template_host.video_mode == "";});
\r
117 if(template_host_it != template_hosts.end())
\r
118 return *template_host_it;
\r
120 template_host template_host;
\r
121 template_host.filename = "cg.fth";
\r
122 template_host.width = desc.width;
\r
123 template_host.height = desc.height;
\r
124 return template_host;
\r
127 class flash_renderer
\r
129 const std::wstring filename_;
\r
131 const std::shared_ptr<core::frame_factory> frame_factory_;
\r
133 CComObject<caspar::flash::FlashAxContainer>* ax_;
\r
134 safe_ptr<core::basic_frame> head_;
\r
137 safe_ptr<diagnostics::graph> graph_;
\r
138 boost::timer frame_timer_;
\r
139 boost::timer tick_timer_;
\r
141 high_prec_timer timer_;
\r
143 const size_t width_;
\r
144 const size_t height_;
\r
147 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
149 , filename_(filename)
\r
150 , frame_factory_(frame_factory)
\r
152 , head_(core::basic_frame::empty())
\r
153 , bmp_(width, height)
\r
157 graph_->add_guide("frame-time", 0.5f);
\r
158 graph_->set_color("frame-time", diagnostics::color(0.8f, 0.3f, 0.4f));
\r
159 graph_->add_guide("tick-time", 0.5);
\r
160 graph_->set_color("tick-time", diagnostics::color(0.1f, 0.7f, 0.8f));
\r
161 graph_->set_color("param", diagnostics::color(1.0f, 0.5f, 0.0f));
\r
162 graph_->set_color("underflow", diagnostics::color(0.6f, 0.3f, 0.9f));
\r
164 if(FAILED(CComObject<caspar::flash::FlashAxContainer>::CreateInstance(&ax_)))
\r
165 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Failed to create FlashAxContainer"));
\r
167 ax_->set_print([this]{return L"flash_renderer";});
\r
169 if(FAILED(ax_->CreateAxControl()))
\r
170 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Failed to Create FlashAxControl"));
\r
172 CComPtr<IShockwaveFlash> spFlash;
\r
173 if(FAILED(ax_->QueryControl(&spFlash)))
\r
174 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Failed to Query FlashAxControl"));
\r
176 if(FAILED(spFlash->put_Playing(true)) )
\r
177 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Failed to start playing Flash"));
\r
179 if(FAILED(spFlash->put_Movie(CComBSTR(filename.c_str()))))
\r
180 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Failed to Load Template Host"));
\r
182 if(FAILED(spFlash->put_ScaleMode(2))) //Exact fit. Scale without respect to the aspect ratio.
\r
183 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Failed to Set Scale Mode"));
\r
185 ax_->SetSize(width_, height_);
\r
187 CASPAR_LOG(info) << print() << L" Thread started.";
\r
188 CASPAR_LOG(info) << print() << L" Successfully initialized with template-host: " << filename << L" width: " << width_ << L" height: " << height_ << L".";
\r
195 ax_->DestroyAxControl();
\r
198 CASPAR_LOG(info) << print() << L" Thread ended.";
\r
201 void param(const std::wstring& param)
\r
203 if(!ax_->FlashCall(param))
\r
204 CASPAR_LOG(warning) << print() << " Flash function call failed. Param: " << param << ".";
\r
205 graph_->add_tag("param");
\r
208 safe_ptr<core::basic_frame> render_frame(bool has_underflow)
\r
210 float frame_time = 1.0f/ax_->GetFPS();
\r
212 graph_->update_value("tick-time", static_cast<float>(tick_timer_.elapsed()/frame_time)*0.5f);
\r
213 tick_timer_.restart();
\r
216 return core::basic_frame::empty();
\r
218 if(!has_underflow)
\r
219 timer_.tick(frame_time); // This will block the thread.
\r
221 graph_->add_tag("underflow");
\r
223 frame_timer_.restart();
\r
226 if(ax_->InvalidRect())
\r
228 fast_memclr(bmp_.data(), width_*height_*4);
\r
229 ax_->DrawControl(bmp_);
\r
231 auto frame = frame_factory_->create_frame(this, width_, height_);
\r
232 fast_memcpy(frame->image_data().begin(), bmp_.data(), width_*height_*4);
\r
237 graph_->update_value("frame-time", static_cast<float>(frame_timer_.elapsed()/frame_time)*0.5f);
\r
243 return ax_->GetFPS();
\r
246 std::wstring print()
\r
248 return L"flash[" + boost::filesystem::wpath(filename_).filename() + L"]";
\r
252 struct flash_producer : public core::frame_producer
\r
254 const std::wstring filename_;
\r
255 const safe_ptr<core::frame_factory> frame_factory_;
\r
257 tbb::atomic<int> fps_;
\r
259 std::shared_ptr<diagnostics::graph> graph_;
\r
261 tbb::concurrent_bounded_queue<safe_ptr<core::basic_frame>> frame_buffer_;
\r
263 safe_ptr<core::basic_frame> last_frame_;
\r
265 com_context<flash_renderer> context_;
\r
267 tbb::spin_mutex exception_mutex_;
\r
268 std::exception_ptr exception_;
\r
273 flash_producer(const safe_ptr<core::frame_factory>& frame_factory, const std::wstring& filename, size_t width, size_t height)
\r
274 : filename_(filename)
\r
275 , frame_factory_(frame_factory)
\r
276 , context_(L"flash_producer")
\r
277 , last_frame_(core::basic_frame::empty())
\r
278 , width_(width > 0 ? width : frame_factory->get_video_format_desc().width)
\r
279 , height_(height > 0 ? height : frame_factory->get_video_format_desc().height)
\r
281 if(!boost::filesystem::exists(filename))
\r
282 BOOST_THROW_EXCEPTION(file_not_found() << boost::errinfo_file_name(narrow(filename)));
\r
286 graph_ = diagnostics::create_graph([this]{return print();});
\r
287 graph_->set_color("output-buffer", diagnostics::color(0.0f, 1.0f, 0.0f));
\r
289 frame_buffer_.set_capacity(1);
\r
291 context_.reset([&]{return new flash_renderer(safe_ptr<diagnostics::graph>(graph_), frame_factory_, filename_, width_, height_);});
\r
292 while(frame_buffer_.try_push(core::basic_frame::empty())){}
\r
298 frame_buffer_.clear();
\r
303 virtual safe_ptr<core::basic_frame> receive(int)
\r
306 tbb::spin_mutex::scoped_lock lock(exception_mutex_);
\r
307 if(exception_ != nullptr)
\r
308 std::rethrow_exception(exception_);
\r
311 graph_->set_value("output-buffer", static_cast<float>(frame_buffer_.size())/static_cast<float>(frame_buffer_.capacity()));
\r
313 auto frame = core::basic_frame::late();
\r
314 if(frame_buffer_.try_pop(frame))
\r
315 last_frame_ = frame;
\r
320 virtual safe_ptr<core::basic_frame> last_frame() const
\r
322 return last_frame_;
\r
325 virtual void param(const std::wstring& param)
\r
328 tbb::spin_mutex::scoped_lock lock(exception_mutex_);
\r
329 if(exception_ != nullptr)
\r
330 std::rethrow_exception(exception_);
\r
333 context_.begin_invoke([=]
\r
337 context_->param(param);
\r
339 const auto& format_desc = frame_factory_->get_video_format_desc();
\r
340 if(abs(context_->fps() - format_desc.fps) > 2.0 && abs(context_->fps()/2.0 - format_desc.fps) > 2.0)
\r
341 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
345 CASPAR_LOG_CURRENT_EXCEPTION();
\r
346 context_.reset(nullptr);
\r
348 tbb::spin_mutex::scoped_lock lock(exception_mutex_);
\r
349 exception_ = std::current_exception();
\r
354 virtual std::wstring print() const
\r
356 return L"flash[" + boost::filesystem::wpath(filename_).filename() + L", " + boost::lexical_cast<std::wstring>(fps_) + L"]";
\r
363 context_.begin_invoke([=]
\r
367 const auto& format_desc = frame_factory_->get_video_format_desc();
\r
369 if(abs(context_->fps()/2.0 - format_desc.fps) < 2.0) // flash == 2 * format -> interlace
\r
371 auto frame1 = context_->render_frame(frame_buffer_.size() < frame_buffer_.capacity());
\r
372 auto frame2 = context_->render_frame(frame_buffer_.size() < frame_buffer_.capacity());
\r
373 frame_buffer_.push(core::basic_frame::interlace(frame1, frame2, format_desc.mode));
\r
375 else if(abs(context_->fps()- format_desc.fps/2.0) < 2.0) // format == 2 * flash -> duplicate
\r
377 auto frame = context_->render_frame(frame_buffer_.size() < frame_buffer_.capacity());
\r
378 frame_buffer_.push(frame);
\r
379 frame_buffer_.push(frame);
\r
381 else //if(abs(renderer_->fps() - format_desc_.fps) < 0.1) // format == flash -> simple
\r
383 auto frame = context_->render_frame(frame_buffer_.size() < frame_buffer_.capacity());
\r
384 frame_buffer_.push(frame);
\r
387 graph_->set_value("output-buffer", static_cast<float>(frame_buffer_.size())/static_cast<float>(frame_buffer_.capacity()));
\r
388 fps_.fetch_and_store(static_cast<int>(context_->fps()*100.0));
\r
394 CASPAR_LOG_CURRENT_EXCEPTION();
\r
395 context_.reset(nullptr);
\r
397 tbb::spin_mutex::scoped_lock lock(exception_mutex_);
\r
398 exception_ = std::current_exception();
\r
404 safe_ptr<core::frame_producer> create_flash_producer(const safe_ptr<core::frame_factory>& frame_factory, const std::vector<std::wstring>& params)
\r
406 auto template_host = get_template_host(frame_factory->get_video_format_desc());
\r
408 return make_safe<flash_producer>(frame_factory, env::template_folder() + L"\\" + widen(template_host.filename), template_host.width, template_host.height);
\r
411 std::wstring find_flash_template(const std::wstring& template_name)
\r
413 if(boost::filesystem::exists(template_name + L".ft"))
\r
414 return template_name + L".ft";
\r
416 if(boost::filesystem::exists(template_name + L".ct"))
\r
417 return template_name + L".ct";
\r