]> git.sesse.net Git - casparcg/blob - modules/flash/producer/flash_producer.cpp
2.0.0.2:
[casparcg] / modules / flash / producer / 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 : 4146)\r
25 #pragma warning (disable : 4244)\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 <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
36 \r
37 #include <common/env.h>\r
38 #include <common/concurrency/executor.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
43 \r
44 #include <boost/filesystem.hpp>\r
45 #include <boost/thread.hpp>\r
46 #include <boost/timer.hpp>\r
47 \r
48 #include <functional>\r
49 \r
50 namespace caspar {\r
51                 \r
52 class flash_renderer\r
53 {\r
54         struct co_init\r
55         {\r
56                 co_init(){CoInitialize(nullptr);}\r
57                 ~co_init(){CoUninitialize();}\r
58         } co_;\r
59         \r
60         const std::wstring filename_;\r
61         const core::video_format_desc format_desc_;\r
62 \r
63         std::shared_ptr<core::frame_factory> frame_factory_;\r
64         \r
65         BYTE* bmp_data_;        \r
66         std::shared_ptr<void> hdc_;\r
67         std::shared_ptr<void> bmp_;\r
68 \r
69         CComObject<caspar::flash::FlashAxContainer>* ax_;\r
70         safe_ptr<core::basic_frame> head_;\r
71         \r
72         safe_ptr<diagnostics::graph> graph_;\r
73         boost::timer frame_timer_;\r
74         boost::timer tick_timer_;\r
75 \r
76         high_prec_timer timer_;\r
77         \r
78 public:\r
79         flash_renderer(const safe_ptr<diagnostics::graph>& graph, const std::shared_ptr<core::frame_factory>& frame_factory, const std::wstring& filename) \r
80                 : graph_(graph)\r
81                 , filename_(filename)\r
82                 , format_desc_(frame_factory->get_video_format_desc())\r
83                 , frame_factory_(frame_factory)\r
84                 , bmp_data_(nullptr)\r
85                 , hdc_(CreateCompatibleDC(0), DeleteDC)\r
86                 , ax_(nullptr)\r
87                 , head_(core::basic_frame::empty())\r
88         {\r
89                 graph_->add_guide("frame-time", 0.5f);\r
90                 graph_->set_color("frame-time", diagnostics::color(1.0f, 0.0f, 0.0f));  \r
91                 graph_->add_guide("tick-time", 0.5);\r
92                 graph_->set_color("tick-time", diagnostics::color(0.1f, 0.7f, 0.8f));\r
93                 graph_->set_color("param", diagnostics::color(1.0f, 0.5f, 0.0f));       \r
94                 graph_->set_color("underflow", diagnostics::color(0.6f, 0.3f, 0.9f));                   \r
95                 \r
96                 if(FAILED(CComObject<caspar::flash::FlashAxContainer>::CreateInstance(&ax_)))\r
97                         BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Failed to create FlashAxContainer"));\r
98                 \r
99                 ax_->set_print([this]{return L"";});\r
100 \r
101                 if(FAILED(ax_->CreateAxControl()))\r
102                         BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Failed to Create FlashAxControl"));\r
103                 \r
104                 CComPtr<IShockwaveFlash> spFlash;\r
105                 if(FAILED(ax_->QueryControl(&spFlash)))\r
106                         BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Failed to Query FlashAxControl"));\r
107                                                                                                 \r
108                 if(FAILED(spFlash->put_Playing(true)) )\r
109                         BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Failed to start playing Flash"));\r
110 \r
111                 if(FAILED(spFlash->put_Movie(CComBSTR(filename.c_str()))))\r
112                         BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Failed to Load Template Host"));\r
113                                                                                 \r
114                 if(FAILED(spFlash->put_ScaleMode(2)))  //Exact fit. Scale without respect to the aspect ratio.\r
115                         BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Failed to Set Scale Mode"));\r
116                                                 \r
117                 ax_->SetFormat(format_desc_);\r
118                 \r
119                 BITMAPINFO info;\r
120                 memset(&info, 0, sizeof(BITMAPINFO));\r
121                 info.bmiHeader.biBitCount = 32;\r
122                 info.bmiHeader.biCompression = BI_RGB;\r
123                 info.bmiHeader.biHeight = -format_desc_.height;\r
124                 info.bmiHeader.biPlanes = 1;\r
125                 info.bmiHeader.biSize = sizeof(BITMAPINFO);\r
126                 info.bmiHeader.biWidth = format_desc_.width;\r
127 \r
128                 bmp_.reset(CreateDIBSection(static_cast<HDC>(hdc_.get()), &info, DIB_RGB_COLORS, reinterpret_cast<void**>(&bmp_data_), 0, 0), DeleteObject);\r
129                 SelectObject(static_cast<HDC>(hdc_.get()), bmp_.get()); \r
130                 CASPAR_LOG(info) << print() << L" Thread started.";\r
131         }\r
132 \r
133         ~flash_renderer()\r
134         {               \r
135                 if(ax_)\r
136                 {\r
137                         ax_->DestroyAxControl();\r
138                         ax_->Release();\r
139                 }\r
140                 CASPAR_LOG(info) << print() << L" Thread ended.";\r
141         }\r
142         \r
143         void param(const std::wstring& param)\r
144         {               \r
145                 if(!ax_->FlashCall(param))\r
146                         CASPAR_LOG(warning) << print() << " Flash function call failed. Param: " << param << ".";\r
147                 graph_->add_tag("param");\r
148 \r
149                 if(abs(ax_->GetFPS() - format_desc_.fps) > 0.001 && abs(ax_->GetFPS()/2.0 - format_desc_.fps) > 0.001)\r
150                         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
151         }\r
152         \r
153         safe_ptr<core::basic_frame> render_frame(bool has_underflow)\r
154         {\r
155                 float frame_time = 1.0f/ax_->GetFPS();\r
156 \r
157                 graph_->update_value("tick-time", static_cast<float>(tick_timer_.elapsed()/frame_time)*0.5f);\r
158                 tick_timer_.restart();\r
159 \r
160                 if(ax_->IsEmpty())\r
161                         return core::basic_frame::empty();              \r
162                 \r
163                 if(!has_underflow)                      \r
164                         timer_.tick(frame_time);        \r
165                 else\r
166                         graph_->add_tag("underflow");\r
167                         \r
168                 frame_timer_.restart();\r
169 \r
170                 ax_->Tick();\r
171                 if(ax_->InvalidRect())\r
172                 {                       \r
173                         fast_memclr(bmp_data_,  format_desc_.size);\r
174                         ax_->DrawControl(static_cast<HDC>(hdc_.get()));\r
175                 \r
176                         auto frame = frame_factory_->create_frame(this);\r
177                         fast_memcpy(frame->image_data().begin(), bmp_data_, format_desc_.size);\r
178                         head_ = frame;\r
179                 }               \r
180                                 \r
181                 graph_->update_value("frame-time", static_cast<float>(frame_timer_.elapsed()/frame_time)*0.5f);\r
182                 return head_;\r
183         }\r
184 \r
185         double fps() const\r
186         {\r
187                 return ax_->GetFPS();   \r
188         }\r
189         \r
190         std::wstring print()\r
191         {\r
192                 return L"flash[" + boost::filesystem::wpath(filename_).filename() + L"]";               \r
193         }\r
194 };\r
195 \r
196 struct flash_producer : public core::frame_producer\r
197 {       \r
198         const std::wstring filename_;   \r
199         tbb::atomic<int> fps_;\r
200         bool underfow_;\r
201 \r
202         std::shared_ptr<diagnostics::graph> graph_;\r
203 \r
204         safe_ptr<core::basic_frame> tail_;\r
205         tbb::concurrent_bounded_queue<safe_ptr<core::basic_frame>> frame_buffer_;\r
206 \r
207         std::shared_ptr<flash_renderer> renderer_;\r
208         safe_ptr<core::frame_factory> frame_factory_;\r
209         const core::video_format_desc format_desc_;\r
210                         \r
211         executor executor_;\r
212                 \r
213 public:\r
214         flash_producer(const safe_ptr<core::frame_factory>& frame_factory, const std::wstring& filename) \r
215                 : filename_(filename)           \r
216                 , tail_(core::basic_frame::empty())             \r
217                 , frame_factory_(frame_factory)\r
218                 , format_desc_(frame_factory->get_video_format_desc())\r
219                 , executor_(L"flash_producer", true)\r
220         {       \r
221                 if(!boost::filesystem::exists(filename))\r
222                         BOOST_THROW_EXCEPTION(file_not_found() << boost::errinfo_file_name(narrow(filename)));  \r
223                  \r
224                 frame_buffer_.set_capacity(2);\r
225                 graph_ = diagnostics::create_graph([this]{return print();});\r
226                 graph_->set_color("output-buffer", diagnostics::color(0.0f, 1.0f, 0.0f));\r
227                 \r
228                 executor_.begin_invoke([=]\r
229                 {\r
230                         init_renderer();\r
231                 });\r
232                                 \r
233                 fps_ = 0;\r
234         }\r
235 \r
236         ~flash_producer()\r
237         {\r
238                 executor_.clear();\r
239                 frame_buffer_.clear();\r
240                 executor_.invoke([=]\r
241                 {\r
242                         renderer_ = nullptr;\r
243                 });             \r
244         }\r
245 \r
246         // frame_producer\r
247                 \r
248         virtual safe_ptr<core::basic_frame> receive()\r
249         {                               \r
250                 graph_->set_value("output-buffer", static_cast<float>(frame_buffer_.size())/static_cast<float>(frame_buffer_.capacity()));\r
251                 if(!frame_buffer_.try_pop(tail_))\r
252                         return tail_;\r
253                 \r
254                 return tail_;\r
255         }\r
256         \r
257         virtual void param(const std::wstring& param) \r
258         {       \r
259                 executor_.begin_invoke([=]\r
260                 {\r
261                         if(!renderer_)\r
262                                 init_renderer();\r
263 \r
264                         try\r
265                         {\r
266                                 renderer_->param(param);        \r
267                         }\r
268                         catch(...)\r
269                         {\r
270                                 CASPAR_LOG_CURRENT_EXCEPTION();\r
271                                 renderer_ = nullptr;\r
272                         }\r
273                 });\r
274         }\r
275                 \r
276         virtual std::wstring print() const\r
277         { \r
278                 return L"flash[" + boost::filesystem::wpath(filename_).filename() + L", " + \r
279                                         boost::lexical_cast<std::wstring>(fps_) + L"]";         \r
280         }       \r
281 \r
282         // flash_producer\r
283 \r
284         void init_renderer()\r
285         {\r
286                 renderer_.reset(new flash_renderer(safe_ptr<diagnostics::graph>(graph_), frame_factory_, filename_));\r
287                 while(frame_buffer_.try_push(core::basic_frame::empty())){}             \r
288                 executor_.begin_invoke([=]\r
289                 {\r
290                         render(renderer_);\r
291                 });             \r
292         }\r
293 \r
294         void render(const std::shared_ptr<flash_renderer>& renderer)\r
295         {\r
296                 if(renderer_ != renderer) // Make sure the recursive calls are only for a specific instance.\r
297                 {\r
298                         frame_buffer_.push(core::basic_frame::empty());\r
299                         return;\r
300                 }\r
301 \r
302                 try\r
303                 {               \r
304                         auto frame = core::basic_frame::empty();\r
305                         if(abs(renderer_->fps()/2.0 - format_desc_.fps) < 0.1) //flash 50, format 50i\r
306                         {\r
307                                 auto frame1 = renderer_->render_frame(frame_buffer_.size() < frame_buffer_.capacity());\r
308                                 auto frame2 = renderer_->render_frame(frame_buffer_.size() < frame_buffer_.capacity());\r
309                                 frame_buffer_.push(core::basic_frame::interlace(frame1, frame2, format_desc_.mode));\r
310                                 frame = frame2;\r
311                         }\r
312                         else if(abs(renderer_->fps()- format_desc_.fps/2.0 ) < 0.1) //flash 25, format 50p\r
313                         {\r
314                                 frame = renderer_->render_frame(frame_buffer_.size() < frame_buffer_.capacity());\r
315                                 frame_buffer_.push(frame);\r
316                                 frame_buffer_.push(frame);\r
317                         }\r
318                         else //if(abs(renderer_->fps() - format_desc_.fps) < 0.1) // flash 25, format 50i or flash 50, format 50p\r
319                         {\r
320                                 frame = renderer_->render_frame(frame_buffer_.size() < frame_buffer_.capacity());\r
321                                 frame_buffer_.push(frame);\r
322                         }\r
323 \r
324                         graph_->set_value("output-buffer", static_cast<float>(frame_buffer_.size())/static_cast<float>(frame_buffer_.capacity()));      \r
325                         fps_.fetch_and_store(static_cast<int>(renderer_->fps()*100.0));\r
326 \r
327                         executor_.begin_invoke([=]\r
328                         {\r
329                                 render(renderer);\r
330                         });                     \r
331                 }\r
332                 catch(...)\r
333                 {\r
334                         CASPAR_LOG_CURRENT_EXCEPTION();\r
335                         renderer_ = nullptr;\r
336                 }\r
337         }\r
338 };\r
339 \r
340 safe_ptr<core::frame_producer> create_flash_producer(const safe_ptr<core::frame_factory>& frame_factory, const std::vector<std::wstring>& params)\r
341 {\r
342         std::wstring filename = env::template_folder() + L"\\" + params[0];\r
343         \r
344         return make_safe<flash_producer>(frame_factory, filename);\r
345 }\r
346 \r
347 std::wstring find_flash_template(const std::wstring& template_name)\r
348 {\r
349         if(boost::filesystem::exists(template_name + L".ft")) \r
350                 return template_name + L".ft";\r
351         \r
352         if(boost::filesystem::exists(template_name + L".ct"))\r
353                 return template_name + L".ct";\r
354 \r
355         return L"";\r
356 }\r
357 \r
358 }