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
25 #include "frame/basic_frame.h"
\r
26 #include "frame/frame_transform.h"
\r
28 #include "color/color_producer.h"
\r
29 #include "playlist/playlist_producer.h"
\r
30 #include "separated/separated_producer.h"
\r
32 #include <common/memory/safe_ptr.h>
\r
33 #include <common/concurrency/executor.h>
\r
34 #include <common/exception/exceptions.h>
\r
35 #include <common/utility/move_on_copy.h>
\r
37 namespace caspar { namespace core {
\r
39 std::vector<const producer_factory_t> g_factories;
\r
40 std::vector<const producer_factory_t> g_thumbnail_factories;
\r
42 class destroy_producer_proxy : public frame_producer
\r
44 std::unique_ptr<std::shared_ptr<frame_producer>> producer_;
\r
46 destroy_producer_proxy(safe_ptr<frame_producer>&& producer)
\r
47 : producer_(new std::shared_ptr<frame_producer>(std::move(producer)))
\r
51 ~destroy_producer_proxy()
\r
53 static auto destroyers = std::make_shared<tbb::concurrent_bounded_queue<std::shared_ptr<executor>>>();
\r
54 static tbb::atomic<int> destroyer_count;
\r
58 std::shared_ptr<executor> destroyer;
\r
59 if(!destroyers->try_pop(destroyer))
\r
61 destroyer.reset(new executor(L"destroyer"));
\r
62 destroyer->set_priority_class(below_normal_priority_class);
\r
63 if(++destroyer_count > 16)
\r
64 CASPAR_LOG(warning) << L"Potential destroyer dead-lock detected.";
\r
65 CASPAR_LOG(trace) << "Created destroyer: " << destroyer_count;
\r
68 auto producer = producer_.release();
\r
69 auto pool = destroyers;
\r
70 destroyer->begin_invoke([=]
\r
72 std::unique_ptr<std::shared_ptr<frame_producer>> producer2(producer);
\r
74 auto str = (*producer2)->print();
\r
77 if(!producer->unique())
\r
78 CASPAR_LOG(trace) << str << L" Not destroyed on asynchronous destruction thread: " << producer->use_count();
\r
80 CASPAR_LOG(trace) << str << L" Destroying on asynchronous destruction thread.";
\r
85 pool->push(destroyer);
\r
90 CASPAR_LOG_CURRENT_EXCEPTION();
\r
99 virtual safe_ptr<basic_frame> receive(int hints) override {return (*producer_)->receive(hints);}
\r
100 virtual safe_ptr<basic_frame> last_frame() const override {return (*producer_)->last_frame();}
\r
101 virtual safe_ptr<basic_frame> create_thumbnail_frame() override {return (*producer_)->create_thumbnail_frame();}
\r
102 virtual std::wstring print() const override {return (*producer_)->print();}
\r
103 virtual boost::property_tree::wptree info() const override {return (*producer_)->info();}
\r
104 virtual boost::unique_future<std::wstring> call(const std::wstring& str) override {return (*producer_)->call(str);}
\r
105 virtual safe_ptr<frame_producer> get_following_producer() const override {return (*producer_)->get_following_producer();}
\r
106 virtual void set_leading_producer(const safe_ptr<frame_producer>& producer) override {(*producer_)->set_leading_producer(producer);}
\r
107 virtual uint32_t nb_frames() const override {return (*producer_)->nb_frames();}
\r
110 safe_ptr<core::frame_producer> create_producer_destroy_proxy(safe_ptr<core::frame_producer> producer)
\r
112 return make_safe<destroy_producer_proxy>(std::move(producer));
\r
115 class print_producer_proxy : public frame_producer
\r
117 std::shared_ptr<frame_producer> producer_;
\r
119 print_producer_proxy(safe_ptr<frame_producer>&& producer)
\r
120 : producer_(std::move(producer))
\r
122 CASPAR_LOG(info) << producer_->print() << L" Initialized.";
\r
125 ~print_producer_proxy()
\r
127 auto str = producer_->print();
\r
128 CASPAR_LOG(trace) << str << L" Uninitializing.";
\r
130 CASPAR_LOG(info) << str << L" Uninitialized.";
\r
133 virtual safe_ptr<basic_frame> receive(int hints) override {return (producer_)->receive(hints);}
\r
134 virtual safe_ptr<basic_frame> last_frame() const override {return (producer_)->last_frame();}
\r
135 virtual safe_ptr<basic_frame> create_thumbnail_frame() override {return (producer_)->create_thumbnail_frame();}
\r
136 virtual std::wstring print() const override {return (producer_)->print();}
\r
137 virtual boost::property_tree::wptree info() const override {return (producer_)->info();}
\r
138 virtual boost::unique_future<std::wstring> call(const std::wstring& str) override {return (producer_)->call(str);}
\r
139 virtual safe_ptr<frame_producer> get_following_producer() const override {return (producer_)->get_following_producer();}
\r
140 virtual void set_leading_producer(const safe_ptr<frame_producer>& producer) override {(producer_)->set_leading_producer(producer);}
\r
141 virtual uint32_t nb_frames() const override {return (producer_)->nb_frames();}
\r
144 safe_ptr<core::frame_producer> create_producer_print_proxy(safe_ptr<core::frame_producer> producer)
\r
146 return make_safe<print_producer_proxy>(std::move(producer));
\r
149 class last_frame_producer : public frame_producer
\r
151 const std::wstring print_;
\r
152 const safe_ptr<basic_frame> frame_;
\r
153 const uint32_t nb_frames_;
\r
155 last_frame_producer(const safe_ptr<frame_producer>& producer)
\r
156 : print_(producer->print())
\r
157 , frame_(producer->last_frame() != basic_frame::eof() ? producer->last_frame() : basic_frame::empty())
\r
158 , nb_frames_(producer->nb_frames())
\r
162 virtual safe_ptr<basic_frame> receive(int){return frame_;}
\r
163 virtual safe_ptr<core::basic_frame> last_frame() const{return frame_;}
\r
164 virtual safe_ptr<core::basic_frame> create_thumbnail_frame() {return frame_;}
\r
165 virtual std::wstring print() const{return L"dummy[" + print_ + L"]";}
\r
166 virtual uint32_t nb_frames() const {return nb_frames_;}
\r
167 virtual boost::property_tree::wptree info() const override
\r
169 boost::property_tree::wptree info;
\r
170 info.add(L"type", L"last-frame-producer");
\r
175 struct empty_frame_producer : public frame_producer
\r
177 virtual safe_ptr<basic_frame> receive(int){return basic_frame::empty();}
\r
178 virtual safe_ptr<basic_frame> last_frame() const{return basic_frame::empty();}
\r
179 virtual void set_frame_factory(const safe_ptr<frame_factory>&){}
\r
180 virtual uint32_t nb_frames() const {return 0;}
\r
181 virtual std::wstring print() const { return L"empty";}
\r
183 virtual boost::property_tree::wptree info() const override
\r
185 boost::property_tree::wptree info;
\r
186 info.add(L"type", L"empty-producer");
\r
191 const safe_ptr<frame_producer>& frame_producer::empty() // nothrow
\r
193 static safe_ptr<frame_producer> producer = make_safe<empty_frame_producer>();
\r
197 safe_ptr<basic_frame> frame_producer::create_thumbnail_frame()
\r
199 return basic_frame::empty();
\r
202 safe_ptr<basic_frame> receive_and_follow(safe_ptr<frame_producer>& producer, int hints)
\r
204 auto frame = producer->receive(hints);
\r
205 if(frame == basic_frame::eof())
\r
207 CASPAR_LOG(info) << producer->print() << " End Of File.";
\r
208 auto following = producer->get_following_producer();
\r
209 if(following != frame_producer::empty())
\r
211 following->set_leading_producer(producer);
\r
212 producer = std::move(following);
\r
215 producer = make_safe<last_frame_producer>(producer);
\r
217 return receive_and_follow(producer, hints);
\r
222 void register_producer_factory(const producer_factory_t& factory)
\r
224 g_factories.push_back(factory);
\r
227 void register_thumbnail_producer_factory(const producer_factory_t& factory)
\r
229 g_thumbnail_factories.push_back(factory);
\r
232 safe_ptr<core::frame_producer> do_create_producer(const safe_ptr<frame_factory>& my_frame_factory, const std::vector<std::wstring>& params, const std::vector<const producer_factory_t>& factories, bool throw_on_fail = false)
\r
235 BOOST_THROW_EXCEPTION(invalid_argument() << arg_name_info("params") << arg_value_info(""));
\r
237 auto producer = frame_producer::empty();
\r
238 std::any_of(factories.begin(), factories.end(), [&](const producer_factory_t& factory) -> bool
\r
242 producer = factory(my_frame_factory, params);
\r
249 CASPAR_LOG_CURRENT_EXCEPTION();
\r
251 return producer != frame_producer::empty();
\r
254 if(producer == frame_producer::empty())
\r
255 producer = create_color_producer(my_frame_factory, params);
\r
257 if(producer == frame_producer::empty())
\r
258 producer = create_playlist_producer(my_frame_factory, params);
\r
263 safe_ptr<core::frame_producer> create_producer(const safe_ptr<frame_factory>& my_frame_factory, const std::vector<std::wstring>& params)
\r
265 auto producer = do_create_producer(my_frame_factory, params, g_factories);
\r
266 auto key_producer = frame_producer::empty();
\r
268 try // to find a key file.
\r
270 auto params_copy = params;
\r
271 if(params_copy.size() > 0)
\r
273 params_copy[0] += L"_A";
\r
274 key_producer = do_create_producer(my_frame_factory, params_copy, g_factories);
\r
275 if(key_producer == frame_producer::empty())
\r
277 params_copy[0] += L"LPHA";
\r
278 key_producer = do_create_producer(my_frame_factory, params_copy, g_factories);
\r
284 if(producer != frame_producer::empty() && key_producer != frame_producer::empty())
\r
285 return create_separated_producer(producer, key_producer);
\r
287 if(producer == frame_producer::empty())
\r
290 BOOST_FOREACH(auto& param, params)
\r
291 str += param + L" ";
\r
292 BOOST_THROW_EXCEPTION(file_not_found() << msg_info("No match found for supplied commands. Check syntax.") << arg_value_info(narrow(str)));
\r
298 safe_ptr<core::frame_producer> create_thumbnail_producer(const safe_ptr<frame_factory>& my_frame_factory, const std::wstring& media_file)
\r
300 std::vector<std::wstring> params;
\r
301 params.push_back(media_file);
\r
303 auto producer = do_create_producer(my_frame_factory, params, g_thumbnail_factories, true);
\r
304 auto key_producer = frame_producer::empty();
\r
306 try // to find a key file.
\r
308 auto params_copy = params;
\r
309 if (params_copy.size() > 0)
\r
311 params_copy[0] += L"_A";
\r
312 key_producer = do_create_producer(my_frame_factory, params_copy, g_thumbnail_factories, true);
\r
313 if (key_producer == frame_producer::empty())
\r
315 params_copy[0] += L"LPHA";
\r
316 key_producer = do_create_producer(my_frame_factory, params_copy, g_thumbnail_factories, true);
\r
322 if (producer != frame_producer::empty() && key_producer != frame_producer::empty())
\r
323 return create_separated_thumbnail_producer(producer, key_producer);
\r
328 safe_ptr<core::frame_producer> create_producer(const safe_ptr<frame_factory>& factory, const std::wstring& params)
\r
330 std::wstringstream iss(params);
\r
331 std::vector<std::wstring> tokens;
\r
332 typedef std::istream_iterator<std::wstring, wchar_t, std::char_traits<wchar_t> > iterator;
\r
333 std::copy(iterator(iss), iterator(), std::back_inserter(tokens));
\r
334 return create_producer(factory, tokens);
\r