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 "../../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
40 #include "../../frame/gpu_frame.h"
\r
41 #include "../../frame/gpu_composite_frame.h"
\r
43 #include <boost/assign.hpp>
\r
44 #include <boost/filesystem.hpp>
\r
45 #include <boost/thread.hpp>
\r
47 #include <tbb/atomic.h>
\r
48 #include <tbb/concurrent_queue.h>
\r
50 #include <type_traits>
\r
52 namespace caspar { namespace core { namespace flash {
\r
54 using namespace boost::assign;
\r
56 // NOTE: 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
60 struct flash_producer::implementation
\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
66 if(!boost::filesystem::exists(filename))
\r
67 BOOST_THROW_EXCEPTION(file_not_found() << boost::errinfo_file_name(common::narrow(filename)));
\r
69 frame_buffer_.set_capacity(flash_producer::DEFAULT_BUFFER_SIZE);
\r
77 void start(bool force = true)
\r
79 if(executor_.is_running() && !force)
\r
85 executor_.stop(); // Restart if running
\r
87 executor_.invoke([=]()
\r
89 if(FAILED(CComObject<FlashAxContainer>::CreateInstance(&flashax_container_)) ||
\r
90 flashax_container_ == nullptr)
\r
91 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("Failed to create FlashAxContainer"));
\r
93 flashax_container_->pflash_producer_ = self_;
\r
94 CComPtr<IShockwaveFlash> spFlash;
\r
96 if(FAILED(flashax_container_->CreateAxControl()))
\r
97 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("Failed to Create FlashAxControl"));
\r
99 if(FAILED(flashax_container_->QueryControl(&spFlash)))
\r
100 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("Failed to Query FlashAxControl"));
\r
102 if(FAILED(spFlash->put_Playing(true)) )
\r
103 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("Failed to start playing Flash"));
\r
105 if(FAILED(spFlash->put_Movie(CComBSTR(filename_.c_str()))))
\r
106 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("Failed to Load Template Host"));
\r
108 //Exact fit. Scale without respect to the aspect ratio.
\r
109 if(FAILED(spFlash->put_ScaleMode(2)))
\r
110 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("Failed to Set Scale Mode"));
\r
113 if(FAILED(flashax_container_->SetFormat(format_desc_)))
\r
114 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("Failed to Set Format"));
\r
116 current_frame_ = nullptr; // Force re-render of current frame
\r
122 CASPAR_LOG_CURRENT_EXCEPTION();
\r
130 if(executor_.is_running())
\r
132 frame_buffer_.clear();
\r
137 void param(const std::wstring& param)
\r
139 if(!executor_.is_running())
\r
145 catch(caspar_exception& e)
\r
147 e << msg_info("Flashproducer failed to recover from failure.");
\r
152 executor_.invoke([&]()
\r
154 for(size_t retries = 0; !flashax_container_->CallFunction(param); ++retries)
\r
156 CASPAR_LOG(debug) << "Retrying. Count: " << retries;
\r
158 BOOST_THROW_EXCEPTION(operation_failed() << warg_name_info(L"param") << warg_value_info(param));
\r
160 is_empty_ = false;
\r
166 win32_exception::install_handler();
\r
167 CASPAR_LOG(info) << L"started flash_producer Thread";
\r
171 ::OleInitialize(nullptr);
\r
172 CASPAR_SCOPE_EXIT(::OleUninitialize);
\r
174 CASPAR_SCOPE_EXIT([=]
\r
176 if(flashax_container_ != nullptr)
\r
178 flashax_container_->DestroyAxControl();
\r
179 flashax_container_->Release();
\r
180 flashax_container_ = nullptr;
\r
184 CASPAR_SCOPE_EXIT([=]
\r
188 frame_buffer_.clear();
\r
189 frame_buffer_.try_push(nullptr); // EOF
\r
191 current_frame_ = nullptr;
\r
194 while(executor_.is_running())
\r
199 while(executor_.try_execute()){}
\r
203 executor_.execute();
\r
204 while(executor_.try_execute()){}
\r
210 CASPAR_LOG_CURRENT_EXCEPTION();
\r
213 CASPAR_LOG(info) << L"Ended flash_producer Thread";
\r
218 assert(flashax_container_);
\r
220 if(!is_empty_ || current_frame_ == nullptr)
\r
222 if(!flashax_container_->IsReadyToRender())
\r
224 CASPAR_LOG(trace) << "Flash Producer Underflow";
\r
225 boost::thread::yield();
\r
229 bool is_progressive = format_desc_.mode == video_mode::progressive || (flashax_container_->GetFPS() - format_desc_.fps/2 == 0);
\r
231 gpu_frame_ptr result;
\r
233 if(is_progressive)
\r
234 result = render_frame();
\r
237 gpu_frame_ptr frame1 = render_frame();
\r
238 gpu_frame_ptr frame2 = render_frame();
\r
239 result = gpu_composite_frame::interlace(frame1, frame2, format_desc_.mode);
\r
242 frame_buffer_.push(result);
\r
243 is_empty_ = flashax_container_->IsEmpty();
\r
247 gpu_frame_ptr render_frame()
\r
249 flashax_container_->Tick();
\r
250 invalid_count_ = !flashax_container_->InvalidRectangle() ? std::min(2, invalid_count_+1) : 0;
\r
251 if(current_frame_ == nullptr || invalid_count_ < 2)
\r
254 if(!bitmap_pool_->try_pop(frame))
\r
256 CASPAR_LOG(trace) << "Allocated bitmap";
\r
257 frame = std::make_shared<bitmap>(format_desc_.width, format_desc_.height);
\r
258 common::clear(frame->data(), frame->size());
\r
260 flashax_container_->DrawControl(frame->hdc());
\r
262 auto pool = bitmap_pool_;
\r
263 current_frame_.reset(frame.get(), [=](bitmap*)
\r
265 if(pool->try_push(frame))
\r
266 common::clear(frame->data(), frame->size());
\r
270 auto frame = factory_->create_frame(format_desc_);
\r
271 common::aligned_memcpy(frame->data(), current_frame_->data(), current_frame_->size());
\r
276 gpu_frame_ptr get_frame()
\r
278 if(!frame_buffer_.try_pop(last_frame_) && is_empty_)
\r
279 return gpu_frame::null();
\r
281 return last_frame_;
\r
284 void initialize(const frame_factory_ptr& factory)
\r
286 factory_ = factory;
\r
290 typedef tbb::concurrent_bounded_queue<bitmap_ptr> bitmap_pool;
\r
291 std::shared_ptr<bitmap_pool> bitmap_pool_;
\r
292 frame_format_desc format_desc_;
\r
294 CComObject<flash::FlashAxContainer>* flashax_container_;
\r
296 tbb::concurrent_bounded_queue<gpu_frame_ptr> frame_buffer_;
\r
297 gpu_frame_ptr last_frame_;
\r
298 bitmap_ptr current_frame_;
\r
300 std::wstring filename_;
\r
301 flash_producer* self_;
\r
303 tbb::atomic<bool> is_empty_;
\r
304 common::executor executor_;
\r
305 int invalid_count_;
\r
307 frame_factory_ptr factory_;
\r
310 flash_producer::flash_producer(const std::wstring& filename, const frame_format_desc& format_desc) : impl_(new implementation(this, filename, format_desc)){}
\r
311 gpu_frame_ptr flash_producer::get_frame(){return impl_->get_frame();}
\r
312 void flash_producer::param(const std::wstring& param){impl_->param(param);}
\r
313 const frame_format_desc& flash_producer::get_frame_format_desc() const { return impl_->format_desc_; }
\r
314 void flash_producer::initialize(const frame_factory_ptr& factory) { impl_->initialize(factory);}
\r
316 std::wstring flash_producer::find_template(const std::wstring& template_name)
\r
318 if(boost::filesystem::exists(template_name + TEXT(".ft")))
\r
319 return template_name + TEXT(".ft");
\r
321 if(boost::filesystem::exists(template_name + TEXT(".ct")))
\r
322 return template_name + TEXT(".ct");
\r
327 flash_producer_ptr create_flash_producer(const std::vector<std::wstring>& params, const frame_format_desc& format_desc)
\r
329 // TODO: Check for flash support
\r
330 auto filename = params[0];
\r
331 std::wstring result_filename = common::find_file(server::media_folder() + filename,
\r
334 return result_filename.empty() ? nullptr : std::make_shared<flash_producer>(result_filename, format_desc);
\r