Make automated FSCommand invocation tests show player-side output.
[gnash.git] / libsound / LiveSound.h
blob82df6a5a3416fc53e390145698254fdecabf4162
1 // LiveSound.h: - base class for embedded sound handling, 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.
10 //
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
20 #ifndef SOUND_LIVESOUND_H
21 #define SOUND_LIVESOUND_H
23 #include <memory>
24 #include <cassert>
25 #include <cstdint> // For C99 int types
27 #include "InputStream.h"
28 #include "AudioDecoder.h"
29 #include "SimpleBuffer.h"
31 // Forward declarations
32 namespace gnash {
33 namespace media {
34 class MediaHandler;
35 class SoundInfo;
39 namespace gnash {
40 namespace sound {
42 /// Instance of a defined %sound (LiveSoundData)
44 /// This class contains a pointer to the LiveSoundData used for playing
45 /// and a SimpleBuffer to use when decoding is needed.
46 class LiveSound : public InputStream
48 protected:
50 /// Create an embedded %sound instance
52 /// @param mh The MediaHandler to use for on-demand decoding
53 /// @param inPoint Offset in output samples this instance should start
54 /// playing from. These are post-resampling samples (44100
55 /// for one second of samples).
56 /// @param info The media::SoundInfo for this sound.
57 LiveSound(media::MediaHandler& mh, const media::SoundInfo& info,
58 size_t inPoint);
60 // Pointer handling and checking functions
61 const std::int16_t* getDecodedData(unsigned long int pos) const {
62 assert(pos < _decodedData.size());
63 return reinterpret_cast<const std::int16_t*>(
64 _decodedData.data() + pos);
67 /// Called when more decoded sound data is required.
69 /// This will be called whenever no more decoded data is available
70 /// but decoding is not complete.
71 virtual bool moreData() = 0;
73 /// True if there is no more data ever.
75 /// The InputStream will be disconnected when this is true.
76 virtual bool eof() const = 0;
78 /// Start from the beginning again.
79 void restart() {
80 _playbackPosition = _inPoint;
81 _samplesFetched = 0;
84 /// How many samples have been fetched since the beginning
86 /// Note that this is reset on each loop.
87 unsigned int samplesFetched() const {
88 return _samplesFetched;
91 size_t playbackPosition() const {
92 return _playbackPosition;
95 media::AudioDecoder& decoder() const {
96 return *_decoder;
99 void appendDecodedData(std::uint8_t* data, unsigned int size) {
100 _decodedData.append(data, size);
101 delete [] data;
104 /// Return number of already-decoded samples available
105 /// from playback position on
106 unsigned int decodedSamplesAhead() const {
108 const unsigned int dds = _decodedData.size();
109 if (dds <= _playbackPosition) return 0;
111 size_t bytesAhead = dds - _playbackPosition;
112 bytesAhead = checkEarlierEnd(bytesAhead, _playbackPosition);
114 assert(!(bytesAhead % 2));
116 const unsigned int samplesAhead = bytesAhead / 2;
117 return samplesAhead;
120 private:
122 /// Check if the sound data ends earlier than expected.
124 /// This is a way to deal with the outpoint in EmbedSoundInst, but isn't
125 /// very tidy.
126 virtual size_t checkEarlierEnd(size_t left, size_t) const {
127 return left;
130 // See dox in sound_handler.h (InputStream)
131 unsigned int fetchSamples(std::int16_t* to, unsigned int nSamples);
133 void createDecoder(media::MediaHandler& mediaHandler,
134 const media::SoundInfo& info);
136 virtual bool decodingCompleted() const = 0;
138 const size_t _inPoint;
140 /// Current playback position in the decoded stream
141 size_t _playbackPosition;
143 /// Number of samples fetched so far.
144 unsigned long _samplesFetched;
146 std::unique_ptr<media::AudioDecoder> _decoder;
148 /// The decoded buffer
149 SimpleBuffer _decodedData;
154 } // gnash.sound namespace
155 } // namespace gnash
157 #endif // SOUND_EMBEDSOUNDINST_H