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/executor.h>
\r
35 #include <common/future.h>
\r
36 #include <common/memory.h>
\r
38 #include <boost/thread.hpp>
\r
40 namespace caspar { namespace core {
\r
42 std::vector<const producer_factory_t> g_factories;
\r
44 void register_producer_factory(const producer_factory_t& factory)
\r
46 g_factories.push_back(factory);
\r
49 struct frame_producer_base::impl
\r
51 tbb::atomic<uint32_t> frame_number_;
\r
52 tbb::atomic<bool> paused_;
\r
53 frame_producer_base& self_;
\r
54 draw_frame last_frame_;
\r
56 impl(frame_producer_base& self)
\r
58 , last_frame_(draw_frame::empty())
\r
64 draw_frame receive()
\r
67 return self_.last_frame();
\r
69 auto frame = self_.receive_impl();
\r
70 if(frame == draw_frame::late())
\r
75 return last_frame_ = draw_frame::push(frame);
\r
78 void paused(bool value)
\r
83 draw_frame last_frame() const
\r
85 return draw_frame::still(last_frame_);
\r
89 frame_producer_base::frame_producer_base() : impl_(new impl(*this))
\r
93 frame_producer_base::frame_producer_base(frame_producer_base& self) : impl_(new impl(self))
\r
97 draw_frame frame_producer_base::receive()
\r
99 return impl_->receive();
\r
102 void frame_producer_base::paused(bool value)
\r
104 impl_->paused(value);
\r
107 draw_frame frame_producer_base::last_frame() const
\r
109 return impl_->last_frame();
\r
112 boost::unique_future<std::wstring> frame_producer_base::call(const std::wstring&)
\r
114 BOOST_THROW_EXCEPTION(not_supported());
\r
117 uint32_t frame_producer_base::nb_frames() const
\r
119 return std::numeric_limits<uint32_t>::max();
\r
122 uint32_t frame_producer_base::frame_number() const
\r
124 return impl_->frame_number_;
\r
127 const spl::shared_ptr<frame_producer>& frame_producer::empty()
\r
129 class empty_frame_producer : public frame_producer
\r
132 empty_frame_producer(){}
\r
133 draw_frame receive() override{return draw_frame::empty();}
\r
134 void paused(bool value) override{}
\r
135 uint32_t nb_frames() const override {return 0;}
\r
136 std::wstring print() const override { return L"empty";}
\r
137 void subscribe(const monitor::observable::observer_ptr& o) override{}
\r
138 void unsubscribe(const monitor::observable::observer_ptr& o) override{}
\r
139 std::wstring name() const override {return L"empty";}
\r
140 uint32_t frame_number() const override {return 0;}
\r
141 boost::unique_future<std::wstring> call(const std::wstring& params) override{BOOST_THROW_EXCEPTION(not_supported());}
\r
142 draw_frame last_frame() const {return draw_frame::empty();}
\r
144 boost::property_tree::wptree info() const override
\r
146 boost::property_tree::wptree info;
\r
147 info.add(L"type", L"empty-producer");
\r
152 static spl::shared_ptr<frame_producer> producer = spl::make_shared<empty_frame_producer>();
\r
156 class destroy_producer_proxy : public frame_producer
\r
158 std::shared_ptr<frame_producer> producer_;
\r
160 destroy_producer_proxy(spl::shared_ptr<frame_producer>&& producer)
\r
161 : producer_(std::move(producer))
\r
165 virtual ~destroy_producer_proxy()
\r
167 static tbb::atomic<int> counter = tbb::atomic<int>();
\r
169 if(producer_ == core::frame_producer::empty())
\r
173 CASPAR_VERIFY(counter < 8);
\r
175 auto producer = new spl::shared_ptr<frame_producer>(std::move(producer_));
\r
178 std::unique_ptr<spl::shared_ptr<frame_producer>> pointer_guard(producer);
\r
179 auto str = (*producer)->print();
\r
182 if(!producer->unique())
\r
183 CASPAR_LOG(trace) << str << L" Not destroyed on asynchronous destruction thread: " << producer->use_count();
\r
185 CASPAR_LOG(trace) << str << L" Destroying on asynchronous destruction thread.";
\r
189 pointer_guard.reset();
\r
190 CASPAR_LOG(info) << str << L" Destroyed.";
\r
196 draw_frame receive() override {return producer_->receive();}
\r
197 std::wstring print() const override {return producer_->print();}
\r
198 void paused(bool value) override {producer_->paused(value);}
\r
199 std::wstring name() const override {return producer_->name();}
\r
200 uint32_t frame_number() const override {return producer_->frame_number();}
\r
201 boost::property_tree::wptree info() const override {return producer_->info();}
\r
202 boost::unique_future<std::wstring> call(const std::wstring& str) override {return producer_->call(str);}
\r
203 void leading_producer(const spl::shared_ptr<frame_producer>& producer) override {return producer_->leading_producer(producer);}
\r
204 uint32_t nb_frames() const override {return producer_->nb_frames();}
\r
205 class draw_frame last_frame() const {return producer_->last_frame();}
\r
206 void subscribe(const monitor::observable::observer_ptr& o) {return producer_->subscribe(o);}
\r
207 void unsubscribe(const monitor::observable::observer_ptr& o) {return producer_->unsubscribe(o);}
\r
210 spl::shared_ptr<core::frame_producer> create_destroy_proxy(spl::shared_ptr<core::frame_producer> producer)
\r
212 return spl::make_shared<destroy_producer_proxy>(std::move(producer));
\r
215 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)
\r
218 BOOST_THROW_EXCEPTION(invalid_argument() << arg_name_info("params") << arg_value_info(""));
\r
220 auto producer = frame_producer::empty();
\r
221 std::any_of(g_factories.begin(), g_factories.end(), [&](const producer_factory_t& factory) -> bool
\r
225 producer = factory(my_frame_factory, format_desc, params);
\r
229 CASPAR_LOG_CURRENT_EXCEPTION();
\r
231 return producer != frame_producer::empty();
\r
234 if(producer == frame_producer::empty())
\r
235 producer = create_color_producer(my_frame_factory, params);
\r
237 if(producer == frame_producer::empty())
\r
240 return create_destroy_proxy(producer);
\r
243 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)
\r
245 auto producer = do_create_producer(my_frame_factory, format_desc, params);
\r
246 auto key_producer = frame_producer::empty();
\r
248 try // to find a key file.
\r
250 auto params_copy = params;
\r
251 if(params_copy.size() > 0)
\r
253 params_copy[0] += L"_A";
\r
254 key_producer = do_create_producer(my_frame_factory, format_desc, params_copy);
\r
255 if(key_producer == frame_producer::empty())
\r
257 params_copy[0] += L"LPHA";
\r
258 key_producer = do_create_producer(my_frame_factory, format_desc, params_copy);
\r
264 if(producer != frame_producer::empty() && key_producer != frame_producer::empty())
\r
265 return create_separated_producer(producer, key_producer);
\r
267 if(producer == frame_producer::empty())
\r
270 BOOST_FOREACH(auto& param, params)
\r
271 str += param + L" ";
\r
272 BOOST_THROW_EXCEPTION(file_not_found() << msg_info("No match found for supplied commands. Check syntax.") << arg_value_info(u8(str)));
\r
279 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)
\r
281 std::wstringstream iss(params);
\r
282 std::vector<std::wstring> tokens;
\r
283 typedef std::istream_iterator<std::wstring, wchar_t, std::char_traits<wchar_t> > iterator;
\r
284 std::copy(iterator(iss), iterator(), std::back_inserter(tokens));
\r
285 return create_producer(factory, format_desc, tokens);
\r