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>
34 namespace caspar { namespace core {
36 std::vector<const consumer_factory_t> g_factories;
38 void register_consumer_factory(const consumer_factory_t& factory)
40 g_factories.push_back(factory);
43 class destroy_consumer_proxy : public frame_consumer
45 std::shared_ptr<frame_consumer> consumer_;
47 destroy_consumer_proxy(spl::shared_ptr<frame_consumer>&& consumer)
48 : consumer_(std::move(consumer))
52 ~destroy_consumer_proxy()
54 static tbb::atomic<int> counter = tbb::atomic<int>();
57 CASPAR_VERIFY(counter < 8);
59 auto consumer = new std::shared_ptr<frame_consumer>(std::move(consumer_));
62 std::unique_ptr<std::shared_ptr<frame_consumer>> pointer_guard(consumer);
64 auto str = (*consumer)->print();
67 if(!consumer->unique())
68 CASPAR_LOG(trace) << str << L" Not destroyed on asynchronous destruction thread: " << consumer->use_count();
70 CASPAR_LOG(trace) << str << L" Destroying on asynchronous destruction thread.";
74 pointer_guard.reset();
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 void subscribe(const monitor::observable::observer_ptr& o) override {consumer_->subscribe(o);}
89 void unsubscribe(const monitor::observable::observer_ptr& o) override {consumer_->unsubscribe(o);}
92 class print_consumer_proxy : public frame_consumer
94 std::shared_ptr<frame_consumer> consumer_;
96 print_consumer_proxy(spl::shared_ptr<frame_consumer>&& consumer)
97 : consumer_(std::move(consumer))
99 CASPAR_LOG(info) << consumer_->print() << L" Initialized.";
102 ~print_consumer_proxy()
104 auto str = consumer_->print();
105 CASPAR_LOG(trace) << str << L" Uninitializing.";
107 CASPAR_LOG(info) << str << L" Uninitialized.";
110 boost::unique_future<bool> send(const_frame frame) override {return consumer_->send(std::move(frame));}
111 virtual void initialize(const struct video_format_desc& format_desc, int channel_index) override {return consumer_->initialize(format_desc, channel_index);}
112 std::wstring print() const override {return consumer_->print();}
113 std::wstring name() const override {return consumer_->name();}
114 boost::property_tree::wptree info() const override {return consumer_->info();}
115 bool has_synchronization_clock() const override {return consumer_->has_synchronization_clock();}
116 int buffer_depth() const override {return consumer_->buffer_depth();}
117 int index() const override {return consumer_->index();}
118 void subscribe(const monitor::observable::observer_ptr& o) override {consumer_->subscribe(o);}
119 void unsubscribe(const monitor::observable::observer_ptr& o) override {consumer_->unsubscribe(o);}
122 class recover_consumer_proxy : public frame_consumer
124 std::shared_ptr<frame_consumer> consumer_;
126 video_format_desc format_desc_;
128 recover_consumer_proxy(spl::shared_ptr<frame_consumer>&& consumer)
129 : consumer_(std::move(consumer))
133 virtual boost::unique_future<bool> send(const_frame frame)
137 return consumer_->send(frame);
141 CASPAR_LOG_CURRENT_EXCEPTION();
144 consumer_->initialize(format_desc_, channel_index_);
145 return consumer_->send(frame);
149 CASPAR_LOG_CURRENT_EXCEPTION();
150 CASPAR_LOG(error) << print() << " Failed to recover consumer.";
151 return wrap_as_future(false);
156 virtual void initialize(const struct video_format_desc& format_desc, int channel_index)
158 format_desc_ = format_desc;
159 channel_index_ = channel_index;
160 return consumer_->initialize(format_desc, channel_index);
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 void subscribe(const monitor::observable::observer_ptr& o) override {consumer_->subscribe(o);}
170 void unsubscribe(const monitor::observable::observer_ptr& o) override {consumer_->unsubscribe(o);}
173 // This class is used to guarantee that audio cadence is correct. This is important for NTSC audio.
174 class cadence_guard : public frame_consumer
176 spl::shared_ptr<frame_consumer> consumer_;
177 std::vector<int> audio_cadence_;
178 video_format_desc format_desc_;
179 boost::circular_buffer<std::size_t> sync_buffer_;
181 cadence_guard(const spl::shared_ptr<frame_consumer>& consumer)
182 : consumer_(consumer)
186 void initialize(const video_format_desc& format_desc, int channel_index) override
188 audio_cadence_ = format_desc.audio_cadence;
189 sync_buffer_ = boost::circular_buffer<std::size_t>(format_desc.audio_cadence.size());
190 format_desc_ = format_desc;
191 consumer_->initialize(format_desc, channel_index);
194 boost::unique_future<bool> send(const_frame frame) override
196 if(audio_cadence_.size() == 1)
197 return consumer_->send(frame);
199 boost::unique_future<bool> result = wrap_as_future(true);
201 if(boost::range::equal(sync_buffer_, audio_cadence_) && audio_cadence_.front() * format_desc_.audio_channels == static_cast<int>(frame.audio_data().size()))
203 // Audio sent so far is in sync, now we can send the next chunk.
204 result = consumer_->send(frame);
205 boost::range::rotate(audio_cadence_, std::begin(audio_cadence_)+1);
208 CASPAR_LOG(trace) << print() << L" Syncing audio.";
210 sync_buffer_.push_back(static_cast<int>(frame.audio_data().size() / format_desc_.audio_channels));
212 return std::move(result);
215 std::wstring print() const override {return consumer_->print();}
216 std::wstring name() const override {return consumer_->name();}
217 boost::property_tree::wptree info() const override {return consumer_->info();}
218 bool has_synchronization_clock() const override {return consumer_->has_synchronization_clock();}
219 int buffer_depth() const override {return consumer_->buffer_depth();}
220 int index() const override {return consumer_->index();}
221 void subscribe(const monitor::observable::observer_ptr& o) override {consumer_->subscribe(o);}
222 void unsubscribe(const monitor::observable::observer_ptr& o) override {consumer_->unsubscribe(o);}
225 spl::shared_ptr<core::frame_consumer> create_consumer(const std::vector<std::wstring>& params)
228 CASPAR_THROW_EXCEPTION(invalid_argument() << arg_name_info("params") << arg_value_info(""));
230 auto consumer = frame_consumer::empty();
231 std::any_of(g_factories.begin(), g_factories.end(), [&](const consumer_factory_t& factory) -> bool
235 consumer = factory(params);
239 CASPAR_LOG_CURRENT_EXCEPTION();
241 return consumer != frame_consumer::empty();
244 if(consumer == frame_consumer::empty())
245 CASPAR_THROW_EXCEPTION(file_not_found() << msg_info("No match found for supplied commands. Check syntax."));
247 return spl::make_shared<destroy_consumer_proxy>(
248 spl::make_shared<print_consumer_proxy>(
249 spl::make_shared<recover_consumer_proxy>(
250 spl::make_shared<cadence_guard>(
251 std::move(consumer)))));
254 const spl::shared_ptr<frame_consumer>& frame_consumer::empty()
256 class empty_frame_consumer : public frame_consumer
259 boost::unique_future<bool> send(const_frame) override {return wrap_as_future(false);}
260 void initialize(const video_format_desc&, int) override{}
261 std::wstring print() const override {return L"empty";}
262 std::wstring name() const override {return L"empty";}
263 bool has_synchronization_clock() const override {return false;}
264 int buffer_depth() const override {return 0;};
265 virtual int index() const{return -1;}
266 void subscribe(const monitor::observable::observer_ptr& o) override{}
267 void unsubscribe(const monitor::observable::observer_ptr& o) override{}
268 boost::property_tree::wptree info() const override
270 boost::property_tree::wptree info;
271 info.add(L"type", L"empty");
275 static spl::shared_ptr<frame_consumer> consumer = spl::make_shared<empty_frame_consumer>();