Ported across from using boost pointers, and prepped for integration of audio interface into core OSG

This commit is contained in:
Robert Osfield 2009-02-27 17:00:28 +00:00
parent eef4801ba7
commit 116655c7c5
9 changed files with 96 additions and 16 deletions

View File

@ -1,12 +1,12 @@
#ifndef HEADER_GUARD_OSGFFMPEG_AUDIO_SINK_INTERFACE_H
#define HEADER_GUARD_OSGFFMPEG_AUDIO_SINK_INTERFACE_H
#ifndef OSG_AUDIOSINKINTERFACE_H
#define OSG_AUDIOSINKINTERFACE_H
#include <osg/Object>
namespace osgFFmpeg
namespace osg
{
class AudioSinkInterface : public osg::Object

View File

@ -46,7 +46,7 @@ bool FFmpegDecoder::open(const std::string & filename)
if (av_open_input_file(&p_format_context, filename.c_str(), 0, 0, 0) != 0)
throw std::runtime_error("av_open_input_file() failed");
m_format_context.reset(p_format_context, av_close_input_file);
m_format_context.reset(p_format_context);
// Retrieve stream info
if (av_find_stream_info(p_format_context) < 0)

View File

@ -2,15 +2,58 @@
#ifndef HEADER_GUARD_OSGFFMPEG_FFMPEG_DECODER_H
#define HEADER_GUARD_OSGFFMPEG_FFMPEG_DECODER_H
#include <boost/shared_ptr.hpp>
#include "FFmpegDecoderAudio.hpp"
#include "FFmpegDecoderVideo.hpp"
#include <osg/Notify>
namespace osgFFmpeg {
class FormatContextPtr
{
public:
typedef AVFormatContext T;
explicit FormatContextPtr() : _ptr(0) {}
explicit FormatContextPtr(T* ptr) : _ptr(ptr) {}
~FormatContextPtr()
{
cleanup();
}
T* get() { return _ptr; }
T * operator-> () const // never throws
{
return _ptr;
}
void reset(T* ptr)
{
if (ptr==_ptr) return;
cleanup();
_ptr = ptr;
}
void cleanup()
{
if (_ptr)
{
osg::notify(osg::NOTICE)<<"Calling av_close_input_file("<<_ptr<<")"<<std::endl;
av_close_input_file(_ptr);
}
_ptr = 0;
}
protected:
T* _ptr;
};
class FFmpegDecoder
@ -45,7 +88,6 @@ protected:
REWINDING
};
typedef boost::shared_ptr<AVFormatContext> FormatContextPtr;
typedef BoundedMessageQueue<FFmpegPacket> PacketQueue;
void findAudioStream();

View File

@ -106,7 +106,7 @@ void FFmpegDecoderAudio::run()
void FFmpegDecoderAudio::setAudioSink(osg::ref_ptr<AudioSinkInterface> audio_sink)
void FFmpegDecoderAudio::setAudioSink(osg::ref_ptr<osg::AudioSinkInterface> audio_sink)
{
// The FFmpegDecoderAudio object takes the responsability of destroying the audio_sink.
m_audio_sink = audio_sink;

View File

@ -30,7 +30,7 @@ public:
void open(AVStream * stream);
virtual void run();
void setAudioSink(osg::ref_ptr<AudioSinkInterface> audio_sink);
void setAudioSink(osg::ref_ptr<osg::AudioSinkInterface> audio_sink);
void fillBuffer(void * buffer, size_t size);
bool validContext() const;
@ -41,7 +41,7 @@ public:
private:
//typedef boost::shared_ptr<AVFrame> FramePtr;
typedef osg::ref_ptr<AudioSinkInterface> SinkPtr;
typedef osg::ref_ptr<osg::AudioSinkInterface> SinkPtr;
typedef std::vector<uint8_t> Buffer;
void decodeLoop();

View File

@ -72,10 +72,10 @@ void FFmpegDecoderVideo::open(AVStream * const stream)
throw std::runtime_error("avcodec_open() failed");
// Allocate video frame
m_frame.reset(avcodec_alloc_frame(), av_free);
m_frame.reset(avcodec_alloc_frame());
// Allocate converted RGB frame
m_frame_rgba.reset(avcodec_alloc_frame(), av_free);
m_frame_rgba.reset(avcodec_alloc_frame());
m_buffer_rgba.resize(avpicture_get_size(PIX_FMT_RGB32, width(), height()));
m_buffer_rgba_public.resize(m_buffer_rgba.size());

View File

@ -13,7 +13,46 @@
namespace osgFFmpeg {
class FramePtr
{
public:
typedef AVFrame T;
explicit FramePtr() : _ptr(0) {}
explicit FramePtr(T* ptr) : _ptr(ptr) {}
~FramePtr()
{
cleanup();
}
T* get() { return _ptr; }
T * operator-> () const // never throws
{
return _ptr;
}
void reset(T* ptr)
{
if (ptr==_ptr) return;
cleanup();
_ptr = ptr;
}
void cleanup()
{
if (_ptr) av_free(_ptr);
_ptr = 0;
}
protected:
T* _ptr;
};
class FFmpegDecoderVideo : public OpenThreads::Thread
{
@ -40,7 +79,6 @@ public:
private:
typedef boost::shared_ptr<AVFrame> FramePtr;
typedef std::vector<uint8_t> Buffer;
void decodeLoop();

View File

@ -121,13 +121,12 @@ void FFmpegImageStream::quit(bool waitForThreadToExit)
void FFmpegImageStream::setAudioSink(osg::ref_ptr<AudioSinkInterface> audio_sink)
void FFmpegImageStream::setAudioSink(osg::AudioSinkInterface* audio_sink)
{
m_decoder->audio_decoder().setAudioSink(audio_sink);
}
void FFmpegImageStream::fillAudioBuffer(void * const buffer, const size_t size)
{
m_decoder->audio_decoder().fillBuffer(buffer, size);

View File

@ -53,7 +53,8 @@ namespace osgFFmpeg
virtual void rewind();
virtual void quit(bool waitForThreadToExit = true);
void setAudioSink(osg::ref_ptr<AudioSinkInterface> audio_sink);
virtual void setAudioSink(osg::AudioSinkInterface* audio_sink);
void fillAudioBuffer(void * const buffer, const size_t size);
double duration() const;