]> git.sesse.net Git - casparcg/blob - modules/oal/consumer/oal_consumer.cpp
2.0.0.2: decklink_consumer: Fixed prerolling bug which caused audio to be out of...
[casparcg] / modules / oal / consumer / oal_consumer.cpp
1 /*\r
2 * copyright (c) 2010 Sveriges Television AB <info@casparcg.com>\r
3 *\r
4 *  This file is part of CasparCG.\r
5 *\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
10 *\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
15 \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
18 *\r
19 */\r
20  \r
21 #include "oal_consumer.h"\r
22 \r
23 #include <common/diagnostics/graph.h>\r
24 #include <common/log/log.h>\r
25 #include <common/utility/timer.h>\r
26 \r
27 #include <core/video_format.h>\r
28 \r
29 #include <core/mixer/read_frame.h>\r
30 \r
31 #include <SFML/Audio.hpp>\r
32 \r
33 #include <boost/circular_buffer.hpp>\r
34 #include <boost/timer.hpp>\r
35 \r
36 #include <tbb/concurrent_queue.h>\r
37 \r
38 namespace caspar {\r
39 \r
40 struct oal_consumer::implementation : public sf::SoundStream, boost::noncopyable\r
41 {\r
42         safe_ptr<diagnostics::graph> graph_;\r
43         boost::timer perf_timer_;\r
44 \r
45         tbb::concurrent_bounded_queue<std::vector<short>> input_;\r
46         boost::circular_buffer<std::vector<short>> container_;\r
47         tbb::atomic<bool> is_running_;\r
48 \r
49         core::video_format_desc format_desc_;\r
50 public:\r
51         implementation(const core::video_format_desc& format_desc) \r
52                 : graph_(diagnostics::create_graph(narrow(print())))\r
53                 , container_(5)\r
54                 , format_desc_(format_desc)\r
55         {\r
56                 graph_->add_guide("tick-time", 0.5);\r
57                 graph_->set_color("tick-time", diagnostics::color(0.1f, 0.7f, 0.8f));\r
58                 is_running_ = true;\r
59                 input_.set_capacity(4);\r
60 \r
61                 // Fill input buffer with silence.\r
62                 for(size_t n = 0; n < buffer_depth(); ++n)\r
63                         input_.push(std::vector<short>(static_cast<size_t>(48000.0f/format_desc_.fps)*2, 0)); \r
64 \r
65                 sf::SoundStream::Initialize(2, 48000);\r
66                 Play();         \r
67                 CASPAR_LOG(info) << print() << " Sucessfully initialized.";\r
68         }\r
69 \r
70         ~implementation()\r
71         {\r
72                 is_running_ = false;\r
73                 input_.try_push(std::vector<short>());\r
74                 input_.try_push(std::vector<short>());\r
75                 Stop();\r
76                 CASPAR_LOG(info) << print() << L" Shutting down.";      \r
77         }\r
78         \r
79         void send(const safe_ptr<const core::read_frame>& frame)\r
80         {                               \r
81                 if(!frame->audio_data().empty())\r
82                         input_.push(std::vector<short>(frame->audio_data().begin(), frame->audio_data().end()));        \r
83                 else\r
84                         input_.push(std::vector<short>(3840, 0)); //static_cast<size_t>(48000.0f/format_desc_.fps)*2\r
85         }\r
86 \r
87         size_t buffer_depth() const{return 3;}\r
88 \r
89         virtual bool OnGetData(sf::SoundStream::Chunk& data)\r
90         {               \r
91                 std::vector<short> audio_data;          \r
92                 input_.pop(audio_data);\r
93                                 \r
94                 container_.push_back(std::move(audio_data));\r
95                 data.Samples = container_.back().data();\r
96                 data.NbSamples = container_.back().size();      \r
97                 \r
98                 graph_->update_value("tick-time", perf_timer_.elapsed()*format_desc_.fps*0.5);          \r
99                 perf_timer_.restart();\r
100 \r
101                 return is_running_;\r
102         }\r
103 \r
104         std::wstring print() const\r
105         {\r
106                 return L"oal[" + format_desc_.name + L"]";\r
107         }\r
108 };\r
109 \r
110 oal_consumer::oal_consumer(){}\r
111 oal_consumer::oal_consumer(oal_consumer&& other) : impl_(std::move(other.impl_)){}\r
112 void oal_consumer::send(const safe_ptr<const core::read_frame>& frame){impl_->send(frame);}\r
113 size_t oal_consumer::buffer_depth() const{return impl_->buffer_depth();}\r
114 void oal_consumer::initialize(const core::video_format_desc& format_desc){impl_.reset(new implementation(format_desc));}\r
115 std::wstring oal_consumer::print() const { return impl_->print(); }\r
116 const core::video_format_desc& oal_consumer::get_video_format_desc() const{return impl_->format_desc_;}\r
117 \r
118 safe_ptr<core::frame_consumer> create_oal_consumer(const std::vector<std::wstring>& params)\r
119 {\r
120         if(params.size() < 1 || params[0] != L"OAL")\r
121                 return core::frame_consumer::empty();\r
122 \r
123         return make_safe<oal_consumer>();\r
124 }\r
125 }\r