Advanced Capture Application Example for C++

The following code demonstrates an advanced capture application. It utilizes most of the functions available in the ltmfCapture object.

// include the LEAD Media Foundation TOOLKIT header 
#include "ltmf.h" 
#include "resource.h" 
#include <tchar.h> 
#include <stdio.h> 
#include <olectl.h> 
#include <math.h> 
#include <assert.h> 
 
#define SZ_WNDCLASS_CAPTURE _T("CAPTURE WNDCLASS") 
#define WM_CAPTURENOTIFY (WM_USER + 1000) 
HINSTANCE g_hInstance;    // application instance handle 
HWND g_hwndCapture;      // video frame window 
IltmfCapture* g_pCapture;      // capture object interface pointer 
// 
// FreeTarget 
// resets target and free asssociated resources 
// 
void FreeTarget(void) 
{ 
   long type; 
   VARIANT var; 
   g_pCapture->get_TargetType(&type); 
   if(type == ltmfCapture_Target_File) 
   { 
      g_pCapture->ResetTarget(); 
   } 
} 
// 
// SnapFrameToVideo 
// resizes the frame window to match the video width and height 
// 
void SnapFrameToVideo(void) 
{ 
   HWND hwnd; 
   RECT rcWindow, rcClient; 
   long cx, cy; 
   // get the frame window 
   g_pCapture->get_VideoWindowFrame((long*) &hwnd); 
   // get the video dimensions 
   g_pCapture->get_VideoWidth(&cx); 
   g_pCapture->get_VideoHeight(&cy); 
   // adjust by the border dimensions 
   GetWindowRect(hwnd, &rcWindow); 
   GetClientRect(hwnd, &rcClient); 
   cx += ((rcWindow.right - rcWindow.left) - (rcClient.right - rcClient.left)); 
   cy += ((rcWindow.bottom - rcWindow.top) - (rcClient.bottom - rcClient.top)); 
   // resize the window 
   SetWindowPos(hwnd, NULL, 0, 0, cx, cy, SWP_NOMOVE | SWP_NOZORDER); 
} 
// 
// CaptureWndProc 
// video frame window procedure 
// 
LRESULT CALLBACK CaptureWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) 
{ 
   VARIANT_BOOL f; 
   HRESULT hr; 
   HGLOBAL hDIB; 
   IPictureDisp* pPictureDisp; 
   BSTR bstr; 
   IltmfDevices* pDevices; 
   long index; 
   long l; 
   long l2; 
   BOOL fActive; 
   ltmfSizeMode sm; 
   TCHAR sz[2048]; 
   LPTSTR p; 
   double d; 
   VARIANT var; 
   long x, y, cx, cy; 
   RECT rc; 
   POINT pt; 
   switch (message)  
   { 
   case WM_CREATE: 
      g_hwndCapture = hwnd; 
      // window is the video window frame 
      g_pCapture->put_VideoWindowFrame((long) hwnd); 
      // want notification messages as well 
      g_pCapture->SetNotifyWindow((long) hwnd, WM_CAPTURENOTIFY); 
      // set preview source video only 
      g_pCapture->put_PreviewSource(ltmfCapture_Preview_Video); 
      // enable preview 
      g_pCapture->put_Preview(VARIANT_TRUE); 
      // assign initial frame delay 
      g_pCapture->put_FrameDelay(1.0); 
      // assign a target file 
      bstr = SysAllocString(L"c:\\target.mp4"); 
      g_pCapture->put_TargetFile(bstr); 
      SysFreeString(bstr); 
      // set output format to MP4 
      g_pCapture->put_TargetFormat(ltmfCapture_TargetFormat_MP4); 
 #ifdef _DEBUG 
   { 
      long v; 
      g_pCapture->get_TargetFormat(&v); 
      assert(v == ltmfCapture_TargetFormat_MP4); 
   } 
 #endif 
 
      // get target format 
 
      IltmfTargetFormats* pTargetFormats; 
 
      IltmfTargetFormat*  pTargetFormat; 
 
      g_pCapture->get_TargetFormats(&pTargetFormats); 
 
    
 
      long v; 
 
      pTargetFormats->get_Selection(&v); 
 
      pTargetFormats->Item(v, &pTargetFormat); 
 
      pTargetFormats->Release(); 
 
      // get target video formats 
 
      IltmfTargetVideoFormats* pTargetVideoFormats; 
 
      pTargetFormat->get_VideoFormats(&pTargetVideoFormats); 
 
      // select the H264 target video format 
 
      bstr = SysAllocString(L"{34363248-0000-0010-8000-00AA00389B71}"); 
 
      pTargetVideoFormats->Find(bstr, &index); 
 
      SysFreeString(bstr); 
 
      if(index < 0) 
      { 
 
         // video target format isn't registered 
 
         pTargetFormat->Release(); 
 
         pTargetVideoFormats->Release(); 
 
         return E_FAIL; 
      } 
 
      pTargetVideoFormats->put_Selection(index); 
 
      pTargetVideoFormats->Release(); 
 
      // get target audio formats 
 
      IltmfTargetAudioFormats* pTargetAudioFormats; 
 
      pTargetFormat->get_AudioFormats(&pTargetAudioFormats); 
 
      // select the AAC target audio format 
 
      bstr = SysAllocString(L"{00001610-0000-0010-8000-00AA00389B71}"); 
 
      pTargetAudioFormats->Find(bstr, &index); 
 
      SysFreeString(bstr); 
 
      if(index < 0) 
      { 
 
         // audio target format isn't registered 
 
         pTargetFormat->Release(); 
 
         pTargetAudioFormats->Release(); 
 
         return E_FAIL; 
      } 
 
      pTargetAudioFormats->put_Selection(index); 
 
      pTargetAudioFormats->Release(); 
 
      pTargetFormat->Release(); 
      // select the first audio device available 
      g_pCapture->get_AudioDevices(&pDevices); 
      pDevices->put_Selection(0); 
      pDevices->Release(); 
      // select the first video device available 
      g_pCapture->get_VideoDevices(&pDevices); 
      pDevices->put_Selection(0); 
      pDevices->Release(); 
      SnapFrameToVideo(); 
      return 0; 
      break; 
   case WM_CAPTURENOTIFY: 
      switch(wParam) 
      { 
      case ltmfCapture_Notify_Started: 
         _stprintf(sz, _T("Started")); 
         SetWindowText(hwnd, sz); 
         break; 
      case ltmfCapture_Notify_Complete: 
         g_pCapture->get_Mode(&l); 
         if(l == ltmfCapture_Mode_Still) 
         { 
            _stprintf(sz, _T("Complete - [still]")); 
         } 
         else 
         { 
            g_pCapture->get_TargetType(&l); 
            if(l == ltmfCapture_Target_File) 
            { 
               g_pCapture->get_TargetFile(&bstr); 
               _stprintf(sz, _T("Complete - [%ls]"), bstr); 
               SysFreeString(bstr); 
            } 
         } 
         SetWindowText(hwnd, sz); 
         break; 
      case ltmfCapture_Notify_ErrorAbort: 
         _stprintf(sz, _T("Error 0x%.8X. Capture stopped."), lParam); 
         MessageBox(hwnd, sz, _T("Play"), MB_ICONEXCLAMATION | MB_OK); 
         break; 
      case ltmfCapture_Notify_Progress: 
         p = sz; 
         *p = _T('\0'); 
         hr = g_pCapture->get_NumDropped(&l); 
         if(SUCCEEDED(hr)) 
         { 
            if(p != sz) 
               p += _stprintf(p, _T(", ")); 
            p += _stprintf(p, _T("Dropped: %d"), l); 
         } 
         hr = g_pCapture->get_NumNotDropped(&l); 
         if(SUCCEEDED(hr)) 
         { 
            if(p != sz) 
               p += _stprintf(p, _T(", ")); 
            p += _stprintf(p, _T("Not Dropped: %d"), l); 
         } 
         hr = g_pCapture->get_AverageFrameSize(&l); 
         if(SUCCEEDED(hr)) 
         { 
            if(p != sz) 
               p += _stprintf(p, _T(", ")); 
            p += _stprintf(p, _T("Frame Size: %d"), l); 
         } 
         g_pCapture->get_Mode(&l); 
         if(l != ltmfCapture_Mode_Still || l != ltmfCapture_Mode_ManualFrames) 
         { 
            // only display the capture time for free running captures 
            hr = g_pCapture->get_CaptureTime(&d); 
            if(SUCCEEDED(hr)) 
            { 
               if(p != sz) 
                  p += _stprintf(p, _T(", ")); 
               p += _stprintf(p, _T("Time: %f"), d); 
            } 
         } 
         SetWindowText(hwnd, sz); 
         break; 
      } 
      return 0; 
      break; 
   case WM_KEYDOWN: 
      if(wParam == VK_ESCAPE) 
      { 
         // if fullscreen mode then exit it 
         g_pCapture->get_FullScreenMode(&f); 
         if(f) 
         { 
            g_pCapture->put_FullScreenMode(VARIANT_FALSE); 
            return 0; 
         } 
      } 
      break; 
   case WM_DESTROY: 
      FreeTarget(); 
      // no video frame 
      g_pCapture->put_VideoWindowFrame((long) NULL); 
      // no more notifications 
      g_pCapture->SetNotifyWindow((long) NULL, 0); 
      PostQuitMessage(0); 
      break; 
   case WM_INITMENUPOPUP: 
      if(GetSubMenu(GetMenu(hwnd), 0) == (HMENU)wParam) 
      { 
         // determine whether the object is active 
         g_pCapture->get_State(&l); 
         fActive = (l == ltmfCapture_State_Pending || l == ltmfCapture_State_Running); 
         // enable stop if active 
         EnableMenuItem((HMENU) wParam, ID_CONTROL_STOPCAPTURE, fActive ? MF_ENABLED : MF_GRAYED); 
         // enable run if paused 
         EnableMenuItem((HMENU) wParam, ID_CONTROL_RUN, (l == ltmfCapture_State_Paused) ? MF_ENABLED : MF_GRAYED); 
         // enable pause if running or pending 
         EnableMenuItem((HMENU) wParam, ID_CONTROL_PAUSE, (l == ltmfCapture_State_Running || l == ltmfCapture_State_Pending) ? MF_ENABLED : MF_GRAYED); 
         // can perform normal capture? 
         g_pCapture->IsModeAvailable(ltmfCapture_Mode_VideoOrAudio, &f); 
         EnableMenuItem((HMENU) wParam, ID_CONTROL_STARTCAPTURE, f ? MF_ENABLED : MF_GRAYED); 
    
         // can perform auto frame capture? 
         g_pCapture->IsModeAvailable(ltmfCapture_Mode_AutoFrames, &f); 
         EnableMenuItem((HMENU) wParam, ID_CONTROL_CAPTUREAUTOFRAMES, f ? MF_ENABLED : MF_GRAYED); 
         // can perform manual frame capture? 
         g_pCapture->IsModeAvailable(ltmfCapture_Mode_ManualFrames, &f); 
         EnableMenuItem((HMENU) wParam, ID_CONTROL_CAPTUREMANUALFRAMES, f ? MF_ENABLED : MF_GRAYED); 
         // can perform still image capture? 
         g_pCapture->IsModeAvailable(ltmfCapture_Mode_Still, &f); 
         EnableMenuItem((HMENU) wParam, ID_CONTROL_CAPTUREDIB, f ? MF_ENABLED : MF_GRAYED); 
         EnableMenuItem((HMENU) wParam, ID_CONTROL_CAPTUREPICTURE, f ? MF_ENABLED : MF_GRAYED); 
         EnableMenuItem((HMENU) wParam, ID_CONTROL_GETSTILLDIB, f ? MF_ENABLED : MF_GRAYED); 
         EnableMenuItem((HMENU) wParam, ID_CONTROL_GETSTILLPICTURE, f ? MF_ENABLED : MF_GRAYED); 
       
         // check the current video size mode 
         g_pCapture->get_VideoWindowSizeMode(&sm); 
         CheckMenuItem((HMENU) wParam, ID_CONTROL_FITTOWINDOW, (sm == ltmfFit) ? MF_CHECKED : MF_UNCHECKED); 
         CheckMenuItem((HMENU) wParam, ID_CONTROL_STRETCHTOWINDOW, (sm == ltmfStretch) ? MF_CHECKED : MF_UNCHECKED); 
         // check preview 
         g_pCapture->get_Preview(&f); 
         CheckMenuItem((HMENU) wParam, ID_CONTROL_PREVIEW, f ? MF_CHECKED : MF_UNCHECKED); 
         EnableMenuItem((HMENU) wParam, ID_CONTROL_PREVIEW, !fActive ? MF_ENABLED : MF_GRAYED); 
    
         // check preview audio 
         g_pCapture->get_PreviewSource(&l); 
         CheckMenuItem((HMENU) wParam, ID_CONTROL_PREVIEWVIDEOANDAUDIO, (l == ltmfCapture_Preview_VideoAndAudio) ? MF_CHECKED : MF_UNCHECKED); 
         EnableMenuItem((HMENU) wParam, ID_CONTROL_PREVIEWVIDEOANDAUDIO, !fActive ? MF_ENABLED : MF_GRAYED); 
       
         // check frame rate 
         g_pCapture->get_UseFrameRate(&f); 
         g_pCapture->get_FrameRate(&d); 
         CheckMenuItem((HMENU) wParam, ID_CONTROL_FRAMERATE_DEFAULT, (!f) ? MF_CHECKED : MF_UNCHECKED); 
         CheckMenuItem((HMENU) wParam, ID_CONTROL_FRAMERATE_15FPS, (f && fabs(d - 15.0) < 0.1) ? MF_CHECKED : MF_UNCHECKED); 
         CheckMenuItem((HMENU) wParam, ID_CONTROL_FRAMERATE_30FPS, (f && fabs(d - 30.0) < 0.1) ? MF_CHECKED : MF_UNCHECKED); 
         EnableMenuItem((HMENU) wParam, ID_CONTROL_FRAMERATE_DEFAULT, (!fActive) ? MF_ENABLED : MF_GRAYED); 
         EnableMenuItem((HMENU) wParam, ID_CONTROL_FRAMERATE_15FPS, (!fActive) ? MF_ENABLED : MF_GRAYED); 
         EnableMenuItem((HMENU) wParam, ID_CONTROL_FRAMERATE_30FPS, (!fActive) ? MF_ENABLED : MF_GRAYED); 
         // check time limit 
         g_pCapture->get_UseTimeLimit(&f); 
         g_pCapture->get_TimeLimit(&d); 
         CheckMenuItem((HMENU) wParam, ID_CONTROL_TIMELIMIT_NONE, (!f) ? MF_CHECKED : MF_UNCHECKED); 
         CheckMenuItem((HMENU) wParam, ID_CONTROL_TIMELIMIT_15SEC, (f && fabs(d - 15.0) < 0.1) ? MF_CHECKED : MF_UNCHECKED); 
         CheckMenuItem((HMENU) wParam, ID_CONTROL_TIMELIMIT_30SEC, (f && fabs(d - 30.0) < 0.1) ? MF_CHECKED : MF_UNCHECKED); 
         EnableMenuItem((HMENU) wParam, ID_CONTROL_TIMELIMIT_NONE, (!fActive) ? MF_ENABLED : MF_GRAYED); 
         EnableMenuItem((HMENU) wParam, ID_CONTROL_TIMELIMIT_15SEC, (!fActive) ? MF_ENABLED : MF_GRAYED); 
         EnableMenuItem((HMENU) wParam, ID_CONTROL_TIMELIMIT_30SEC, (!fActive) ? MF_ENABLED : MF_GRAYED); 
       
         // check frame delay 
         g_pCapture->get_FrameDelay(&d); 
         CheckMenuItem((HMENU) wParam, ID_CONTROL_FRAMEDELAY_1SEC, (fabs(d - 1.0) < 0.1) ? MF_CHECKED : MF_UNCHECKED); 
         CheckMenuItem((HMENU) wParam, ID_CONTROL_FRAMEDELAY_5SEC, (fabs(d - 5.0) < 0.1) ? MF_CHECKED : MF_UNCHECKED); 
         EnableMenuItem((HMENU) wParam, ID_CONTROL_FRAMEDELAY_1SEC, (!fActive) ? MF_ENABLED : MF_GRAYED); 
         EnableMenuItem((HMENU) wParam, ID_CONTROL_FRAMEDELAY_5SEC, (!fActive) ? MF_ENABLED : MF_GRAYED); 
         // check target 
         g_pCapture->get_TargetType(&l); 
         CheckMenuItem((HMENU) wParam, ID_CONTROL_TARGET_FILE, (l == ltmfCapture_Target_File) ? MF_CHECKED : MF_UNCHECKED); 
         EnableMenuItem((HMENU) wParam, ID_CONTROL_TARGET_FILE, (!fActive) ? MF_ENABLED : MF_GRAYED); 
         // check target format 
         g_pCapture->get_TargetFormat(&l); 
         g_pCapture->get_TargetType(&l2); 
         CheckMenuItem((HMENU) wParam, ID_CONTROL_TARGETFORMAT_MP4, (l == ltmfCapture_TargetFormat_MP4) ? MF_CHECKED : MF_UNCHECKED); 
         CheckMenuItem((HMENU) wParam, ID_CONTROL_TARGETFORMAT_WMV, (l == ltmfCapture_TargetFormat_WMV) ? MF_CHECKED : MF_UNCHECKED); 
         EnableMenuItem((HMENU) wParam, ID_CONTROL_TARGETFORMAT_MP4, (!fActive && l2 == ltmfCapture_Target_File) ? MF_ENABLED : MF_GRAYED); 
         EnableMenuItem((HMENU) wParam, ID_CONTROL_TARGETFORMAT_WMV, (!fActive && l2 == ltmfCapture_Target_File) ? MF_ENABLED : MF_GRAYED); 
         // check properties preview 
         g_pCapture->get_ShowDialogPreview(&f); 
         CheckMenuItem((HMENU) wParam, ID_CONTROL_PROPERTIESPREVIEW, (f) ? MF_CHECKED : MF_UNCHECKED); 
      } 
      break; 
   case WM_COMMAND: 
      switch(LOWORD(wParam)) 
      { 
      case ID_CONTROL_FITTOWINDOW: 
         // fit the video to the window 
         g_pCapture->put_VideoWindowSizeMode(ltmfFit); 
         return 0; 
         break; 
      case ID_CONTROL_STRETCHTOWINDOW: 
         // stretch the video to the window 
         g_pCapture->put_VideoWindowSizeMode(ltmfStretch); 
         return 0; 
         break; 
      case ID_CONTROL_FULLSCREEN: 
         // toggle fullscreen mode 
         g_pCapture->ToggleFullScreenMode(); 
         return 0; 
         break; 
      case ID_CONTROL_PREVIEW: 
         // toggle preview 
         g_pCapture->TogglePreview(); 
         return 0; 
         break; 
      case ID_CONTROL_PREVIEWVIDEOANDAUDIO: 
         // toggle audio preview 
         g_pCapture->get_PreviewSource(&l); 
         if(l == ltmfCapture_Preview_VideoAndAudio) 
            g_pCapture->put_PreviewSource(ltmfCapture_Preview_Video); 
         else 
            g_pCapture->put_PreviewSource(ltmfCapture_Preview_VideoAndAudio); 
         return 0; 
         break; 
      case ID_CONTROL_STOPCAPTURE: 
         // stop capturing 
         g_pCapture->StopCapture(); 
         return 0; 
         break; 
      case ID_CONTROL_STARTCAPTURE: 
         // ready capture to eliminate start delays 
         hr = g_pCapture->ReadyCapture(ltmfCapture_Mode_VideoOrAudio); 
         if(SUCCEEDED(hr)) 
         { 
            if(MessageBox(hwnd, _T("Ready to capture. Click 'ok' to start."), _T("Capture"), MB_OKCANCEL) == IDOK) 
            { 
               // start capturing 
               g_pCapture->StartCapture(ltmfCapture_Mode_VideoOrAudio); 
            } 
            else 
            { 
               // cancel capture 
               g_pCapture->StopCapture(); 
            } 
         } 
         return 0; 
         break; 
      case ID_CONTROL_RUN: 
         // run the capture graph 
         g_pCapture->RunCapture(); 
         return 0; 
         break; 
      case ID_CONTROL_PAUSE: 
         // pause the capture graph 
         g_pCapture->PauseCapture(); 
         return 0; 
         break; 
      case ID_CONTROL_CAPTUREAUTOFRAMES: 
         hr = g_pCapture->ReadyCapture(ltmfCapture_Mode_AutoFrames); 
         if(SUCCEEDED(hr)) 
         { 
            // start capturing automatic frame sequence 
            g_pCapture->StartCapture(ltmfCapture_Mode_AutoFrames); 
         } 
         return 0; 
         break; 
      case ID_CONTROL_CAPTUREMANUALFRAMES: 
       
         hr = g_pCapture->ReadyCapture(ltmfCapture_Mode_ManualFrames); 
         if(SUCCEEDED(hr)) 
         { 
            // start capturing automatic frame sequence 
            hr = g_pCapture->StartCapture(ltmfCapture_Mode_ManualFrames); 
            if(SUCCEEDED(hr)) 
            { 
               while(MessageBox(hwnd, _T("Press 'ok' to capture frame."), _T("Capture Manual Frames"), MB_OKCANCEL) == IDOK) 
                  g_pCapture->CaptureFrame(); 
               g_pCapture->StopCapture(); 
            } 
         } 
         return 0; 
         break; 
      case ID_CONTROL_CAPTUREDIB: 
         // capture device independent bitmap 
         hr = g_pCapture->CaptureDIB((long*) &hDIB); 
         if(SUCCEEDED(hr)) 
         { 
            // copy to the clipboard 
            OpenClipboard(hwnd); 
            EmptyClipboard(); 
            SetClipboardData(CF_DIB, hDIB); 
            CloseClipboard(); 
         } 
         return 0; 
         break; 
      case ID_CONTROL_CAPTUREPICTURE: 
         // capture ole picture object 
         hr = g_pCapture->CapturePicture(&pPictureDisp); 
         if(SUCCEEDED(hr)) 
         { 
            // save the picture 
            bstr = SysAllocString(L"c:\\still.bmp"); 
            OleSavePictureFile(pPictureDisp, bstr); 
            SysFreeString(bstr); 
            pPictureDisp->Release(); 
         } 
         return 0; 
         break; 
      case ID_CONTROL_GETSTILLDIB: 
         // alternate method of capturing a device independent bitmap 
         hr = g_pCapture->StartCapture(ltmfCapture_Mode_Still); 
         if(SUCCEEDED(hr)) 
         { 
            hr = g_pCapture->GetStillDIB(INFINITE, (long*) &hDIB); 
            g_pCapture->StopCapture(); 
            if(SUCCEEDED(hr)) 
            { 
               // copy to the clipboard 
               OpenClipboard(hwnd); 
               EmptyClipboard(); 
               SetClipboardData(CF_DIB, hDIB); 
               CloseClipboard(); 
            } 
         } 
         return 0; 
         break; 
      case ID_CONTROL_GETSTILLPICTURE: 
         // alternate method of capturing an ole picture object 
         hr = g_pCapture->StartCapture(ltmfCapture_Mode_Still); 
         if(SUCCEEDED(hr)) 
         { 
            hr = g_pCapture->GetStillPicture(INFINITE, &pPictureDisp); 
            g_pCapture->StopCapture(); 
            if(SUCCEEDED(hr)) 
            { 
               // save the picture 
               bstr = SysAllocString(L"c:\\still.bmp"); 
               OleSavePictureFile(pPictureDisp, bstr); 
               SysFreeString(bstr); 
               pPictureDisp->Release(); 
            } 
         } 
         return 0; 
         break; 
      case ID_CONTROL_FRAMERATE_DEFAULT: 
         // set rate to default 
         g_pCapture->put_UseFrameRate(VARIANT_FALSE); 
         return 0; 
         break; 
      case ID_CONTROL_FRAMERATE_15FPS: 
         // set rate to 15 fps 
         g_pCapture->put_FrameRate(15.0); 
         g_pCapture->put_UseFrameRate(VARIANT_TRUE); 
         return 0; 
         break; 
      case ID_CONTROL_FRAMERATE_30FPS: 
         // set rate to 30 fps 
         g_pCapture->put_FrameRate(30.0); 
         g_pCapture->put_UseFrameRate(VARIANT_TRUE); 
         return 0; 
         break; 
      case ID_CONTROL_TIMELIMIT_NONE: 
         // no time limit 
         g_pCapture->put_UseTimeLimit(VARIANT_FALSE); 
         return 0; 
         break; 
      case ID_CONTROL_TIMELIMIT_15SEC: 
         // 15 sec time limit 
         g_pCapture->put_TimeLimit(15.0); 
         g_pCapture->put_UseTimeLimit(VARIANT_TRUE); 
         return 0; 
         break; 
      case ID_CONTROL_TIMELIMIT_30SEC: 
         // 30 sec time limit 
         g_pCapture->put_TimeLimit(30.0); 
         g_pCapture->put_UseTimeLimit(VARIANT_TRUE); 
         return 0; 
         break; 
      case ID_CONTROL_FRAMEDELAY_1SEC: 
         // 1 sec frame delay 
         g_pCapture->put_FrameDelay(1.0); 
         return 0; 
         break; 
      case ID_CONTROL_FRAMEDELAY_5SEC: 
         // 5 sec frame delay 
         g_pCapture->put_FrameDelay(5.0); 
         return 0; 
         break; 
      case ID_CONTROL_TARGET_FILE: 
         // assign a target file 
         FreeTarget(); 
         // set output format to MP4 
         g_pCapture->put_TargetFormat(ltmfCapture_TargetFormat_MP4); 
#ifdef _DEBUG 
            { 
               long v; 
               g_pCapture->get_TargetFormat(&v); 
               assert(v == ltmfCapture_TargetFormat_MP4); 
            } 
#endif 
 
         // get target format 
 
         IltmfTargetFormats* pTargetFormats; 
 
         IltmfTargetFormat*  pTargetFormat; 
 
         g_pCapture->get_TargetFormats(&pTargetFormats); 
 
     
 
         long v; 
 
         pTargetFormats->get_Selection(&v); 
 
         pTargetFormats->Item(v, &pTargetFormat); 
 
         pTargetFormats->Release(); 
 
         // get target video formats 
 
         IltmfTargetVideoFormats* pTargetVideoFormats; 
 
         pTargetFormat->get_VideoFormats(&pTargetVideoFormats); 
 
         // select the H264 video target video format 
 
         bstr = SysAllocString(L"{34363248-0000-0010-8000-00AA00389B71}"); 
 
         pTargetVideoFormats->Find(bstr, &index); 
 
         SysFreeString(bstr); 
 
         if(index < 0) 
         { 
 
            // video target format isn't registered 
 
            pTargetFormat->Release(); 
 
            pTargetVideoFormats->Release(); 
 
            return E_FAIL; 
         } 
 
         pTargetVideoFormats->put_Selection(index); 
 
         pTargetVideoFormats->Release(); 
 
         // get target audio formats 
 
         IltmfTargetAudioFormats* pTargetAudioFormats; 
 
         pTargetFormat->get_AudioFormats(&pTargetAudioFormats); 
 
         // select the AAC target audio format 
 
         bstr = SysAllocString(L"{00001610-0000-0010-8000-00AA00389B71}"); 
 
         pTargetAudioFormats->Find(bstr, &index); 
 
         SysFreeString(bstr); 
 
         if(index < 0) 
         { 
 
            // audio target format isn't registered 
 
            pTargetFormat->Release(); 
 
            pTargetAudioFormats->Release(); 
 
            return E_FAIL; 
         } 
 
         pTargetAudioFormats->put_Selection(index); 
 
         pTargetAudioFormats->Release(); 
 
         pTargetFormat->Release(); 
         return 0; 
         break; 
      case ID_CONTROL_TARGETFORMAT_MP4: 
         // is target a file? 
         g_pCapture->get_TargetType(&l); 
         if(l == ltmfCapture_Target_File) 
         { 
            // rename it 
            bstr = SysAllocString(L"c:\\target.mp4"); 
            g_pCapture->put_TargetFile(bstr); 
            SysFreeString(bstr); 
         } 
         g_pCapture->put_TargetFormat(ltmfCapture_TargetFormat_MP4); 
         return 0; 
         break; 
      case ID_CONTROL_TARGETFORMAT_WMV: 
         // is target a file? 
         g_pCapture->get_TargetType(&l); 
         if(l == ltmfCapture_Target_File) 
         { 
            // rename it 
            bstr = SysAllocString(L"c:\\target.wmv"); 
            g_pCapture->put_TargetFile(bstr); 
            SysFreeString(bstr); 
         } 
         g_pCapture->put_TargetFormat(ltmfCapture_TargetFormat_WMV); 
         return 0; 
         break; 
      case ID_CONTROL_AUDIOPROCESSORS: 
         // edit audio processors 
         g_pCapture->ShowDialog(ltmfCapture_Dlg_AudioProcessors, (long) hwnd); 
         return 0; 
         break; 
      case ID_CONTROL_VIDEOPROCESSORS: 
         // edit video processors 
         g_pCapture->ShowDialog(ltmfCapture_Dlg_VideoProcessors, (long) hwnd); 
         return 0; 
         break; 
      case ID_CONTROL_PROPERTIESPREVIEW: 
         // toggle preview while editing properties 
         g_pCapture->get_ShowDialogPreview(&f); 
         g_pCapture->put_ShowDialogPreview(f ? VARIANT_FALSE : VARIANT_TRUE); 
         return 0; 
         break; 
      } 
      break; 
   case WM_LBUTTONDOWN: 
      g_pCapture->get_State(&l); 
      if(l != ltmfCapture_State_Pending || l != ltmfCapture_State_Running) 
      { 
         g_pCapture->get_VideoWindowLeft(&x); 
         g_pCapture->get_VideoWindowTop(&y); 
         g_pCapture->get_VideoWindowWidth(&cx); 
         g_pCapture->get_VideoWindowHeight(&cy); 
         SetRect(&rc, x, y, x + cx, y + cy); 
         pt.x = (short) LOWORD(lParam); 
         pt.y = (short) HIWORD(lParam); 
         if(PtInRect(&rc, pt)) 
         { 
            // set target format properties 
            g_pCapture->ShowDialog(ltmfCapture_Dlg_TargetFormat, (long) hwnd); 
         } 
      } 
      return 0; 
      break; 
   } 
   return DefWindowProc(hwnd, message, wParam, lParam); 
} 
int APIENTRY WinMain(HINSTANCE hInstance, 
                     HINSTANCE hPrevInstance, 
                     LPSTR     lpCmdLine, 
                     int       nCmdShow) 
{ 
   MSG msg; 
   HRESULT hr; 
   WNDCLASSEX wcex; 
    
   g_hInstance = hInstance; 
   // initialize COM library 
   hr = CoInitialize(NULL); 
   if(FAILED(hr)) 
      goto error; 
    
    
   // register the video frame window class 
   wcex.cbSize = sizeof(WNDCLASSEX);  
   wcex.style         = CS_HREDRAW | CS_VREDRAW; 
   wcex.lpfnWndProc   = CaptureWndProc; 
   wcex.cbClsExtra    = 0; 
   wcex.cbWndExtra    = 0; 
   wcex.hInstance      = g_hInstance; 
   wcex.hIcon         = NULL; 
   wcex.hCursor      = LoadCursor(NULL, IDC_ARROW); 
   wcex.hbrBackground   = (HBRUSH) (COLOR_APPWORKSPACE + 1); 
   wcex.lpszMenuName   = (LPCWSTR)IDR_MENU; 
   wcex.lpszClassName   = SZ_WNDCLASS_CAPTURE; 
   wcex.hIconSm      = NULL; 
    
   if(!RegisterClassEx(&wcex)) 
      goto error; 
    
   // create the capture object 
   hr = CoCreateInstance(CLSID_ltmfCapture, NULL, CLSCTX_INPROC_SERVER, IID_IltmfCapture, (void**) &g_pCapture); 
   if(FAILED(hr)) 
      goto error; 
    
   // create the video frame window 
   if(!CreateWindow(SZ_WNDCLASS_CAPTURE, _T("Capture"), WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN, 
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, g_hInstance, NULL)) 
      goto error; 
    
   ShowWindow(g_hwndCapture, nCmdShow); 
   UpdateWindow(g_hwndCapture); 
   // process until done 
   while (GetMessage(&msg, NULL, 0, 0))  
   { 
      TranslateMessage(&msg); 
      DispatchMessage(&msg); 
   } 
    
error: 
   if(g_pCapture) 
      g_pCapture->Release(); 
   CoUninitialize(); 
   return 0;    
} 

Help Version 21.0.2021.7.2
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2021 LEAD Technologies, Inc. All Rights Reserved.

LEADTOOLS Media Foundation C API Help
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2021 LEAD Technologies, Inc. All Rights Reserved.