]> git.sesse.net Git - casparcg/blob - core/producer/flash/flash_producer.cpp
2.0.0.2: Moved ATL init to shell.
[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 <core/video_format.h>\r
32 \r
33 #include <mixer/frame/draw_frame.h>\r
34 #include <mixer/frame_mixer_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 class flash_renderer\r
44 {\r
45         struct co_init\r
46         {\r
47                 co_init(){CoInitialize(nullptr);}\r
48                 ~co_init(){CoUninitialize();}\r
49         } co_;\r
50 \r
51         const std::wstring filename_;\r
52         const video_format_desc format_desc_;\r
53 \r
54         std::shared_ptr<frame_factory> frame_factory_;\r
55         \r
56         BYTE* bmp_data_;        \r
57         std::shared_ptr<void> hdc_;\r
58         std::shared_ptr<void> bmp_;\r
59 \r
60         CComObject<caspar::flash::FlashAxContainer>* ax_;\r
61         safe_ptr<draw_frame> head_;\r
62 \r
63 public:\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
70                 , ax_(nullptr)\r
71                 , head_(draw_frame::empty())\r
72         {\r
73                 CASPAR_LOG(info) << print() << L" Started";\r
74                 \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
77                 \r
78                 if(FAILED(ax_->CreateAxControl()))\r
79                         BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(bprint() + "Failed to Create FlashAxControl"));\r
80                 \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
84                                                                                                 \r
85                 if(FAILED(spFlash->put_Playing(true)) )\r
86                         BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(bprint() + "Failed to start playing Flash"));\r
87 \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
90                                                                                 \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
93                                                                                                                 \r
94                 ax_->SetFormat(format_desc_);\r
95                 \r
96                 BITMAPINFO info;\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
104 \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
107         }\r
108 \r
109         ~flash_renderer()\r
110         {               \r
111                 if(ax_)\r
112                 {\r
113                         ax_->DestroyAxControl();\r
114                         ax_->Release();\r
115                 }\r
116                 CASPAR_LOG(info) << print() << L" Ended";\r
117         }\r
118         \r
119         void param(const std::wstring& param)\r
120         {               \r
121                 if(!ax_->FlashCall(param))\r
122                         CASPAR_LOG(warning) << "Flash Function Call Failed. Param: " << param;\r
123         }\r
124         \r
125         safe_ptr<draw_frame> render_frame()\r
126         {\r
127                 if(ax_->IsEmpty())\r
128                         return draw_frame::empty();\r
129 \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
133                 return frame;\r
134         }\r
135                 \r
136         timer timer_;\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
139 \r
140 private:\r
141 \r
142         safe_ptr<draw_frame> render_simple_frame()\r
143         {\r
144                 timer_.tick(1.0/ax_->GetFPS()); // Tick doesnt work on nested timelines, force an actual sync\r
145 \r
146                 ax_->Tick();\r
147 \r
148                 if(ax_->InvalidRect())\r
149                 {                       \r
150                         std::fill_n(bmp_data_, format_desc_.size, 0);\r
151                         ax_->DrawControl(static_cast<HDC>(hdc_.get()));\r
152                 \r
153                         auto frame = frame_factory_->create_frame();\r
154                         std::copy_n(bmp_data_, format_desc_.size, frame->image_data().begin());\r
155                         head_ = frame;\r
156                 }               \r
157 \r
158                 return head_;\r
159         }\r
160 };\r
161 \r
162 struct flash_producer::implementation\r
163 {       \r
164         safe_ptr<draw_frame> tail_;\r
165         tbb::concurrent_bounded_queue<safe_ptr<draw_frame>> frame_buffer_;\r
166 \r
167         std::shared_ptr<flash_renderer> renderer_;\r
168         std::shared_ptr<frame_factory> frame_factory_;\r
169         \r
170         std::wstring print() const{ return L"flash[" + boost::filesystem::wpath(filename_).filename() + L"]"; } \r
171         std::wstring filename_;\r
172 \r
173         executor executor_;\r
174 public:\r
175         implementation(const std::wstring& filename) \r
176                 : filename_(filename)\r
177                 , tail_(draw_frame::empty())\r
178         {       \r
179                 if(!boost::filesystem::exists(filename))\r
180                         BOOST_THROW_EXCEPTION(file_not_found() << boost::errinfo_file_name(narrow(filename)));          \r
181         }\r
182 \r
183         ~implementation()\r
184         {\r
185                 executor_.clear();\r
186                 executor_.invoke([=]\r
187                 {\r
188                         renderer_ = nullptr;\r
189                 });\r
190         }\r
191         \r
192         virtual void initialize(const safe_ptr<frame_factory>& frame_factory)\r
193         {\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
196                 executor_.start();\r
197         }\r
198 \r
199         virtual safe_ptr<draw_frame> receive()\r
200         {               \r
201                 if(!frame_buffer_.try_pop(tail_))\r
202                         CASPAR_LOG(trace) << print() << " underflow";\r
203                 else\r
204                 {\r
205                         executor_.begin_invoke([=]\r
206                         {\r
207                                 if(!renderer_)\r
208                                         return;\r
209 \r
210                                 try\r
211                                 {\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
215                                 }\r
216                                 catch(...)\r
217                                 {\r
218                                         CASPAR_LOG_CURRENT_EXCEPTION();\r
219                                         renderer_ = nullptr;\r
220                                 }\r
221                         });     \r
222                 }\r
223 \r
224                 return tail_;\r
225         }\r
226         \r
227         void param(const std::wstring& param) \r
228         {       \r
229                 executor_.begin_invoke([=]\r
230                 {\r
231                         if(!renderer_)\r
232                         {\r
233                                 renderer_.reset(new flash_renderer(frame_factory_, filename_));\r
234                                 while(frame_buffer_.try_push(draw_frame::empty())){}\r
235                         }\r
236 \r
237                         try\r
238                         {\r
239                                 renderer_->param(param);\r
240                         }\r
241                         catch(...)\r
242                         {\r
243                                 CASPAR_LOG_CURRENT_EXCEPTION();\r
244                                 renderer_ = nullptr;\r
245                         }\r
246                 });\r
247         }\r
248 };\r
249 \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
256 \r
257 std::wstring flash_producer::find_template(const std::wstring& template_name)\r
258 {\r
259         if(boost::filesystem::exists(template_name + L".ft")) \r
260                 return template_name + L".ft";\r
261         \r
262         if(boost::filesystem::exists(template_name + L".ct"))\r
263                 return template_name + L".ct";\r
264 \r
265         return L"";\r
266 }\r
267 \r
268 }}}