2 * Copyright 2013 Sveriges Television AB http://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 "oal_consumer.h"
26 #include <common/exception/exceptions.h>
27 #include <common/diagnostics/graph.h>
28 #include <common/log/log.h>
29 #include <common/utility/timer.h>
30 #include <common/utility/string.h>
31 #include <common/concurrency/future_util.h>
32 #include <common/exception/win32_exception.h>
34 #include <core/parameters/parameters.h>
35 #include <core/consumer/frame_consumer.h>
36 #include <core/mixer/audio/audio_util.h>
37 #include <core/video_format.h>
39 #include <core/mixer/read_frame.h>
41 #include <SFML/Audio.hpp>
43 #include <boost/property_tree/ptree.hpp>
44 #include <boost/timer.hpp>
45 #include <boost/thread/future.hpp>
46 #include <boost/optional.hpp>
48 #include <tbb/concurrent_queue.h>
50 namespace caspar { namespace oal {
52 typedef std::vector<int16_t, tbb::cache_aligned_allocator<int16_t>> audio_buffer_16;
54 struct oal_consumer : public core::frame_consumer, public sf::SoundStream
56 safe_ptr<diagnostics::graph> graph_;
57 boost::timer tick_timer_;
60 tbb::concurrent_bounded_queue<std::shared_ptr<std::vector<audio_buffer_16>>> input_;
61 std::shared_ptr<std::vector<audio_buffer_16>> chunk_builder_;
62 audio_buffer_16 container_;
63 tbb::atomic<bool> is_running_;
65 core::video_format_desc format_desc_;
66 core::channel_layout channel_layout_;
67 size_t frames_to_buffer_;
72 core::default_channel_layout_repository().get_by_name(
75 graph_->set_color("tick-time", diagnostics::color(0.0f, 0.6f, 0.9f));
76 graph_->set_color("dropped-frame", diagnostics::color(0.3f, 0.6f, 0.3f));
77 graph_->set_color("late-frame", diagnostics::color(0.6f, 0.3f, 0.3f));
78 diagnostics::register_graph(graph_);
81 input_.set_capacity(1);
87 input_.try_push(std::make_shared<std::vector<audio_buffer_16>>());
88 input_.try_push(std::make_shared<std::vector<audio_buffer_16>>());
90 input_.try_push(std::make_shared<std::vector<audio_buffer_16>>());
91 input_.try_push(std::make_shared<std::vector<audio_buffer_16>>());
93 CASPAR_LOG(info) << print() << L" Successfully Uninitialized.";
98 virtual void initialize(const core::video_format_desc& format_desc, int channel_index) override
100 format_desc_ = format_desc;
101 channel_index_ = channel_index;
102 graph_->set_text(print());
104 if(Status() != Playing)
106 sf::SoundStream::Initialize(2, format_desc.audio_sample_rate);
108 // Each time OnGetData is called it seems to no longer be enough
109 // with the samples of one frame (since the change to statically
110 // linked SFML 1.6), so the samples of a few frames is needed to be
112 static const double SAMPLES_NEEDED_BY_SFML_MULTIPLE = 0.1;
113 int min_num_samples_in_chunk = static_cast<int>(format_desc.audio_sample_rate * SAMPLES_NEEDED_BY_SFML_MULTIPLE);
114 int min_num_samples_in_frame = *std::min_element(format_desc.audio_cadence.begin(), format_desc.audio_cadence.end());
115 int min_frames_to_buffer = min_num_samples_in_chunk / min_num_samples_in_frame + (min_num_samples_in_chunk % min_num_samples_in_frame ? 1 : 0);
116 frames_to_buffer_ = min_frames_to_buffer;
120 CASPAR_LOG(info) << print() << " Sucessfully Initialized.";
123 virtual int64_t presentation_frame_age_millis() const override
128 virtual boost::unique_future<bool> send(const safe_ptr<core::read_frame>& frame) override
130 audio_buffer_16 buffer;
132 if (core::needs_rearranging(
133 frame->multichannel_view(),
135 channel_layout_.num_channels))
137 core::audio_buffer downmixed;
139 frame->multichannel_view().num_samples()
140 * channel_layout_.num_channels,
143 auto dest_view = core::make_multichannel_view<int32_t>(
144 downmixed.begin(), downmixed.end(), channel_layout_);
146 core::rearrange_or_rearrange_and_mix(
147 frame->multichannel_view(),
149 core::default_mix_config_repository());
151 buffer = core::audio_32_to_16(downmixed);
155 buffer = core::audio_32_to_16(frame->audio_data());
160 chunk_builder_.reset(new std::vector<audio_buffer_16>);
161 chunk_builder_->push_back(std::move(buffer));
165 chunk_builder_->push_back(std::move(buffer));
168 if (chunk_builder_->size() == frames_to_buffer_)
170 if (!input_.try_push(chunk_builder_))
172 graph_->set_tag("dropped-frame");
173 chunk_builder_->pop_back();
177 chunk_builder_.reset();
181 return wrap_as_future(is_running_.load());
184 virtual std::wstring print() const override
186 return L"oal[" + boost::lexical_cast<std::wstring>(channel_index_) + L"|" + format_desc_.name + L"]";
189 virtual bool has_synchronization_clock() const override
194 virtual boost::property_tree::wptree info() const override
196 boost::property_tree::wptree info;
197 info.add(L"type", L"oal-consumer");
201 virtual size_t buffer_depth() const override
208 virtual bool OnGetData(sf::SoundStream::Chunk& data) override
210 win32_exception::ensure_handler_installed_for_thread(
211 "sfml-audio-thread");
212 std::shared_ptr<std::vector<audio_buffer_16>> audio_data;
214 if (!input_.try_pop(audio_data))
216 graph_->set_tag("late-frame");
217 input_.pop(audio_data); // Block until available
222 // Concatenate to one large buffer.
223 BOOST_FOREACH(auto& buffer, *audio_data)
225 std::copy(buffer.begin(), buffer.end(), std::back_inserter(container_));
228 data.Samples = container_.data();
229 data.NbSamples = container_.size();
231 graph_->set_value("tick-time", tick_timer_.elapsed()*format_desc_.fps*0.5 / frames_to_buffer_);
232 tick_timer_.restart();
237 virtual int index() const override
243 safe_ptr<core::frame_consumer> create_consumer(const core::parameters& params)
245 if(params.size() < 1 || params[0] != L"AUDIO")
246 return core::frame_consumer::empty();
248 return make_safe<oal_consumer>();
251 safe_ptr<core::frame_consumer> create_consumer()
253 return make_safe<oal_consumer>();