]> git.sesse.net Git - casparcg/blob - modules/oal/consumer/oal_consumer.cpp
## Refactoring of rendering pipeline and mixer.
[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/exception/exceptions.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/mixer/audio/audio_util.h>\r
32 #include <core/mixer/audio/audio_mixer.h>\r
33 #include <core/video_format.h>\r
34 \r
35 #include <core/frame.h>\r
36 \r
37 #include <SFML/Audio/SoundStream.hpp>\r
38 \r
39 #include <boost/circular_buffer.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         safe_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                 CASPAR_LOG(info) << print() << L" Successfully Uninitialized."; \r
83         }\r
84 \r
85         // frame consumer\r
86 \r
87         virtual 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                 CASPAR_LOG(info) << print() << " Sucessfully Initialized.";\r
99         }\r
100         \r
101         virtual bool send(const safe_ptr<const core::frame>& frame) override\r
102         {                       \r
103                 input_.push(std::make_shared<audio_buffer_16>(core::audio_32_to_16(frame->audio_data())));\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 boost::property_tree::wptree info() const override\r
113         {\r
114                 boost::property_tree::wptree info;\r
115                 info.add(L"type", L"oal-consumer");\r
116                 return info;\r
117         }\r
118         \r
119         virtual int buffer_depth() const override\r
120         {\r
121                 return 3;\r
122         }\r
123 \r
124         // oal_consumer\r
125         \r
126         virtual bool OnGetData(sf::SoundStream::Chunk& data) override\r
127         {               \r
128                 std::shared_ptr<audio_buffer_16> audio_data;            \r
129                 input_.pop(audio_data);\r
130                                 \r
131                 container_.push_back(std::move(*audio_data));\r
132                 data.Samples = container_.back().data();\r
133                 data.NbSamples = container_.back().size();      \r
134                 \r
135                 graph_->set_value("tick-time", perf_timer_.elapsed()*format_desc_.fps*0.5);             \r
136                 perf_timer_.restart();\r
137 \r
138                 return is_running_;\r
139         }\r
140 \r
141         virtual int index() const override\r
142         {\r
143                 return 500;\r
144         }\r
145 };\r
146 \r
147 safe_ptr<core::frame_consumer> create_consumer(const std::vector<std::wstring>& params)\r
148 {\r
149         if(params.size() < 1 || params[0] != L"AUDIO")\r
150                 return core::frame_consumer::empty();\r
151 \r
152         return make_safe<oal_consumer>();\r
153 }\r
154 \r
155 safe_ptr<core::frame_consumer> create_consumer()\r
156 {\r
157         return make_safe<oal_consumer>();\r
158 }\r
159 \r
160 }}\r