public int Invoke(IMFAsyncResult asyncResult) { IMFMediaEvent mediaEvent = null; this.mediaSession.EndGetEvent(asyncResult, out mediaEvent); // Get the session event type uint type = Consts.MESessionUnknown; mediaEvent.GetType(out type); // Get the session event HRESULT int status = 0; mediaEvent.GetStatus(out status); // Fire the C# event if (this.MediaEvent != null) { this.MediaEvent(type, status); } // Get the next session event this.mediaSession.BeginGetEvent(this, null); return(0); }
private static HResult RunMediaSession(IMFMediaSession mediaSession) { HResult hr = S_OK; bool receiveSessionEvent = true; while (receiveSessionEvent) { HResult hrStatus = S_OK; IMFMediaEvent mediaEvent = null; MediaEventType eventType = MediaEventType.MEUnknown; MFTopoStatus topoStatus = MFTopoStatus.Invalid; hr = mediaSession.GetEvent(MFEventFlag.None, out mediaEvent); if (Succeeded(hr)) { hr = mediaEvent.GetStatus(out hrStatus); } if (Succeeded(hr)) { hr = mediaEvent.GetType(out eventType); } if (Succeeded(hr) && Succeeded(hrStatus)) { switch (eventType) { case MediaEventType.MESessionTopologySet: Debug.WriteLine("MediaSession:TopologySetEvent"); break; case MediaEventType.MESessionTopologyStatus: Debug.WriteLine("MediaSession:TopologStatusEvent"); hr = mediaEvent.GetUINT32(MF_EVENT_TOPOLOGY_STATUS, out int topoStatusInt); if (Succeeded(hr)) { topoStatus = (MFTopoStatus)topoStatusInt; switch (topoStatus) { case MFTopoStatus.Ready: Debug.WriteLine("MediaSession:TopologyStatus: MFTopoStatus.Ready"); hr = mediaSession.Start(); break; default: Debug.WriteLine("MediaSession:TopologyStatus: MFTopoStatus." + topoStatus); break; } } break; case MediaEventType.MESessionClosed: Debug.WriteLine("MediaSession:SessionClosedEvent"); receiveSessionEvent = false; break; case MediaEventType.MESessionStopped: Debug.WriteLine("MediaSession:SesssionStoppedEvent"); hr = mediaSession.Stop(); break; default: Debug.WriteLine("MediaSession:Event: " + eventType); break; } mediaEvent = null; if (Failed(hr) || Failed(hrStatus)) { receiveSessionEvent = false; } } } return(hr); }
void IMFAsyncCallback.Invoke(IMFAsyncResult pResult) { IMFMediaEvent pEvent = null; MediaEventType meType = MediaEventType.MEUnknown; // Event type int hrStatus = 0; // Event status MFTopoStatus TopoStatus = MFTopoStatus.Invalid; // Used with MESessionTopologyStatus event. try { // Get the event from the event queue. m_pSession.EndGetEvent(pResult, out pEvent); // Get the event type. pEvent.GetType(out meType); // Get the event status. If the operation that triggered the event did // not succeed, the status is a failure code. pEvent.GetStatus(out hrStatus); TRACE(string.Format("Media event: " + meType.ToString())); // Check if the async operation succeeded. if (Succeeded(hrStatus)) { // Switch on the event type. Update the internal state of the CPlayer as needed. switch (meType) { case MediaEventType.MESessionTopologyStatus: // Get the status code. int i; pEvent.GetUINT32(MFAttributesClsid.MF_EVENT_TOPOLOGY_STATUS, out i); TopoStatus = (MFTopoStatus)i; switch (TopoStatus) { case MFTopoStatus.Ready: OnTopologyReady(pEvent); break; default: // Nothing to do. break; } break; case MediaEventType.MESessionStarted: OnSessionStarted(pEvent); break; case MediaEventType.MESessionPaused: OnSessionPaused(pEvent); break; case MediaEventType.MESessionClosed: OnSessionClosed(pEvent); break; case MediaEventType.MEEndOfPresentation: OnPresentationEnded(pEvent); break; } } else { // The async operation failed. Notify the application NotifyError(hrStatus); } } finally { // Request another event. if (meType != MediaEventType.MESessionClosed) { m_pSession.BeginGetEvent(this, null); } SafeRelease(pEvent); } }
/// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+= /// <summary> /// Part of the IMFAsyncCallback interface. This is called when an /// asynchronous operation is completed. /// </summary> /// <param name="pResult">Pointer to the IMFAsyncResult interface. </param> /// <returns>S_OK for success, others for fail</returns> /// <history> /// 01 Nov 18 Cynic - Originally Written /// </history> HResult IMFAsyncCallback.Invoke(IMFAsyncResult pResult) { HResult hr; IMFMediaEvent eventObj = null; MediaEventType meType = MediaEventType.MEUnknown; // Event type HResult hrStatus = 0; // Event status lock (this) { try { if (MediaSession == null) { return(HResult.S_OK); } // Complete the asynchronous request this is tied to the previous BeginGetEvent call // and MUST be done. The output here is a pointer to the IMFMediaEvent interface describing // this event. Note we MUST release this interface hr = MediaSession.EndGetEvent(pResult, out eventObj); if (hr != HResult.S_OK) { throw new Exception("IMFAsyncCallback.Invoke call to MediaSession.EndGetEvent failed. Err=" + hr.ToString()); } if (eventObj == null) { throw new Exception("IMFAsyncCallback.Invoke call to MediaSession.EndGetEvent failed. eventObj == null"); } // Get the event type. The event type indicates what happened to trigger the event. // It also defines the meaning of the event value. hr = eventObj.GetType(out meType); if (hr != HResult.S_OK) { throw new Exception("IMFAsyncCallback.Invoke call to IMFMediaEvent.GetType failed. Err=" + hr.ToString()); } // Get the event status. If the operation that generated the event was successful, // the value is a success code. A failure code means that an error condition triggered the event. hr = eventObj.GetStatus(out hrStatus); if (hr != HResult.S_OK) { throw new Exception("IMFAsyncCallback.Invoke call to IMFMediaEvent.GetStatus failed. Err=" + hr.ToString()); } // Check if we are being told that the the async event succeeded. if (hrStatus != HResult.S_OK) { // The async operation failed. Notify the application if (MediaSessionAsyncCallBackError != null) { MediaSessionAsyncCallBackError(this, "Error Code =" + hrStatus.ToString(), null); } } else { // we are being told the operation succeeded and therefore the event contents are meaningful. // Switch on the event type. switch (meType) { // we let the app handle all of these. There is not really much we can do here default: MediaSessionAsyncCallBackEvent(this, eventObj, meType); break; } } } catch (Exception ex) { // The async operation failed. Notify the application if (MediaSessionAsyncCallBackError != null) { MediaSessionAsyncCallBackError(this, ex.Message, ex); } } finally { // Request another event if we are still operational. if (((meType == MediaEventType.MESessionClosed) || (meType == MediaEventType.MEEndOfPresentation)) == false) { // Begins an asynchronous request for the next event in the queue hr = MediaSession.BeginGetEvent(this, null); if (hr != HResult.S_OK) { throw new Exception("IMFAsyncCallback.Invoke call to MediaSession.BeginGetEvent failed. Err=" + hr.ToString()); } } // release the event we just processed if (eventObj != null) { Marshal.ReleaseComObject(eventObj); } } } // bottom of lock(this) return(HResult.S_OK); }