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