2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
\r
4 * This file is part of CasparCG (www.casparcg.com).
\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
19 * Author: Robert Nagy, ronag89@gmail.com
\r
22 #include "../StdAfx.h"
\r
24 #include "frame_producer.h"
\r
26 #include "../frame/draw_frame.h"
\r
27 #include "../frame/frame_transform.h"
\r
29 #include "color/color_producer.h"
\r
30 #include "separated/separated_producer.h"
\r
32 #include <common/assert.h>
\r
33 #include <common/except.h>
\r
34 #include <common/concurrency/executor.h>
\r
35 #include <common/concurrency/async.h>
\r
36 #include <common/spl/memory.h>
\r
38 namespace caspar { namespace core {
\r
40 std::vector<const producer_factory_t> g_factories;
\r
42 void register_producer_factory(const producer_factory_t& factory)
\r
44 g_factories.push_back(factory);
\r
47 boost::unique_future<std::wstring> frame_producer::call(const std::wstring&)
\r
49 BOOST_THROW_EXCEPTION(not_supported());
\r
52 struct empty_frame_producer : public frame_producer
\r
54 virtual spl::shared_ptr<draw_frame> receive(int){return draw_frame::empty();}
\r
55 virtual spl::shared_ptr<draw_frame> last_frame() const{return draw_frame::empty();}
\r
56 virtual void set_frame_factory(const spl::shared_ptr<frame_factory>&){}
\r
57 virtual uint32_t nb_frames() const {return 0;}
\r
58 virtual std::wstring print() const { return L"empty";}
\r
59 virtual void subscribe(const monitor::observable::observer_ptr& o){}
\r
60 virtual void unsubscribe(const monitor::observable::observer_ptr& o){}
\r
61 virtual std::wstring name() const {return L"empty";}
\r
63 virtual boost::property_tree::wptree info() const override
\r
65 boost::property_tree::wptree info;
\r
66 info.add(L"type", L"empty-producer");
\r
71 const spl::shared_ptr<frame_producer>& frame_producer::empty() // nothrow
\r
73 static spl::shared_ptr<frame_producer> producer = spl::make_shared<empty_frame_producer>();
\r
77 class producer_proxy_base : public frame_producer
\r
80 std::shared_ptr<frame_producer> producer_;
\r
82 producer_proxy_base(spl::shared_ptr<frame_producer>&& producer)
\r
83 : producer_(std::move(producer))
\r
87 virtual spl::shared_ptr<draw_frame> receive(int hints) override {return producer_->receive(hints);}
\r
88 virtual spl::shared_ptr<draw_frame> last_frame() const override {return producer_->last_frame();}
\r
89 virtual std::wstring print() const override {return producer_->print();}
\r
90 virtual std::wstring name() const override {return producer_->name();}
\r
91 virtual boost::property_tree::wptree info() const override {return producer_->info();}
\r
92 virtual boost::unique_future<std::wstring> call(const std::wstring& str) override {return producer_->call(str);}
\r
93 virtual spl::shared_ptr<frame_producer> following_producer() const override {return producer_->following_producer();}
\r
94 virtual void leading_producer(const spl::shared_ptr<frame_producer>& producer) override {return producer_->leading_producer(producer);}
\r
95 virtual uint32_t nb_frames() const override {return producer_->nb_frames();}
\r
96 virtual void subscribe(const monitor::observable::observer_ptr& o) {return producer_->subscribe(o);}
\r
97 virtual void unsubscribe(const monitor::observable::observer_ptr& o) {return producer_->unsubscribe(o);}
\r
100 class follow_producer_proxy : public producer_proxy_base
\r
102 monitor::subject event_subject_;
\r
104 follow_producer_proxy(spl::shared_ptr<frame_producer>&& producer)
\r
105 : producer_proxy_base(std::move(producer))
\r
107 producer->subscribe(event_subject_);
\r
110 virtual spl::shared_ptr<draw_frame> receive(int hints) override
\r
112 auto frame = producer_->receive(hints);
\r
113 if(frame == draw_frame::eof())
\r
115 CASPAR_LOG(info) << producer_->print() << " End Of File.";
\r
116 auto following = producer_->following_producer();
\r
117 if(following != frame_producer::empty())
\r
119 following->leading_producer(spl::make_shared_ptr(producer_));
\r
121 producer_->unsubscribe(event_subject_);
\r
122 producer_ = std::move(following);
\r
123 producer_->subscribe(event_subject_);
\r
126 return receive(hints);
\r
131 virtual spl::shared_ptr<draw_frame> last_frame() const override
\r
133 return draw_frame::mute(producer_->last_frame());
\r
136 virtual void subscribe(const monitor::observable::observer_ptr& o) override
\r
138 event_subject_.subscribe(o);
\r
141 virtual void unsubscribe(const monitor::observable::observer_ptr& o) override
\r
143 event_subject_.unsubscribe(o);
\r
147 class destroy_producer_proxy : public producer_proxy_base
\r
150 destroy_producer_proxy(spl::shared_ptr<frame_producer>&& producer)
\r
151 : producer_proxy_base(std::move(producer))
\r
155 ~destroy_producer_proxy()
\r
157 static tbb::atomic<int> counter = tbb::atomic<int>();
\r
160 CASPAR_VERIFY(counter < 32);
\r
162 auto producer = new spl::shared_ptr<frame_producer>(std::move(producer_));
\r
165 std::unique_ptr<spl::shared_ptr<frame_producer>> pointer_guard(producer);
\r
166 auto str = (*producer)->print();
\r
169 if(!producer->unique())
\r
170 CASPAR_LOG(trace) << str << L" Not destroyed on asynchronous destruction thread: " << producer->use_count();
\r
172 CASPAR_LOG(trace) << str << L" Destroying on asynchronous destruction thread.";
\r
176 pointer_guard.reset();
\r
183 class print_producer_proxy : public producer_proxy_base
\r
186 print_producer_proxy(spl::shared_ptr<frame_producer>&& producer)
\r
187 : producer_proxy_base(std::move(producer))
\r
189 CASPAR_LOG(info) << producer_->print() << L" Initialized.";
\r
192 ~print_producer_proxy()
\r
194 auto str = producer_->print();
\r
195 CASPAR_LOG(trace) << str << L" Uninitializing.";
\r
197 CASPAR_LOG(info) << str << L" Uninitialized.";
\r
201 spl::shared_ptr<core::frame_producer> wrap_producer(spl::shared_ptr<core::frame_producer> producer)
\r
203 return spl::make_shared<follow_producer_proxy>(
\r
204 spl::make_shared<destroy_producer_proxy>(
\r
205 spl::make_shared<print_producer_proxy>(
\r
206 std::move(producer))));
\r
209 spl::shared_ptr<core::frame_producer> do_create_producer(const spl::shared_ptr<frame_factory>& my_frame_factory, const std::vector<std::wstring>& params)
\r
212 BOOST_THROW_EXCEPTION(invalid_argument() << arg_name_info("params") << arg_value_info(""));
\r
214 auto producer = frame_producer::empty();
\r
215 std::any_of(g_factories.begin(), g_factories.end(), [&](const producer_factory_t& factory) -> bool
\r
219 producer = factory(my_frame_factory, params);
\r
223 CASPAR_LOG_CURRENT_EXCEPTION();
\r
225 return producer != frame_producer::empty();
\r
228 if(producer == frame_producer::empty())
\r
229 producer = create_color_producer(my_frame_factory, params);
\r
231 if(producer == frame_producer::empty())
\r
237 spl::shared_ptr<core::frame_producer> create_producer(const spl::shared_ptr<frame_factory>& my_frame_factory, const std::vector<std::wstring>& params)
\r
239 auto producer = do_create_producer(my_frame_factory, params);
\r
240 auto key_producer = frame_producer::empty();
\r
242 try // to find a key file.
\r
244 auto params_copy = params;
\r
245 if(params_copy.size() > 0)
\r
247 params_copy[0] += L"_A";
\r
248 key_producer = do_create_producer(my_frame_factory, params_copy);
\r
249 if(key_producer == frame_producer::empty())
\r
251 params_copy[0] += L"LPHA";
\r
252 key_producer = do_create_producer(my_frame_factory, params_copy);
\r
258 if(producer != frame_producer::empty() && key_producer != frame_producer::empty())
\r
259 return create_separated_producer(producer, key_producer);
\r
261 if(producer == frame_producer::empty())
\r
264 BOOST_FOREACH(auto& param, params)
\r
265 str += param + L" ";
\r
266 BOOST_THROW_EXCEPTION(file_not_found() << msg_info("No match found for supplied commands. Check syntax.") << arg_value_info(u8(str)));
\r
273 spl::shared_ptr<core::frame_producer> create_producer(const spl::shared_ptr<frame_factory>& factory, const std::wstring& params)
\r
275 std::wstringstream iss(params);
\r
276 std::vector<std::wstring> tokens;
\r
277 typedef std::istream_iterator<std::wstring, wchar_t, std::char_traits<wchar_t> > iterator;
\r
278 std::copy(iterator(iss), iterator(), std::back_inserter(tokens));
\r
279 return create_producer(factory, tokens);
\r