[osg-users] ffmpeg plugin streaming video proof of concept

Robert Osfield robert.osfield at gmail.com
Thu Mar 5 03:16:23 PST 2009


Hi All,

We now have the beginnings of the ffmpeg checked into svn/trunk, and
my hope is that this might one day get capable enough to replace the
existing xine-lib plugin and quicktime plugins as the source of video
support.  Just having one video backend would help with maintenance as
well enabling a full feature set across all platforms, but this will
only be possible if we can get the new ffmpeg working well across all
platforms with all the original functionality.  One item that the
quicktime plugin has is stream support under Windows and OSX, so...
this is also one of the items that needs to be added if possible.

The "if possible" question is something that I wanted to explore, and
various code fragments on the web suggested that it was possible, so
I've hacked together a proof of concept extension to the ffmpeg so
that it attempts to open a video device as input.  This didn't
actually take too long to try out, so under linux you can now do:

   osgmovie -e ffmpeg /dev/video0

Which if you have a webcam or similar device on /dev/video0 it'll open
it up using video4linux2 and stream it, full screen.

The proof of concept is only a dozen lines of code so far, with
defaults chosen that work fine for my hardware/OS setup.  For other
OS's the settings will certainly be different, and it's not entirely
clear to me that ffmpeg does support such device streaming across all
platforms... More investigation will be required.  Please dive in to
ffmpeg docs/online discussions to see if you can spot the approach to
take under Windows + OSX etc.

For a final incarnation of live video streaming support we'll need to
add controls for the various device parameters, either via parsing of
ReaderWriter::Options strings, or extended Options to include
appropriate device parameters.   We will probably need some kind of
scheme for querying the device options available too.  I really don't
have much of clue how we might go about all this yet as it's very
early days for me delving into this side of things.

For those interested in helping out with development of the streaming
support have a look at the code I just added to
src/osgPlugins/ffmpeg/FFmpegDecode.cpp, in particular the bool
FFmpegDecoder::open(const std::string & filename) method:

        if (filename.compare(0, 5, "/dev/")==0)
        {
            avdevice_register_all();

            osg::notify(osg::NOTICE)<<"Attempting to stream
"<<filename<<std::endl;

            AVFormatParameters formatParams;
            memset(&formatParams, 0, sizeof(AVFormatParameters));
            AVInputFormat *iformat;

            formatParams.channel = 0;
            formatParams.standard = 0;
            formatParams.width = 640;
            formatParams.height = 480;
            formatParams.time_base.num = 1;
            formatParams.time_base.den = 50;

            iformat = av_find_input_format("video4linux2");

            if (iformat)
            {
                osg::notify(osg::NOTICE)<<"Found input format"<<std::endl;
            }
            else
            {
                osg::notify(osg::NOTICE)<<"Failed to find
input_format"<<std::endl;
            }

            if (av_open_input_file(&p_format_context,
filename.c_str(), iformat, 0, &formatParams) != 0)
                throw std::runtime_error("av_open_input_file() failed");
        }
        else
        {
            if (av_open_input_file(&p_format_context,
filename.c_str(), 0, 0, 0) !=0 )
                throw std::runtime_error("av_open_input_file() failed");
        }

The ReaderWriter::readImage(..) method also catches the /dev/ filename
usage, so if you try this out for a non linux platform you'll need to
tweak the filename checking here as well.

Robert.



More information about the osg-users mailing list