Include program counter on action limit notification log
[gnash.git] / libsound / EmbedSound.cpp
blob5c93c241b955b8df9054abf231f3226d08cfbfa0
1 // EmbedSound.cpp - embedded sound definition, for gnash
2 //
3 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Free Software
4 // 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 "EmbedSound.h"
22 #include "EmbedSoundInst.h" // for createInstance
23 #include "SoundInfo.h"
25 #include "MediaHandler.h" // for the singleton (getInputPaddingSize)
27 #include "log.h" // will import boost::format too
28 #include "GnashException.h" // for SoundException
30 //#include <cmath>
31 #include <vector>
32 //#include <boost/scoped_array.hpp>
33 #include <boost/cstdint.hpp> // For C99 int types
35 namespace gnash {
36 namespace sound {
38 void
39 EmbedSound::append(boost::uint8_t* data, unsigned int size)
41 /// @todo, rather then copying the data over,
42 /// keep it in its original form (multi-buffer)
43 /// This way we avoid memory copies and we'd
44 /// have no need for the additional m_frames_sizes
45 /// map..
47 // Remember size of this block, indexing by offset
48 m_frames_size[_buf->size()] = size;
50 _buf->reserve(_buf->size() + size + _paddingBytes);
51 _buf->append(data, size);
53 // since ownership was transferred...
54 delete [] data;
57 EmbedSound::EmbedSound(std::auto_ptr<SimpleBuffer> data,
58 std::auto_ptr<media::SoundInfo> info, int nVolume, size_t paddingBytes)
60 _buf(data),
61 soundinfo(info),
62 volume(nVolume),
63 _paddingBytes(paddingBytes)
65 if ( _buf.get() )
67 if (_buf->capacity() - _buf->size() < paddingBytes) {
68 log_error("EmbedSound creator didn't appropriately pad sound data. "
69 "We'll do now, but will cost memory copies.");
70 _buf->reserve(_buf->size()+paddingBytes);
73 else
75 _buf.reset(new SimpleBuffer());
79 void
80 EmbedSound::clearInstances()
82 boost::mutex::scoped_lock lock(_soundInstancesMutex);
83 _soundInstances.clear();
86 EmbedSound::Instances::iterator
87 EmbedSound::eraseActiveSound(Instances::iterator i)
89 // Mutex intentionally NOT locked...
90 return _soundInstances.erase(i);
93 std::auto_ptr<EmbedSoundInst>
94 EmbedSound::createInstance(media::MediaHandler& mh,
95 unsigned long blockOffset,
96 unsigned int inPoint,
97 unsigned int outPoint,
98 const SoundEnvelopes* envelopes,
99 unsigned int loopCount)
101 std::auto_ptr<EmbedSoundInst> ret ( new EmbedSoundInst(
102 *this,
103 mh, blockOffset,
104 inPoint, outPoint,
105 envelopes,
106 loopCount) );
108 boost::mutex::scoped_lock lock(_soundInstancesMutex);
110 // Push the sound onto the playing sounds container.
111 _soundInstances.push_back(ret.get());
113 return ret;
116 EmbedSound::~EmbedSound()
118 clearInstances();
121 void
122 EmbedSound::eraseActiveSound(EmbedSoundInst* inst)
124 boost::mutex::scoped_lock lock(_soundInstancesMutex);
126 Instances::iterator it = std::find(
127 _soundInstances.begin(),
128 _soundInstances.end(),
129 inst);
131 if ( it == _soundInstances.end() )
133 log_error("EmbedSound::eraseActiveSound: instance %p not found!", inst);
134 return;
137 eraseActiveSound(it);
140 bool
141 EmbedSound::isPlaying() const
143 boost::mutex::scoped_lock lock(_soundInstancesMutex);
144 return !_soundInstances.empty();
147 size_t
148 EmbedSound::numPlayingInstances() const
150 boost::mutex::scoped_lock lock(_soundInstancesMutex);
151 return _soundInstances.size();
154 EmbedSoundInst*
155 EmbedSound::firstPlayingInstance() const
157 boost::mutex::scoped_lock lock(_soundInstancesMutex);
158 return _soundInstances.front();
161 void
162 EmbedSound::getPlayingInstances(std::vector<InputStream*>& to) const
164 boost::mutex::scoped_lock lock(_soundInstancesMutex);
165 for (Instances::const_iterator i=_soundInstances.begin(), e=_soundInstances.end();
166 i!=e; ++i)
168 to.push_back(*i);
172 } // gnash.sound namespace
173 } // namespace gnash