]> git.sesse.net Git - casparcg/blob - modules/flash/producer/flash_producer.cpp
* Enforce help descriptions for producers in code.
[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/producer/frame_producer.h>
41 #include <core/monitor/monitor.h>
42 #include <core/help/help_repository.h>
43 #include <core/help/help_sink.h>
44
45 #include <common/env.h>
46 #include <common/executor.h>
47 #include <common/lock.h>
48 #include <common/diagnostics/graph.h>
49 #include <common/prec_timer.h>
50 #include <common/array.h>
51
52 #include <boost/filesystem.hpp>
53 #include <boost/property_tree/ptree.hpp>
54 #include <boost/thread.hpp>
55 #include <boost/timer.hpp>
56 #include <boost/algorithm/string.hpp>
57
58 #include <tbb/spin_mutex.h>
59
60 #include <asmlib.h>
61
62 #include <functional>
63
64 namespace caspar { namespace flash {
65                 
66 class bitmap
67 {
68 public:
69         bitmap(int width, int height)
70                 : bmp_data_(nullptr)
71                 , hdc_(CreateCompatibleDC(0), DeleteDC)
72         {       
73                 BITMAPINFO info;
74                 memset(&info, 0, sizeof(BITMAPINFO));
75                 info.bmiHeader.biBitCount = 32;
76                 info.bmiHeader.biCompression = BI_RGB;
77                 info.bmiHeader.biHeight = static_cast<LONG>(-height);
78                 info.bmiHeader.biPlanes = 1;
79                 info.bmiHeader.biSize = sizeof(BITMAPINFO);
80                 info.bmiHeader.biWidth = static_cast<LONG>(width);
81
82                 bmp_.reset(CreateDIBSection(static_cast<HDC>(hdc_.get()), &info, DIB_RGB_COLORS, reinterpret_cast<void**>(&bmp_data_), 0, 0), DeleteObject);
83                 SelectObject(static_cast<HDC>(hdc_.get()), bmp_.get()); 
84
85                 if(!bmp_data_)
86                         CASPAR_THROW_EXCEPTION(bad_alloc());
87         }
88
89         operator HDC() {return static_cast<HDC>(hdc_.get());}
90
91         BYTE* data() { return bmp_data_;}
92         const BYTE* data() const { return bmp_data_;}
93
94 private:
95         BYTE* bmp_data_;        
96         std::shared_ptr<void> hdc_;
97         std::shared_ptr<void> bmp_;
98 };
99
100 struct template_host
101 {
102         std::wstring  video_mode;
103         std::wstring  filename;
104         int                       width;
105         int                       height;
106 };
107
108 template_host get_template_host(const core::video_format_desc& desc)
109 {
110         try
111         {
112                 std::vector<template_host> template_hosts;
113                 for (auto& xml_mapping : env::properties().get_child(L"configuration.template-hosts"))
114                 {
115                         try
116                         {
117                                 template_host template_host;
118                                 template_host.video_mode                = xml_mapping.second.get(L"video-mode", L"");
119                                 template_host.filename                  = xml_mapping.second.get(L"filename",   L"cg.fth");
120                                 template_host.width                             = xml_mapping.second.get(L"width",              desc.width);
121                                 template_host.height                    = xml_mapping.second.get(L"height",             desc.height);
122                                 template_hosts.push_back(template_host);
123                         }
124                         catch(...){}
125                 }
126
127                 auto template_host_it = boost::find_if(template_hosts, [&](template_host template_host){return template_host.video_mode == desc.name;});
128                 if(template_host_it == template_hosts.end())
129                         template_host_it = boost::find_if(template_hosts, [&](template_host template_host){return template_host.video_mode == L"";});
130
131                 if(template_host_it != template_hosts.end())
132                         return *template_host_it;
133         }
134         catch(...){}
135                 
136         template_host template_host;
137         template_host.filename = L"cg.fth";
138
139         for(auto it = boost::filesystem::directory_iterator(env::template_folder()); it != boost::filesystem::directory_iterator(); ++it)
140         {
141                 if(boost::iequals(it->path().extension().wstring(), L"." + desc.name))
142                 {
143                         template_host.filename = it->path().filename().wstring();
144                         break;
145                 }
146         }
147
148         template_host.width =  desc.square_width;
149         template_host.height = desc.square_height;
150         return template_host;
151 }
152
153 class flash_renderer
154 {       
155         struct com_init
156         {
157                 HRESULT result_ = CoInitialize(nullptr);
158
159                 com_init()
160                 {
161                         if(FAILED(result_))
162                                 CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info("Failed to initialize com-context for flash-player"));
163                 }
164
165                 ~com_init()
166                 {
167                         if(SUCCEEDED(result_))
168                                 ::CoUninitialize();
169                 }
170         } com_init_;
171
172         core::monitor::subject&                                                 monitor_subject_;
173
174         const std::wstring                                                              filename_;
175         const int                                                                               width_;
176         const int                                                                               height_;
177
178         const std::shared_ptr<core::frame_factory>              frame_factory_;
179         
180         CComObject<caspar::flash::FlashAxContainer>*    ax_                                     = nullptr;
181         core::draw_frame                                                                head_                           = core::draw_frame::empty();
182         bitmap                                                                                  bmp_                            { width_, height_ };
183         
184         spl::shared_ptr<diagnostics::graph>                             graph_;
185         
186         prec_timer                                                                              timer_;
187
188         
189 public:
190         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) 
191                 : monitor_subject_(monitor_subject)
192                 , graph_(graph)
193                 , filename_(filename)
194                 , width_(width)
195                 , height_(height)
196                 , frame_factory_(frame_factory)
197                 , bmp_(width, 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_            { static_cast<double>(width_), static_cast<double>(height_) };
334         const int                                                                               buffer_size_            = env::properties().get(L"configuration.flash.buffer-depth", format_desc_.fps > 30.0 ? 4 : 2);
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_                       = L"flash_producer";
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         {       
357                 fps_ = 0;
358          
359                 graph_->set_color("buffer-size", diagnostics::color(1.0f, 1.0f, 0.0f));
360                 graph_->set_color("tick-time", diagnostics::color(0.0f, 0.6f, 0.9f));
361                 graph_->set_color("late-frame", diagnostics::color(0.6f, 0.3f, 0.9f));
362                 graph_->set_text(print());
363                 diagnostics::register_graph(graph_);
364
365                 CASPAR_LOG(info) << print() << L" Initialized";
366         }
367
368         ~flash_producer()
369         {
370                 executor_.invoke([this]
371                 {
372                         renderer_.reset();
373                 }, task_priority::high_priority);
374         }
375
376         // frame_producer
377                 
378         core::draw_frame receive_impl() override
379         {                                       
380                 auto frame = last_frame_;
381                 
382                 if(output_buffer_.try_pop(frame))                       
383                         executor_.begin_invoke(std::bind(&flash_producer::next, this));         
384                 else            
385                         graph_->set_tag("late-frame");          
386                                 
387                 monitor_subject_ << core::monitor::message("/host/path")        % filename_
388                                                 << core::monitor::message("/host/width")        % width_
389                                                 << core::monitor::message("/host/height")       % height_
390                                                 << core::monitor::message("/host/fps")          % fps_
391                                                 << core::monitor::message("/buffer")            % output_buffer_.size() % buffer_size_;
392
393                 return last_frame_ = frame;
394         }
395
396         core::constraints& pixel_constraints() override
397         {
398                 return constraints_;
399         }
400                 
401         std::future<std::wstring> call(const std::vector<std::wstring>& params) override
402         {
403                 auto param = boost::algorithm::join(params, L" ");
404
405                 return executor_.begin_invoke([this, param]() -> std::wstring
406                 {                       
407                         try
408                         {
409                                 if(!renderer_)
410                                 {
411                                         renderer_.reset(new flash_renderer(monitor_subject_, graph_, frame_factory_, filename_, width_, height_));
412
413                                         while(output_buffer_.size() < buffer_size_)
414                                                 output_buffer_.push(core::draw_frame::empty());
415                                 }
416
417                                 return renderer_->call(param);  
418                         }
419                         catch(...)
420                         {
421                                 CASPAR_LOG_CURRENT_EXCEPTION();
422                                 renderer_.reset(nullptr);
423                         }
424
425                         return L"";
426                 });
427         }
428                 
429         std::wstring print() const override
430         { 
431                 return L"flash[" + boost::filesystem::path(filename_).wstring() + L"|" + boost::lexical_cast<std::wstring>(fps_) + L"]";                
432         }       
433
434         std::wstring name() const override
435         {
436                 return L"flash";
437         }
438
439         boost::property_tree::wptree info() const override
440         {
441                 boost::property_tree::wptree info;
442                 info.add(L"type", L"flash");
443                 return info;
444         }
445
446         core::monitor::subject& monitor_output()
447         {
448                 return monitor_subject_;
449         }
450
451         // flash_producer
452         
453         void tick()
454         {
455                 double ratio = std::min(1.0, static_cast<double>(output_buffer_.size())/static_cast<double>(std::max(1, buffer_size_ - 1)));
456                 double sync  = 2*ratio - ratio*ratio;
457                 renderer_->tick(sync);
458         }
459         
460         void next()
461         {       
462                 if(!renderer_)
463                         frame_buffer_.push(core::draw_frame::empty());
464
465                 tick_timer_.restart();                          
466
467                 if(frame_buffer_.empty())
468                 {                                       
469                         tick();
470                         auto frame = renderer_->render();
471
472                         if(abs(renderer_->fps()/2.0 - format_desc_.fps) < 2.0) // flash == 2 * format -> interlace
473                         {                                       
474                                 tick();
475                                 if(format_desc_.field_mode != core::field_mode::progressive)
476                                         frame = core::draw_frame::interlace(frame, renderer_->render(), format_desc_.field_mode);
477                                 
478                                 frame_buffer_.push(frame);
479                         }
480                         else if(abs(renderer_->fps() - format_desc_.fps/2.0) < 2.0) // format == 2 * flash -> duplicate
481                         {
482                                 frame_buffer_.push(frame);
483                                 frame_buffer_.push(frame);
484                         }
485                         else //if(abs(renderer_->fps() - format_desc_.fps) < 0.1) // format == flash -> simple
486                         {
487                                 frame_buffer_.push(frame);
488                         }
489                                                 
490                         fps_.fetch_and_store(static_cast<int>(renderer_->fps()*100.0));                         
491                         graph_->set_text(print());
492                         
493                         if(renderer_->is_empty())                       
494                                 renderer_.reset();
495                 }
496
497                 graph_->set_value("tick-time", static_cast<float>(tick_timer_.elapsed()/fps_)*0.5f);
498                 monitor_subject_ << core::monitor::message("/profiler/time") % tick_timer_.elapsed() % fps_;
499
500                 output_buffer_.push(std::move(frame_buffer_.front()));
501                 frame_buffer_.pop();
502         }
503 };
504
505 spl::shared_ptr<core::frame_producer> create_producer(const core::frame_producer_dependencies& dependencies, const std::vector<std::wstring>& params)
506 {
507         auto template_host = get_template_host(dependencies.format_desc);
508         
509         auto filename = env::template_folder() + L"\\" + template_host.filename;
510         
511         if(!boost::filesystem::exists(filename))
512                 CASPAR_THROW_EXCEPTION(file_not_found() << boost::errinfo_file_name(u8(filename)));     
513
514         return create_destroy_proxy(spl::make_shared<flash_producer>(dependencies.frame_factory, dependencies.format_desc, filename, template_host.width, template_host.height));
515 }
516
517 void describe_swf_producer(core::help_sink& sink, const core::help_repository& repo)
518 {
519         sink.short_description(L"Plays flash files (.swf files).");
520         sink.syntax(L"[swf_file:string]");
521         sink.para()->text(L"Plays flash files (.swf files). The file should reside under the media folder.");
522         sink.para()->text(L"Examples:");
523         sink.example(L">> PLAY 1-10 folder/swf_file");
524 }
525
526 spl::shared_ptr<core::frame_producer> create_swf_producer(const core::frame_producer_dependencies& dependencies, const std::vector<std::wstring>& params)
527 {
528         auto filename = env::media_folder() + L"\\" + params.at(0) + L".swf";
529
530         if (!boost::filesystem::exists(filename))
531                 return core::frame_producer::empty();
532
533         swf_t::header_t header(filename);
534
535         return create_destroy_proxy(
536                 spl::make_shared<flash_producer>(dependencies.frame_factory, dependencies.format_desc, filename, header.frame_width, header.frame_height));
537 }
538
539 std::wstring find_template(const std::wstring& template_name)
540 {
541         if(boost::filesystem::exists(template_name + L".ft")) 
542                 return template_name + L".ft";
543         
544         if(boost::filesystem::exists(template_name + L".ct"))
545                 return template_name + L".ct";
546         
547         if(boost::filesystem::exists(template_name + L".swf"))
548                 return template_name + L".swf";
549
550         return L"";
551 }
552
553 }}