2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
4 * This file is part of CasparCG (www.casparcg.com).
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.
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.
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/>.
19 * Author: Robert Nagy, ronag89@gmail.com
22 #include "../stdafx.h"
25 #pragma warning (disable : 4146)
26 #pragma warning (disable : 4244)
29 #include "flash_producer.h"
30 #include "FlashAxContainer.h"
32 #include "../util/swf.h"
34 #include <core/video_format.h>
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>
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>
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>
55 #include <tbb/spin_mutex.h>
61 namespace caspar { namespace flash {
66 bitmap(int width, int height)
68 , hdc_(CreateCompatibleDC(0), DeleteDC)
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);
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());
83 CASPAR_THROW_EXCEPTION(bad_alloc());
86 operator HDC() {return static_cast<HDC>(hdc_.get());}
88 BYTE* data() { return bmp_data_;}
89 const BYTE* data() const { return bmp_data_;}
93 std::shared_ptr<void> hdc_;
94 std::shared_ptr<void> bmp_;
99 std::wstring video_mode;
100 std::wstring filename;
105 template_host get_template_host(const core::video_format_desc& desc)
109 std::vector<template_host> template_hosts;
110 for (auto& xml_mapping : env::properties().get_child(L"configuration.template-hosts"))
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);
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"";});
128 if(template_host_it != template_hosts.end())
129 return *template_host_it;
133 template_host template_host;
134 template_host.filename = L"cg.fth";
136 for(auto it = boost::filesystem::directory_iterator(env::template_folder()); it != boost::filesystem::directory_iterator(); ++it)
138 if(boost::iequals(it->path().extension().wstring(), L"." + desc.name))
140 template_host.filename = it->path().filename().wstring();
145 template_host.width = desc.square_width;
146 template_host.height = desc.square_height;
147 return template_host;
157 : result_(CoInitialize(nullptr))
160 CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info("Failed to initialize com-context for flash-player"));
165 if(SUCCEEDED(result_))
170 core::monitor::subject& monitor_subject_;
172 const std::wstring filename_;
174 const std::shared_ptr<core::frame_factory> frame_factory_;
176 CComObject<caspar::flash::FlashAxContainer>* ax_;
177 core::draw_frame head_;
180 spl::shared_ptr<diagnostics::graph> graph_;
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)
191 , filename_(filename)
192 , frame_factory_(frame_factory)
194 , head_(core::draw_frame::empty())
195 , bmp_(width, height)
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));
203 if(FAILED(CComObject<caspar::flash::FlashAxContainer>::CreateInstance(&ax_)))
204 CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(print() + L" Failed to create FlashAxContainer"));
206 ax_->set_print([this]{return print();});
208 if(FAILED(ax_->CreateAxControl()))
209 CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(print() + L" Failed to Create FlashAxControl"));
211 CComPtr<IShockwaveFlash> spFlash;
212 if(FAILED(ax_->QueryControl(&spFlash)))
213 CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(print() + L" Failed to Query FlashAxControl"));
215 if(FAILED(spFlash->put_Playing(true)) )
216 CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(print() + L" Failed to start playing Flash"));
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"));
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"));
224 ax_->SetSize(width_, height_);
229 CASPAR_LOG(info) << print() << L" Initialized.";
236 ax_->DestroyAxControl();
239 graph_->set_value("tick-time", 0.0f);
240 graph_->set_value("frame-time", 0.0f);
241 CASPAR_LOG(info) << print() << L" Uninitialized.";
244 std::wstring call(const std::wstring& param)
248 CASPAR_LOG(trace) << print() << " Call: " << param;
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");
257 void tick(double sync)
259 const float frame_time = 1.0f/ax_->GetFPS();
262 timer_.tick(frame_time*sync); // This will block the thread.
264 graph_->set_tag("sync");
266 graph_->set_value("sync", sync);
267 monitor_subject_ << core::monitor::message("/sync") % sync;
272 while(PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE)) // DO NOT REMOVE THE MESSAGE DISPATCH LOOP. Without this some stuff doesn't work!
274 if(msg.message == WM_TIMER && msg.wParam == 3 && msg.lParam == 0) // We tick this inside FlashAxContainer
277 TranslateMessage(&msg);
278 DispatchMessage(&msg);
282 core::draw_frame render()
284 const float frame_time = 1.0f/fps();
286 boost::timer frame_timer;
288 if(ax_->InvalidRect())
290 A_memset(bmp_.data(), 0, width_*height_*4);
291 ax_->DrawControl(bmp_);
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);
297 A_memcpy(frame.image_data(0).begin(), bmp_.data(), width_*height_*4);
298 head_ = core::draw_frame(std::move(frame));
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;
306 bool is_empty() const
308 return ax_->IsEmpty();
313 return ax_->GetFPS();
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_)
325 struct flash_producer : public core::frame_producer_base
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_;
333 core::constraints constraints_;
334 const int buffer_size_;
336 tbb::atomic<int> fps_;
338 spl::shared_ptr<diagnostics::graph> graph_;
340 std::queue<core::draw_frame> frame_buffer_;
341 tbb::concurrent_bounded_queue<core::draw_frame> output_buffer_;
343 core::draw_frame last_frame_;
345 boost::timer tick_timer_;
346 std::unique_ptr<flash_renderer> renderer_;
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")
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_);
368 CASPAR_LOG(info) << print() << L" Initialized";
373 executor_.invoke([this]
376 }, task_priority::high_priority);
381 core::draw_frame receive_impl() override
383 auto frame = last_frame_;
385 if(output_buffer_.try_pop(frame))
386 executor_.begin_invoke(std::bind(&flash_producer::next, this));
388 graph_->set_tag("late-frame");
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_;
396 return last_frame_ = frame;
399 core::constraints& pixel_constraints() override
404 std::future<std::wstring> call(const std::vector<std::wstring>& params) override
406 auto param = boost::algorithm::join(params, L" ");
408 return executor_.begin_invoke([this, param]() -> std::wstring
414 renderer_.reset(new flash_renderer(monitor_subject_, graph_, frame_factory_, filename_, width_, height_));
416 while(output_buffer_.size() < buffer_size_)
417 output_buffer_.push(core::draw_frame::empty());
420 return renderer_->call(param);
424 CASPAR_LOG_CURRENT_EXCEPTION();
425 renderer_.reset(nullptr);
432 std::wstring print() const override
434 return L"flash[" + boost::filesystem::path(filename_).wstring() + L"|" + boost::lexical_cast<std::wstring>(fps_) + L"]";
437 std::wstring name() const override
442 boost::property_tree::wptree info() const override
444 boost::property_tree::wptree info;
445 info.add(L"type", L"flash");
449 core::monitor::subject& monitor_output()
451 return monitor_subject_;
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);
466 frame_buffer_.push(core::draw_frame::empty());
468 tick_timer_.restart();
470 if(frame_buffer_.empty())
473 auto frame = renderer_->render();
475 if(abs(renderer_->fps()/2.0 - format_desc_.fps) < 2.0) // flash == 2 * format -> interlace
478 if(format_desc_.field_mode != core::field_mode::progressive)
479 frame = core::draw_frame::interlace(frame, renderer_->render(), format_desc_.field_mode);
481 frame_buffer_.push(frame);
483 else if(abs(renderer_->fps() - format_desc_.fps/2.0) < 2.0) // format == 2 * flash -> duplicate
485 frame_buffer_.push(frame);
486 frame_buffer_.push(frame);
488 else //if(abs(renderer_->fps() - format_desc_.fps) < 0.1) // format == flash -> simple
490 frame_buffer_.push(frame);
493 fps_.fetch_and_store(static_cast<int>(renderer_->fps()*100.0));
494 graph_->set_text(print());
496 if(renderer_->is_empty())
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_;
503 output_buffer_.push(std::move(frame_buffer_.front()));
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)
510 auto template_host = get_template_host(format_desc);
512 auto filename = env::template_folder() + L"\\" + template_host.filename;
514 if(!boost::filesystem::exists(filename))
515 CASPAR_THROW_EXCEPTION(file_not_found() << boost::errinfo_file_name(u8(filename)));
517 return create_destroy_proxy(spl::make_shared<flash_producer>(frame_factory, format_desc, filename, template_host.width, template_host.height));
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)
522 auto filename = env::media_folder() + L"\\" + params.at(0) + L".swf";
524 if (!boost::filesystem::exists(filename))
525 return core::frame_producer::empty();
527 swf_t::header_t header(filename);
529 return create_destroy_proxy(
530 spl::make_shared<flash_producer>(frame_factory, format_desc, filename, header.frame_width, header.frame_height));
533 std::wstring find_template(const std::wstring& template_name)
535 if(boost::filesystem::exists(template_name + L".ft"))
536 return template_name + L".ft";
538 if(boost::filesystem::exists(template_name + L".ct"))
539 return template_name + L".ct";
541 if(boost::filesystem::exists(template_name + L".swf"))
542 return template_name + L".swf";