dcsimg
 

Direct Show Application for Previewing and Playing Movies

Tuesday Nov 6th 2001 by Ashar Maqbool Khan

It's a simple Direct Show Application, used for previeing video files from disk. It also captures the real time data from a video capture device and allows you to preview it.

.



Environment: VC6 SP4, Win98/2000

It's a simple Direct Show Application, used for previeing video files from disk. It also captures the real time data from a video capture device and allows you to preview it.

Technical Side:

I've used pure MFC classes and created a dialog based application and everything regarding GUI is the same as any particular MFC app is created. I've used the Direct Show 8 APIs that are mainly written in the form of COM Interfaces. You can download the Direct Show 8 SDK from Microsoft's site or from the Direct Show site itself.

Some of the interfaces used in the Applliation include:

  1. IGraphBuilder: Opens and renders the video file
  2. IMediaControl : Is used to control the playback commands to the file
  3. IMediaEventEx : Controls the events on media files.It implements the callback functions to implement various events like play, pause, stop etc.
  4. IMediaSeeking : Provides set of functions to seek into the video file backwards or forwards.
  5. IBasicAudio : Controls the only audio renderer
  6. IBasicVideo : Controls the video only files that do not have any playback sound.
  7. IVideoWindow : Provides functions to view the video data in a view window.

Initilialize and play the movie file using the following code fragment. This code can be downloaded as part of the demo project.

BOOL CMovieDlg::OpenMediaFile()
{
  LPCWSTR wstrMediaFile;

  CFileDialog dlgFile(TRUE, 
                      NULL,
                      NULL,
                      OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
                      "Movie Files (*.avi;*.mpg;*.mpeg) | 
                      *.avi;*.mpg;*.mpeg | 
                      Audio Files (*.wav;*mp3;*.mpa;*.mpu;*.au) |
                      *.wav;*.mp3;*.mpa;*.mpu;*.au | 
                      Midi Files (*.mid;*.midi;*.rmi) |
                      *.mid;*.midi;*.rmi| | ", this);

  if(dlgFile.DoModal() == IDOK)
  {
     m_strMediaFile  = dlgFile.GetPathName();
  }
  else
     return FALSE;

  USES_CONVERSION;
  wstrMediaFile = A2W(m_strMediaFile);

  CoCreateInstance( CLSID_FilterGraph,
                    NULL,
                    CLSCTX_INPROC,
                    IID_IGraphBuilder,
                    (void **)&m_pGraph);
  HRESULT hr = m_pGraph->RenderFile(wstrMediaFile, NULL);
  if(FAILED(hr))
  {
     char szMsg[200];
     AMGetErrorText(hr, szMsg, sizeof(szMsg));
     AfxMessageBox(szMsg);
  }

  // Specify the owner window.
  IVideoWindow* pVidWin = NULL;
  m_pGraph->QueryInterface(IID_IVideoWindow, (void **)&pVidWin);
  pVidWin->put_Owner((OAHWND)m_hWnd);
  pVidWin->put_WindowStyle(WS_CHILD | WS_CLIPSIBLINGS);

  CRect rc;
  GetDlgItem(IDC_STATIC_FRAME)->GetWindowRect(rc);
  ScreenToClient(rc);
  pVidWin->SetWindowPosition(rc.left, rc.top, rc.Width(), rc.Height());

  // Set the owner window to receive event notices.
  m_pGraph->QueryInterface(IID_IMediaEventEx,
                           (void **)&m_pEvent);
  m_pEvent->SetNotifyWindow((OAHWND)m_hWnd, WM_GRAPHNOTIFY, 0);

  // Set the Media Seeking
  m_pGraph->QueryInterface(IID_IMediaSeeking,
                           (void **)&m_pMediaSeeking);
  m_pGraph->QueryInterface(IID_IBasicAudio,
                           (void **)&m_pBasicAudio);
  m_pGraph->QueryInterface(IID_IBasicVideo,
                           (void **)&m_pBasicVideo);

  if ((!pVidWin) || (!m_pBasicVideo))
  {
     m_bAudioOnly = TRUE;
     TRACE0("No video interface.  Assuming audio/MIDI "
            "file or unsupported video codec.\r\n");
  }

  long lVisible;
  hr = pVidWin->get_Visible(&lVisible);

  m_bAudioOnly = FALSE;
  if(FAILED(hr))
  {
     // If this is an audio-only clip, get_Visible() won't work.
     //
     // Also, if this video is encoded with an unsupported codec,
     // we won't see any video, although the audio will work 
     // if it is of a supported format.
   
     if(hr == E_NOINTERFACE)
     {
       m_bAudioOnly = TRUE;
       m_pBasicAudio->put_Volume(m_lVolume);
     }
     else
       TRACE1(TEXT("Failed(%08lx) in pVW->get_Visible()!\r\n"),
              hr);
  }

  LONGLONG lDuration;

  hr = m_pMediaSeeking->GetDuration(&lDuration);
  if(SUCCEEDED(hr))
  {
     m_sldrVideo.SetRange(0,(int)1000);
     m_sldrVideo.SetLineSize((int)100);
     m_sldrVideo.SetTicFreq((int)10);
  }

  return TRUE;
}

BOOL CMovieDlg::Play()
{
  // Run the graph
  IVideoWindow *pVidWin = NULL;

  if(m_pGraph)
  {
     m_pGraph->QueryInterface(IID_IVideoWindow,
                              (void **)&pVidWin);
     pVidWin->put_Visible(OATRUE);

     m_pGraph->QueryInterface(IID_IMediaControl,
                              (void **)&m_pMediaControl);
     m_pMediaControl->Run();


     SetTimer(101, 100, NULL);

     return TRUE;
  }

  return FALSE;
}

Downloads

Download demo project - 9.79 Kb
Download source - 826 Kb
Home
Mobile Site | Full Site
Copyright 2018 © QuinStreet Inc. All Rights Reserved