]> git.sesse.net Git - casparcg/blob - core/producer/flash/flash_producer.cpp
c4d15bbaf8e1d02b26765074fb1a161eee4afdf3
[casparcg] / core / producer / flash / flash_producer.cpp
1 /*\r
2 * copyright (c) 2010 Sveriges Television AB <info@casparcg.com>\r
3 *\r
4 *  This file is part of CasparCG.\r
5 *\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
10 *\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
15 \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
18 *\r
19 */\r
20  \r
21 #include "..\..\stdafx.h"\r
22 \r
23 #if defined(_MSC_VER)\r
24 #pragma warning (disable : 4714) // marked as __forceinline not inlined\r
25 #pragma warning (disable : 4146)\r
26 #endif\r
27 \r
28 #include "flash_producer.h"\r
29 #include "FlashAxContainer.h"\r
30 \r
31 #include "../../format/video_format.h"\r
32 \r
33 #include "../../processor/draw_frame.h"\r
34 #include "../../processor/frame_processor_device.h"\r
35 \r
36 #include <common/concurrency/executor.h>\r
37 #include <common/utility/timer.h>\r
38 \r
39 #include <boost/filesystem.hpp>\r
40 \r
41 namespace caspar { namespace core { namespace flash {\r
42 \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
46 \r
47 class flash_renderer\r
48 {\r
49 public:\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
53         {\r
54                 CASPAR_LOG(info) << print() << L" Started";\r
55                 \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
58                 \r
59                 if(FAILED(ax_->CreateAxControl()))\r
60                         BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(bprint() + "Failed to Create FlashAxControl"));\r
61                 \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
65                                                                                                 \r
66                 if(FAILED(spFlash->put_Playing(true)) )\r
67                         BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(bprint() + "Failed to start playing Flash"));\r
68 \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
71                                                                                 \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
74                                                                                                                 \r
75                 ax_->SetFormat(format_desc_);\r
76                 \r
77                 BITMAPINFO info;\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
85 \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
88         }\r
89 \r
90         ~flash_renderer()\r
91         {               \r
92                 if(ax_)\r
93                 {\r
94                         ax_->DestroyAxControl();\r
95                         ax_->Release();\r
96                 }\r
97                 CASPAR_LOG(info) << print() << L" Ended";\r
98         }\r
99         \r
100         void param(const std::wstring& param)\r
101         {               \r
102                 if(!ax_->FlashCall(param))\r
103                         CASPAR_LOG(warning) << "Flash Function Call Failed. Param: " << param;\r
104         }\r
105         \r
106         safe_ptr<draw_frame> render_frame()\r
107         {\r
108                 if(ax_->IsEmpty())\r
109                         return draw_frame::empty();\r
110 \r
111                 auto frame = render_simple_frame();\r
112                 \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
117                 return frame;\r
118         }\r
119                 \r
120         timer timer_;\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
123 \r
124 private:\r
125 \r
126         safe_ptr<draw_frame> render_simple_frame()\r
127         {\r
128                 timer_.wait(ax_->GetFPS()); // Tick doesnt work on nested timelines, force an actual sync\r
129 \r
130                 ax_->Tick();\r
131 \r
132                 if(ax_->InvalidRect())\r
133                 {                       \r
134                         std::fill_n(bmp_data_, format_desc_.size, 0);\r
135                         ax_->DrawControl(static_cast<HDC>(hdc_.get()));\r
136                 \r
137                         auto frame = frame_processor_->create_frame();\r
138                         std::copy_n(bmp_data_, format_desc_.size, frame->image_data().begin());\r
139                         head_ = frame;\r
140                 }               \r
141 \r
142                 return head_;\r
143         }\r
144 \r
145         std::wstring filename_;\r
146         std::shared_ptr<frame_processor_device> frame_processor_;\r
147         video_format_desc format_desc_;\r
148         \r
149         BYTE* bmp_data_;\r
150         \r
151         std::shared_ptr<void> hdc_;\r
152         std::shared_ptr<void> bmp_;\r
153 \r
154         CComObject<caspar::flash::FlashAxContainer>* ax_;\r
155         safe_ptr<draw_frame> head_;\r
156 };\r
157 \r
158 struct flash_producer::implementation\r
159 {       \r
160         implementation(const std::wstring& filename) : filename_(filename), tail_(draw_frame::empty())\r
161         {       \r
162                 if(!boost::filesystem::exists(filename))\r
163                         BOOST_THROW_EXCEPTION(file_not_found() << boost::errinfo_file_name(narrow(filename)));\r
164                 \r
165                 executor_.begin_invoke([=]\r
166                 {\r
167                         ::OleInitialize(nullptr);\r
168                 });\r
169 \r
170                 frame_buffer_.set_capacity(8);\r
171                 while(frame_buffer_.try_push(draw_frame::empty())){}\r
172         }\r
173 \r
174         ~implementation()\r
175         {\r
176                 executor_.clear();\r
177                 executor_.invoke([=]\r
178                 {\r
179                         renderer_ = nullptr;\r
180                         ::OleUninitialize();\r
181                 });\r
182         }\r
183 \r
184         virtual safe_ptr<draw_frame> receive()\r
185         {               \r
186                 if(!frame_buffer_.try_pop(tail_))\r
187                         CASPAR_LOG(trace) << print() << " underflow";\r
188                 else\r
189                 {\r
190                         executor_.begin_invoke([=]\r
191                         {\r
192                                 if(!renderer_)\r
193                                         return;\r
194 \r
195                                 try\r
196                                 {\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
200                                 }\r
201                                 catch(...)\r
202                                 {\r
203                                         CASPAR_LOG_CURRENT_EXCEPTION();\r
204                                         renderer_ = nullptr;\r
205                                 }\r
206                         });     \r
207                 }                               \r
208                 return tail_;\r
209         }\r
210         \r
211         virtual void initialize(const safe_ptr<frame_processor_device>& frame_processor)\r
212         {\r
213                 frame_processor_ = frame_processor;\r
214                 executor_.start();\r
215         }\r
216         \r
217         void param(const std::wstring& param) \r
218         {       \r
219                 executor_.begin_invoke([=]\r
220                 {\r
221                         if(!renderer_)\r
222                                 renderer_.reset(new flash_renderer(frame_processor_, filename_));\r
223 \r
224                         try\r
225                         {\r
226                                 renderer_->param(param);\r
227                         }\r
228                         catch(...)\r
229                         {\r
230                                 CASPAR_LOG_CURRENT_EXCEPTION();\r
231                                 renderer_ = nullptr;\r
232                         }\r
233                 });\r
234         }\r
235 \r
236         safe_ptr<draw_frame> tail_;\r
237         tbb::concurrent_bounded_queue<safe_ptr<draw_frame>> frame_buffer_;\r
238         executor executor_;\r
239 \r
240         std::shared_ptr<flash_renderer> renderer_;\r
241         std::shared_ptr<frame_processor_device> frame_processor_;\r
242         \r
243         std::wstring print() const{ return L"flash[" + boost::filesystem::wpath(filename_).filename() + L"]"; } \r
244         std::wstring filename_;\r
245 };\r
246 \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
253 \r
254 std::wstring flash_producer::find_template(const std::wstring& template_name)\r
255 {\r
256         if(boost::filesystem::exists(template_name + L".ft")) \r
257                 return template_name + L".ft";\r
258         \r
259         if(boost::filesystem::exists(template_name + L".ct"))\r
260                 return template_name + L".ct";\r
261 \r
262         return L"";\r
263 }\r
264 \r
265 }}}