The sfeMovie project seems awesome, and I do not doubt that I could use it. However trying out the tutorials for GStreamer SDK I found it good performance-wise as well as quite easy to use and install, for being a very flexible, large and complex SDK. Which that is why I will try to use it in my project. However if that fails miserably I will probably try to use sfeMovie instead.
My (initial) plan is to use GStreamers playbin2 and the gst-plugin-gl (OpenGL plugin for GStreamer, which is included in the OSSBuild) to draw video as a sprite (or at least on a textured quad). As the plugin will give you an actual OpenGL-texture-id when a frame has been decoded. The plugin enables the current frame to be uploaded to the GPU without any work, except for the actual setup of the pipeline . Decoding a 1080p h264 movie with the help of playbin2 works very nice, getting a very similar preformenc as VLC and similar media-players on Windows (haven't tried it on *nix just yet).
If you still want to make your own player, here are some points you should have a close look at:
- audio/video syncing
- audio/video decoding latency handling
- video stream structure: especially the fact that most frames in a video file are incomplete frames, which prevents you from simply seeking to a specific time
As far as I know GStreamer does all this for you.
Some interesting links:
GStreamer SDK tutorials:
http://docs.gstreamer.com/display/GstSDK/TutorialsOSSBuild, GStreamer that includes the OpenGL plugin and a lot more.
http://code.google.com/p/ossbuild/GStreamer SDK and the OSSBuild works nicely on Windows in VS2010.
Here is a very simple example (from gst-plugin-gl) of how to use the OpenGL plugin, drawing a test video on the sides of a cube:
#include <GL/glew.h>
#include <gst/gst.h>
#include <iostream>
#include <string>
static gboolean bus_call (GstBus *bus, GstMessage *msg, gpointer data)
{
GMainLoop *loop = (GMainLoop*)data;
switch (GST_MESSAGE_TYPE (msg))
{
case GST_MESSAGE_EOS:
g_print ("End-of-stream\n");
g_main_loop_quit (loop);
break;
case GST_MESSAGE_ERROR:
{
gchar *debug = NULL;
GError *err = NULL;
gst_message_parse_error (msg, &err, &debug);
g_print ("Error: %s\n", err->message);
g_error_free (err);
if (debug)
{
g_print ("Debug deails: %s\n", debug);
g_free (debug);
}
g_main_loop_quit (loop);
break;
}
default:
break;
}
return TRUE;
}
//client reshape callback
void reshapeCallback (GLuint width, GLuint height, gpointer data)
{
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45, (gfloat)width/(gfloat)height, 0.1, 100);
glMatrixMode(GL_MODELVIEW);
}
//client draw callback
gboolean drawCallback (GLuint texture, GLuint width, GLuint height, gpointer data)
{
static GLfloat xrot = 0;
static GLfloat yrot = 0;
static GLfloat zrot = 0;
static GTimeVal current_time;
static glong last_sec = current_time.tv_sec;
static gint nbFrames = 0;
g_get_current_time (¤t_time);
nbFrames++ ;
if ((current_time.tv_sec - last_sec) >= 1)
{
std::cout << "GRPHIC FPS = " << nbFrames << std::endl;
nbFrames = 0;
last_sec = current_time.tv_sec;
}
glEnable(GL_DEPTH_TEST);
glEnable (GL_TEXTURE_RECTANGLE_ARB);
glBindTexture (GL_TEXTURE_RECTANGLE_ARB, texture);
glTexParameteri (GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri (GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri (GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri (GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0f,0.0f,-5.0f);
glRotatef(xrot,1.0f,0.0f,0.0f);
glRotatef(yrot,0.0f,1.0f,0.0f);
glRotatef(zrot,0.0f,0.0f,1.0f);
glBegin(GL_QUADS);
// Front Face
glTexCoord2f((gfloat)width, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
glTexCoord2f(0.0f, (gfloat)height); glVertex3f( 1.0f, 1.0f, 1.0f);
glTexCoord2f((gfloat)width, (gfloat)height); glVertex3f(-1.0f, 1.0f, 1.0f);
// Back Face
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
glTexCoord2f(0.0f, (gfloat)height); glVertex3f(-1.0f, 1.0f, -1.0f);
glTexCoord2f((gfloat)width, (gfloat)height); glVertex3f( 1.0f, 1.0f, -1.0f);
glTexCoord2f((gfloat)width, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
// Top Face
glTexCoord2f((gfloat)width, (gfloat)height); glVertex3f(-1.0f, 1.0f, -1.0f);
glTexCoord2f((gfloat)width, 0.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, (gfloat)height); glVertex3f( 1.0f, 1.0f, -1.0f);
// Bottom Face
glTexCoord2f((gfloat)width, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
glTexCoord2f(0.0f, (gfloat)height); glVertex3f( 1.0f, -1.0f, 1.0f);
glTexCoord2f((gfloat)width,(gfloat)height); glVertex3f(-1.0f, -1.0f, 1.0f);
// Right face
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
glTexCoord2f(0.0f, (gfloat)height); glVertex3f( 1.0f, 1.0f, -1.0f);
glTexCoord2f((gfloat)width, (gfloat)height); glVertex3f( 1.0f, 1.0f, 1.0f);
glTexCoord2f((gfloat)width, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
// Left Face
glTexCoord2f((gfloat)width, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
glTexCoord2f(0.0f, (gfloat)height); glVertex3f(-1.0f, 1.0f, 1.0f);
glTexCoord2f((gfloat)width, (gfloat)height); glVertex3f(-1.0f, 1.0f, -1.0f);
glEnd();
xrot+=0.3f;
yrot+=0.2f;
zrot+=0.4f;
//return TRUE causes a postRedisplay
return TRUE;
}
//gst-launch-0.10 videotestsrc num_buffers=400 ! video/x-raw-rgb, width=320, height=240 !
//glgraphicmaker ! glfiltercube ! video/x-raw-gl, width=800, height=600 ! glimagesink
gint main (gint argc, gchar *argv[])
{
GstStateChangeReturn ret;
GstElement *pipeline, *videosrc, *glupload, *glimagesink;
GMainLoop *loop;
GstBus *bus;
/* initialization */
gst_init (&argc, &argv);
loop = g_main_loop_new (NULL, FALSE);
/* create elements */
pipeline = gst_pipeline_new ("pipeline");
/* watch for messages on the pipeline's bus (note that this will only
* work like this when a GLib main loop is running) */
bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
gst_bus_add_watch (bus, bus_call, loop);
gst_object_unref (bus);
/* create elements */
videosrc = gst_element_factory_make ("videotestsrc", "videotestsrc0");
glupload = gst_element_factory_make ("glupload", "glupload0");
glimagesink = gst_element_factory_make ("glimagesink", "glimagesink0");
if (!videosrc || !glupload || !glimagesink)
{
g_print ("one element could not be found \n");
return -1;
}
/* change video source caps */
GstCaps *caps = gst_caps_new_simple("video/x-raw-rgb",
"width", G_TYPE_INT, 320,
"height", G_TYPE_INT, 240,
"framerate", GST_TYPE_FRACTION, 25, 1,
NULL) ;
GstCaps *outcaps = gst_caps_new_simple("video/x-raw-gl",
"width", G_TYPE_INT, 800,
"height", G_TYPE_INT, 600,
NULL) ;
/* configure elements */
g_object_set(G_OBJECT(videosrc), "num-buffers", 400, NULL);
g_object_set(G_OBJECT(glimagesink), "client-reshape-callback", reshapeCallback, NULL);
g_object_set(G_OBJECT(glimagesink), "client-draw-callback", drawCallback, NULL);
g_object_set(G_OBJECT(glimagesink), "client-data", NULL, NULL);
/* add elements */
gst_bin_add_many (GST_BIN (pipeline), videosrc, glupload, glimagesink, NULL);
/* link elements */
gboolean link_ok = gst_element_link_filtered(videosrc, glupload, caps) ;
gst_caps_unref(caps) ;
if(!link_ok)
{
g_warning("Failed to link videosrc to glupload!\n") ;
return -1 ;
}
link_ok = gst_element_link_filtered(glupload, glimagesink, outcaps) ;
gst_caps_unref(outcaps) ;
if(!link_ok)
{
g_warning("Failed to link glupload to glimagesink!\n") ;
return -1 ;
}
/* run */
ret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
if (ret == GST_STATE_CHANGE_FAILURE)
{
g_print ("Failed to start up pipeline!\n");
/* check if there is an error message with details on the bus */
GstMessage* msg = gst_bus_poll (bus, GST_MESSAGE_ERROR, 0);
if (msg)
{
GError *err = NULL;
gst_message_parse_error (msg, &err, NULL);
g_print ("ERROR: %s\n", err->message);
g_error_free (err);
gst_message_unref (msg);
}
return -1;
}
g_main_loop_run (loop);
/* clean up */
gst_element_set_state (pipeline, GST_STATE_NULL);
gst_object_unref (pipeline);
return 0;
}
I do not yet know exacly how I will setup the actual rendering of the quad in SFML, I might try to sub-class the Drawable(?) class or draw the thing in "pure" OpenGL.
The goal is to be able to draw at-least 3 (hopefully more) 1080p movies at the same time.
// Zap