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
27 #include "flash_producer.h"
\r
28 #include "FlashAxContainer.h"
\r
30 #include <core/video_format.h>
\r
32 #include <core/producer/frame/basic_frame.h>
\r
33 #include <core/producer/frame/write_frame.h>
\r
35 #include <common/concurrency/executor.h>
\r
36 #include <common/utility/timer.h>
\r
37 #include <common/diagnostics/graph.h>
\r
39 #include <boost/filesystem.hpp>
\r
41 #include <functional>
\r
42 #include <boost/thread.hpp>
\r
46 bool interlaced(double fps, const core::video_format_desc& format_desc)
\r
48 return abs(fps/2.0 - format_desc.fps) < 0.1;
\r
51 class flash_renderer
\r
55 co_init(){CoInitialize(nullptr);}
\r
56 ~co_init(){CoUninitialize();}
\r
59 printer parent_printer_;
\r
60 const std::wstring filename_;
\r
61 const core::video_format_desc format_desc_;
\r
63 std::shared_ptr<core::frame_factory> frame_factory_;
\r
66 std::shared_ptr<void> hdc_;
\r
67 std::shared_ptr<void> bmp_;
\r
69 CComObject<caspar::flash::FlashAxContainer>* ax_;
\r
70 safe_ptr<core::basic_frame> head_;
\r
74 safe_ptr<diagnostics::graph> graph_;
\r
77 std::wstring print()
\r
79 return parent_printer_();
\r
83 flash_renderer(const safe_ptr<diagnostics::graph>& graph, const std::shared_ptr<core::frame_factory>& frame_factory, const std::wstring& filename, const printer& parent_printer)
\r
84 : parent_printer_(parent_printer)
\r
86 , filename_(filename)
\r
87 , format_desc_(frame_factory->get_video_format_desc())
\r
88 , frame_factory_(frame_factory)
\r
89 , bmp_data_(nullptr)
\r
90 , hdc_(CreateCompatibleDC(0), DeleteDC)
\r
92 , head_(core::basic_frame::empty())
\r
94 graph_->add_guide("frame-time", 0.5f);
\r
95 graph_->set_color("frame-time", diagnostics::color(1.0f, 0.0f, 0.0f));
\r
96 graph_->set_color("param", diagnostics::color(1.0f, 0.5f, 0.0f));
\r
97 graph_->set_color("underflow", diagnostics::color(0.6f, 0.3f, 0.9f));
\r
99 if(FAILED(CComObject<caspar::flash::FlashAxContainer>::CreateInstance(&ax_)))
\r
100 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Failed to create FlashAxContainer"));
\r
102 ax_->set_print(parent_printer_);
\r
104 if(FAILED(ax_->CreateAxControl()))
\r
105 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Failed to Create FlashAxControl"));
\r
107 CComPtr<IShockwaveFlash> spFlash;
\r
108 if(FAILED(ax_->QueryControl(&spFlash)))
\r
109 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Failed to Query FlashAxControl"));
\r
111 if(FAILED(spFlash->put_Playing(true)) )
\r
112 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Failed to start playing Flash"));
\r
114 if(FAILED(spFlash->put_Movie(CComBSTR(filename.c_str()))))
\r
115 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Failed to Load Template Host"));
\r
117 if(FAILED(spFlash->put_ScaleMode(2))) //Exact fit. Scale without respect to the aspect ratio.
\r
118 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Failed to Set Scale Mode"));
\r
120 ax_->SetFormat(format_desc_);
\r
123 memset(&info, 0, sizeof(BITMAPINFO));
\r
124 info.bmiHeader.biBitCount = 32;
\r
125 info.bmiHeader.biCompression = BI_RGB;
\r
126 info.bmiHeader.biHeight = -format_desc_.height;
\r
127 info.bmiHeader.biPlanes = 1;
\r
128 info.bmiHeader.biSize = sizeof(BITMAPINFO);
\r
129 info.bmiHeader.biWidth = format_desc_.width;
\r
131 bmp_.reset(CreateDIBSection(static_cast<HDC>(hdc_.get()), &info, DIB_RGB_COLORS, reinterpret_cast<void**>(&bmp_data_), 0, 0), DeleteObject);
\r
132 SelectObject(static_cast<HDC>(hdc_.get()), bmp_.get());
\r
133 CASPAR_LOG(info) << parent_printer_() << L" Thread started.";
\r
140 ax_->DestroyAxControl();
\r
143 CASPAR_LOG(info) << print() << L" Thread ended.";
\r
146 void param(const std::wstring& param)
\r
148 if(!ax_->FlashCall(param))
\r
149 CASPAR_LOG(warning) << print() << " Flash function call failed. Param: " << param << ".";
\r
150 graph_->add_tag("param");
\r
152 if(abs(ax_->GetFPS() / format_desc_.fps) > 0.001)
\r
153 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
156 safe_ptr<core::basic_frame> render_frame(bool has_underflow)
\r
159 return core::basic_frame::empty();
\r
161 auto frame = render_simple_frame(has_underflow);
\r
162 if(interlaced(ax_->GetFPS(), format_desc_))
\r
163 frame = core::basic_frame::interlace(frame, render_simple_frame(has_underflow), format_desc_.mode);
\r
169 return ax_->GetFPS();
\r
174 safe_ptr<core::basic_frame> render_simple_frame(bool underflow)
\r
177 graph_->add_tag("underflow");
\r
179 double frame_time = 1.0/ax_->GetFPS()*(underflow ? 0.90 : 1.0); // Reduce sync-time if in underflow.
\r
180 timer_.tick(frame_time); // Tick doesnt work on nested timelines, force an actual sync
\r
182 perf_timer_.reset();
\r
185 if(ax_->InvalidRect())
\r
187 std::fill_n(bmp_data_, format_desc_.size, 0);
\r
188 ax_->DrawControl(static_cast<HDC>(hdc_.get()));
\r
190 auto frame = frame_factory_->create_frame();
\r
191 std::copy_n(bmp_data_, format_desc_.size, frame->image_data().begin());
\r
195 graph_->update_value("frame-time", static_cast<float>(perf_timer_.elapsed()/frame_time));
\r
200 struct flash_producer::implementation
\r
202 printer parent_printer_;
\r
203 const std::wstring filename_;
\r
204 tbb::atomic<int> fps_;
\r
206 std::shared_ptr<diagnostics::graph> graph_;
\r
208 safe_ptr<core::basic_frame> tail_;
\r
209 tbb::concurrent_bounded_queue<safe_ptr<core::basic_frame>> frame_buffer_;
\r
211 std::shared_ptr<flash_renderer> renderer_;
\r
212 std::shared_ptr<core::frame_factory> frame_factory_;
\r
213 core::video_format_desc format_desc_;
\r
215 executor executor_;
\r
217 std::wstring print() const
\r
219 return (parent_printer_ ? parent_printer_() + L"/" : L"") + L"flash[" + boost::filesystem::wpath(filename_).filename() + L", " +
\r
220 boost::lexical_cast<std::wstring>(fps_) +
\r
221 (interlaced(fps_, format_desc_) ? L"i" : L"p") + L"]";
\r
225 implementation(const std::wstring& filename)
\r
226 : filename_(filename)
\r
227 , tail_(core::basic_frame::empty())
\r
228 , executor_(print())
\r
230 if(!boost::filesystem::exists(filename))
\r
231 BOOST_THROW_EXCEPTION(file_not_found() << boost::errinfo_file_name(narrow(filename)));
\r
233 graph_ = diagnostics::create_graph(boost::bind(&implementation::print, this));
\r
235 graph_->set_color("output-buffer", diagnostics::color(0.0f, 1.0f, 0.0f));
\r
241 executor_.invoke([=]
\r
243 renderer_ = nullptr;
\r
247 virtual void initialize(const safe_ptr<core::frame_factory>& frame_factory)
\r
249 frame_factory_ = frame_factory;
\r
250 format_desc_ = frame_factory->get_video_format_desc();
\r
251 frame_buffer_.set_capacity(static_cast<size_t>(format_desc_.fps/4.0));
\r
255 virtual void set_parent_printer(const printer& parent_printer)
\r
257 parent_printer_ = parent_printer;
\r
260 virtual safe_ptr<core::basic_frame> receive()
\r
263 return core::basic_frame::empty();
\r
265 graph_->set_value("output-buffer", static_cast<float>(frame_buffer_.size())/static_cast<float>(frame_buffer_.capacity()));
\r
266 if(!frame_buffer_.try_pop(tail_))
\r
269 executor_.begin_invoke([=]
\r
276 auto frame = core::basic_frame::empty();
\r
277 do{frame = renderer_->render_frame(frame_buffer_.size() < frame_buffer_.capacity()-2);}
\r
278 while(frame_buffer_.try_push(frame) && frame == core::basic_frame::empty());
\r
279 graph_->set_value("output-buffer", static_cast<float>(frame_buffer_.size())/static_cast<float>(frame_buffer_.capacity()));
\r
280 fps_.fetch_and_store(static_cast<int>(renderer_->fps()*100.0));
\r
284 CASPAR_LOG_CURRENT_EXCEPTION();
\r
285 renderer_ = nullptr;
\r
292 void param(const std::wstring& param)
\r
294 executor_.begin_invoke([=]
\r
298 renderer_.reset(new flash_renderer(safe_ptr<diagnostics::graph>(graph_), frame_factory_, filename_, [=]{return print();}));
\r
299 while(frame_buffer_.try_push(core::basic_frame::empty())){}
\r
304 renderer_->param(param);
\r
308 CASPAR_LOG_CURRENT_EXCEPTION();
\r
309 renderer_ = nullptr;
\r
315 flash_producer::flash_producer(flash_producer&& other) : impl_(std::move(other.impl_)){}
\r
316 flash_producer::flash_producer(const std::wstring& filename) : impl_(new implementation(filename)){}
\r
317 safe_ptr<core::basic_frame> flash_producer::receive(){return impl_->receive();}
\r
318 void flash_producer::param(const std::wstring& param){impl_->param(param);}
\r
319 void flash_producer::initialize(const safe_ptr<core::frame_factory>& frame_factory) { impl_->initialize(frame_factory);}
\r
320 void flash_producer::set_parent_printer(const printer& parent_printer){impl_->set_parent_printer(parent_printer);}
\r
321 std::wstring flash_producer::print() const {return impl_->print();}
\r
323 std::wstring flash_producer::find_template(const std::wstring& template_name)
\r
325 if(boost::filesystem::exists(template_name + L".ft"))
\r
326 return template_name + L".ft";
\r
328 if(boost::filesystem::exists(template_name + L".ct"))
\r
329 return template_name + L".ct";
\r