2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
\r
4 * This file is part of CasparCG (www.casparcg.com).
\r
6 * CasparCG is free software: you can redistribute it and/or modify
\r
7 * it under the terms of the GNU General Public License as published by
\r
8 * the Free Software Foundation, either version 3 of the License, or
\r
9 * (at your option) any later version.
\r
11 * CasparCG is distributed in the hope that it will be useful,
\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 * GNU General Public License for more details.
\r
16 * You should have received a copy of the GNU General Public License
\r
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
\r
19 * Author: Robert Nagy, ronag89@gmail.com
\r
22 #include "../StdAfx.h"
\r
24 #include "frame_consumer.h"
\r
26 #include <common/env.h>
\r
27 #include <common/memory/safe_ptr.h>
\r
28 #include <common/exception/exceptions.h>
\r
29 #include <common/concurrency/future_util.h>
\r
30 #include <core/video_format.h>
\r
31 #include <core/mixer/read_frame.h>
\r
33 #include <boost/circular_buffer.hpp>
\r
35 namespace caspar { namespace core {
\r
37 std::vector<const consumer_factory_t> g_factories;
\r
39 void register_consumer_factory(const consumer_factory_t& factory)
\r
41 g_factories.push_back(factory);
\r
44 safe_ptr<core::frame_consumer> create_consumer(const std::vector<std::wstring>& params)
\r
47 BOOST_THROW_EXCEPTION(invalid_argument() << arg_name_info("params") << arg_value_info(""));
\r
49 auto consumer = frame_consumer::empty();
\r
50 std::any_of(g_factories.begin(), g_factories.end(), [&](const consumer_factory_t& factory) -> bool
\r
54 consumer = factory(params);
\r
58 CASPAR_LOG_CURRENT_EXCEPTION();
\r
60 return consumer != frame_consumer::empty();
\r
63 if(consumer == frame_consumer::empty())
\r
64 BOOST_THROW_EXCEPTION(file_not_found() << msg_info("No match found for supplied commands. Check syntax."));
\r
69 // This class is used to guarantee that audio cadence is correct. This is important for NTSC audio.
\r
70 class cadence_guard : public frame_consumer
\r
72 safe_ptr<frame_consumer> consumer_;
\r
73 std::vector<size_t> audio_cadence_;
\r
74 boost::circular_buffer<size_t> sync_buffer_;
\r
76 cadence_guard(const safe_ptr<frame_consumer>& consumer)
\r
77 : consumer_(consumer)
\r
81 virtual void initialize(const video_format_desc& format_desc, int channel_index) override
\r
83 audio_cadence_ = format_desc.audio_cadence;
\r
84 sync_buffer_ = boost::circular_buffer<size_t>(format_desc.audio_cadence.size());
\r
85 consumer_->initialize(format_desc, channel_index);
\r
88 virtual boost::unique_future<bool> send(const safe_ptr<read_frame>& frame) override
\r
90 if(audio_cadence_.size() == 1)
\r
91 return consumer_->send(frame);
\r
93 boost::unique_future<bool> result = caspar::wrap_as_future(true);
\r
95 if(boost::range::equal(sync_buffer_, audio_cadence_) && audio_cadence_.front() == static_cast<size_t>(frame->audio_data().size()))
\r
97 // Audio sent so far is in sync, now we can send the next chunk.
\r
98 result = consumer_->send(frame);
\r
99 boost::range::rotate(audio_cadence_, std::begin(audio_cadence_)+1);
\r
102 CASPAR_LOG(trace) << print() << L" Syncing audio.";
\r
104 sync_buffer_.push_back(static_cast<size_t>(frame->audio_data().size()));
\r
106 return std::move(result);
\r
109 virtual std::wstring print() const override
\r
111 return consumer_->print();
\r
114 virtual boost::property_tree::wptree info() const override
\r
116 return consumer_->info();
\r
119 virtual bool has_synchronization_clock() const override
\r
121 return consumer_->has_synchronization_clock();
\r
124 virtual size_t buffer_depth() const override
\r
126 return consumer_->buffer_depth();
\r
129 virtual int index() const override
\r
131 return consumer_->index();
\r
135 safe_ptr<frame_consumer> create_consumer_cadence_guard(const safe_ptr<frame_consumer>& consumer)
\r
137 return make_safe<cadence_guard>(std::move(consumer));
\r
140 const safe_ptr<frame_consumer>& frame_consumer::empty()
\r
142 struct empty_frame_consumer : public frame_consumer
\r
144 virtual boost::unique_future<bool> send(const safe_ptr<read_frame>&) override { return caspar::wrap_as_future(false); }
\r
145 virtual void initialize(const video_format_desc&, int) override{}
\r
146 virtual std::wstring print() const override {return L"empty";}
\r
147 virtual bool has_synchronization_clock() const override {return false;}
\r
148 virtual size_t buffer_depth() const override {return 0;};
\r
149 virtual int index() const{return -1;}
\r
150 virtual boost::property_tree::wptree info() const override
\r
152 boost::property_tree::wptree info;
\r
153 info.add(L"type", L"empty-consumer");
\r
157 static safe_ptr<frame_consumer> consumer = make_safe<empty_frame_consumer>();
\r