Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 5.77 KB | None | 0 0
  1. program Test;
  2.  
  3. {$APPTYPE CONSOLE}
  4.  
  5. ////////////////////////////////////////////////////////////////////////////////
  6. uses
  7.   SysUtils,
  8.   ActiveX,
  9.   Windows,
  10.  
  11.   DXSUtil,
  12.   DirectShow9;
  13.  
  14. ////////////////////////////////////////////////////////////////////////////////
  15. const
  16.   CLSID_SourceFilter: TGUID = '{54A1835E-9A68-499E-B9ED-041B4EF9FB3E}'; // SANYO HD Camera Source Filter
  17.   CLSID_SinkFilter:   TGUID = '{36A5F770-FE4C-11CE-A8ED-00AA002FEAB5}'; // SANYO HD Camera Sink Filter
  18.  
  19. ////////////////////////////////////////////////////////////////////////////////
  20. var
  21.   Key: Char;
  22.   i: Integer;
  23.  
  24.   g_pMC: IMediaControl;
  25.   g_pGraph: IGraphBuilder;
  26.   g_pCapture: ICaptureGraphBuilder2;
  27.  
  28.   hr: HRESULT;
  29.  
  30. ////////////////////////////////////////////////////////////////////////////////
  31. function FindFilter(const FilterGUID: TGUID; out ppFilter: IBaseFilter): HRESULT;
  32. var
  33.   hr: HRESULT;
  34.   pSrc: IBaseFilter;
  35.   pMoniker: IMoniker;
  36.   pDevEnum: ICreateDevEnum;
  37.   pClassEnum: IEnumMoniker;
  38.   PropBag: IPropertyBag;
  39.   Name: OleVariant;
  40.   classID: TGUID;
  41.   Fetched: ULONG;
  42.  
  43. begin
  44.   hr := S_OK;
  45.  
  46.   // Create the system device enumerator
  47.   hr := CoCreateInstance(CLSID_SystemDeviceEnum, nil, CLSCTX_INPROC,
  48.     IID_ICreateDevEnum, pDevEnum);
  49.  
  50.   if FAILED(hr) then begin
  51.     WriteLn('Couldn''t create system enumerator! hr=0x%x', hr);
  52.   end;
  53.  
  54.   // Create an enumerator for DirectShow filters
  55.   if SUCCEEDED(hr) then begin
  56.     hr := pDevEnum.CreateClassEnumerator(CLSID_LegacyAmFilterCategory, pClassEnum, 0);
  57.   end;
  58.  
  59.   if FAILED(hr) then begin
  60.     WriteLn('Couldn''t create class enumerator! hr=0x%x', hr);
  61.   end;
  62.  
  63.   if SUCCEEDED(hr) then begin
  64.     // If there are no enumerators for the requested type, then
  65.         // CreateClassEnumerator will succeed, but pClassEnum will be NULL.
  66.     if pClassEnum = nil then begin
  67.       WriteLn('No filters found.');
  68.       hr := E_FAIL;
  69.     end;
  70.   end;
  71.  
  72.   // Find filter
  73.   if SUCCEEDED(hr) then begin
  74.     while (pClassEnum.Next(1, pMoniker, @Fetched) = S_OK) do begin
  75.       pMoniker.BindToStorage(nil, nil, IID_IPropertyBag, PropBag);
  76.       if (PropBag.Read('CLSID', Name, nil) = S_OK) then begin
  77.         if Name=GUIDToString(FilterGUID) then begin
  78.           hr := pMoniker.BindToObject(nil, nil, IID_IBaseFilter, ppFilter);
  79.           if (FAILED(hr)) then begin
  80.             WriteLn('Couldn''t bind moniker to filter object!  hr=0x%x', hr);
  81.           end;
  82.         end;
  83.       end;
  84.       pMoniker := nil;
  85.       PropBag := nil;
  86.     end;
  87.   end;
  88.   if ppFilter=nil then begin
  89.     hr := E_FAIL;
  90.   end;
  91.   Result := hr;
  92. end;
  93.  
  94. ////////////////////////////////////////////////////////////////////////////////
  95. function CloseInterfaces: HRESULT;
  96. var
  97.   hr: HRESULT;
  98. begin
  99.   Result := S_OK;
  100. end;
  101.  
  102. ////////////////////////////////////////////////////////////////////////////////
  103. function GetInterfaces: HRESULT;
  104. var
  105.   hr: HRESULT;
  106. begin
  107.   // Create the filter graph
  108.   hr := CoCreateInstance(CLSID_FilterGraph, nil, CLSCTX_INPROC,
  109.     IID_IGraphBuilder, g_pGraph);
  110.   if (FAILED(hr)) then begin
  111.     Result := hr;
  112.   end;
  113.  
  114.   // Create the capture graph builder
  115.   hr := CoCreateInstance(CLSID_CaptureGraphBuilder2, nil, CLSCTX_INPROC,
  116.     IID_ICaptureGraphBuilder2, g_pCapture);
  117.   if FAILED(hr) then begin
  118.     Result := hr;
  119.   end;
  120.  
  121.   // Obtain interface for media control
  122.   hr := g_pGraph.QueryInterface(IID_IMediaControl,g_pMC);
  123.   if FAILED(hr) then begin
  124.     Result := hr;
  125.   end;
  126.  
  127.   Result := hr;
  128. end;
  129.  
  130. ////////////////////////////////////////////////////////////////////////////////
  131. function RecordVideo: HRESULT;
  132. var
  133.   hr: HRESULT;
  134.   pSrcFilter: IBaseFilter;
  135.   pSinkFilter: IBaseFilter;
  136.  
  137. begin
  138.   // Get DirectShow interfaces
  139.   hr := GetInterfaces;
  140.   if FAILED(hr) then begin
  141.     WriteLn('Failed to get video interfaces!  hr=0x%x', hr);
  142.     Result := hr;
  143.   end;
  144.  
  145.   // Attach the filter graph to the capture graph
  146.   hr := g_pCapture.SetFiltergraph(g_pGraph);
  147.   if FAILED(hr) then begin
  148.     WriteLn('Failed to set capture filter graph!  hr=0x%x', hr);
  149.     Result := hr;
  150.   end;
  151.  
  152.   // Use the system device enumerator and class enumerator to find
  153.   // required filters
  154.   hr := FindFilter(CLSID_SourceFilter, pSrcFilter);
  155.   if FAILED(hr) then begin
  156.     // Don't display a message because FindFilter will handle it
  157.     Result := hr;
  158.   end;
  159.  
  160.   hr := FindFilter(CLSID_SinkFilter, pSinkFilter);
  161.   if FAILED(hr) then begin
  162.     Result := hr;
  163.   end;
  164.  
  165.   // Add source filter to our graph
  166.   hr := g_pGraph.AddFilter(pSrcFilter, 'SANYO HD Camera Source');
  167.   if FAILED(hr) then begin
  168.     WriteLn('Couldn''t add the source filter to the graph!  hr=0x%x', hr);
  169.     Result := hr;
  170.   end;
  171.  
  172.   // Add sink filter to graph
  173.   hr := g_pGraph.AddFilter(pSinkFilter, 'SANYO HD Camera Sink');
  174.   if FAILED(hr) then begin
  175.     WriteLn('Couldn''t add the sink filter to the graph!  hr=0x%x', hr);
  176.     Result := hr;
  177.   end;
  178.  
  179.   // Render the preview pin on the video capture filter
  180.   // Use this instead of g_pGraph->RenderFile
  181.   hr := g_pCapture.RenderStream(@PIN_CATEGORY_PREVIEW, nil,
  182.     pSrcFilter, nil, pSinkFilter);
  183.   CheckDSError(hr);
  184.   if FAILED(hr) then begin
  185.     WriteLn('Couldn''t render the video capture stream.  hr=0x%x', hr);
  186.     Result := hr;
  187.   end;
  188.  
  189.   // Start previewing video data
  190.   hr := g_pMC.Run;
  191.   if FAILED(hr) then begin
  192.     WriteLn('Couldn''t run the graph!  hr=0x%x', hr);
  193.     Result := hr;
  194.   end;
  195.  
  196.   // Remember current state
  197.   //g_psCurrent = Running;
  198.  
  199.   Result := S_OK;
  200. end;
  201.  
  202. ////////////////////////////////////////////////////////////////////////////////
  203. begin
  204.   CoInitialize(nil);
  205.  
  206.   // Create DirectShow graph and start recording
  207.   hr := RecordVideo;
  208.   if (FAILED (hr)) then begin
  209.     CloseInterfaces;
  210.   end;
  211.  
  212.   WriteLn('Press [Enter]...');
  213.   Read(Key);
  214.  
  215.   //CheckDSError
  216.  
  217.   CoUninitialize;
  218. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement