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