First test of libVLC include files.

External libraries (GTK, GSL, SDL, Allegro, OpenGL, etc) questions.
D.J.Peters
Posts: 7419
Joined: May 28, 2005 3:28

First test of libVLC include files.

Postby D.J.Peters » Oct 13, 2015 8:56

I tested the libVLC FreeBASIC include files successfull :-)

Joshy

idea from: simplest libVLC example

Code: Select all

' from: simplest libVLC example
' https://sourceforge.net/projects/simplestlibvlcexample/

#include once "vlc/vlc.bi"

' I tested flc, mp4, avi, xvid ...

const as string TEST = "./HAL 9000 'I'm sorry Dave, I'm afraid I can't do that'.mp4"
chdir exepath() 
var instance = libvlc_new (0, NULL)
var media    = libvlc_media_new_path (instance,TEST)
var player   = libvlc_media_player_new_from_media (media)
libvlc_media_release(media)
libvlc_media_player_play(player)
dim as long w,h,l,timeout=2000 ' 2 seconds
print "wait on start ..."
while w=0 andalso h=0 andalso l=0 andalso timeout>=0
  w = libvlc_video_get_width(player)
  h = libvlc_video_get_height(player)
  l = libvlc_media_player_get_length(player)
  sleep 100 : timeout-=100
wend
if timeout<0 then
  libvlc_media_player_release(player)
  libvlc_release(instance)
  ?:?:?
  print "play back not started !"
  print "error log: in console ?"
  beep:sleep:end
end if
print "size: " & w & " x " & h & " length: " & l\1000
print "playing ..."
print "sleep press any key"
sleep
libvlc_media_player_stop(player)
libvlc_media_player_release(player)
libvlc_release(instance)
Last edited by D.J.Peters on Mar 20, 2016 21:42, edited 1 time in total.
WQ1980
Posts: 32
Joined: Sep 25, 2015 12:04
Location: Russia

Re: First test of libVLC include files.

Postby WQ1980 » Nov 03, 2015 11:31

Hi!
My simple code (libvlc.dll, libvlccore.dll and folder "plugins" from VLC folder -> code folder):

Code: Select all

#Define NIL 0
Type libvlc_media_player_t As Any
Type libvlc_instance_t As Any
Type libvlc_media_t As Any

Dim Shared libvlc_get_version As Function Cdecl() As ZString Ptr
Dim Shared libvlc_new As Function Cdecl (ByVal argc As Integer, ByVal argv As String Ptr Ptr) As libvlc_instance_t Ptr
Dim Shared libvlc_release As Sub Cdecl (ByVal p_instance As libvlc_instance_t Ptr)
Dim Shared libvlc_media_new_location As Function Cdecl (ByVal p_mi As libvlc_media_t Ptr,  pt As ZString Ptr) As libvlc_media_t Ptr
Dim Shared libvlc_media_new_path As Function Cdecl (ByVal p_mi As libvlc_instance_t Ptr, pt As ZString Ptr) As libvlc_media_t Ptr
Dim Shared libvlc_media_player_new_from_media As Function Cdecl (ByVal p_mi As libvlc_media_t Ptr) As libvlc_media_player_t Ptr
Dim Shared libvlc_media_release As Sub Cdecl (ByVal p_md As libvlc_media_t Ptr)
Dim Shared libvlc_media_player_set_hwnd As Sub Cdecl (ByVal p_mi As libvlc_media_player_t Ptr, ByVal drawable As Any Ptr)
Dim Shared libvlc_media_player_play As Function Cdecl (ByVal p_mi As libvlc_media_player_t Ptr) As Integer
Dim Shared ibvlc_media_player_release As Sub Cdecl (ByVal p_mi As libvlc_media_player_t Ptr)
Dim Shared libvlc_media_player_stop As Sub Cdecl (ByVal p_mi As libvlc_media_player_t Ptr)
Dim Shared libvlc_audio_set_volume As Function Cdecl (ByVal p_mi As libvlc_media_player_t Ptr, ByVal i_volume As Integer) As Integer
Dim Shared libvlc_media_player_get_length As Function Cdecl (ByVal p_mi As libvlc_media_player_t Ptr) As Integer
Dim Shared libvlc_media_player_get_time As Function Cdecl (ByVal p_mi As libvlc_media_player_t Ptr) As Integer
Dim Shared libvlc_media_player_get_position As Function Cdecl (ByVal p_mi As libvlc_media_player_t Ptr) As Integer
Dim Shared libvlc_video_set_marquee_string As Sub Cdecl (ByVal p_mi As libvlc_media_player_t Ptr, ByVal option As UInteger, ByVal psz_text As ZString Ptr)
Dim Shared libvlc_video_set_marquee_int As Sub Cdecl (ByVal p_mi As libvlc_media_player_t Ptr, ByVal option As UInteger, ByVal i_val As Integer)

Dim stdhandle As Any Ptr

#Ifdef __FB_WIN32__
stdhandle = DyLibLoad("libvlc.dll")
#Else
stdhandle = DyLibLoad("libvlc.so")
#EndIf

libvlc_get_version=DyLibSymbol(stdhandle,"libvlc_get_version")
libvlc_new =DyLibSymbol(stdhandle,"libvlc_new")
libvlc_release=DyLibSymbol(stdhandle,"libvlc_release")
libvlc_media_new_location=DyLibSymbol(stdhandle,"libvlc_media_new_location")
libvlc_media_new_path=DyLibSymbol(stdhandle,"libvlc_media_new_path")
libvlc_media_player_new_from_media=DyLibSymbol(stdhandle,"libvlc_media_player_new_from_media")
libvlc_media_release=DyLibSymbol(stdhandle,"libvlc_media_release")
libvlc_media_player_set_hwnd=DyLibSymbol(stdhandle,"libvlc_media_player_set_hwnd")
libvlc_media_player_play=DyLibSymbol(stdhandle,"libvlc_media_player_play")
ibvlc_media_player_release=DyLibSymbol(stdhandle,"ibvlc_media_player_release")
libvlc_media_player_stop=DyLibSymbol(stdhandle,"libvlc_media_player_stop")
libvlc_audio_set_volume=DyLibSymbol(stdhandle,"libvlc_audio_set_volume")
libvlc_media_player_get_length=DyLibSymbol(stdhandle,"libvlc_media_player_get_length")
libvlc_media_player_get_time=DyLibSymbol(stdhandle,"libvlc_media_player_get_time")
libvlc_media_player_get_position=DyLibSymbol(stdhandle,"libvlc_media_player_get_position")
libvlc_video_set_marquee_string=DyLibSymbol(stdhandle,"libvlc_video_set_marquee_string")
libvlc_video_set_marquee_int=DyLibSymbol(stdhandle,"libvlc_video_set_marquee_string")

?*libvlc_get_version()

Dim Shared As ZString*255 sPath

Dim Shared As String Ptr vlc_argv
vlc_argv = Callocate(SizeOf(String) * 2)
vlc_argv[0]="--no-video-title-show" ' no title
vlc_argv[1]="--no-audio" '  no audio
Var vlc1 = libvlc_new(1, vlc_argv)


sPath = "C:\Ob\1.mkv"

Var m = libvlc_media_new_path(vlc1,sPath)
Var mp = libvlc_media_player_new_from_media(m)
libvlc_media_release(m)
libvlc_media_player_play(mp)
Sleep



How to pass >1 parameters in libvlc_new?

Only now 1 parameter, "--no-video-title-show" -> work
"--no-audio" -> not work

Code: Select all

Dim Shared As String Ptr vlc_argv
vlc_argv = Callocate(SizeOf(String) * 2)
vlc_argv[0]="--no-video-title-show" ' no title
vlc_argv[1]="--no-audio" '  no audio
Var vlc1 = libvlc_new(1, vlc_argv)  ' (2, vlc_argv) and etc. -> error



C++ simple code:

Code: Select all

char const *vlc_argv[] =
{
//"--no-audio",
"--no-xlib",
"-q",
//"-vvv",   
"--no-video-title-show",
"--quiet",
"--ignore-config",
"--vout", "vmem", 
"-I", "dumy"
};

int vlc_argc = sizeof(vlc_argv) / sizeof(*vlc_argv);

libvlc = libvlc_new( vlc_argc, vlc_argv );

How correctly convert from C++ to FB?
grindstone
Posts: 491
Joined: May 05, 2015 5:35
Location: Germany

Re: First test of libVLC include files.

Postby grindstone » Nov 05, 2015 10:40

C++ simple code:

Code: [Select all] [Expand/Collapse] [Download] (Untitled.bas)
char const *vlc_argv[] =
{
//"--no-audio",
"--no-xlib",
"-q",
//"-vvv",
"--no-video-title-show",
"--quiet",
"--ignore-config",
"--vout", "vmem",
"-I", "dumy"
};

int vlc_argc = sizeof(vlc_argv) / sizeof(*vlc_argv);

libvlc = libvlc_new( vlc_argc, vlc_argv );

GeSHi ©


How correctly convert from C++ to FB?


Code: Select all

Dim As String arg(0 To ...) = { "--no-audio", _
                                "--no-xlib", _
                                "-q", _
                                "-vvv", _
                                "--no-video-title-show", _
                                "--quiet", _
                                "--ignore-config", _
                                "--vout", "vmem", _
                                "-I", "dummy" }
                                              
ReDim As Any Ptr vlc_argc(UBound(arg))

For x As Integer = 0 To UBound(arg)
   vlc_argc(x) = StrPtr(arg(x))
Next
libvlc = libvlc_new(UBound(vlc_argc) + 1, @vlc_argc(0))


Regards
grindstone
D.J.Peters
Posts: 7419
Joined: May 28, 2005 3:28

Re: First test of libVLC include files.

Postby D.J.Peters » Nov 05, 2015 12:57

yes it's an array of pointers to C strings (terminated by 0)

Code: Select all

Dim as zstring ptr argv(0 To ...) = { _
  @"--no-audio", _
  @"--no-xlib", _
  @"-q", _
  @"-vvv", _
  @"--no-video-title-show", _
  @"--quiet", _
  @"--ignore-config", _
  @"--vout", _
  @"vmem", _
  @"-I", _
  @"dummy"}
dim as long argc=ubound(argv)+1
print argc
sleep
WQ1980
Posts: 32
Joined: Sep 25, 2015 12:04
Location: Russia

Re: First test of libVLC include files.

Postby WQ1980 » Nov 05, 2015 19:26

D.J.Peters
grindstone
Thanks!

One more question...
It is also possible to translate this C++ code ?
Receiving video frame with VLC plug "vmem" -> FB image (it can be used for various operations on video)

C++ code:

Code: Select all

#include "stdafx.h"
#include <stdlib.h>
#include <windows.h>
#include <vlc\vlc.h>

unsigned char * pixels;

void *lock( void *data, void **p_pixels )
{
*p_pixels = pixels;
return NULL;
}


void display( void *data, void *id )
{

}


void unlock( void *data, void *id, void *const *ipixels )
{

}

int _tmain(int argc, _TCHAR* argv[])
{

char const *vlc_argv[] =
{
//"--no-audio",
"--no-xlib",
"-q",
//"-vvv",   
"--no-video-title-show",
"--quiet",
"--ignore-config",
"--vout", "vmem",  // render
"-I", "dumy"
};
int vlc_argc = sizeof(vlc_argv) / sizeof(*vlc_argv);
pixels=new unsigned char[ 1280 * 720 * 4 ];

libvlc_instance_t * inst;
libvlc_media_player_t *mp;
libvlc_media_t *m;

inst = libvlc_new(vlc_argc, vlc_argv);
m = libvlc_media_new_path(inst, "Wildlife.wmv");
mp = libvlc_media_player_new_from_media(m);
libvlc_media_release(m);

libvlc_video_set_callbacks( mp, lock, unlock, display, NULL );
libvlc_video_set_format( mp, "RV32", 1280, 720, 1280*4 );

libvlc_media_player_play(mp);

Sleep(10000);

libvlc_media_player_stop(mp);
libvlc_media_player_release(mp);
libvlc_release(inst);

return 0;
}


My FB code:

Code: Select all


Type libvlc_media_player_t As Any
Type libvlc_instance_t As Any
Type libvlc_media_t As Any

Type libvlc_video_lock_cb As Function (ByVal opaque As Any Ptr, ByVal planes As Any Ptr Ptr) As Any Ptr
Type libvlc_video_unlock_cb As Sub (ByVal opaque As Any Ptr, ByVal picture As Any Ptr, ByVal planes As Any Const Ptr Ptr)
Type libvlc_video_display_cb As Sub (ByVal opaque As Any Ptr, ByVal picture As Any Ptr)

Dim Shared libvlc_get_version As Function Cdecl() As ZString Ptr
Dim Shared libvlc_new As Function Cdecl (ByVal argc As Integer,ByVal argv As Const ZString Ptr Ptr ) As libvlc_instance_t Ptr
Dim Shared libvlc_release As Sub Cdecl (ByVal p_instance As libvlc_instance_t Ptr)
Dim Shared libvlc_media_new_location As Function Cdecl (ByVal p_mi As libvlc_media_t Ptr, pt As ZString Ptr) As libvlc_media_t Ptr
Dim Shared libvlc_media_new_path As Function Cdecl (ByVal p_mi As libvlc_instance_t Ptr, pt As ZString Ptr) As libvlc_media_t Ptr
Dim Shared libvlc_media_player_new_from_media As Function Cdecl (ByVal p_mi As libvlc_media_t Ptr) As libvlc_media_player_t Ptr
Dim Shared libvlc_media_release As Sub Cdecl (ByVal p_md As libvlc_media_t Ptr)
Dim Shared libvlc_media_player_set_hwnd As Sub Cdecl (ByVal p_mi As libvlc_media_player_t Ptr, ByVal drawable As Any Ptr)
Dim Shared libvlc_media_player_play As Function Cdecl (ByVal p_mi As libvlc_media_player_t Ptr) As Integer
Dim Shared ibvlc_media_player_release As Sub Cdecl (ByVal p_mi As libvlc_media_player_t Ptr)
Dim Shared libvlc_media_player_stop As Sub Cdecl (ByVal p_mi As libvlc_media_player_t Ptr)
Dim Shared libvlc_audio_set_volume As Function Cdecl (ByVal p_mi As libvlc_media_player_t Ptr, ByVal i_volume As Integer) As Integer
Dim Shared libvlc_media_player_get_length As Function Cdecl (ByVal p_mi As libvlc_media_player_t Ptr) As Integer
Dim Shared libvlc_media_player_get_time As Function Cdecl (ByVal p_mi As libvlc_media_player_t Ptr) As Integer
Dim Shared libvlc_media_player_get_position As Function Cdecl (ByVal p_mi As libvlc_media_player_t Ptr) As Integer
Dim Shared libvlc_video_set_marquee_string As Sub Cdecl (ByVal p_mi As libvlc_media_player_t Ptr, ByVal option As UInteger, ByVal psz_text As ZString Ptr)
Dim Shared libvlc_video_set_marquee_int As Sub Cdecl (ByVal p_mi As libvlc_media_player_t Ptr, ByVal option As UInteger, ByVal i_val As Integer)
Dim Shared libvlc_video_set_callbacks As Sub Cdecl (ByVal mp As libvlc_media_player_t Ptr, ByVal Lock_ As libvlc_video_lock_cb, ByVal UnLock_ As libvlc_video_unlock_cb, ByVal display As libvlc_video_display_cb, ByVal opaque As Any Ptr)
Dim Shared libvlc_video_set_format As Sub Cdecl (ByVal mp As libvlc_media_player_t Ptr, ByVal chroma As Const ZString Ptr, ByVal Width As ULong, ByVal height As ULong, ByVal pitch As ULong)
Dim Shared libvlc_video_get_height As Function Cdecl (byval p_mi as libvlc_media_player_t ptr) as long
Dim Shared  libvlc_video_get_width As Function Cdecl (byval p_mi as libvlc_media_player_t ptr) as Long
Dim stdhandle As Any Ptr

stdhandle = DylibLoad("libvlc.dll")

libvlc_get_version=DyLibSymbol(stdhandle,"libvlc_get_version")
libvlc_new =DyLibSymbol(stdhandle,"libvlc_new")
libvlc_release=DyLibSymbol(stdhandle,"libvlc_release")
libvlc_media_new_location=DyLibSymbol(stdhandle,"libvlc_media_new_location")
libvlc_media_new_path=DyLibSymbol(stdhandle,"libvlc_media_new_path")
libvlc_media_player_new_from_media=DyLibSymbol(stdhandle,"libvlc_media_player_new_from_media")
libvlc_media_release=DyLibSymbol(stdhandle,"libvlc_media_release")
libvlc_media_player_set_hwnd=DyLibSymbol(stdhandle,"libvlc_media_player_set_hwnd")
libvlc_media_player_play=DyLibSymbol(stdhandle,"libvlc_media_player_play")
ibvlc_media_player_release=DyLibSymbol(stdhandle,"ibvlc_media_player_release")
libvlc_media_player_stop=DyLibSymbol(stdhandle,"libvlc_media_player_stop")
libvlc_audio_set_volume=DyLibSymbol(stdhandle,"libvlc_audio_set_volume")
libvlc_media_player_get_length=DyLibSymbol(stdhandle,"libvlc_media_player_get_length")
libvlc_media_player_get_time=DyLibSymbol(stdhandle,"libvlc_media_player_get_time")
libvlc_media_player_get_position=DyLibSymbol(stdhandle,"libvlc_media_player_get_position")
libvlc_video_set_marquee_string=DyLibSymbol(stdhandle,"libvlc_video_set_marquee_string")
libvlc_video_set_marquee_int=DyLibSymbol(stdhandle,"libvlc_video_set_marquee_string")
libvlc_video_set_callbacks = DyLibSymbol(stdhandle,"libvlc_video_set_callbacks")
libvlc_video_set_format= DyLibSymbol(stdhandle,"libvlc_video_set_format")
libvlc_video_get_height= DyLibSymbol(stdhandle,"libvlc_video_get_height")
libvlc_video_get_width= DyLibSymbol(stdhandle,"libvlc_video_get_width")
?*libvlc_get_version()


Dim Shared As UByte Ptr pixels

Sub display  (ByVal opaque As Any Ptr, ByVal picture As Any Ptr)
   ?"3"
End Sub
Sub UnLock_  (ByVal opaque As Any Ptr, ByVal picture As Any Ptr, ByVal planes As Any Const Ptr Ptr)
   ?"2"
End Sub

'get video frame
Function Lock_ (ByVal opaque As Any Ptr, ByVal planes As Any Ptr Ptr) As Any Ptr
   pixels = *planes
   ?"1"
   Return 0
End Function


Dim Shared As ZString*255 sPath
Dim As ZString Ptr vlc_argv(0 To ...) = { _
@"--no-xlib",  _
@"-q", _
@"--no-video-title-show", _
@"--quiet", _
@"--ignore-config", _
@"--vout", @"vmem", _
@"-I", @"dumy"}

Dim As Long argc = UBound(vlc_argv) + 1
Var vlc1 = libvlc_new(argc, @vlc_argv(0))
sPath = "C:\Ob\1.mkv"
Var m = libvlc_media_new_path(vlc1,sPath)
Var mp = libvlc_media_player_new_from_media(m)
libvlc_media_release(m)

libvlc_video_set_callbacks(mp, @Lock_, @UnLock_, @display, 0) ' set callbacks
libvlc_video_set_format(mp, "RV32", 1280, 720, 1280*4 ) 'set video set_format


libvlc_media_player_play(mp)

Sleep


Code closes with error
grindstone
Posts: 491
Joined: May 05, 2015 5:35
Location: Germany

Re: First test of libVLC include files.

Postby grindstone » Nov 06, 2015 17:12

@ WQ1980
I'm trying in vain to get a similar code working since 3 weeks. From the things I found out yet I can tell you this:
1. The "lock" - procedure in your C - code is incomplete. The first thing you have to do is to lock a mutex.
2. Referring to the description (as far as I understood it) "**p_pixels" is a pointer to an array of 1 or 3 pointers which each points to something called "pixel plane". You have to allocate the memory for these "pixel planes", write the pointer(s) into a pointer array and then write the pointer to this array to "*p_pixels".
This is the piece of code from the libVLC - source that calls the "lock" - callback (in the module "modules\video_output\vmem.c"):

Code: Select all

static int Lock(picture_t *picture)
{
    picture_sys_t *picsys = picture->p_sys;
    vout_display_sys_t *sys = picsys->sys;
    void *planes[PICTURE_PLANE_MAX];

    picsys->id = sys->lock(sys->opaque, planes);

    for (int i = 0; i < picture->i_planes; i++)
        picture->p[i].p_pixels = planes[i];

    return VLC_SUCCESS;
}
where "sys->lock" is the pointer to the "lock" - callback routine and PICTURE_PLANE_MAX has a value of 5.

This is my latest attempt of the lock - callback routine, and for my understanding it should work, but the program continues crashing after returning from the callback.

Code: Select all

Dim Shared As Any Ptr render
render = MutexCreate

Dim Shared As Any Ptr plane(4)
plane(0) = Allocate(720*576*4)
plane(1) = Allocate(720*576*4)
plane(2) = Allocate(720*576*4)
plane(3) = Allocate(720*576*4)
plane(4) = Allocate(720*576*4)

Function _lock(opaque As Any Ptr, planes As Any Ptr Ptr) As Any Ptr
   
    MutexLock render
     
    *planes = @plane(0)
    ? planes
    ? @plane(0)
    ? plane(0)
    ?"lock OK"   
    Return 0
End Function
WQ1980
Posts: 32
Joined: Sep 25, 2015 12:04
Location: Russia

Re: First test of libVLC include files.

Postby WQ1980 » Nov 06, 2015 20:54

@grindstone
C++ code example works

My litle C++ "wrapper" VLC video frame -> FB image
dll + bas file example
http://www.mediafire.com/download/0hr9cducjoqxoo6/VLCex.zip
(libvlc.dll, libvlccore.dll and folder "plugins" from VLC folder -> code folder)

Yes, but would like to only FB code, so uncomfortable(
grindstone
Posts: 491
Joined: May 05, 2015 5:35
Location: Germany

Re: First test of libVLC include files.

Postby grindstone » Nov 06, 2015 22:11

Alas, I'm using libVLC version 2.2.1 here, and it seems to be incompatible to your wrapper.
grindstone
Posts: 491
Joined: May 05, 2015 5:35
Location: Germany

Re: First test of libVLC include files.

Postby grindstone » Nov 08, 2015 13:30

Hello guys!

Finally I made it work, although I didn't manage to make it display the correct aspect ratio yet.

Code: Select all

#Include "vlc/vlc.bi"

Dim As libvlc_instance_t Ptr vlcInstance
Dim As libvlc_media_player_t Ptr mp
Dim As libvlc_media_t Ptr media
Dim As String vmem_options, myMediaFile
Dim Shared As Any Ptr imgbuffer, pixbuffer

ScreenRes(1124,676,32)
imgbuffer = ImageCreate(1024,576,RGB(100,100,100),32)

Sub prepareRender Cdecl (p_video_data As Any Ptr, pp_pixel_buffer As UByte Ptr Ptr, size As Integer)
      
   If pixbuffer = 0 Then
      pixbuffer = Callocate(size)
   EndIf
   
   *pp_pixel_buffer = pixbuffer

End Sub

Function handleStream Cdecl (p_video_data As Any Ptr, p_pixel_buffer As uint8_t Ptr, _
                  _width As Integer, _height As Integer, pixel_pitch As Integer, _
                  size As Integer, pts As int64_t) As Any Ptr

Dim As UByte Ptr pp, ip
Dim As Integer imgheight, imgwidth, imgbpp, imgpitch, imgsize, y
ImageInfo imgbuffer,imgheight,imgwidth, imgbpp, imgpitch, ip, imgsize
pp = Cast(UByte Ptr,p_pixel_buffer)

For y = 0 To _height - 1
   ImageConvertRow(@pp[ y * size / _height],  24, @ip[ y * imgpitch ], imgbpp*8, _width,0)
Next
   
Return 0

End Function

'#######################################################################################

SetEnviron ("VLC_PLUGIN_PATH=e:\vlc-2_2_1\plugins") 'replace with your plugin path

vmem_options = "#transcode{vcodec=RV24 }:smem{video-postrender-callback=" + Str(@handleStream) + "%lld," _
               + "video-prerender-callback=" + Str(@prepareRender) + "%lld }"
 
Dim As Any Ptr vlc_args(0 To ...) = { @"--verbose=3", _
                                      @"--sout",  _
                                StrPtr(vmem_options) }     
                                     
vlcInstance = libvlc_new (UBound(vlc_args) + 1, @vlc_args(0))       
myMediaFile = "C:\videos\myVideo.mpg"
media = libvlc_media_new_path(vlcInstance, myMediaFile)
mp = libvlc_media_player_new_from_media(media)
libvlc_media_player_play(mp)
libvlc_media_release(media)

Do
   ScreenLock
   Put (30,10),imgbuffer,PSet
   ScreenUnlock
   Sleep 1
Loop Until InKey = " "

libvlc_media_player_stop(mp)
libvlc_media_player_release(mp)
libvlc_release(vlcInstance)
DeAllocate pixbuffer
ImageDestroy imgbuffer
WQ1980
Posts: 32
Joined: Sep 25, 2015 12:04
Location: Russia

Re: First test of libVLC include files.

Postby WQ1980 » Nov 08, 2015 18:13

@grindstone
I downloaded VLC http://get.videolan.org/vlc/2.2.1/win32 ... 1-win32.7z
Unpacked and added VLCex.dll and test_pix.bas... Working...



My version VLC 2.1.5
Your code works in 2.1.5 and not work in VLC 2.2.1 )))

...correct aspect ratio...

I think you can add in vmem options video width and video height:

Code: Select all

vmem_options = "#transcode{vcodec=RV24,width=1280,height=720}:smem{video-postrender-callback=" + Str(@handleStream) + "%lld," _
+ "video-prerender-callback=" + Str(@prepareRender) + "%lld }"
grindstone
Posts: 491
Joined: May 05, 2015 5:35
Location: Germany

Re: First test of libVLC include files.

Postby grindstone » Nov 09, 2015 9:36

@WQ1980:
Thank you for that hint. It works perfect. The video can even be zoomed this way. :-)

The function "libvlc_video_set_aspect_ratio" is ignored as well as "libvlc_video_set_format".

Your code works in 2.1.5 and not work in VLC 2.2.1 )))
Very strange... My libVLC version is definitely 2.2.1 and it works. At libVlc the .dlls and the plugins must always be of the same version. Maybe this causes the problems.

Regards
grindstone
JohnK
Posts: 279
Joined: Sep 01, 2005 5:20
Location: Earth, usually
Contact:

Re: First test of libVLC include files.

Postby JohnK » Mar 20, 2016 21:14

[Edit] Under FreeBASIC-1.05.0-win64 if you get

C:\FreeBASIC-1.05.0-win64\fbc -s console "vlc_example.bas"
C:\FreeBASIC-1.05.0-win64\bin\win64\ld.exe: skipping incompatible ./libvlc.dll when searching for -lvlc
C:\FreeBASIC-1.05.0-win64\bin\win64\ld.exe: skipping incompatible ./libvlc.dll when searching for -lvlc
C:\FreeBASIC-1.05.0-win64\bin\win64\ld.exe: cannot find -lvlc

you need to download the 64-bit distribution of VCL, (https://www.videolan.org/vlc/)
I put all .dll and the "plugins" folder in the same folder as the fb exe then it runs
D.J.Peters
Posts: 7419
Joined: May 28, 2005 3:28

Re: First test of libVLC include files.

Postby D.J.Peters » Oct 28, 2017 3:31

First I like libVLC I can get all pixels from any video media (file, DVD, network stream, USB camera ...)

But there is one issue with this fine free library.

To get decoded video frames as FBImage or directly to the FBGFX screen
you must setup the pixel format and size of the media where you like to grab the pixels from
then you have to setup some callbacks to get the decoded frames

than you are ready to play the media ...

This worlks without any problems but wait how do you setup the size (before you can play) if you don't know the resolution ?

So far I know before the player begins to play (decoding the first frame)
there isn't a way to get the resolution from the media object !

So you have to setup a format callback and create a dummy player instance.
Than you start playing and the format callback are called and we got the size of the video.
Now we can stop the dummy player and release it.

After we got width and height of the media we can create a FBGFX screen or an fb image for the rendering.

Joshy

here are the right steps:

Code: Select all

#include once "vlc/vlc.bi"

dim shared as integer frame
dim shared as long pause,videoW,videoH,gotFormat

' we need this callback to get the dimension of the media !?!
function formatCB cdecl(byval userdata as any ptr ptr, _
                        byval chroma as zstring ptr, _
                        byval w as ulong ptr, _
                        byval h as ulong ptr, _
                        byval pitches as ulong ptr, _
                        byval lines as ulong ptr) as long
  videoW=*w : videoH=*h : gotFormat=1
  return 0
end function

function lockCB cdecl (byval image as any ptr, byval pPlanes as any ptr ptr) as any ptr
  if (image=0) then ' we use the the FBGFX screen as target so we must lock it
    screenlock : pPlanes[0]=screenptr() ' get the address of the screen pixel buffer
  else ' we use any RGB image get the address of the pixel buffer
    Imageinfo(image,,,,,pPlanes[0])
  end if
  return 0
end function

sub unlockCB cdecl (byval image as any ptr, byval pPicture as any ptr, byval pPlanes as any const ptr ptr)
  if (image=0) then screenunlock ' if we use the FBGFX screen we must unlock it
end sub

sub displayCB cdecl (byval image as any ptr, byval pPicture as any ptr)
  ' ignore it in pause mode
  if pause then return
  ' count the new frame
  frame+=1
  ' if we use the screen all rendering are done and we can go out here 
  if (image=0) then return
  ' use the decoded image for what ever you need here
  ' I blit it on screen
  put (0,0),image,PSET
end sub

'
' main
'
' set your video url here
dim as zstring ptr VIDEO = @"./HAL_9000_I_m_sorry_Dave_I_m_afraid_I_can_t_do_that.mp4"

' this image or the screen becomes the target of the video decoder
dim as any ptr image

' in my test case the video are in the same folder as the exe
chdir exepath()

' I don't like the debug output of libVLC so I try to disable them
SetEnviron "VLC_VERBOSE=-1"

' init the lib
var instance = libvlc_new (0,NULL)
' create the media object from path/url
var media = libvlc_media_new_path(instance, VIDEO)

' create a player onyl to get the format of the video !!!
var player = libvlc_media_player_new_from_media(media)

' set the callback to retreive the format
libvlc_video_set_format_callbacks(player,@formatCB,0)

' to enable the formatCB we have to setup the other callbacks also
libvlc_video_set_callbacks(player, @lockCB, @unlockCB, @displayCB, image)

' start the player to get the video resolution
var result = libvlc_media_player_play(player)
if (result<>0) then
  print "error: play get format ! ..."
  libvlc_media_player_release(player)
  libvlc_release(instance)
  beep : sleep : end 2
end if

' we must wait for the FormatCB here
while gotFormat=0 andalso inkey()=""
  sleep 10
wend 

' now we got the video resolution and we can strop and relase our dummy player :-)
libvlc_media_player_stop(player) : sleep 500,1 ' give it a litle bit time to stop
libvlc_media_player_release(player) ' release it


' here I use the half of the size (why not)
dim as integer SCR_W=videoW\2
dim as integer SCR_H=videoH\2
screenres SCR_W,SCR_H,32

' we use an image here (if you comment it out the screen will be used)
image=imagecreate(SCR_W, SCR_H)

dim as integer frameW,frameH,frameP
if image=0 then ' we use the screen get current size and pitch
  screeninfo frameW,frameH,,,frameP
else ' we use an image get size and pitch from image
  imageinfo image,frameW,frameH,,frameP
end if

' now we have to create a new player object to play the video
player = libvlc_media_player_new_from_media(media)

' set our format (from screen or image)
libvlc_video_set_format(player,@"BGRA", frameW, frameH, frameP)
' set the decoder callbacks
libvlc_video_set_callbacks(player, @lockCB, @unlockCB, @displayCB, image)
' start to play
result = libvlc_media_player_play(player)
if (result<>0) then
  print "error: can't play ! ..."
  libvlc_media_player_release(player)
  libvlc_release(instance)
  beep : sleep : end 2
end if

' only for testing: get time of the media in ms
'var milliseconds = libvlc_media_get_duration(media)
'while milliseconds=-1 andalso inkey()=""
'  windowtitle "warning: can't get duration of the media !"
'  sleep 10 ' don't eat all CPU cycles here
'  milliseconds = libvlc_media_get_duration(media)
'wend
'windowtitle "seconds: " &  milliseconds/1000

' we not longer need the media
libvlc_media_release(media)


'result = libvlc_media_player_will_play(player)
'while result=0
'  beep
'  result = libvlc_media_player_will_play(player)
'wend

' optionmal set the audio volume 0-100%
libvlc_audio_set_volume(player,100)

dim as libvlc_state_t PlayerState

var key=asc(inkey())

' play the the video or press [ESCAPE] to interrupt
while key<>27 and (PlayerState<>libvlc_Ended)
  ' only for testing press space to togle the pause mode
  if key=32 then pause=1-pause : libvlc_media_player_set_pause(player,pause)
  ' get the curreent state of the player 
  PlayerState = libvlc_media_player_get_state(player)
  windowtitle "frame: " & frame & " player state: " & iif(PlayerState=3,"playing ...",iif(PlayerState=4,"paused ...","ended"))
  key=asc(inkey())
  sleep 100
wend 
' if the vide was interrupted stop it manualy
if (PlayerState<>libvlc_Ended) then libvlc_media_player_stop(player) : sleep 500,1 ' give it a bit time to end all callbacks
' free it
libvlc_media_player_release(player)
' close it
libvlc_release(instance)
JohnK
Posts: 279
Joined: Sep 01, 2005 5:20
Location: Earth, usually
Contact:

Re: First test of libVLC include files.

Postby JohnK » Oct 29, 2017 5:33

Maybe I am missing something... don't you call
libvlc_video_get_width
libvlc_video_get_height
libvlc_video_get_size
and then you can set your FB image correctly?
D.J.Peters
Posts: 7419
Joined: May 28, 2005 3:28

Re: First test of libVLC include files.

Postby D.J.Peters » Oct 30, 2017 13:02

libvlc_video_get_XYZ works only after you play the media
(after the first frame are decoded the player knows width and height but not before)

Joshy

Return to “Libraries”

Who is online

Users browsing this forum: No registered users and 1 guest