SDL2 and libvlc help?
Posted: 11 Jan 2014 00:30
all day i have been trying to integrate libvlc into sdl but cant seem to get it right. my code compiles but on run it gives a segfault, any help would be appreciated.
//video.cpp
thiis is where my debugger complains
//video.cpp
Code: Select all
#include "video.hpp"
void *lock(void *data, void **p_pixels);
void unlock(void *data, void *id, void *const *p_pixels);
void display(void *data, void *id);
Video::Video()
{
}
Video::~Video(){
//cleanup time
if(isPlaying == true){
this->stop();
}
libvlc_media_player_release(mp);
libvlc_release(inst);
SDL_DestroyMutex(this->con->mutex);
SDL_DestroyRenderer(con->renderer);
}
Video::Video(SDL_Renderer *r){
this->inst = libvlc_new(0, NULL);
this->con->renderer = r;
this->con->t = SDL_CreateTexture(this->con->renderer,SDL_PIXELFORMAT_BGR565,SDL_TEXTUREACCESS_STREAMING,400,400);
this->con->mutex = SDL_CreateMutex();
libvlc_video_set_callbacks(mp, lock,unlock,display,&this->con);
libvlc_video_set_format(mp, "RV16", 100, 100, 200);
}
void *lock(void *data, void **p_pixels)
{
Context *ctx = (Context*)data;
int n;
SDL_LockMutex(ctx->mutex);
SDL_LockTexture(ctx->t,NULL,p_pixels, &ctx->n);
//*p_pixels = ctx->surf->pixels;
return NULL; /* picture identifier, not needed here */
}
void unlock(void *data, void *id, void *const *p_pixels)
{
Context *ctx = (Context*)data;
SDL_UnlockTexture(ctx->t);
SDL_UnlockMutex(ctx->mutex);
}
void display(void *data, void *id)
{
/* VLC wants to display the video */
(void) data;
}
void Video::loadTrack(std::string path){
currentTrack = path;
m = libvlc_media_new_path(inst, path.c_str());
mp = libvlc_media_player_new_from_media(m);
libvlc_media_release(m);
}
void Video::pause(){
if(paused == false)
{
libvlc_media_player_set_pause(mp, 1);
paused = true;
}else if(paused == true){
libvlc_media_player_set_pause(mp, 0);
paused = false;
}
}
void Video::play(){
if (isPlaying == false){
libvlc_media_player_play(mp);
isPlaying = true;
}
}
std::string Video::current(){
return currentTrack;
}
void Video::stop(){
if(isPlaying == true){
libvlc_media_player_stop(mp);
isPlaying = false;
}
}
Code: Select all
#ifndef VIDEO_H
#define VIDEO_H
#include <SDL2/SDL.h>
#include <vlc/vlc.h>
#include <string>
struct Context {
SDL_mutex *mutex;
SDL_Renderer *renderer;
SDL_Texture *t;
int n;
};
class Video
{
public:
Video();
Video(SDL_Renderer *r);
~Video();
void play();
void pause();
void stop();
void loadTrack(std::string);
std::string current();
//void lock(void *data, void **pixels);
//void unlock(void *data, void *id, void *const *p_pixels);
//void display(void *data, void *id);
void init();
std::string currentTrack;
private:
bool isPlaying; // is the media player playing anything
bool paused; // is the media player currently paused
libvlc_instance_t *inst; //holds our libvlc instance
libvlc_media_player_t *mp; //media player
libvlc_media_t *m; // stores data for each media object
SDL_Rect rect;
SDL_Surface *screen;
SDL_Renderer *renderrer;
Context *con;
};
#endif // VIDEO_H
Code: Select all
libvlc_video_set_callbacks(mp, lock,unlock,display,&this->con);