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