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