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