]> git.sesse.net Git - casparcg/blob - modules/flash/producer/flash_producer.cpp
ยค412
[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 #endif\r
26 \r
27 #include "flash_producer.h"\r
28 #include "FlashAxContainer.h"\r
29 \r
30 #include <core/video_format.h>\r
31 \r
32 #include <core/producer/frame/basic_frame.h>\r
33 #include <core/producer/frame/write_frame.h>\r
34 \r
35 #include <common/env.h>\r
36 #include <common/concurrency/executor.h>\r
37 #include <common/utility/timer.h>\r
38 #include <common/diagnostics/graph.h>\r
39 \r
40 #include <boost/filesystem.hpp>\r
41 \r
42 #include <functional>\r
43 #include <boost/thread.hpp>\r
44 \r
45 namespace caspar {\r
46                 \r
47 bool interlaced(double fps, const core::video_format_desc& format_desc)\r
48 {\r
49         return abs(fps/2.0 - format_desc.fps) < 0.1;\r
50 }\r
51 \r
52 class flash_renderer\r
53 {\r
54         struct co_init\r
55         {\r
56                 co_init(){CoInitialize(nullptr);}\r
57                 ~co_init(){CoUninitialize();}\r
58         } co_;\r
59         \r
60         const object* parent_;\r
61         const std::wstring filename_;\r
62         const core::video_format_desc format_desc_;\r
63 \r
64         std::shared_ptr<core::frame_factory> frame_factory_;\r
65         \r
66         BYTE* bmp_data_;        \r
67         std::shared_ptr<void> hdc_;\r
68         std::shared_ptr<void> bmp_;\r
69 \r
70         CComObject<caspar::flash::FlashAxContainer>* ax_;\r
71         safe_ptr<core::basic_frame> head_;\r
72         \r
73         safe_ptr<diagnostics::graph> graph_;\r
74         timer perf_timer_;\r
75         \r
76 public:\r
77         flash_renderer(const object* parent, const safe_ptr<diagnostics::graph>& graph, const std::shared_ptr<core::frame_factory>& frame_factory, const std::wstring& filename) \r
78                 : parent_(parent)\r
79                 , graph_(graph)\r
80                 , filename_(filename)\r
81                 , format_desc_(frame_factory->get_video_format_desc())\r
82                 , frame_factory_(frame_factory)\r
83                 , bmp_data_(nullptr)\r
84                 , hdc_(CreateCompatibleDC(0), DeleteDC)\r
85                 , ax_(nullptr)\r
86                 , head_(core::basic_frame::empty())\r
87         {\r
88                 graph_->add_guide("frame-time", 0.5f);\r
89                 graph_->set_color("frame-time", diagnostics::color(1.0f, 0.0f, 0.0f));  \r
90                 graph_->set_color("param", diagnostics::color(1.0f, 0.5f, 0.0f));       \r
91                 graph_->set_color("underflow", diagnostics::color(0.6f, 0.3f, 0.9f));                   \r
92                 \r
93                 if(FAILED(CComObject<caspar::flash::FlashAxContainer>::CreateInstance(&ax_)))\r
94                         BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Failed to create FlashAxContainer"));\r
95                 \r
96                 ax_->set_print([this]{return L"";});\r
97 \r
98                 if(FAILED(ax_->CreateAxControl()))\r
99                         BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Failed to Create FlashAxControl"));\r
100                 \r
101                 CComPtr<IShockwaveFlash> spFlash;\r
102                 if(FAILED(ax_->QueryControl(&spFlash)))\r
103                         BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Failed to Query FlashAxControl"));\r
104                                                                                                 \r
105                 if(FAILED(spFlash->put_Playing(true)) )\r
106                         BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Failed to start playing Flash"));\r
107 \r
108                 if(FAILED(spFlash->put_Movie(CComBSTR(filename.c_str()))))\r
109                         BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Failed to Load Template Host"));\r
110                                                                                 \r
111                 if(FAILED(spFlash->put_ScaleMode(2)))  //Exact fit. Scale without respect to the aspect ratio.\r
112                         BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Failed to Set Scale Mode"));\r
113                                                 \r
114                 ax_->SetFormat(format_desc_);\r
115                 \r
116                 BITMAPINFO info;\r
117                 memset(&info, 0, sizeof(BITMAPINFO));\r
118                 info.bmiHeader.biBitCount = 32;\r
119                 info.bmiHeader.biCompression = BI_RGB;\r
120                 info.bmiHeader.biHeight = -format_desc_.height;\r
121                 info.bmiHeader.biPlanes = 1;\r
122                 info.bmiHeader.biSize = sizeof(BITMAPINFO);\r
123                 info.bmiHeader.biWidth = format_desc_.width;\r
124 \r
125                 bmp_.reset(CreateDIBSection(static_cast<HDC>(hdc_.get()), &info, DIB_RGB_COLORS, reinterpret_cast<void**>(&bmp_data_), 0, 0), DeleteObject);\r
126                 SelectObject(static_cast<HDC>(hdc_.get()), bmp_.get()); \r
127                 CASPAR_LOG(info) << print() << L" Thread started.";\r
128         }\r
129 \r
130         ~flash_renderer()\r
131         {               \r
132                 if(ax_)\r
133                 {\r
134                         ax_->DestroyAxControl();\r
135                         ax_->Release();\r
136                 }\r
137                 CASPAR_LOG(info) << print() << L" Thread ended.";\r
138         }\r
139         \r
140         void param(const std::wstring& param)\r
141         {               \r
142                 if(!ax_->FlashCall(param))\r
143                         CASPAR_LOG(warning) << print() << " Flash function call failed. Param: " << param << ".";\r
144                 graph_->add_tag("param");\r
145 \r
146                 if(abs(ax_->GetFPS() - format_desc_.fps) > 0.001 && abs(ax_->GetFPS()/2.0 - format_desc_.fps) > 0.001)\r
147                         CASPAR_LOG(warning) << print() << " Invalid frame-rate: " << ax_->GetFPS() << L". Should be either " << format_desc_.fps << L" or " << format_desc_.fps*2.0 << L".";\r
148         }\r
149         \r
150         safe_ptr<core::basic_frame> render_frame(bool has_underflow)\r
151         {\r
152                 if(ax_->IsEmpty())\r
153                         return core::basic_frame::empty();\r
154 \r
155                 auto frame = render_simple_frame(has_underflow);\r
156                 if(interlaced(ax_->GetFPS(), format_desc_))\r
157                         frame = core::basic_frame::interlace(frame, render_simple_frame(has_underflow), format_desc_.mode);\r
158                 return frame;\r
159         }\r
160 \r
161         double fps() const\r
162         {\r
163                 return ax_->GetFPS();   \r
164         }\r
165         \r
166         std::wstring print()\r
167         {\r
168                 return parent_->print();\r
169         }\r
170 \r
171 private:\r
172 \r
173         safe_ptr<core::basic_frame> render_simple_frame(bool underflow)\r
174         {\r
175                 if(underflow)\r
176                         graph_->add_tag("underflow");\r
177 \r
178                 double frame_time = 1.0/ax_->GetFPS();\r
179 \r
180                 perf_timer_.reset();\r
181                 ax_->Tick();\r
182 \r
183                 if(ax_->InvalidRect())\r
184                 {                       \r
185                         std::fill_n(bmp_data_, format_desc_.size, 0);\r
186                         ax_->DrawControl(static_cast<HDC>(hdc_.get()));\r
187                 \r
188                         auto frame = frame_factory_->create_frame();\r
189                         std::copy_n(bmp_data_, format_desc_.size, frame->image_data().begin());\r
190                         head_ = frame;\r
191                 }               \r
192                 \r
193                 graph_->update_value("frame-time", static_cast<float>(perf_timer_.elapsed()/frame_time));\r
194                 return head_;\r
195         }\r
196 };\r
197 \r
198 struct flash_producer : public core::frame_producer\r
199 {       \r
200         const std::wstring filename_;   \r
201         tbb::atomic<int> fps_;\r
202 \r
203         std::shared_ptr<diagnostics::graph> graph_;\r
204 \r
205         safe_ptr<core::basic_frame> tail_;\r
206         tbb::concurrent_bounded_queue<safe_ptr<core::basic_frame>> frame_buffer_;\r
207 \r
208         std::shared_ptr<flash_renderer> renderer_;\r
209         std::shared_ptr<core::frame_factory> frame_factory_;\r
210         core::video_format_desc format_desc_;\r
211                         \r
212         executor executor_;\r
213                 \r
214 public:\r
215         flash_producer(const std::wstring& filename) \r
216                 : filename_(filename)           \r
217                 , tail_(core::basic_frame::empty())             \r
218                 , executor_(L"flash_producer")\r
219         {       \r
220                 if(!boost::filesystem::exists(filename))\r
221                         BOOST_THROW_EXCEPTION(file_not_found() << boost::errinfo_file_name(narrow(filename)));  \r
222                  \r
223                 fps_ = 0;\r
224         }\r
225 \r
226         ~flash_producer()\r
227         {\r
228                 executor_.clear();\r
229                 executor_.invoke([=]\r
230                 {\r
231                         renderer_ = nullptr;\r
232                 });\r
233         }\r
234         \r
235         virtual void set_frame_factory(const safe_ptr<core::frame_factory>& frame_factory)\r
236         {\r
237                 frame_factory_ = frame_factory;\r
238                 format_desc_ = frame_factory->get_video_format_desc();\r
239                 frame_buffer_.set_capacity(4);\r
240                 graph_ = diagnostics::create_graph([this]{return print();});\r
241                 graph_->set_color("output-buffer", diagnostics::color(0.0f, 1.0f, 0.0f));       \r
242                 executor_.start();\r
243         }\r
244         \r
245         virtual safe_ptr<core::basic_frame> receive()\r
246         {               \r
247                 if(!renderer_)\r
248                         return core::basic_frame::empty();\r
249                 \r
250                 graph_->set_value("output-buffer", static_cast<float>(frame_buffer_.size())/static_cast<float>(frame_buffer_.capacity()));\r
251                 if(!frame_buffer_.try_pop(tail_))\r
252                         return tail_;\r
253 \r
254                 executor_.begin_invoke([=]\r
255                 {\r
256                         if(!renderer_)\r
257                                 return;\r
258 \r
259                         try\r
260                         {\r
261                                 auto frame = core::basic_frame::empty();\r
262                                 do{frame = renderer_->render_frame(frame_buffer_.size() < frame_buffer_.capacity()-2);}\r
263                                 while(frame_buffer_.try_push(frame) && frame == core::basic_frame::empty());\r
264                                 graph_->set_value("output-buffer", static_cast<float>(frame_buffer_.size())/static_cast<float>(frame_buffer_.capacity()));      \r
265                                 fps_.fetch_and_store(static_cast<int>(renderer_->fps()*100.0));\r
266                         }\r
267                         catch(...)\r
268                         {\r
269                                 CASPAR_LOG_CURRENT_EXCEPTION();\r
270                                 renderer_ = nullptr;\r
271                         }\r
272                 });                     \r
273 \r
274                 return tail_;\r
275         }\r
276         \r
277         virtual void param(const std::wstring& param) \r
278         {       \r
279                 executor_.begin_invoke([=]\r
280                 {\r
281                         if(!renderer_)\r
282                         {\r
283                                 renderer_.reset(new flash_renderer(this, safe_ptr<diagnostics::graph>(graph_), frame_factory_, filename_));\r
284                                 while(frame_buffer_.try_push(core::basic_frame::empty())){}             \r
285                         }\r
286 \r
287                         try\r
288                         {\r
289                                 renderer_->param(param);        \r
290                         }\r
291                         catch(...)\r
292                         {\r
293                                 CASPAR_LOG_CURRENT_EXCEPTION();\r
294                                 renderer_ = nullptr;\r
295                         }\r
296                 });\r
297         }\r
298                 \r
299         virtual std::wstring print() const\r
300         { \r
301                 return frame_producer::print() + L"flash[" + boost::filesystem::wpath(filename_).filename() + L", " + \r
302                                         boost::lexical_cast<std::wstring>(fps_) + \r
303                                         (interlaced(fps_, format_desc_) ? L"i" : L"p") + L"]";          \r
304         }       \r
305 };\r
306 \r
307 safe_ptr<core::frame_producer> create_flash_producer(const std::vector<std::wstring>& params)\r
308 {\r
309         std::wstring filename = env::template_folder() + L"\\" + params[0];\r
310         \r
311         return make_safe<flash_producer>(filename);\r
312 }\r
313 \r
314 std::wstring find_flash_template(const std::wstring& template_name)\r
315 {\r
316         if(boost::filesystem::exists(template_name + L".ft")) \r
317                 return template_name + L".ft";\r
318         \r
319         if(boost::filesystem::exists(template_name + L".ct"))\r
320                 return template_name + L".ct";\r
321 \r
322         return L"";\r
323 }\r
324 \r
325 }