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 frame_producer_dependencies::frame_producer_dependencies(
57 const spl::shared_ptr<core::frame_factory>& frame_factory,
58 const std::vector<spl::shared_ptr<video_channel>>& channels,
59 const video_format_desc& format_desc)
60 : frame_factory(frame_factory)
62 , format_desc(format_desc)
66 constraints::constraints(double width, double height)
67 : width(width), height(height)
71 constraints::constraints()
75 struct frame_producer_base::impl
77 tbb::atomic<uint32_t> frame_number_;
78 tbb::atomic<bool> paused_;
79 frame_producer_base& self_;
80 draw_frame last_frame_;
82 impl(frame_producer_base& self)
84 , last_frame_(draw_frame::empty())
93 return self_.last_frame();
95 auto frame = self_.receive_impl();
96 if(frame == draw_frame::late())
97 return self_.last_frame();
101 return last_frame_ = draw_frame::push(frame);
104 void paused(bool value)
109 draw_frame last_frame()
111 return draw_frame::still(last_frame_);
113 draw_frame create_thumbnail_frame()
115 return draw_frame::empty();
119 frame_producer_base::frame_producer_base() : impl_(new impl(*this))
123 draw_frame frame_producer_base::receive()
125 return impl_->receive();
128 void frame_producer_base::paused(bool value)
130 impl_->paused(value);
133 draw_frame frame_producer_base::last_frame()
135 return impl_->last_frame();
137 draw_frame frame_producer_base::create_thumbnail_frame()
139 return impl_->create_thumbnail_frame();
142 std::future<std::wstring> frame_producer_base::call(const std::vector<std::wstring>&)
144 CASPAR_THROW_EXCEPTION(not_supported());
147 uint32_t frame_producer_base::nb_frames() const
149 return std::numeric_limits<uint32_t>::max();
152 uint32_t frame_producer_base::frame_number() const
154 return impl_->frame_number_;
157 variable& frame_producer_base::get_variable(const std::wstring& name)
159 CASPAR_THROW_EXCEPTION(caspar_exception()
160 << msg_info(L"No variable called " + name + L" found in " + print()));
163 const std::vector<std::wstring>& frame_producer_base::get_variables() const
165 static std::vector<std::wstring> empty;
170 const spl::shared_ptr<frame_producer>& frame_producer::empty()
172 class empty_frame_producer : public frame_producer
175 empty_frame_producer(){}
176 draw_frame receive() override{return draw_frame::empty();}
177 void paused(bool value) override{}
178 uint32_t nb_frames() const override {return 0;}
179 std::wstring print() const override { return L"empty";}
180 monitor::subject& monitor_output() override {static monitor::subject monitor_subject(""); return monitor_subject;}
181 std::wstring name() const override {return L"empty";}
182 uint32_t frame_number() const override {return 0;}
183 std::future<std::wstring> call(const std::vector<std::wstring>& params) override{CASPAR_THROW_EXCEPTION(not_supported());}
184 variable& get_variable(const std::wstring& name) override { CASPAR_THROW_EXCEPTION(not_supported()); }
185 const std::vector<std::wstring>& get_variables() const override { static std::vector<std::wstring> empty; return empty; }
186 draw_frame last_frame() {return draw_frame::empty();}
187 draw_frame create_thumbnail_frame() {return draw_frame::empty();}
188 constraints& pixel_constraints() override { static constraints c; return c; }
190 boost::property_tree::wptree info() const override
192 boost::property_tree::wptree info;
193 info.add(L"type", L"empty-producer");
198 static spl::shared_ptr<frame_producer> producer = spl::make_shared<empty_frame_producer>();
202 class destroy_producer_proxy : public frame_producer
204 std::shared_ptr<frame_producer> producer_;
206 destroy_producer_proxy(spl::shared_ptr<frame_producer>&& producer)
207 : producer_(std::move(producer))
211 virtual ~destroy_producer_proxy()
213 static tbb::atomic<int> counter;
214 static std::once_flag counter_init_once;
215 std::call_once(counter_init_once, []{ counter = 0; });
217 if(producer_ == core::frame_producer::empty())
221 CASPAR_VERIFY(counter < 8);
223 auto producer = new spl::shared_ptr<frame_producer>(std::move(producer_));
226 std::unique_ptr<spl::shared_ptr<frame_producer>> pointer_guard(producer);
227 auto str = (*producer)->print();
230 if(!producer->unique())
231 CASPAR_LOG(trace) << str << L" Not destroyed on asynchronous destruction thread: " << producer->use_count();
233 CASPAR_LOG(trace) << str << L" Destroying on asynchronous destruction thread.";
239 pointer_guard.reset();
240 CASPAR_LOG(info) << str << L" Destroyed.";
244 CASPAR_LOG_CURRENT_EXCEPTION();
251 draw_frame receive() override {return producer_->receive();}
252 std::wstring print() const override {return producer_->print();}
253 void paused(bool value) override {producer_->paused(value);}
254 std::wstring name() const override {return producer_->name();}
255 uint32_t frame_number() const override {return producer_->frame_number();}
256 boost::property_tree::wptree info() const override {return producer_->info();}
257 std::future<std::wstring> call(const std::vector<std::wstring>& params) override {return producer_->call(params);}
258 variable& get_variable(const std::wstring& name) override {return producer_->get_variable(name);}
259 const std::vector<std::wstring>& get_variables() const override {return producer_->get_variables();}
260 void leading_producer(const spl::shared_ptr<frame_producer>& producer) override {return producer_->leading_producer(producer);}
261 uint32_t nb_frames() const override {return producer_->nb_frames();}
262 draw_frame last_frame() {return producer_->last_frame();}
263 draw_frame create_thumbnail_frame() {return producer_->create_thumbnail_frame();}
264 monitor::subject& monitor_output() override {return producer_->monitor_output();}
265 bool collides(double x, double y) const override {return producer_->collides(x, y);}
266 void on_interaction(const interaction_event::ptr& event) override {return producer_->on_interaction(event);}
267 constraints& pixel_constraints() override {return producer_->pixel_constraints();}
270 spl::shared_ptr<core::frame_producer> create_destroy_proxy(spl::shared_ptr<core::frame_producer> producer)
272 return spl::make_shared<destroy_producer_proxy>(std::move(producer));
275 spl::shared_ptr<core::frame_producer> do_create_producer(const frame_producer_dependencies& dependencies, const std::vector<std::wstring>& params, const std::vector<producer_factory_t>& factories, bool throw_on_fail = false)
278 CASPAR_THROW_EXCEPTION(invalid_argument() << arg_name_info("params") << arg_value_info(""));
280 auto producer = frame_producer::empty();
281 std::any_of(factories.begin(), factories.end(), [&](const producer_factory_t& factory) -> bool
285 producer = factory(dependencies, params);
292 CASPAR_LOG_CURRENT_EXCEPTION();
294 return producer != frame_producer::empty();
297 if(producer == frame_producer::empty())
298 producer = create_color_producer(dependencies.frame_factory, params);
300 if (producer == frame_producer::empty())
301 producer = create_freehand_producer(dependencies.frame_factory, params);
303 if(producer == frame_producer::empty())
309 spl::shared_ptr<core::frame_producer> create_thumbnail_producer(const frame_producer_dependencies& dependencies, const std::wstring& media_file)
311 std::vector<std::wstring> params;
312 params.push_back(media_file);
314 auto producer = do_create_producer(dependencies, params, g_thumbnail_factories, true);
315 auto key_producer = frame_producer::empty();
317 try // to find a key file.
319 auto params_copy = params;
320 if (params_copy.size() > 0)
322 params_copy[0] += L"_A";
323 key_producer = do_create_producer(dependencies, params_copy, g_thumbnail_factories, true);
324 if (key_producer == frame_producer::empty())
326 params_copy[0] += L"LPHA";
327 key_producer = do_create_producer(dependencies, params_copy, g_thumbnail_factories, true);
333 if (producer != frame_producer::empty() && key_producer != frame_producer::empty())
334 return create_separated_producer(producer, key_producer);
339 spl::shared_ptr<core::frame_producer> create_producer(const frame_producer_dependencies& dependencies, const std::vector<std::wstring>& params)
341 auto producer = do_create_producer(dependencies, params, g_producer_factories);
342 auto key_producer = frame_producer::empty();
344 try // to find a key file.
346 auto params_copy = params;
347 if(params_copy.size() > 0)
349 params_copy[0] += L"_A";
350 key_producer = do_create_producer(dependencies, params_copy, g_producer_factories);
351 if(key_producer == frame_producer::empty())
353 params_copy[0] += L"LPHA";
354 key_producer = do_create_producer(dependencies, params_copy, g_producer_factories);
360 if(producer != frame_producer::empty() && key_producer != frame_producer::empty())
361 return create_separated_producer(producer, key_producer);
363 if(producer == frame_producer::empty())
366 for (auto& param : params)
368 CASPAR_THROW_EXCEPTION(file_not_found() << msg_info("No match found for supplied commands. Check syntax.") << arg_value_info(u8(str)));
375 spl::shared_ptr<core::frame_producer> create_producer(const frame_producer_dependencies& dependencies, const std::wstring& params)
377 std::wstringstream iss(params);
378 std::vector<std::wstring> tokens;
379 typedef std::istream_iterator<std::wstring, wchar_t, std::char_traits<wchar_t> > iterator;
380 std::copy(iterator(iss), iterator(), std::back_inserter(tokens));
381 return create_producer(dependencies, tokens);