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 "oal_consumer.h"
\r
24 #include <common/except.h>
\r
25 #include <common/executor.h>
\r
26 #include <common/diagnostics/graph.h>
\r
27 #include <common/log.h>
\r
28 #include <common/utf.h>
\r
29 #include <common/env.h>
\r
31 #include <core/consumer/frame_consumer.h>
\r
32 #include <core/frame/frame.h>
\r
33 #include <core/mixer/audio/audio_util.h>
\r
34 #include <core/mixer/audio/audio_mixer.h>
\r
35 #include <core/video_format.h>
\r
37 #include <boost/circular_buffer.hpp>
\r
38 #include <boost/lexical_cast.hpp>
\r
39 #include <boost/property_tree/ptree.hpp>
\r
40 #include <boost/timer.hpp>
\r
41 #include <boost/foreach.hpp>
\r
43 #include <tbb/concurrent_queue.h>
\r
50 namespace caspar { namespace oal {
\r
52 typedef std::vector<int16_t, tbb::cache_aligned_allocator<int16_t>> audio_buffer_16;
\r
54 struct oal_consumer : public core::frame_consumer
\r
56 spl::shared_ptr<diagnostics::graph> graph_;
\r
57 boost::timer perf_timer_;
\r
60 core::video_format_desc format_desc_;
\r
63 ALCcontext* context_;
\r
65 std::array<ALuint, 3> buffers_;
\r
71 : channel_index_(-1)
\r
75 , executor_(L"oal_consumer")
\r
79 graph_->set_color("tick-time", diagnostics::color(0.0f, 0.6f, 0.9f));
\r
80 graph_->set_color("dropped-frame", diagnostics::color(0.3f, 0.6f, 0.3f));
\r
81 diagnostics::register_graph(graph_);
\r
83 executor_.invoke([=]
\r
85 device_ = alcOpenDevice(nullptr);
\r
88 BOOST_THROW_EXCEPTION(invalid_operation());
\r
90 context_ = alcCreateContext(device_, nullptr);
\r
93 BOOST_THROW_EXCEPTION(invalid_operation());
\r
95 if(alcMakeContextCurrent(context_) == ALC_FALSE)
\r
96 BOOST_THROW_EXCEPTION(invalid_operation());
\r
102 executor_.begin_invoke([=]
\r
106 alSourceStop(source_);
\r
107 alDeleteSources(1, &source_);
\r
110 BOOST_FOREACH(auto& buffer, buffers_)
\r
113 alDeleteBuffers(1, &buffer);
\r
116 alcMakeContextCurrent(nullptr);
\r
119 alcDestroyContext(context_);
\r
122 alcCloseDevice(device_);
\r
128 void initialize(const core::video_format_desc& format_desc, int channel_index) override
\r
130 format_desc_ = format_desc;
\r
131 channel_index_ = channel_index;
\r
132 graph_->set_text(print());
\r
134 executor_.begin_invoke([=]
\r
136 alGenBuffers(static_cast<ALsizei>(buffers_.size()), buffers_.data());
\r
137 alGenSources(1, &source_);
\r
139 for(std::size_t n = 0; n < buffers_.size(); ++n)
\r
141 std::vector<int16_t> audio(format_desc_.audio_cadence[n % format_desc_.audio_cadence.size()], 0);
\r
142 alBufferData(buffers_[n], AL_FORMAT_STEREO16, audio.data(), static_cast<ALsizei>(audio.size()*sizeof(int16_t)), format_desc_.audio_sample_rate);
\r
143 alSourceQueueBuffers(source_, 1, &buffers_[n]);
\r
146 alSourcei(source_, AL_LOOPING, AL_FALSE);
\r
150 bool send(core::const_frame frame) override
\r
152 executor_.begin_invoke([=]
\r
155 alGetSourcei(source_, AL_SOURCE_STATE,&state);
\r
156 if(state != AL_PLAYING)
\r
157 alSourcePlay(source_);
\r
159 auto audio = core::audio_32_to_16(frame.audio_data());
\r
161 ALuint buffer = 0;
\r
162 alSourceUnqueueBuffers(source_, 1, &buffer);
\r
165 alBufferData(buffer, AL_FORMAT_STEREO16, audio.data(), static_cast<ALsizei>(audio.size()*sizeof(int16_t)), format_desc_.audio_sample_rate);
\r
166 alSourceQueueBuffers(source_, 1, &buffer);
\r
169 graph_->set_tag("dropped-frame");
\r
171 graph_->set_value("tick-time", perf_timer_.elapsed()*format_desc_.fps*0.5);
\r
172 perf_timer_.restart();
\r
178 std::wstring print() const override
\r
180 return L"oal[" + boost::lexical_cast<std::wstring>(channel_index_) + L"|" + format_desc_.name + L"]";
\r
183 std::wstring name() const override
\r
185 return L"system-audio";
\r
188 boost::property_tree::wptree info() const override
\r
190 boost::property_tree::wptree info;
\r
191 info.add(L"type", L"system-audio");
\r
195 bool has_synchronization_clock() const override
\r
200 int buffer_depth() const override
\r
205 int index() const override
\r
211 spl::shared_ptr<core::frame_consumer> create_consumer(const std::vector<std::wstring>& params)
\r
213 if(params.size() < 1 || params[0] != L"AUDIO")
\r
214 return core::frame_consumer::empty();
\r
216 return spl::make_shared<oal_consumer>();
\r
219 spl::shared_ptr<core::frame_consumer> create_consumer()
\r
221 return spl::make_shared<oal_consumer>();
\r