]> git.sesse.net Git - casparcg/blob - modules/oal/consumer/oal_consumer.cpp
2.1.0: Send ffmpeg_consumer frame-number through OSC.
[casparcg] / modules / oal / consumer / oal_consumer.cpp
1 /*\r
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>\r
3 *\r
4 * This file is part of CasparCG (www.casparcg.com).\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 * Author: Robert Nagy, ronag89@gmail.com\r
20 */\r
21 \r
22 #include "oal_consumer.h"\r
23 \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
30 \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
36 \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
42 #include <boost/thread/once.hpp>\r
43 \r
44 #include <tbb/concurrent_queue.h>\r
45 \r
46 #include <al/alc.h>\r
47 #include <al/al.h>\r
48 \r
49 #include <array>\r
50 \r
51 namespace caspar { namespace oal {\r
52 \r
53 typedef std::vector<int16_t, tbb::cache_aligned_allocator<int16_t>> audio_buffer_16;\r
54 \r
55 class device\r
56 {\r
57         ALCdevice*                                                                                      device_;\r
58         ALCcontext*                                                                                     context_;\r
59 \r
60 public:\r
61         device()\r
62                 : device_(0)\r
63                 , context_(0)\r
64         {\r
65                 device_ = alcOpenDevice(nullptr);\r
66 \r
67                 if(!device_)\r
68                         CASPAR_THROW_EXCEPTION(invalid_operation() << msg_info("Failed to initialize audio device."));\r
69 \r
70                 context_ = alcCreateContext(device_, nullptr);\r
71 \r
72                 if(!context_)\r
73                         CASPAR_THROW_EXCEPTION(invalid_operation() << msg_info("Failed to create audio context."));\r
74                         \r
75                 if(alcMakeContextCurrent(context_) == ALC_FALSE)\r
76                         CASPAR_THROW_EXCEPTION(invalid_operation() << msg_info("Failed to activate audio context."));\r
77         }\r
78 \r
79         ~device()\r
80         {\r
81                 alcMakeContextCurrent(nullptr);\r
82 \r
83                 if(context_)\r
84                         alcDestroyContext(context_);\r
85 \r
86                 if(device_)\r
87                         alcCloseDevice(device_);\r
88         }\r
89 \r
90         ALCdevice* get()\r
91         {\r
92                 return device_;\r
93         }\r
94 };\r
95 \r
96 void init_device()\r
97 {\r
98         static std::unique_ptr<device> instance;\r
99         static boost::once_flag f = BOOST_ONCE_INIT;\r
100         \r
101         boost::call_once(f, []{instance.reset(new device());});\r
102 }\r
103 \r
104 struct oal_consumer : public core::frame_consumer\r
105 {\r
106         spl::shared_ptr<diagnostics::graph>                                     graph_;\r
107         boost::timer                                                                            perf_timer_;\r
108         int                                                                                                     channel_index_;\r
109         \r
110         core::video_format_desc                                                         format_desc_;\r
111 \r
112         ALuint                                                                                          source_;\r
113         std::array<ALuint, 3>                                                           buffers_;\r
114 \r
115         executor                                                                                        executor_;\r
116 \r
117 public:\r
118         oal_consumer() \r
119                 : channel_index_(-1)\r
120                 , source_(0)\r
121                 , executor_(L"oal_consumer")\r
122         {\r
123                 buffers_.assign(0);\r
124 \r
125                 init_device();\r
126 \r
127                 graph_->set_color("tick-time", diagnostics::color(0.0f, 0.6f, 0.9f));   \r
128                 graph_->set_color("dropped-frame", diagnostics::color(0.3f, 0.6f, 0.3f));\r
129                 graph_->set_color("late-frame", diagnostics::color(0.6f, 0.3f, 0.3f));\r
130                 diagnostics::register_graph(graph_);\r
131         }\r
132 \r
133         ~oal_consumer()\r
134         {\r
135                 executor_.begin_invoke([=]\r
136                 {               \r
137                         if(source_)\r
138                         {\r
139                                 alSourceStop(source_);\r
140                                 alDeleteSources(1, &source_);\r
141                         }\r
142 \r
143                         BOOST_FOREACH(auto& buffer, buffers_)\r
144                         {\r
145                                 if(buffer)\r
146                                         alDeleteBuffers(1, &buffer);\r
147                         };\r
148                 });\r
149         }\r
150 \r
151         // frame consumer\r
152 \r
153         void initialize(const core::video_format_desc& format_desc, int channel_index) override\r
154         {\r
155                 format_desc_    = format_desc;          \r
156                 channel_index_  = channel_index;\r
157                 graph_->set_text(print());\r
158                 \r
159                 executor_.begin_invoke([=]\r
160                 {               \r
161                         alGenBuffers(static_cast<ALsizei>(buffers_.size()), buffers_.data());\r
162                         alGenSources(1, &source_);\r
163 \r
164                         for(std::size_t n = 0; n < buffers_.size(); ++n)\r
165                         {\r
166                                 std::vector<int16_t> audio(format_desc_.audio_cadence[n % format_desc_.audio_cadence.size()], 0);\r
167                                 alBufferData(buffers_[n], AL_FORMAT_STEREO16, audio.data(), static_cast<ALsizei>(audio.size()*sizeof(int16_t)), format_desc_.audio_sample_rate);\r
168                                 alSourceQueueBuffers(source_, 1, &buffers_[n]);\r
169                         }\r
170                         \r
171                         alSourcei(source_, AL_LOOPING, AL_FALSE);\r
172 \r
173                         alSourcePlay(source_);  \r
174                 });\r
175         }\r
176         \r
177         bool send(core::const_frame frame) override\r
178         {                       \r
179                 executor_.begin_invoke([=]\r
180                 {\r
181                         ALenum state; \r
182                         alGetSourcei(source_, AL_SOURCE_STATE,&state);\r
183                         if(state != AL_PLAYING)\r
184                         {\r
185                                 for(int n = 0; n < buffers_.size()-1; ++n)\r
186                                 {                                       \r
187                                         ALuint buffer = 0;  \r
188                                         alSourceUnqueueBuffers(source_, 1, &buffer);\r
189                                         if(buffer)\r
190                                         {\r
191                                                 std::vector<int16_t> audio(format_desc_.audio_cadence[n % format_desc_.audio_cadence.size()], 0);\r
192                                                 alBufferData(buffer, AL_FORMAT_STEREO16, audio.data(), static_cast<ALsizei>(audio.size()*sizeof(int16_t)), format_desc_.audio_sample_rate);\r
193                                                 alSourceQueueBuffers(source_, 1, &buffer);\r
194                                         }\r
195                                 }\r
196                                 alSourcePlay(source_);          \r
197                                 graph_->set_tag("late-frame");  \r
198                         }\r
199 \r
200                         auto audio = core::audio_32_to_16(frame.audio_data());\r
201                         \r
202                         ALuint buffer = 0;  \r
203                         alSourceUnqueueBuffers(source_, 1, &buffer);\r
204                         if(buffer)\r
205                         {\r
206                                 alBufferData(buffer, AL_FORMAT_STEREO16, audio.data(), static_cast<ALsizei>(audio.size()*sizeof(int16_t)), format_desc_.audio_sample_rate);\r
207                                 alSourceQueueBuffers(source_, 1, &buffer);\r
208                         }\r
209                         else\r
210                                 graph_->set_tag("dropped-frame");\r
211 \r
212                         graph_->set_value("tick-time", perf_timer_.elapsed()*format_desc_.fps*0.5);             \r
213                         perf_timer_.restart();\r
214                 });\r
215 \r
216                 return true;\r
217         }\r
218         \r
219         std::wstring print() const override\r
220         {\r
221                 return L"oal[" + boost::lexical_cast<std::wstring>(channel_index_) + L"|" + format_desc_.name + L"]";\r
222         }\r
223 \r
224         std::wstring name() const override\r
225         {\r
226                 return L"system-audio";\r
227         }\r
228 \r
229         boost::property_tree::wptree info() const override\r
230         {\r
231                 boost::property_tree::wptree info;\r
232                 info.add(L"type", L"system-audio");\r
233                 return info;\r
234         }\r
235         \r
236         bool has_synchronization_clock() const override\r
237         {\r
238                 return false;\r
239         }\r
240         \r
241         int buffer_depth() const override\r
242         {\r
243                 return 3;\r
244         }\r
245                 \r
246         int index() const override\r
247         {\r
248                 return 500;\r
249         }\r
250 \r
251         void subscribe(const monitor::observable::observer_ptr& o) override\r
252         {\r
253         }\r
254 \r
255         void unsubscribe(const monitor::observable::observer_ptr& o) override\r
256         {\r
257         }       \r
258 };\r
259 \r
260 spl::shared_ptr<core::frame_consumer> create_consumer(const std::vector<std::wstring>& params)\r
261 {\r
262         if(params.size() < 1 || params[0] != L"AUDIO")\r
263                 return core::frame_consumer::empty();\r
264 \r
265         return spl::make_shared<oal_consumer>();\r
266 }\r
267 \r
268 spl::shared_ptr<core::frame_consumer> create_consumer()\r
269 {\r
270         return spl::make_shared<oal_consumer>();\r
271 }\r
272 \r
273 }}\r