Gnash  0.8.10
LiveSound.h
Go to the documentation of this file.
00001 // LiveSound.h: - base class for embedded sound handling, for gnash
00002 // 
00003 //   Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
00004 //   Free Software Foundation, Inc
00005 // 
00006 // This program is free software; you can redistribute it and/or modify
00007 // it under the terms of the GNU General Public License as published by
00008 // the Free Software Foundation; either version 3 of the License, or
00009 // (at your option) any later version.
00010 // 
00011 // This program is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 // 
00016 // You should have received a copy of the GNU General Public License
00017 // along with this program; if not, write to the Free Software
00018 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00019 
00020 #ifndef SOUND_LIVESOUND_H
00021 #define SOUND_LIVESOUND_H
00022 
00023 #include <boost/scoped_ptr.hpp>
00024 #include <cassert>
00025 #include <boost/cstdint.hpp> // For C99 int types
00026 
00027 #include "InputStream.h" 
00028 #include "AudioDecoder.h" 
00029 #include "SimpleBuffer.h" 
00030 #include "SoundInfo.h"
00031 
00032 // Forward declarations
00033 namespace gnash {
00034     namespace media {
00035         class MediaHandler;
00036     }
00037 }
00038 
00039 namespace gnash {
00040 namespace sound {
00041 
00043 //
00046 class LiveSound : public InputStream
00047 {
00048 protected:
00049 
00051     //
00057     LiveSound(media::MediaHandler& mh, const media::SoundInfo& info,
00058             size_t inPoint);
00059 
00060     // Pointer handling and checking functions
00061     const boost::int16_t* getDecodedData(unsigned long int pos) const {
00062         assert(pos < _decodedData.size());
00063         return reinterpret_cast<const boost::int16_t*>(
00064                 _decodedData.data() + pos);
00065     }
00066 
00068     //
00071     virtual bool moreData() = 0;
00072 
00074     //
00076     virtual bool eof() const = 0;
00077 
00079     void restart() {
00080         _playbackPosition = _inPoint;
00081         _samplesFetched = 0;
00082     }
00083 
00085     //
00087     unsigned int samplesFetched() const {
00088         return _samplesFetched;
00089     }
00090 
00091     size_t playbackPosition() const {
00092         return _playbackPosition;
00093     }
00094 
00095     media::AudioDecoder& decoder() const {
00096         return *_decoder;
00097     }
00098 
00099     void appendDecodedData(boost::uint8_t* data, unsigned int size) {
00100         _decodedData.append(data, size);
00101         delete [] data;
00102     }
00103 
00106     unsigned int decodedSamplesAhead() const {
00107 
00108         const unsigned int dds = _decodedData.size();
00109         if (dds <= _playbackPosition) return 0; 
00110 
00111         size_t bytesAhead = dds - _playbackPosition;
00112         bytesAhead = checkEarlierEnd(bytesAhead, _playbackPosition);
00113 
00114         assert(!(bytesAhead % 2));
00115 
00116         const unsigned int samplesAhead = bytesAhead / 2;
00117         return samplesAhead;
00118     }
00119 
00120 private:
00121 
00123     //
00126     virtual size_t checkEarlierEnd(size_t left, size_t) const {
00127         return left;
00128     }
00129 
00130     // See dox in sound_handler.h (InputStream)
00131     unsigned int fetchSamples(boost::int16_t* to, unsigned int nSamples);
00132 
00133     void createDecoder(media::MediaHandler& mediaHandler,
00134             const media::SoundInfo& info);
00135 
00136     virtual bool decodingCompleted() const = 0;
00137 
00138     const size_t _inPoint;
00139 
00141     size_t _playbackPosition;
00142 
00144     unsigned long _samplesFetched;
00145 
00146     boost::scoped_ptr<media::AudioDecoder> _decoder;
00147 
00149     SimpleBuffer _decodedData;
00150 
00151 };
00152 
00153 
00154 } // gnash.sound namespace 
00155 } // namespace gnash
00156 
00157 #endif // SOUND_EMBEDSOUNDINST_H