]> git.sesse.net Git - casparcg/blob - modules/oal/consumer/oal_consumer.cpp
cb727f62905b54c5189abdbcd2066b8543fd2926
[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/video_format.h>\r
31 \r
32 #include <core/mixer/read_frame.h>\r
33 \r
34 #include <SFML/Audio.hpp>\r
35 \r
36 #include <boost/circular_buffer.hpp>\r
37 #include <boost/timer.hpp>\r
38 \r
39 #include <tbb/concurrent_queue.h>\r
40 \r
41 namespace caspar {\r
42 \r
43 struct oal_consumer : public core::frame_consumer,  public sf::SoundStream\r
44 {\r
45         safe_ptr<diagnostics::graph>                                            graph_;\r
46         boost::timer                                                                            perf_timer_;\r
47 \r
48         tbb::concurrent_bounded_queue<std::shared_ptr<std::vector<short>>>      input_;\r
49         boost::circular_buffer<std::vector<short>>                      container_;\r
50         tbb::atomic<bool>                                                                       is_running_;\r
51 \r
52         core::video_format_desc                                                         format_desc_;\r
53         int                                                                                                     preroll_count_;\r
54 public:\r
55         oal_consumer() \r
56                 : graph_(diagnostics::create_graph(narrow(print())))\r
57                 , container_(16)\r
58                 , preroll_count_(0)\r
59         {\r
60                 if(core::consumer_buffer_depth() < 3)\r
61                         BOOST_THROW_EXCEPTION(invalid_argument() << msg_info("audio-consumer does not support buffer-depth lower than 3."));\r
62 \r
63                 graph_->add_guide("tick-time", 0.5);\r
64                 graph_->set_color("tick-time", diagnostics::color(0.0f, 0.6f, 0.9f));   \r
65                 is_running_ = true;\r
66                 input_.set_capacity(core::consumer_buffer_depth()-2);\r
67         }\r
68 \r
69         ~oal_consumer()\r
70         {\r
71                 is_running_ = false;\r
72                 input_.try_push(std::make_shared<std::vector<short>>());\r
73                 input_.try_push(std::make_shared<std::vector<short>>());\r
74                 Stop();\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                 sf::SoundStream::Initialize(2, 48000);\r
82                 CASPAR_LOG(info) << print() << " Sucessfully initialized.";\r
83         }\r
84         \r
85         virtual bool send(const safe_ptr<core::read_frame>& frame)\r
86         {                       \r
87                 if(preroll_count_ < input_.capacity())\r
88                 {\r
89                         while(input_.try_push(std::make_shared<std::vector<int16_t>>(format_desc_.audio_samples_per_frame, 0)))\r
90                                 ++preroll_count_;\r
91                         Play();         \r
92                 }\r
93 \r
94                 std::vector<int16_t> audio16(frame->audio_data().size());\r
95                 for(size_t n = 0; n < audio16.size(); ++n)              \r
96                         audio16[n] = (frame->audio_data()[n] >> 16) & 0xffff;           \r
97 \r
98                 input_.push(std::make_shared<std::vector<int16_t>>(std::move(audio16)));\r
99 \r
100                 return true;\r
101         }\r
102         \r
103         virtual bool OnGetData(sf::SoundStream::Chunk& data)\r
104         {               \r
105                 std::shared_ptr<std::vector<short>> audio_data;         \r
106                 input_.pop(audio_data);\r
107                                 \r
108                 container_.push_back(std::move(*audio_data));\r
109                 data.Samples = container_.back().data();\r
110                 data.NbSamples = container_.back().size();      \r
111                 \r
112                 graph_->update_value("tick-time", perf_timer_.elapsed()*format_desc_.fps*0.5);          \r
113                 perf_timer_.restart();\r
114 \r
115                 return is_running_;\r
116         }\r
117 \r
118         virtual std::wstring print() const\r
119         {\r
120                 return L"oal[" + format_desc_.name + L"]";\r
121         }\r
122 \r
123         virtual const core::video_format_desc& get_video_format_desc() const\r
124         {\r
125                 return format_desc_;\r
126         }\r
127 };\r
128 \r
129 safe_ptr<core::frame_consumer> create_oal_consumer(const std::vector<std::wstring>& params)\r
130 {\r
131         if(params.size() < 1 || params[0] != L"OAL")\r
132                 return core::frame_consumer::empty();\r
133 \r
134         return make_safe<oal_consumer>();\r
135 }\r
136 \r
137 safe_ptr<core::frame_consumer> create_oal_consumer()\r
138 {\r
139         return make_safe<oal_consumer>();\r
140 }\r
141 \r
142 }\r