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 "separated/separated_producer.h"
33 #include <common/assert.h>
34 #include <common/except.h>
35 #include <common/executor.h>
36 #include <common/future.h>
37 #include <common/memory.h>
39 #include <boost/thread.hpp>
41 namespace caspar { namespace core {
43 struct frame_producer_registry::impl
45 std::vector<producer_factory_t> producer_factories;
46 std::vector<thumbnail_producer_t> thumbnail_producers;
47 spl::shared_ptr<help_repository> help_repo;
49 impl(spl::shared_ptr<help_repository> help_repo)
50 : help_repo(std::move(help_repo))
55 frame_producer_registry::frame_producer_registry(spl::shared_ptr<help_repository> help_repo)
56 : impl_(new impl(std::move(help_repo)))
60 void frame_producer_registry::register_producer_factory(std::wstring name, const producer_factory_t& factory, const help_item_describer& describer)
62 impl_->producer_factories.push_back(factory);
63 impl_->help_repo->register_item({ L"producer" }, std::move(name), describer);
66 void frame_producer_registry::register_thumbnail_producer(const thumbnail_producer_t& thumbnail_producer)
68 impl_->thumbnail_producers.push_back(thumbnail_producer);
71 frame_producer_dependencies::frame_producer_dependencies(
72 const spl::shared_ptr<core::frame_factory>& frame_factory,
73 const std::vector<spl::shared_ptr<video_channel>>& channels,
74 const video_format_desc& format_desc,
75 const spl::shared_ptr<const frame_producer_registry> producer_registry)
76 : frame_factory(frame_factory)
78 , format_desc(format_desc)
79 , producer_registry(producer_registry)
83 constraints::constraints(double width, double height)
84 : width(width), height(height)
88 constraints::constraints()
92 struct frame_producer_base::impl
94 tbb::atomic<uint32_t> frame_number_;
95 tbb::atomic<bool> paused_;
96 frame_producer_base& self_;
97 draw_frame last_frame_;
99 impl(frame_producer_base& self)
101 , last_frame_(draw_frame::empty())
110 return self_.last_frame();
112 auto frame = self_.receive_impl();
113 if(frame == draw_frame::late())
114 return self_.last_frame();
118 return last_frame_ = draw_frame::push(frame);
121 void paused(bool value)
126 draw_frame last_frame()
128 return draw_frame::still(last_frame_);
132 frame_producer_base::frame_producer_base() : impl_(new impl(*this))
136 draw_frame frame_producer_base::receive()
138 return impl_->receive();
141 void frame_producer_base::paused(bool value)
143 impl_->paused(value);
146 draw_frame frame_producer_base::last_frame()
148 return impl_->last_frame();
151 std::future<std::wstring> frame_producer_base::call(const std::vector<std::wstring>&)
153 CASPAR_THROW_EXCEPTION(not_supported());
156 uint32_t frame_producer_base::nb_frames() const
158 return std::numeric_limits<uint32_t>::max();
161 uint32_t frame_producer_base::frame_number() const
163 return impl_->frame_number_;
166 variable& frame_producer_base::get_variable(const std::wstring& name)
168 CASPAR_THROW_EXCEPTION(user_error()
169 << msg_info(L"No variable called " + name + L" found in " + print()));
172 const std::vector<std::wstring>& frame_producer_base::get_variables() const
174 static std::vector<std::wstring> empty;
179 const spl::shared_ptr<frame_producer>& frame_producer::empty()
181 class empty_frame_producer : public frame_producer
184 empty_frame_producer(){}
185 draw_frame receive() override{return draw_frame::empty();}
186 void paused(bool value) override{}
187 uint32_t nb_frames() const override {return 0;}
188 std::wstring print() const override { return L"empty";}
189 monitor::subject& monitor_output() override {static monitor::subject monitor_subject(""); return monitor_subject;}
190 std::wstring name() const override {return L"empty";}
191 uint32_t frame_number() const override {return 0;}
192 std::future<std::wstring> call(const std::vector<std::wstring>& params) override{CASPAR_THROW_EXCEPTION(not_implemented());}
193 variable& get_variable(const std::wstring& name) override { CASPAR_THROW_EXCEPTION(not_implemented()); }
194 const std::vector<std::wstring>& get_variables() const override { static std::vector<std::wstring> empty; return empty; }
195 draw_frame last_frame() {return draw_frame::empty();}
196 constraints& pixel_constraints() override { static constraints c; return c; }
198 boost::property_tree::wptree info() const override
200 boost::property_tree::wptree info;
201 info.add(L"type", L"empty-producer");
206 static spl::shared_ptr<frame_producer> producer = spl::make_shared<empty_frame_producer>();
210 tbb::atomic<bool>& destroy_producers_in_separate_thread()
212 static tbb::atomic<bool> state;
217 void destroy_producers_synchronously()
219 destroy_producers_in_separate_thread() = false;
222 class destroy_producer_proxy : public frame_producer
224 std::shared_ptr<frame_producer> producer_;
226 destroy_producer_proxy(spl::shared_ptr<frame_producer>&& producer)
227 : producer_(std::move(producer))
229 destroy_producers_in_separate_thread() = true;
232 virtual ~destroy_producer_proxy()
234 static tbb::atomic<int> counter;
235 static std::once_flag counter_init_once;
236 std::call_once(counter_init_once, []{ counter = 0; });
238 if(producer_ == core::frame_producer::empty() || !destroy_producers_in_separate_thread())
242 CASPAR_VERIFY(counter < 8);
244 auto producer = new spl::shared_ptr<frame_producer>(std::move(producer_));
247 std::unique_ptr<spl::shared_ptr<frame_producer>> pointer_guard(producer);
248 auto str = (*producer)->print();
251 ensure_gpf_handler_installed_for_thread(u8(L"Destroyer: " + str).c_str());
253 if (!producer->unique())
254 CASPAR_LOG(debug) << str << L" Not destroyed on asynchronous destruction thread: " << producer->use_count();
256 CASPAR_LOG(debug) << str << L" Destroying on asynchronous destruction thread.";
262 pointer_guard.reset();
263 CASPAR_LOG(info) << str << L" Destroyed.";
267 CASPAR_LOG_CURRENT_EXCEPTION();
274 draw_frame receive() override {return producer_->receive();}
275 std::wstring print() const override {return producer_->print();}
276 void paused(bool value) override {producer_->paused(value);}
277 std::wstring name() const override {return producer_->name();}
278 uint32_t frame_number() const override {return producer_->frame_number();}
279 boost::property_tree::wptree info() const override {return producer_->info();}
280 std::future<std::wstring> call(const std::vector<std::wstring>& params) override {return producer_->call(params);}
281 variable& get_variable(const std::wstring& name) override {return producer_->get_variable(name);}
282 const std::vector<std::wstring>& get_variables() const override {return producer_->get_variables();}
283 void leading_producer(const spl::shared_ptr<frame_producer>& producer) override {return producer_->leading_producer(producer);}
284 uint32_t nb_frames() const override {return producer_->nb_frames();}
285 draw_frame last_frame() {return producer_->last_frame();}
286 monitor::subject& monitor_output() override {return producer_->monitor_output();}
287 bool collides(double x, double y) const override {return producer_->collides(x, y);}
288 void on_interaction(const interaction_event::ptr& event) override {return producer_->on_interaction(event);}
289 constraints& pixel_constraints() override {return producer_->pixel_constraints();}
292 spl::shared_ptr<core::frame_producer> create_destroy_proxy(spl::shared_ptr<core::frame_producer> producer)
294 return spl::make_shared<destroy_producer_proxy>(std::move(producer));
297 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)
300 CASPAR_THROW_EXCEPTION(invalid_argument() << msg_info("params cannot be empty"));
302 auto producer = frame_producer::empty();
303 std::any_of(factories.begin(), factories.end(), [&](const producer_factory_t& factory) -> bool
307 producer = factory(dependencies, params);
318 CASPAR_LOG_CURRENT_EXCEPTION();
320 return producer != frame_producer::empty();
323 if(producer == frame_producer::empty())
324 producer = create_color_producer(dependencies.frame_factory, params);
326 if(producer == frame_producer::empty())
332 draw_frame do_create_thumbnail_frame(
333 const frame_producer_dependencies& dependencies,
334 const std::wstring& media_file,
335 const std::vector<thumbnail_producer_t>& thumbnail_producers)
337 for (auto& thumbnail_producer : thumbnail_producers)
339 auto frame = thumbnail_producer(dependencies, media_file);
341 if (frame != draw_frame::empty())
345 return draw_frame::empty();
348 draw_frame frame_producer_registry::create_thumbnail(const frame_producer_dependencies& dependencies, const std::wstring& media_file) const
350 auto& thumbnail_producers = impl_->thumbnail_producers;
351 std::vector<std::wstring> params;
352 params.push_back(media_file);
354 auto fill_frame = do_create_thumbnail_frame(dependencies, media_file, thumbnail_producers);
355 auto key_frame = do_create_thumbnail_frame(dependencies, media_file + L"_A", thumbnail_producers);
357 if (key_frame == draw_frame::empty())
358 key_frame = do_create_thumbnail_frame(dependencies, media_file + L"_ALPHA", thumbnail_producers);
360 if (fill_frame != draw_frame::empty() && key_frame != draw_frame::empty())
361 return draw_frame::mask(fill_frame, key_frame);
366 spl::shared_ptr<core::frame_producer> frame_producer_registry::create_producer(const frame_producer_dependencies& dependencies, const std::vector<std::wstring>& params) const
368 auto& producer_factories = impl_->producer_factories;
369 auto producer = do_create_producer(dependencies, params, producer_factories);
370 auto key_producer = frame_producer::empty();
372 try // to find a key file.
374 auto params_copy = params;
375 if(params_copy.size() > 0)
377 params_copy[0] += L"_A";
378 key_producer = do_create_producer(dependencies, params_copy, producer_factories);
379 if(key_producer == frame_producer::empty())
381 params_copy[0] += L"LPHA";
382 key_producer = do_create_producer(dependencies, params_copy, producer_factories);
388 if(producer != frame_producer::empty() && key_producer != frame_producer::empty())
389 return create_separated_producer(producer, key_producer);
391 if(producer == frame_producer::empty())
394 for (auto& param : params)
396 CASPAR_THROW_EXCEPTION(file_not_found() << msg_info("No match found for supplied commands. Check syntax.") << arg_value_info(u8(str)));
403 spl::shared_ptr<core::frame_producer> frame_producer_registry::create_producer(const frame_producer_dependencies& dependencies, const std::wstring& params) const
405 std::wstringstream iss(params);
406 std::vector<std::wstring> tokens;
407 typedef std::istream_iterator<std::wstring, wchar_t, std::char_traits<wchar_t> > iterator;
408 std::copy(iterator(iss), iterator(), std::back_inserter(tokens));
409 return create_producer(dependencies, tokens);