]> git.sesse.net Git - casparcg/blob - modules/oal/consumer/oal_consumer.cpp
3acd140f21e17f48a561c332b09127b5f273717b
[casparcg] / modules / oal / consumer / oal_consumer.cpp
1 /*\r
2 * copyright (c) 2010 Sveriges Television AB <info@casparcg.com>\r
3 *\r
4 *  This file is part of CasparCG.\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 */\r
20  \r
21 #include "oal_consumer.h"\r
22 \r
23 #include <common/exception/exceptions.h>\r
24 #include <common/diagnostics/graph.h>\r
25 #include <common/log/log.h>\r
26 #include <common/utility/timer.h>\r
27 #include <common/utility/string.h>\r
28 \r
29 #include <core/consumer/frame_consumer.h>\r
30 #include <core/mixer/audio/audio_util.h>\r
31 #include <core/video_format.h>\r
32 \r
33 #include <core/mixer/read_frame.h>\r
34 \r
35 #include <SFML/Audio.hpp>\r
36 \r
37 #include <boost/circular_buffer.hpp>\r
38 #include <boost/timer.hpp>\r
39 \r
40 #include <tbb/concurrent_queue.h>\r
41 \r
42 namespace caspar { namespace oal {\r
43 \r
44 struct oal_consumer : public core::frame_consumer,  public sf::SoundStream\r
45 {\r
46         safe_ptr<diagnostics::graph>                                            graph_;\r
47         boost::timer                                                                            perf_timer_;\r
48 \r
49         tbb::concurrent_bounded_queue<std::shared_ptr<std::vector<int16_t, tbb::cache_aligned_allocator<int16_t>>>>     input_;\r
50         boost::circular_buffer<std::vector<int16_t, tbb::cache_aligned_allocator<int16_t>>>                     container_;\r
51         tbb::atomic<bool>                                                                       is_running_;\r
52 \r
53         core::video_format_desc                                                         format_desc_;\r
54 public:\r
55         oal_consumer() \r
56                 : container_(16)\r
57         {\r
58                 graph_->add_guide("tick-time", 0.5);\r
59                 graph_->set_color("tick-time", diagnostics::color(0.0f, 0.6f, 0.9f));   \r
60                 graph_->set_text(print());\r
61                 diagnostics::register_graph(graph_);\r
62 \r
63                 is_running_ = true;\r
64                 input_.set_capacity(1);\r
65         }\r
66 \r
67         ~oal_consumer()\r
68         {\r
69                 is_running_ = false;\r
70                 input_.try_push(std::make_shared<std::vector<int16_t, tbb::cache_aligned_allocator<int16_t>>>());\r
71                 input_.try_push(std::make_shared<std::vector<int16_t, tbb::cache_aligned_allocator<int16_t>>>());\r
72                 Stop();\r
73                 input_.try_push(std::make_shared<std::vector<int16_t, tbb::cache_aligned_allocator<int16_t>>>());\r
74                 input_.try_push(std::make_shared<std::vector<int16_t, tbb::cache_aligned_allocator<int16_t>>>());\r
75                 CASPAR_LOG(info) << print() << L" Shutting down.";      \r
76         }\r
77 \r
78         virtual void initialize(const core::video_format_desc& format_desc)\r
79         {\r
80                 format_desc_ = format_desc;             \r
81                 if(Status() != Playing)\r
82                 {\r
83                         sf::SoundStream::Initialize(2, 48000);\r
84                         Play();         \r
85                 }\r
86                 CASPAR_LOG(info) << print() << " Sucessfully initialized.";\r
87         }\r
88         \r
89         virtual bool send(const safe_ptr<core::read_frame>& frame)\r
90         {                       \r
91                 input_.push(std::make_shared<std::vector<int16_t, tbb::cache_aligned_allocator<int16_t>>>(core::audio_32_to_16_sse(frame->audio_data())));\r
92 \r
93                 return true;\r
94         }\r
95         \r
96         virtual bool OnGetData(sf::SoundStream::Chunk& data)\r
97         {               \r
98                 std::shared_ptr<std::vector<int16_t, tbb::cache_aligned_allocator<int16_t>>> audio_data;                \r
99                 input_.pop(audio_data);\r
100                                 \r
101                 container_.push_back(std::move(*audio_data));\r
102                 data.Samples = container_.back().data();\r
103                 data.NbSamples = container_.back().size();      \r
104                 \r
105                 graph_->update_value("tick-time", perf_timer_.elapsed()*format_desc_.fps*0.5);          \r
106                 perf_timer_.restart();\r
107 \r
108                 return is_running_;\r
109         }\r
110 \r
111         virtual std::wstring print() const\r
112         {\r
113                 return L"oal[" + format_desc_.name + L"]";\r
114         }\r
115         \r
116         virtual size_t buffer_depth() const\r
117         {\r
118                 return 2;\r
119         }\r
120 };\r
121 \r
122 safe_ptr<core::frame_consumer> create_consumer(const std::vector<std::wstring>& params)\r
123 {\r
124         if(params.size() < 1 || params[0] != L"AUDIO")\r
125                 return core::frame_consumer::empty();\r
126 \r
127         return make_safe<oal_consumer>();\r
128 }\r
129 \r
130 safe_ptr<core::frame_consumer> create_consumer()\r
131 {\r
132         return make_safe<oal_consumer>();\r
133 }\r
134 \r
135 }}\r