]> git.sesse.net Git - casparcg/blob - core/producer/cg_proxy.cpp
* Merged core/fwd.h from 2.0 and added more types to it. Started using it all over...
[casparcg] / core / producer / cg_proxy.cpp
1 /*
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
3 *
4 * This file is part of CasparCG (www.casparcg.com).
5 *
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.
10 *
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.
15 *
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/>.
18 *
19 * Author: Helge Norberg, helge.norberg@svt.se
20 */
21
22 #include "../StdAfx.h"
23
24 #include "cg_proxy.h"
25
26 #include "frame_producer.h"
27 #include "stage.h"
28 #include "../video_channel.h"
29 #include "../diagnostics/call_context.h"
30
31 #include <common/env.h>
32 #include <common/os/filesystem.h>
33
34 #include <boost/thread/mutex.hpp>
35 #include <boost/thread/lock_guard.hpp>
36 #include <boost/filesystem.hpp>
37 #include <boost/algorithm/string/predicate.hpp>
38 #include <boost/optional.hpp>
39
40 #include <future>
41 #include <map>
42
43 namespace caspar { namespace core {
44
45 const spl::shared_ptr<cg_proxy>& cg_proxy::empty()
46 {
47         class empty_proxy : public cg_proxy
48         {
49                 void add(int, const std::wstring&, bool, const std::wstring&, const std::wstring&) override {}
50                 void remove(int) override {}
51                 void play(int) override {}
52                 void stop(int, unsigned int) override {}
53                 void next(int) override {}
54                 void update(int, const std::wstring&) override {}
55                 std::wstring invoke(int, const std::wstring&) override { return L""; }
56                 std::wstring description(int) override { return L"empty cg producer"; }
57                 std::wstring template_host_info() override { return L"empty cg producer"; }
58         };
59
60         static spl::shared_ptr<cg_proxy> instance = spl::make_shared<empty_proxy>();
61         return instance;
62 }
63
64 using namespace boost::multi_index;
65
66 struct cg_producer_registry::impl
67 {
68 private:
69         struct record
70         {
71                 std::wstring                    name;
72                 meta_info_extractor             info_extractor;
73                 cg_proxy_factory                proxy_factory;
74                 cg_producer_factory             producer_factory;
75                 bool                                    reusable_producer_instance;
76         };
77
78         struct name {};
79         struct extension {};
80
81         mutable boost::mutex                    mutex_;
82         std::map<std::wstring, record>  records_by_extension_;
83 public:
84         void register_cg_producer(
85                         std::wstring cg_producer_name,
86                         std::set<std::wstring> file_extensions,
87                         meta_info_extractor info_extractor,
88                         cg_proxy_factory proxy_factory,
89                         cg_producer_factory producer_factory,
90                         bool reusable_producer_instance)
91         {
92                 boost::lock_guard<boost::mutex> lock(mutex_);
93
94                 record rec
95                 {
96                         std::move(cg_producer_name),
97                         std::move(info_extractor),
98                         std::move(proxy_factory),
99                         std::move(producer_factory),
100                         reusable_producer_instance
101                 };
102
103                 for (auto& extension : file_extensions)
104                 {
105                         records_by_extension_.insert(std::make_pair(extension, rec));
106                 }
107         }
108
109         spl::shared_ptr<frame_producer> create_producer(
110                         const spl::shared_ptr<video_channel>& video_channel,
111                         const std::wstring& filename) const
112         {
113                 auto found = find_record(filename);
114
115                 if (!found)
116                         return frame_producer::empty();
117
118                 return found->producer_factory(
119                                 video_channel->frame_factory(),
120                                 video_channel->video_format_desc(),
121                                 filename);
122         }
123
124         spl::shared_ptr<cg_proxy> get_proxy(const spl::shared_ptr<frame_producer>& producer) const
125         {
126                 auto producer_name = producer->name();
127
128                 boost::lock_guard<boost::mutex> lock(mutex_);
129
130                 for (auto& elem : records_by_extension_)
131                 {
132                         if (elem.second.name == producer_name)
133                                 return elem.second.proxy_factory(producer);
134                 }
135
136                 return cg_proxy::empty();
137         }
138
139         spl::shared_ptr<cg_proxy> get_proxy(
140                         const spl::shared_ptr<video_channel>& video_channel,
141                         int render_layer) const
142         {
143                 auto producer = spl::make_shared_ptr(video_channel->stage().foreground(render_layer).get());
144
145                 return get_proxy(producer);
146         }
147
148         spl::shared_ptr<cg_proxy> get_or_create_proxy(
149                         const spl::shared_ptr<video_channel>& video_channel,
150                         int render_layer,
151                         const std::wstring& filename) const
152         {
153                 using namespace boost::filesystem;
154
155                 auto found = find_record(filename);
156
157                 if (!found)
158                         return cg_proxy::empty();
159
160                 auto producer = spl::make_shared_ptr(video_channel->stage().foreground(render_layer).get());
161                 auto current_producer_name = producer->name();
162                 bool create_new = current_producer_name != found->name || !found->reusable_producer_instance;
163
164                 if (create_new)
165                 {
166                         diagnostics::scoped_call_context save;
167                         diagnostics::call_context::for_thread().video_channel = video_channel->index();
168                         diagnostics::call_context::for_thread().layer = render_layer;
169
170                         producer = found->producer_factory(
171                                         video_channel->frame_factory(),
172                                         video_channel->video_format_desc(),
173                                         filename);
174                         video_channel->stage().load(render_layer, producer);
175                         video_channel->stage().play(render_layer);
176                 }
177
178                 return found->proxy_factory(producer);
179         }
180
181         std::string read_meta_info(const std::wstring& filename) const
182         {
183                 using namespace boost::filesystem;
184
185                 auto basepath = path(env::template_folder()) / path(filename);
186
187                 boost::lock_guard<boost::mutex> lock(mutex_);
188
189                 for (auto& rec : records_by_extension_)
190                 {
191                         auto p = path(basepath.wstring() + rec.first);
192                         auto found = find_case_insensitive(p.wstring());
193
194                         if (found)
195                                 return rec.second.info_extractor(*found);
196                 }
197
198                 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(L"No meta info extractor for " + filename));
199         }
200
201         bool is_cg_extension(const std::wstring& extension) const
202         {
203                 boost::lock_guard<boost::mutex> lock(mutex_);
204
205                 return records_by_extension_.find(extension) != records_by_extension_.end();
206         }
207 private:
208         boost::optional<record> find_record(const std::wstring& filename) const
209         {
210                 using namespace boost::filesystem;
211
212                 auto basepath = path(env::template_folder()) / path(filename);
213
214                 boost::lock_guard<boost::mutex> lock(mutex_);
215
216                 for (auto& rec : records_by_extension_)
217                 {
218                         auto p = path(basepath.wstring() + rec.first);
219
220                         if (find_case_insensitive(p.wstring()))
221                                 return rec.second;
222                 }
223
224                 return boost::none;
225         }
226 };
227
228 cg_producer_registry::cg_producer_registry() : impl_(new impl) { }
229
230 void cg_producer_registry::register_cg_producer(
231                 std::wstring cg_producer_name,
232                 std::set<std::wstring> file_extensions,
233                 meta_info_extractor info_extractor,
234                 cg_proxy_factory proxy_factory,
235                 cg_producer_factory producer_factory,
236                 bool reusable_producer_instance)
237 {
238         impl_->register_cg_producer(
239                         std::move(cg_producer_name),
240                         std::move(file_extensions),
241                         std::move(info_extractor),
242                         std::move(proxy_factory),
243                         std::move(producer_factory),
244                         reusable_producer_instance);
245 }
246
247 spl::shared_ptr<frame_producer> cg_producer_registry::create_producer(
248                 const spl::shared_ptr<video_channel>& video_channel,
249                 const std::wstring& filename) const
250 {
251         return impl_->create_producer(video_channel, filename);
252 }
253
254 spl::shared_ptr<cg_proxy> cg_producer_registry::get_proxy(
255                 const spl::shared_ptr<frame_producer>& producer) const
256 {
257         return impl_->get_proxy(producer);
258 }
259
260 spl::shared_ptr<cg_proxy> cg_producer_registry::get_proxy(
261                 const spl::shared_ptr<video_channel>& video_channel,
262                 int render_layer) const
263 {
264         return impl_->get_proxy(video_channel, render_layer);
265 }
266
267 spl::shared_ptr<cg_proxy> cg_producer_registry::get_or_create_proxy(
268                 const spl::shared_ptr<video_channel>& video_channel,
269                 int render_layer,
270                 const std::wstring& filename) const
271 {
272         return impl_->get_or_create_proxy(video_channel, render_layer, filename);
273 }
274
275 std::string cg_producer_registry::read_meta_info(const std::wstring& filename) const
276 {
277         return impl_->read_meta_info(filename);
278 }
279
280 bool cg_producer_registry::is_cg_extension(const std::wstring& extension) const
281 {
282         return impl_->is_cg_extension(extension);
283 }
284
285 }}