]> git.sesse.net Git - casparcg/blob - core/producer/frame_producer.cpp
* Enforce help descriptions for producers in code.
[casparcg] / core / producer / frame_producer.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: Robert Nagy, ronag89@gmail.com
20 */
21
22 #include "../StdAfx.h"
23
24 #include "frame_producer.h"
25
26 #include "../frame/draw_frame.h"
27 #include "../frame/frame_transform.h"
28
29 #include "color/color_producer.h"
30 #include "draw/freehand_producer.h"
31 #include "separated/separated_producer.h"
32 #include "variable.h"
33
34 #include <common/assert.h>
35 #include <common/except.h>
36 #include <common/executor.h>
37 #include <common/future.h>
38 #include <common/memory.h>
39
40 #include <boost/thread.hpp>
41
42 namespace caspar { namespace core {
43
44 struct frame_producer_registry::impl
45 {
46         std::vector<producer_factory_t>         producer_factories;
47         std::vector<producer_factory_t>         thumbnail_factories;
48         spl::shared_ptr<help_repository>        help_repo;
49
50         impl(spl::shared_ptr<help_repository> help_repo)
51                 : help_repo(std::move(help_repo))
52         {
53         }
54 };
55
56 frame_producer_registry::frame_producer_registry(spl::shared_ptr<help_repository> help_repo)
57     : impl_(new impl(std::move(help_repo)))
58 {
59 }
60
61 void frame_producer_registry::register_producer_factory(std::wstring name, const producer_factory_t& factory, const help_item_describer& describer)
62 {
63         impl_->producer_factories.push_back(factory);
64         impl_->help_repo->register_item({ L"producer" }, std::move(name), describer);
65 }
66
67 void frame_producer_registry::register_thumbnail_producer_factory(const producer_factory_t& factory)
68 {
69         impl_->thumbnail_factories.push_back(factory);
70 }
71
72 frame_producer_dependencies::frame_producer_dependencies(
73                 const spl::shared_ptr<core::frame_factory>& frame_factory,
74                 const std::vector<spl::shared_ptr<video_channel>>& channels,
75                 const video_format_desc& format_desc,
76                 const spl::shared_ptr<const frame_producer_registry> producer_registry)
77         : frame_factory(frame_factory)
78         , channels(channels)
79         , format_desc(format_desc)
80         , producer_registry(producer_registry)
81 {
82 }
83
84 constraints::constraints(double width, double height)
85         : width(width), height(height)
86 {
87 }
88
89 constraints::constraints()
90 {
91 }
92
93 struct frame_producer_base::impl
94 {
95         tbb::atomic<uint32_t>   frame_number_;
96         tbb::atomic<bool>               paused_;
97         frame_producer_base&    self_;
98         draw_frame                              last_frame_;
99
100         impl(frame_producer_base& self)
101                 : self_(self)
102                 , last_frame_(draw_frame::empty())
103         {
104                 frame_number_ = 0;
105                 paused_ = false;
106         }
107         
108         draw_frame receive()
109         {
110                 if(paused_)
111                         return self_.last_frame();
112
113                 auto frame = self_.receive_impl();
114                 if(frame == draw_frame::late())
115                         return self_.last_frame();
116
117                 ++frame_number_;
118
119                 return last_frame_ = draw_frame::push(frame);
120         }
121
122         void paused(bool value)
123         {
124                 paused_ = value;
125         }
126
127         draw_frame last_frame()
128         {
129                 return draw_frame::still(last_frame_);
130         }
131         draw_frame create_thumbnail_frame()
132         {
133                 return draw_frame::empty();
134         }
135 };
136
137 frame_producer_base::frame_producer_base() : impl_(new impl(*this))
138 {
139 }
140
141 draw_frame frame_producer_base::receive()
142 {
143         return impl_->receive();
144 }
145
146 void frame_producer_base::paused(bool value)
147 {
148         impl_->paused(value);
149 }
150
151 draw_frame frame_producer_base::last_frame()
152 {
153         return impl_->last_frame();
154 }
155 draw_frame frame_producer_base::create_thumbnail_frame()
156 {
157         return impl_->create_thumbnail_frame();
158 }
159
160 std::future<std::wstring> frame_producer_base::call(const std::vector<std::wstring>&) 
161 {
162         CASPAR_THROW_EXCEPTION(not_supported());
163 }
164
165 uint32_t frame_producer_base::nb_frames() const
166 {
167         return std::numeric_limits<uint32_t>::max();
168 }
169
170 uint32_t frame_producer_base::frame_number() const
171 {
172         return impl_->frame_number_;
173 }
174
175 variable& frame_producer_base::get_variable(const std::wstring& name)
176 {
177         CASPAR_THROW_EXCEPTION(caspar_exception() 
178                         << msg_info(L"No variable called " + name + L" found in " + print()));
179 }
180
181 const std::vector<std::wstring>& frame_producer_base::get_variables() const
182 {
183         static std::vector<std::wstring> empty;
184
185         return empty;
186 }
187
188 const spl::shared_ptr<frame_producer>& frame_producer::empty() 
189 {
190         class empty_frame_producer : public frame_producer
191         {
192         public:
193                 empty_frame_producer(){}
194                 draw_frame receive() override{return draw_frame::empty();}
195                 void paused(bool value) override{}
196                 uint32_t nb_frames() const override {return 0;}
197                 std::wstring print() const override { return L"empty";}
198                 monitor::subject& monitor_output() override {static monitor::subject monitor_subject(""); return monitor_subject;}                                                                              
199                 std::wstring name() const override {return L"empty";}
200                 uint32_t frame_number() const override {return 0;}
201                 std::future<std::wstring> call(const std::vector<std::wstring>& params) override{CASPAR_THROW_EXCEPTION(not_supported());}
202                 variable& get_variable(const std::wstring& name) override { CASPAR_THROW_EXCEPTION(not_supported()); }
203                 const std::vector<std::wstring>& get_variables() const override { static std::vector<std::wstring> empty; return empty; }
204                 draw_frame last_frame() {return draw_frame::empty();}
205                 draw_frame create_thumbnail_frame() {return draw_frame::empty();}
206                 constraints& pixel_constraints() override { static constraints c; return c; }
207         
208                 boost::property_tree::wptree info() const override
209                 {
210                         boost::property_tree::wptree info;
211                         info.add(L"type", L"empty-producer");
212                         return info;
213                 }
214         };
215
216         static spl::shared_ptr<frame_producer> producer = spl::make_shared<empty_frame_producer>();
217         return producer;
218 }       
219
220 class destroy_producer_proxy : public frame_producer
221 {       
222         std::shared_ptr<frame_producer> producer_;
223 public:
224         destroy_producer_proxy(spl::shared_ptr<frame_producer>&& producer) 
225                 : producer_(std::move(producer))
226         {
227         }
228
229         virtual ~destroy_producer_proxy()
230         {               
231                 static tbb::atomic<int> counter;
232                 static std::once_flag counter_init_once;
233                 std::call_once(counter_init_once, []{ counter = 0; });
234                 
235                 if(producer_ == core::frame_producer::empty())
236                         return;
237
238                 ++counter;
239                 CASPAR_VERIFY(counter < 8);
240                 
241                 auto producer = new spl::shared_ptr<frame_producer>(std::move(producer_));
242                 boost::thread([=]
243                 {
244                         std::unique_ptr<spl::shared_ptr<frame_producer>> pointer_guard(producer);
245                         auto str = (*producer)->print();
246                         try
247                         {
248                                 if(!producer->unique())
249                                         CASPAR_LOG(trace) << str << L" Not destroyed on asynchronous destruction thread: " << producer->use_count();
250                                 else
251                                         CASPAR_LOG(trace) << str << L" Destroying on asynchronous destruction thread.";
252                         }
253                         catch(...){}
254                         
255                         try
256                         {
257                                 pointer_guard.reset();
258                                 CASPAR_LOG(info) << str << L" Destroyed.";
259                         }
260                         catch(...)
261                         {
262                                 CASPAR_LOG_CURRENT_EXCEPTION();
263                         }
264
265                         --counter;
266                 }).detach(); 
267         }
268         
269         draw_frame                                                                                      receive() override                                                                                                                                                                                                              {return producer_->receive();}
270         std::wstring                                                                            print() const override                                                                                                                  {return producer_->print();}
271         void                                                                                            paused(bool value) override                                                                                                             {producer_->paused(value);}
272         std::wstring                                                                            name() const override                                                                                                                   {return producer_->name();}
273         uint32_t                                                                                        frame_number() const override                                                                                                   {return producer_->frame_number();}
274         boost::property_tree::wptree                                            info() const override                                                                                                                   {return producer_->info();}
275         std::future<std::wstring>                                                       call(const std::vector<std::wstring>& params) override                                                  {return producer_->call(params);}
276         variable&                                                                                       get_variable(const std::wstring& name) override                                                                 {return producer_->get_variable(name);}
277         const std::vector<std::wstring>&                                        get_variables() const override                                                                                                  {return producer_->get_variables();}
278         void                                                                                            leading_producer(const spl::shared_ptr<frame_producer>& producer) override              {return producer_->leading_producer(producer);}
279         uint32_t                                                                                        nb_frames() const override                                                                                                              {return producer_->nb_frames();}
280         draw_frame                                                                                      last_frame()                                                                                                                                    {return producer_->last_frame();}
281         draw_frame                                                                                      create_thumbnail_frame()                                                                                                                {return producer_->create_thumbnail_frame();}
282         monitor::subject&                                                                       monitor_output() override                                                                                                               {return producer_->monitor_output();}                                                                           
283         bool                                                                                            collides(double x, double y) const override                                                                             {return producer_->collides(x, y);}
284         void                                                                                            on_interaction(const interaction_event::ptr& event)     override                                        {return producer_->on_interaction(event);}
285         constraints&                                                                            pixel_constraints() override                                                                                                    {return producer_->pixel_constraints();}
286 };
287
288 spl::shared_ptr<core::frame_producer> create_destroy_proxy(spl::shared_ptr<core::frame_producer> producer)
289 {
290         return spl::make_shared<destroy_producer_proxy>(std::move(producer));
291 }
292
293 spl::shared_ptr<core::frame_producer> do_create_producer(const frame_producer_dependencies& dependencies, const std::vector<std::wstring>& params, const std::vector<producer_factory_t>& factories, bool throw_on_fail = false)
294 {
295         if(params.empty())
296                 CASPAR_THROW_EXCEPTION(invalid_argument() << arg_name_info("params") << arg_value_info(""));
297         
298         auto producer = frame_producer::empty();
299         std::any_of(factories.begin(), factories.end(), [&](const producer_factory_t& factory) -> bool
300                 {
301                         try
302                         {
303                                 producer = factory(dependencies, params);
304                         }
305                         catch(...)
306                         {
307                                 if(throw_on_fail)
308                                         throw;
309                                 else
310                                         CASPAR_LOG_CURRENT_EXCEPTION();
311                         }
312                         return producer != frame_producer::empty();
313                 });
314
315         if(producer == frame_producer::empty())
316                 producer = create_color_producer(dependencies.frame_factory, params);
317
318         if (producer == frame_producer::empty())
319                 producer = create_freehand_producer(dependencies.frame_factory, params);
320
321         if(producer == frame_producer::empty())
322                 return producer;
323                 
324         return producer;
325 }
326
327 spl::shared_ptr<core::frame_producer> frame_producer_registry::create_thumbnail_producer(const frame_producer_dependencies& dependencies, const std::wstring& media_file) const
328 {
329         auto& thumbnail_factories = impl_->thumbnail_factories;
330         std::vector<std::wstring> params;
331         params.push_back(media_file);
332
333         auto producer = do_create_producer(dependencies, params, thumbnail_factories, true);
334         auto key_producer = frame_producer::empty();
335   
336         try // to find a key file.
337         {
338                 auto params_copy = params;
339                 if (params_copy.size() > 0)
340                 {
341                         params_copy[0] += L"_A";
342                         key_producer = do_create_producer(dependencies, params_copy, thumbnail_factories, true);
343                         if (key_producer == frame_producer::empty())
344                         {
345                                 params_copy[0] += L"LPHA";
346                                 key_producer = do_create_producer(dependencies, params_copy, thumbnail_factories, true);
347                         }
348                 }
349         }
350         catch(...){}
351
352         if (producer != frame_producer::empty() && key_producer != frame_producer::empty())
353                 return create_separated_producer(producer, key_producer);
354   
355         return producer;
356 }
357
358 spl::shared_ptr<core::frame_producer> frame_producer_registry::create_producer(const frame_producer_dependencies& dependencies, const std::vector<std::wstring>& params) const
359 {       
360         auto& producer_factories = impl_->producer_factories;
361         auto producer = do_create_producer(dependencies, params, producer_factories);
362         auto key_producer = frame_producer::empty();
363         
364         try // to find a key file.
365         {
366                 auto params_copy = params;
367                 if(params_copy.size() > 0)
368                 {
369                         params_copy[0] += L"_A";
370                         key_producer = do_create_producer(dependencies, params_copy, producer_factories);
371                         if(key_producer == frame_producer::empty())
372                         {
373                                 params_copy[0] += L"LPHA";
374                                 key_producer = do_create_producer(dependencies, params_copy, producer_factories);
375                         }
376                 }
377         }
378         catch(...){}
379
380         if(producer != frame_producer::empty() && key_producer != frame_producer::empty())
381                 return create_separated_producer(producer, key_producer);
382         
383         if(producer == frame_producer::empty())
384         {
385                 std::wstring str;
386                 for (auto& param : params)
387                         str += param + L" ";
388                 CASPAR_THROW_EXCEPTION(file_not_found() << msg_info("No match found for supplied commands. Check syntax.") << arg_value_info(u8(str)));
389         }
390
391         return producer;
392 }
393
394
395 spl::shared_ptr<core::frame_producer> frame_producer_registry::create_producer(const frame_producer_dependencies& dependencies, const std::wstring& params) const
396 {
397         std::wstringstream iss(params);
398         std::vector<std::wstring> tokens;
399         typedef std::istream_iterator<std::wstring, wchar_t, std::char_traits<wchar_t> > iterator;
400         std::copy(iterator(iss),  iterator(), std::back_inserter(tokens));
401         return create_producer(dependencies, tokens);
402 }
403
404 }}