]> git.sesse.net Git - casparcg/blob - core/producer/flash/flash_producer.cpp
2.0.0.2:
[casparcg] / core / producer / flash / 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 : 4714) // marked as __forceinline not inlined\r
25 #endif\r
26 \r
27 #include "flash_producer.h"\r
28 #include "FlashAxContainer.h"\r
29 #include "TimerHelper.h"\r
30 #include "bitmap.h"\r
31 \r
32 #include "../../format/video_format.h"\r
33 #include "../../server.h"\r
34 #include "../../../common/concurrency/executor.h"\r
35 #include "../../../common/utility/scope_exit.h"\r
36 \r
37 #include "../../processor/frame.h"\r
38 #include "../../processor/composite_frame.h"\r
39 \r
40 #include <boost/assign.hpp>\r
41 #include <boost/filesystem.hpp>\r
42 #include <boost/thread.hpp>\r
43 \r
44 #include <tbb/atomic.h>\r
45 #include <tbb/concurrent_queue.h>\r
46 \r
47 #include <type_traits>\r
48 \r
49 namespace caspar { namespace core { namespace flash {\r
50 \r
51 using namespace boost::assign;\r
52 \r
53 // NOTE: This is needed in order to make CComObject work since this is not a real ATL project.\r
54 CComModule _AtlModule;\r
55 extern __declspec(selectany) CAtlModule* _pAtlModule = &_AtlModule;\r
56 \r
57 struct flash_producer::implementation\r
58 {       \r
59         implementation(flash_producer* self, const std::wstring& filename) \r
60                 : flashax_container_(nullptr), filename_(filename), self_(self), executor_([=]{run();}), invalid_count_(0)\r
61         {       \r
62                 if(!boost::filesystem::exists(filename))\r
63                         BOOST_THROW_EXCEPTION(file_not_found() << boost::errinfo_file_name(common::narrow(filename)));\r
64 \r
65                 frame_buffer_.set_capacity(3);          \r
66         }\r
67 \r
68         ~implementation() \r
69         {\r
70                 stop();\r
71         }\r
72 \r
73         void start(bool force = true)\r
74         {               \r
75                 if(executor_.is_running() && !force)\r
76                         return;\r
77 \r
78                 try\r
79                 {\r
80                         is_empty_ = true;\r
81                         executor_.stop(); // Restart if running\r
82                         executor_.start();\r
83                         executor_.invoke([=]()\r
84                         {\r
85                                 if(FAILED(CComObject<FlashAxContainer>::CreateInstance(&flashax_container_)) || \r
86                                                         flashax_container_ == nullptr)\r
87                                         BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("Failed to create FlashAxContainer"));\r
88                 \r
89                                 flashax_container_->pflash_producer_ = self_;\r
90                                 CComPtr<IShockwaveFlash> spFlash;\r
91 \r
92                                 if(FAILED(flashax_container_->CreateAxControl()))\r
93                                         BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("Failed to Create FlashAxControl"));\r
94 \r
95                                 if(FAILED(flashax_container_->QueryControl(&spFlash)))\r
96                                         BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("Failed to Query FlashAxControl"));\r
97                                                                                                 \r
98                                 if(FAILED(spFlash->put_Playing(true)) )\r
99                                         BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("Failed to start playing Flash"));\r
100 \r
101                                 if(FAILED(spFlash->put_Movie(CComBSTR(filename_.c_str()))))\r
102                                         BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("Failed to Load Template Host"));\r
103                                                 \r
104                                 //Exact fit. Scale without respect to the aspect ratio.\r
105                                 if(FAILED(spFlash->put_ScaleMode(2))) \r
106                                         BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("Failed to Set Scale Mode"));\r
107                                                                                                 \r
108                                 // stop if failed\r
109                                 if(FAILED(flashax_container_->SetFormat(frame_processor_->get_video_format_desc()))) \r
110                                         BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("Failed to Set Format"));\r
111 \r
112                                 current_frame_ = nullptr; // Force re-render of current frame                   \r
113                         });\r
114                 }\r
115                 catch(...)\r
116                 {\r
117                         stop();\r
118                         CASPAR_LOG_CURRENT_EXCEPTION();\r
119                         throw;\r
120                 }\r
121         }\r
122 \r
123         void stop()\r
124         {\r
125                 is_empty_ = true;\r
126                 if(executor_.is_running())\r
127                 {\r
128                         frame_buffer_.clear();\r
129                         executor_.stop();\r
130                 }\r
131         }\r
132 \r
133         void param(const std::wstring& param) \r
134         {       \r
135                 if(!executor_.is_running())\r
136                 {\r
137                         try\r
138                         {\r
139                                 start();\r
140                         }\r
141                         catch(caspar_exception& e)\r
142                         {\r
143                                 e << msg_info("Flashproducer failed to recover from failure.");\r
144                                 throw e;\r
145                         }\r
146                 }\r
147 \r
148                 executor_.invoke([&]()\r
149                 {                       \r
150                         for(size_t retries = 0; !flashax_container_->CallFunction(param); ++retries)\r
151                         {\r
152                                 CASPAR_LOG(debug) << "Retrying. Count: " << retries;\r
153                                 if(retries > 3)\r
154                                         BOOST_THROW_EXCEPTION(operation_failed() << arg_name_info("param") << arg_value_info(common::narrow(param)));\r
155                         }\r
156                         is_empty_ = false;      \r
157                 });\r
158         }\r
159         \r
160         void run()\r
161         {\r
162                 win32_exception::install_handler();\r
163                 CASPAR_LOG(info) << L"started flash_producer Thread";\r
164 \r
165                 try\r
166                 {\r
167                         ::OleInitialize(nullptr);\r
168                         CASPAR_SCOPE_EXIT(::OleUninitialize);\r
169 \r
170                         CASPAR_SCOPE_EXIT([=]\r
171                         {\r
172                                 if(flashax_container_ != nullptr)\r
173                                 {\r
174                                         flashax_container_->DestroyAxControl();\r
175                                         flashax_container_->Release();\r
176                                         flashax_container_ = nullptr;\r
177                                 }\r
178                         });\r
179 \r
180                         CASPAR_SCOPE_EXIT([=]\r
181                         {                                               \r
182                                 stop();\r
183 \r
184                                 frame_buffer_.clear();\r
185                                 frame_buffer_.try_push(nullptr); // EOF\r
186                 \r
187                                 current_frame_ = nullptr;\r
188                         });\r
189 \r
190                         while(executor_.is_running())\r
191                         {       \r
192                                 if(!is_empty_)\r
193                                 {\r
194                                         render();       \r
195                                         while(executor_.try_execute()){}\r
196                                 }\r
197                                 else\r
198                                 {\r
199                                         executor_.execute();\r
200                                         while(executor_.try_execute()){}\r
201                                 }\r
202                         }\r
203                 }\r
204                 catch(...)\r
205                 {\r
206                         CASPAR_LOG_CURRENT_EXCEPTION();\r
207                 }\r
208                 \r
209                 CASPAR_LOG(info) << L"Ended flash_producer Thread";\r
210         }\r
211 \r
212         void render()\r
213         {\r
214                 assert(flashax_container_);\r
215                 \r
216                 if(!is_empty_ || current_frame_ == nullptr)\r
217                 {\r
218                         if(!flashax_container_->IsReadyToRender())\r
219                         {\r
220                                 CASPAR_LOG(trace) << "Flash Producer Underflow";\r
221                                 boost::thread::yield();\r
222                                 return;\r
223                         }\r
224 \r
225                         auto format_desc = frame_processor_->get_video_format_desc();\r
226                         bool is_progressive = format_desc.mode == video_mode::progressive || (flashax_container_->GetFPS() - format_desc.fps/2 == 0);\r
227 \r
228                         frame_ptr result;\r
229 \r
230                         if(is_progressive)                                                      \r
231                                 result = do_render_frame();             \r
232                         else\r
233                         {\r
234                                 frame_ptr frame1 = do_render_frame();\r
235                                 frame_ptr frame2 = do_render_frame();\r
236                                 result = composite_frame::interlace(frame1, frame2, format_desc.mode);\r
237                         }\r
238 \r
239                         frame_buffer_.push(result);\r
240                         is_empty_ = flashax_container_->IsEmpty();\r
241                 }\r
242         }\r
243                 \r
244         frame_ptr do_render_frame()\r
245         {\r
246                 auto format_desc = frame_processor_->get_video_format_desc();\r
247 \r
248                 flashax_container_->Tick();\r
249                 invalid_count_ = !flashax_container_->InvalidRectangle() ? std::min(2, invalid_count_+1) : 0;\r
250                 if(current_frame_ == nullptr || invalid_count_ < 2)\r
251                 {                               \r
252                         std::fill_n(bmp_frame_->data(), bmp_frame_->size(), 0);                 \r
253                         flashax_container_->DrawControl(bmp_frame_->hdc());\r
254                         current_frame_ = bmp_frame_;\r
255                 }       \r
256 \r
257                 auto frame = frame_processor_->create_frame(format_desc.width, format_desc.height);\r
258                 std::copy(current_frame_->data(), current_frame_->data() + current_frame_->size(), frame->data().begin());\r
259                 return frame;\r
260         }\r
261                 \r
262         frame_ptr render_frame()\r
263         {               \r
264                 return (frame_buffer_.try_pop(last_frame_) || !is_empty_) && last_frame_ ? last_frame_ : frame::empty();\r
265         }\r
266 \r
267         void initialize(const frame_processor_device_ptr& frame_processor)\r
268         {\r
269                 frame_processor_ = frame_processor;\r
270                 auto format_desc = frame_processor_->get_video_format_desc();\r
271                 bmp_frame_ = std::make_shared<bitmap>(format_desc.width, format_desc.height);\r
272                 start(false);\r
273         }\r
274 \r
275         std::wstring print() const\r
276         {\r
277                 return L"flash_producer. filename: " + filename_;\r
278         }\r
279         \r
280         CComObject<flash::FlashAxContainer>* flashax_container_;\r
281                 \r
282         tbb::concurrent_bounded_queue<frame_ptr> frame_buffer_;\r
283 \r
284         frame_ptr last_frame_;\r
285 \r
286         bitmap_ptr current_frame_;\r
287         bitmap_ptr bmp_frame_;\r
288 \r
289         std::wstring filename_;\r
290         flash_producer* self_;\r
291 \r
292         tbb::atomic<bool> is_empty_;\r
293         common::executor executor_;\r
294         int invalid_count_;\r
295 \r
296         frame_processor_device_ptr frame_processor_;\r
297 };\r
298 \r
299 flash_producer::flash_producer(const std::wstring& filename) : impl_(new implementation(this, filename)){}\r
300 frame_ptr flash_producer::render_frame(){return impl_->render_frame();}\r
301 void flash_producer::param(const std::wstring& param){impl_->param(param);}\r
302 void flash_producer::initialize(const frame_processor_device_ptr& frame_processor) { impl_->initialize(frame_processor);}\r
303 std::wstring flash_producer::print() const {return impl_->print();}\r
304 \r
305 std::wstring flash_producer::find_template(const std::wstring& template_name)\r
306 {\r
307         if(boost::filesystem::exists(template_name + L".ft")) \r
308                 return template_name + L".ft";\r
309         \r
310         if(boost::filesystem::exists(template_name + L".ct"))\r
311                 return template_name + L".ct";\r
312 \r
313         return L"";\r
314 }\r
315 \r
316 flash_producer_ptr create_flash_producer(const std::vector<std::wstring>& params)\r
317 {\r
318         static const std::vector<std::wstring> extensions = list_of(L"swf");\r
319         std::wstring filename = server::media_folder() + L"\\" + params[0];\r
320         \r
321         auto ext = std::find_if(extensions.begin(), extensions.end(), [&](const std::wstring& ex) -> bool\r
322                 {                                       \r
323                         return boost::filesystem::is_regular_file(boost::filesystem::wpath(filename).replace_extension(ex));\r
324                 });\r
325 \r
326         if(ext == extensions.end())\r
327                 return nullptr;\r
328 \r
329         return std::make_shared<flash_producer>(filename + L"." + *ext);\r
330 }\r
331 \r
332 }}}