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