2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
4 * This file is part of CasparCG (www.casparcg.com).
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.
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.
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/>.
19 * Author: Robert Nagy, ronag89@gmail.com
22 #include "../StdAfx.h"
24 #include "frame_consumer.h"
26 #include <common/except.h>
27 #include <common/future.h>
29 #include <core/video_format.h>
30 #include <core/frame/frame.h>
32 #include <boost/thread.hpp>
38 namespace caspar { namespace core {
40 struct frame_consumer_registry::impl
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;
46 impl(spl::shared_ptr<help_repository> help_repo)
47 : help_repo(std::move(help_repo))
52 frame_consumer_registry::frame_consumer_registry(spl::shared_ptr<help_repository> help_repo)
53 : impl_(new impl(std::move(help_repo)))
57 void frame_consumer_registry::register_consumer_factory(const std::wstring& name, const consumer_factory_t& factory, const help_item_describer& describer)
59 impl_->consumer_factories.push_back(factory);
60 impl_->help_repo->register_item({ L"consumer" }, std::move(name), describer);
63 void frame_consumer_registry::register_preconfigured_consumer_factory(
64 const std::wstring& element_name,
65 const preconfigured_consumer_factory_t& factory)
67 impl_->preconfigured_consumer_factories.insert(std::make_pair(element_name, factory));
70 class destroy_consumer_proxy : public frame_consumer
72 std::shared_ptr<frame_consumer> consumer_;
74 destroy_consumer_proxy(spl::shared_ptr<frame_consumer>&& consumer)
75 : consumer_(std::move(consumer))
79 ~destroy_consumer_proxy()
81 static tbb::atomic<int> counter;
82 static std::once_flag counter_init_once;
83 std::call_once(counter_init_once, []{ counter = 0; });
86 CASPAR_VERIFY(counter < 8);
88 auto consumer = new std::shared_ptr<frame_consumer>(std::move(consumer_));
91 std::unique_ptr<std::shared_ptr<frame_consumer>> pointer_guard(consumer);
93 auto str = (*consumer)->print();
96 if(!consumer->unique())
97 CASPAR_LOG(trace) << str << L" Not destroyed on asynchronous destruction thread: " << consumer->use_count();
99 CASPAR_LOG(trace) << str << L" Destroying on asynchronous destruction thread.";
103 pointer_guard.reset();
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 monitor::subject& monitor_output() override {return consumer_->monitor_output();}
119 class print_consumer_proxy : public frame_consumer
121 std::shared_ptr<frame_consumer> consumer_;
123 print_consumer_proxy(spl::shared_ptr<frame_consumer>&& consumer)
124 : consumer_(std::move(consumer))
126 CASPAR_LOG(info) << consumer_->print() << L" Initialized.";
129 ~print_consumer_proxy()
131 auto str = consumer_->print();
132 CASPAR_LOG(trace) << str << L" Uninitializing.";
134 CASPAR_LOG(info) << str << L" Uninitialized.";
137 std::future<bool> send(const_frame frame) override {return consumer_->send(std::move(frame));}
138 virtual void initialize(const video_format_desc& format_desc, int channel_index) override {return consumer_->initialize(format_desc, channel_index);}
139 std::wstring print() const override {return consumer_->print();}
140 std::wstring name() const override {return consumer_->name();}
141 boost::property_tree::wptree info() const override {return consumer_->info();}
142 bool has_synchronization_clock() const override {return consumer_->has_synchronization_clock();}
143 int buffer_depth() const override {return consumer_->buffer_depth();}
144 int index() const override {return consumer_->index();}
145 monitor::subject& monitor_output() override {return consumer_->monitor_output();}
148 class recover_consumer_proxy : public frame_consumer
150 std::shared_ptr<frame_consumer> consumer_;
151 int channel_index_ = -1;
152 video_format_desc format_desc_;
154 recover_consumer_proxy(spl::shared_ptr<frame_consumer>&& consumer)
155 : consumer_(std::move(consumer))
159 virtual std::future<bool> send(const_frame frame)
163 return consumer_->send(frame);
167 CASPAR_LOG_CURRENT_EXCEPTION();
170 consumer_->initialize(format_desc_, channel_index_);
171 return consumer_->send(frame);
175 CASPAR_LOG_CURRENT_EXCEPTION();
176 CASPAR_LOG(error) << print() << " Failed to recover consumer.";
177 return make_ready_future(false);
182 virtual void initialize(const video_format_desc& format_desc, int channel_index)
184 format_desc_ = format_desc;
185 channel_index_ = channel_index;
186 return consumer_->initialize(format_desc, channel_index);
189 std::wstring print() const override {return consumer_->print();}
190 std::wstring name() const override {return consumer_->name();}
191 boost::property_tree::wptree info() const override {return consumer_->info();}
192 bool has_synchronization_clock() const override {return consumer_->has_synchronization_clock();}
193 int buffer_depth() const override {return consumer_->buffer_depth();}
194 int index() const override {return consumer_->index();}
195 monitor::subject& monitor_output() override {return consumer_->monitor_output();}
198 // This class is used to guarantee that audio cadence is correct. This is important for NTSC audio.
199 class cadence_guard : public frame_consumer
201 spl::shared_ptr<frame_consumer> consumer_;
202 std::vector<int> audio_cadence_;
203 video_format_desc format_desc_;
204 boost::circular_buffer<std::size_t> sync_buffer_;
206 cadence_guard(const spl::shared_ptr<frame_consumer>& consumer)
207 : consumer_(consumer)
211 void initialize(const video_format_desc& format_desc, int channel_index) override
213 audio_cadence_ = format_desc.audio_cadence;
214 sync_buffer_ = boost::circular_buffer<std::size_t>(format_desc.audio_cadence.size());
215 format_desc_ = format_desc;
216 consumer_->initialize(format_desc, channel_index);
219 std::future<bool> send(const_frame frame) override
221 if(audio_cadence_.size() == 1)
222 return consumer_->send(frame);
224 std::future<bool> result = make_ready_future(true);
226 if(boost::range::equal(sync_buffer_, audio_cadence_) && audio_cadence_.front() * format_desc_.audio_channels == static_cast<int>(frame.audio_data().size()))
228 // Audio sent so far is in sync, now we can send the next chunk.
229 result = consumer_->send(frame);
230 boost::range::rotate(audio_cadence_, std::begin(audio_cadence_)+1);
233 CASPAR_LOG(trace) << print() << L" Syncing audio.";
235 sync_buffer_.push_back(static_cast<int>(frame.audio_data().size() / format_desc_.audio_channels));
237 return std::move(result);
240 std::wstring print() const override {return consumer_->print();}
241 std::wstring name() const override {return consumer_->name();}
242 boost::property_tree::wptree info() const override {return consumer_->info();}
243 bool has_synchronization_clock() const override {return consumer_->has_synchronization_clock();}
244 int buffer_depth() const override {return consumer_->buffer_depth();}
245 int index() const override {return consumer_->index();}
246 monitor::subject& monitor_output() override {return consumer_->monitor_output();}
249 spl::shared_ptr<core::frame_consumer> frame_consumer_registry::create_consumer(
250 const std::vector<std::wstring>& params, interaction_sink* sink) const
253 CASPAR_THROW_EXCEPTION(invalid_argument() << arg_name_info("params") << arg_value_info(""));
255 auto consumer = frame_consumer::empty();
256 auto& consumer_factories = impl_->consumer_factories;
257 std::any_of(consumer_factories.begin(), consumer_factories.end(), [&](const consumer_factory_t& factory) -> bool
261 consumer = factory(params, sink);
265 CASPAR_LOG_CURRENT_EXCEPTION();
267 return consumer != frame_consumer::empty();
270 if(consumer == frame_consumer::empty())
271 CASPAR_THROW_EXCEPTION(file_not_found() << msg_info("No match found for supplied commands. Check syntax."));
273 return spl::make_shared<destroy_consumer_proxy>(
274 spl::make_shared<print_consumer_proxy>(
275 spl::make_shared<recover_consumer_proxy>(
276 spl::make_shared<cadence_guard>(
277 std::move(consumer)))));
280 spl::shared_ptr<frame_consumer> frame_consumer_registry::create_consumer(
281 const std::wstring& element_name,
282 const boost::property_tree::wptree& element,
283 interaction_sink* sink) const
285 auto& preconfigured_consumer_factories = impl_->preconfigured_consumer_factories;
286 auto found = preconfigured_consumer_factories.find(element_name);
288 if (found == preconfigured_consumer_factories.end())
289 CASPAR_THROW_EXCEPTION(file_not_found()
290 << msg_info(L"No consumer factory registered for element name " + element_name));
292 return spl::make_shared<destroy_consumer_proxy>(
293 spl::make_shared<print_consumer_proxy>(
294 spl::make_shared<recover_consumer_proxy>(
295 spl::make_shared<cadence_guard>(
296 found->second(element, sink)))));
299 const spl::shared_ptr<frame_consumer>& frame_consumer::empty()
301 class empty_frame_consumer : public frame_consumer
304 std::future<bool> send(const_frame) override { return make_ready_future(false); }
305 void initialize(const video_format_desc&, int) override{}
306 std::wstring print() const override {return L"empty";}
307 std::wstring name() const override {return L"empty";}
308 bool has_synchronization_clock() const override {return false;}
309 int buffer_depth() const override {return 0;};
310 virtual int index() const{return -1;}
311 monitor::subject& monitor_output() override {static monitor::subject monitor_subject(""); return monitor_subject;}
312 boost::property_tree::wptree info() const override
314 boost::property_tree::wptree info;
315 info.add(L"type", L"empty");
319 static spl::shared_ptr<frame_consumer> consumer = spl::make_shared<empty_frame_consumer>();