Direct Show Application for Previewing and Playing Movies

.



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


Comments

  • Problem opening file with bad indexes

    Posted by sbarbaz on 09/29/2006 11:51am

    Hi, using this application I tryed opening an avi with bad indexes and I had a problem. If I play them using MPC I got not problems, when i use IGraphBuilder:RenderFile(), it can not build the graph. Using Graph Edit, I noticed MPC uses different filters when it plays video with bad indexes, but that filters are not available in DirectShow, and using IGraphBuilder:RenderFile() the GraphBuilder can't find them. Does someone know something about the filter used by MPC? Does it use internal filter? Is it possible using them? Can you play avi with bad indexes using directshow? If yes, what codec are you using? Is available source code for repairing an avi with bad indexes? Please if you know something that can help me, or if you can redirect me to a better discussion place for this topic, reply... I'm very afraid with this problem. Thanks in advance for help.

    Reply
  • Compile error in Visual C++.net

    Posted by Aryana on 02/23/2005 12:45am

    Hi, I tried to compile your code in Visual C++.net and win2000. I got several occurances of these errors in afxstr.h: C2976: 'StrTraitMFC_DLL' : too few template arguments C3203: 'StrTraitMFC_DLL' : class template invalid as template argument for template parameter 'StringTraits', expected a real type What is the solution? Can this code be used in Visual C++.net? Thanks

    • Set project properties

      Posted by AndreyO on 02/22/2006 05:07am

      Set "Minimize CRT use in ATL" to "No"

      Reply
    • Solution?

      Posted by stoff on 08/20/2005 01:39am

      I have the same problem, have any one found a solution?

      Reply
    Reply
  • To make work this sample

    Posted by fft2d on 07/15/2004 08:04am

    After insatalling DirectX SDK, Open project file of DirectAPP sample. With Visual 6.0, go to Project settings, open link tab. Remove all the Object/library modules libs without quartz.lib and strmmiids.lib. Open "StdAfx.h" and make " strmif.h" directory suitable for your SDK system. With Visual Studio. Net,Open Solution properties and go to Linker configuration and input property in turn. Take care of most of libs exactly as above VS6.0 on Additional Dependencies item and also modify "StdAfx.h". This sample works fine with Both of 6.0 and .Net. I think there is still one bugs here. When playing movie file for the first time, no video stream's on Picture box even though we can here audio stream. If so,Try another file. After that every thing's will be fine. I didn't check the preview mode. I hope this will be helpful to everybody. Good sample........

    Reply
  • (possible) linking problem solution

    Posted by Legacy on 11/26/2003 12:00am

    Originally posted by: fro

    Hello everybody,
    I had problems with linking, something like
    "error LNK2001: unresolved external symbol _CLSID_CaptureGraphBuilder2
    error LNK2001: unresolved external symbol _IID_ICaptureGraphBuilder2 "
    despite of including in 'Tools->Options->Directories->(show directories for)Library files' path to 'DX9_SDK\Lib'(where strmiids.lib was), and setting this path to the top of directories' tree.
    If you have a problem similiar to mentioned above, try this:
    'Project->Add to project->Files', set 'file type' in dialog to 'library files', find and choose strmiids.lib. It has helped me.

    Greetings for everyone

    f.

    P.S. Sorry for my (poor) English, but I'am still working on improving it.

    • To make work this sample

      Posted by fft2d on 07/15/2004 08:02am

      After insatalling DirectX SDK, Open project file of DirectAPP sample. With Visual 6.0, go to Project settings, open link tab. Remove all the Object/library modules libs without quartz.lib and strmmiids.lib. Open "StdAfx.h" and make " strmif.h" directory suitable for your SDK system. With Visual Studio. Net,Open Solution properties and go to Linker configuration and input property in turn. Take care of most of libs exactly as above VS6.0 on Additional Dependencies item and also modify "StdAfx.h". This sample works fine with Both of 6.0 and .Net. I think there is still one bugs here. When playing movie file for the first time, no video stream's on Picture box even though we can here audio stream. If so,Try another file. After that every thing's will be fine. I didn't check the preview mode. I hope this will be helpful to everybody. Good sample........

      Reply
    Reply
  • XP directx 9 error link !!! urgent

    Posted by Legacy on 10/09/2003 12:00am

    Originally posted by: joseph

    I have all links to libraries yet i cant compile it... when it says not all windows can be opened does it mean i am lacking some important links? i am using XP and directX 9

    errors
    error LNK2001: unresolved external symbol _CLSID_CaptureGraphBuilder2
    error LNK2001: unresolved external symbol _IID_ICaptureGraphBuilder2
    ..\Debug/Movie.exe : fatal error LNK1120: 2 unresolved externals

    pls help me... thanx

    Reply
  • Detecting Source.?

    Posted by Legacy on 08/04/2003 12:00am

    Originally posted by: Dillip

    Hi there.,

    In my application , there are 3 video source attached . One is from Webcam connected to USB and other one is from TV-Tuner card which sends 2 sources i,e. (a) S-Video ,(b) Composite Video (Composite line). But, when I detect, it shows me only 2 sources in the list . I tested with your application also.. but I receive the same ..only 2 sources .. 1. Webcam & other one is either S-Video or Composite Video . How can I detect these 2 sources(S-Video & Composite Video) comming from TV tuner card ..?

    Thanks in Advance.!~

    Best Regards,
    DILLIP.

    Reply
  • directshow

    Posted by Legacy on 07/15/2003 12:00am

    Originally posted by: Ram

    hello!
    i m an intrested dev. i m working 4 proj n my module is to dev a mpeg player. i kneed to sep the A and V. i m using directshow methods. i m not able to operate the mpeg1 splitter. plz help me on this issue n send me some code to use those mpeg1 splitter filter easily.
    thanking you,
    Ram

    Reply
  • DirectShow Networking Problem

    Posted by Legacy on 06/20/2003 12:00am

    Originally posted by: Muzammil

    I am developing a client/server application, server captures the video from video input device and transmit it over the network to its client(s). I am getting the video and its buffer too but don't know how to transmit it over the network. Does DirectShow provides any solution for it. I have little time so plz tell me with thanks

    Reply
  • errors while linking

    Posted by Legacy on 04/02/2003 12:00am

    Originally posted by: Christophe

    I have stille 2 errors while linking.

    PreviewMovieDlg.obj : error LNK2001: unresolved external symbol _CLSID_CaptureGraphBuilder2
    PreviewMovieDlg.obj : error LNK2001: unresolved external symbol _IID_ICaptureGraphBuilder2

    I have included strmbasd.lib!
    Can anyone help me?

    Christophe

    Reply
  • How to get a frame of image from a video?

    Posted by Legacy on 12/27/2002 12:00am

    Originally posted by: XU Jianfeng

    Such as avi,mpeg.Thanks.
    

    Reply
  • Loading, Please Wait ...

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Managing your company's financials is the backbone of your business and is vital to the long-term health and viability of your company. To continue applying the necessary financial rigor to support rapid growth, the accounting department needs the right tools to most efficiently do their job. Read this white paper to understand the 10 essentials of a complete financial management system and how the right solution can help you keep up with the rapidly changing business world.

  • Protecting business operations means shifting the priorities around availability from disaster recovery to business continuity. Enterprises are shifting their focus from recovery from a disaster to preventing the disaster in the first place. With this change in mindset, disaster recovery is no longer the first line of defense; the organizations with a smarter business continuity practice are less impacted when disasters strike. This SmartSelect will provide insight to help guide your enterprise toward better …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds