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/producer/frame/write_frame.h>
\r
37 #include <common/env.h>
\r
38 #include <common/concurrency/executor.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
52 class flash_renderer
\r
56 co_init(){CoInitialize(nullptr);}
\r
57 ~co_init(){CoUninitialize();}
\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
72 safe_ptr<diagnostics::graph> graph_;
\r
73 boost::timer perf_timer_;
\r
75 high_prec_timer timer_;
\r
78 flash_renderer(const safe_ptr<diagnostics::graph>& graph, const std::shared_ptr<core::frame_factory>& frame_factory, const std::wstring& filename)
\r
80 , filename_(filename)
\r
81 , format_desc_(frame_factory->get_video_format_desc())
\r
82 , frame_factory_(frame_factory)
\r
83 , bmp_data_(nullptr)
\r
84 , hdc_(CreateCompatibleDC(0), DeleteDC)
\r
86 , head_(core::basic_frame::empty())
\r
88 graph_->add_guide("frame-time", 0.5f);
\r
89 graph_->set_color("frame-time", diagnostics::color(1.0f, 0.0f, 0.0f));
\r
90 graph_->set_color("param", diagnostics::color(1.0f, 0.5f, 0.0f));
\r
91 graph_->set_color("underflow", diagnostics::color(0.6f, 0.3f, 0.9f));
\r
93 if(FAILED(CComObject<caspar::flash::FlashAxContainer>::CreateInstance(&ax_)))
\r
94 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Failed to create FlashAxContainer"));
\r
96 ax_->set_print([this]{return L"";});
\r
98 if(FAILED(ax_->CreateAxControl()))
\r
99 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Failed to Create FlashAxControl"));
\r
101 CComPtr<IShockwaveFlash> spFlash;
\r
102 if(FAILED(ax_->QueryControl(&spFlash)))
\r
103 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Failed to Query FlashAxControl"));
\r
105 if(FAILED(spFlash->put_Playing(true)) )
\r
106 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Failed to start playing Flash"));
\r
108 if(FAILED(spFlash->put_Movie(CComBSTR(filename.c_str()))))
\r
109 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Failed to Load Template Host"));
\r
111 if(FAILED(spFlash->put_ScaleMode(2))) //Exact fit. Scale without respect to the aspect ratio.
\r
112 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Failed to Set Scale Mode"));
\r
114 ax_->SetFormat(format_desc_);
\r
117 memset(&info, 0, sizeof(BITMAPINFO));
\r
118 info.bmiHeader.biBitCount = 32;
\r
119 info.bmiHeader.biCompression = BI_RGB;
\r
120 info.bmiHeader.biHeight = -format_desc_.height;
\r
121 info.bmiHeader.biPlanes = 1;
\r
122 info.bmiHeader.biSize = sizeof(BITMAPINFO);
\r
123 info.bmiHeader.biWidth = format_desc_.width;
\r
125 bmp_.reset(CreateDIBSection(static_cast<HDC>(hdc_.get()), &info, DIB_RGB_COLORS, reinterpret_cast<void**>(&bmp_data_), 0, 0), DeleteObject);
\r
126 SelectObject(static_cast<HDC>(hdc_.get()), bmp_.get());
\r
127 CASPAR_LOG(info) << print() << L" Thread started.";
\r
134 ax_->DestroyAxControl();
\r
137 CASPAR_LOG(info) << print() << L" Thread ended.";
\r
140 void param(const std::wstring& param)
\r
142 if(!ax_->FlashCall(param))
\r
143 CASPAR_LOG(warning) << print() << " Flash function call failed. Param: " << param << ".";
\r
144 graph_->add_tag("param");
\r
146 if(abs(ax_->GetFPS() - format_desc_.fps) > 0.001 && abs(ax_->GetFPS()/2.0 - format_desc_.fps) > 0.001)
\r
147 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
150 safe_ptr<core::basic_frame> render_frame(bool has_underflow)
\r
153 return core::basic_frame::empty();
\r
155 double frame_time = 1.0/ax_->GetFPS();
\r
160 graph_->add_tag("underflow");
\r
164 timer_.tick(frame_time);
\r
165 perf_timer_.restart();
\r
168 if(ax_->InvalidRect())
\r
170 fast_memclr(bmp_data_, format_desc_.size);
\r
171 ax_->DrawControl(static_cast<HDC>(hdc_.get()));
\r
173 auto frame = frame_factory_->create_frame(this);
\r
174 fast_memcpy(frame->image_data().begin(), bmp_data_, format_desc_.size);
\r
179 graph_->update_value("frame-time", static_cast<float>(perf_timer_.elapsed()/frame_time)*0.5f);
\r
185 return ax_->GetFPS();
\r
188 std::wstring print()
\r
190 return L"flash[" + boost::filesystem::wpath(filename_).filename() + L"]";
\r
194 struct flash_producer : public core::frame_producer
\r
196 const std::wstring filename_;
\r
197 tbb::atomic<int> fps_;
\r
199 std::shared_ptr<diagnostics::graph> graph_;
\r
201 safe_ptr<core::basic_frame> tail_;
\r
202 tbb::concurrent_bounded_queue<safe_ptr<core::basic_frame>> frame_buffer_;
\r
204 std::shared_ptr<flash_renderer> renderer_;
\r
205 safe_ptr<core::frame_factory> frame_factory_;
\r
206 const core::video_format_desc format_desc_;
\r
208 executor executor_;
\r
211 flash_producer(const safe_ptr<core::frame_factory>& frame_factory, const std::wstring& filename)
\r
212 : filename_(filename)
\r
213 , tail_(core::basic_frame::empty())
\r
214 , frame_factory_(frame_factory)
\r
215 , format_desc_(frame_factory->get_video_format_desc())
\r
216 , executor_(L"flash_producer", true)
\r
218 if(!boost::filesystem::exists(filename))
\r
219 BOOST_THROW_EXCEPTION(file_not_found() << boost::errinfo_file_name(narrow(filename)));
\r
221 frame_buffer_.set_capacity(2);
\r
222 graph_ = diagnostics::create_graph([this]{return print();});
\r
223 graph_->set_color("output-buffer", diagnostics::color(0.0f, 1.0f, 0.0f));
\r
225 executor_.begin_invoke([=]
\r
230 executor_.begin_invoke([=]
\r
241 frame_buffer_.clear();
\r
242 executor_.invoke([=]
\r
244 renderer_ = nullptr;
\r
250 virtual safe_ptr<core::basic_frame> receive()
\r
252 graph_->set_value("output-buffer", static_cast<float>(frame_buffer_.size())/static_cast<float>(frame_buffer_.capacity()));
\r
253 if(!frame_buffer_.try_pop(tail_))
\r
259 virtual void param(const std::wstring& param)
\r
261 executor_.begin_invoke([=]
\r
268 renderer_->param(param);
\r
272 CASPAR_LOG_CURRENT_EXCEPTION();
\r
273 renderer_ = nullptr;
\r
278 virtual std::wstring print() const
\r
280 return L"flash[" + boost::filesystem::wpath(filename_).filename() + L", " +
\r
281 boost::lexical_cast<std::wstring>(fps_) + L"]";
\r
286 void init_renderer()
\r
288 renderer_.reset(new flash_renderer(safe_ptr<diagnostics::graph>(graph_), frame_factory_, filename_));
\r
289 while(frame_buffer_.try_push(core::basic_frame::empty())){}
\r
296 frame_buffer_.push(core::basic_frame::empty());
\r
302 auto frame = core::basic_frame::empty();
\r
303 if(abs(renderer_->fps()/2.0 - format_desc_.fps) < 0.1) //flash 50, format 50i
\r
305 auto frame1 = renderer_->render_frame(frame_buffer_.size() < frame_buffer_.capacity()-3);
\r
306 auto frame2 = renderer_->render_frame(frame_buffer_.size() < frame_buffer_.capacity()-3);
\r
307 frame_buffer_.push(core::basic_frame::interlace(frame1, frame2, format_desc_.mode));
\r
310 else if(abs(renderer_->fps()- format_desc_.fps/2.0 ) < 0.1) //flash 25, format 50p
\r
312 frame = renderer_->render_frame(frame_buffer_.size() < frame_buffer_.capacity()-3);
\r
313 frame_buffer_.push(frame);
\r
314 frame_buffer_.push(frame);
\r
316 else //if(abs(renderer_->fps() - format_desc_.fps) < 0.1) // flash 25, format 50i or flash 50, format 50p
\r
318 frame = renderer_->render_frame(frame_buffer_.size() < frame_buffer_.capacity()-3);
\r
319 frame_buffer_.push(frame);
\r
322 graph_->set_value("output-buffer", static_cast<float>(frame_buffer_.size())/static_cast<float>(frame_buffer_.capacity()));
\r
323 fps_.fetch_and_store(static_cast<int>(renderer_->fps()*100.0));
\r
325 executor_.begin_invoke([=]
\r
332 CASPAR_LOG_CURRENT_EXCEPTION();
\r
333 renderer_ = nullptr;
\r
338 safe_ptr<core::frame_producer> create_flash_producer(const safe_ptr<core::frame_factory>& frame_factory, const std::vector<std::wstring>& params)
\r
340 std::wstring filename = env::template_folder() + L"\\" + params[0];
\r
342 return make_safe<flash_producer>(frame_factory, filename);
\r
345 std::wstring find_flash_template(const std::wstring& template_name)
\r
347 if(boost::filesystem::exists(template_name + L".ft"))
\r
348 return template_name + L".ft";
\r
350 if(boost::filesystem::exists(template_name + L".ct"))
\r
351 return template_name + L".ct";
\r