]> git.sesse.net Git - casparcg/blob - modules/oal/consumer/oal_consumer.cpp
2.1.0: -oal_consumer: Set thread priority to higher than normal.
[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         bool                                                                                            set_thread_priority_;\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                 , set_thread_priority_(true)\r
66         {\r
67                 graph_->set_color("tick-time", diagnostics::color(0.0f, 0.6f, 0.9f));   \r
68                 graph_->set_color("dropped-frame", diagnostics::color(0.3f, 0.6f, 0.3f));\r
69                 diagnostics::register_graph(graph_);\r
70 \r
71                 is_running_ = true;\r
72                 input_.set_capacity(1);\r
73         }\r
74 \r
75         ~oal_consumer()\r
76         {\r
77                 is_running_ = false;\r
78                 input_.try_push(std::make_shared<audio_buffer_16>());\r
79                 input_.try_push(std::make_shared<audio_buffer_16>());\r
80                 Stop();\r
81                 input_.try_push(std::make_shared<audio_buffer_16>());\r
82                 input_.try_push(std::make_shared<audio_buffer_16>());\r
83         }\r
84 \r
85         // frame consumer\r
86 \r
87         void initialize(const core::video_format_desc& format_desc, int channel_index) override\r
88         {\r
89                 format_desc_    = format_desc;          \r
90                 channel_index_  = channel_index;\r
91                 graph_->set_text(print());\r
92 \r
93                 if(Status() != Playing)\r
94                 {\r
95                         sf::SoundStream::Initialize(2, 48000);\r
96                         Play();         \r
97                 }\r
98         }\r
99         \r
100         bool send(core::const_frame frame) override\r
101         {                       \r
102                 if(!input_.try_push(std::make_shared<audio_buffer_16>(core::audio_32_to_16(frame.audio_data()))))\r
103                         graph_->set_tag("dropped-frame");\r
104 \r
105                 return true;\r
106         }\r
107         \r
108         std::wstring print() const override\r
109         {\r
110                 return L"oal[" + boost::lexical_cast<std::wstring>(channel_index_) + L"|" + format_desc_.name + L"]";\r
111         }\r
112 \r
113         std::wstring name() const override\r
114         {\r
115                 return L"system-audio";\r
116         }\r
117 \r
118         boost::property_tree::wptree info() const override\r
119         {\r
120                 boost::property_tree::wptree info;\r
121                 info.add(L"type", L"system-audio");\r
122                 return info;\r
123         }\r
124         \r
125         bool has_synchronization_clock() const override\r
126         {\r
127                 return false;\r
128         }\r
129         \r
130         int buffer_depth() const override\r
131         {\r
132                 return 3;\r
133         }\r
134 \r
135         // oal_consumer\r
136         \r
137         bool OnGetData(sf::SoundStream::Chunk& data) override\r
138         {               \r
139                 if(set_thread_priority_)\r
140                 {\r
141                         SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_ABOVE_NORMAL);\r
142                         set_thread_priority_ = false;\r
143                 }\r
144 \r
145                 std::shared_ptr<audio_buffer_16> audio_data;            \r
146                 input_.pop(audio_data);\r
147                                 \r
148                 container_.push_back(std::move(*audio_data));\r
149                 data.Samples = container_.back().data();\r
150                 data.NbSamples = container_.back().size();      \r
151                 \r
152                 graph_->set_value("tick-time", perf_timer_.elapsed()*format_desc_.fps*0.5);             \r
153                 perf_timer_.restart();\r
154 \r
155                 return is_running_;\r
156         }\r
157 \r
158         int index() const override\r
159         {\r
160                 return 500;\r
161         }\r
162 };\r
163 \r
164 spl::shared_ptr<core::frame_consumer> create_consumer(const std::vector<std::wstring>& params)\r
165 {\r
166         if(params.size() < 1 || params[0] != L"AUDIO")\r
167                 return core::frame_consumer::empty();\r
168 \r
169         return spl::make_shared<oal_consumer>();\r
170 }\r
171 \r
172 spl::shared_ptr<core::frame_consumer> create_consumer()\r
173 {\r
174         return spl::make_shared<oal_consumer>();\r
175 }\r
176 \r
177 }}\r