c++ - LibVLC tutorial without SDL? -


i trying write video frame frame *.yuv file , found this tutorial rendering video sdl surface.

now i'm not sure how use code without sdl library. example, lock function:

static void *lock(void *data, void **p_pixels) {     struct ctx *ctx = data;      sdl_lockmutex(ctx->mutex);     sdl_locksurface(ctx->surf);     *p_pixels = ctx->surf->pixels;     return null; /* picture identifier, not needed here */ } 

how can write function without struct ctx, sdl_lockmutex , sdl_locksurface?

as can imagine, not experienced programmer please patient me ;)
in advance!

i don't know libvlc, here how go removing sdl based on particular example.

removing sdl

in example, there couple of different functions have pass through libvlc callback function...

libvlc_video_set_callbacks(mp, lock, unlock, display, &ctx); 

now, understand lock function doing, need understand little of multi-threading , how image stored in memory.

  • essentially, make sure nothing accesses area of memory libvlc using while writing it, 'locks' known mutex. if try lock mutex locked else, current execution wait until has been unlocked.

    if accessed these pixels while half-written, can imagine how horrible be? half-written, , use save yuv file. quite disastrous.

  • the second thing lock function specify area in memory vlc can use load image frame. example uses sdl_surface this, can create own if you're careful.

so, if using libvlc, you'll want find alternative these things.


i'll go in reverse order list above. in example, use sdl_surface if unable use that, have create own structure in memory store pixel data if wanting extract it. simple way create char array of correct size. i'll use ctx struct it's convenient: know asked not using it, quite useful in case need pass multiple pieces of information through lock function.

struct ctx {     unsigned char *pixeldata; }; 

now, somewhere in main function need create area in memory. if know video size , how many bits per pixel (bpp) used: pretty simple. careful, if don't correctly: end memory corruption.

ctx.pixeldata = new unsigned char[width * height * bpp]; 

make sure delete @ end of program...

delete[] ctx.pixeldata; 

the next thing mutex. isn't strictly needed, can come across problems mentioned above. if want use mutex, need specify unlock function in libvlc_video_set_callbacks (you can specify null unlock if don't want use mutex).

the problem mutex use purpose (if want use one, suggest do)? if using newer c++11 standard can use std::mutex. if aren't, you'll have find else boost threading library or write similar of own. if using c++11, you'd add ctx struct...

#include <mutex>  struct ctx {     unsigned char *pixeldata;     std::mutex imagemutex; }; 

now actual lock function itself.

static void *lock(void *data, void **p_pixels) {     struct ctx *ctx = data;      ctx->imagemutex.lock()     *p_pixels = ctx->pixeldata;      return null; } 

your unlock function this...

static void unlock(void *data, void *id, void *const *p_pixels) {     struct ctx *ctx = data;      ctx->unlock();      assert(id == null); } 

and whenever want access pixel data safely...

ctx->imagemutex.lock(); /* access data here */ ctx->imagemutex.unlock(); 

using sdl

i wanted add briefly sdl. while can used display video screen, don't need to. personally, if you're not experienced, suggest continue use sdl , remove display code further down example. handles memory in example, it's bit easier writing own safe code if don't know how.


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -