Sign in to follow this  
glmisscelan

Playing video

Recommended Posts

Hi, I want to load some video movies on my game, im using gl with sdl, actually im using smpeg because i want it working on linux and windows. But im getting some problems with it on windows. Besides, with this you can only use MPEG-1 and that cause me several headaches. The movies are made originally on flash. So, anyone know any multiplatform library that allow me to play video or any cool way to put some flashs movies without to rewrite lot of my code??? Thanks in advance.

Share this post


Link to post
Share on other sites
First up; NO, this code isn't cross-platform compatible. Its a shame, but its just how it is.

This code snippet uses gstreamer to play any type of video that gstreamer can. I suggest you install the ffmpeg plugin for maximum benefit.

It uses two variable, vidHeight and vidWidth to specify the size of the video, which you will need to find and change to suit your source file. Using gstreamer to find these details is outside the scope of this code

And its in python, but the libraries are available in C++. Pygame is a wrapper for SDL, pyOpenGL is a wrapper for...


#!/usr/bin/python
import os
import sys

import gobject
gobject.threads_init()

import gst
import pygame
import OpenGL.GL
import OpenGL.GLU

vidSource = blah.format #Needs to be relative to the current directory
vidWidth = 100 #You will need to change this.
vidHeight = 100 #You will also need to change this

class videoPlayer():

def __init__(self, (windowWidth, windowHeight)):
self.updated = False
pygame.init()
flags = pygame.DOUBLEBUF|pygame.OPENGL
pygame.display.set_mode((windowWidth,windowHeight), flags)
OpenGL.GL.glEnable(OpenGL.GL.GL_TEXTURE_2D)
OpenGL.GL.glClearColor(0.0, 0.0, 0.0, 0.0)
OpenGL.GL.glClearDepth(1.0)
#Setup the lighting
OpenGL.GL.glLightfv( OpenGL.GL.GL_LIGHT1, OpenGL.GL.GL_AMBIENT,
(0.5, 0.5, 0.5, 1.0) )
OpenGL.GL.glLightfv( OpenGL.GL.GL_LIGHT1, OpenGL.GL.GL_DIFFUSE,
(1.0, 1.0, 1.0, 1.0) )
OpenGL.GL.glLightfv( OpenGL.GL.GL_LIGHT1, OpenGL.GL.GL_POSITION,
(0.0, 0.0, 2.0, 1.0) )
OpenGL.GL.glEnable( OpenGL.GL.GL_LIGHT1 )

OpenGL.GL.glViewport(0, 0, self.width, self.height)
OpenGL.GL.glMatrixMode(OpenGL.GL.GL_PROJECTION)
OpenGL.GL.glLoadIdentity()
OpenGL.GLU.gluPerspective(60.0, float(windowWidth)/float(, 1, 1000.0)
OpenGL.GL.glMatrixMode(OpenGL.GL.GL_MODELVIEW)
OpenGL.GL.glLoadIdentity()

def textureSetup(self, vidHeight, vidWidth):
self.vidHeight = vidHeight
self.vidWidth = vidWidth
blankSurface = pygame.Surface((self.srcWidth, self.srcHeight),
pygame.HWSURFACE, 24)
blankSurface.fill((0,0,0))
self.textureNo = OpenGL.GL.glGenTextures(1)
surfaceData = pygame.image.tostring(blankSurface, "RGB", True)
self.buffah = surfaceData

OpenGL.GL.glBindTexture(OpenGL.GL.GL_TEXTURE_2D, self.textureNo)
OpenGL.GLU.gluBuild2DMipmaps(OpenGL.GL.GL_TEXTURE_2D, OpenGL.GL.GL_RGB,
vidWidth, vidHeight, OpenGL.GL.GL_RGB,
OpenGL.GL.GL_UNSIGNED_BYTE, surfaceData)
OpenGL.GL.glTexParameteri(OpenGL.GL.GL_TEXTURE_2D,
OpenGL.GL.GL_TEXTURE_MAG_FILTER, OpenGL.GL.GL_LINEAR)
OpenGL.GL.glTexParameteri(OpenGL.GL.GL_TEXTURE_2D,
OpenGL.GL.GL_TEXTURE_MIN_FILTER, OpenGL.GL.GL_LINEAR)

self.listNo = OpenGL.GL.glGenLists(1)
OpenGL.GL.glNewList(self.listNo, OpenGL.GL.GL_COMPILE)
OpenGL.GL.glBegin(OpenGL.GL.GL_QUADS)
OpenGL.GL.glNormal3f(0.0, 0.0, 1.0)
OpenGL.GL.glTexCoord2f(0.0, 1.0); OpenGL.GL.glVertex3f(-400, -300, -0)
OpenGL.GL.glTexCoord2f(1.0, 1.0); OpenGL.GL.glVertex3f( 400, -300, -0)
OpenGL.GL.glTexCoord2f(1.0, 0.0); OpenGL.GL.glVertex3f( 400, 300, -0)
OpenGL.GL.glTexCoord2f(0.0, 0.0); OpenGL.GL.glVertex3f(-400, 300, -0)

OpenGL.GL.glEnd()
OpenGL.GL.glEndList()

def vidSetup(self, vidSource):
#Setup Gstreamer
s = ''' filesrc name=input
! decodebin name=dbin
! queue name =q
! ffmpegcolorspace ! video/x-raw-rgb
! fakesink name=output signal-handoffs=true sync=true
dbin. ! queue
! audioconvert ! audiorate ! alsasink
'''


self.myPipeline = gst.parse_launch(s)
self.myInput = myPipeline.get_by_name('input')
self.myFakeSink = myPipeline.get_by_name('output')
self.myInput.set_property("location", os.getcwd() + '/' + vidSource)
self.myFakeSink.connect ("handoff", self.texUpdate)
self.myPipeline.set_state(gst.STATE_PLAYING)

def texUpdate(self, sink, buffer, pad):
self.buffah = buffer
self.updated = True

def play(self):
while True:
if self.updated:
surfaceData = self.buffah
OpenGL.GL.glBindTexture(OpenGL.GL.GL_TEXTURE_2D, self.textureNo)
OpenGL.GLU.gluBuild2DMipmaps(OpenGL.GL.GL_TEXTURE_2D,
OpenGL.GL.GL_RGB, self.vidWidth, self.vidHeight,
OpenGL.GL.GL_RGB, OpenGL.GL.GL_UNSIGNED_BYTE,
surfaceData)
OpenGL.GL.glTexParameteri(OpenGL.GL.GL_TEXTURE_2D,
OpenGL.GL.GL_TEXTURE_MAG_FILTER, OpenGL.GL.GL_LINEAR)
OpenGL.GL.glTexParameteri(OpenGL.GL.GL_TEXTURE_2D,
OpenGL.GL.GL_TEXTURE_MIN_FILTER, OpenGL.GL.GL_LINEAR)
OpenGL.GL.glCallList(self.listNo)
self.updated = False
else:
OpenGL.GL.glBindTexture(OpenGL.GL.GL_TEXTURE_2D, self.textureNo)
OpenGL.GL.glCallList(self.listNo)

myVideoPlayer = videoPlayer((800,600))
myVideoPlayer.textureSetup(vidHeight, vidWidth)
myVideoPlayer.vidSetup(vidSource)
myVideoPlayer.play()




NOTE: I didn't get a chance to test this code, I might need to post an working update later, but this should give you the general idea of how to acheive the playback in linux.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this