]> git.sesse.net Git - casparcg/blob - core/consumer/frame_consumer.cpp
* Refactored so that frame_consumers are stored in a frame_consumer_registry instance...
[casparcg] / core / consumer / frame_consumer.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_consumer.h"
25
26 #include <common/except.h>
27 #include <common/future.h>
28
29 #include <core/video_format.h>
30 #include <core/frame/frame.h>
31
32 #include <boost/thread.hpp>
33
34 #include <future>
35 #include <vector>
36 #include <map>
37
38 namespace caspar { namespace core {
39
40 struct frame_consumer_registry::impl
41 {
42         std::vector<consumer_factory_t> consumer_factories;
43         std::map<std::wstring, preconfigured_consumer_factory_t> preconfigured_consumer_factories;
44 };
45
46 frame_consumer_registry::frame_consumer_registry()
47         : impl_(new impl)
48 {
49 }
50
51 void frame_consumer_registry::register_consumer_factory(const consumer_factory_t& factory)
52 {
53         impl_->consumer_factories.push_back(factory);
54 }
55
56 void frame_consumer_registry::register_preconfigured_consumer_factory(
57                 const std::wstring& element_name,
58                 const preconfigured_consumer_factory_t& factory)
59 {
60         impl_->preconfigured_consumer_factories.insert(std::make_pair(element_name, factory));
61 }
62
63 class destroy_consumer_proxy : public frame_consumer
64 {       
65         std::shared_ptr<frame_consumer> consumer_;
66 public:
67         destroy_consumer_proxy(spl::shared_ptr<frame_consumer>&& consumer) 
68                 : consumer_(std::move(consumer))
69         {
70         }
71
72         ~destroy_consumer_proxy()
73         {               
74                 static tbb::atomic<int> counter;
75                 static std::once_flag counter_init_once;
76                 std::call_once(counter_init_once, []{ counter = 0; });
77                         
78                 ++counter;
79                 CASPAR_VERIFY(counter < 8);
80                 
81                 auto consumer = new std::shared_ptr<frame_consumer>(std::move(consumer_));
82                 boost::thread([=]
83                 {
84                         std::unique_ptr<std::shared_ptr<frame_consumer>> pointer_guard(consumer);
85
86                         auto str = (*consumer)->print();
87                         try
88                         {
89                                 if(!consumer->unique())
90                                         CASPAR_LOG(trace) << str << L" Not destroyed on asynchronous destruction thread: " << consumer->use_count();
91                                 else
92                                         CASPAR_LOG(trace) << str << L" Destroying on asynchronous destruction thread.";
93                         }
94                         catch(...){}
95
96                         pointer_guard.reset();
97
98                 }).detach(); 
99         }
100         
101         std::future<bool> send(const_frame frame) override                                                                                                      {return consumer_->send(std::move(frame));}
102         virtual void initialize(const video_format_desc& format_desc, int channel_index)        override        {return consumer_->initialize(format_desc, channel_index);}
103         std::wstring print() const override                                                                                                                                     {return consumer_->print();}    
104         std::wstring name() const override                                                                                                                                      {return consumer_->name();}
105         boost::property_tree::wptree info() const override                                                                                                      {return consumer_->info();}
106         bool has_synchronization_clock() const override                                                                                                         {return consumer_->has_synchronization_clock();}
107         int buffer_depth() const override                                                                                                                                       {return consumer_->buffer_depth();}
108         int index() const override                                                                                                                                                      {return consumer_->index();}
109         monitor::subject& monitor_output() override                                                                                                                     {return consumer_->monitor_output();}                                                                           
110 };
111
112 class print_consumer_proxy : public frame_consumer
113 {       
114         std::shared_ptr<frame_consumer> consumer_;
115 public:
116         print_consumer_proxy(spl::shared_ptr<frame_consumer>&& consumer) 
117                 : consumer_(std::move(consumer))
118         {
119                 CASPAR_LOG(info) << consumer_->print() << L" Initialized.";
120         }
121
122         ~print_consumer_proxy()
123         {               
124                 auto str = consumer_->print();
125                 CASPAR_LOG(trace) << str << L" Uninitializing.";
126                 consumer_.reset();
127                 CASPAR_LOG(info) << str << L" Uninitialized.";
128         }
129         
130         std::future<bool> send(const_frame frame) override                                                                                                      {return consumer_->send(std::move(frame));}
131         virtual void initialize(const video_format_desc& format_desc, int channel_index)        override        {return consumer_->initialize(format_desc, channel_index);}
132         std::wstring print() const override                                                                                                                                     {return consumer_->print();}
133         std::wstring name() const override                                                                                                                                      {return consumer_->name();}
134         boost::property_tree::wptree info() const override                                                                                                      {return consumer_->info();}
135         bool has_synchronization_clock() const override                                                                                                         {return consumer_->has_synchronization_clock();}
136         int buffer_depth() const override                                                                                                                                       {return consumer_->buffer_depth();}
137         int index() const override                                                                                                                                                      {return consumer_->index();}
138         monitor::subject& monitor_output() override                                                                                                                     {return consumer_->monitor_output();}                                                                           
139 };
140
141 class recover_consumer_proxy : public frame_consumer
142 {       
143         std::shared_ptr<frame_consumer> consumer_;
144         int                                                             channel_index_  = -1;
145         video_format_desc                               format_desc_;
146 public:
147         recover_consumer_proxy(spl::shared_ptr<frame_consumer>&& consumer) 
148                 : consumer_(std::move(consumer))
149         {
150         }
151         
152         virtual std::future<bool> send(const_frame frame)                                       
153         {
154                 try
155                 {
156                         return consumer_->send(frame);
157                 }
158                 catch(...)
159                 {
160                         CASPAR_LOG_CURRENT_EXCEPTION();
161                         try
162                         {
163                                 consumer_->initialize(format_desc_, channel_index_);
164                                 return consumer_->send(frame);
165                         }
166                         catch(...)
167                         {
168                                 CASPAR_LOG_CURRENT_EXCEPTION();
169                                 CASPAR_LOG(error) << print() << " Failed to recover consumer.";
170                                 return make_ready_future(false);
171                         }
172                 }
173         }
174
175         virtual void initialize(const video_format_desc& format_desc, int channel_index)                
176         {
177                 format_desc_    = format_desc;
178                 channel_index_  = channel_index;
179                 return consumer_->initialize(format_desc, channel_index);
180         }
181
182         std::wstring print() const override                                                                             {return consumer_->print();}
183         std::wstring name() const override                                                                              {return consumer_->name();}
184         boost::property_tree::wptree info() const override                                              {return consumer_->info();}
185         bool has_synchronization_clock() const override                                                 {return consumer_->has_synchronization_clock();}
186         int buffer_depth() const override                                                                               {return consumer_->buffer_depth();}
187         int index() const override                                                                                              {return consumer_->index();}
188         monitor::subject& monitor_output() override                                                             {return consumer_->monitor_output();}                                                                           
189 };
190
191 // This class is used to guarantee that audio cadence is correct. This is important for NTSC audio.
192 class cadence_guard : public frame_consumer
193 {
194         spl::shared_ptr<frame_consumer>         consumer_;
195         std::vector<int>                                        audio_cadence_;
196         video_format_desc                                       format_desc_;
197         boost::circular_buffer<std::size_t>     sync_buffer_;
198 public:
199         cadence_guard(const spl::shared_ptr<frame_consumer>& consumer)
200                 : consumer_(consumer)
201         {
202         }
203         
204         void initialize(const video_format_desc& format_desc, int channel_index) override
205         {
206                 audio_cadence_  = format_desc.audio_cadence;
207                 sync_buffer_    = boost::circular_buffer<std::size_t>(format_desc.audio_cadence.size());
208                 format_desc_    = format_desc;
209                 consumer_->initialize(format_desc, channel_index);
210         }
211
212         std::future<bool> send(const_frame frame) override
213         {               
214                 if(audio_cadence_.size() == 1)
215                         return consumer_->send(frame);
216
217                 std::future<bool> result = make_ready_future(true);
218                 
219                 if(boost::range::equal(sync_buffer_, audio_cadence_) && audio_cadence_.front() * format_desc_.audio_channels == static_cast<int>(frame.audio_data().size())) 
220                 {       
221                         // Audio sent so far is in sync, now we can send the next chunk.
222                         result = consumer_->send(frame);
223                         boost::range::rotate(audio_cadence_, std::begin(audio_cadence_)+1);
224                 }
225                 else
226                         CASPAR_LOG(trace) << print() << L" Syncing audio.";
227
228                 sync_buffer_.push_back(static_cast<int>(frame.audio_data().size() / format_desc_.audio_channels));
229                 
230                 return std::move(result);
231         }
232         
233         std::wstring print() const override                                                                             {return consumer_->print();}
234         std::wstring name() const override                                                                              {return consumer_->name();}
235         boost::property_tree::wptree info() const override                                              {return consumer_->info();}
236         bool has_synchronization_clock() const override                                                 {return consumer_->has_synchronization_clock();}
237         int buffer_depth() const override                                                                               {return consumer_->buffer_depth();}
238         int index() const override                                                                                              {return consumer_->index();}
239         monitor::subject& monitor_output() override                                                             {return consumer_->monitor_output();}                                                                           
240 };
241
242 spl::shared_ptr<core::frame_consumer> frame_consumer_registry::create_consumer(
243                 const std::vector<std::wstring>& params, interaction_sink* sink) const
244 {
245         if(params.empty())
246                 CASPAR_THROW_EXCEPTION(invalid_argument() << arg_name_info("params") << arg_value_info(""));
247         
248         auto consumer = frame_consumer::empty();
249         auto& consumer_factories = impl_->consumer_factories;
250         std::any_of(consumer_factories.begin(), consumer_factories.end(), [&](const consumer_factory_t& factory) -> bool
251                 {
252                         try
253                         {
254                                 consumer = factory(params, sink);
255                         }
256                         catch(...)
257                         {
258                                 CASPAR_LOG_CURRENT_EXCEPTION();
259                         }
260                         return consumer != frame_consumer::empty();
261                 });
262
263         if(consumer == frame_consumer::empty())
264                 CASPAR_THROW_EXCEPTION(file_not_found() << msg_info("No match found for supplied commands. Check syntax."));
265
266         return spl::make_shared<destroy_consumer_proxy>(
267                         spl::make_shared<print_consumer_proxy>(
268                          spl::make_shared<recover_consumer_proxy>(
269                           spl::make_shared<cadence_guard>(
270                            std::move(consumer)))));
271 }
272
273 spl::shared_ptr<frame_consumer> frame_consumer_registry::create_consumer(
274                 const std::wstring& element_name,
275                 const boost::property_tree::wptree& element,
276                 interaction_sink* sink) const
277 {
278         auto& preconfigured_consumer_factories = impl_->preconfigured_consumer_factories;
279         auto found = preconfigured_consumer_factories.find(element_name);
280
281         if (found == preconfigured_consumer_factories.end())
282                 CASPAR_THROW_EXCEPTION(file_not_found()
283                         << msg_info(L"No consumer factory registered for element name " + element_name));
284
285         return spl::make_shared<destroy_consumer_proxy>(
286                         spl::make_shared<print_consumer_proxy>(
287                                         spl::make_shared<recover_consumer_proxy>(
288                                                         spl::make_shared<cadence_guard>(
289                                                                         found->second(element, sink)))));
290 }
291
292 const spl::shared_ptr<frame_consumer>& frame_consumer::empty()
293 {
294         class empty_frame_consumer : public frame_consumer
295         {
296         public:
297                 std::future<bool> send(const_frame) override { return make_ready_future(false); }
298                 void initialize(const video_format_desc&, int) override{}
299                 std::wstring print() const override {return L"empty";}
300                 std::wstring name() const override {return L"empty";}
301                 bool has_synchronization_clock() const override {return false;}
302                 int buffer_depth() const override {return 0;};
303                 virtual int index() const{return -1;}
304                 monitor::subject& monitor_output() override {static monitor::subject monitor_subject(""); return monitor_subject;}                                                                              
305                 boost::property_tree::wptree info() const override
306                 {
307                         boost::property_tree::wptree info;
308                         info.add(L"type", L"empty");
309                         return info;
310                 }
311         };
312         static spl::shared_ptr<frame_consumer> consumer = spl::make_shared<empty_frame_consumer>();
313         return consumer;
314 }
315
316 }}