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/image/image.h"
\r
38 #include "../../../common/utility/scope_exit.h"
\r
40 #include <boost/assign.hpp>
\r
41 #include <boost/filesystem.hpp>
\r
42 #include <boost/thread.hpp>
\r
44 #include <tbb/atomic.h>
\r
45 #include <tbb/concurrent_queue.h>
\r
47 #include <type_traits>
\r
49 namespace caspar { namespace flash {
\r
51 using namespace boost::assign;
\r
53 // 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
57 struct flash_producer::implementation
\r
59 implementation(flash_producer* self, const std::wstring& filename, const frame_format_desc& format_desc)
\r
60 : flashax_container_(nullptr), filename_(filename), self_(self), format_desc_(format_desc),
\r
61 bitmap_pool_(new bitmap_pool), executor_([=]{run();}), invalid_count_(0)
\r
63 if(!boost::filesystem::exists(filename))
\r
64 BOOST_THROW_EXCEPTION(file_not_found() << boost::errinfo_file_name(common::narrow(filename)));
\r
66 frame_buffer_.set_capacity(flash_producer::DEFAULT_BUFFER_SIZE);
\r
67 last_frame_ = std::make_shared<bitmap>(format_desc_.width, format_desc_.height);
\r
82 executor_.stop(); // Restart if running
\r
84 executor_.invoke([=]()
\r
86 if(FAILED(CComObject<FlashAxContainer>::CreateInstance(&flashax_container_)) ||
\r
87 flashax_container_ == nullptr)
\r
88 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("Failed to create FlashAxContainer"));
\r
90 flashax_container_->pflash_producer_ = self_;
\r
91 CComPtr<IShockwaveFlash> spFlash;
\r
93 if(FAILED(flashax_container_->CreateAxControl()))
\r
94 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("Failed to Create FlashAxControl"));
\r
96 if(FAILED(flashax_container_->QueryControl(&spFlash)))
\r
97 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("Failed to Query FlashAxControl"));
\r
99 if(FAILED(spFlash->put_Playing(true)) )
\r
100 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("Failed to start playing Flash"));
\r
102 if(FAILED(spFlash->put_Movie(CComBSTR(filename_.c_str()))))
\r
103 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("Failed to Load Template Host"));
\r
105 //Exact fit. Scale without respect to the aspect ratio.
\r
106 if(FAILED(spFlash->put_ScaleMode(2)))
\r
107 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("Failed to Set Scale Mode"));
\r
110 if(FAILED(flashax_container_->SetFormat(format_desc_)))
\r
111 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("Failed to Set Format"));
\r
113 current_frame_ = nullptr; // Force re-render of current frame
\r
119 CASPAR_LOG_CURRENT_EXCEPTION();
\r
127 if(executor_.is_running())
\r
129 frame_buffer_.clear();
\r
134 void param(const std::wstring& param)
\r
136 if(!executor_.is_running())
\r
142 catch(caspar_exception& e)
\r
144 e << msg_info("Flashproducer failed to recover from failure.");
\r
149 executor_.invoke([&]()
\r
151 for(size_t retries = 0; !flashax_container_->CallFunction(param); ++retries)
\r
153 CASPAR_LOG(debug) << "Retrying. Count: " << retries;
\r
155 BOOST_THROW_EXCEPTION(operation_failed() << warg_name_info(L"param") << warg_value_info(param));
\r
157 is_empty_ = false;
\r
163 win32_exception::install_handler();
\r
164 CASPAR_LOG(info) << L"started flash_producer Thread";
\r
168 ::OleInitialize(nullptr);
\r
169 CASPAR_SCOPE_EXIT(::OleUninitialize);
\r
171 CASPAR_SCOPE_EXIT([=]
\r
173 if(flashax_container_ != nullptr)
\r
175 flashax_container_->DestroyAxControl();
\r
176 flashax_container_->Release();
\r
177 flashax_container_ = nullptr;
\r
181 CASPAR_SCOPE_EXIT([=]
\r
185 frame_buffer_.clear();
\r
186 frame_buffer_.try_push(nullptr); // EOF
\r
188 current_frame_ = nullptr;
\r
191 while(executor_.is_running())
\r
196 while(executor_.try_execute()){}
\r
200 executor_.execute();
\r
201 while(executor_.try_execute()){}
\r
207 CASPAR_LOG_CURRENT_EXCEPTION();
\r
210 CASPAR_LOG(info) << L"Ended flash_producer Thread";
\r
215 assert(flashax_container_);
\r
217 if(!is_empty_ || current_frame_ == nullptr)
\r
219 if(!flashax_container_->IsReadyToRender())
\r
221 CASPAR_LOG(trace) << "Flash Producer Underflow";
\r
222 boost::thread::yield();
\r
226 bool isProgressive = format_desc_.mode == video_mode::progressive || (flashax_container_->GetFPS() - format_desc_.fps/2 == 0);
\r
227 frame_buffer_.push(isProgressive ? render_frame() : render_interlace_frame());
\r
228 is_empty_ = flashax_container_->IsEmpty();
\r
232 bitmap_ptr render_interlace_frame()
\r
234 bitmap_ptr frame1 = render_frame();
\r
235 bitmap_ptr frame2 = render_frame();
\r
236 common::image::copy(frame1->data(), frame2->data(), frame1->size());
\r
240 bitmap_ptr render_frame()
\r
242 flashax_container_->Tick();
\r
243 invalid_count_ = !flashax_container_->InvalidRectangle() ? std::min(2, invalid_count_+1) : 0;
\r
244 if(current_frame_ == nullptr || invalid_count_ < 2)
\r
247 if(!bitmap_pool_->try_pop(frame))
\r
249 CASPAR_LOG(trace) << "Allocated bitmap";
\r
250 frame = std::make_shared<bitmap>(format_desc_.width, format_desc_.height);
\r
251 common::image::clear(frame->data(), frame->size());
\r
253 flashax_container_->DrawControl(frame->hdc());
\r
255 auto pool = bitmap_pool_;
\r
256 current_frame_.reset(frame.get(), [=](bitmap*)
\r
258 common::function_task::enqueue([=]
\r
260 if(pool->try_push(frame))
\r
261 common::image::clear(frame->data(), frame->size());
\r
265 return current_frame_;
\r
268 gpu_frame_ptr get_frame()
\r
270 if(!frame_buffer_.try_pop(last_frame_) && is_empty_)
\r
271 return gpu_frame::null();
\r
273 auto frame = factory_->create_frame(format_desc_);
\r
274 common::image::copy(frame->data(), last_frame_->data(), last_frame_->size());
\r
279 void initialize(const frame_factory_ptr& factory)
\r
281 factory_ = factory;
\r
284 typedef tbb::concurrent_bounded_queue<bitmap_ptr> bitmap_pool;
\r
285 std::shared_ptr<bitmap_pool> bitmap_pool_;
\r
286 frame_format_desc format_desc_;
\r
288 CComObject<caspar::flash::FlashAxContainer>* flashax_container_;
\r
290 tbb::concurrent_bounded_queue<bitmap_ptr> frame_buffer_;
\r
291 bitmap_ptr last_frame_;
\r
292 bitmap_ptr current_frame_;
\r
294 std::wstring filename_;
\r
295 flash_producer* self_;
\r
297 tbb::atomic<bool> is_empty_;
\r
298 common::executor executor_;
\r
299 int invalid_count_;
\r
301 frame_factory_ptr factory_;
\r
304 flash_producer::flash_producer(const std::wstring& filename, const frame_format_desc& format_desc) : impl_(new implementation(this, filename, format_desc)){}
\r
305 gpu_frame_ptr flash_producer::get_frame(){return impl_->get_frame();}
\r
306 void flash_producer::param(const std::wstring& param){impl_->param(param);}
\r
307 const frame_format_desc& flash_producer::get_frame_format_desc() const { return impl_->format_desc_; }
\r
308 void flash_producer::initialize(const frame_factory_ptr& factory) { impl_->initialize(factory);}
\r
310 std::wstring flash_producer::find_template(const std::wstring& template_name)
\r
312 if(boost::filesystem::exists(template_name + TEXT(".ft")))
\r
313 return template_name + TEXT(".ft");
\r
315 if(boost::filesystem::exists(template_name + TEXT(".ct")))
\r
316 return template_name + TEXT(".ct");
\r
321 flash_producer_ptr create_flash_producer(const std::vector<std::wstring>& params, const frame_format_desc& format_desc)
\r
323 // TODO: Check for flash support
\r
324 auto filename = params[0];
\r
325 std::wstring result_filename = common::find_file(server::media_folder() + filename,
\r
328 return result_filename.empty() ? nullptr : std::make_shared<flash_producer>(result_filename, format_desc);
\r