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 "oal_consumer.h"
24 #include <common/except.h>
25 #include <common/executor.h>
26 #include <common/diagnostics/graph.h>
27 #include <common/log.h>
28 #include <common/utf.h>
29 #include <common/env.h>
30 #include <common/future.h>
31 #include <common/param.h>
33 #include <core/consumer/frame_consumer.h>
34 #include <core/frame/frame.h>
35 #include <core/frame/audio_channel_layout.h>
36 #include <core/mixer/audio/audio_util.h>
37 #include <core/mixer/audio/audio_mixer.h>
38 #include <core/video_format.h>
39 #include <core/help/help_sink.h>
40 #include <core/help/help_repository.h>
42 #include <boost/circular_buffer.hpp>
43 #include <boost/lexical_cast.hpp>
44 #include <boost/property_tree/ptree.hpp>
45 #include <boost/timer.hpp>
46 #include <boost/thread/once.hpp>
47 #include <boost/algorithm/string.hpp>
49 #include <tbb/concurrent_queue.h>
54 namespace caspar { namespace oal {
56 typedef cache_aligned_vector<int16_t> audio_buffer_16;
60 ALCdevice* device_ = nullptr;
61 ALCcontext* context_ = nullptr;
66 device_ = alcOpenDevice(nullptr);
69 CASPAR_THROW_EXCEPTION(invalid_operation() << msg_info("Failed to initialize audio device."));
71 context_ = alcCreateContext(device_, nullptr);
74 CASPAR_THROW_EXCEPTION(invalid_operation() << msg_info("Failed to create audio context."));
76 if(alcMakeContextCurrent(context_) == ALC_FALSE)
77 CASPAR_THROW_EXCEPTION(invalid_operation() << msg_info("Failed to activate audio context."));
82 alcMakeContextCurrent(nullptr);
85 alcDestroyContext(context_);
88 alcCloseDevice(device_);
99 static std::unique_ptr<device> instance;
100 static boost::once_flag f = BOOST_ONCE_INIT;
102 boost::call_once(f, []{instance.reset(new device());});
105 struct oal_consumer : public core::frame_consumer
107 core::monitor::subject monitor_subject_;
109 spl::shared_ptr<diagnostics::graph> graph_;
110 boost::timer perf_timer_;
111 tbb::atomic<int64_t> presentation_age_;
112 int channel_index_ = -1;
114 core::video_format_desc format_desc_;
115 core::audio_channel_layout out_channel_layout_;
116 std::unique_ptr<core::audio_channel_remapper> channel_remapper_;
119 std::vector<ALuint> buffers_;
122 executor executor_ { L"oal_consumer" };
125 oal_consumer(const core::audio_channel_layout& out_channel_layout, int latency_millis)
126 : out_channel_layout_(out_channel_layout)
127 , latency_millis_(latency_millis)
129 presentation_age_ = 0;
133 graph_->set_color("tick-time", diagnostics::color(0.0f, 0.6f, 0.9f));
134 graph_->set_color("dropped-frame", diagnostics::color(0.3f, 0.6f, 0.3f));
135 graph_->set_color("late-frame", diagnostics::color(0.6f, 0.3f, 0.3f));
136 diagnostics::register_graph(graph_);
145 alSourceStop(source_);
146 alDeleteSources(1, &source_);
149 for (auto& buffer : buffers_)
152 alDeleteBuffers(1, &buffer);
159 void initialize(const core::video_format_desc& format_desc, const core::audio_channel_layout& channel_layout, int channel_index) override
161 format_desc_ = format_desc;
162 channel_index_ = channel_index;
163 if (out_channel_layout_ == core::audio_channel_layout::invalid())
164 out_channel_layout_ = channel_layout.num_channels == 2 ? channel_layout : *core::audio_channel_layout_repository::get_default()->get_layout(L"stereo");
166 out_channel_layout_.num_channels = 2;
168 channel_remapper_.reset(new core::audio_channel_remapper(channel_layout, out_channel_layout_));
169 graph_->set_text(print());
171 executor_.begin_invoke([=]
173 buffers_.resize(format_desc_.fps > 30 ? 8 : 4);
174 alGenBuffers(static_cast<ALsizei>(buffers_.size()), buffers_.data());
175 alGenSources(1, &source_);
177 for(std::size_t n = 0; n < buffers_.size(); ++n)
179 audio_buffer_16 audio(format_desc_.audio_cadence[n % format_desc_.audio_cadence.size()]*2, 0);
180 alBufferData(buffers_[n], AL_FORMAT_STEREO16, audio.data(), static_cast<ALsizei>(audio.size()*sizeof(int16_t)), format_desc_.audio_sample_rate);
181 alSourceQueueBuffers(source_, 1, &buffers_[n]);
184 alSourcei(source_, AL_LOOPING, AL_FALSE);
186 alSourcePlay(source_);
190 int64_t presentation_frame_age_millis() const override
192 return presentation_age_;
195 std::future<bool> send(core::const_frame frame) override
197 // Will only block if the default executor queue capacity of 512 is
198 // exhausted, which should not happen
199 executor_.begin_invoke([=]
202 alGetSourcei(source_, AL_SOURCE_STATE,&state);
203 if(state != AL_PLAYING)
205 for(int n = 0; n < buffers_.size()-1; ++n)
208 alSourceUnqueueBuffers(source_, 1, &buffer);
211 std::vector<int16_t> audio(format_desc_.audio_cadence[n % format_desc_.audio_cadence.size()] * 2, 0);
212 alBufferData(buffer, AL_FORMAT_STEREO16, audio.data(), static_cast<ALsizei>(audio.size()*sizeof(int16_t)), format_desc_.audio_sample_rate);
213 alSourceQueueBuffers(source_, 1, &buffer);
216 alSourcePlay(source_);
217 graph_->set_tag(diagnostics::tag_severity::WARNING, "late-frame");
220 auto audio = core::audio_32_to_16(channel_remapper_->mix_and_rearrange(frame.audio_data()));
223 alSourceUnqueueBuffers(source_, 1, &buffer);
226 alBufferData(buffer, AL_FORMAT_STEREO16, audio.data(), static_cast<ALsizei>(audio.size()*sizeof(int16_t)), format_desc_.audio_sample_rate);
227 alSourceQueueBuffers(source_, 1, &buffer);
230 graph_->set_tag(diagnostics::tag_severity::WARNING, "dropped-frame");
232 graph_->set_value("tick-time", perf_timer_.elapsed()*format_desc_.fps*0.5);
233 perf_timer_.restart();
234 presentation_age_ = frame.get_age_millis() + latency_millis();
237 return make_ready_future(true);
240 std::wstring print() const override
242 return L"oal[" + boost::lexical_cast<std::wstring>(channel_index_) + L"|" + format_desc_.name + L"]";
245 std::wstring name() const override
247 return L"system-audio";
250 boost::property_tree::wptree info() const override
252 boost::property_tree::wptree info;
253 info.add(L"type", L"system-audio");
257 bool has_synchronization_clock() const override
262 int latency_millis() const
264 return latency_millis_;
267 int buffer_depth() const override
269 int delay_in_frames = static_cast<int>(latency_millis() / (1000.0 / format_desc_.fps));
271 return delay_in_frames;
274 int index() const override
279 core::monitor::subject& monitor_output()
281 return monitor_subject_;
285 void describe_consumer(core::help_sink& sink, const core::help_repository& repo)
287 sink.short_description(L"A system audio consumer.");
288 sink.syntax(L"AUDIO {CHANNEL_LAYOUT [channel_layout:string]} {LATENCY [latency_millis:int|200]}");
289 sink.para()->text(L"Uses the system's default audio playback device.");
290 sink.para()->text(L"Examples:");
291 sink.example(L">> ADD 1 AUDIO");
292 sink.example(L">> ADD 1 AUDIO CHANNEL_LAYOUT matrix", L"Uses the matrix channel layout");
293 sink.example(L">> ADD 1 AUDIO LATENCY 500", L"Specifies that the system-audio chain: openal => driver => sound card => speaker output is 500ms");
296 spl::shared_ptr<core::frame_consumer> create_consumer(const std::vector<std::wstring>& params, core::interaction_sink*)
298 if(params.size() < 1 || !boost::iequals(params.at(0), L"AUDIO"))
299 return core::frame_consumer::empty();
301 auto channel_layout = core::audio_channel_layout::invalid();
302 auto channel_layout_spec = get_param(L"CHANNEL_LAYOUT", params);
304 if (!channel_layout_spec.empty())
306 auto found_layout = core::audio_channel_layout_repository::get_default()->get_layout(channel_layout_spec);
309 CASPAR_THROW_EXCEPTION(file_not_found() << msg_info(L"Channel layout " + channel_layout_spec + L" not found."));
311 channel_layout = *found_layout;
314 auto latency_millis = get_param(L"LATENCY", params, 200);
316 return spl::make_shared<oal_consumer>(channel_layout, latency_millis);
319 spl::shared_ptr<core::frame_consumer> create_preconfigured_consumer(const boost::property_tree::wptree& ptree, core::interaction_sink*)
321 auto channel_layout = core::audio_channel_layout::invalid();
322 auto channel_layout_spec = ptree.get_optional<std::wstring>(L"channel-layout");
324 if (channel_layout_spec)
326 auto found_layout = core::audio_channel_layout_repository::get_default()->get_layout(*channel_layout_spec);
329 CASPAR_THROW_EXCEPTION(file_not_found() << msg_info(L"Channel layout " + *channel_layout_spec + L" not found."));
331 channel_layout = *found_layout;
334 auto latency_millis = ptree.get(L"latency", 200);
336 return spl::make_shared<oal_consumer>(channel_layout, latency_millis);