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/draw_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(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(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(draw_frame::eof()); // 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
228 std::shared_ptr<draw_frame> frame;
\r
230 frame = do_receive();
\r
232 frame = std::shared_ptr<composite_frame>(composite_frame::interlace(do_receive(), do_receive(), format_desc.mode));
\r
234 frame_buffer_.push(frame);
\r
235 is_empty_ = flashax_container_->IsEmpty();
\r
239 safe_ptr<draw_frame> do_receive()
\r
241 auto format_desc = frame_processor_->get_video_format_desc();
\r
243 flashax_container_->Tick();
\r
244 invalid_count_ = !flashax_container_->InvalidRectangle() ? std::min(2, invalid_count_+1) : 0;
\r
245 if(current_frame_ == nullptr || invalid_count_ < 2)
\r
247 std::fill_n(bmp_frame_->data(), bmp_frame_->size(), 0);
\r
248 flashax_container_->DrawControl(bmp_frame_->hdc());
\r
249 current_frame_ = bmp_frame_;
\r
252 auto frame = frame_processor_->create_frame(format_desc.width, format_desc.height);
\r
253 std::copy(current_frame_->data(), current_frame_->data() + current_frame_->size(), frame->pixel_data().begin());
\r
257 safe_ptr<draw_frame> receive()
\r
259 return ((frame_buffer_.try_pop(last_frame_) || !is_empty_) && last_frame_) ? safe_ptr<draw_frame>(last_frame_) : draw_frame::empty();
\r
262 void initialize(const safe_ptr<frame_processor_device>& frame_processor)
\r
264 frame_processor_ = frame_processor;
\r
265 auto format_desc = frame_processor_->get_video_format_desc();
\r
266 bmp_frame_ = std::make_shared<bitmap>(format_desc.width, format_desc.height);
\r
270 std::wstring print() const
\r
272 return L"flash_producer. filename: " + filename_;
\r
275 CComObject<flash::FlashAxContainer>* flashax_container_;
\r
277 tbb::concurrent_bounded_queue<std::shared_ptr<draw_frame>> frame_buffer_;
\r
279 std::shared_ptr<draw_frame> last_frame_;
\r
281 bitmap_ptr current_frame_;
\r
282 bitmap_ptr bmp_frame_;
\r
284 std::wstring filename_;
\r
285 flash_producer* self_;
\r
287 tbb::atomic<bool> is_empty_;
\r
288 executor executor_;
\r
289 int invalid_count_;
\r
291 std::shared_ptr<frame_processor_device> frame_processor_;
\r
294 flash_producer::flash_producer(flash_producer&& other) : impl_(std::move(other.impl_)){}
\r
295 flash_producer::flash_producer(const std::wstring& filename) : impl_(new implementation(this, filename)){}
\r
296 safe_ptr<draw_frame> flash_producer::receive(){return impl_->receive();}
\r
297 void flash_producer::param(const std::wstring& param){impl_->param(param);}
\r
298 void flash_producer::initialize(const safe_ptr<frame_processor_device>& frame_processor) { impl_->initialize(frame_processor);}
\r
299 std::wstring flash_producer::print() const {return impl_->print();}
\r
301 std::wstring flash_producer::find_template(const std::wstring& template_name)
\r
303 if(boost::filesystem::exists(template_name + L".ft"))
\r
304 return template_name + L".ft";
\r
306 if(boost::filesystem::exists(template_name + L".ct"))
\r
307 return template_name + L".ct";
\r
312 safe_ptr<flash_producer> create_flash_producer(const std::vector<std::wstring>& params)
\r
314 static const std::vector<std::wstring> extensions = list_of(L"swf");
\r
315 std::wstring filename = server::media_folder() + L"\\" + params[0];
\r
317 auto ext = std::find_if(extensions.begin(), extensions.end(), [&](const std::wstring& ex) -> bool
\r
319 return boost::filesystem::is_regular_file(boost::filesystem::wpath(filename).replace_extension(ex));
\r
322 if(ext == extensions.end())
\r
323 BOOST_THROW_EXCEPTION(file_not_found() << msg_info(narrow(filename)));
\r
325 return make_safe<flash_producer>(filename + L"." + *ext);
\r