1 // StreamingSoundData.h - embedded sound definition, for gnash
3 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
4 // Free Software Foundation, Inc
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
20 #ifndef SOUND_STREAMING_SOUND_DATA_H
21 #define SOUND_STREAMING_SOUND_DATA_H
29 #include "SoundInfo.h"
31 // Forward declarations
46 /// Definition of an embedded sound
47 class StreamingSoundData
51 /// Container for the active instances of this sounds being played
53 /// NOTE: This class does NOT own the active sounds
54 typedef std::list
<InputStream
*> Instances
;
56 /// Construct a sound with given data, info and volume.
58 /// @param info encoding info
59 /// @param nVolume initial volume (0..100).
60 StreamingSoundData(media::SoundInfo info
, int nVolume
);
62 ~StreamingSoundData();
64 /// Append a sound data block
66 /// @param data Undecoded sound data. Must be appropriately
67 /// padded (see MediaHandler::getInputPaddingBytes())
68 /// @param sampleCount The number of samples when decoded.
69 /// @param seekSamples Where to start playing from at a particular frame.
70 size_t append(SimpleBuffer data
, size_t sampleCount
,
73 /// Do we have any data?
75 return _buffers
.empty();
78 const SimpleBuffer
& getBlock(size_t index
) const {
79 return _buffers
[index
];
82 size_t getSampleCount(size_t index
) const {
83 return _blockData
[index
].sampleCount
;
86 size_t getSeekSamples(size_t index
) const {
87 return _blockData
[index
].seekSamples
;
90 size_t blockCount() const {
91 return _buffers
.size();
94 size_t playingBlock() const;
96 /// Are there known playing instances of this sound ?
98 /// Locks _soundInstancesMutex
100 bool isPlaying() const;
102 /// Return number of playing instances of this sound
104 /// Locks _soundInstancesMutex
106 size_t numPlayingInstances() const;
108 /// Append to the given vector all playing instances of this sound def
109 void getPlayingInstances(std::vector
<InputStream
*>& to
) const;
111 /// Return the first created instance of this sound
113 /// Locks _soundInstancesMutex
115 InputStream
* firstPlayingInstance() const;
117 /// Create an instance of this sound
119 /// The returned instance ownership is transferred
121 /// @param mh The MediaHandler to use for on-demand decoding
122 /// @param blockOffset Block number in the immutable (encoded) data
123 /// this instance should start decoding.
124 /// This refers to a specific StreamSoundBlock.
125 /// @see gnash::swf::StreamSoundBlockTag
126 /// Locks the _soundInstancesMutex when pushing to it
127 std::unique_ptr
<StreamingSound
> createInstance(media::MediaHandler
& mh
,
128 unsigned long blockOffset
);
130 /// Drop all active sounds
132 /// Locks _soundInstancesMutex
134 void clearInstances();
136 /// Drop an active sound (by iterator)
138 /// Does *NOT* lock the _soundInstancesMutex
140 /// @return iterator after the one being erased
142 Instances::iterator
eraseActiveSound(Instances::iterator i
);
144 /// Drop an active sound (by pointer)
146 /// @param inst The active sound instance to unregister
148 /// This is intended to be called by StreamingSoundDataInst
149 /// destructor, which may be called by a separate thread
150 /// so MUST be thread-safe
152 /// Does lock the _soundInstancesMutex
153 void eraseActiveSound(InputStream
* inst
);
155 /// Object holding information about the sound
156 media::SoundInfo soundinfo
;
158 /// Volume for AS-sounds, range: 0-100.
159 /// It's the SWF range that is represented here.
166 BlockData(size_t count
, int seek
)
176 /// Playing instances of this sound definition
178 /// Multithread access to this member is protected
179 /// by the _soundInstancesMutex mutex
180 Instances _soundInstances
;
182 /// Mutex protecting access to _soundInstances
183 mutable std::mutex _soundInstancesMutex
;
185 std::vector
<SimpleBuffer
> _buffers
;
187 std::vector
<BlockData
> _blockData
;
190 } // gnash.sound namespace
193 #endif // SOUND_EMBEDSOUND_H