étape 6 : gérer les événements de Graph
Cette rubrique est l’étape 6 de la lecture audio/vidéo du didacticiel dans DirectShow. le code complet est présenté dans la rubrique exemple de lecture DirectShow.
lorsque l’application crée une nouvelle instance du filtre Graph Manager, elle appelle IMediaEventEx :: SetNotifyWindow. Cette méthode inscrit la fenêtre d’application pour recevoir des événements du graphique de filtre.
hr = m_pGraph->QueryInterface(IID_PPV_ARGS(&m_pEvent));
if (FAILED(hr))
{
goto done;
}
// Set up event notification.
hr = m_pEvent->SetNotifyWindow((OAHWND)m_hwnd, WM_GRAPH_EVENT, NULL);
if (FAILED(hr))
{
goto done;
}
La valeur WM_GRAPH_EVENT est un message de fenêtre privée. Chaque fois que l’application reçoit ce message, elle appelle la DShowPlayer::HandleGraphEvent méthode.
case WM_GRAPH_EVENT:
g_pPlayer->HandleGraphEvent(OnGraphEvent);
return 0;
La méthode DShowPlayer::HandleGraphEvent effectue les opérations suivantes :
- Appelle IMediaEvent :: GetEvent dans une boucle pour obtenir tous les événements mis en file d’attente.
- Appelle une fonction de rappel (pfnOnGraphEvent).
- Appelle IMediaEvent :: FreeEventParams pour libérer les données associées à chaque événement.
// Respond to a graph event.
//
// The owning window should call this method when it receives the window
// message that the application specified when it called SetEventWindow.
//
// Caution: Do not tear down the graph from inside the callback.
HRESULT DShowPlayer::HandleGraphEvent(GraphEventFN pfnOnGraphEvent)
{
if (!m_pEvent)
{
return E_UNEXPECTED;
}
long evCode = 0;
LONG_PTR param1 = 0, param2 = 0;
HRESULT hr = S_OK;
// Get the events from the queue.
while (SUCCEEDED(m_pEvent->GetEvent(&evCode, ¶m1, ¶m2, 0)))
{
// Invoke the callback.
pfnOnGraphEvent(m_hwnd, evCode, param1, param2);
// Free the event data.
hr = m_pEvent->FreeEventParams(evCode, param1, param2);
if (FAILED(hr))
{
break;
}
}
return hr;
}
Le code suivant illustre la fonction de rappel qui est passée à DShowPlayer::HandleGraphEvent . La fonction gère le nombre minimal d’événements de graphe (EC _ Complete, EC _ ERRORABORTet EC _ USERABORT); vous pouvez développer la fonction pour gérer des événements supplémentaires.
void CALLBACK OnGraphEvent(HWND hwnd, long evCode, LONG_PTR param1, LONG_PTR param2)
{
switch (evCode)
{
case EC_COMPLETE:
case EC_USERABORT:
g_pPlayer->Stop();
break;
case EC_ERRORABORT:
NotifyError(hwnd, L"Playback error.");
g_pPlayer->Stop();
break;
}
}