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