IltmmTargetFormat::put_UseFilterCache Example for C++

#include "ltmm.h"
#include "Filters/ILMMpg2MxT.h"
#include <time.h>


HRESULT UseFilterCache_Example (IltmmCapture* pCapture) {   HRESULT hr;   IltmmDevices* devices   = NULL;   IltmmCompressors* compressors = NULL;   IltmmTargetFormats* pformats = NULL;   IltmmTargetFormat*  pformat  = NULL;   IUnknown*           pUnknown = NULL;   ILMMpg2MxT*         pMpgMux = NULL;   long lIndex = 0;
  // mpeg2 compressor   static TCHAR SZ_MPEG2_Compressor[] = "@device:sw:{33D9A760-90C8-11D0-BD43-00A0C911CE86}\\LEAD MPEG2 Encoder (3.0)";
  // get the video devices collection   hr = m_capture->get_VideoDevices(&devices);   if(FAILED(hr))      return hr;
  // select the capture device   hr = devices->put_Selection(0); // Use a different video device if you want
  // release the video devices collection object   devices->Release();   if(FAILED(hr))      return hr;
  // get the video compressors collection   hr = m_capture->get_VideoCompressors(&compressors);   if(FAILED(hr))      return hr;
  USES_CONVERSION;   // find mpeg2 compressor   hr = compressors->Find(T2OLE(SZ_MPEG2_Compressor), &lIndex);   if(FAILED(hr) || lIndex == -1)   {      // release the compressors collection object
      compressors->Release();      return S_FALSE;   }
  // set the video compressor (only if the capture device is not already capturing compressed video)   compressors->put_Selection(lIndex);
  // release the compressors collection object   compressors->Release();
  // set the target output file   BSTR bstr = SysAllocString(L"c:\\CaptureKLV.avi");   m_capture->put_TargetFile(bstr);   SysFreeString(bstr);
  // just 10 seconds of capture time   m_capture->put_UseTimeLimit (VARIANT_TRUE);   m_capture->put_TimeLimit(10);
  // get the target formats collection   hr = m_capture->get_TargetFormats(&pformats);   if(FAILED(hr))      return hr;
  // get the MPEG2Transport target format object   hr = pformats->Item(ltmmCapture_TargetFormat_MPEG2_TRANSPORT, &pformat);   if(FAILED(hr))   {      // release the formats collection object      pformats->Release();      return hr;   }
  // IN a capture situation   // in order to get the mux in a capture situation, set the UseFilterCache property for the target format to TRUE   // This tells the toolkit to create a Mux object and keep it around when building or rebuilding graphs   // enable filter cache   hr = pformat->put_UseFilterCache(VARIANT_TRUE);   if(FAILED(hr))   {      // release the format and collection objects      pformat->Release();      pformats->Release();      return hr;   }
  // select MPEG2Transport target format   hr = m_capture->put_TargetFormat (ltmmCapture_TargetFormat_MPEG2_TRANSPORT);   if(FAILED(hr))      return hr;
  // get the multiplexer object for this format   hr = pformat->GetCacheObject(ltmmTargetFormat_Object_Mux, &pUnknown);   if(FAILED(hr))   {      // release the format and collection objects      pformat->Release();      pformats->Release();      return hr;   }
  // release the format and collection objects   pformat->Release();   pformats->Release();   hr = pUnknown->QueryInterface(IID_ILMMpg2MxT, (void **)&pMpgMux);   pUnknown->Release();   if(FAILED(hr))      return hr;
  if(pMpgMux)   {      pMpgMux->put_PrivateDataPID(0x70);      pMpgMux->put_PrivateDataFormatID(0x41564c4b);      pMpgMux->put_EnablePrivateData(VARIANT_TRUE);   }
  // release the multiplexer object   pMpgMux->Release();
  // start capture   hr = m_capture->StartCapture(ltmmCapture_Mode_Video);   if(FAILED(hr))      return hr;
  hr = WriteKLVData(pMpgMux);   return hr; }
 
HRESULT WriteKLVData (ILMMpg2MxT*   pMpgMux )
{
   HRESULT hr = S_OK;
   time_t basetime;
   time(&basetime);
   unsigned __int64 timestamp = ((unsigned __int64) basetime * 1000000);
 
   // get the mux builder
   ILMKlvBuilder* builder;
   hr = pMpgMux->get_KlvBuilder(&builder);
   if(FAILED(hr))
      goto abort;
 
   // clear out any existing data
   builder->Clear();
 
   // write the UDS timestamp
   hr = builder->InsertUInt64(-1L, CComBSTR(L"06 0E 2B 34 01 01 01 03 07 02 01 01 01 05 00 00"), timestamp);
   if(FAILED(hr))
   {
      builder->Release();
      goto abort;
   }
 
   // get KLV data
   VARIANT v;
   VariantInit(&v);
   hr = builder->GetData(&v);
   if(FAILED(hr))
   {
      builder->Release();
      goto abort;
   }
 
   // write one KLV data at the beginning
   hr = pMpgMux->WritePrivateData(Mpg2MxT_WriteFlag_PTSValid | Mpg2MxT_WriteFlag_PTSInSeconds, 1.0, v, -1);
   VariantClear(&v);
   if(FAILED(hr))
   {
      builder->Release();
      goto abort;
   }
 
   builder->Release();
 
abort:
   // close the stream
   pMpgMux->ClosePrivateData();
   return hr;
}