]> git.sesse.net Git - casparcg/blob - modules/oal/consumer/oal_consumer.cpp
Reverted gpu refactoring. Will move to its own branch.
[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/video_format.h>\r
33 \r
34 #include <core/mixer/read_frame.h>\r
35 \r
36 #include <SFML/Audio/SoundStream.hpp>\r
37 \r
38 #include <boost/circular_buffer.hpp>\r
39 #include <boost/property_tree/ptree.hpp>\r
40 #include <boost/timer.hpp>\r
41 \r
42 #include <tbb/concurrent_queue.h>\r
43 \r
44 namespace caspar { namespace oal {\r
45 \r
46 typedef std::vector<int16_t, tbb::cache_aligned_allocator<int16_t>> audio_buffer_16;\r
47 \r
48 struct oal_consumer : public core::frame_consumer,  public sf::SoundStream\r
49 {\r
50         safe_ptr<diagnostics::graph>                                            graph_;\r
51         boost::timer                                                                            perf_timer_;\r
52         int                                                                                                     channel_index_;\r
53 \r
54         tbb::concurrent_bounded_queue<std::shared_ptr<audio_buffer_16>> input_;\r
55         boost::circular_buffer<audio_buffer_16>                         container_;\r
56         tbb::atomic<bool>                                                                       is_running_;\r
57         core::audio_buffer                                                                      temp;\r
58 \r
59         core::video_format_desc                                                         format_desc_;\r
60 public:\r
61         oal_consumer() \r
62                 : container_(16)\r
63                 , channel_index_(-1)\r
64         {\r
65                 graph_->set_color("tick-time", diagnostics::color(0.0f, 0.6f, 0.9f));   \r
66                 diagnostics::register_graph(graph_);\r
67 \r
68                 is_running_ = true;\r
69                 input_.set_capacity(1);\r
70         }\r
71 \r
72         ~oal_consumer()\r
73         {\r
74                 is_running_ = false;\r
75                 input_.try_push(std::make_shared<audio_buffer_16>());\r
76                 input_.try_push(std::make_shared<audio_buffer_16>());\r
77                 Stop();\r
78                 input_.try_push(std::make_shared<audio_buffer_16>());\r
79                 input_.try_push(std::make_shared<audio_buffer_16>());\r
80 \r
81                 CASPAR_LOG(info) << print() << L" Successfully Uninitialized."; \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                 CASPAR_LOG(info) << print() << " Sucessfully Initialized.";\r
98         }\r
99         \r
100         virtual bool send(const safe_ptr<core::read_frame>& frame) override\r
101         {                       \r
102                 input_.push(std::make_shared<audio_buffer_16>(core::audio_32_to_16(frame->audio_data())));\r
103                 return true;\r
104         }\r
105         \r
106         virtual std::wstring print() const override\r
107         {\r
108                 return L"oal[" + boost::lexical_cast<std::wstring>(channel_index_) + L"|" + format_desc_.name + L"]";\r
109         }\r
110 \r
111         virtual boost::property_tree::wptree info() const override\r
112         {\r
113                 boost::property_tree::wptree info;\r
114                 info.add(L"type", L"oal-consumer");\r
115                 return info;\r
116         }\r
117         \r
118         virtual int buffer_depth() const override\r
119         {\r
120                 return 3;\r
121         }\r
122 \r
123         // oal_consumer\r
124         \r
125         virtual bool OnGetData(sf::SoundStream::Chunk& data) override\r
126         {               \r
127                 std::shared_ptr<audio_buffer_16> audio_data;            \r
128                 input_.pop(audio_data);\r
129                                 \r
130                 container_.push_back(std::move(*audio_data));\r
131                 data.Samples = container_.back().data();\r
132                 data.NbSamples = container_.back().size();      \r
133                 \r
134                 graph_->set_value("tick-time", perf_timer_.elapsed()*format_desc_.fps*0.5);             \r
135                 perf_timer_.restart();\r
136 \r
137                 return is_running_;\r
138         }\r
139 \r
140         virtual int index() const override\r
141         {\r
142                 return 500;\r
143         }\r
144 };\r
145 \r
146 safe_ptr<core::frame_consumer> create_consumer(const std::vector<std::wstring>& params)\r
147 {\r
148         if(params.size() < 1 || params[0] != L"AUDIO")\r
149                 return core::frame_consumer::empty();\r
150 \r
151         return make_safe<oal_consumer>();\r
152 }\r
153 \r
154 safe_ptr<core::frame_consumer> create_consumer()\r
155 {\r
156         return make_safe<oal_consumer>();\r
157 }\r
158 \r
159 }}\r