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 : 4714) // marked as __forceinline not inlined
\r
25 #pragma warning (disable : 4146)
\r
28 #include "flash_producer.h"
\r
29 #include "FlashAxContainer.h"
\r
31 #include <core/video_format.h>
\r
33 #include <mixer/frame/draw_frame.h>
\r
34 #include <mixer/frame_mixer_device.h>
\r
36 #include <common/concurrency/executor.h>
\r
37 #include <common/utility/timer.h>
\r
39 #include <boost/filesystem.hpp>
\r
41 namespace caspar { namespace core { namespace flash {
\r
43 class flash_renderer
\r
47 co_init(){CoInitialize(nullptr);}
\r
48 ~co_init(){CoUninitialize();}
\r
51 const std::wstring filename_;
\r
52 const video_format_desc format_desc_;
\r
54 std::shared_ptr<frame_factory> frame_factory_;
\r
57 std::shared_ptr<void> hdc_;
\r
58 std::shared_ptr<void> bmp_;
\r
60 CComObject<caspar::flash::FlashAxContainer>* ax_;
\r
61 safe_ptr<draw_frame> head_;
\r
64 flash_renderer(const std::shared_ptr<frame_factory>& frame_factory, const std::wstring& filename)
\r
65 : filename_(filename)
\r
66 , format_desc_(frame_factory->get_video_format_desc())
\r
67 , frame_factory_(frame_factory)
\r
68 , bmp_data_(nullptr)
\r
69 , hdc_(CreateCompatibleDC(0), DeleteDC)
\r
71 , head_(draw_frame::empty())
\r
73 CASPAR_LOG(info) << print() << L" Started";
\r
75 if(FAILED(CComObject<caspar::flash::FlashAxContainer>::CreateInstance(&ax_)))
\r
76 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(bprint() + "Failed to create FlashAxContainer"));
\r
78 if(FAILED(ax_->CreateAxControl()))
\r
79 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(bprint() + "Failed to Create FlashAxControl"));
\r
81 CComPtr<IShockwaveFlash> spFlash;
\r
82 if(FAILED(ax_->QueryControl(&spFlash)))
\r
83 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(bprint() + "Failed to Query FlashAxControl"));
\r
85 if(FAILED(spFlash->put_Playing(true)) )
\r
86 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(bprint() + "Failed to start playing Flash"));
\r
88 if(FAILED(spFlash->put_Movie(CComBSTR(filename.c_str()))))
\r
89 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(bprint() + "Failed to Load Template Host"));
\r
91 if(FAILED(spFlash->put_ScaleMode(2))) //Exact fit. Scale without respect to the aspect ratio.
\r
92 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(bprint() + "Failed to Set Scale Mode"));
\r
94 ax_->SetFormat(format_desc_);
\r
97 memset(&info, 0, sizeof(BITMAPINFO));
\r
98 info.bmiHeader.biBitCount = 32;
\r
99 info.bmiHeader.biCompression = BI_RGB;
\r
100 info.bmiHeader.biHeight = -format_desc_.height;
\r
101 info.bmiHeader.biPlanes = 1;
\r
102 info.bmiHeader.biSize = sizeof(BITMAPINFO);
\r
103 info.bmiHeader.biWidth = format_desc_.width;
\r
105 bmp_.reset(CreateDIBSection(static_cast<HDC>(hdc_.get()), &info, DIB_RGB_COLORS, reinterpret_cast<void**>(&bmp_data_), 0, 0), DeleteObject);
\r
106 SelectObject(static_cast<HDC>(hdc_.get()), bmp_.get());
\r
113 ax_->DestroyAxControl();
\r
116 CASPAR_LOG(info) << print() << L" Ended";
\r
119 void param(const std::wstring& param)
\r
121 if(!ax_->FlashCall(param))
\r
122 CASPAR_LOG(warning) << "Flash Function Call Failed. Param: " << param;
\r
125 safe_ptr<draw_frame> render_frame()
\r
128 return draw_frame::empty();
\r
130 auto frame = render_simple_frame();
\r
131 if(ax_->GetFPS()/2.0 - format_desc_.fps >= 0.0)
\r
132 frame = draw_frame::interlace(frame, render_simple_frame(), format_desc_.mode);
\r
137 std::wstring print() const{ return L"flash[" + boost::filesystem::wpath(filename_).filename() + L"] Render thread"; }
\r
138 std::string bprint() const{ return narrow(print()); }
\r
142 safe_ptr<draw_frame> render_simple_frame()
\r
144 timer_.tick(1.0/ax_->GetFPS()); // Tick doesnt work on nested timelines, force an actual sync
\r
148 if(ax_->InvalidRect())
\r
150 std::fill_n(bmp_data_, format_desc_.size, 0);
\r
151 ax_->DrawControl(static_cast<HDC>(hdc_.get()));
\r
153 auto frame = frame_factory_->create_frame();
\r
154 std::copy_n(bmp_data_, format_desc_.size, frame->image_data().begin());
\r
162 struct flash_producer::implementation
\r
164 safe_ptr<draw_frame> tail_;
\r
165 tbb::concurrent_bounded_queue<safe_ptr<draw_frame>> frame_buffer_;
\r
167 std::shared_ptr<flash_renderer> renderer_;
\r
168 std::shared_ptr<frame_factory> frame_factory_;
\r
170 std::wstring print() const{ return L"flash[" + boost::filesystem::wpath(filename_).filename() + L"]"; }
\r
171 std::wstring filename_;
\r
173 executor executor_;
\r
175 implementation(const std::wstring& filename)
\r
176 : filename_(filename)
\r
177 , tail_(draw_frame::empty())
\r
179 if(!boost::filesystem::exists(filename))
\r
180 BOOST_THROW_EXCEPTION(file_not_found() << boost::errinfo_file_name(narrow(filename)));
\r
186 executor_.invoke([=]
\r
188 renderer_ = nullptr;
\r
192 virtual void initialize(const safe_ptr<frame_factory>& frame_factory)
\r
194 frame_factory_ = frame_factory;
\r
195 frame_buffer_.set_capacity(static_cast<size_t>(frame_factory->get_video_format_desc().fps/2.0));
\r
199 virtual safe_ptr<draw_frame> receive()
\r
201 if(!frame_buffer_.try_pop(tail_))
\r
202 CASPAR_LOG(trace) << print() << " underflow";
\r
205 executor_.begin_invoke([=]
\r
212 auto frame = draw_frame::empty();
\r
213 do{frame = renderer_->render_frame();}
\r
214 while(frame_buffer_.try_push(frame) && frame == draw_frame::empty());
\r
218 CASPAR_LOG_CURRENT_EXCEPTION();
\r
219 renderer_ = nullptr;
\r
227 void param(const std::wstring& param)
\r
229 executor_.begin_invoke([=]
\r
233 renderer_.reset(new flash_renderer(frame_factory_, filename_));
\r
234 while(frame_buffer_.try_push(draw_frame::empty())){}
\r
239 renderer_->param(param);
\r
243 CASPAR_LOG_CURRENT_EXCEPTION();
\r
244 renderer_ = nullptr;
\r
250 flash_producer::flash_producer(flash_producer&& other) : impl_(std::move(other.impl_)){}
\r
251 flash_producer::flash_producer(const std::wstring& filename) : impl_(new implementation(filename)){}
\r
252 safe_ptr<draw_frame> flash_producer::receive(){return impl_->receive();}
\r
253 void flash_producer::param(const std::wstring& param){impl_->param(param);}
\r
254 void flash_producer::initialize(const safe_ptr<frame_factory>& frame_factory) { impl_->initialize(frame_factory);}
\r
255 std::wstring flash_producer::print() const {return impl_->print();}
\r
257 std::wstring flash_producer::find_template(const std::wstring& template_name)
\r
259 if(boost::filesystem::exists(template_name + L".ft"))
\r
260 return template_name + L".ft";
\r
262 if(boost::filesystem::exists(template_name + L".ct"))
\r
263 return template_name + L".ct";
\r