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