Introduced osg::AudioStream class to help manage audio streams coming in from movie reading plugins

This commit is contained in:
Robert Osfield
2009-02-27 20:16:08 +00:00
parent 116655c7c5
commit 4ebd316130
13 changed files with 146 additions and 88 deletions

80
include/osg/AudioStream Normal file
View File

@@ -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 <osg/Image>
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<const AudioStream*>(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

View File

@@ -15,6 +15,7 @@
#define OSG_IMAGESTREAM 1
#include <osg/Image>
#include <osg/AudioStream>
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<osg::AudioStream> > 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

30
src/osg/AudioStream.cpp Normal file
View File

@@ -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 <osg/AudioStream>
using namespace osg;
AudioSinkInterface::AudioSinkInterface() :
_delay(0.0)
{
}
AudioStream::AudioStream()
{
}
AudioStream::AudioStream(const AudioStream& audio,const CopyOp& copyop):
osg::Object(audio, copyop)
{
}

View File

@@ -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

View File

@@ -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)
{
}

View File

@@ -1,40 +0,0 @@
#ifndef OSG_AUDIOSINKINTERFACE_H
#define OSG_AUDIOSINKINTERFACE_H
#include <osg/Object>
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

View File

@@ -20,7 +20,6 @@ SET(TARGET_SRC
)
SET(TARGET_H
AudioSinkInterface.hpp
BoundedMessageQueue.hpp
FFmpegClocks.hpp
FFmpegDecoderAudio.hpp

View File

@@ -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;

View File

@@ -6,13 +6,14 @@
#include "FFmpegClocks.hpp"
#include "FFmpegPacket.hpp"
#include "FFmpegSampleFormat.hpp"
#include "AudioSinkInterface.hpp"
#include <osg/AudioStream>
#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;
}

View File

@@ -2,9 +2,11 @@
#ifndef HEADER_GUARD_FFMPEG_HEADERS_H
#define HEADER_GUARD_FFMPEG_HEADERS_H
extern "C"
{
#define __STDC_CONSTANT_MACROS
#include <stdint.h>
#include <avcodec.h>
#include <avformat.h>
}

View File

@@ -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();
}

View File

@@ -7,10 +7,6 @@
#include <OpenThreads/Condition>
#include <OpenThreads/Thread>
#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:

View File

@@ -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