]> git.sesse.net Git - casparcg/blob - modules/oal/consumer/oal_consumer.cpp
2.1.0: -oal_consumer: Is no longer synchronizing.
[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/diagnostics/graph.h>\r
26 #include <common/log.h>\r
27 #include <common/utf.h>\r
28 #include <common/env.h>\r
29 \r
30 #include <core/consumer/frame_consumer.h>\r
31 #include <core/frame/frame.h>\r
32 #include <core/mixer/audio/audio_util.h>\r
33 #include <core/mixer/audio/audio_mixer.h>\r
34 #include <core/video_format.h>\r
35 \r
36 #include <SFML/Audio/SoundStream.hpp>\r
37 \r
38 #include <boost/circular_buffer.hpp>\r
39 #include <boost/lexical_cast.hpp>\r
40 #include <boost/property_tree/ptree.hpp>\r
41 #include <boost/timer.hpp>\r
42 \r
43 #include <tbb/concurrent_queue.h>\r
44 \r
45 namespace caspar { namespace oal {\r
46 \r
47 typedef std::vector<int16_t, tbb::cache_aligned_allocator<int16_t>> audio_buffer_16;\r
48 \r
49 struct oal_consumer : public core::frame_consumer,  public sf::SoundStream\r
50 {\r
51         spl::shared_ptr<diagnostics::graph>                                             graph_;\r
52         boost::timer                                                                            perf_timer_;\r
53         int                                                                                                     channel_index_;\r
54 \r
55         tbb::concurrent_bounded_queue<std::shared_ptr<audio_buffer_16>> input_;\r
56         boost::circular_buffer<audio_buffer_16>                         container_;\r
57         tbb::atomic<bool>                                                                       is_running_;\r
58         core::audio_buffer                                                                      temp;\r
59 \r
60         core::video_format_desc                                                         format_desc_;\r
61 public:\r
62         oal_consumer() \r
63                 : container_(16)\r
64                 , channel_index_(-1)\r
65         {\r
66                 graph_->set_color("tick-time", diagnostics::color(0.0f, 0.6f, 0.9f));   \r
67                 graph_->set_color("dropped-frame", diagnostics::color(0.3f, 0.6f, 0.3f));\r
68                 diagnostics::register_graph(graph_);\r
69 \r
70                 is_running_ = true;\r
71                 input_.set_capacity(2);\r
72         }\r
73 \r
74         ~oal_consumer()\r
75         {\r
76                 is_running_ = false;\r
77                 input_.try_push(std::make_shared<audio_buffer_16>());\r
78                 input_.try_push(std::make_shared<audio_buffer_16>());\r
79                 Stop();\r
80                 input_.try_push(std::make_shared<audio_buffer_16>());\r
81                 input_.try_push(std::make_shared<audio_buffer_16>());\r
82         }\r
83 \r
84         // frame consumer\r
85 \r
86         virtual void initialize(const core::video_format_desc& format_desc, int channel_index) override\r
87         {\r
88                 format_desc_    = format_desc;          \r
89                 channel_index_  = channel_index;\r
90                 graph_->set_text(print());\r
91 \r
92                 if(Status() != Playing)\r
93                 {\r
94                         sf::SoundStream::Initialize(2, 48000);\r
95                         Play();         \r
96                 }\r
97         }\r
98         \r
99         virtual bool send(core::const_frame frame) override\r
100         {                       \r
101                 if(!input_.try_push(std::make_shared<audio_buffer_16>(core::audio_32_to_16(frame.audio_data()))))\r
102                         graph_->set_tag("dropped-frame");\r
103 \r
104                 return true;\r
105         }\r
106         \r
107         virtual std::wstring print() const override\r
108         {\r
109                 return L"oal[" + boost::lexical_cast<std::wstring>(channel_index_) + L"|" + format_desc_.name + L"]";\r
110         }\r
111 \r
112         virtual std::wstring name() const override\r
113         {\r
114                 return L"system-audio";\r
115         }\r
116 \r
117         virtual boost::property_tree::wptree info() const override\r
118         {\r
119                 boost::property_tree::wptree info;\r
120                 info.add(L"type", L"system-audio");\r
121                 return info;\r
122         }\r
123         \r
124         virtual bool has_synchronization_clock() const override\r
125         {\r
126                 return false;\r
127         }\r
128         \r
129         virtual int buffer_depth() const override\r
130         {\r
131                 return 3;\r
132         }\r
133 \r
134         // oal_consumer\r
135         \r
136         virtual bool OnGetData(sf::SoundStream::Chunk& data) override\r
137         {               \r
138                 std::shared_ptr<audio_buffer_16> audio_data;            \r
139                 input_.pop(audio_data);\r
140                                 \r
141                 container_.push_back(std::move(*audio_data));\r
142                 data.Samples = container_.back().data();\r
143                 data.NbSamples = container_.back().size();      \r
144                 \r
145                 graph_->set_value("tick-time", perf_timer_.elapsed()*format_desc_.fps*0.5);             \r
146                 perf_timer_.restart();\r
147 \r
148                 return is_running_;\r
149         }\r
150 \r
151         virtual int index() const override\r
152         {\r
153                 return 500;\r
154         }\r
155 };\r
156 \r
157 spl::shared_ptr<core::frame_consumer> create_consumer(const std::vector<std::wstring>& params)\r
158 {\r
159         if(params.size() < 1 || params[0] != L"AUDIO")\r
160                 return core::frame_consumer::empty();\r
161 \r
162         return spl::make_shared<oal_consumer>();\r
163 }\r
164 \r
165 spl::shared_ptr<core::frame_consumer> create_consumer()\r
166 {\r
167         return spl::make_shared<oal_consumer>();\r
168 }\r
169 \r
170 }}\r