]> git.sesse.net Git - casparcg/blob - core/consumer/frame_consumer.cpp
Merge branch '2.1.0' of https://github.com/CasparCG/Server into 2.1.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 namespace caspar { namespace core {
35                 
36 std::vector<const consumer_factory_t> g_factories;
37
38 void register_consumer_factory(const consumer_factory_t& factory)
39 {
40         g_factories.push_back(factory);
41 }
42
43 class destroy_consumer_proxy : public frame_consumer
44 {       
45         std::shared_ptr<frame_consumer> consumer_;
46 public:
47         destroy_consumer_proxy(spl::shared_ptr<frame_consumer>&& consumer) 
48                 : consumer_(std::move(consumer))
49         {
50         }
51
52         ~destroy_consumer_proxy()
53         {               
54                 static tbb::atomic<int> counter = tbb::atomic<int>();
55                         
56                 ++counter;
57                 CASPAR_VERIFY(counter < 8);
58                 
59                 auto consumer = new std::shared_ptr<frame_consumer>(std::move(consumer_));
60                 boost::thread([=]
61                 {
62                         std::unique_ptr<std::shared_ptr<frame_consumer>> pointer_guard(consumer);
63
64                         auto str = (*consumer)->print();
65                         try
66                         {
67                                 if(!consumer->unique())
68                                         CASPAR_LOG(trace) << str << L" Not destroyed on asynchronous destruction thread: " << consumer->use_count();
69                                 else
70                                         CASPAR_LOG(trace) << str << L" Destroying on asynchronous destruction thread.";
71                         }
72                         catch(...){}
73
74                         pointer_guard.reset();
75
76                         --counter;
77                 }).detach(); 
78         }
79         
80         boost::unique_future<bool> send(const_frame frame) override                                                                                     {return consumer_->send(std::move(frame));}
81         virtual void initialize(const struct video_format_desc& format_desc, int channel_index) override        {return consumer_->initialize(format_desc, channel_index);}
82         std::wstring print() const override                                                                                                                                     {return consumer_->print();}    
83         std::wstring name() const override                                                                                                                                      {return consumer_->name();}
84         boost::property_tree::wptree info() const override                                                                                                      {return consumer_->info();}
85         bool has_synchronization_clock() const override                                                                                                         {return consumer_->has_synchronization_clock();}
86         int buffer_depth() const override                                                                                                                                       {return consumer_->buffer_depth();}
87         int index() const override                                                                                                                                                      {return consumer_->index();}
88         monitor::source& monitor_output() override                                                                                                                      {return consumer_->monitor_output();}                                                                           
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         boost::unique_future<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         monitor::source& monitor_output() override                                                                                                      {return consumer_->monitor_output();}                                                                           
118 };
119
120 class recover_consumer_proxy : public frame_consumer
121 {       
122         std::shared_ptr<frame_consumer> consumer_;
123         int                                                             channel_index_;
124         video_format_desc                               format_desc_;
125 public:
126         recover_consumer_proxy(spl::shared_ptr<frame_consumer>&& consumer) 
127                 : consumer_(std::move(consumer))
128         {
129         }
130         
131         virtual boost::unique_future<bool> send(const_frame frame)                                      
132         {
133                 try
134                 {
135                         return consumer_->send(frame);
136                 }
137                 catch(...)
138                 {
139                         CASPAR_LOG_CURRENT_EXCEPTION();
140                         try
141                         {
142                                 consumer_->initialize(format_desc_, channel_index_);
143                                 return consumer_->send(frame);
144                         }
145                         catch(...)
146                         {
147                                 CASPAR_LOG_CURRENT_EXCEPTION();
148                                 CASPAR_LOG(error) << print() << " Failed to recover consumer.";
149                                 return wrap_as_future(false);
150                         }
151                 }
152         }
153
154         virtual void initialize(const struct video_format_desc& format_desc, int channel_index)         
155         {
156                 format_desc_    = format_desc;
157                 channel_index_  = channel_index;
158                 return consumer_->initialize(format_desc, channel_index);
159         }
160
161         std::wstring print() const override                                                                             {return consumer_->print();}
162         std::wstring name() const override                                                                              {return consumer_->name();}
163         boost::property_tree::wptree info() const override                                              {return consumer_->info();}
164         bool has_synchronization_clock() const override                                                 {return consumer_->has_synchronization_clock();}
165         int buffer_depth() const override                                                                               {return consumer_->buffer_depth();}
166         int index() const override                                                                                              {return consumer_->index();}
167         monitor::source& monitor_output() override                                                              {return consumer_->monitor_output();}                                                                           
168 };
169
170 // This class is used to guarantee that audio cadence is correct. This is important for NTSC audio.
171 class cadence_guard : public frame_consumer
172 {
173         spl::shared_ptr<frame_consumer>         consumer_;
174         std::vector<int>                                        audio_cadence_;
175         video_format_desc                                       format_desc_;
176         boost::circular_buffer<std::size_t>     sync_buffer_;
177 public:
178         cadence_guard(const spl::shared_ptr<frame_consumer>& consumer)
179                 : consumer_(consumer)
180         {
181         }
182         
183         void initialize(const video_format_desc& format_desc, int channel_index) override
184         {
185                 audio_cadence_  = format_desc.audio_cadence;
186                 sync_buffer_    = boost::circular_buffer<std::size_t>(format_desc.audio_cadence.size());
187                 format_desc_    = format_desc;
188                 consumer_->initialize(format_desc, channel_index);
189         }
190
191         boost::unique_future<bool> send(const_frame frame) override
192         {               
193                 if(audio_cadence_.size() == 1)
194                         return consumer_->send(frame);
195
196                 boost::unique_future<bool> result = wrap_as_future(true);
197                 
198                 if(boost::range::equal(sync_buffer_, audio_cadence_) && audio_cadence_.front() * format_desc_.audio_channels == 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() / format_desc_.audio_channels));
208                 
209                 return std::move(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         monitor::source& monitor_output() override                                                              {return consumer_->monitor_output();}                                                                           
219 };
220
221 spl::shared_ptr<core::frame_consumer> create_consumer(const std::vector<std::wstring>& params)
222 {
223         if(params.empty())
224                 CASPAR_THROW_EXCEPTION(invalid_argument() << arg_name_info("params") << arg_value_info(""));
225         
226         auto consumer = frame_consumer::empty();
227         std::any_of(g_factories.begin(), g_factories.end(), [&](const consumer_factory_t& factory) -> bool
228                 {
229                         try
230                         {
231                                 consumer = factory(params);
232                         }
233                         catch(...)
234                         {
235                                 CASPAR_LOG_CURRENT_EXCEPTION();
236                         }
237                         return consumer != frame_consumer::empty();
238                 });
239
240         if(consumer == frame_consumer::empty())
241                 CASPAR_THROW_EXCEPTION(file_not_found() << msg_info("No match found for supplied commands. Check syntax."));
242
243         return spl::make_shared<destroy_consumer_proxy>(
244                         spl::make_shared<print_consumer_proxy>(
245                          spl::make_shared<recover_consumer_proxy>(
246                           spl::make_shared<cadence_guard>(
247                            std::move(consumer)))));
248 }
249
250 const spl::shared_ptr<frame_consumer>& frame_consumer::empty()
251 {
252         class empty_frame_consumer : public frame_consumer
253         {
254         public:
255                 boost::unique_future<bool> send(const_frame) override {return wrap_as_future(false);}
256                 void initialize(const video_format_desc&, int) override{}
257                 std::wstring print() const override {return L"empty";}
258                 std::wstring name() const override {return L"empty";}
259                 bool has_synchronization_clock() const override {return false;}
260                 int buffer_depth() const override {return 0;};
261                 virtual int index() const{return -1;}
262                 monitor::source& monitor_output() override {static monitor::subject monitor_subject(""); return monitor_subject;}                                                                               
263                 boost::property_tree::wptree info() const override
264                 {
265                         boost::property_tree::wptree info;
266                         info.add(L"type", L"empty");
267                         return info;
268                 }
269         };
270         static spl::shared_ptr<frame_consumer> consumer = spl::make_shared<empty_frame_consumer>();
271         return consumer;
272 }
273
274 }}