Step 2: Create the CPlayer Object

This topic is step 2 of the tutorial How to Play Media Files with Media Foundation. The complete code is shown in the topic Media Session Playback Example.

To create an instance of the CPlayer class, the application calls the static CPlayer::CreateInstance method. This method takes the following parameters:

  • hVideo specifies the window to display video.
  • hEvent specifies a window to receive events. It is valid to pass the same handle for both window handles.
  • ppPlayer receives a pointer to a new CPlayer instance.

The following code shows the CreateInstance method:

//  Static class method to create the CPlayer object.

HRESULT CPlayer::CreateInstance(
    HWND hVideo,                  // Video window.
    HWND hEvent,                  // Window to receive notifications.
    CPlayer **ppPlayer)           // Receives a pointer to the CPlayer object.
{
    if (ppPlayer == NULL)
    {
        return E_POINTER;
    }

    CPlayer *pPlayer = new (std::nothrow) CPlayer(hVideo, hEvent);
    if (pPlayer == NULL)
    {
        return E_OUTOFMEMORY;
    }

    HRESULT hr = pPlayer->Initialize();
    if (SUCCEEDED(hr))
    {
        *ppPlayer = pPlayer;
    }
    else
    {
        pPlayer->Release();
    }
    return hr;
}

HRESULT CPlayer::Initialize()
{
    // Start up Media Foundation platform.
    HRESULT hr = MFStartup(MF_VERSION);
    if (SUCCEEDED(hr))
    {
        m_hCloseEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
        if (m_hCloseEvent == NULL)
        {
            hr = HRESULT_FROM_WIN32(GetLastError());
        }
    }
    return hr;
}

The following code shows the CPlayer constructor:

CPlayer::CPlayer(HWND hVideo, HWND hEvent) : 
    m_pSession(NULL),
    m_pSource(NULL),
    m_pVideoDisplay(NULL),
    m_hwndVideo(hVideo),
    m_hwndEvent(hEvent),
    m_state(Closed),
    m_hCloseEvent(NULL),
    m_nRefCount(1)
{
}

The constructor does the following:

  1. Calls MFStartup to initialize the Media Foundation platform.
  2. Creates an auto-reset event. This event is used when closing the Media Session; see Step 7: Shut Down the Media Session.

The destructor shuts down the Media Session, as described in Step 7: Shut Down the Media Session.

CPlayer::~CPlayer()
{
    assert(m_pSession == NULL);  
    // If FALSE, the app did not call Shutdown().

    // When CPlayer calls IMediaEventGenerator::BeginGetEvent on the
    // media session, it causes the media session to hold a reference 
    // count on the CPlayer. 
    
    // This creates a circular reference count between CPlayer and the 
    // media session. Calling Shutdown breaks the circular reference 
    // count.

    // If CreateInstance fails, the application will not call 
    // Shutdown. To handle that case, call Shutdown in the destructor. 

    Shutdown();
}

Note that the constructor and destructor are both protected class methods. The application creates the object using the static CreateInstance method. It destroys the object by calling IUnknown::Release, rather than explicitly using delete.

Next: Step 3: Open a Media File

Audio/Video Playback

How to Play Media Files with Media Foundation