Update with current status
[gnash.git] / libsound / StreamingSoundData.cpp
blobd02c07fd1c62855dc41697f6fe8d63bde790f0cb
1 // StreamingSoundData.cpp - embedded sound definition, for gnash
2 //
3 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
4 // Free Software Foundation, Inc
5 //
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 3 of the License, or
9 // (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "StreamingSoundData.h"
23 #include <vector>
24 #include <cstdint>
26 #include "SoundInfo.h"
27 #include "MediaHandler.h"
28 #include "log.h"
29 #include "GnashException.h"
30 #include "StreamingSound.h"
31 #include "utility.h"
33 namespace gnash {
34 namespace sound {
37 size_t
38 StreamingSoundData::append(SimpleBuffer data,
39 size_t sampleCount, int seekSamples)
41 _buffers.push_back(std::move(data));
42 _blockData.emplace_back(sampleCount, seekSamples);
43 assert(_blockData.size() == _buffers.size());
44 return _buffers.size() - 1;
47 StreamingSoundData::StreamingSoundData(media::SoundInfo info,
48 int nVolume)
50 soundinfo(std::move(info)),
51 volume(nVolume)
55 size_t
56 StreamingSoundData::playingBlock() const
58 if (_soundInstances.empty()) return 0;
59 return static_cast<StreamingSound*>(_soundInstances.front())->currentBlock();
62 void
63 StreamingSoundData::clearInstances()
65 std::lock_guard<std::mutex> lock(_soundInstancesMutex);
66 _soundInstances.clear();
69 StreamingSoundData::Instances::iterator
70 StreamingSoundData::eraseActiveSound(Instances::iterator i)
72 // Mutex intentionally NOT locked...
73 return _soundInstances.erase(i);
76 std::unique_ptr<StreamingSound>
77 StreamingSoundData::createInstance(media::MediaHandler& mh, unsigned long block)
79 std::unique_ptr<StreamingSound> ret(new StreamingSound(*this, mh, block));
81 std::lock_guard<std::mutex> lock(_soundInstancesMutex);
83 // Push the sound onto the playing sounds container.
84 _soundInstances.push_back(ret.get());
86 return ret;
89 StreamingSoundData::~StreamingSoundData()
91 clearInstances();
94 void
95 StreamingSoundData::eraseActiveSound(InputStream* inst)
97 std::lock_guard<std::mutex> lock(_soundInstancesMutex);
99 Instances::iterator it = std::find(
100 _soundInstances.begin(),
101 _soundInstances.end(),
102 inst);
104 if (it == _soundInstances.end()) {
105 log_error("StreamingSoundData::eraseActiveSound: instance %p "
106 "not found!", inst);
107 return;
110 eraseActiveSound(it);
113 bool
114 StreamingSoundData::isPlaying() const
116 std::lock_guard<std::mutex> lock(_soundInstancesMutex);
117 return !_soundInstances.empty();
120 size_t
121 StreamingSoundData::numPlayingInstances() const
123 std::lock_guard<std::mutex> lock(_soundInstancesMutex);
124 return _soundInstances.size();
127 InputStream*
128 StreamingSoundData::firstPlayingInstance() const
130 std::lock_guard<std::mutex> lock(_soundInstancesMutex);
131 return _soundInstances.front();
134 void
135 StreamingSoundData::getPlayingInstances(std::vector<InputStream*>& to) const
137 std::lock_guard<std::mutex> lock(_soundInstancesMutex);
138 for (InputStream* stream : _soundInstances)
140 to.push_back(stream);
144 } // gnash.sound namespace
145 } // namespace gnash