unit main;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Menus, ExtCtrls;

type
  TForm1 = class(TForm)
    MainMenu1: TMainMenu;
    File1: TMenuItem;
    Open1: TMenuItem;
    OpenDialog1: TOpenDialog;
    Panel1: TPanel;
    procedure Open1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

uses ActiveX, DirectShow9;

var
  pGraphBuilder        : IGraphBuilder         = nil;
  pCaptureGraphBuilder2: ICaptureGraphBuilder2 = nil;
  pSourceFile          : IBaseFilter           = nil; 
  pMediaControl        : IMediaControl         = nil;
  pMediaEvent          : IMediaEvent           = nil;
  pVideoWindow         : IVideoWindow          = nil;

procedure TForm1.FormCreate(Sender: TObject);
begin
  CoInitialize(nil);
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  CoUninitialize;
end;

procedure TForm1.Open1Click(Sender: TObject);
var
  hr: HRESULT;
  evCode: Integer;
begin
  if not OpenDialog1.Execute then exit;

  if Assigned(pGraphBuilder) then pGraphBuilder := nil;
  if Assigned(pCaptureGraphBuilder2) then pCaptureGraphBuilder2 := nil;
  if Assigned(pSourceFile) then pSourceFile := nil;
  if Assigned(pMediaControl) then pMediaControl := nil;
  if Assigned(pMediaEvent) then pMediaEvent := nil;
  if Assigned(pVideoWindow) then pVideoWindow := nil;

  hr := CoCreateInstance(CLSID_FilterGraph, nil, CLSCTX_INPROC, IID_IGraphBuilder, pGraphBuilder);
  if FAILED(hr) then begin
    ShowMessage('Не удается создать GraphBuilder');
    exit;
  end;

  hr := CoCreateInstance(CLSID_CaptureGraphBuilder2, nil, CLSCTX_INPROC, IID_ICaptureGraphBuilder2, pCaptureGraphBuilder2);
  if FAILED(hr) then begin
    ShowMessage('Не удается создать CaptureGraphBuilder');
    exit;
  end;

  hr := pCaptureGraphBuilder2.SetFiltergraph(pGraphBuilder);
  if FAILED(hr) then begin
    ShowMessage('Не удается установить фильтр');
    exit;
  end;


  hr := pGraphBuilder.AddSourceFilter(StringToOleStr(OpenDialog1.FileName), '', pSourceFile);
  if FAILED(hr) then begin
    ShowMessage('Не удается добавить к графу фильт источника');
    exit;
  end;

  hr := pGraphBuilder.QueryInterface(IID_IMediaControl, pMediaControl);
  if FAILED(hr) then begin
    ShowMessage('Не удается получить интерфейс IMediaControl');
    exit;
  end;

  hr := pGraphBuilder.QueryInterface(IID_IMediaEvent, pMediaEvent);
  if FAILED(hr) then begin
    ShowMessage('Не удается получить интерфейс IMEdiaEvent');
    exit;
  end;

  pCaptureGraphBuilder2.RenderStream(nil, nil, pSourceFile as IBaseFilter, nil, nil);

  hr := pGraphBuilder.QueryInterface(IID_IVideoWindow, pVideoWindow);
  if FAILED(hr) then begin
    ShowMessage('Не удается получить IVideoWindow');
    exit;
  end;

  hr := pVideoWindow.put_Owner(Panel1.Handle);
  if FAILED(hr) then begin
    ShowMessage('Не удается установить VideoWindow');
    exit;
  end;

  hr := pVideoWindow.put_WindowStyle(WS_CHILD);
  if FAILED(hr) then begin
    ShowMessage('Не удается стиль VideoWindow');
    exit;
  end;

  pVideoWindow.put_Left(0);
  pVideoWindow.put_Width(Panel1.Width);
  pVideoWindow.put_Top(0);
  pVideoWindow.put_Height(Panel1.Height);

  pMediaControl.Run;

  pMediaEvent.WaitForCompletion(INFINITE, evCode);

end;


end.
