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 "../../server.h"
\r
34 #include "../../../common/concurrency/executor.h"
\r
35 #include "../../../common/utility/scope_exit.h"
\r
37 #include "../../processor/frame.h"
\r
38 #include "../../processor/composite_frame.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 core { namespace flash {
\r
51 using namespace boost::assign;
\r
53 // NOTE: 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)
\r
60 : flashax_container_(nullptr), filename_(filename), self_(self), executor_([=]{run();}), invalid_count_(0)
\r
62 if(!boost::filesystem::exists(filename))
\r
63 BOOST_THROW_EXCEPTION(file_not_found() << boost::errinfo_file_name(common::narrow(filename)));
\r
65 frame_buffer_.set_capacity(3);
\r
73 void start(bool force = true)
\r
75 if(executor_.is_running() && !force)
\r
81 executor_.stop(); // Restart if running
\r
83 executor_.invoke([=]()
\r
85 if(FAILED(CComObject<FlashAxContainer>::CreateInstance(&flashax_container_)) ||
\r
86 flashax_container_ == nullptr)
\r
87 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("Failed to create FlashAxContainer"));
\r
89 flashax_container_->pflash_producer_ = self_;
\r
90 CComPtr<IShockwaveFlash> spFlash;
\r
92 if(FAILED(flashax_container_->CreateAxControl()))
\r
93 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("Failed to Create FlashAxControl"));
\r
95 if(FAILED(flashax_container_->QueryControl(&spFlash)))
\r
96 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("Failed to Query FlashAxControl"));
\r
98 if(FAILED(spFlash->put_Playing(true)) )
\r
99 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("Failed to start playing Flash"));
\r
101 if(FAILED(spFlash->put_Movie(CComBSTR(filename_.c_str()))))
\r
102 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("Failed to Load Template Host"));
\r
104 //Exact fit. Scale without respect to the aspect ratio.
\r
105 if(FAILED(spFlash->put_ScaleMode(2)))
\r
106 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("Failed to Set Scale Mode"));
\r
109 if(FAILED(flashax_container_->SetFormat(frame_processor_->get_video_format_desc())))
\r
110 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("Failed to Set Format"));
\r
112 current_frame_ = nullptr; // Force re-render of current frame
\r
118 CASPAR_LOG_CURRENT_EXCEPTION();
\r
126 if(executor_.is_running())
\r
128 frame_buffer_.clear();
\r
133 void param(const std::wstring& param)
\r
135 if(!executor_.is_running())
\r
141 catch(caspar_exception& e)
\r
143 e << msg_info("Flashproducer failed to recover from failure.");
\r
148 executor_.invoke([&]()
\r
150 for(size_t retries = 0; !flashax_container_->CallFunction(param); ++retries)
\r
152 CASPAR_LOG(debug) << "Retrying. Count: " << retries;
\r
154 BOOST_THROW_EXCEPTION(operation_failed() << arg_name_info("param") << arg_value_info(common::narrow(param)));
\r
156 is_empty_ = false;
\r
162 win32_exception::install_handler();
\r
163 CASPAR_LOG(info) << L"started flash_producer Thread";
\r
167 ::OleInitialize(nullptr);
\r
168 CASPAR_SCOPE_EXIT(::OleUninitialize);
\r
170 CASPAR_SCOPE_EXIT([=]
\r
172 if(flashax_container_ != nullptr)
\r
174 flashax_container_->DestroyAxControl();
\r
175 flashax_container_->Release();
\r
176 flashax_container_ = nullptr;
\r
180 CASPAR_SCOPE_EXIT([=]
\r
184 frame_buffer_.clear();
\r
185 frame_buffer_.try_push(nullptr); // EOF
\r
187 current_frame_ = nullptr;
\r
190 while(executor_.is_running())
\r
195 while(executor_.try_execute()){}
\r
199 executor_.execute();
\r
200 while(executor_.try_execute()){}
\r
206 CASPAR_LOG_CURRENT_EXCEPTION();
\r
209 CASPAR_LOG(info) << L"Ended flash_producer Thread";
\r
214 assert(flashax_container_);
\r
216 if(!is_empty_ || current_frame_ == nullptr)
\r
218 if(!flashax_container_->IsReadyToRender())
\r
220 CASPAR_LOG(trace) << "Flash Producer Underflow";
\r
221 boost::thread::yield();
\r
225 auto format_desc = frame_processor_->get_video_format_desc();
\r
226 bool is_progressive = format_desc.mode == video_mode::progressive || (flashax_container_->GetFPS() - format_desc.fps/2 == 0);
\r
230 if(is_progressive)
\r
231 result = do_render_frame();
\r
234 frame_ptr frame1 = do_render_frame();
\r
235 frame_ptr frame2 = do_render_frame();
\r
236 result = composite_frame::interlace(frame1, frame2, format_desc.mode);
\r
239 frame_buffer_.push(result);
\r
240 is_empty_ = flashax_container_->IsEmpty();
\r
244 frame_ptr do_render_frame()
\r
246 auto format_desc = frame_processor_->get_video_format_desc();
\r
248 flashax_container_->Tick();
\r
249 invalid_count_ = !flashax_container_->InvalidRectangle() ? std::min(2, invalid_count_+1) : 0;
\r
250 if(current_frame_ == nullptr || invalid_count_ < 2)
\r
252 std::fill_n(bmp_frame_->data(), bmp_frame_->size(), 0);
\r
253 flashax_container_->DrawControl(bmp_frame_->hdc());
\r
254 current_frame_ = bmp_frame_;
\r
257 auto frame = frame_processor_->create_frame(format_desc.width, format_desc.height);
\r
258 std::copy(current_frame_->data(), current_frame_->data() + current_frame_->size(), frame->data().begin());
\r
262 frame_ptr render_frame()
\r
264 return (frame_buffer_.try_pop(last_frame_) || !is_empty_) && last_frame_ ? last_frame_ : frame::empty();
\r
267 void initialize(const frame_processor_device_ptr& frame_processor)
\r
269 frame_processor_ = frame_processor;
\r
270 auto format_desc = frame_processor_->get_video_format_desc();
\r
271 bmp_frame_ = std::make_shared<bitmap>(format_desc.width, format_desc.height);
\r
275 std::wstring print() const
\r
277 return L"flash_producer. filename: " + filename_;
\r
280 CComObject<flash::FlashAxContainer>* flashax_container_;
\r
282 tbb::concurrent_bounded_queue<frame_ptr> frame_buffer_;
\r
284 frame_ptr last_frame_;
\r
286 bitmap_ptr current_frame_;
\r
287 bitmap_ptr bmp_frame_;
\r
289 std::wstring filename_;
\r
290 flash_producer* self_;
\r
292 tbb::atomic<bool> is_empty_;
\r
293 common::executor executor_;
\r
294 int invalid_count_;
\r
296 frame_processor_device_ptr frame_processor_;
\r
299 flash_producer::flash_producer(const std::wstring& filename) : impl_(new implementation(this, filename)){}
\r
300 frame_ptr flash_producer::render_frame(){return impl_->render_frame();}
\r
301 void flash_producer::param(const std::wstring& param){impl_->param(param);}
\r
302 void flash_producer::initialize(const frame_processor_device_ptr& frame_processor) { impl_->initialize(frame_processor);}
\r
303 std::wstring flash_producer::print() const {return impl_->print();}
\r
305 std::wstring flash_producer::find_template(const std::wstring& template_name)
\r
307 if(boost::filesystem::exists(template_name + L".ft"))
\r
308 return template_name + L".ft";
\r
310 if(boost::filesystem::exists(template_name + L".ct"))
\r
311 return template_name + L".ct";
\r
316 flash_producer_ptr create_flash_producer(const std::vector<std::wstring>& params)
\r
318 static const std::vector<std::wstring> extensions = list_of(L"swf");
\r
319 std::wstring filename = server::media_folder() + L"\\" + params[0];
\r
321 auto ext = std::find_if(extensions.begin(), extensions.end(), [&](const std::wstring& ex) -> bool
\r
323 return boost::filesystem::is_regular_file(boost::filesystem::wpath(filename).replace_extension(ex));
\r
326 if(ext == extensions.end())
\r
329 return std::make_shared<flash_producer>(filename + L"." + *ext);
\r