home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Game Programming for Teens / VBGPFT.cdr / DirectX8 / dx8a_sdk.exe / samples / multimedia / directshow / editing / sampgrabcb / sampgrabcb.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-10-02  |  6.9 KB  |  255 lines

  1. //------------------------------------------------------------------------------
  2. // File: SampGrabCB.cpp
  3. //
  4. // Desc: DirectShow sample code - C++ console application demonstrating
  5. //       use of the IMediaDet interface to create a graph that contains a
  6. //       sample grabber filter.  It shows how to use the sample grabber 
  7. //       and a COM object callback to display information about media
  8. //       samples in a running video file.  
  9. //
  10. // Copyright (c) 1998 - 2000, Microsoft Corporation.  All rights reserved.
  11. //------------------------------------------------------------------------------
  12.  
  13.  
  14. #include "stdafx.h"
  15.  
  16. // Function prototypes
  17. //
  18. int GrabSamples( char * pFilename ); 
  19.  
  20.  
  21. // This semi-COM object is a callback COM object for the sample grabber
  22. //
  23. class CFakeCallback : public ISampleGrabberCB 
  24. {
  25. public:
  26.     STDMETHODIMP_(ULONG) AddRef() { return 2; }
  27.     STDMETHODIMP_(ULONG) Release() { return 1; }
  28.  
  29.     STDMETHODIMP QueryInterface(REFIID riid, void ** ppv)
  30.     {
  31.         if (riid == IID_ISampleGrabberCB || riid == IID_IUnknown) 
  32.         {
  33.             *ppv = (void *) static_cast<ISampleGrabberCB *>(this);
  34.             return NOERROR;
  35.         }    
  36.         return E_NOINTERFACE;
  37.     }
  38.  
  39.     STDMETHODIMP SampleCB( double SampleTime, IMediaSample * pSample )
  40.     {
  41.         static long counter = 0;
  42.         printf( "Sample received = %05ld  Clock = %ld\r\n", 
  43.                  counter++, timeGetTime( ) );
  44.         return 0;
  45.     }
  46.  
  47.     STDMETHODIMP BufferCB( double SampleTime, BYTE * pBuffer, long BufferLen )
  48.     {
  49.         return 0;
  50.     }
  51.  
  52. };
  53.  
  54.  
  55. int main(int argc, char* argv[])
  56. {
  57.     if( argc != 2 )
  58.     {
  59.         printf( "Usage: SampGrabCB <filename>\r\n\r\n" );
  60.         printf( "This application reads a media file and receives callbacks for every\r\n"
  61.                 "media sample processed.  You must provide a valid filename.\r\n");
  62.         return -1;
  63.     }
  64.  
  65.     // Initialize COM
  66.     CoInitialize( NULL );
  67.  
  68.     // Run the test on the filename specified on the command line
  69.     GrabSamples( argv[1] );
  70.  
  71.     // COM cleanup
  72.     CoUninitialize( );
  73.  
  74.     return 0;
  75. }
  76.  
  77.  
  78. int GrabSamples( char * pFilename )
  79. {
  80.     USES_CONVERSION;
  81.  
  82.     CFakeCallback pCallback;
  83.     HRESULT hr;
  84.  
  85.     printf("Grabbing samples from %s.\r\n", pFilename);
  86.  
  87.     // create a media detector
  88.     //
  89.     CComPtr< IMediaDet > pDet;
  90.     hr = CoCreateInstance( CLSID_MediaDet, NULL, CLSCTX_INPROC_SERVER, 
  91.                            IID_IMediaDet, (void**) &pDet );
  92.     if( FAILED( hr ) ) 
  93.     {
  94.         printf( "Failed in CoCreateInstance!  hr=0x%x\r\n", hr );
  95.         return hr;
  96.     }
  97.  
  98.     // set filename
  99.     //
  100.     hr = pDet->put_Filename( T2W( pFilename ) );
  101.     if( FAILED( hr ) ) 
  102.     {
  103.         printf( "couldn't load the file!  hr=0x%x\r\n", hr );
  104.         return hr;
  105.     }
  106.  
  107.     // look for a video stream
  108.     //
  109.     long Streams = 0;
  110.     hr = pDet->get_OutputStreams( &Streams );
  111.     if( FAILED( hr ) ) 
  112.     {
  113.         printf( "couldn't get the output streams!  hr=0x%x\r\n", hr );
  114.         return hr;
  115.     }
  116.  
  117.     BOOL bFoundVideo = FALSE;
  118.  
  119.     for( int i = 0 ; i < Streams ; i++ )
  120.     {
  121.         BOOL bIsVideo = FALSE;
  122.  
  123.         AM_MEDIA_TYPE Type;
  124.         memset( &Type, 0, sizeof( Type ) );
  125.  
  126.         // Select a media stream
  127.         hr = pDet->put_CurrentStream( i );
  128.         if( FAILED( hr ) ) 
  129.         {
  130.             printf( "couldn't put stream %d  hr=0x%x\r\n", i, hr );
  131.             return hr;
  132.         }
  133.  
  134.         // Read the media type of the selected stream
  135.         hr = pDet->get_StreamMediaType( &Type );
  136.         if( FAILED( hr ) ) 
  137.         {
  138.             printf( "couldn't get stream media type for stream %d  hr=0x%x\r\n",
  139.                     i, hr );
  140.             return hr;
  141.         }
  142.  
  143.         // Does this stream contain video?
  144.         if( Type.majortype == MEDIATYPE_Video )
  145.             bIsVideo = TRUE;
  146.  
  147.         FreeMediaType( Type );
  148.  
  149.         if( !bIsVideo ) 
  150.             continue;
  151.  
  152.         // Found a video stream
  153.         bFoundVideo = TRUE;
  154.         break;
  155.     }
  156.  
  157.     if( !bFoundVideo )
  158.     {
  159.         printf( "Couldn't find a video stream\r\n" );
  160.         return 0;
  161.     }
  162.  
  163.     // this method will change the MediaDet to go into 
  164.     // "sample grabbing mode" at time 0.
  165.     //
  166.     hr = pDet->EnterBitmapGrabMode( 0.0 );
  167.     if( FAILED( hr ) ) 
  168.     {
  169.         printf( "Failed in EnterBitmapGrabMode!  hr=0x%x\r\n", hr );
  170.         return hr;
  171.     }
  172.  
  173.     // ask for the sample grabber filter that we know lives inside the
  174.     // graph made by the MediaDet
  175.     //
  176.     CComPtr< ISampleGrabber > pGrabber;
  177.     hr = pDet->GetSampleGrabber( &pGrabber );
  178.     if( FAILED(hr) || !pGrabber)
  179.     {
  180.         printf( "couldn't find the sample grabber filter!  hr=0x%x\r\n", hr );
  181.         return hr;
  182.     }
  183.  
  184.     // set the callback (our COM object callback)
  185.     //
  186.     CComQIPtr< ISampleGrabberCB, &IID_ISampleGrabberCB > pCB( &pCallback );
  187.     CComQIPtr< IBaseFilter, &IID_IBaseFilter > pFilter( pGrabber );
  188.     hr = pGrabber->SetCallback( pCB, 0 );
  189.     hr = pGrabber->SetOneShot( FALSE );       // don't do one-shot mode
  190.     hr = pGrabber->SetBufferSamples( FALSE ); // don't buffer samples
  191.  
  192.     // find the filter graph interface from the sample grabber filter
  193.     //
  194.     FILTER_INFO fi;
  195.     memset( &fi, 0, sizeof( fi ) );
  196.     hr = pFilter->QueryFilterInfo( &fi );
  197.     if( FAILED( hr ) ) 
  198.     {
  199.         printf( "Failed in QueryFilterInfo!  hr=0x%x\r\n", hr );
  200.         return hr;
  201.     }
  202.  
  203.     // Release the filter's graph reference
  204.     if( fi.pGraph ) 
  205.         fi.pGraph->Release( );
  206.     IFilterGraph * pGraph = fi.pGraph;
  207.  
  208.     // The graph will have been paused by entering bitmap grab mode.
  209.     // We'll need to seek back to 0 to get it to deliver correctly.
  210.     //
  211.     CComQIPtr< IMediaSeeking, &IID_IMediaSeeking > pSeeking( pGraph );
  212.     REFERENCE_TIME Start = 0;
  213.     REFERENCE_TIME Duration = 0;
  214.  
  215.     hr = pSeeking->GetDuration( &Duration );
  216.     if( FAILED( hr ) ) 
  217.     {
  218.         printf( "Failed in GetDuration!  hr=0x%x\r\n", hr );
  219.         return hr;
  220.     }
  221.  
  222.     hr = pSeeking->SetPositions( &Start,    AM_SEEKING_AbsolutePositioning, 
  223.                                  &Duration, AM_SEEKING_AbsolutePositioning );
  224.     if( FAILED( hr ) ) 
  225.     {
  226.         printf( "Failed in SetPositions!  hr=0x%x\r\n", hr );
  227.         return hr;
  228.     }
  229.  
  230.     // run the graph
  231.     //
  232.     CComQIPtr< IMediaEvent, &IID_IMediaEvent > pEvent( pGraph );
  233.     CComQIPtr< IMediaControl, &IID_IMediaControl > pControl( pGraph );
  234.     hr = pControl->Run( );
  235.     if( FAILED( hr ) ) 
  236.     {
  237.         printf( "Failed to run the graph!  hr=0x%x\r\n", hr );
  238.         return hr;
  239.     }
  240.  
  241.     // wait 
  242.     //
  243.     long EventCode = 0;
  244.     hr = pEvent->WaitForCompletion( INFINITE, &EventCode );
  245.     if( FAILED( hr ) ) 
  246.     {
  247.         printf( "Failed in WaitForCompletion!  hr=0x%x\r\n", hr );
  248.         return hr;
  249.     }
  250.  
  251.     printf("Sample grabbing complete.\r\n");
  252.     return 0;
  253. }
  254.  
  255.