From 4ebd31613093470344bd9f5a7b5f13b7158cdc9b Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Fri, 27 Feb 2009 20:16:08 +0000 Subject: [PATCH] Introduced osg::AudioStream class to help manage audio streams coming in from movie reading plugins --- include/osg/AudioStream | 80 ++++++++++++++++++++ include/osg/ImageStream | 9 +++ src/osg/AudioStream.cpp | 30 ++++++++ src/osg/CMakeLists.txt | 2 + src/osg/ImageStream.cpp | 3 +- src/osgPlugins/ffmpeg/AudioSinkInterface.hpp | 40 ---------- src/osgPlugins/ffmpeg/CMakeLists.txt | 1 - src/osgPlugins/ffmpeg/FFmpegDecoderAudio.cpp | 12 +-- src/osgPlugins/ffmpeg/FFmpegDecoderAudio.hpp | 11 +-- src/osgPlugins/ffmpeg/FFmpegHeaders.hpp | 2 + src/osgPlugins/ffmpeg/FFmpegImageStream.cpp | 4 +- src/osgPlugins/ffmpeg/FFmpegImageStream.hpp | 17 ++--- src/osgPlugins/ffmpeg/FFmpegSampleFormat.hpp | 23 ------ 13 files changed, 146 insertions(+), 88 deletions(-) create mode 100644 include/osg/AudioStream create mode 100644 src/osg/AudioStream.cpp delete mode 100644 src/osgPlugins/ffmpeg/AudioSinkInterface.hpp delete mode 100644 src/osgPlugins/ffmpeg/FFmpegSampleFormat.hpp diff --git a/include/osg/AudioStream b/include/osg/AudioStream new file mode 100644 index 000000000..35b1daf12 --- /dev/null +++ b/include/osg/AudioStream @@ -0,0 +1,80 @@ +/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield + * + * This library is open source and may be redistributed and/or modified under + * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or + * (at your option) any later version. The full license is in LICENSE file + * included with this distribution, and on the openscenegraph.org website. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * OpenSceneGraph Public License for more details. +*/ + +#ifndef OSG_AUDIOSTREAM +#define OSG_AUDIOSTREAM 1 + +#include + +namespace osg { + +/** Pure virtual AudioSinkInterface bass class that is used to connect the audio system with AudioStreams. */ +class OSG_EXPORT AudioSinkInterface : public osg::Object +{ +public: + + AudioSinkInterface(); + + virtual void startPlaying() = 0; + virtual bool playing() const = 0; + + virtual double getDelay() const { return _delay; } + virtual void setDelay(const double delay) { _delay = delay; } + + virtual const char * libraryName() const { return "osgFFmpeg"; } + virtual const char * className() const { return "AudioSinkInterface"; } + +private: + + virtual AudioSinkInterface * cloneType() const { return 0; } + virtual AudioSinkInterface * clone(const osg::CopyOp &) const { return 0; } + + double _delay; +}; + +/** Pure virtual AudioStream base class. Subclasses provide mechanism for reading/generating audio data*/ +class OSG_EXPORT AudioStream : public osg::Object +{ + public: + AudioStream(); + + /** Copy constructor using CopyOp to manage deep vs shallow copy. */ + AudioStream(const AudioStream& audio,const CopyOp& copyop=CopyOp::SHALLOW_COPY); + + virtual bool isSameKindAs(const Object* obj) const { return dynamic_cast(obj)!=0; } + virtual const char* libraryName() const { return "osg"; } + virtual const char* className() const { return "AudioStream"; } + + virtual void setAudioSink(osg::AudioSinkInterface* audio_sink) = 0; + + virtual void consumeAudioBuffer(void * const buffer, const size_t size) = 0; + + virtual bool audioStream() const = 0; + virtual int audioFrequency() const = 0; + virtual int audioNbChannels() const = 0; + + enum SampleFormat + { + SAMPLE_FORMAT_U8, + SAMPLE_FORMAT_S16, + SAMPLE_FORMAT_S24, + SAMPLE_FORMAT_S32, + SAMPLE_FORMAT_F32 + }; + + virtual SampleFormat audioSampleFormat() const = 0; +}; + +} // namespace + +#endif diff --git a/include/osg/ImageStream b/include/osg/ImageStream index ff3481d3a..d9449c707 100644 --- a/include/osg/ImageStream +++ b/include/osg/ImageStream @@ -15,6 +15,7 @@ #define OSG_IMAGESTREAM 1 #include +#include namespace osg { @@ -86,6 +87,12 @@ class OSG_EXPORT ImageStream : public Image virtual void setVolume(float) {} virtual float getVolume() const { return 0.0f; } + + + typedef std::vector< osg::ref_ptr > AudioStreams; + void setAudioStreams(const AudioStreams& asl) { _audioStreams = asl; } + AudioStreams& getAudioStreams() { return _audioStreams; } + const AudioStreams& getAudioStreams() const { return _audioStreams; } protected: @@ -95,6 +102,8 @@ class OSG_EXPORT ImageStream : public Image StreamStatus _status; LoopingMode _loopingMode; + + AudioStreams _audioStreams; }; } // namespace diff --git a/src/osg/AudioStream.cpp b/src/osg/AudioStream.cpp new file mode 100644 index 000000000..635ab0290 --- /dev/null +++ b/src/osg/AudioStream.cpp @@ -0,0 +1,30 @@ +/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield + * + * This library is open source and may be redistributed and/or modified under + * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or + * (at your option) any later version. The full license is in LICENSE file + * included with this distribution, and on the openscenegraph.org website. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * OpenSceneGraph Public License for more details. +*/ + +#include + +using namespace osg; + +AudioSinkInterface::AudioSinkInterface() : + _delay(0.0) +{ +} + +AudioStream::AudioStream() +{ +} + +AudioStream::AudioStream(const AudioStream& audio,const CopyOp& copyop): + osg::Object(audio, copyop) +{ +} diff --git a/src/osg/CMakeLists.txt b/src/osg/CMakeLists.txt index 38073dbd3..1a16f4a65 100644 --- a/src/osg/CMakeLists.txt +++ b/src/osg/CMakeLists.txt @@ -25,6 +25,7 @@ SET(LIB_PUBLIC_HEADERS ${HEADER_PATH}/ApplicationUsage ${HEADER_PATH}/ArgumentParser ${HEADER_PATH}/Array + ${HEADER_PATH}/AudioStream ${HEADER_PATH}/AutoTransform ${HEADER_PATH}/Billboard ${HEADER_PATH}/BlendColor @@ -193,6 +194,7 @@ ADD_LIBRARY(${LIB_NAME} ApplicationUsage.cpp ArgumentParser.cpp Array.cpp + AudioStream.cpp AutoTransform.cpp Billboard.cpp BlendColor.cpp diff --git a/src/osg/ImageStream.cpp b/src/osg/ImageStream.cpp index c5c67a501..6674682dd 100644 --- a/src/osg/ImageStream.cpp +++ b/src/osg/ImageStream.cpp @@ -32,7 +32,8 @@ ImageStream::ImageStream(): ImageStream::ImageStream(const ImageStream& image,const CopyOp& copyop): Image(image,copyop), _status(image._status), - _loopingMode(image._loopingMode) + _loopingMode(image._loopingMode), + _audioStreams(image._audioStreams) { } diff --git a/src/osgPlugins/ffmpeg/AudioSinkInterface.hpp b/src/osgPlugins/ffmpeg/AudioSinkInterface.hpp deleted file mode 100644 index 8279fd27a..000000000 --- a/src/osgPlugins/ffmpeg/AudioSinkInterface.hpp +++ /dev/null @@ -1,40 +0,0 @@ - -#ifndef OSG_AUDIOSINKINTERFACE_H -#define OSG_AUDIOSINKINTERFACE_H - -#include - - - -namespace osg -{ - - class AudioSinkInterface : public osg::Object - { - public: - - AudioSinkInterface() : - m_delay(0.0) { } - - virtual void startPlaying() = 0; - virtual bool playing() const = 0; - - virtual double getDelay() const { return m_delay; } - virtual void setDelay(const double delay) { m_delay = delay; } - - virtual const char * libraryName() const { return "osgFFmpeg"; } - virtual const char * className() const { return "AudioSinkInterface"; } - - private: - - virtual AudioSinkInterface * cloneType() const { return 0; } - virtual AudioSinkInterface * clone(const osg::CopyOp &) const { return 0; } - - double m_delay; - }; - -} - - - -#endif // HEADER_GUARD_OSGFFMPEG_AUDIO_SINK_INTERFACE_H diff --git a/src/osgPlugins/ffmpeg/CMakeLists.txt b/src/osgPlugins/ffmpeg/CMakeLists.txt index 82265ec34..9e31d4caf 100644 --- a/src/osgPlugins/ffmpeg/CMakeLists.txt +++ b/src/osgPlugins/ffmpeg/CMakeLists.txt @@ -20,7 +20,6 @@ SET(TARGET_SRC ) SET(TARGET_H - AudioSinkInterface.hpp BoundedMessageQueue.hpp FFmpegClocks.hpp FFmpegDecoderAudio.hpp diff --git a/src/osgPlugins/ffmpeg/FFmpegDecoderAudio.cpp b/src/osgPlugins/ffmpeg/FFmpegDecoderAudio.cpp index b5804deac..5fb92bf48 100644 --- a/src/osgPlugins/ffmpeg/FFmpegDecoderAudio.cpp +++ b/src/osgPlugins/ffmpeg/FFmpegDecoderAudio.cpp @@ -56,7 +56,7 @@ void FFmpegDecoderAudio::open(AVStream * const stream) m_frequency = m_context->sample_rate; m_nb_channels = m_context->channels; - m_sample_format = FFmpegSampleFormat(m_context->sample_fmt); + m_sample_format = osg::AudioStream::SampleFormat(m_context->sample_fmt); // Check stream sanity if (m_context->codec_id == CODEC_ID_NONE) @@ -199,23 +199,23 @@ void FFmpegDecoderAudio::adjustBufferEndTps(const size_t buffer_size) switch (sampleFormat()) { - case SAMPLE_FORMAT_U8: + case osg::AudioStream::SAMPLE_FORMAT_U8: sample_size *= 1; break; - case SAMPLE_FORMAT_S16: + case osg::AudioStream::SAMPLE_FORMAT_S16: sample_size *= 2; break; - case SAMPLE_FORMAT_S24: + case osg::AudioStream::SAMPLE_FORMAT_S24: sample_size *= 3; break; - case SAMPLE_FORMAT_S32: + case osg::AudioStream::SAMPLE_FORMAT_S32: sample_size *= 4; break; - case SAMPLE_FORMAT_F32: + case osg::AudioStream::SAMPLE_FORMAT_F32: sample_size *= 4; break; diff --git a/src/osgPlugins/ffmpeg/FFmpegDecoderAudio.hpp b/src/osgPlugins/ffmpeg/FFmpegDecoderAudio.hpp index 47d9a4ae8..52f325e36 100644 --- a/src/osgPlugins/ffmpeg/FFmpegDecoderAudio.hpp +++ b/src/osgPlugins/ffmpeg/FFmpegDecoderAudio.hpp @@ -6,13 +6,14 @@ #include "FFmpegClocks.hpp" #include "FFmpegPacket.hpp" -#include "FFmpegSampleFormat.hpp" -#include "AudioSinkInterface.hpp" +#include + #include "BoundedMessageQueue.hpp" + namespace osgFFmpeg { @@ -36,7 +37,7 @@ public: bool validContext() const; int frequency() const; int nbChannels() const; - FFmpegSampleFormat sampleFormat() const; + osg::AudioStream::SampleFormat sampleFormat() const; private: @@ -63,7 +64,7 @@ private: int m_frequency; int m_nb_channels; - FFmpegSampleFormat m_sample_format; + osg::AudioStream::SampleFormat m_sample_format; SinkPtr m_audio_sink; @@ -93,7 +94,7 @@ inline int FFmpegDecoderAudio::nbChannels() const } -inline FFmpegSampleFormat FFmpegDecoderAudio::sampleFormat() const +inline osg::AudioStream::SampleFormat FFmpegDecoderAudio::sampleFormat() const { return m_sample_format; } diff --git a/src/osgPlugins/ffmpeg/FFmpegHeaders.hpp b/src/osgPlugins/ffmpeg/FFmpegHeaders.hpp index bc24c05c9..412eda91a 100644 --- a/src/osgPlugins/ffmpeg/FFmpegHeaders.hpp +++ b/src/osgPlugins/ffmpeg/FFmpegHeaders.hpp @@ -2,9 +2,11 @@ #ifndef HEADER_GUARD_FFMPEG_HEADERS_H #define HEADER_GUARD_FFMPEG_HEADERS_H + extern "C" { #define __STDC_CONSTANT_MACROS +#include #include #include } diff --git a/src/osgPlugins/ffmpeg/FFmpegImageStream.cpp b/src/osgPlugins/ffmpeg/FFmpegImageStream.cpp index 2ac08a328..e9b3ac03f 100644 --- a/src/osgPlugins/ffmpeg/FFmpegImageStream.cpp +++ b/src/osgPlugins/ffmpeg/FFmpegImageStream.cpp @@ -127,7 +127,7 @@ void FFmpegImageStream::setAudioSink(osg::AudioSinkInterface* audio_sink) } -void FFmpegImageStream::fillAudioBuffer(void * const buffer, const size_t size) +void FFmpegImageStream::consumeAudioBuffer(void * const buffer, const size_t size) { m_decoder->audio_decoder().fillBuffer(buffer, size); } @@ -183,7 +183,7 @@ int FFmpegImageStream::audioNbChannels() const -FFmpegSampleFormat FFmpegImageStream::audioSampleFormat() const +osg::AudioStream::SampleFormat FFmpegImageStream::audioSampleFormat() const { return m_decoder->audio_decoder().sampleFormat(); } diff --git a/src/osgPlugins/ffmpeg/FFmpegImageStream.hpp b/src/osgPlugins/ffmpeg/FFmpegImageStream.hpp index 5211cbfaf..89d1c96dd 100644 --- a/src/osgPlugins/ffmpeg/FFmpegImageStream.hpp +++ b/src/osgPlugins/ffmpeg/FFmpegImageStream.hpp @@ -7,10 +7,6 @@ #include #include -#include "AudioSinkInterface.hpp" -#include "FFmpegSampleFormat.hpp" - - #ifdef _WIN32 #if defined OSG_LIBRARY_STATIC @@ -18,7 +14,7 @@ #elif defined OSG_LIBRARY || defined osgFFmpeg_EXPORTS #define OSGFFMPEG_EXPORT_API __declspec(dllexport) #else - #define OSGFFMPEG_EXPORT_API __declspec(dllimport) + #define OSGFFMPEG_EXPORT_API __declspec(dllimport); #endif #else #define OSGFFMPEG_EXPORT_API @@ -55,7 +51,12 @@ namespace osgFFmpeg virtual void setAudioSink(osg::AudioSinkInterface* audio_sink); - void fillAudioBuffer(void * const buffer, const size_t size); + void consumeAudioBuffer(void * const buffer, const size_t size); + + bool audioStream() const; + int audioFrequency() const; + int audioNbChannels() const; + osg::AudioStream::SampleFormat audioSampleFormat() const; double duration() const; @@ -63,10 +64,6 @@ namespace osgFFmpeg double videoAspectRatio() const; double videoFrameRate() const; - bool audioStream() const; - int audioFrequency() const; - int audioNbChannels() const; - FFmpegSampleFormat audioSampleFormat() const; private: diff --git a/src/osgPlugins/ffmpeg/FFmpegSampleFormat.hpp b/src/osgPlugins/ffmpeg/FFmpegSampleFormat.hpp deleted file mode 100644 index 0dfe4dc05..000000000 --- a/src/osgPlugins/ffmpeg/FFmpegSampleFormat.hpp +++ /dev/null @@ -1,23 +0,0 @@ - -#ifndef HEADER_GUARD_OSGFFMPEG_FFMPEG_SAMPLE_FORMAT_H -#define HEADER_GUARD_OSGFFMPEG_FFMPEG_SAMPLE_FORMAT_H - - - -namespace osgFFmpeg -{ - - enum FFmpegSampleFormat - { - SAMPLE_FORMAT_U8, //= SAMPLE_FMT_U8, - SAMPLE_FORMAT_S16, //= SAMPLE_FMT_S16, - SAMPLE_FORMAT_S24, //= SAMPLE_FMT_S24, - SAMPLE_FORMAT_S32, //= SAMPLE_FMT_S32, - SAMPLE_FORMAT_F32 //= SAMPLE_FMT_FLT - }; - -} - - - -#endif // HEADER_GUARD_OSGFFMPEG_FFMPEG_SAMPLE_FORMAT_H