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