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