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/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
52 class flash_renderer
\r
54 const std::wstring filename_;
\r
55 const core::video_format_desc format_desc_;
\r
57 const std::shared_ptr<core::frame_factory> frame_factory_;
\r
60 std::shared_ptr<void> hdc_;
\r
61 std::shared_ptr<void> bmp_;
\r
63 CComObject<caspar::flash::FlashAxContainer>* ax_;
\r
64 safe_ptr<core::basic_frame> head_;
\r
66 safe_ptr<diagnostics::graph> graph_;
\r
67 boost::timer frame_timer_;
\r
68 boost::timer tick_timer_;
\r
70 high_prec_timer timer_;
\r
73 flash_renderer(const safe_ptr<diagnostics::graph>& graph, const std::shared_ptr<core::frame_factory>& frame_factory, const std::wstring& filename)
\r
75 , filename_(filename)
\r
76 , format_desc_(frame_factory->get_video_format_desc())
\r
77 , frame_factory_(frame_factory)
\r
78 , bmp_data_(nullptr)
\r
79 , hdc_(CreateCompatibleDC(0), DeleteDC)
\r
81 , head_(core::basic_frame::empty())
\r
83 graph_->add_guide("frame-time", 0.5f);
\r
84 graph_->set_color("frame-time", diagnostics::color(1.0f, 0.0f, 0.0f));
\r
85 graph_->add_guide("tick-time", 0.5);
\r
86 graph_->set_color("tick-time", diagnostics::color(0.1f, 0.7f, 0.8f));
\r
87 graph_->set_color("param", diagnostics::color(1.0f, 0.5f, 0.0f));
\r
88 graph_->set_color("underflow", diagnostics::color(0.6f, 0.3f, 0.9f));
\r
90 if(FAILED(CComObject<caspar::flash::FlashAxContainer>::CreateInstance(&ax_)))
\r
91 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Failed to create FlashAxContainer"));
\r
93 ax_->set_print([this]{return L"flash_renderer";});
\r
95 if(FAILED(ax_->CreateAxControl()))
\r
96 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Failed to Create FlashAxControl"));
\r
98 CComPtr<IShockwaveFlash> spFlash;
\r
99 if(FAILED(ax_->QueryControl(&spFlash)))
\r
100 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Failed to Query FlashAxControl"));
\r
102 if(FAILED(spFlash->put_Playing(true)) )
\r
103 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Failed to start playing Flash"));
\r
105 if(FAILED(spFlash->put_Movie(CComBSTR(filename.c_str()))))
\r
106 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Failed to Load Template Host"));
\r
108 if(FAILED(spFlash->put_ScaleMode(2))) //Exact fit. Scale without respect to the aspect ratio.
\r
109 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Failed to Set Scale Mode"));
\r
111 ax_->SetFormat(format_desc_);
\r
114 memset(&info, 0, sizeof(BITMAPINFO));
\r
115 info.bmiHeader.biBitCount = 32;
\r
116 info.bmiHeader.biCompression = BI_RGB;
\r
117 info.bmiHeader.biHeight = -format_desc_.height;
\r
118 info.bmiHeader.biPlanes = 1;
\r
119 info.bmiHeader.biSize = sizeof(BITMAPINFO);
\r
120 info.bmiHeader.biWidth = format_desc_.width;
\r
122 bmp_.reset(CreateDIBSection(static_cast<HDC>(hdc_.get()), &info, DIB_RGB_COLORS, reinterpret_cast<void**>(&bmp_data_), 0, 0), DeleteObject);
\r
123 SelectObject(static_cast<HDC>(hdc_.get()), bmp_.get());
\r
124 CASPAR_LOG(info) << print() << L" Thread started.";
\r
131 ax_->DestroyAxControl();
\r
134 CASPAR_LOG(info) << print() << L" Thread ended.";
\r
137 void param(const std::wstring& param)
\r
139 if(!ax_->FlashCall(param))
\r
140 CASPAR_LOG(warning) << print() << " Flash function call failed. Param: " << param << ".";
\r
141 graph_->add_tag("param");
\r
143 if(abs(ax_->GetFPS() - format_desc_.fps) > 0.001 && abs(ax_->GetFPS()/2.0 - format_desc_.fps) > 0.001)
\r
144 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
147 safe_ptr<core::basic_frame> render_frame(bool has_underflow)
\r
149 float frame_time = 1.0f/ax_->GetFPS();
\r
151 graph_->update_value("tick-time", static_cast<float>(tick_timer_.elapsed()/frame_time)*0.5f);
\r
152 tick_timer_.restart();
\r
155 return core::basic_frame::empty();
\r
157 if(!has_underflow)
\r
158 timer_.tick(frame_time); // This will block the thread.
\r
160 graph_->add_tag("underflow");
\r
162 frame_timer_.restart();
\r
165 if(ax_->InvalidRect())
\r
167 fast_memclr(bmp_data_, format_desc_.size);
\r
168 ax_->DrawControl(static_cast<HDC>(hdc_.get()));
\r
170 auto frame = frame_factory_->create_frame(this);
\r
171 fast_memcpy(frame->image_data().begin(), bmp_data_, format_desc_.size);
\r
175 graph_->update_value("frame-time", static_cast<float>(frame_timer_.elapsed()/frame_time)*0.5f);
\r
181 return ax_->GetFPS();
\r
184 std::wstring print()
\r
186 return L"flash[" + boost::filesystem::wpath(filename_).filename() + L"]";
\r
190 struct flash_producer : public core::frame_producer
\r
192 const std::wstring filename_;
\r
193 const safe_ptr<core::frame_factory> frame_factory_;
\r
195 tbb::atomic<int> fps_;
\r
197 std::shared_ptr<diagnostics::graph> graph_;
\r
199 safe_ptr<core::basic_frame> tail_;
\r
200 tbb::concurrent_bounded_queue<safe_ptr<core::basic_frame>> frame_buffer_;
\r
202 com_context<flash_renderer> context_;
\r
204 flash_producer(const safe_ptr<core::frame_factory>& frame_factory, const std::wstring& filename)
\r
205 : filename_(filename)
\r
206 , frame_factory_(frame_factory)
\r
207 , tail_(core::basic_frame::empty())
\r
208 , context_(L"flash_producer")
\r
210 if(!boost::filesystem::exists(filename))
\r
211 BOOST_THROW_EXCEPTION(file_not_found() << boost::errinfo_file_name(narrow(filename)));
\r
215 graph_ = diagnostics::create_graph([this]{return print();});
\r
216 graph_->set_color("output-buffer", diagnostics::color(0.0f, 1.0f, 0.0f));
\r
218 frame_buffer_.set_capacity(2);
\r
225 frame_buffer_.clear();
\r
230 virtual safe_ptr<core::basic_frame> receive()
\r
232 graph_->set_value("output-buffer", static_cast<float>(frame_buffer_.size())/static_cast<float>(frame_buffer_.capacity()));
\r
234 frame_buffer_.try_pop(tail_);
\r
238 virtual void param(const std::wstring& param)
\r
240 context_.begin_invoke([=]
\r
242 if(!context_.get())
\r
247 context_->param(param);
\r
251 CASPAR_LOG_CURRENT_EXCEPTION();
\r
252 context_.reset(nullptr);
\r
253 frame_buffer_.push(core::basic_frame::empty());
\r
258 virtual std::wstring print() const
\r
260 return L"flash[" + boost::filesystem::wpath(filename_).filename() + L", " +
\r
261 boost::lexical_cast<std::wstring>(fps_) + L"]";
\r
268 context_.reset([&]{return new flash_renderer(safe_ptr<diagnostics::graph>(graph_), frame_factory_, filename_);});
\r
269 while(frame_buffer_.try_push(core::basic_frame::empty())){}
\r
270 render(context_.get());
\r
273 void render(const flash_renderer* renderer)
\r
275 context_.begin_invoke([=]
\r
277 if(context_.get() != renderer) // Since initialize will start a new recursive call make sure the recursive calls are only for a specific instance.
\r
282 const auto& format_desc = frame_factory_->get_video_format_desc();
\r
283 auto frame = core::basic_frame::empty();
\r
284 if(abs(context_->fps()/2.0 - format_desc.fps) < 0.1) // flash 50fps, format 50i
\r
286 auto frame1 = context_->render_frame(frame_buffer_.size() < frame_buffer_.capacity());
\r
287 auto frame2 = context_->render_frame(frame_buffer_.size() < frame_buffer_.capacity());
\r
288 frame_buffer_.push(core::basic_frame::interlace(frame1, frame2, format_desc.mode));
\r
291 else if(abs(context_->fps()- format_desc.fps/2.0 ) < 0.1) // flash 25fps, format 50p
\r
293 frame = context_->render_frame(frame_buffer_.size() < frame_buffer_.capacity());
\r
294 frame_buffer_.push(frame);
\r
295 frame_buffer_.push(frame);
\r
297 else //if(abs(renderer_->fps() - format_desc_.fps) < 0.1) // flash 25fps, format 50i or flash 50fps, format 50p
\r
299 frame = context_->render_frame(frame_buffer_.size() < frame_buffer_.capacity());
\r
300 frame_buffer_.push(frame);
\r
303 graph_->set_value("output-buffer", static_cast<float>(frame_buffer_.size())/static_cast<float>(frame_buffer_.capacity()));
\r
304 fps_.fetch_and_store(static_cast<int>(context_->fps()*100.0));
\r
310 CASPAR_LOG_CURRENT_EXCEPTION();
\r
311 context_.reset(nullptr);
\r
312 frame_buffer_.push(core::basic_frame::empty());
\r
318 safe_ptr<core::frame_producer> create_flash_producer(const safe_ptr<core::frame_factory>& frame_factory, const std::vector<std::wstring>& params)
\r
320 std::wstring filename = env::template_folder() + L"\\" + params[0];
\r
322 return make_safe<flash_producer>(frame_factory, filename);
\r
325 std::wstring find_flash_template(const std::wstring& template_name)
\r
327 if(boost::filesystem::exists(template_name + L".ft"))
\r
328 return template_name + L".ft";
\r
330 if(boost::filesystem::exists(template_name + L".ct"))
\r
331 return template_name + L".ct";
\r