Gnash  0.8.11dev
LiveSound.h
Go to the documentation of this file.
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
19 
20 #ifndef SOUND_LIVESOUND_H
21 #define SOUND_LIVESOUND_H
22 
23 #include <memory>
24 #include <cassert>
25 #include <cstdint> // For C99 int types
26 
27 #include "InputStream.h"
28 #include "AudioDecoder.h"
29 #include "SimpleBuffer.h"
30 
31 // Forward declarations
32 namespace gnash {
33  namespace media {
34  class MediaHandler;
35  class SoundInfo;
36  }
37 }
38 
39 namespace gnash {
40 namespace sound {
41 
43 //
46 class LiveSound : public InputStream
47 {
48 protected:
49 
51  //
58  size_t inPoint);
59 
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);
65  }
66 
68  //
71  virtual bool moreData() = 0;
72 
74  //
76  virtual bool eof() const = 0;
77 
79  void restart() {
80  _playbackPosition = _inPoint;
81  _samplesFetched = 0;
82  }
83 
85  //
87  unsigned int samplesFetched() const {
88  return _samplesFetched;
89  }
90 
91  size_t playbackPosition() const {
92  return _playbackPosition;
93  }
94 
96  return *_decoder;
97  }
98 
99  void appendDecodedData(std::uint8_t* data, unsigned int size) {
100  _decodedData.append(data, size);
101  delete [] data;
102  }
103 
106  unsigned int decodedSamplesAhead() const {
107 
108  const unsigned int dds = _decodedData.size();
109  if (dds <= _playbackPosition) return 0;
110 
111  size_t bytesAhead = dds - _playbackPosition;
112  bytesAhead = checkEarlierEnd(bytesAhead, _playbackPosition);
113 
114  assert(!(bytesAhead % 2));
115 
116  const unsigned int samplesAhead = bytesAhead / 2;
117  return samplesAhead;
118  }
119 
120 private:
121 
123  //
126  virtual size_t checkEarlierEnd(size_t left, size_t) const {
127  return left;
128  }
129 
130  // See dox in sound_handler.h (InputStream)
131  unsigned int fetchSamples(std::int16_t* to, unsigned int nSamples);
132 
133  void createDecoder(media::MediaHandler& mediaHandler,
134  const media::SoundInfo& info);
135 
136  virtual bool decodingCompleted() const = 0;
137 
138  const size_t _inPoint;
139 
141  size_t _playbackPosition;
142 
144  unsigned long _samplesFetched;
145 
146  std::unique_ptr<media::AudioDecoder> _decoder;
147 
149  SimpleBuffer _decodedData;
150 
151 };
152 
153 
154 } // gnash.sound namespace
155 } // namespace gnash
156 
157 #endif // SOUND_EMBEDSOUNDINST_H
A sound input stream.
Definition: InputStream.h:47
Class containing information about an embedded sound definition.
Definition: SoundInfo.h:34
media::AudioDecoder & decoder() const
Definition: LiveSound.h:95
Instance of a defined sound (LiveSoundData)
Definition: LiveSound.h:46
SimpleBuffer data
Definition: LocalConnection_as.cpp:151
Anonymous namespace for callbacks, local functions, event handlers etc.
Definition: dbus_ext.cpp:40
unsigned int decodedSamplesAhead() const
Definition: LiveSound.h:106
The MediaHandler class acts as a factory to provide parser and decoders.
Definition: MediaHandler.h:69
size_t playbackPosition() const
Definition: LiveSound.h:91
void restart()
Start from the beginning again.
Definition: LiveSound.h:79
void appendDecodedData(std::uint8_t *data, unsigned int size)
Definition: LiveSound.h:99
unsigned int samplesFetched() const
How many samples have been fetched since the beginning.
Definition: LiveSound.h:87
A simple buffer of bytes.
Definition: SimpleBuffer.h:38
Audio decoding base class.
Definition: AudioDecoder.h:36
const std::int16_t * getDecodedData(unsigned long int pos) const
Definition: LiveSound.h:61