2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
4 * This file is part of CasparCG (www.casparcg.com).
6 * CasparCG is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * CasparCG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
19 * Author: Robert Nagy, ronag89@gmail.com
22 #include "../StdAfx.h"
24 #include "frame_producer.h"
26 #include "../frame/draw_frame.h"
27 #include "../frame/frame_transform.h"
29 #include "color/color_producer.h"
30 #include "draw/freehand_producer.h"
31 #include "separated/separated_producer.h"
34 #include <common/assert.h>
35 #include <common/except.h>
36 #include <common/executor.h>
37 #include <common/future.h>
38 #include <common/memory.h>
40 #include <boost/thread.hpp>
42 namespace caspar { namespace core {
44 std::vector<producer_factory_t> g_producer_factories;
45 std::vector<producer_factory_t> g_thumbnail_factories;
47 void register_producer_factory(const producer_factory_t& factory)
49 g_producer_factories.push_back(factory);
51 void register_thumbnail_producer_factory(const producer_factory_t& factory)
53 g_thumbnail_factories.push_back(factory);
56 constraints::constraints(double width, double height)
57 : width(width), height(height)
61 constraints::constraints()
65 struct frame_producer_base::impl
67 tbb::atomic<uint32_t> frame_number_;
68 tbb::atomic<bool> paused_;
69 frame_producer_base& self_;
70 draw_frame last_frame_;
72 impl(frame_producer_base& self)
74 , last_frame_(draw_frame::empty())
83 return self_.last_frame();
85 auto frame = self_.receive_impl();
86 if(frame == draw_frame::late())
87 return self_.last_frame();
91 return last_frame_ = draw_frame::push(frame);
94 void paused(bool value)
99 draw_frame last_frame()
101 return draw_frame::still(last_frame_);
103 draw_frame create_thumbnail_frame()
105 return draw_frame::empty();
109 frame_producer_base::frame_producer_base() : impl_(new impl(*this))
113 draw_frame frame_producer_base::receive()
115 return impl_->receive();
118 void frame_producer_base::paused(bool value)
120 impl_->paused(value);
123 draw_frame frame_producer_base::last_frame()
125 return impl_->last_frame();
127 draw_frame frame_producer_base::create_thumbnail_frame()
129 return impl_->create_thumbnail_frame();
132 std::future<std::wstring> frame_producer_base::call(const std::vector<std::wstring>&)
134 CASPAR_THROW_EXCEPTION(not_supported());
137 uint32_t frame_producer_base::nb_frames() const
139 return std::numeric_limits<uint32_t>::max();
142 uint32_t frame_producer_base::frame_number() const
144 return impl_->frame_number_;
147 variable& frame_producer_base::get_variable(const std::wstring& name)
149 CASPAR_THROW_EXCEPTION(caspar_exception()
150 << msg_info(L"No variable called " + name + L" found in " + print()));
153 const std::vector<std::wstring>& frame_producer_base::get_variables() const
155 static std::vector<std::wstring> empty;
160 const spl::shared_ptr<frame_producer>& frame_producer::empty()
162 class empty_frame_producer : public frame_producer
165 empty_frame_producer(){}
166 draw_frame receive() override{return draw_frame::empty();}
167 void paused(bool value) override{}
168 uint32_t nb_frames() const override {return 0;}
169 std::wstring print() const override { return L"empty";}
170 monitor::subject& monitor_output() override {static monitor::subject monitor_subject(""); return monitor_subject;}
171 std::wstring name() const override {return L"empty";}
172 uint32_t frame_number() const override {return 0;}
173 std::future<std::wstring> call(const std::vector<std::wstring>& params) override{CASPAR_THROW_EXCEPTION(not_supported());}
174 variable& get_variable(const std::wstring& name) override { CASPAR_THROW_EXCEPTION(not_supported()); }
175 const std::vector<std::wstring>& get_variables() const override { static std::vector<std::wstring> empty; return empty; }
176 draw_frame last_frame() {return draw_frame::empty();}
177 draw_frame create_thumbnail_frame() {return draw_frame::empty();}
178 constraints& pixel_constraints() override { static constraints c; return c; }
180 boost::property_tree::wptree info() const override
182 boost::property_tree::wptree info;
183 info.add(L"type", L"empty-producer");
188 static spl::shared_ptr<frame_producer> producer = spl::make_shared<empty_frame_producer>();
192 class destroy_producer_proxy : public frame_producer
194 std::shared_ptr<frame_producer> producer_;
196 destroy_producer_proxy(spl::shared_ptr<frame_producer>&& producer)
197 : producer_(std::move(producer))
201 virtual ~destroy_producer_proxy()
203 static tbb::atomic<int> counter = tbb::atomic<int>();
205 if(producer_ == core::frame_producer::empty())
209 CASPAR_VERIFY(counter < 8);
211 auto producer = new spl::shared_ptr<frame_producer>(std::move(producer_));
214 std::unique_ptr<spl::shared_ptr<frame_producer>> pointer_guard(producer);
215 auto str = (*producer)->print();
218 if(!producer->unique())
219 CASPAR_LOG(trace) << str << L" Not destroyed on asynchronous destruction thread: " << producer->use_count();
221 CASPAR_LOG(trace) << str << L" Destroying on asynchronous destruction thread.";
227 pointer_guard.reset();
228 CASPAR_LOG(info) << str << L" Destroyed.";
232 CASPAR_LOG_CURRENT_EXCEPTION();
239 draw_frame receive() override {return producer_->receive();}
240 std::wstring print() const override {return producer_->print();}
241 void paused(bool value) override {producer_->paused(value);}
242 std::wstring name() const override {return producer_->name();}
243 uint32_t frame_number() const override {return producer_->frame_number();}
244 boost::property_tree::wptree info() const override {return producer_->info();}
245 std::future<std::wstring> call(const std::vector<std::wstring>& params) override {return producer_->call(params);}
246 variable& get_variable(const std::wstring& name) override {return producer_->get_variable(name);}
247 const std::vector<std::wstring>& get_variables() const override {return producer_->get_variables();}
248 void leading_producer(const spl::shared_ptr<frame_producer>& producer) override {return producer_->leading_producer(producer);}
249 uint32_t nb_frames() const override {return producer_->nb_frames();}
250 class draw_frame last_frame() {return producer_->last_frame();}
251 draw_frame create_thumbnail_frame() {return producer_->create_thumbnail_frame();}
252 monitor::subject& monitor_output() override {return producer_->monitor_output();}
253 bool collides(double x, double y) const override {return producer_->collides(x, y);}
254 void on_interaction(const interaction_event::ptr& event) override {return producer_->on_interaction(event);}
255 constraints& pixel_constraints() override {return producer_->pixel_constraints();}
258 spl::shared_ptr<core::frame_producer> create_destroy_proxy(spl::shared_ptr<core::frame_producer> producer)
260 return spl::make_shared<destroy_producer_proxy>(std::move(producer));
263 spl::shared_ptr<core::frame_producer> do_create_producer(const spl::shared_ptr<frame_factory>& my_frame_factory, const video_format_desc& format_desc, const std::vector<std::wstring>& params, const std::vector<producer_factory_t>& factories, bool throw_on_fail = false)
266 CASPAR_THROW_EXCEPTION(invalid_argument() << arg_name_info("params") << arg_value_info(""));
268 auto producer = frame_producer::empty();
269 std::any_of(factories.begin(), factories.end(), [&](const producer_factory_t& factory) -> bool
273 producer = factory(my_frame_factory, format_desc, params);
280 CASPAR_LOG_CURRENT_EXCEPTION();
282 return producer != frame_producer::empty();
285 if(producer == frame_producer::empty())
286 producer = create_color_producer(my_frame_factory, params);
288 if (producer == frame_producer::empty())
289 producer = create_freehand_producer(my_frame_factory, params);
291 if(producer == frame_producer::empty())
297 spl::shared_ptr<core::frame_producer> create_thumbnail_producer(const spl::shared_ptr<frame_factory>& my_frame_factory, const video_format_desc& format_desc, const std::wstring& media_file)
299 std::vector<std::wstring> params;
300 params.push_back(media_file);
302 auto producer = do_create_producer(my_frame_factory, format_desc, params, g_thumbnail_factories, true);
303 auto key_producer = frame_producer::empty();
305 try // to find a key file.
307 auto params_copy = params;
308 if (params_copy.size() > 0)
310 params_copy[0] += L"_A";
311 key_producer = do_create_producer(my_frame_factory, format_desc, params_copy, g_thumbnail_factories, true);
312 if (key_producer == frame_producer::empty())
314 params_copy[0] += L"LPHA";
315 key_producer = do_create_producer(my_frame_factory, format_desc, params_copy, g_thumbnail_factories, true);
321 if (producer != frame_producer::empty() && key_producer != frame_producer::empty())
322 return create_separated_producer(producer, key_producer);
327 spl::shared_ptr<core::frame_producer> create_producer(const spl::shared_ptr<frame_factory>& my_frame_factory, const video_format_desc& format_desc, const std::vector<std::wstring>& params)
329 auto producer = do_create_producer(my_frame_factory, format_desc, params, g_producer_factories);
330 auto key_producer = frame_producer::empty();
332 try // to find a key file.
334 auto params_copy = params;
335 if(params_copy.size() > 0)
337 params_copy[0] += L"_A";
338 key_producer = do_create_producer(my_frame_factory, format_desc, params_copy, g_producer_factories);
339 if(key_producer == frame_producer::empty())
341 params_copy[0] += L"LPHA";
342 key_producer = do_create_producer(my_frame_factory, format_desc, params_copy, g_producer_factories);
348 if(producer != frame_producer::empty() && key_producer != frame_producer::empty())
349 return create_separated_producer(producer, key_producer);
351 if(producer == frame_producer::empty())
354 for (auto& param : params)
356 CASPAR_THROW_EXCEPTION(file_not_found() << msg_info("No match found for supplied commands. Check syntax.") << arg_value_info(u8(str)));
363 spl::shared_ptr<core::frame_producer> create_producer(const spl::shared_ptr<frame_factory>& factory, const video_format_desc& format_desc, const std::wstring& params)
365 std::wstringstream iss(params);
366 std::vector<std::wstring> tokens;
367 typedef std::istream_iterator<std::wstring, wchar_t, std::char_traits<wchar_t> > iterator;
368 std::copy(iterator(iss), iterator(), std::back_inserter(tokens));
369 return create_producer(factory, format_desc, tokens);