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"
32 #include <common/assert.h>
33 #include <common/except.h>
34 #include <common/executor.h>
35 #include <common/future.h>
36 #include <common/memory.h>
38 #include <boost/thread.hpp>
40 namespace caspar { namespace core {
42 std::vector<const producer_factory_t> g_factories;
44 void register_producer_factory(const producer_factory_t& factory)
46 g_factories.push_back(factory);
49 struct frame_producer_base::impl
51 tbb::atomic<uint32_t> frame_number_;
52 tbb::atomic<bool> paused_;
53 frame_producer_base& self_;
54 draw_frame last_frame_;
56 impl(frame_producer_base& self)
58 , last_frame_(draw_frame::empty())
67 return self_.last_frame();
69 auto frame = self_.receive_impl();
70 if(frame == draw_frame::late())
71 return self_.last_frame();
75 return last_frame_ = draw_frame::push(frame);
78 void paused(bool value)
83 draw_frame last_frame()
85 return draw_frame::still(last_frame_);
89 frame_producer_base::frame_producer_base() : impl_(new impl(*this))
93 draw_frame frame_producer_base::receive()
95 return impl_->receive();
98 void frame_producer_base::paused(bool value)
100 impl_->paused(value);
103 draw_frame frame_producer_base::last_frame()
105 return impl_->last_frame();
108 boost::unique_future<std::wstring> frame_producer_base::call(const std::wstring&)
110 CASPAR_THROW_EXCEPTION(not_supported());
113 uint32_t frame_producer_base::nb_frames() const
115 return std::numeric_limits<uint32_t>::max();
118 uint32_t frame_producer_base::frame_number() const
120 return impl_->frame_number_;
123 const spl::shared_ptr<frame_producer>& frame_producer::empty()
125 class empty_frame_producer : public frame_producer
128 empty_frame_producer(){}
129 draw_frame receive() override{return draw_frame::empty();}
130 void paused(bool value) override{}
131 uint32_t nb_frames() const override {return 0;}
132 std::wstring print() const override { return L"empty";}
133 void subscribe(const monitor::observable::observer_ptr& o) override{}
134 void unsubscribe(const monitor::observable::observer_ptr& o) override{}
135 std::wstring name() const override {return L"empty";}
136 uint32_t frame_number() const override {return 0;}
137 boost::unique_future<std::wstring> call(const std::wstring& params) override{CASPAR_THROW_EXCEPTION(not_supported());}
138 draw_frame last_frame() {return draw_frame::empty();}
140 boost::property_tree::wptree info() const override
142 boost::property_tree::wptree info;
143 info.add(L"type", L"empty-producer");
148 static spl::shared_ptr<frame_producer> producer = spl::make_shared<empty_frame_producer>();
152 class destroy_producer_proxy : public frame_producer
154 std::shared_ptr<frame_producer> producer_;
156 destroy_producer_proxy(spl::shared_ptr<frame_producer>&& producer)
157 : producer_(std::move(producer))
161 virtual ~destroy_producer_proxy()
163 static tbb::atomic<int> counter = tbb::atomic<int>();
165 if(producer_ == core::frame_producer::empty())
169 CASPAR_VERIFY(counter < 8);
171 auto producer = new spl::shared_ptr<frame_producer>(std::move(producer_));
174 std::unique_ptr<spl::shared_ptr<frame_producer>> pointer_guard(producer);
175 auto str = (*producer)->print();
178 if(!producer->unique())
179 CASPAR_LOG(trace) << str << L" Not destroyed on asynchronous destruction thread: " << producer->use_count();
181 CASPAR_LOG(trace) << str << L" Destroying on asynchronous destruction thread.";
187 pointer_guard.reset();
188 CASPAR_LOG(info) << str << L" Destroyed.";
192 CASPAR_LOG_CURRENT_EXCEPTION();
199 draw_frame receive() override {return producer_->receive();}
200 std::wstring print() const override {return producer_->print();}
201 void paused(bool value) override {producer_->paused(value);}
202 std::wstring name() const override {return producer_->name();}
203 uint32_t frame_number() const override {return producer_->frame_number();}
204 boost::property_tree::wptree info() const override {return producer_->info();}
205 boost::unique_future<std::wstring> call(const std::wstring& str) override {return producer_->call(str);}
206 void leading_producer(const spl::shared_ptr<frame_producer>& producer) override {return producer_->leading_producer(producer);}
207 uint32_t nb_frames() const override {return producer_->nb_frames();}
208 class draw_frame last_frame() {return producer_->last_frame();}
209 void subscribe(const monitor::observable::observer_ptr& o) {return producer_->subscribe(o);}
210 void unsubscribe(const monitor::observable::observer_ptr& o) {return producer_->unsubscribe(o);}
213 spl::shared_ptr<core::frame_producer> create_destroy_proxy(spl::shared_ptr<core::frame_producer> producer)
215 return spl::make_shared<destroy_producer_proxy>(std::move(producer));
218 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)
221 CASPAR_THROW_EXCEPTION(invalid_argument() << arg_name_info("params") << arg_value_info(""));
223 auto producer = frame_producer::empty();
224 std::any_of(g_factories.begin(), g_factories.end(), [&](const producer_factory_t& factory) -> bool
228 producer = factory(my_frame_factory, format_desc, params);
232 CASPAR_LOG_CURRENT_EXCEPTION();
234 return producer != frame_producer::empty();
237 if(producer == frame_producer::empty())
238 producer = create_color_producer(my_frame_factory, params);
240 if(producer == frame_producer::empty())
246 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)
248 auto producer = do_create_producer(my_frame_factory, format_desc, params);
249 auto key_producer = frame_producer::empty();
251 try // to find a key file.
253 auto params_copy = params;
254 if(params_copy.size() > 0)
256 params_copy[0] += L"_A";
257 key_producer = do_create_producer(my_frame_factory, format_desc, params_copy);
258 if(key_producer == frame_producer::empty())
260 params_copy[0] += L"LPHA";
261 key_producer = do_create_producer(my_frame_factory, format_desc, params_copy);
267 if(producer != frame_producer::empty() && key_producer != frame_producer::empty())
268 return create_separated_producer(producer, key_producer);
270 if(producer == frame_producer::empty())
273 BOOST_FOREACH(auto& param, params)
275 CASPAR_THROW_EXCEPTION(file_not_found() << msg_info("No match found for supplied commands. Check syntax.") << arg_value_info(u8(str)));
282 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)
284 std::wstringstream iss(params);
285 std::vector<std::wstring> tokens;
286 typedef std::istream_iterator<std::wstring, wchar_t, std::char_traits<wchar_t> > iterator;
287 std::copy(iterator(iss), iterator(), std::back_inserter(tokens));
288 return create_producer(factory, format_desc, tokens);