]> git.sesse.net Git - casparcg/blob - modules/flash/producer/flash_producer.cpp
Refactored to use range based for instead of BOOST_FOREACH
[casparcg] / modules / flash / producer / flash_producer.cpp
1 /*
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
3 *
4 * This file is part of CasparCG (www.casparcg.com).
5 *
6 * CasparCG is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * CasparCG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * Author: Robert Nagy, ronag89@gmail.com
20 */
21
22 #include "../stdafx.h"
23
24 #if defined(_MSC_VER)
25 #pragma warning (disable : 4146)
26 #pragma warning (disable : 4244)
27 #endif
28
29 #include "flash_producer.h"
30 #include "FlashAxContainer.h"
31
32 #include "../util/swf.h"
33
34 #include <core/video_format.h>
35
36 #include <core/frame/frame.h>
37 #include <core/frame/draw_frame.h>
38 #include <core/frame/frame_factory.h>
39 #include <core/frame/pixel_format.h>
40 #include <core/monitor/monitor.h>
41
42 #include <common/env.h>
43 #include <common/executor.h>
44 #include <common/lock.h>
45 #include <common/diagnostics/graph.h>
46 #include <common/prec_timer.h>
47 #include <common/array.h>
48
49 #include <boost/filesystem.hpp>
50 #include <boost/property_tree/ptree.hpp>
51 #include <boost/thread.hpp>
52 #include <boost/timer.hpp>
53 #include <boost/algorithm/string.hpp>
54
55 #include <tbb/spin_mutex.h>
56
57 #include <asmlib.h>
58
59 #include <functional>
60
61 namespace caspar { namespace flash {
62                 
63 class bitmap
64 {
65 public:
66         bitmap(int width, int height)
67                 : bmp_data_(nullptr)
68                 , hdc_(CreateCompatibleDC(0), DeleteDC)
69         {       
70                 BITMAPINFO info;
71                 memset(&info, 0, sizeof(BITMAPINFO));
72                 info.bmiHeader.biBitCount = 32;
73                 info.bmiHeader.biCompression = BI_RGB;
74                 info.bmiHeader.biHeight = static_cast<LONG>(-height);
75                 info.bmiHeader.biPlanes = 1;
76                 info.bmiHeader.biSize = sizeof(BITMAPINFO);
77                 info.bmiHeader.biWidth = static_cast<LONG>(width);
78
79                 bmp_.reset(CreateDIBSection(static_cast<HDC>(hdc_.get()), &info, DIB_RGB_COLORS, reinterpret_cast<void**>(&bmp_data_), 0, 0), DeleteObject);
80                 SelectObject(static_cast<HDC>(hdc_.get()), bmp_.get()); 
81
82                 if(!bmp_data_)
83                         CASPAR_THROW_EXCEPTION(bad_alloc());
84         }
85
86         operator HDC() {return static_cast<HDC>(hdc_.get());}
87
88         BYTE* data() { return bmp_data_;}
89         const BYTE* data() const { return bmp_data_;}
90
91 private:
92         BYTE* bmp_data_;        
93         std::shared_ptr<void> hdc_;
94         std::shared_ptr<void> bmp_;
95 };
96
97 struct template_host
98 {
99         std::wstring  video_mode;
100         std::wstring  filename;
101         int                       width;
102         int                       height;
103 };
104
105 template_host get_template_host(const core::video_format_desc& desc)
106 {
107         try
108         {
109                 std::vector<template_host> template_hosts;
110                 for (auto& xml_mapping : env::properties().get_child(L"configuration.template-hosts"))
111                 {
112                         try
113                         {
114                                 template_host template_host;
115                                 template_host.video_mode                = xml_mapping.second.get(L"video-mode", L"");
116                                 template_host.filename                  = xml_mapping.second.get(L"filename",   L"cg.fth");
117                                 template_host.width                             = xml_mapping.second.get(L"width",              desc.width);
118                                 template_host.height                    = xml_mapping.second.get(L"height",             desc.height);
119                                 template_hosts.push_back(template_host);
120                         }
121                         catch(...){}
122                 }
123
124                 auto template_host_it = boost::find_if(template_hosts, [&](template_host template_host){return template_host.video_mode == desc.name;});
125                 if(template_host_it == template_hosts.end())
126                         template_host_it = boost::find_if(template_hosts, [&](template_host template_host){return template_host.video_mode == L"";});
127
128                 if(template_host_it != template_hosts.end())
129                         return *template_host_it;
130         }
131         catch(...){}
132                 
133         template_host template_host;
134         template_host.filename = L"cg.fth";
135
136         for(auto it = boost::filesystem::directory_iterator(env::template_folder()); it != boost::filesystem::directory_iterator(); ++it)
137         {
138                 if(boost::iequals(it->path().extension().wstring(), L"." + desc.name))
139                 {
140                         template_host.filename = it->path().filename().wstring();
141                         break;
142                 }
143         }
144
145         template_host.width =  desc.square_width;
146         template_host.height = desc.square_height;
147         return template_host;
148 }
149
150 class flash_renderer
151 {       
152         struct com_init
153         {
154                 HRESULT result_;
155
156                 com_init()
157                         : result_(CoInitialize(nullptr))
158                 {
159                         if(FAILED(result_))
160                                 CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info("Failed to initialize com-context for flash-player"));
161                 }
162
163                 ~com_init()
164                 {
165                         if(SUCCEEDED(result_))
166                                 ::CoUninitialize();
167                 }
168         } com_init_;
169
170         core::monitor::subject&                                         monitor_subject_;
171
172         const std::wstring                                                      filename_;
173
174         const std::shared_ptr<core::frame_factory>      frame_factory_;
175         
176         CComObject<caspar::flash::FlashAxContainer>* ax_;
177         core::draw_frame                                                        head_;
178         bitmap                                                                          bmp_;
179         
180         spl::shared_ptr<diagnostics::graph>                     graph_;
181         
182         prec_timer                                                                      timer_;
183
184         const int                                                                       width_;
185         const int                                                                       height_;
186         
187 public:
188         flash_renderer(core::monitor::subject& monitor_subject, const spl::shared_ptr<diagnostics::graph>& graph, const std::shared_ptr<core::frame_factory>& frame_factory, const std::wstring& filename, int width, int height) 
189                 : monitor_subject_(monitor_subject)
190                 , graph_(graph)
191                 , filename_(filename)
192                 , frame_factory_(frame_factory)
193                 , ax_(nullptr)
194                 , head_(core::draw_frame::empty())
195                 , bmp_(width, height)
196                 , width_(width)
197                 , height_(height)
198         {               
199                 graph_->set_color("frame-time", diagnostics::color(0.1f, 1.0f, 0.1f));
200                 graph_->set_color("param", diagnostics::color(1.0f, 0.5f, 0.0f));       
201                 graph_->set_color("sync", diagnostics::color(0.8f, 0.3f, 0.2f));                        
202                 
203                 if(FAILED(CComObject<caspar::flash::FlashAxContainer>::CreateInstance(&ax_)))
204                         CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(print() + L" Failed to create FlashAxContainer"));
205                 
206                 ax_->set_print([this]{return print();});
207
208                 if(FAILED(ax_->CreateAxControl()))
209                         CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(print() + L" Failed to Create FlashAxControl"));
210                 
211                 CComPtr<IShockwaveFlash> spFlash;
212                 if(FAILED(ax_->QueryControl(&spFlash)))
213                         CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(print() + L" Failed to Query FlashAxControl"));
214                                                                                                 
215                 if(FAILED(spFlash->put_Playing(true)) )
216                         CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(print() + L" Failed to start playing Flash"));
217
218                 if(FAILED(spFlash->put_Movie(CComBSTR(filename.c_str()))))
219                         CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(print() + L" Failed to Load Template Host"));
220                                                                                 
221                 if(FAILED(spFlash->put_ScaleMode(2)))  //Exact fit. Scale without respect to the aspect ratio.
222                         CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(print() + L" Failed to Set Scale Mode"));
223                                                 
224                 ax_->SetSize(width_, height_);          
225
226                 tick(false);
227                 render();
228
229                 CASPAR_LOG(info) << print() << L" Initialized.";
230         }
231
232         ~flash_renderer()
233         {               
234                 if(ax_)
235                 {
236                         ax_->DestroyAxControl();
237                         ax_->Release();
238                 }
239                 graph_->set_value("tick-time", 0.0f);
240                 graph_->set_value("frame-time", 0.0f);
241                 CASPAR_LOG(info) << print() << L" Uninitialized.";
242         }
243         
244         std::wstring call(const std::wstring& param)
245         {               
246                 std::wstring result;
247
248                 CASPAR_LOG(trace) << print() << " Call: " << param;
249
250                 if(!ax_->FlashCall(param, result))
251                         CASPAR_LOG(warning) << print() << L" Flash call failed:" << param;//CASPAR_THROW_EXCEPTION(invalid_operation() << msg_info("Flash function call failed.") << arg_name_info("param") << arg_value_info(narrow(param)));
252                 graph_->set_tag("param");
253
254                 return result;
255         }
256
257         void tick(double sync)
258         {               
259                 const float frame_time = 1.0f/ax_->GetFPS();
260
261                 if(sync > 0.00001)                      
262                         timer_.tick(frame_time*sync); // This will block the thread.
263                 else
264                         graph_->set_tag("sync");
265
266                 graph_->set_value("sync", sync);
267                 monitor_subject_ << core::monitor::message("/sync") % sync;
268                 
269                 ax_->Tick();
270                                         
271                 MSG msg;
272                 while(PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE)) // DO NOT REMOVE THE MESSAGE DISPATCH LOOP. Without this some stuff doesn't work!  
273                 {
274                         if(msg.message == WM_TIMER && msg.wParam == 3 && msg.lParam == 0) // We tick this inside FlashAxContainer
275                                 continue;
276                         
277                         TranslateMessage(&msg);
278                         DispatchMessage(&msg);                  
279                 }
280         }
281         
282         core::draw_frame render()
283         {                       
284                 const float frame_time = 1.0f/fps();
285
286                 boost::timer frame_timer;
287
288                 if(ax_->InvalidRect())
289                 {                       
290                         A_memset(bmp_.data(), 0, width_*height_*4);
291                         ax_->DrawControl(bmp_);
292                 
293                         core::pixel_format_desc desc = core::pixel_format::bgra;
294                         desc.planes.push_back(core::pixel_format_desc::plane(width_, height_, 4));
295                         auto frame = frame_factory_->create_frame(this, desc);
296
297                         A_memcpy(frame.image_data(0).begin(), bmp_.data(), width_*height_*4);
298                         head_ = core::draw_frame(std::move(frame));     
299                 }               
300                                                                                 
301                 graph_->set_value("frame-time", static_cast<float>(frame_timer.elapsed()/frame_time)*0.5f);
302                 monitor_subject_ << core::monitor::message("/renderer/profiler/time") % frame_timer.elapsed() % frame_time;
303                 return head_;
304         }
305         
306         bool is_empty() const
307         {
308                 return ax_->IsEmpty();
309         }
310
311         double fps() const
312         {
313                 return ax_->GetFPS();   
314         }
315         
316         std::wstring print()
317         {
318                 return L"flash-player[" + boost::filesystem::wpath(filename_).filename().wstring() 
319                                   + L"|" + boost::lexical_cast<std::wstring>(width_)
320                                   + L"x" + boost::lexical_cast<std::wstring>(height_)
321                                   + L"]";               
322         }
323 };
324
325 struct flash_producer : public core::frame_producer_base
326 {       
327         core::monitor::subject                                                  monitor_subject_;
328         const std::wstring                                                              filename_;      
329         const spl::shared_ptr<core::frame_factory>              frame_factory_;
330         const core::video_format_desc                                   format_desc_;
331         const int                                                                               width_;
332         const int                                                                               height_;
333         core::constraints                                                               constraints_;
334         const int                                                                               buffer_size_;
335
336         tbb::atomic<int>                                                                fps_;
337
338         spl::shared_ptr<diagnostics::graph>                             graph_;
339
340         std::queue<core::draw_frame>                                    frame_buffer_;
341         tbb::concurrent_bounded_queue<core::draw_frame> output_buffer_;
342
343         core::draw_frame                                                                last_frame_;
344                                 
345         boost::timer                                                                    tick_timer_;
346         std::unique_ptr<flash_renderer>                                 renderer_;
347         
348         executor                                                                                executor_;      
349 public:
350         flash_producer(const spl::shared_ptr<core::frame_factory>& frame_factory, const core::video_format_desc& format_desc, const std::wstring& filename, int width, int height) 
351                 : filename_(filename)           
352                 , frame_factory_(frame_factory)
353                 , format_desc_(format_desc)
354                 , width_(width > 0 ? width : format_desc.width)
355                 , height_(height > 0 ? height : format_desc.height)
356                 , constraints_(width_, height_)
357                 , buffer_size_(env::properties().get(L"configuration.flash.buffer-depth", format_desc.fps > 30.0 ? 4 : 2))
358                 , executor_(L"flash_producer")
359         {       
360                 fps_ = 0;
361          
362                 graph_->set_color("buffer-size", diagnostics::color(1.0f, 1.0f, 0.0f));
363                 graph_->set_color("tick-time", diagnostics::color(0.0f, 0.6f, 0.9f));
364                 graph_->set_color("late-frame", diagnostics::color(0.6f, 0.3f, 0.9f));
365                 graph_->set_text(print());
366                 diagnostics::register_graph(graph_);
367
368                 CASPAR_LOG(info) << print() << L" Initialized";
369         }
370
371         ~flash_producer()
372         {
373                 executor_.invoke([this]
374                 {
375                         renderer_.reset();
376                 }, task_priority::high_priority);
377         }
378
379         // frame_producer
380                 
381         core::draw_frame receive_impl() override
382         {                                       
383                 auto frame = last_frame_;
384                 
385                 if(output_buffer_.try_pop(frame))                       
386                         executor_.begin_invoke(std::bind(&flash_producer::next, this));         
387                 else            
388                         graph_->set_tag("late-frame");          
389                                 
390                 monitor_subject_ << core::monitor::message("/host/path")        % filename_
391                                                 << core::monitor::message("/host/width")        % width_
392                                                 << core::monitor::message("/host/height")       % height_
393                                                 << core::monitor::message("/host/fps")          % fps_
394                                                 << core::monitor::message("/buffer")            % output_buffer_.size() % buffer_size_;
395
396                 return last_frame_ = frame;
397         }
398
399         core::constraints& pixel_constraints() override
400         {
401                 return constraints_;
402         }
403                 
404         std::future<std::wstring> call(const std::vector<std::wstring>& params) override
405         {
406                 auto param = boost::algorithm::join(params, L" ");
407
408                 return executor_.begin_invoke([this, param]() -> std::wstring
409                 {                       
410                         try
411                         {
412                                 if(!renderer_)
413                                 {
414                                         renderer_.reset(new flash_renderer(monitor_subject_, graph_, frame_factory_, filename_, width_, height_));
415
416                                         while(output_buffer_.size() < buffer_size_)
417                                                 output_buffer_.push(core::draw_frame::empty());
418                                 }
419
420                                 return renderer_->call(param);  
421                         }
422                         catch(...)
423                         {
424                                 CASPAR_LOG_CURRENT_EXCEPTION();
425                                 renderer_.reset(nullptr);
426                         }
427
428                         return L"";
429                 });
430         }
431                 
432         std::wstring print() const override
433         { 
434                 return L"flash[" + boost::filesystem::path(filename_).wstring() + L"|" + boost::lexical_cast<std::wstring>(fps_) + L"]";                
435         }       
436
437         std::wstring name() const override
438         {
439                 return L"flash";
440         }
441
442         boost::property_tree::wptree info() const override
443         {
444                 boost::property_tree::wptree info;
445                 info.add(L"type", L"flash");
446                 return info;
447         }
448
449         core::monitor::subject& monitor_output()
450         {
451                 return monitor_subject_;
452         }
453
454         // flash_producer
455         
456         void tick()
457         {
458                 double ratio = std::min(1.0, static_cast<double>(output_buffer_.size())/static_cast<double>(std::max(1, buffer_size_ - 1)));
459                 double sync  = 2*ratio - ratio*ratio;
460                 renderer_->tick(sync);
461         }
462         
463         void next()
464         {       
465                 if(!renderer_)
466                         frame_buffer_.push(core::draw_frame::empty());
467
468                 tick_timer_.restart();                          
469
470                 if(frame_buffer_.empty())
471                 {                                       
472                         tick();
473                         auto frame = renderer_->render();
474
475                         if(abs(renderer_->fps()/2.0 - format_desc_.fps) < 2.0) // flash == 2 * format -> interlace
476                         {                                       
477                                 tick();
478                                 if(format_desc_.field_mode != core::field_mode::progressive)
479                                         frame = core::draw_frame::interlace(frame, renderer_->render(), format_desc_.field_mode);
480                                 
481                                 frame_buffer_.push(frame);
482                         }
483                         else if(abs(renderer_->fps() - format_desc_.fps/2.0) < 2.0) // format == 2 * flash -> duplicate
484                         {
485                                 frame_buffer_.push(frame);
486                                 frame_buffer_.push(frame);
487                         }
488                         else //if(abs(renderer_->fps() - format_desc_.fps) < 0.1) // format == flash -> simple
489                         {
490                                 frame_buffer_.push(frame);
491                         }
492                                                 
493                         fps_.fetch_and_store(static_cast<int>(renderer_->fps()*100.0));                         
494                         graph_->set_text(print());
495                         
496                         if(renderer_->is_empty())                       
497                                 renderer_.reset();
498                 }
499
500                 graph_->set_value("tick-time", static_cast<float>(tick_timer_.elapsed()/fps_)*0.5f);
501                 monitor_subject_ << core::monitor::message("/profiler/time") % tick_timer_.elapsed() % fps_;
502
503                 output_buffer_.push(std::move(frame_buffer_.front()));
504                 frame_buffer_.pop();
505         }
506 };
507
508 spl::shared_ptr<core::frame_producer> create_producer(const spl::shared_ptr<core::frame_factory>& frame_factory, const core::video_format_desc& format_desc, const std::vector<std::wstring>& params)
509 {
510         auto template_host = get_template_host(format_desc);
511         
512         auto filename = env::template_folder() + L"\\" + template_host.filename;
513         
514         if(!boost::filesystem::exists(filename))
515                 CASPAR_THROW_EXCEPTION(file_not_found() << boost::errinfo_file_name(u8(filename)));     
516
517         return create_destroy_proxy(spl::make_shared<flash_producer>(frame_factory, format_desc, filename, template_host.width, template_host.height));
518 }
519
520 spl::shared_ptr<core::frame_producer> create_swf_producer(const spl::shared_ptr<core::frame_factory>& frame_factory, const core::video_format_desc& format_desc, const std::vector<std::wstring>& params)
521 {
522         auto filename = env::media_folder() + L"\\" + params.at(0) + L".swf";
523
524         if (!boost::filesystem::exists(filename))
525                 return core::frame_producer::empty();
526
527         swf_t::header_t header(filename);
528
529         return create_destroy_proxy(
530                 spl::make_shared<flash_producer>(frame_factory, format_desc, filename, header.frame_width, header.frame_height));
531 }
532
533 std::wstring find_template(const std::wstring& template_name)
534 {
535         if(boost::filesystem::exists(template_name + L".ft")) 
536                 return template_name + L".ft";
537         
538         if(boost::filesystem::exists(template_name + L".ct"))
539                 return template_name + L".ct";
540         
541         if(boost::filesystem::exists(template_name + L".swf"))
542                 return template_name + L".swf";
543
544         return L"";
545 }
546
547 }}