IMediaEvent Interface


This interface supports event notification from the filter graph and filters within it to the application. It is decoupled by using a queuing scheme rather than callbacks, because events can be notified from worker threads that cannot safely call back into application code.

An event code and two DWORD values represent event notification information. Your application can use this for typical completion of asynchronous operations, errors that occur during asynchronous operation, or user-initiated events, such as when a user clicks a hot spot.

Filters within the filter graph and the filter graph itself raise event notifications. Possible events include playback completion or asynchronous playback errors. In addition, the filter graph provides a method to generate events at specific reference clock times. The filter graph exposes an IMediaEventSink interface that the filters within the graph can call to pass event notifications to the application.

Event notifications are placed in a queue. An application calls the IMediaEvent::GetEvent method to retrieve the next notification from the queue. This method blocks until there is an event to return. The GetEvent time-out parameter (msTimeout) allows the application to specify the time, in milliseconds, to wait for an event, including values of zero and INFINITE. After calling GetEvent, applications should always call FreeEventParams to release any resource associated with the event.

In addition, applications can retrieve the event handle. IMediaEvent::GetEventHandle returns a handle to a manual-reset event created by the Microsoft® Win32® CreateEvent function. This event is in a signaled state as long as there are event notifications to collect. The IMediaEvent::GetEvent method clears the event when there are no more event notifications to collect. This allows an application to use an application programming interface (API), such as MsgWaitForMultipleObjects, to wait for events and other occurrences at the same time. This event handle will be closed when the filter graph is released; therefore, applications should ensure that they are not using it after this point.

The filter graph manager handles some events raised by filters that are not passed to the application. One example of this is the EC_REPAINT event notification. By default the filter graph manager handles this event by pausing the filter graph and repainting the video renderer's static images. An application can override default handling for a specific event by calling the IMediaEvent::CancelDefaultHandling method with the event value as a parameter. The IMediaEvent::RestoreDefaultHandling method reinstates default handling for the specified event value. These methods have no effect on events that have no default handling.

If an error occurs during the transition to a running state on any filter, the IMediaControl::Run method returns an error value. In this case, some filters within the graph might be running successfully. The filter graph leaves it up to the application to determine whether to stop the graph in case of an error. After the IMediaControl::Run method has returned, event notifications report any additional errors. The EC_ERRORABORT and EC_USERABORT event notifications indicate that playback has probably stopped in the graph (certainly in the filter that reported it). Other errors and events indicate that it is still running. Note, however, that in all cases the graph remains in running mode until the application explicitly changes it to stopped or paused mode.

If the streams in the filter graph detect the end of the stream, the streams report this by using the EC_COMPLETE event notification. The filter graph manager asks filters if they can report EC_COMPLETE by means of seekable renderers.

A seekable renderer is one that supports the IMediaPosition interface from the filter and that has only input pins, or whose input pins report through IPin::QueryInternalConnections that they are rendered. The filter graph uses IPin::QueryInternalConnections and IMediaPosition to detect seekable renderers. A seekable renderer should report EC_COMPLETE when all seekable streams on that filter have reached the stream's end.

A renderer can produce EC_COMPLETE (and a regular filter produce EndOfStream) for one of four reasons as follows:

The filter graph manager will not pass EC_COMPLETE to the application until an EC_COMPLETE event notification has been received from each stream. For example, if a live camera stream is playing as the background for a video playing from a file, the application will be notified about EC_COMPLETE when the video and audio streams from the file have come to the stream's end, even though the live source is still playing. In this case, too, the filter graph remains in running mode until the application explicitly calls the IMediaControl::Pause or IMediaControl::Stop method.

Your application can disable the aggregation of EC_COMPLETE messages by calling IMediaEvent::CancelDefaultHandling with EC_COMPLETE as the parameter. In this case, all EC_COMPLETE events raised by the filters will be passed directly to the application.

For a list of system-defined event notifications, see Event Notification Codes.

Note All events must be handled if a handle to an IMediaEvent interface is obtained otherwise events will pile up and cause the heap to be used up.

When to Implement

The filter graph manager implements this interface.

You can use the CMediaEvent class, which handles the IDispatch implementation for Automation, to help implement this interface.

When to Use

Applications use this interface to retrieve event notifications or event handles from the filter graph manager. For example, an application can retrieve the EC_COMPLETE notification to find out when a media stream has been rendered completely.

Methods in Vtable Order
IUnknown methods Description
QueryInterface Returns pointers to supported interfaces.
AddRef Increments the reference count.
Release Decrements the reference count.
IDispatch methods Description
GetTypeInfoCount Determines whether there is type information available for this dispinterface.
GetTypeInfo Retrieves the type information for this dispinterface if GetTypeInfoCount returned successfully.
GetIDsOfNames Converts text names of properties and methods (including arguments) to their corresponding DISPIDs.
Invoke Calls a method or accesses a property in this dispinterface if given a DISPID and any other necessary parameters.
IMediaEvent methods Description
GetEventHandle Retrieves a handle to a manual-reset event that will be signaled.
GetEvent Retrieves the next notification event.
WaitForCompletion Blocks execution of the application thread until the graph's operation finishes.
CancelDefaultHandling Cancels any default handling of the specified event by the filter graph.
RestoreDefaultHandling Restores default handling for this event.
FreeEventParams Frees resources associated with the parameters to an event.


IMediaEvent::CancelDefaultHandling

IMediaEvent Interface

Cancels any default handling by the filter graph of the specified event and ensures that it is passed to the application.

HRESULT CancelDefaultHandling(
  long lEvCode
  );

Parameters
lEvCode
Event code for which to cancel default handling.
Return Values

Returns S_OK if successful, or S_FALSE if the event does not have any default handling.

Remarks

Currently the filter graph manager applies default handling only to EC_COMPLETE and EC_REPAINT.


IMediaEvent::FreeEventParams

IMediaEvent Interface

Frees resources associated with the parameters of an event.

HRESULT FreeEventParams(
  long lEventCode,
  long lParam1,
  long lParam2
  );

Parameters
lEventCode
[in] Next event notification.
lParam1
[in] First parameter of the event.
lParam2
[in] Second parameter of the event.
Return Values

Returns an HRESULT value.

Remarks

Event parameters can be of type LONG or BSTR. If a BSTR is passed as an event, it will have been allocated by the task allocator and should be freed using this method. No reference-counted interfaces are passed to an application using IMediaEvent::GetEvent because these cannot be overridden by IMediaEvent::CancelDefaultHandling. Therefore, this method is not used to release interfaces.


IMediaEvent::GetEvent

IMediaEvent Interface

Retrieves the next notification event.

HRESULT GetEvent(
  long * lEventCode,
  long * lParam1,
  long * lParam2,
  long msTimeout
  );

Parameters
IEventCode
[out] Next event notification.
lParam1
[out] First parameter of the event.
lParam2
[out] Second parameter of the event.
msTimeout
[in] Time, in milliseconds, to wait before assuming that there are no events.
Return Values

Returns an HRESULT value that depends on the implementation of the interface. If the time-out is zero and no event is waiting, or if the time-out elapses before an event appears, this method returns E_ABORT.

Remarks

The application can pass a time-out value of INFINITE to indicate that the method should block until there is an event; however, applications should avoid using INFINITE. Threads cannot process any messages while waiting in GetEvent. If you call GetEvent from the thread that processes Windows messages, specify only small wait times on the call in order to remain responsive to user input. This is most important when streaming data from a source such as the Internet, because state transitions can take significantly more time to complete.

After calling GetEvent, applications should always call FreeEventParams to release any resource associated with the event.

For a list of notification codes and event parameter values, see Event Notification Codes.


IMediaEvent::GetEventHandle

IMediaEvent Interface

Retrieves a handle to a manual-reset event that will be signaled as long as there are event notifications to deliver.

HRESULT GetEventHandle(
  OAEVENT * hEvent
  );

Parameters
hEvent
[out] Handle for the event.
Return Values

Returns an HRESULT value.

Remarks

You can monitor events (including the retrieved event) and messages on a single thread; to do this, declare a HANDLE variable, cast it to an OAEVENT pointer, then pass it to GetEventHandle. You must cast the pointer to an OAEVENT pointer because HANDLE is not a valid OLE Automation type. The following code fragment demonstrates how to cast and use the HANDLE.


HANDLE hEvent;
GetEventHandle( (OAEVENT*) &hEvent );

You can pass the retrieved event handle to the Microsoft Win32 WaitForMultipleObjects or MsgWaitForMultipleObjects functions to wait for event notifications at the same time as other messages and events on a single thread. The implementation of GetEvent sets and resets the handle within the application, so applications should not reset the handle themselves.


IMediaEvent::RestoreDefaultHandling

IMediaEvent Interface

Reinstates the normal default handling by a filter graph for the specified event, if there is one.

HRESULT RestoreDefaultHandling(
  long lEvCode
  );

Parameters
lEvCode
[in] Event to restore.
Return Values

Returns S_OK if successful, or S_FALSE if there is no default handling for this event.

Remarks

Events that have default handling in place, such as EC_REPAINT, are not typically passed to the application.


IMediaEvent::WaitForCompletion

IMediaEvent Interface

Blocks execution of the application thread until the graph's operation finishes.

HRESULT WaitForCompletion(
  long msTimeout,
  long * pEvCode
  );

Parameters
msTimeout
[in] Duration of the time-out, in milliseconds. Pass zero to return immediately. To block indefinitely, pass INFINITE.
pEvCode
[out] Event that terminated the wait. This value can be one of the following:
Value Meaning
EC_COMPLETE Operation completed.
EC_ERRORABORT Error. Playback can't continue.
EC_USERABORT User terminated the operation.
Zero (0) Operation has not completed.
Return Values

Returns one of the following HRESULT values.
Value Meaning
E_ABORT Function timed out before the operation completed. This is equivalent to a zero pEvCode value.
S_OK Operation completed.

Remarks

This method is the equivalent of blocking until the event notification EC_COMPLETE, EC_ERRORABORT, or EC_USERABORT is received, or the time-out occurs.

When this method returns, the filter graph is still running. This method assumes that separate calls to the IMediaEvent interface are not being made. This method fails if the graph is not in or transitioning into a running state.

The time-out parameter (msTimeout) specifies the length of time to wait for completion. To test if the operation completed, specify a zero msTimeout value and check the event code value (pEvCode) for zero, indicating that the operation is not completed.

© 1997 Microsoft Corporation. All rights reserved. Terms of Use.