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