Suivi d’événements dans DirectShow
DirectShow prend en charge Suivi d’v nements pour Windows (ETW), qui peut être utilisé pour créer des journaux d’événements pour l’instrumentation ou le débogage. pour plus d’informations sur ETW, reportez-vous à la documentation SDK Windows. pour consommer des événements ETW dans une application DirectShow, vous devez activer le suivi, puis traiter les événements de trace. Effectuez les étapes suivantes.
Définir les clés de Registre nécessaires
Pour activer le suivi sur l’ordinateur de l’utilisateur, commencez par définir les clés de Registre suivantes :
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\DirectX
GlitchInstrumentation = 0x00000001 (REG_DWORD)
HKEY_LOCAL_MACHINE\SOFTWARE\DEBUG\Quartz.dll
PERFLOG = 0x00000001 (REG_DWORD)
Ces clés s’appliquent aux fichiers binaires de mise en sortie et de débogage.
Activer le suivi dans votre application
Pour activer le suivi dans votre application, procédez comme suit :
- Appelez StartTrace pour démarrer une nouvelle session de suivi.
- Appelez EnableTrace pour activer le suivi. le guid du fournisseur pour DirectShow est guid _ DSHOW _ CTL.
- Avant de quitter l’application, appelez StopTrace pour fermer la session de suivi.
Traiter les événements
Pour traiter les événements, procédez comme suit :
- Appelez OpenTrace pour ouvrir le suivi à des fins de traitement.
- Appelez ProcessTrace pour traiter les événements.
- Dans le rappel ProcessTrace , utilisez le GUID d’événement pour rechercher le type d’événement. Le GUID d’événement indique la structure utilisée pour les données d’événement. Consultez la page GUID des événements de trace.
- Appelez CloseTrace pour fermer le descripteur de trace.
Exemple de code
Le code suivant montre une classe d’assistance qui active le suivi. Ce code montre comment écrire des événements dans un fichier journal, qui peut être traité une fois la session terminée. Vous pouvez également traiter les événements en temps réel. pour plus d’informations, reportez-vous à la documentation ETW dans le SDK Windows.
#include <wmistr.h>
#include <evntrace.h>
#include <perfstruct.h>
// Event classes. These are defined in dxmperf.h.
#ifndef DXMPERF_VIDEOREND
#define DXMPERF_VIDEOREND 0x00000001
#endif
#ifndef AUDIOBREAK_BIT
#define AUDIOBREAK_BIT 0x00000010
#endif
// This structure extends the EVENT_TRACE_PROPERTIES by adding fields
// for the name of the WMI session name and the log file.
struct PERFMON_LOGGERINFO
{
EVENT_TRACE_PROPERTIES TraceProperties;
WCHAR wcSessionName[ MAX_PATH ]; // Session name.
WCHAR wcLogFileName[ MAX_PATH ]; // Log file.
};
// Helper class for DirectShow event tracing.
class CTrace
{
public:
CTrace() : m_SessionLogger((TRACEHANDLE) INVALID_HANDLE_VALUE)
{
ZeroMemory(&m_LogInfo, sizeof(&m_LogInfo));
}
// Start: Starts a trace session.
HRESULT Start(WCHAR *wszLogFile)
{
const WCHAR* wszSessionName = L"PerfMon_DirectShow";
HRESULT hr = S_OK;
ULONG result;
ZeroMemory(&m_LogInfo, sizeof(m_LogInfo));
EVENT_TRACE_PROPERTIES& prop = m_LogInfo.TraceProperties;
prop.Wnode.BufferSize = sizeof(m_LogInfo); // Size of the structure.
prop.Wnode.Flags = WNODE_FLAG_TRACED_GUID; // Must be this value.
// Use the QPC (high resolution timer).
prop.Wnode.ClientContext = 1;
prop.Wnode.Guid = GUID_DSHOW_CTL; // Event provider GUID.
prop.LogFileMode =
EVENT_TRACE_FILE_MODE_CIRCULAR | EVENT_TRACE_USE_PAGED_MEMORY;
prop.EnableFlags =
EVENT_TRACE_FLAG_PROCESS; // Process events.
// Set the offset from the start of the structure to the log file name.
prop.LogFileNameOffset =
sizeof(m_LogInfo.TraceProperties) + sizeof(m_LogInfo.wcSessionName);
// Set the offset from the start of the structure to the session name.
prop.LoggerNameOffset = sizeof(m_LogInfo.TraceProperties);
// Copy the names into the structure.
StringCchCopy(m_LogInfo.wcSessionName, MAX_PATH, wszSessionName);
StringCchCopy(m_LogInfo.wcLogFileName, MAX_PATH, wszLogFile);
// Start the trace.
result = StartTrace(
&m_SessionLogger,
m_LogInfo.wcSessionName,
&m_LogInfo.TraceProperties
);
if (result == ERROR_SUCCESS)
{
result = EnableTrace(
TRUE, // Enable.
AUDIOBREAK_BIT | DXMPERF_VIDEOREND, // Event classes.
TRACE_LEVEL_VERBOSE, // Trace level.
&GUID_DSHOW_CTL, // Event provider.
m_SessionLogger // Session handle.
);
}
if (result != ERROR_SUCCESS)
{
hr = __HRESULT_FROM_WIN32(result);
}
return hr;
}
HRESULT Stop()
{
HRESULT hr = S_OK;
// Stop the trace.
if (m_SessionLogger != (TRACEHANDLE)INVALID_HANDLE_VALUE)
{
LONG result = 0;
result = EnableTrace(FALSE, 0, 0, &GUID_DSHOW_CTL, m_SessionLogger);
if (result == ERROR_SUCCESS)
{
result = StopTrace(
m_SessionLogger,
m_LogInfo.wcSessionName,
&m_LogInfo.TraceProperties);
}
m_SessionLogger = (TRACEHANDLE)INVALID_HANDLE_VALUE;
if (result != ERROR_SUCCESS)
{
hr = __HRESULT_FROM_WIN32(result);
}
}
return hr;
}
protected:
TRACEHANDLE m_SessionLogger;
PERFMON_LOGGERINFO m_LogInfo;
};
Le code suivant montre comment traiter le journal des événements :
// Callback for event processing.
VOID WINAPI EventCallback(PEVENT_TRACE pEvent)
{
PERFINFO_DSHOW_STREAMTRACE *pStreamTrace = NULL;
PERFINFO_DSHOW_AVREND *pVideoRender = NULL;
PERFINFO_DSHOW_AUDIOBREAK *pAudioBreak = NULL;
if (pEvent->Header.Guid == GUID_STREAMTRACE)
{
pStreamTrace = (PPERFINFO_DSHOW_STREAMTRACE)pEvent->MofData;
switch (pStreamTrace->id)
{
// TODO: Handle the event.
}
}
else if(pEvent->Header.Guid == GUID_VIDEOREND)
{
pVideoRender = (PPERFINFO_DSHOW_AVREND)pEvent->MofData;
// TODO: Handle the event.
}
else if(pEvent->Header.Guid == GUID_AUDIOBREAK)
{
pAudioBreak = (PPERFINFO_DSHOW_AUDIOBREAK)pEvent->MofData;
// TODO: Handle the event.
}
}
void ProcessTraceEvents(WCHAR *wszLogFile)
{
ULONG result = 0;
EVENT_TRACE_LOGFILE logfile;
ZeroMemory(&logfile, sizeof(logfile));
logfile.LogFileName = wszLogFile;
logfile.EventCallback = EventCallback;
TRACEHANDLE handle = OpenTrace(&logfile);
if (handle != (TRACEHANDLE)INVALID_HANDLE_VALUE)
{
result = ProcessTrace(&handle, 1, NULL, NULL);
CloseTrace(handle);
}
}