1 // EmbedSound.cpp - embedded sound definition, for gnash
3 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Free Software
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.
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
32 //#include <boost/scoped_array.hpp>
33 #include <boost/cstdint.hpp> // For C99 int types
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
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...
57 EmbedSound::EmbedSound(std::auto_ptr
<SimpleBuffer
> data
,
58 std::auto_ptr
<media::SoundInfo
> info
, int nVolume
, size_t paddingBytes
)
63 _paddingBytes(paddingBytes
)
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
);
75 _buf
.reset(new SimpleBuffer());
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
,
97 unsigned int outPoint
,
98 const SoundEnvelopes
* envelopes
,
99 unsigned int loopCount
)
101 std::auto_ptr
<EmbedSoundInst
> ret ( new EmbedSoundInst(
108 boost::mutex::scoped_lock
lock(_soundInstancesMutex
);
110 // Push the sound onto the playing sounds container.
111 _soundInstances
.push_back(ret
.get());
116 EmbedSound::~EmbedSound()
122 EmbedSound::eraseActiveSound(EmbedSoundInst
* inst
)
124 boost::mutex::scoped_lock
lock(_soundInstancesMutex
);
126 Instances::iterator it
= std::find(
127 _soundInstances
.begin(),
128 _soundInstances
.end(),
131 if ( it
== _soundInstances
.end() )
133 log_error("EmbedSound::eraseActiveSound: instance %p not found!", inst
);
137 eraseActiveSound(it
);
141 EmbedSound::isPlaying() const
143 boost::mutex::scoped_lock
lock(_soundInstancesMutex
);
144 return !_soundInstances
.empty();
148 EmbedSound::numPlayingInstances() const
150 boost::mutex::scoped_lock
lock(_soundInstancesMutex
);
151 return _soundInstances
.size();
155 EmbedSound::firstPlayingInstance() const
157 boost::mutex::scoped_lock
lock(_soundInstancesMutex
);
158 return _soundInstances
.front();
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();
172 } // gnash.sound namespace