]> git.sesse.net Git - casparcg/blob - core/consumer/oal/oal_frame_consumer.cpp
2.0.0.2:
[casparcg] / core / consumer / oal / oal_frame_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_frame_consumer.h"\r
24 \r
25 #include "../../frame/gpu_frame.h"\r
26 #include "../../frame/frame_format.h"\r
27 \r
28 #include <SFML/Audio.hpp>\r
29 \r
30 #include <boost/circular_buffer.hpp>\r
31 #include <boost/foreach.hpp>\r
32 \r
33 #include <windows.h>\r
34 \r
35 namespace caspar{ namespace audio{      \r
36 \r
37 class sound_channel : public sf::SoundStream\r
38 {\r
39 public:\r
40         sound_channel() : internal_chunks_(5), silence_(1920*2, 0)\r
41         {\r
42                 external_chunks_.set_capacity(3);\r
43                 sf::SoundStream::Initialize(2, 48000);\r
44         }\r
45 \r
46         ~sound_channel()\r
47         {\r
48                 external_chunks_.clear();\r
49                 external_chunks_.push(nullptr);\r
50                 Stop();\r
51         }\r
52         \r
53         void push(const gpu_frame_ptr& frame)\r
54         {\r
55                 if(frame->audio_data().empty())\r
56                         frame->audio_data() = silence_;\r
57 \r
58                 //if(!external_chunks_.try_push(frame))\r
59                 //{\r
60                         //CASPAR_LOG(debug) << "Sound Buffer Overrun";\r
61                         external_chunks_.push(frame);\r
62                 //}\r
63 \r
64                 if(GetStatus() != Playing && external_chunks_.size() >= 3)\r
65                         Play();\r
66         }\r
67 \r
68 private:\r
69 \r
70         bool OnStart() \r
71         {\r
72                 external_chunks_.clear();\r
73                 return true;\r
74         }\r
75 \r
76         bool OnGetData(sf::SoundStream::Chunk& data)\r
77     {\r
78                 gpu_frame_ptr frame;\r
79                 //if(!external_chunks_.try_pop(frame))\r
80                 //{\r
81                         //CASPAR_LOG(trace) << "Sound Buffer Underrun";\r
82                         external_chunks_.pop(frame);\r
83                 //}\r
84 \r
85                 if(frame == nullptr)\r
86                 {\r
87                         external_chunks_.clear();\r
88                         return false;\r
89                 }\r
90 \r
91                 internal_chunks_.push_back(frame);\r
92                 //SetVolume(pChunk->volume());\r
93                 data.Samples = reinterpret_cast<sf::Int16*>(frame->audio_data().data());\r
94                 data.NbSamples = frame->audio_data().size();\r
95         return true;\r
96     }\r
97 \r
98         std::vector<short> silence_;\r
99         boost::circular_buffer<gpu_frame_ptr> internal_chunks_;\r
100         tbb::concurrent_bounded_queue<gpu_frame_ptr> external_chunks_;\r
101 };\r
102 typedef std::shared_ptr<sound_channel> sound_channelPtr;\r
103 \r
104 struct oal_frame_consumer::implementation : boost::noncopyable\r
105 {       \r
106         implementation(const frame_format_desc& format_desc) : format_desc_(format_desc)\r
107         {\r
108         }\r
109         \r
110         void push(const gpu_frame_ptr& frame)\r
111         {\r
112                 channel_.push(frame);\r
113         }\r
114                 \r
115         sound_channel channel_;\r
116 \r
117         caspar::frame_format_desc format_desc_;\r
118 };\r
119 \r
120 oal_frame_consumer::oal_frame_consumer(const caspar::frame_format_desc& format_desc) : impl_(new implementation(format_desc)){}\r
121 const caspar::frame_format_desc& oal_frame_consumer::get_frame_format_desc() const{return impl_->format_desc_;}\r
122 void oal_frame_consumer::prepare(const gpu_frame_ptr& frame){impl_->push(frame);}\r
123 }}\r