]> git.sesse.net Git - casparcg/blob - modules/flash/producer/flash_producer.cpp
concrt-exp: Merged in changes from 2.0.2 branch.
[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/mixer/write_frame.h>\r
36 \r
37 #include <common/concurrency/throw_away.h>\r
38 #include <common/diagnostics/graph.h>\r
39 #include <common/env.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 #include <boost/algorithm/string.hpp>\r
48 \r
49 #include <functional>\r
50 \r
51 #include <agents.h>\r
52 #include <agents_extras.h>\r
53 #include <concrt_extras.h>\r
54 \r
55 using namespace Concurrency;\r
56 \r
57 namespace caspar { namespace flash {\r
58                 \r
59 class bitmap\r
60 {\r
61 public:\r
62         bitmap(size_t width, size_t height)\r
63                 : bmp_data_(nullptr)\r
64                 , hdc_(CreateCompatibleDC(0), DeleteDC)\r
65         {       \r
66                 BITMAPINFO info;\r
67                 memset(&info, 0, sizeof(BITMAPINFO));\r
68                 info.bmiHeader.biBitCount = 32;\r
69                 info.bmiHeader.biCompression = BI_RGB;\r
70                 info.bmiHeader.biHeight = -height;\r
71                 info.bmiHeader.biPlanes = 1;\r
72                 info.bmiHeader.biSize = sizeof(BITMAPINFO);\r
73                 info.bmiHeader.biWidth = width;\r
74 \r
75                 bmp_.reset(CreateDIBSection(static_cast<HDC>(hdc_.get()), &info, DIB_RGB_COLORS, reinterpret_cast<void**>(&bmp_data_), 0, 0), DeleteObject);\r
76                 SelectObject(static_cast<HDC>(hdc_.get()), bmp_.get()); \r
77 \r
78                 if(!bmp_data_)\r
79                         BOOST_THROW_EXCEPTION(std::bad_alloc());\r
80         }\r
81 \r
82         operator HDC() {return static_cast<HDC>(hdc_.get());}\r
83 \r
84         BYTE* data() { return bmp_data_;}\r
85         const BYTE* data() const { return bmp_data_;}\r
86 \r
87 private:\r
88         BYTE* bmp_data_;        \r
89         std::shared_ptr<void> hdc_;\r
90         std::shared_ptr<void> bmp_;\r
91 };\r
92 \r
93 struct template_host\r
94 {\r
95         std::string  field_mode;\r
96         std::string  filename;\r
97         size_t           width;\r
98         size_t           height;\r
99 };\r
100 \r
101 template_host get_template_host(const core::video_format_desc& desc)\r
102 {\r
103         try\r
104         {\r
105                 std::vector<template_host> template_hosts;\r
106                 BOOST_FOREACH(auto& xml_mapping, env::properties().get_child("configuration.producers.template-hosts"))\r
107                 {\r
108                         try\r
109                         {\r
110                                 template_host template_host;\r
111                                 template_host.field_mode                = xml_mapping.second.get("video-mode", narrow(desc.name));\r
112                                 template_host.filename                  = xml_mapping.second.get("filename", "cg.fth");\r
113                                 template_host.width                             = xml_mapping.second.get("width", desc.width);\r
114                                 template_host.height                    = xml_mapping.second.get("height", desc.height);\r
115                                 template_hosts.push_back(template_host);\r
116                         }\r
117                         catch(...){}\r
118                 }\r
119 \r
120                 auto template_host_it = boost::find_if(template_hosts, [&](template_host template_host){return template_host.field_mode == narrow(desc.name);});\r
121                 if(template_host_it == template_hosts.end())\r
122                         template_host_it = boost::find_if(template_hosts, [&](template_host template_host){return template_host.field_mode == "";});\r
123 \r
124                 if(template_host_it != template_hosts.end())\r
125                         return *template_host_it;       \r
126         }\r
127         catch(...)\r
128         {\r
129         }\r
130                 \r
131         template_host template_host;\r
132         template_host.filename = "cg.fth";\r
133 \r
134         for(auto it = boost::filesystem2::wdirectory_iterator(env::template_folder()); it != boost::filesystem2::wdirectory_iterator(); ++it)\r
135         {\r
136                 if(boost::iequals(it->path().extension(), L"." + desc.name))\r
137                 {\r
138                         template_host.filename = narrow(it->filename());\r
139                         break;\r
140                 }\r
141         }\r
142 \r
143         template_host.width = desc.width;\r
144         template_host.height = desc.height;\r
145         return template_host;\r
146 }\r
147 \r
148 class flash_player\r
149 {\r
150         const std::wstring      filename_;\r
151         const size_t            width_;\r
152         const size_t            height_;\r
153         CComObject<caspar::flash::FlashAxContainer>*    ax_;\r
154 public:\r
155         flash_player(const std::wstring& filename, size_t width, size_t height)\r
156                 : filename_(filename)\r
157                 , width_(width)\r
158                 , height_(height)\r
159         {               \r
160                 if(FAILED(CComObject<caspar::flash::FlashAxContainer>::CreateInstance(&ax_)))\r
161                         BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Failed to create FlashAxContainer"));\r
162                 \r
163                 ax_->set_print([this]{return L"flash_renderer";});\r
164 \r
165                 if(FAILED(ax_->CreateAxControl()))\r
166                         BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Failed to Create FlashAxControl"));\r
167                 \r
168                 CComPtr<IShockwaveFlash> spFlash;\r
169                 if(FAILED(ax_->QueryControl(&spFlash)))\r
170                         BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Failed to Query FlashAxControl"));\r
171                                                                                                 \r
172                 if(FAILED(spFlash->put_Playing(true)) )\r
173                         BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Failed to start playing Flash"));\r
174 \r
175                 if(FAILED(spFlash->put_Movie(CComBSTR(filename.c_str()))))\r
176                         BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Failed to Load Template Host"));\r
177                                                                                 \r
178                 if(FAILED(spFlash->put_ScaleMode(2)))  //Exact fit. Scale without respect to the aspect ratio.\r
179                         BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Failed to Set Scale Mode"));\r
180                                                 \r
181                 ax_->SetSize(width, height);    \r
182 \r
183                 CASPAR_LOG(info) << "Initialized" << print();\r
184         }\r
185 \r
186         ~flash_player()\r
187         {\r
188                 if(ax_)\r
189                 {\r
190                         ax_->DestroyAxControl();\r
191                         ax_->Release();\r
192                 }\r
193                 CASPAR_LOG(info) << "Uninitialized " << print();\r
194         }\r
195 \r
196         bool is_empty() const\r
197         {\r
198                 return ax_->IsEmpty();\r
199         }\r
200 \r
201         void tick()\r
202         {\r
203                 ax_->Tick();\r
204         }\r
205 \r
206         bool invalid_rect() const\r
207         {\r
208                 return ax_->InvalidRect();\r
209         }\r
210 \r
211         bool draw_control(HDC targetDC)\r
212         {\r
213                 return ax_->DrawControl(targetDC);\r
214         }\r
215 \r
216         bool flash_call(const std::wstring& param)\r
217         {\r
218                 return ax_->FlashCall(param);\r
219         }\r
220 \r
221         double fps() const\r
222         {\r
223                 return ax_->GetFPS();\r
224         }\r
225 \r
226         std::wstring print()\r
227         {\r
228                 return L"flash-player[" + boost::filesystem::wpath(filename_).filename() + L"|" + boost::lexical_cast<std::wstring>(width_) + L"|" + boost::lexical_cast<std::wstring>(height_) + L"]";         \r
229         }\r
230 };\r
231 \r
232 class flash_renderer\r
233 {       \r
234         const std::wstring                                                              filename_;\r
235 \r
236         const safe_ptr<core::frame_factory>                             frame_factory_;\r
237         safe_ptr<diagnostics::graph>                                    graph_;\r
238 \r
239         high_prec_timer                                                                 timer_;\r
240         safe_ptr<core::basic_frame>                                             head_;\r
241         bitmap                                                                                  bmp_;\r
242 \r
243         const size_t                                                                    width_;\r
244         const size_t                                                                    height_;\r
245                         \r
246         boost::timer                                                                    frame_timer_;\r
247         boost::timer                                                                    tick_timer_;\r
248 \r
249         std::unique_ptr<flash_player>                                   player_;\r
250         \r
251 public:\r
252         flash_renderer(const safe_ptr<diagnostics::graph>& graph, const safe_ptr<core::frame_factory>& frame_factory, const std::wstring& filename, int width, int height) \r
253                 : filename_(filename)\r
254                 , frame_factory_(frame_factory)\r
255                 , graph_(graph)\r
256                 , head_(core::basic_frame::empty())\r
257                 , bmp_(width, height)\r
258                 , width_(width)\r
259                 , height_(height)\r
260         {               \r
261                 graph_->add_guide("frame-time", 0.5f);\r
262                 graph_->set_color("frame-time", diagnostics::color(0.1f, 1.0f, 0.1f));\r
263                 graph_->add_guide("tick-time", 0.5);\r
264                 graph_->set_color("tick-time", diagnostics::color(0.0f, 0.6f, 0.9f));\r
265                 graph_->set_color("param", diagnostics::color(1.0f, 0.5f, 0.0f));               \r
266         }\r
267                 \r
268         bool param(const std::wstring& param)\r
269         {               \r
270                 if(!player_)\r
271                         player_.reset(new flash_player(filename_, width_, height_));\r
272 \r
273                 bool success = player_->flash_call(param); \r
274                 if(!success)\r
275                         CASPAR_LOG(warning) << print() << L" Flash call failed:" << param;//BOOST_THROW_EXCEPTION(invalid_operation() << msg_info("Flash function call failed.") << arg_name_info("param") << arg_value_info(narrow(param)));\r
276                 else\r
277                         graph_->add_tag("param");\r
278                 return success;\r
279         }\r
280         \r
281         safe_ptr<core::basic_frame> operator()()\r
282         {\r
283                 if(player_ && player_->is_empty())\r
284                         player_.reset();\r
285 \r
286                 if(!player_)\r
287                 {\r
288                         graph_->update_value("tick-time", 0.0);\r
289                         return core::basic_frame::empty();      \r
290                 }\r
291 \r
292                 const float frame_time = 1.0f/player_->fps();\r
293 \r
294                 graph_->update_value("tick-time", static_cast<float>(tick_timer_.elapsed()/frame_time)*0.5f);\r
295                 tick_timer_.restart();\r
296                 \r
297                 Concurrency::scoped_oversubcription_token oversubscribe;\r
298                 timer_.tick(frame_time); // This will block the thread.\r
299                 //Concurrency::wait(std::max<int>(0, frame_time-3));\r
300                         \r
301                 frame_timer_.restart();\r
302 \r
303                 player_->tick();\r
304                 if(player_->invalid_rect())\r
305                 {                       \r
306                         fast_memclr(bmp_.data(), width_*height_*4);\r
307                         player_->draw_control(bmp_);\r
308                 \r
309                         auto frame = frame_factory_->create_frame(this, width_, height_);\r
310                         fast_memcpy(frame->image_data().begin(), bmp_.data(), width_*height_*4);\r
311                         frame->commit();\r
312                         head_ = frame;\r
313                 }               \r
314                                 \r
315                 graph_->update_value("frame-time", static_cast<float>(frame_timer_.elapsed()/frame_time)*0.5f);\r
316                 return head_;\r
317         }\r
318 \r
319         double fps() const\r
320         {\r
321                 return player_ ? player_->fps() : 0.0;  \r
322         }\r
323         \r
324         std::wstring print()\r
325         {\r
326                 return L"flash[" + boost::filesystem::wpath(filename_).filename() + L"]";               \r
327         }\r
328 };\r
329 \r
330 struct flash_producer : public Concurrency::agent, public core::frame_producer\r
331 {       \r
332         unbounded_buffer<std::wstring>                                                          params_;\r
333         bounded_buffer<safe_ptr<core::basic_frame>>                                     frames_;\r
334 \r
335         tbb::atomic<bool>                                                                                       is_running_;\r
336                 \r
337         const safe_ptr<core::frame_factory>                                                     frame_factory_;\r
338         const std::wstring                                                                                      filename_;      \r
339         tbb::atomic<int>                                                                                        fps_;\r
340         const int                                                                                                       width_;\r
341         const int                                                                                                       height_;\r
342                 \r
343         mutable overwrite_buffer<safe_ptr<core::basic_frame>>           last_frame_;\r
344 \r
345         safe_ptr<diagnostics::graph>                                                            graph_;\r
346                                 \r
347 public:\r
348         flash_producer(const safe_ptr<core::frame_factory>& frame_factory, const std::wstring& filename, size_t width, size_t height) \r
349                 : frames_(frame_factory->get_video_format_desc().fps > 30.0 ? 2 : 1)\r
350                 , frame_factory_(frame_factory)\r
351                 , filename_(filename)           \r
352                 , width_(width > 0 ? width : frame_factory->get_video_format_desc().width)\r
353                 , height_(height > 0 ? height : frame_factory->get_video_format_desc().height)\r
354         {       \r
355                 if(!boost::filesystem::exists(filename))\r
356                         BOOST_THROW_EXCEPTION(file_not_found() << boost::errinfo_file_name(narrow(filename)));  \r
357                 \r
358                 graph_->set_color("underflow", diagnostics::color(0.6f, 0.3f, 0.9f));\r
359                 graph_->set_text(print());\r
360                 diagnostics::register_graph(graph_);\r
361 \r
362                 Concurrency::send(last_frame_, core::basic_frame::empty());\r
363                 \r
364                 fps_            = 0;\r
365                 is_running_ = true;\r
366 \r
367                 start();\r
368         }\r
369 \r
370         ~flash_producer()\r
371         {\r
372                 is_running_ = false;\r
373                 link_throw_away(frames_);\r
374                 agent::wait(this);\r
375         }\r
376         \r
377         virtual void run()\r
378         {               \r
379                 try\r
380                 {\r
381                         struct co_init\r
382                         {\r
383                                 co_init()  {CoInitialize(NULL);}\r
384                                 ~co_init() {CoUninitialize();}\r
385                         } init;\r
386 \r
387                         flash_renderer renderer(graph_, frame_factory_, filename_, width_, height_);\r
388 \r
389                         is_running_ = true;\r
390                         while(is_running_)\r
391                         {\r
392                                 std::wstring param;\r
393                                 while(is_running_ && Concurrency::try_receive(params_, param))\r
394                                         renderer.param(param);\r
395                         \r
396                                 const auto& format_desc = frame_factory_->get_video_format_desc();\r
397 \r
398                                 if(abs(renderer.fps()/2.0 - format_desc.fps) < 2.0) // flash == 2 * format -> interlace\r
399                                 {\r
400                                         auto frame1 = renderer();\r
401                                         auto frame2 = renderer();\r
402                                         send(last_frame_, frame2);\r
403                                         send(frames_, core::basic_frame::interlace(frame1, frame2, format_desc.field_mode));\r
404                                 }\r
405                                 else if(abs(renderer.fps()- format_desc.fps/2.0) < 2.0) // format == 2 * flash -> duplicate\r
406                                 {\r
407                                         auto frame = renderer();\r
408                                         send(last_frame_, frame);\r
409                                         send(frames_, frame);\r
410                                         send(frames_, frame);\r
411                                 }\r
412                                 else //if(abs(renderer_->fps() - format_desc_.fps) < 0.1) // format == flash -> simple\r
413                                 {\r
414                                         auto frame = renderer();\r
415                                         send(last_frame_, frame);\r
416                                         send(frames_, frame);\r
417                                 }\r
418 \r
419                                 fps_ = static_cast<int>(renderer.fps()*100.0);\r
420                                 graph_->set_text(narrow(print()));\r
421                         }\r
422                 }\r
423                 catch(...)\r
424                 {\r
425                         CASPAR_LOG_CURRENT_EXCEPTION();\r
426                 }\r
427                 \r
428                 is_running_ = false;\r
429                 done();\r
430         }\r
431         \r
432         // frame_producer\r
433                 \r
434         virtual safe_ptr<core::basic_frame> receive(int)\r
435         {                                               \r
436                 auto frame = core::basic_frame::late();\r
437 \r
438                 try\r
439                 {\r
440                         frame = Concurrency::receive(frames_, 5);\r
441                 }\r
442                 catch(operation_timed_out&)\r
443                 {                       \r
444                         graph_->add_tag("underflow");\r
445                 }\r
446 \r
447                 return frame;\r
448         }\r
449 \r
450         virtual safe_ptr<core::basic_frame> last_frame() const\r
451         {\r
452                 return last_frame_.value();\r
453         }               \r
454         \r
455         virtual bool param(const std::wstring& param) \r
456         {       \r
457                 if(!is_running_.fetch_and_store(true))\r
458                 {\r
459                         agent::wait(this);\r
460                         start();\r
461                 }\r
462                 asend(params_, param);\r
463                 return true;\r
464         }\r
465                 \r
466         virtual std::wstring print() const\r
467         { \r
468                 return L"flash[" + boost::filesystem::wpath(filename_).filename() + L"|" + boost::lexical_cast<std::wstring>(fps_) + L"]";              \r
469         }       \r
470 };\r
471 \r
472 safe_ptr<core::frame_producer> create_flash_producer(const safe_ptr<core::frame_factory> frame_factory, const std::vector<std::wstring>& params)\r
473 {               \r
474         static const std::vector<std::wstring> extensions = boost::assign::list_of\r
475                 (L"swf");\r
476 \r
477         std::wstring filename = env::media_folder() + L"\\" + params[0];\r
478         \r
479         auto ext = boost::find_if(extensions, [&](const std::wstring& ex)\r
480         {                                       \r
481                 return boost::filesystem::is_regular_file(boost::filesystem::wpath(filename + L"." + ex));\r
482         });\r
483 \r
484         if(ext == extensions.end())\r
485                 return core::frame_producer::empty();\r
486                 \r
487         auto path = filename + L"." + *ext;\r
488         return make_safe<flash_producer>(frame_factory, path, 0, 0);\r
489 }\r
490 \r
491 safe_ptr<core::frame_producer> create_producer(const safe_ptr<core::frame_factory>& frame_factory, const std::vector<std::wstring>& params)\r
492 {\r
493         auto template_host = get_template_host(frame_factory->get_video_format_desc());\r
494         \r
495         return make_safe<flash_producer>(frame_factory, env::template_folder() + L"\\" + widen(template_host.filename), template_host.width, template_host.height);\r
496 }\r
497 \r
498 std::wstring find_template(const std::wstring& template_name)\r
499 {\r
500         if(boost::filesystem::exists(template_name + L".ft")) \r
501                 return template_name + L".ft";\r
502         \r
503         if(boost::filesystem::exists(template_name + L".ct"))\r
504                 return template_name + L".ct";\r
505 \r
506         return L"";\r
507 }\r
508 \r
509 }}