]> git.sesse.net Git - casparcg/blob - modules/oal/consumer/oal_consumer.cpp
* Improved precision of prec_timer on Linux.
[casparcg] / modules / oal / consumer / oal_consumer.cpp
1 /*
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
3 *
4 * This file is part of CasparCG (www.casparcg.com).
5 *
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.
10 *
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.
15 *
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/>.
18 *
19 * Author: Robert Nagy, ronag89@gmail.com
20 */
21
22 #include "oal_consumer.h"
23
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
32 #include <core/consumer/frame_consumer.h>
33 #include <core/frame/frame.h>
34 #include <core/mixer/audio/audio_util.h>
35 #include <core/mixer/audio/audio_mixer.h>
36 #include <core/video_format.h>
37 #include <core/help/help_sink.h>
38 #include <core/help/help_repository.h>
39
40 #include <boost/circular_buffer.hpp>
41 #include <boost/lexical_cast.hpp>
42 #include <boost/property_tree/ptree.hpp>
43 #include <boost/timer.hpp>
44 #include <boost/thread/once.hpp>
45 #include <boost/algorithm/string.hpp>
46
47 #include <tbb/concurrent_queue.h>
48
49 #include <AL/alc.h>
50 #include <AL/al.h>
51
52 namespace caspar { namespace oal {
53
54 typedef cache_aligned_vector<int16_t> audio_buffer_16;
55
56 class device
57 {
58         ALCdevice*              device_         = nullptr;
59         ALCcontext*             context_        = nullptr;
60
61 public:
62         device()
63         {
64                 device_ = alcOpenDevice(nullptr);
65
66                 if(!device_)
67                         CASPAR_THROW_EXCEPTION(invalid_operation() << msg_info("Failed to initialize audio device."));
68
69                 context_ = alcCreateContext(device_, nullptr);
70
71                 if(!context_)
72                         CASPAR_THROW_EXCEPTION(invalid_operation() << msg_info("Failed to create audio context."));
73                         
74                 if(alcMakeContextCurrent(context_) == ALC_FALSE)
75                         CASPAR_THROW_EXCEPTION(invalid_operation() << msg_info("Failed to activate audio context."));
76         }
77
78         ~device()
79         {
80                 alcMakeContextCurrent(nullptr);
81
82                 if(context_)
83                         alcDestroyContext(context_);
84
85                 if(device_)
86                         alcCloseDevice(device_);
87         }
88
89         ALCdevice* get()
90         {
91                 return device_;
92         }
93 };
94
95 void init_device()
96 {
97         static std::unique_ptr<device> instance;
98         static boost::once_flag f = BOOST_ONCE_INIT;
99         
100         boost::call_once(f, []{instance.reset(new device());});
101 }
102
103 struct oal_consumer : public core::frame_consumer
104 {
105         core::monitor::subject                          monitor_subject_;
106
107         spl::shared_ptr<diagnostics::graph>     graph_;
108         boost::timer                                            perf_timer_;
109         tbb::atomic<int64_t>                            presentation_age_;
110         int                                                                     channel_index_ = -1;
111         
112         core::video_format_desc                         format_desc_;
113
114         ALuint                                                          source_                         = 0;
115         std::vector<ALuint>                                     buffers_;
116
117         executor                                                        executor_                       { L"oal_consumer" };
118
119 public:
120         oal_consumer() 
121         {
122                 presentation_age_ = 0;
123
124                 init_device();
125
126                 graph_->set_color("tick-time", diagnostics::color(0.0f, 0.6f, 0.9f));   
127                 graph_->set_color("dropped-frame", diagnostics::color(0.3f, 0.6f, 0.3f));
128                 graph_->set_color("late-frame", diagnostics::color(0.6f, 0.3f, 0.3f));
129                 diagnostics::register_graph(graph_);
130         }
131
132         ~oal_consumer()
133         {
134                 executor_.invoke([=]
135                 {               
136                         if(source_)
137                         {
138                                 alSourceStop(source_);
139                                 alDeleteSources(1, &source_);
140                         }
141
142                         for (auto& buffer : buffers_)
143                         {
144                                 if(buffer)
145                                         alDeleteBuffers(1, &buffer);
146                         };
147                 });
148         }
149
150         // frame consumer
151
152         void initialize(const core::video_format_desc& format_desc, int channel_index) override
153         {
154                 format_desc_    = format_desc;          
155                 channel_index_  = channel_index;
156                 graph_->set_text(print());
157
158                 executor_.begin_invoke([=]
159                 {               
160                         buffers_.resize(format_desc_.fps > 30 ? 8 : 4);
161                         alGenBuffers(static_cast<ALsizei>(buffers_.size()), buffers_.data());
162                         alGenSources(1, &source_);
163
164                         for(std::size_t n = 0; n < buffers_.size(); ++n)
165                         {
166                                 audio_buffer_16 audio(format_desc_.audio_cadence[n % format_desc_.audio_cadence.size()]*format_desc_.audio_channels, 0);
167                                 alBufferData(buffers_[n], AL_FORMAT_STEREO16, audio.data(), static_cast<ALsizei>(audio.size()*sizeof(int16_t)), format_desc_.audio_sample_rate);
168                                 alSourceQueueBuffers(source_, 1, &buffers_[n]);
169                         }
170                         
171                         alSourcei(source_, AL_LOOPING, AL_FALSE);
172
173                         alSourcePlay(source_);  
174                 });
175         }
176
177         int64_t presentation_frame_age_millis() const override
178         {
179                 return presentation_age_;
180         }
181
182         std::future<bool> send(core::const_frame frame) override
183         {
184                 // Will only block if the default executor queue capacity of 512 is
185                 // exhausted, which should not happen
186                 executor_.begin_invoke([=]
187                 {
188                         ALenum state; 
189                         alGetSourcei(source_, AL_SOURCE_STATE,&state);
190                         if(state != AL_PLAYING)
191                         {
192                                 for(int n = 0; n < buffers_.size()-1; ++n)
193                                 {                                       
194                                         ALuint buffer = 0;  
195                                         alSourceUnqueueBuffers(source_, 1, &buffer);
196                                         if(buffer)
197                                         {
198                                                 std::vector<int16_t> audio(format_desc_.audio_cadence[n % format_desc_.audio_cadence.size()] * format_desc_.audio_channels, 0);
199                                                 alBufferData(buffer, AL_FORMAT_STEREO16, audio.data(), static_cast<ALsizei>(audio.size()*sizeof(int16_t)), format_desc_.audio_sample_rate);
200                                                 alSourceQueueBuffers(source_, 1, &buffer);
201                                         }
202                                 }
203                                 alSourcePlay(source_);          
204                                 graph_->set_tag("late-frame");  
205                         }
206
207                         auto audio = core::audio_32_to_16(frame.audio_data());
208                         
209                         ALuint buffer = 0;  
210                         alSourceUnqueueBuffers(source_, 1, &buffer);
211                         if(buffer)
212                         {
213                                 alBufferData(buffer, AL_FORMAT_STEREO16, audio.data(), static_cast<ALsizei>(audio.size()*sizeof(int16_t)), format_desc_.audio_sample_rate);
214                                 alSourceQueueBuffers(source_, 1, &buffer);
215                         }
216                         else
217                                 graph_->set_tag("dropped-frame");
218
219                         graph_->set_value("tick-time", perf_timer_.elapsed()*format_desc_.fps*0.5);             
220                         perf_timer_.restart();
221                         presentation_age_ = frame.get_age_millis() + delay_millis();
222                 });
223
224                 return make_ready_future(true);
225         }
226         
227         std::wstring print() const override
228         {
229                 return L"oal[" + boost::lexical_cast<std::wstring>(channel_index_) + L"|" + format_desc_.name + L"]";
230         }
231
232         std::wstring name() const override
233         {
234                 return L"system-audio";
235         }
236
237         boost::property_tree::wptree info() const override
238         {
239                 boost::property_tree::wptree info;
240                 info.add(L"type", L"system-audio");
241                 return info;
242         }
243         
244         bool has_synchronization_clock() const override
245         {
246                 return false;
247         }
248
249         int delay_millis() const
250         {
251                 return 213;
252         }
253         
254         int buffer_depth() const override
255         {
256                 int delay_in_frames = static_cast<int>(delay_millis() / (1000.0 / format_desc_.fps));
257                 
258                 return delay_in_frames;
259         }
260                 
261         int index() const override
262         {
263                 return 500;
264         }
265
266         core::monitor::subject& monitor_output()
267         {
268                 return monitor_subject_;
269         }
270 };
271
272 void describe_consumer(core::help_sink& sink, const core::help_repository& repo)
273 {
274         sink.short_description(L"A system audio consumer.");
275         sink.syntax(L"AUDIO");
276         sink.para()->text(L"Uses the system's default audio playback device.");
277         sink.para()->text(L"Examples:");
278         sink.example(L">> ADD 1 AUDIO");
279 }
280
281 spl::shared_ptr<core::frame_consumer> create_consumer(const std::vector<std::wstring>& params, core::interaction_sink*)
282 {
283         if(params.size() < 1 || !boost::iequals(params.at(0), L"AUDIO"))
284                 return core::frame_consumer::empty();
285
286         return spl::make_shared<oal_consumer>();
287 }
288
289 spl::shared_ptr<core::frame_consumer> create_preconfigured_consumer(const boost::property_tree::wptree&, core::interaction_sink*)
290 {
291         return spl::make_shared<oal_consumer>();
292 }
293
294 }}