PDA

View Full Version : Video Streaming from OpenGL



Paul Iomom
06-07-2010, 02:58 AM
Hi there,

I want to create a video stream from an OpenGL application.
This program just displays a quad whose color changes :

#include <SDL/SDL.h>
#include <GL/glew.h>

int main(int argc, char **argv) {

SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTTHREAD);
SDL_SetVideoMode(800, 600, 16, SDL_ANYFORMAT | SDL_HWSURFACE | SDL_DOUBLEBUF | SDL_OPENGL | SDL_FULLSCREEN);

bool running = true;
SDL_Event event;
int rightColor = 0;
int leftColor = 0;

while (running) {

// Events
while (SDL_PollEvent(&amp;event)) {
switch (event.type) {
case SDL_QUIT:
running = false;
break;
case SDL_KEYDOWN:
switch (event.key.keysym.sym) {
case SDLK_ESCAPE:
running = false;
break;
}
}
}

// Quad
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_QUADS);
glColor3ub(0, 0, leftColor);
glVertex2d(-0.75, -0.75);
glVertex2d(-0.75, 0.75);
glColor3ub(rightColor, 0, 0);
glVertex2d(0.75, 0.75);
glVertex2d(0.75, -0.75);
glEnd();

glFlush();
SDL_GL_SwapBuffers();

rightColor = 1+(int)((float)rand()/32767*254);
leftColor = 1+(int)((float)rand()/32767*254);

SDL_Delay(800);
}

SDL_Quit();
return 0;
}

On the other hand, I have a program that uses VLC (LibVLC) to play a video file.

#include <stdio.h>
#include <stdlib.h>
#include <vlc/vlc.h>
#include <unistd.h>

static void quit_on_exception(libvlc_exception_t *excp) {
if (libvlc_exception_raised(excp)) {
fprintf(stderr, "error: %s\n", libvlc_exception_get_message(excp));
exit(EXIT_FAILURE);
}
}

int main(int argc, char **argv) {
libvlc_exception_t excp;
libvlc_instance_t *inst;
libvlc_media_t *media;
libvlc_media_player_t *mediaPlayer;
char *filename = "/home/test/Bureau/out.ogv";

// LibVLC Init
libvlc_exception_init(&amp;excp);
inst = libvlc_new(argc, argv, &amp;excp);
quit_on_exception(&amp;excp);
media = libvlc_media_new(inst, filename, &amp;excp);
quit_on_exception(&amp;excp);
mediaPlayer = libvlc_media_player_new_from_media(media, &amp;excp);
quit_on_exception(&amp;excp);
libvlc_media_release(media);

// Now playing
libvlc_media_player_play(mediaPlayer, &amp;excp);
quit_on_exception(&amp;excp);

usleep(10000000);

// Stop the movie
libvlc_media_player_stop(mediaPlayer, &amp;excp);
quit_on_exception(&amp;excp);

libvlc_media_player_release(mediaPlayer);
libvlc_release(inst);

return 0;

}
I must use that lib because I also want to play movies in various formats.

So the question is : How should I modify these two programs to make them communicate ? I want to redirect the OpenGL animation as a video stream and read the stream using the second program.

After several hours of searching I saw that some spoke of using glReadPixels()... It could store this pixels in memory (and then, transfer them to VLC)...

I specifies that a local solution (i.e. on the same PC, but keeping the two programs separated) would suit me perfectly.

Thanks in advance.

Regards.

yooyo
06-07-2010, 04:16 AM
It is not allowed to access to same GL context from different applications (processes). Depending on your OS there is a tweaks how to do similar thing.