]> git.sesse.net Git - casparcg/blob - core/consumer/oal/oal_consumer.cpp
2.0.0.2: Renamed "processor" to "mixer".
[casparcg] / core / consumer / oal / 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 "../../StdAfx.h"\r
22 \r
23 #include "oal_consumer.h"\r
24 \r
25 #include "../../video_format.h"\r
26 \r
27 #include "../../mixer/frame/read_frame.h"\r
28 \r
29 #include <SFML/Audio.hpp>\r
30 \r
31 #include <boost/circular_buffer.hpp>\r
32 \r
33 namespace caspar { namespace core {\r
34 \r
35 struct oal_consumer::implementation : public sf::SoundStream, boost::noncopyable\r
36 {\r
37         tbb::concurrent_bounded_queue<std::vector<short>> input_;\r
38         boost::circular_buffer<std::vector<short>> container_;\r
39         tbb::atomic<bool> is_running_;\r
40 public:\r
41         implementation() \r
42                 : container_(5)\r
43         {\r
44                 is_running_ = true;\r
45                 sf::SoundStream::Initialize(2, 48000);\r
46                 Play();         \r
47                 CASPAR_LOG(info) << "Sucessfully started oal_consumer";\r
48         }\r
49 \r
50         ~implementation()\r
51         {\r
52                 is_running_ = false;\r
53                 input_.try_push(std::vector<short>());\r
54                 input_.try_push(std::vector<short>());\r
55                 CASPAR_LOG(info) << "Sucessfully ended oal_consumer";\r
56         }\r
57         \r
58         virtual void send(const safe_ptr<const read_frame>& frame)\r
59         {                               \r
60                 if(frame->audio_data().empty())\r
61                         return;\r
62 \r
63                 input_.push(std::vector<short>(frame->audio_data().begin(), frame->audio_data().end()));        \r
64         }\r
65 \r
66         virtual size_t buffer_depth() const{return 3;}\r
67 \r
68         virtual bool OnGetData(sf::SoundStream::Chunk& data)\r
69         {               \r
70                 std::vector<short> audio_data;          \r
71                 input_.pop(audio_data);\r
72                                 \r
73                 container_.push_back(std::move(audio_data));\r
74                 data.Samples = container_.back().data();\r
75                 data.NbSamples = container_.back().size();              \r
76 \r
77                 return is_running_;\r
78         }\r
79 };\r
80 \r
81 oal_consumer::oal_consumer(oal_consumer&& other) : impl_(std::move(other.impl_)){}\r
82 oal_consumer::oal_consumer(const video_format_desc&) : impl_(new implementation()){}\r
83 void oal_consumer::send(const safe_ptr<const read_frame>& frame){impl_->send(frame);}\r
84 size_t oal_consumer::buffer_depth() const{return impl_->buffer_depth();}\r
85 \r
86 safe_ptr<frame_consumer> create_oal_consumer(const std::vector<std::wstring>& params)\r
87 {\r
88         if(params.size() < 2 || params[0] != L"OAL")\r
89                 return frame_consumer::empty();\r
90 \r
91         auto format_desc = video_format_desc::get(params[1]);\r
92         if(format_desc.format == video_format::invalid)\r
93                 return frame_consumer::empty();\r
94 \r
95         return make_safe<oal_consumer>(format_desc);\r
96 }\r
97 }}\r