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 "../../format/video_format.h"
\r
33 #include "../../processor/draw_frame.h"
\r
34 #include "../../processor/frame_processor_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 // NOTE: This is needed in order to make CComObject work since this is not a real ATL project.
\r
44 CComModule _AtlModule;
\r
45 extern __declspec(selectany) CAtlModule* _pAtlModule = &_AtlModule;
\r
47 class flash_renderer
\r
50 flash_renderer(const std::shared_ptr<frame_processor_device>& frame_processor, const std::wstring& filename)
\r
51 : head_(draw_frame::empty()), bmp_data_(nullptr), ax_(nullptr), filename_(filename), hdc_(CreateCompatibleDC(0), DeleteDC), frame_processor_(frame_processor),
\r
52 format_desc_(frame_processor->get_video_format_desc())
\r
54 CASPAR_LOG(info) << print() << L" Started";
\r
56 if(FAILED(CComObject<caspar::flash::FlashAxContainer>::CreateInstance(&ax_)))
\r
57 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(bprint() + "Failed to create FlashAxContainer"));
\r
59 if(FAILED(ax_->CreateAxControl()))
\r
60 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(bprint() + "Failed to Create FlashAxControl"));
\r
62 CComPtr<IShockwaveFlash> spFlash;
\r
63 if(FAILED(ax_->QueryControl(&spFlash)))
\r
64 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(bprint() + "Failed to Query FlashAxControl"));
\r
66 if(FAILED(spFlash->put_Playing(true)) )
\r
67 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(bprint() + "Failed to start playing Flash"));
\r
69 if(FAILED(spFlash->put_Movie(CComBSTR(filename.c_str()))))
\r
70 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(bprint() + "Failed to Load Template Host"));
\r
72 if(FAILED(spFlash->put_ScaleMode(2))) //Exact fit. Scale without respect to the aspect ratio.
\r
73 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(bprint() + "Failed to Set Scale Mode"));
\r
75 ax_->SetFormat(format_desc_);
\r
78 memset(&info, 0, sizeof(BITMAPINFO));
\r
79 info.bmiHeader.biBitCount = 32;
\r
80 info.bmiHeader.biCompression = BI_RGB;
\r
81 info.bmiHeader.biHeight = -format_desc_.height;
\r
82 info.bmiHeader.biPlanes = 1;
\r
83 info.bmiHeader.biSize = sizeof(BITMAPINFO);
\r
84 info.bmiHeader.biWidth = format_desc_.width;
\r
86 bmp_.reset(CreateDIBSection(static_cast<HDC>(hdc_.get()), &info, DIB_RGB_COLORS, reinterpret_cast<void**>(&bmp_data_), 0, 0), DeleteObject);
\r
87 SelectObject(static_cast<HDC>(hdc_.get()), bmp_.get());
\r
94 ax_->DestroyAxControl();
\r
97 CASPAR_LOG(info) << print() << L" Ended";
\r
100 void param(const std::wstring& param)
\r
102 if(!ax_->FlashCall(param))
\r
103 CASPAR_LOG(warning) << "Flash Function Call Failed. Param: " << param;
\r
106 safe_ptr<draw_frame> render_frame()
\r
109 return draw_frame::empty();
\r
111 auto frame = render_simple_frame();
\r
113 auto running_fps = static_cast<int>(ax_->GetFPS());
\r
114 auto target_fps = static_cast<int>(format_desc_.actual_fps);
\r
115 if(target_fps < running_fps)
\r
116 frame = draw_frame::interlace(frame, render_simple_frame(), format_desc_.mode);
\r
121 std::wstring print() const{ return L"flash[" + boost::filesystem::wpath(filename_).filename() + L"] Render thread"; }
\r
122 std::string bprint() const{ return narrow(print()); }
\r
126 safe_ptr<draw_frame> render_simple_frame()
\r
128 timer_.wait(ax_->GetFPS()); // Tick doesnt work on nested timelines, force an actual sync
\r
132 if(ax_->InvalidRect())
\r
134 std::fill_n(bmp_data_, format_desc_.size, 0);
\r
135 ax_->DrawControl(static_cast<HDC>(hdc_.get()));
\r
137 auto frame = frame_processor_->create_frame();
\r
138 std::copy_n(bmp_data_, format_desc_.size, frame->image_data().begin());
\r
145 std::wstring filename_;
\r
146 std::shared_ptr<frame_processor_device> frame_processor_;
\r
147 video_format_desc format_desc_;
\r
151 std::shared_ptr<void> hdc_;
\r
152 std::shared_ptr<void> bmp_;
\r
154 CComObject<caspar::flash::FlashAxContainer>* ax_;
\r
155 safe_ptr<draw_frame> head_;
\r
158 struct flash_producer::implementation
\r
160 implementation(const std::wstring& filename) : filename_(filename), tail_(draw_frame::empty())
\r
162 if(!boost::filesystem::exists(filename))
\r
163 BOOST_THROW_EXCEPTION(file_not_found() << boost::errinfo_file_name(narrow(filename)));
\r
165 executor_.begin_invoke([=]
\r
167 ::OleInitialize(nullptr);
\r
170 frame_buffer_.set_capacity(8);
\r
171 while(frame_buffer_.try_push(draw_frame::empty())){}
\r
177 executor_.invoke([=]
\r
179 renderer_ = nullptr;
\r
180 ::OleUninitialize();
\r
184 virtual safe_ptr<draw_frame> receive()
\r
186 if(!frame_buffer_.try_pop(tail_))
\r
187 CASPAR_LOG(trace) << print() << " underflow";
\r
190 executor_.begin_invoke([=]
\r
197 auto frame = draw_frame::empty();
\r
198 do{frame = renderer_->render_frame();}
\r
199 while(frame_buffer_.try_push(frame) && frame == draw_frame::empty());
\r
203 CASPAR_LOG_CURRENT_EXCEPTION();
\r
204 renderer_ = nullptr;
\r
211 virtual void initialize(const safe_ptr<frame_processor_device>& frame_processor)
\r
213 frame_processor_ = frame_processor;
\r
217 void param(const std::wstring& param)
\r
219 executor_.begin_invoke([=]
\r
222 renderer_.reset(new flash_renderer(frame_processor_, filename_));
\r
226 renderer_->param(param);
\r
230 CASPAR_LOG_CURRENT_EXCEPTION();
\r
231 renderer_ = nullptr;
\r
236 safe_ptr<draw_frame> tail_;
\r
237 tbb::concurrent_bounded_queue<safe_ptr<draw_frame>> frame_buffer_;
\r
238 executor executor_;
\r
240 std::shared_ptr<flash_renderer> renderer_;
\r
241 std::shared_ptr<frame_processor_device> frame_processor_;
\r
243 std::wstring print() const{ return L"flash[" + boost::filesystem::wpath(filename_).filename() + L"]"; }
\r
244 std::wstring filename_;
\r
247 flash_producer::flash_producer(flash_producer&& other) : impl_(std::move(other.impl_)){}
\r
248 flash_producer::flash_producer(const std::wstring& filename) : impl_(new implementation(filename)){}
\r
249 safe_ptr<draw_frame> flash_producer::receive(){return impl_->receive();}
\r
250 void flash_producer::param(const std::wstring& param){impl_->param(param);}
\r
251 void flash_producer::initialize(const safe_ptr<frame_processor_device>& frame_processor) { impl_->initialize(frame_processor);}
\r
252 std::wstring flash_producer::print() const {return impl_->print();}
\r
254 std::wstring flash_producer::find_template(const std::wstring& template_name)
\r
256 if(boost::filesystem::exists(template_name + L".ft"))
\r
257 return template_name + L".ft";
\r
259 if(boost::filesystem::exists(template_name + L".ct"))
\r
260 return template_name + L".ct";
\r