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