i've a liddle Problem: How can I define the chroma without knowing the videorect? If I use libvlc_video_get_width before ore direct after libvlc_media_player_play it always returns 0 and if i use it after libvlc_media_player_play, it doesn't change anything. If I use -1 for width and hight I get a black window.
Thanks
Actual Code:
Code: Select all
unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
ExtCtrls, vlc;
type
{ TForm1 }
TForm1 = class(TForm)
Button1: TButton;
Image1: TImage;
Timer1: TTimer;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Timer1StartTimer(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
private
{ private declarations }
public
{ public declarations }
end;
function lock(data, pixels: Pointer):Pointer; cdecl;
function unlock(data, id, pixels: Pointer):Pointer; cdecl;
var
Form1: TForm1;
VLCctx:tVLCcontext;
implementation
{$R *.lfm}
{ TForm1 }
procedure TForm1.Button1Click(Sender: TObject);
var w,h:Integer;
begin
// create new vlc media from file
vlcMedia := libvlc_media_new_path(vlcInstance, '*********************************');
// create new vlc media player
vlcMediaPlayer := libvlc_media_player_new_from_media(vlcMedia);
// now no need the vlc media, free it
libvlc_media_release(vlcMedia);
//libvlc_video_set_callbacks(vlcMediaPlayer, @lock, @unlock, nil, @VLCctx);
libvlc_video_set_callbacks(vlcMediaPlayer, @lock, nil, nil, @VLCctx);
//ShowMessage(IntToStr(libvlc_video_get_width(vlcMediaPlayer)));
w:=320;
h:=240;
//libvlc_video_set_format(vlcMediaPlayer,'RGBA',w,h,w*4);
libvlc_video_set_format(vlcMediaPlayer,'RGBA',w,h,w*4);
// play media
libvlc_media_player_play(vlcMediaPlayer);
Timer1.Enabled:=true;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
CreateVLCPlayer();
end;
procedure TForm1.Timer1StartTimer(Sender: TObject);
begin
end;
procedure TForm1.Timer1Timer(Sender: TObject);
var ix,iy,c:integer;
begin
for ix:=0 to 320 do
for iy:=0 to 240 do
begin
{c:= VLCctx.vlcpixels[ix*4+0+iy*320*4];
c:=c+VLCctx.vlcpixels[ix*4+1+iy*320*4]*256;
c:=c+VLCctx.vlcpixels[ix*4+2+iy*320*4]*256*256; }
c:= VLCctx.vlcpixels[ix*4+0+iy*320*4];
c:=c+VLCctx.vlcpixels[ix*4+1+iy*320*4]*256;
c:=c+VLCctx.vlcpixels[ix*4+2+iy*320*4]*256*256;
Image1.Canvas.Pixels[ix,iy]:=TColor(c);
end;
end;
function lock(data, pixels: Pointer):Pointer; cdecl;
var w,h:Integer;
begin
w:=libvlc_video_get_width(vlcMediaPlayer);
h:=libvlc_video_get_height(vlcMediaPlayer);
Setlength(VLCctx.vlcpixels,w*h*4);
libvlc_video_set_format(vlcMediaPlayer,'RGBA',w,h,w*4);
Pointer(pixels^) := @(VLCctx.vlcpixels[0]);
Result:=nil;
end;
function unlock(data, id, pixels: Pointer):Pointer; cdecl;
begin
//noch leer
end;
end.
Code: Select all
unit vlc;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Dialogs{$IFDEF WINDOWS}, Windows{$ENDIF};
type
plibvlc_instance_t = type Pointer;
plibvlc_media_player_t = type Pointer;
plibvlc_media_t = type Pointer;
tVLC_lock = function(data, pixels: Pointer):Pointer; cdecl;
tVLC_unlock = function(data, id, pixels: Pointer):Pointer; cdecl;
pVLC_lock = ^tVLC_lock;
pVLC_unlock = ^tVLC_unlock;
TRGBTripleArray = array of TRGBTriple;
pRGBTripleArray = ^TRGBTripleArray;
tVLCcontext = record
VLCpixels:array of byte;
//VLCpixels:tRGBTripleArray
end;
var
libvlc_media_new_path : function(p_instance : Plibvlc_instance_t; path : PAnsiChar) : Plibvlc_media_t; cdecl;
libvlc_media_new_location : function(p_instance : plibvlc_instance_t; psz_mrl : PAnsiChar) : Plibvlc_media_t; cdecl;
libvlc_media_player_new_from_media : function(p_media : Plibvlc_media_t) : Plibvlc_media_player_t; cdecl;
libvlc_media_player_set_hwnd : procedure(p_media_player : Plibvlc_media_player_t; drawable : Pointer); cdecl;
libvlc_media_player_play : procedure(p_media_player : Plibvlc_media_player_t); cdecl;
libvlc_media_player_stop : procedure(p_media_player : Plibvlc_media_player_t); cdecl;
libvlc_media_player_release : procedure(p_media_player : Plibvlc_media_player_t); cdecl;
libvlc_media_player_is_playing : function(p_media_player : Plibvlc_media_player_t) : Integer; cdecl;
libvlc_media_release : procedure(p_media : Plibvlc_media_t); cdecl;
libvlc_new : function(argc : Integer; argv : PAnsiChar) : Plibvlc_instance_t; cdecl;
libvlc_release : procedure(p_instance : Plibvlc_instance_t); cdecl;
libvlc_video_set_callbacks : procedure(p_media_player : Plibvlc_media_player_t; lock : tVLC_lock; unlock: tVLC_unlock; display, opaque: Pointer); cdecl;
// libvlc_video_set_callbacks : procedure(p_media_player : Plibvlc_media_player_t; lock, unlock: Pointer; display, opaque: Pointer); cdecl;
libvlc_video_get_width : function(p_media_player : Plibvlc_media_player_t) : Integer; cdecl;
libvlc_video_get_height : function(p_media_player : Plibvlc_media_player_t) : Integer; cdecl;
libvlc_video_get_size : function(p_media_player : Plibvlc_media_player_t; num:Integer; px, py:Pointer) : Integer; cdecl;
libvlc_video_set_format : procedure(p_media_player : Plibvlc_media_player_t; chroma:String; width, height, pitch: Integer) cdecl;
vlcLib: integer;
vlcInstance: plibvlc_instance_t;
vlcMedia: plibvlc_media_t;
vlcMediaPlayer: plibvlc_media_player_t;
procedure CreateVLCPlayer;
implementation
// -----------------------------------------------------------------------------
// Read registry to get VLC installation path
// -----------------------------------------------------------------------------
function GetVLCLibPath: String;
begin
if DirectoryExists('C:\Program Files (x86)\VideoLAN\VLC') then result:='C:\Program Files (x86)\VideoLAN\VLC';
if DirectoryExists('C:\Program Files\VideoLAN\VLC') then result:='C:\Program Files\VideoLAN\VLC';
end;
// -----------------------------------------------------------------------------
// Load libvlc library into memory
// -----------------------------------------------------------------------------
function LoadVLCLibrary(APath: string): integer;
begin
Result := LoadLibrary(PChar(APath + '\libvlccore.dll'));
//if not FileExists(PChar(APath + '\libvlc.dll')) then ShowMessage('ah');
Result := LoadLibrary(PChar(APath + '\libvlc.dll'));
//ShowMessage(PChar(APath + '\libvlc.dll') + ' :' + IntToStr(Result));
end;
// -----------------------------------------------------------------------------
function GetVLCProcAddress(vlcHandle: integer; var addr: Pointer; procName: string; failedList: TStringList): integer;
begin
addr := GetProcAddress(vlcHandle, PChar(procName));
if Assigned(addr) then Result := 0
else begin
if Assigned(failedList) then failedList.Add(procName);
Result := -1;
end;
end;
// -----------------------------------------------------------------------------
// Get address of libvlc functions
// -----------------------------------------------------------------------------
function LoadVLCFunctions(vlcHandle: integer; failedList: TStringList): Boolean;
begin
GetVLCProcAddress(vlcHandle, pointer(libvlc_new), 'libvlc_new', failedList);
GetVLCProcAddress(vlcHandle, pointer(libvlc_media_new_location), 'libvlc_media_new_location', failedList);
GetVLCProcAddress(vlcHandle, pointer(libvlc_media_player_new_from_media), 'libvlc_media_player_new_from_media', failedList);
GetVLCProcAddress(vlcHandle, pointer(libvlc_media_release), 'libvlc_media_release', failedList);
GetVLCProcAddress(vlcHandle, pointer(libvlc_media_player_set_hwnd), 'libvlc_media_player_set_hwnd', failedList);
GetVLCProcAddress(vlcHandle, pointer(libvlc_media_player_play), 'libvlc_media_player_play', failedList);
GetVLCProcAddress(vlcHandle, pointer(libvlc_media_player_stop), 'libvlc_media_player_stop', failedList);
GetVLCProcAddress(vlcHandle, pointer(libvlc_media_player_release), 'libvlc_media_player_release', failedList);
GetVLCProcAddress(vlcHandle, pointer(libvlc_release), 'libvlc_release', failedList);
GetVLCProcAddress(vlcHandle, pointer(libvlc_media_player_is_playing), 'libvlc_media_player_is_playing', failedList);
GetVLCProcAddress(vlcHandle, pointer(libvlc_media_new_path), 'libvlc_media_new_path', failedList);
GetVLCProcAddress(vlcHandle, pointer(libvlc_video_set_callbacks), 'libvlc_video_set_callbacks', failedList);
GetVLCProcAddress(vlcHandle, pointer(libvlc_video_get_width), 'libvlc_video_get_width', failedList);
GetVLCProcAddress(vlcHandle, pointer(libvlc_video_get_height), 'libvlc_video_get_height', failedList);
GetVLCProcAddress(vlcHandle, pointer(libvlc_video_get_size), 'libvlc_video_get_size', failedList);
GetVLCProcAddress(vlcHandle, pointer(libvlc_video_set_format), 'libvlc_video_set_format', failedList);
// if all functions loaded, result is an empty list, otherwise result is a list of functions failed
Result := failedList.Count = 0;
end;
procedure CreateVLCPlayer;
var sL: TStringList;
begin
// load vlc library
vlclib := LoadVLCLibrary(GetVLCLibPath());
if vlclib = 0 then begin
Showmessage('Load vlc library failed');
Exit;
end;
// sL will contains list of functions fail to load
sL := TStringList.Create;
if not LoadVLCFunctions(vlclib, sL) then begin
Showmessage('Some functions failed to load : ' + #13#10 + sL.Text);
FreeLibrary(vlclib);
sL.Free;
Exit;
end;
sL.Free;
vlcInstance := libvlc_new(0, nil);
end;
end.