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