CodeGuru
Earthweb Search
Login Forums Wireless Jars Gamelan Developer.com
CodeGuru Navigation
RSS Feeds

RSSAll

RSSVC++/C++

RSS.NET/C#

RSSVB

See more EarthWeb Network feeds

follow us on Twitter

Member Sign In
User ID:
Password:
Remember Me:
Forgot Password?
Not a member?
Click here for more information and to register.

Become a Marketplace Partner

jobs.internet.com

internet.commerce
Partners & Affiliates
















Home >> Visual C++ / C++ >> Graphics & Multimedia >> DirectX


Direct Show Application for Previewing and Playing Movies
Rating: none

Ashar Maqbool Khan (view profile)
November 6, 2001

.
(continued)






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

Tools:
Add www.codeguru.com to your favorites
Add www.codeguru.com to your browser search box
IE 7 | Firefox 2.0 | Firefox 1.5.x
Receive news via our XML/RSS feed







RATE THIS ARTICLE:   Excellent  Very Good  Average  Below Average  Poor  

(You must be signed in to rank an article. Not a member? Click here to register)

Latest Comments:
Problem opening file with bad indexes - sbarbaz (09/29/2006)
Compile error in Visual C++.net - Aryana (02/23/2005)
To make work this sample - fft2d (07/15/2004)
(possible) linking problem solution - Legacy CodeGuru (11/26/2003)
XP directx 9 error link !!! urgent - Legacy CodeGuru (10/09/2003)

View All Comments
Add a Comment:
Title:
Comment:
Pre-Formatted: Check this if you want the text to display with the formatting as typed (good for source code)



(You must be signed in to comment on an article. Not a member? Click here to register)

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs