2 * copyright (c) 2010 Sveriges Television AB <info@casparcg.com>
\r
4 * This file is part of CasparCG.
\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
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
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
21 #include "..\..\stdafx.h"
\r
23 #if defined(_MSC_VER)
\r
24 #pragma warning (disable : 4714) // marked as __forceinline not inlined
\r
27 #include "flash_producer.h"
\r
28 #include "FlashAxContainer.h"
\r
29 #include "TimerHelper.h"
\r
32 #include "../../format/video_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
39 #include "../../processor/frame.h"
\r
40 #include "../../processor/composite_frame.h"
\r
42 #include <boost/assign.hpp>
\r
43 #include <boost/filesystem.hpp>
\r
44 #include <boost/thread.hpp>
\r
46 #include <tbb/atomic.h>
\r
47 #include <tbb/concurrent_queue.h>
\r
49 #include <type_traits>
\r
51 namespace caspar { namespace core { namespace flash {
\r
53 using namespace boost::assign;
\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
59 struct flash_producer::implementation
\r
61 implementation(flash_producer* self, const std::wstring& filename)
\r
62 : flashax_container_(nullptr), filename_(filename), self_(self),
\r
63 bitmap_pool_(new bitmap_pool), executor_([=]{run();}), invalid_count_(0)
\r
65 if(!boost::filesystem::exists(filename))
\r
66 BOOST_THROW_EXCEPTION(file_not_found() << boost::errinfo_file_name(common::narrow(filename)));
\r
68 frame_buffer_.set_capacity(flash_producer::DEFAULT_BUFFER_SIZE);
\r
74 if(frame_processor_)
\r
75 frame_processor_->release_tag(this);
\r
78 void start(bool force = true)
\r
80 if(executor_.is_running() && !force)
\r
86 executor_.stop(); // Restart if running
\r
88 executor_.invoke([=]()
\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
94 flashax_container_->pflash_producer_ = self_;
\r
95 CComPtr<IShockwaveFlash> spFlash;
\r
97 if(FAILED(flashax_container_->CreateAxControl()))
\r
98 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("Failed to Create FlashAxControl"));
\r
100 if(FAILED(flashax_container_->QueryControl(&spFlash)))
\r
101 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("Failed to Query FlashAxControl"));
\r
103 if(FAILED(spFlash->put_Playing(true)) )
\r
104 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("Failed to start playing Flash"));
\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
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
114 if(FAILED(flashax_container_->SetFormat(frame_processor_->get_video_format_desc())))
\r
115 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("Failed to Set Format"));
\r
117 current_frame_ = nullptr; // Force re-render of current frame
\r
123 CASPAR_LOG_CURRENT_EXCEPTION();
\r
131 if(executor_.is_running())
\r
133 frame_buffer_.clear();
\r
138 void param(const std::wstring& param)
\r
140 if(!executor_.is_running())
\r
146 catch(caspar_exception& e)
\r
148 e << msg_info("Flashproducer failed to recover from failure.");
\r
153 executor_.invoke([&]()
\r
155 for(size_t retries = 0; !flashax_container_->CallFunction(param); ++retries)
\r
157 CASPAR_LOG(debug) << "Retrying. Count: " << retries;
\r
159 BOOST_THROW_EXCEPTION(operation_failed() << warg_name_info(L"param") << warg_value_info(param));
\r
161 is_empty_ = false;
\r
167 win32_exception::install_handler();
\r
168 CASPAR_LOG(info) << L"started flash_producer Thread";
\r
172 ::OleInitialize(nullptr);
\r
173 CASPAR_SCOPE_EXIT(::OleUninitialize);
\r
175 CASPAR_SCOPE_EXIT([=]
\r
177 if(flashax_container_ != nullptr)
\r
179 flashax_container_->DestroyAxControl();
\r
180 flashax_container_->Release();
\r
181 flashax_container_ = nullptr;
\r
185 CASPAR_SCOPE_EXIT([=]
\r
189 frame_buffer_.clear();
\r
190 frame_buffer_.try_push(nullptr); // EOF
\r
192 current_frame_ = nullptr;
\r
195 while(executor_.is_running())
\r
200 while(executor_.try_execute()){}
\r
204 executor_.execute();
\r
205 while(executor_.try_execute()){}
\r
211 CASPAR_LOG_CURRENT_EXCEPTION();
\r
214 CASPAR_LOG(info) << L"Ended flash_producer Thread";
\r
219 assert(flashax_container_);
\r
221 if(!is_empty_ || current_frame_ == nullptr)
\r
223 if(!flashax_container_->IsReadyToRender())
\r
225 CASPAR_LOG(trace) << "Flash Producer Underflow";
\r
226 boost::thread::yield();
\r
230 auto format_desc = frame_processor_->get_video_format_desc();
\r
231 bool is_progressive = format_desc.update == video_update_format::progressive || (flashax_container_->GetFPS() - format_desc.fps/2 == 0);
\r
235 if(is_progressive)
\r
236 result = do_render_frame();
\r
239 frame_ptr frame1 = do_render_frame();
\r
240 frame_ptr frame2 = do_render_frame();
\r
241 result = composite_frame::interlace(frame1, frame2, format_desc.update);
\r
244 frame_buffer_.push(result);
\r
245 is_empty_ = flashax_container_->IsEmpty();
\r
249 frame_ptr do_render_frame()
\r
251 auto format_desc = frame_processor_->get_video_format_desc();
\r
253 flashax_container_->Tick();
\r
254 invalid_count_ = !flashax_container_->InvalidRectangle() ? std::min(2, invalid_count_+1) : 0;
\r
255 if(current_frame_ == nullptr || invalid_count_ < 2)
\r
258 if(!bitmap_pool_->try_pop(frame))
\r
260 CASPAR_LOG(trace) << "Allocated bitmap";
\r
261 frame = std::make_shared<bitmap>(format_desc.width, format_desc.height);
\r
262 common::clear(frame->data(), frame->size());
\r
264 flashax_container_->DrawControl(frame->hdc());
\r
266 auto pool = bitmap_pool_;
\r
267 current_frame_.reset(frame.get(), [=](bitmap*)
\r
269 if(pool->try_push(frame))
\r
270 common::clear(frame->data(), frame->size());
\r
274 auto frame = frame_processor_->create_frame(format_desc.width, format_desc.height, this);
\r
275 common::aligned_parallel_memcpy(frame->data(), current_frame_->data(), current_frame_->size());
\r
280 frame_ptr render_frame()
\r
282 if(!frame_buffer_.try_pop(last_frame_) && is_empty_)
\r
283 return frame::empty();
\r
285 return last_frame_;
\r
288 void initialize(const frame_processor_device_ptr& frame_processor)
\r
290 frame_processor_ = frame_processor;
\r
294 typedef tbb::concurrent_bounded_queue<bitmap_ptr> bitmap_pool;
\r
295 std::shared_ptr<bitmap_pool> bitmap_pool_;
\r
297 CComObject<flash::FlashAxContainer>* flashax_container_;
\r
299 tbb::concurrent_bounded_queue<frame_ptr> frame_buffer_;
\r
300 frame_ptr last_frame_;
\r
301 bitmap_ptr current_frame_;
\r
303 std::wstring filename_;
\r
304 flash_producer* self_;
\r
306 tbb::atomic<bool> is_empty_;
\r
307 common::executor executor_;
\r
308 int invalid_count_;
\r
310 frame_processor_device_ptr frame_processor_;
\r
313 flash_producer::flash_producer(const std::wstring& filename) : impl_(new implementation(this, filename)){}
\r
314 frame_ptr flash_producer::render_frame(){return impl_->render_frame();}
\r
315 void flash_producer::param(const std::wstring& param){impl_->param(param);}
\r
316 void flash_producer::initialize(const frame_processor_device_ptr& frame_processor) { impl_->initialize(frame_processor);}
\r
318 std::wstring flash_producer::find_template(const std::wstring& template_name)
\r
320 if(boost::filesystem::exists(template_name + L".ft"))
\r
321 return template_name + L".ft";
\r
323 if(boost::filesystem::exists(template_name + L".ct"))
\r
324 return template_name + L".ct";
\r
329 flash_producer_ptr create_flash_producer(const std::vector<std::wstring>& params)
\r
331 // TODO: Check for flash support
\r
332 auto filename = params[0];
\r
333 std::wstring result_filename = common::find_file(server::media_folder() + filename, list_of(L"swf"));
\r
335 return result_filename.empty() ? nullptr : std::make_shared<flash_producer>(result_filename);
\r