2 * copyright (c) 2010 Sveriges Television AB <info@casparcg.com>
\r
4 * This file is part of CasparCG.
\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
21 #include "../StdAfx.h"
\r
23 #include "frame_producer.h"
\r
24 #include "frame/basic_frame.h"
\r
25 #include "frame/frame_transform.h"
\r
27 #include "color/color_producer.h"
\r
28 #include "playlist/playlist_producer.h"
\r
29 #include "separated/separated_producer.h"
\r
31 #include <common/memory/safe_ptr.h>
\r
32 #include <common/concurrency/executor.h>
\r
33 #include <common/exception/exceptions.h>
\r
34 #include <common/utility/move_on_copy.h>
\r
36 namespace caspar { namespace core {
\r
38 std::vector<const producer_factory_t> g_factories;
\r
40 class destroy_producer_proxy : public frame_producer
\r
42 std::shared_ptr<frame_producer>* producer_;
\r
44 destroy_producer_proxy(safe_ptr<frame_producer>&& producer)
\r
45 : producer_(new std::shared_ptr<frame_producer>(std::move(producer)))
\r
49 ~destroy_producer_proxy()
\r
51 static auto destroyers = std::make_shared<tbb::concurrent_bounded_queue<std::shared_ptr<executor>>>();
\r
52 static tbb::atomic<int> destroyer_count;
\r
56 std::shared_ptr<executor> destroyer;
\r
57 if(!destroyers->try_pop(destroyer))
\r
59 destroyer.reset(new executor(L"destroyer"));
\r
60 destroyer->set_priority_class(below_normal_priority_class);
\r
61 if(++destroyer_count > 16)
\r
62 CASPAR_LOG(warning) << L"Potential destroyer dead-lock detected.";
\r
63 CASPAR_LOG(trace) << "Created destroyer: " << destroyer_count;
\r
66 auto producer = producer_;
\r
67 auto pool = destroyers;
\r
68 destroyer->begin_invoke([=]
\r
72 if(!producer->unique())
\r
73 CASPAR_LOG(trace) << (*producer)->print() << L" Not destroyed on safe asynchronous destruction thread: " << producer->use_count();
\r
75 CASPAR_LOG(trace) << (*producer)->print() << L" Destroying on safe asynchronous destruction thread.";
\r
80 pool->push(destroyer);
\r
85 CASPAR_LOG_CURRENT_EXCEPTION();
\r
94 virtual safe_ptr<basic_frame> receive(int hints) override {return (*producer_)->receive(hints);}
\r
95 virtual safe_ptr<basic_frame> last_frame() const override {return (*producer_)->last_frame();}
\r
96 virtual std::wstring print() const override {return (*producer_)->print();}
\r
97 virtual boost::unique_future<std::wstring> call(const std::wstring& str) override {return (*producer_)->call(str);}
\r
98 virtual safe_ptr<frame_producer> get_following_producer() const override {return (*producer_)->get_following_producer();}
\r
99 virtual void set_leading_producer(const safe_ptr<frame_producer>& producer) override {(*producer_)->set_leading_producer(producer);}
\r
100 virtual int64_t nb_frames() const override {return (*producer_)->nb_frames();}
\r
101 virtual int64_t file_nb_frames() const override {return (*producer_)->file_nb_frames();}
\r
102 virtual int64_t frame_number() const override {return (*producer_)->frame_number();}
\r
103 virtual int64_t file_frame_number() const override {return (*producer_)->file_frame_number();}
\r
106 safe_ptr<core::frame_producer> create_destroy_proxy(safe_ptr<core::frame_producer>&& producer)
\r
108 return make_safe<destroy_producer_proxy>(std::move(producer));
\r
111 class last_frame_producer : public frame_producer
\r
113 const std::wstring print_;
\r
114 const safe_ptr<basic_frame> frame_;
\r
115 const int64_t nb_frames_;
\r
117 last_frame_producer(const safe_ptr<frame_producer>& producer)
\r
118 : print_(producer->print())
\r
119 , frame_(producer->last_frame() != basic_frame::eof() ? producer->last_frame() : basic_frame::empty())
\r
120 , nb_frames_(producer->nb_frames())
\r
124 virtual safe_ptr<basic_frame> receive(int){return frame_;}
\r
125 virtual safe_ptr<core::basic_frame> last_frame() const{return frame_;}
\r
126 virtual std::wstring print() const{return L"dummy[" + print_ + L"]";}
\r
127 virtual int64_t nb_frames() const {return nb_frames_;}
\r
130 struct empty_frame_producer : public frame_producer
\r
132 virtual safe_ptr<basic_frame> receive(int){return basic_frame::empty();}
\r
133 virtual safe_ptr<basic_frame> last_frame() const{return basic_frame::empty();}
\r
134 virtual void set_frame_factory(const safe_ptr<frame_factory>&){}
\r
135 virtual int64_t nb_frames() const {return 0;}
\r
136 virtual std::wstring print() const { return L"empty";}
\r
139 const safe_ptr<frame_producer>& frame_producer::empty() // nothrow
\r
141 static safe_ptr<frame_producer> producer = make_safe<empty_frame_producer>();
\r
145 safe_ptr<basic_frame> receive_and_follow(safe_ptr<frame_producer>& producer, int hints)
\r
147 auto frame = producer->receive(hints);
\r
148 if(frame == basic_frame::eof())
\r
150 CASPAR_LOG(info) << producer->print() << " End Of File.";
\r
151 auto following = producer->get_following_producer();
\r
152 if(following != frame_producer::empty())
\r
154 following->set_leading_producer(producer);
\r
155 producer = std::move(following);
\r
158 producer = make_safe<last_frame_producer>(producer);
\r
160 return receive_and_follow(producer, hints);
\r
165 void register_producer_factory(const producer_factory_t& factory)
\r
167 g_factories.push_back(factory);
\r
170 safe_ptr<core::frame_producer> do_create_producer(const safe_ptr<frame_factory>& my_frame_factory, const std::vector<std::wstring>& params)
\r
173 BOOST_THROW_EXCEPTION(invalid_argument() << arg_name_info("params") << arg_value_info(""));
\r
175 auto producer = frame_producer::empty();
\r
176 std::any_of(g_factories.begin(), g_factories.end(), [&](const producer_factory_t& factory) -> bool
\r
180 producer = factory(my_frame_factory, params);
\r
184 CASPAR_LOG_CURRENT_EXCEPTION();
\r
186 return producer != frame_producer::empty();
\r
189 if(producer == frame_producer::empty())
\r
190 producer = create_color_producer(my_frame_factory, params);
\r
192 if(producer == frame_producer::empty())
\r
193 producer = create_playlist_producer(my_frame_factory, params);
\r
199 safe_ptr<core::frame_producer> create_producer(const safe_ptr<frame_factory>& my_frame_factory, const std::vector<std::wstring>& params)
\r
201 auto producer = do_create_producer(my_frame_factory, params);
\r
202 auto key_producer = frame_producer::empty();
\r
204 try // to find a key file.
\r
206 auto params_copy = params;
\r
207 if(params_copy.size() > 0)
\r
209 params_copy[0] += L"_A";
\r
210 key_producer = do_create_producer(my_frame_factory, params_copy);
\r
211 if(key_producer == frame_producer::empty())
\r
213 params_copy[0] += L"LPHA";
\r
214 key_producer = do_create_producer(my_frame_factory, params_copy);
\r
220 if(producer != frame_producer::empty() && key_producer != frame_producer::empty())
\r
221 return create_separated_producer(producer, key_producer);
\r
223 if(producer == frame_producer::empty())
\r
226 BOOST_FOREACH(auto& param, params)
\r
227 str += param + L" ";
\r
228 BOOST_THROW_EXCEPTION(file_not_found() << msg_info("No match found for supplied commands. Check syntax.") << arg_value_info(narrow(str)));
\r
235 safe_ptr<core::frame_producer> create_producer(const safe_ptr<frame_factory>& factory, const std::wstring& params)
\r
237 std::wstringstream iss(params);
\r
238 std::vector<std::wstring> tokens;
\r
239 typedef std::istream_iterator<std::wstring, wchar_t, std::char_traits<wchar_t> > iterator;
\r
240 std::copy(iterator(iss), iterator(), std::back_inserter(tokens));
\r
241 return create_producer(factory, tokens);
\r