IltmmWMScript CreateScriptStream Example for C

This example shows how to add stream scripts from a capture graph.

#define MAKE_MEDIA_PATH(pFileName) (TEXT("C:\\LEADTOOLS 17.5\\Media\\")TEXT(pFileName))


void IltmmWMScript_CreateScriptStream_Example(IltmmCapture *pCapture)
{
   HRESULT hr = S_OK;
   IUnknown *punk;
   IltmmWMScript *pWMScript;

   // source and target path names
   BSTR bstrTarget = SysAllocString(MAKE_MEDIA_PATH("source_script.wmv"));
   
   // set the source, target and WMV target formats for our conversion
   hr = SelectCaptureDevices(pCapture);
   if (FAILED(hr))
   {
      MessageBox(NULL, L"Unable to select capture devices", L"WMScripting Example", MB_OK);
      return;
   }

   IltmmCapture_put_TargetFile(pCapture, bstrTarget);
   IltmmCapture_put_TargetFormat(pCapture, ltmmCapture_TargetFormat_WMV_Mux);

   // free the BSTR
   SysFreeString(bstrTarget);

   // get the target object
   IltmmCapture_GetSubObject(pCapture, ltmmCapture_Object_TargetFilter, &punk);

   if (punk)
   {
      // get the WMScript object
      IUnknown_QueryInterface(punk, &IID_IltmmWMScript, (void**)&pWMScript);
      if (pWMScript)
      {
         WCHAR szTemp[128];
         double scriptTime;
         BSTR bstrScriptCmd;

         // turn off stream scripts and remove any header scripts
         IltmmWMScript_put_EnableScriptStream(pWMScript, VARIANT_TRUE);
         IltmmWMScript_RemoveAllHeaderScripts(pWMScript);

         // create a sample header script (type of caption) and set it to execute @ 2 secs into capture
         scriptTime = 2.0;
         swprintf_s(szTemp, _countof(szTemp), L"Sample caption script at %g seconds", scriptTime);      
         bstrScriptCmd = SysAllocString(szTemp);

         // add the header script
         hr = IltmmWMScript_WriteScriptStream(pWMScript, L"caption", bstrScriptCmd, scriptTime);
         // free the bstr
         SysFreeString(bstrScriptCmd);

         // now close the script stream
         hr = IltmmWMScript_CloseScriptStream(pWMScript);

         IUnknown_Release(pWMScript);
      }
            
      // if all is well, run the capture now
      if (SUCCEEDED(hr))
         hr = IltmmCapture_StartCapture(pCapture, ltmmCapture_Mode_VideoAndAudio);

      IUnknown_Release(punk);
   }
}

BOOL SelectDeviceLike(IltmmDevices *pDevices, LPTSTR szDevSrch)
{
   BOOL bFound = FALSE;
   long i, count = 0;
   
   IltmmDevices_put_Selection(pDevices, -1);
   IltmmDevices_get_Count(pDevices, &count);

   for(i = 0; i < count; i++)
   {
      IltmmDevice *pDevice;
      BSTR name;
      IltmmDevices_Item(pDevices, i, &pDevice);

      if (pDevice)
      {
         // get the friendly name for comparisson check
         IltmmDevice_get_FriendlyName(pDevice, &name);

         // if the device name contains our search string, select it
         if(!wcsstr(name, szDevSrch))
         {
            IltmmDevices_put_Selection(pDevices, i);
            IltmmDevice_Release(pDevice);
            SysFreeString(name);

            bFound = TRUE;
            break;
         }
         
         SysFreeString(name);
         IltmmDevice_Release(pDevice);
         pDevice = NULL;
      }
   }

   return bFound;
}

HRESULT SelectCaptureDevices(IltmmCapture *pCapture)
{
   IltmmDevices *pDevices;
   BOOL bSelected = FALSE;
   
   IltmmCapture_get_VideoDevices(pCapture, &pDevices);
   if (!pDevices)
      return E_FAIL;

   bSelected = SelectDeviceLike(pDevices, L"Logitech");

   IltmmDevices_Release(pDevices);
   pDevices = NULL;

   if (!bSelected)
      return E_FAIL;

   bSelected = FALSE;
   IltmmCapture_get_AudioDevices(pCapture, &pDevices);
   if (!pDevices)
      return E_FAIL;

   bSelected = SelectDeviceLike(pDevices, L"Logitech");
   
   IltmmDevices_Release(pDevices);

   if (!bSelected)
      return E_FAIL;

   return S_OK;
}