2 * copyright (c) 2010 Sveriges Television AB <info@casparcg.com>
\r
4 * This file is part of CasparCG.
\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
20 #include "../StdAfx.h"
\r
22 #include "frame_consumer.h"
\r
24 #include <common/env.h>
\r
25 #include <common/memory/safe_ptr.h>
\r
26 #include <common/exception/exceptions.h>
\r
27 #include <core/video_format.h>
\r
28 #include <core/mixer/read_frame.h>
\r
30 #include <boost/circular_buffer.hpp>
\r
32 namespace caspar { namespace core {
\r
34 std::vector<const consumer_factory_t> g_factories;
\r
36 void register_consumer_factory(const consumer_factory_t& factory)
\r
38 g_factories.push_back(factory);
\r
41 safe_ptr<core::frame_consumer> create_consumer(const std::vector<std::wstring>& params)
\r
44 BOOST_THROW_EXCEPTION(invalid_argument() << arg_name_info("params") << arg_value_info(""));
\r
46 auto consumer = frame_consumer::empty();
\r
47 std::any_of(g_factories.begin(), g_factories.end(), [&](const consumer_factory_t& factory) -> bool
\r
51 consumer = factory(params);
\r
55 CASPAR_LOG_CURRENT_EXCEPTION();
\r
57 return consumer != frame_consumer::empty();
\r
60 if(consumer == frame_consumer::empty())
\r
61 BOOST_THROW_EXCEPTION(file_not_found() << msg_info("No match found for supplied commands. Check syntax."));
\r
66 // This class is used to guarantee that audio cadence is correct. This is important for NTSC audio.
\r
67 class cadence_guard : public frame_consumer
\r
69 safe_ptr<frame_consumer> consumer_;
\r
70 std::vector<size_t> audio_cadence_;
\r
71 boost::circular_buffer<size_t> sync_buffer_;
\r
74 cadence_guard(safe_ptr<frame_consumer>&& consumer)
\r
75 : consumer_(std::move(consumer))
\r
79 virtual void initialize(const video_format_desc& format_desc, int channel_index, int sub_index) override
\r
81 audio_cadence_ = format_desc.audio_cadence;
\r
82 sync_buffer_ = boost::circular_buffer<size_t>(format_desc.audio_cadence.size());
\r
83 consumer_->initialize(format_desc, channel_index, sub_index);
\r
86 virtual bool send(const safe_ptr<read_frame>& frame) override
\r
88 sync_buffer_.push_back(static_cast<size_t>(frame->audio_data().size()));
\r
90 if(!boost::range::equal(sync_buffer_, audio_cadence_))
\r
93 CASPAR_LOG(trace) << L"[cadence_guard] Audio cadence unsynced. Skipping frame.";
\r
99 boost::range::rotate(audio_cadence_, std::begin(audio_cadence_)+1);
\r
103 boost::range::rotate(audio_cadence_, std::begin(audio_cadence_)+1);
\r
105 return consumer_->send(frame);
\r
108 virtual std::wstring print() const override
\r
110 return consumer_->print();
\r
113 virtual bool has_synchronization_clock() const override
\r
115 return consumer_->has_synchronization_clock();
\r
118 virtual size_t buffer_depth() const override
\r
120 return consumer_->buffer_depth();
\r
124 safe_ptr<frame_consumer> create_consumer_cadence_guard(safe_ptr<frame_consumer>&& consumer)
\r
126 return make_safe<cadence_guard>(std::move(consumer));
\r