Threaded DirectX access for two cameras with Delphi XE3, shows the last one duplicated

134 Views Asked by At

I have an app working fine that accesses two different web cameras at once using DirectX selecting them with comboboxes. The probem is doing it in the main thread almost freezes both cameras and works veeeeeery bad. So, my idea was to run each camera stuff in a separate thread. The probem now is that the threaded version changes first image to the last setted camera.

Is it not possible to access cameras from separate threads? Im using also VFrames library from https://github.com/MakeMagazinDE/GRBLize/blob/master/VFrames.pas

type
    TCamAThrd = class(TThread)
    private
        fVideoImage : TVideoImage;
        fVideoBitmap: TBitmap;
    protected
        constructor Create;
    public
        procedure OnNewVideoFrame(Sender : TObject; Width, Height: integer; DataPtr: pointer);
        procedure Execute; override;
end;

type
    TCamBThrd = class(TThread)
    private
        fVideoImageB : TVideoImage;
        fVideoBitmapB: TBitmap;
    protected
        constructor Create;
    public
        procedure OnNewVideoFrameB(Sender : TObject; Width, Height: integer; DataPtr: pointer);
        procedure Execute; override;
end;

constructor TCamAThrd.Create;
begin
    fVideoBitmap    := TBitmap.create;
    fVideoImage     := TVideoImage.Create;
    fVideoImage.OnNewVideoFrame := OnNewVideoFrame;
    Inherited Create(True);
end;

constructor TCamBThrd.Create;
begin
    fVideoBitmapB := TBitmap.create;
    fVideoImageB  := TVideoImage.Create;
    fVideoImageB.OnNewVideoFrame := OnNewVideoFrameB;
    Inherited Create(True);
end;

procedure TCamAThrd.Execute;
begin
    CoInitialize(nil);
    CamASelected := MainForm.ComboBox1.items[MainForm.ComboBox1.itemindex];
    fVideoImage.VideoStart(CamASelected);
end;

procedure TCamBThrd.Execute;
begin
    CoInitialize(nil);
    CamBSelected := MainForm.ComboBox2.items[MainForm.ComboBox2.itemindex];
    fVideoImageB.VideoStart(CamBSelected);
end;

procedure TCamAThrd.OnNewVideoFrame(Sender : TObject; Width, Height: integer; DataPtr: pointer);
begin
    fVideoImage.GetBitmap(fVideoBitmap);
    Synchronize(nil, procedure begin MainForm.Image1.picture.bitmap.assign(fVideoBitmap); end);
end;

procedure TCamBThrd.OnNewVideoFrameB(Sender : TObject; Width, Height: integer; DataPtr: pointer);
begin
    fVideoImageB.GetBitmap(fVideoBitmapB);
    Synchronize(nil, procedure begin MainForm.Image2.picture.bitmap.assign(fVideoBitmapB); end);
end;

I am trying to show two cameras in separated panels at once on screen. When I use threads, I only get the last set cam duplicated.

0

There are 0 best solutions below