From Julen Garcia, "Here there is a small plugin I use to play video files. It is based on GStreamer http://gstreamer.freedesktop.org and I have used the FFmpeg plugin as inspiration."
From Robert Osfield, fixed handled of row widths so that they are padded to a 4 byte boundary as certain row widths were being rendered incorrectly. git-svn-id: http://svn.openscenegraph.org/osg/OpenSceneGraph/trunk@14604 16af8721-9629-0410-8352-f15c8da7e697
This commit is contained in:
@@ -200,6 +200,10 @@ IF(FFMPEG_FOUND AND OSG_CPP_EXCEPTIONS_AVAILABLE)
|
||||
ADD_SUBDIRECTORY(ffmpeg)
|
||||
ENDIF()
|
||||
|
||||
IF(GSTREAMER_FOUND AND GLIB_FOUND)
|
||||
ADD_SUBDIRECTORY(gstreamer)
|
||||
ENDIF()
|
||||
|
||||
IF(DIRECTSHOW_FOUND)
|
||||
ADD_SUBDIRECTORY(directshow)
|
||||
ENDIF()
|
||||
|
||||
24
src/osgPlugins/gstreamer/CMakeLists.txt
Normal file
24
src/osgPlugins/gstreamer/CMakeLists.txt
Normal file
@@ -0,0 +1,24 @@
|
||||
INCLUDE_DIRECTORIES(
|
||||
${GSTREAMER_INCLUDE_DIRS}
|
||||
${GLIB_INCLUDE_DIRS}
|
||||
)
|
||||
|
||||
SET(TARGET_EXTERNAL_LIBRARIES
|
||||
${GSTREAMER_LIBRARIES}
|
||||
${GSTREAMER_APP_LIBRARIES}
|
||||
${GSTREAMER_PBUTILS_LIBRARIES}
|
||||
${GLIB_LIBRARIES}
|
||||
${GLIB_GOBJECT_LIBRARIES}
|
||||
)
|
||||
|
||||
SET(TARGET_SRC
|
||||
GStreamerImageStream.cpp
|
||||
ReaderWriterGStreamer.cpp
|
||||
)
|
||||
|
||||
SET(TARGET_H
|
||||
GStreamerImageStream.hpp
|
||||
)
|
||||
|
||||
#### end var setup ###
|
||||
SETUP_PLUGIN(gstreamer)
|
||||
221
src/osgPlugins/gstreamer/GStreamerImageStream.cpp
Normal file
221
src/osgPlugins/gstreamer/GStreamerImageStream.cpp
Normal file
@@ -0,0 +1,221 @@
|
||||
#include "GStreamerImageStream.hpp"
|
||||
|
||||
namespace osgGStreamer {
|
||||
|
||||
GStreamerImageStream::GStreamerImageStream()
|
||||
{
|
||||
setOrigin(osg::Image::TOP_LEFT);
|
||||
|
||||
loop = g_main_loop_new(NULL, FALSE);
|
||||
}
|
||||
|
||||
GStreamerImageStream::GStreamerImageStream(const GStreamerImageStream & image, const osg::CopyOp & copyop) :
|
||||
osg::ImageStream(image, copyop)
|
||||
{
|
||||
// TODO: probably incorrect or incomplete
|
||||
}
|
||||
|
||||
GStreamerImageStream::~GStreamerImageStream()
|
||||
{
|
||||
OSG_INFO<<"Destructing GStreamerImageStream..."<<std::endl;
|
||||
|
||||
gst_element_set_state(pipeline, GST_STATE_NULL);
|
||||
gst_element_get_state(pipeline, NULL, NULL, GST_CLOCK_TIME_NONE); //wait until the state changed
|
||||
|
||||
g_main_loop_quit(loop);
|
||||
g_main_loop_unref(loop);
|
||||
|
||||
free(internal_buffer);
|
||||
}
|
||||
|
||||
bool GStreamerImageStream::open(const std::string &filename)
|
||||
{
|
||||
setFileName(filename);
|
||||
|
||||
GError *error = NULL;
|
||||
|
||||
// get stream info
|
||||
|
||||
bool has_audio_stream = false;
|
||||
|
||||
GstDiscoverer *item = gst_discoverer_new(1*GST_SECOND, &error);
|
||||
gchar *uri = g_filename_to_uri(filename.c_str(), NULL, NULL);
|
||||
|
||||
if( gst_uri_is_valid(uri) )
|
||||
{
|
||||
GstDiscovererInfo *info = gst_discoverer_discover_uri(item, uri, &error);
|
||||
GList *audio_list = gst_discoverer_info_get_audio_streams(info);
|
||||
|
||||
if( g_list_length(audio_list) > 0 )
|
||||
has_audio_stream = true;
|
||||
|
||||
gst_discoverer_info_unref(info);
|
||||
g_free(uri);
|
||||
}
|
||||
|
||||
// build pipeline
|
||||
|
||||
gchar *audio_pipe = "";
|
||||
|
||||
if( has_audio_stream )
|
||||
audio_pipe = "deco. ! queue ! audioconvert ! autoaudiosink";
|
||||
|
||||
gchar *string = g_strdup_printf("filesrc location=%s ! \
|
||||
decodebin name=deco \
|
||||
deco. ! queue ! videoconvert ! video/x-raw,format=RGB ! appsink name=sink emit-signals=true \
|
||||
%s", filename.c_str(), audio_pipe);
|
||||
|
||||
pipeline = gst_parse_launch(string, &error);
|
||||
|
||||
if( pipeline == NULL )
|
||||
{
|
||||
g_printerr("Error: %s\n", error->message);
|
||||
return false;
|
||||
}
|
||||
|
||||
g_free(string);
|
||||
g_error_free(error);
|
||||
|
||||
// bus
|
||||
|
||||
GstBus *bus = gst_pipeline_get_bus(GST_PIPELINE(pipeline));
|
||||
|
||||
gst_bus_add_watch(bus, (GstBusFunc)on_message, this);
|
||||
|
||||
gst_object_unref(bus);
|
||||
|
||||
|
||||
// sink
|
||||
|
||||
GstElement *sink = gst_bin_get_by_name(GST_BIN(pipeline), "sink");
|
||||
|
||||
g_signal_connect(sink, "new-sample", G_CALLBACK(on_new_sample), this);
|
||||
g_signal_connect(sink, "new-preroll", G_CALLBACK(on_new_preroll), this);
|
||||
|
||||
gst_object_unref(sink);
|
||||
|
||||
gst_element_set_state(pipeline, GST_STATE_PAUSED);
|
||||
gst_element_get_state(pipeline, NULL, NULL, GST_CLOCK_TIME_NONE); // wait until the state changed
|
||||
|
||||
//setPixelBufferObject(new osg::PixelBufferObject(this)); // can help with the performance
|
||||
setImage(width, height, 1, GL_RGB, GL_RGB, GL_UNSIGNED_BYTE, internal_buffer, osg::Image::NO_DELETE);
|
||||
|
||||
start();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
//** Controls **
|
||||
|
||||
void GStreamerImageStream::play()
|
||||
{
|
||||
gst_element_set_state(pipeline, GST_STATE_PLAYING);
|
||||
}
|
||||
|
||||
void GStreamerImageStream::pause()
|
||||
{
|
||||
gst_element_set_state(pipeline, GST_STATE_PAUSED);
|
||||
}
|
||||
|
||||
void GStreamerImageStream::rewind()
|
||||
{
|
||||
gst_element_seek_simple(pipeline, GST_FORMAT_TIME, GstSeekFlags(GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_KEY_UNIT), 0);
|
||||
}
|
||||
|
||||
void GStreamerImageStream::seek(double time)
|
||||
{
|
||||
gst_element_seek_simple(pipeline, GST_FORMAT_TIME, GstSeekFlags(GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_KEY_UNIT), time * GST_MSECOND);
|
||||
}
|
||||
|
||||
int GStreamerImageStream::s() const
|
||||
{
|
||||
return width;
|
||||
}
|
||||
|
||||
int GStreamerImageStream::t() const
|
||||
{
|
||||
return height;
|
||||
}
|
||||
|
||||
//** Callback implementations **
|
||||
|
||||
GstFlowReturn GStreamerImageStream::on_new_sample(GstAppSink *appsink, GStreamerImageStream *user_data)
|
||||
{
|
||||
// get the buffer from appsink
|
||||
|
||||
GstSample *sample = gst_app_sink_pull_sample(appsink);
|
||||
GstBuffer *buffer = gst_sample_get_buffer(sample);
|
||||
|
||||
// upload data
|
||||
|
||||
GstMapInfo info;
|
||||
gst_buffer_map(buffer, &info, GST_MAP_READ);
|
||||
gst_buffer_extract(buffer, 0, user_data->internal_buffer, info.size);
|
||||
|
||||
OSG_NOTICE<<"on_new_sample("<<(user_data->width)<<", "<<(user_data->height)<<")"<<std::endl;
|
||||
|
||||
user_data->setImage(user_data->width, user_data->height, 1, GL_RGB, GL_RGB, GL_UNSIGNED_BYTE, user_data->internal_buffer, osg::Image::NO_DELETE, 4);
|
||||
|
||||
// clean resources
|
||||
|
||||
gst_buffer_unmap(buffer, &info);
|
||||
gst_sample_unref(sample);
|
||||
|
||||
return GST_FLOW_OK;
|
||||
}
|
||||
|
||||
GstFlowReturn GStreamerImageStream::on_new_preroll(GstAppSink *appsink, GStreamerImageStream *user_data)
|
||||
{
|
||||
// get the sample from appsink
|
||||
|
||||
GstSample *sample = gst_app_sink_pull_preroll(appsink);
|
||||
|
||||
// get sample info
|
||||
|
||||
GstCaps *caps = gst_sample_get_caps(sample);
|
||||
GstStructure *structure = gst_caps_get_structure(caps, 0);
|
||||
|
||||
int width;
|
||||
int height;
|
||||
|
||||
gst_structure_get_int(structure, "width", &width);
|
||||
gst_structure_get_int(structure, "height", &height);
|
||||
|
||||
user_data->width = width;
|
||||
user_data->height = height;
|
||||
|
||||
int row_width = width*3;
|
||||
if ((row_width%4)!=0)
|
||||
{
|
||||
OSG_NOTICE<<"Rounding up row width from "<<row_width<<" to ";
|
||||
row_width += (4-(row_width%4));
|
||||
OSG_NOTICE<<row_width<<std::endl;;
|
||||
}
|
||||
|
||||
user_data->internal_buffer = (unsigned char*)malloc(sizeof(unsigned char)*row_width*height);
|
||||
|
||||
OSG_NOTICE<<"on_new_preroll("<<(user_data->width)<<", "<<(user_data->height)<<")"<<std::endl;
|
||||
|
||||
// clean resources
|
||||
|
||||
gst_sample_unref(sample);
|
||||
|
||||
return GST_FLOW_OK;
|
||||
}
|
||||
|
||||
gboolean GStreamerImageStream::on_message(GstBus *bus, GstMessage *message, GStreamerImageStream *user_data)
|
||||
{
|
||||
if( GST_MESSAGE_TYPE(message) == GST_MESSAGE_EOS)
|
||||
{
|
||||
user_data->rewind();
|
||||
}
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
void GStreamerImageStream::run()
|
||||
{
|
||||
g_main_loop_run(loop);
|
||||
}
|
||||
|
||||
} // namespace osgGStreamer
|
||||
53
src/osgPlugins/gstreamer/GStreamerImageStream.hpp
Normal file
53
src/osgPlugins/gstreamer/GStreamerImageStream.hpp
Normal file
@@ -0,0 +1,53 @@
|
||||
#ifndef HEADER_GUARD_OSGGSTREAMER_GSTREAMER_IMAGE_STREAM_H
|
||||
#define HEADER_GUARD_OSGGSTREAMER_GSTREAMER_IMAGE_STREAM_H
|
||||
|
||||
#include <gst/app/gstappsink.h>
|
||||
#include <gst/pbutils/pbutils.h>
|
||||
|
||||
#include <osg/ImageStream>
|
||||
|
||||
#include <OpenThreads/Thread>
|
||||
|
||||
namespace osgGStreamer {
|
||||
|
||||
class GStreamerImageStream : public osg::ImageStream, public OpenThreads::Thread
|
||||
{
|
||||
public:
|
||||
|
||||
GStreamerImageStream();
|
||||
GStreamerImageStream(const GStreamerImageStream & image, const osg::CopyOp & copyop = osg::CopyOp::SHALLOW_COPY);
|
||||
|
||||
META_Object(osgGStreamer, GStreamerImageStream);
|
||||
|
||||
bool open(const std::string &filename);
|
||||
|
||||
virtual void play();
|
||||
virtual void pause();
|
||||
virtual void rewind();
|
||||
virtual void seek(double time);
|
||||
|
||||
virtual int s() const;
|
||||
virtual int t() const;
|
||||
|
||||
private:
|
||||
|
||||
virtual ~GStreamerImageStream();
|
||||
|
||||
static gboolean on_message(GstBus *bus, GstMessage *message, GStreamerImageStream *user_data);
|
||||
|
||||
static GstFlowReturn on_new_sample(GstAppSink *appsink, GStreamerImageStream *user_data);
|
||||
static GstFlowReturn on_new_preroll(GstAppSink *appsink, GStreamerImageStream *user_data);
|
||||
|
||||
virtual void run();
|
||||
|
||||
GMainLoop *loop;
|
||||
GstElement *pipeline;
|
||||
|
||||
unsigned char *internal_buffer;
|
||||
|
||||
int width;
|
||||
int height;
|
||||
};
|
||||
}
|
||||
|
||||
#endif // HEADER_GUARD_OSGGSTREAMER_GSTREAMER_IMAGE_STREAM_H
|
||||
77
src/osgPlugins/gstreamer/ReaderWriterGStreamer.cpp
Normal file
77
src/osgPlugins/gstreamer/ReaderWriterGStreamer.cpp
Normal file
@@ -0,0 +1,77 @@
|
||||
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2010 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 "GStreamerImageStream.hpp"
|
||||
|
||||
#include <osgDB/Registry>
|
||||
#include <osgDB/FileNameUtils>
|
||||
#include <osgDB/FileUtils>
|
||||
|
||||
|
||||
class ReaderWriterGStreamer : public osgDB::ReaderWriter
|
||||
{
|
||||
public:
|
||||
|
||||
ReaderWriterGStreamer()
|
||||
{
|
||||
supportsExtension("avi", "");
|
||||
supportsExtension("flv", "Flash video");
|
||||
supportsExtension("mov", "Quicktime");
|
||||
supportsExtension("ogg", "Theora movie format");
|
||||
supportsExtension("mpg", "Mpeg movie format");
|
||||
supportsExtension("mpv", "Mpeg movie format");
|
||||
supportsExtension("wmv", "Windows Media Video format");
|
||||
supportsExtension("mkv", "Matroska");
|
||||
supportsExtension("mjpeg", "Motion JPEG");
|
||||
supportsExtension("mp4", "MPEG-4");
|
||||
supportsExtension("m4v", "MPEG-4");
|
||||
supportsExtension("sav", "Unknown");
|
||||
supportsExtension("3gp", "3G multi-media format");
|
||||
supportsExtension("sdp", "Session Description Protocol");
|
||||
supportsExtension("m2ts", "MPEG-2 Transport Stream");
|
||||
|
||||
gst_init(NULL, NULL);
|
||||
}
|
||||
|
||||
virtual ~ReaderWriterGStreamer()
|
||||
{
|
||||
}
|
||||
|
||||
virtual const char * className() const
|
||||
{
|
||||
return "ReaderWriterGStreamer";
|
||||
}
|
||||
|
||||
virtual ReadResult readImage(const std::string & filename, const osgDB::ReaderWriter::Options* options) const
|
||||
{
|
||||
const std::string ext = osgDB::getLowerCaseFileExtension(filename);
|
||||
|
||||
if (!acceptsExtension(ext)) return ReadResult::FILE_NOT_HANDLED;
|
||||
|
||||
const std::string path = osgDB::containsServerAddress(filename) ?
|
||||
filename :
|
||||
osgDB::findDataFile(filename, options);
|
||||
|
||||
if (path.empty()) return ReadResult::FILE_NOT_FOUND;
|
||||
|
||||
osg::ref_ptr<osgGStreamer::GStreamerImageStream> imageStream = new osgGStreamer::GStreamerImageStream();
|
||||
|
||||
if (!imageStream->open(filename)) return ReadResult::FILE_NOT_HANDLED;
|
||||
|
||||
return imageStream.release();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
REGISTER_OSGPLUGIN(gstreamer, ReaderWriterGStreamer)
|
||||
Reference in New Issue
Block a user