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