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
21 #include "oal_consumer.h"
\r
23 #include <common/diagnostics/graph.h>
\r
24 #include <common/log/log.h>
\r
25 #include <common/utility/timer.h>
\r
26 #include <common/utility/string.h>
\r
28 #include <core/consumer/frame_consumer.h>
\r
29 #include <core/video_format.h>
\r
31 #include <core/mixer/read_frame.h>
\r
33 #include <SFML/Audio.hpp>
\r
35 #include <boost/circular_buffer.hpp>
\r
36 #include <boost/timer.hpp>
\r
38 #include <tbb/concurrent_queue.h>
\r
42 struct oal_consumer : public core::frame_consumer, public sf::SoundStream
\r
44 safe_ptr<diagnostics::graph> graph_;
\r
45 boost::timer perf_timer_;
\r
47 tbb::concurrent_bounded_queue<std::vector<short>> input_;
\r
48 boost::circular_buffer<std::vector<short>> container_;
\r
49 tbb::atomic<bool> is_running_;
\r
51 core::video_format_desc format_desc_;
\r
55 : graph_(diagnostics::create_graph(narrow(print())))
\r
59 graph_->add_guide("tick-time", 0.5);
\r
60 graph_->set_color("tick-time", diagnostics::color(0.1f, 0.7f, 0.8f));
\r
62 input_.set_capacity(core::consumer_buffer_depth()-2);
\r
67 is_running_ = false;
\r
68 input_.try_push(std::vector<short>());
\r
69 input_.try_push(std::vector<short>());
\r
71 CASPAR_LOG(info) << print() << L" Shutting down.";
\r
74 virtual void initialize(const core::video_format_desc& format_desc)
\r
76 format_desc_ = format_desc;
\r
77 sf::SoundStream::Initialize(2, 48000);
\r
79 CASPAR_LOG(info) << print() << " Sucessfully initialized.";
\r
82 virtual void send(const safe_ptr<core::read_frame>& frame)
\r
84 if(preroll_count_ < input_.capacity())
\r
86 while(input_.try_push(std::vector<int16_t>(format_desc_.audio_samples_per_frame, 0)))
\r
90 input_.push(std::vector<int16_t>(frame->audio_data().begin(), frame->audio_data().end()));
\r
93 virtual size_t buffer_depth() const{return 3;}
\r
95 virtual bool OnGetData(sf::SoundStream::Chunk& data)
\r
97 std::vector<short> audio_data;
\r
98 input_.pop(audio_data);
\r
100 container_.push_back(std::move(audio_data));
\r
101 data.Samples = container_.back().data();
\r
102 data.NbSamples = container_.back().size();
\r
104 graph_->update_value("tick-time", perf_timer_.elapsed()*format_desc_.fps*0.5);
\r
105 perf_timer_.restart();
\r
107 return is_running_;
\r
110 virtual std::wstring print() const
\r
112 return L"oal[" + format_desc_.name + L"]";
\r
115 virtual const core::video_format_desc& get_video_format_desc() const
\r
117 return format_desc_;
\r
121 safe_ptr<core::frame_consumer> create_oal_consumer(const std::vector<std::wstring>& params)
\r
123 if(params.size() < 1 || params[0] != L"OAL")
\r
124 return core::frame_consumer::empty();
\r
126 return make_safe<oal_consumer>();
\r
129 safe_ptr<core::frame_consumer> create_oal_consumer()
\r
131 return make_safe<oal_consumer>();
\r