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 CreateScriptStream(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;
   }

   pCapture->put_TargetFile(bstrTarget);
   pCapture->put_TargetFormat(ltmmCapture_TargetFormat_WMV_Mux);

   // free the BSTR
   SysFreeString(bstrTarget);

   // get the target object
   pCapture->GetSubObject(ltmmCapture_Object_TargetFilter, &punk);

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

         // turn off stream scripts and remove any header scripts
         pWMScript->put_EnableScriptStream(VARIANT_TRUE);
         pWMScript->RemoveAllHeaderScripts();

         // 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 = pWMScript->WriteScriptStream(L"caption", bstrScriptCmd, scriptTime);
         // free the bstr
         SysFreeString(bstrScriptCmd);

         // now close the script stream
         hr = pWMScript->CloseScriptStream();

         pWMScript->Release();
      }
            
      // if all is well, run the capture now
      if (SUCCEEDED(hr))
         hr = pCapture->StartCapture(ltmmCapture_Mode_VideoAndAudio);

      punk->Release();
   }
}

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

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

      if (pDevice)
      {
         // get the friendly name for comparisson check
         pDevice->get_FriendlyName(&name);

         // if the device name contains our search string, select it
         if(!wcsstr(name, szDevSrch))
         {
            pDevices->put_Selection(i);
            pDevice->Release();
            SysFreeString(name);

            bFound = TRUE;
            break;
         }
         
         SysFreeString(name);
         pDevice->Release();
         pDevice = NULL;
      }
   }

   return bFound;
}

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

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

   pDevices->Release();
   pDevices = NULL;

   if (!bSelected)
      return E_FAIL;

   bSelected = FALSE;
   pCapture->get_AudioDevices(&pDevices);
   if (!pDevices)
      return E_FAIL;

   bSelected = SelectDeviceLike(pDevices, L"Logitech");
   
   pDevices->Release();

   if (!bSelected)
      return E_FAIL;

   return S_OK;
}