Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program Test;
- {$APPTYPE CONSOLE}
- ////////////////////////////////////////////////////////////////////////////////
- uses
- SysUtils,
- ActiveX,
- Windows,
- DXSUtil,
- DirectShow9;
- ////////////////////////////////////////////////////////////////////////////////
- const
- CLSID_SourceFilter: TGUID = '{54A1835E-9A68-499E-B9ED-041B4EF9FB3E}'; // SANYO HD Camera Source Filter
- CLSID_SinkFilter: TGUID = '{36A5F770-FE4C-11CE-A8ED-00AA002FEAB5}'; // SANYO HD Camera Sink Filter
- ////////////////////////////////////////////////////////////////////////////////
- var
- Key: Char;
- i: Integer;
- g_pMC: IMediaControl;
- g_pGraph: IGraphBuilder;
- g_pCapture: ICaptureGraphBuilder2;
- hr: HRESULT;
- ////////////////////////////////////////////////////////////////////////////////
- function FindFilter(const FilterGUID: TGUID; out ppFilter: IBaseFilter): HRESULT;
- var
- hr: HRESULT;
- pSrc: IBaseFilter;
- pMoniker: IMoniker;
- pDevEnum: ICreateDevEnum;
- pClassEnum: IEnumMoniker;
- PropBag: IPropertyBag;
- Name: OleVariant;
- classID: TGUID;
- Fetched: ULONG;
- begin
- hr := S_OK;
- // Create the system device enumerator
- hr := CoCreateInstance(CLSID_SystemDeviceEnum, nil, CLSCTX_INPROC,
- IID_ICreateDevEnum, pDevEnum);
- if FAILED(hr) then begin
- WriteLn('Couldn''t create system enumerator! hr=0x%x', hr);
- end;
- // Create an enumerator for DirectShow filters
- if SUCCEEDED(hr) then begin
- hr := pDevEnum.CreateClassEnumerator(CLSID_LegacyAmFilterCategory, pClassEnum, 0);
- end;
- if FAILED(hr) then begin
- WriteLn('Couldn''t create class enumerator! hr=0x%x', hr);
- end;
- if SUCCEEDED(hr) then begin
- // If there are no enumerators for the requested type, then
- // CreateClassEnumerator will succeed, but pClassEnum will be NULL.
- if pClassEnum = nil then begin
- WriteLn('No filters found.');
- hr := E_FAIL;
- end;
- end;
- // Find filter
- if SUCCEEDED(hr) then begin
- while (pClassEnum.Next(1, pMoniker, @Fetched) = S_OK) do begin
- pMoniker.BindToStorage(nil, nil, IID_IPropertyBag, PropBag);
- if (PropBag.Read('CLSID', Name, nil) = S_OK) then begin
- if Name=GUIDToString(FilterGUID) then begin
- hr := pMoniker.BindToObject(nil, nil, IID_IBaseFilter, ppFilter);
- if (FAILED(hr)) then begin
- WriteLn('Couldn''t bind moniker to filter object! hr=0x%x', hr);
- end;
- end;
- end;
- pMoniker := nil;
- PropBag := nil;
- end;
- end;
- if ppFilter=nil then begin
- hr := E_FAIL;
- end;
- Result := hr;
- end;
- ////////////////////////////////////////////////////////////////////////////////
- function CloseInterfaces: HRESULT;
- var
- hr: HRESULT;
- begin
- Result := S_OK;
- end;
- ////////////////////////////////////////////////////////////////////////////////
- function GetInterfaces: HRESULT;
- var
- hr: HRESULT;
- begin
- // Create the filter graph
- hr := CoCreateInstance(CLSID_FilterGraph, nil, CLSCTX_INPROC,
- IID_IGraphBuilder, g_pGraph);
- if (FAILED(hr)) then begin
- Result := hr;
- end;
- // Create the capture graph builder
- hr := CoCreateInstance(CLSID_CaptureGraphBuilder2, nil, CLSCTX_INPROC,
- IID_ICaptureGraphBuilder2, g_pCapture);
- if FAILED(hr) then begin
- Result := hr;
- end;
- // Obtain interface for media control
- hr := g_pGraph.QueryInterface(IID_IMediaControl,g_pMC);
- if FAILED(hr) then begin
- Result := hr;
- end;
- Result := hr;
- end;
- ////////////////////////////////////////////////////////////////////////////////
- function RecordVideo: HRESULT;
- var
- hr: HRESULT;
- pSrcFilter: IBaseFilter;
- pSinkFilter: IBaseFilter;
- begin
- // Get DirectShow interfaces
- hr := GetInterfaces;
- if FAILED(hr) then begin
- WriteLn('Failed to get video interfaces! hr=0x%x', hr);
- Result := hr;
- end;
- // Attach the filter graph to the capture graph
- hr := g_pCapture.SetFiltergraph(g_pGraph);
- if FAILED(hr) then begin
- WriteLn('Failed to set capture filter graph! hr=0x%x', hr);
- Result := hr;
- end;
- // Use the system device enumerator and class enumerator to find
- // required filters
- hr := FindFilter(CLSID_SourceFilter, pSrcFilter);
- if FAILED(hr) then begin
- // Don't display a message because FindFilter will handle it
- Result := hr;
- end;
- hr := FindFilter(CLSID_SinkFilter, pSinkFilter);
- if FAILED(hr) then begin
- Result := hr;
- end;
- // Add source filter to our graph
- hr := g_pGraph.AddFilter(pSrcFilter, 'SANYO HD Camera Source');
- if FAILED(hr) then begin
- WriteLn('Couldn''t add the source filter to the graph! hr=0x%x', hr);
- Result := hr;
- end;
- // Add sink filter to graph
- hr := g_pGraph.AddFilter(pSinkFilter, 'SANYO HD Camera Sink');
- if FAILED(hr) then begin
- WriteLn('Couldn''t add the sink filter to the graph! hr=0x%x', hr);
- Result := hr;
- end;
- // Render the preview pin on the video capture filter
- // Use this instead of g_pGraph->RenderFile
- hr := g_pCapture.RenderStream(@PIN_CATEGORY_PREVIEW, nil,
- pSrcFilter, nil, pSinkFilter);
- CheckDSError(hr);
- if FAILED(hr) then begin
- WriteLn('Couldn''t render the video capture stream. hr=0x%x', hr);
- Result := hr;
- end;
- // Start previewing video data
- hr := g_pMC.Run;
- if FAILED(hr) then begin
- WriteLn('Couldn''t run the graph! hr=0x%x', hr);
- Result := hr;
- end;
- // Remember current state
- //g_psCurrent = Running;
- Result := S_OK;
- end;
- ////////////////////////////////////////////////////////////////////////////////
- begin
- CoInitialize(nil);
- // Create DirectShow graph and start recording
- hr := RecordVideo;
- if (FAILED (hr)) then begin
- CloseInterfaces;
- end;
- WriteLn('Press [Enter]...');
- Read(Key);
- //CheckDSError
- CoUninitialize;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement