Im trying to use libVLC to retrieve an online video stream and display frames as OpenCV IplImages.
This is what I have so far, and for some reason it seems to work sometimes but most of the time it just displays nothing but the audio is still playing.
Code: Select all
struct ctx
{
IplImage* image;
HANDLE mutex;
uchar* pixels;
};
void *lock(void *data, void**p_pixels)
{
struct ctx *ctx = (struct ctx*)data;
WaitForSingleObject(ctx->mutex, INFINITE);
*p_pixels = ctx->pixels;
return NULL;
}
void display(void *data, void *id){
(void) data;
assert(id == NULL);
}
void unlock(void *data, void *id, void *const *p_pixels){
struct ctx *ctx = (struct ctx*)data;
/* VLC just rendered the video, but we can also render stuff */
uchar *pixels = (uchar*)*p_pixels;
cvShowImage("image", ctx->image);
ReleaseMutex(ctx->mutex);
assert(id == NULL); /* picture identifier, not needed here */
}
int main()
{
cvNamedWindow("image", CV_WINDOW_AUTOSIZE);
libvlc_media_t* media = NULL;
libvlc_media_player_t* mediaPlayer = NULL;
char const* vlc_argv[] = {"--plugin-path", "C:\\Users\\Oscar\\Documents\\libvlc\\vlc-1.1.4"};
libvlc_instance_t* instance = libvlc_new(2,vlc_argv);
mediaPlayer = libvlc_media_player_new(instance);
media = libvlc_media_new_path(instance, "mms://81.89.49.210/musicbox");
struct ctx* context = ( struct ctx* )malloc( sizeof( *context ) );
context->mutex = CreateMutex(NULL, FALSE,NULL);
context->image = cvCreateImage(cvSize(VIDEO_WIDTH, VIDEO_HEIGHT), IPL_DEPTH_8U, 4);
context->pixels = (unsigned char *)context->image->imageData;
libvlc_media_player_set_media( mediaPlayer, media);
libvlc_video_set_callbacks(mediaPlayer, lock, unlock, display, context);
libvlc_video_set_format(mediaPlayer, "RV32", VIDEO_WIDTH, VIDEO_HEIGHT, VIDEO_WIDTH*4);
libvlc_media_player_play(mediaPlayer);
while(1)
{
}
return 0;
}
Sorry if the code is messy...I'm just trying to get it work before I clean it up a bit..
Thanks!