예제 #1
0
        /// <summary>
        /// Is called when a new media event code occurs in the session
        /// </summary>
        /// <param name="code">The event code that occured</param>
        /// <param name="param1">The first parameter sent by the graph</param>
        /// <param name="param2">The second parameter sent by the graph</param>
        protected virtual void OnMediaEvent(IMFMediaEvent code)
        {
            MediaEventType MedEventType;

            code.GetType(out MedEventType);
            //Trace.WriteLine("Session Event " + MedEventType.ToString());
            switch (MedEventType)
            {
            case MediaEventType.MEEndOfStream:
                InvokeMediaEnded(null);
                StopGraphPollTimer();
                break;

            case MediaEventType.MESessionPaused:
                break;

            case MediaEventType.MESessionTopologySet:
                AddAudioInterface();
                break;

            case MediaEventType.MESessionClosed:
            case MediaEventType.MESessionEnded:
            case MediaEventType.MEEndOfPresentation:
            case MediaEventType.MEError:
            case MediaEventType.MESessionNotifyPresentationTime:
            case MediaEventType.MESessionStarted:
            default:
                break;
            }
            //COMBase.SafeRelease(MedEventType);
        }
            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);
            }
        public int OnSourceEvent(IMFMediaEvent pEvent)
        {
            m_iCount++;

            MediaEventType typ;
            int hr = pEvent.GetType(out typ);
            MFError.ThrowExceptionForHR(hr);
            Debug.WriteLine(typ);

            return 0;
        }
예제 #4
0
        /// <summary>
        /// Is ran everytime a new media event occurs on the graph
        /// </summary>
        /// <param name="code">The Event code that occured</param>
        /// <param name="lparam1">The first event parameter sent by the graph</param>
        /// <param name="lparam2">The second event parameter sent by the graph</param>
        protected override void OnMediaEvent(IMFMediaEvent code)
        {
            MediaEventType MedEventType;

            code.GetType(out MedEventType);
            if (Loop && MedEventType == MediaEventType.MESessionEnded)
            {
                MediaPosition = 0;
            }
            else
            {
                /* Only run the base when we don't loop
                 * otherwise the default behavior is to
                 * fire a media ended event */
                base.OnMediaEvent(code);
            }

            //COMBase.SafeRelease(MedEventType);
        }
        /// <summary>
        /// Is ran everytime a new media event occurs on the graph
        /// </summary>
        /// <param name="code">The Event code that occured</param>
        /// <param name="lparam1">The first event parameter sent by the graph</param>
        /// <param name="lparam2">The second event parameter sent by the graph</param>
        protected override void OnMediaEvent(IMFMediaEvent code)
        {
            MediaEventType MedEventType;

            code.GetType(out MedEventType);
            if (MedEventType == MediaEventType.MESessionStarted /*|| MedEventType == MediaEventType.MESessionScrubSampleComplete*/)
            {
                if (bIsSeeking)
                {
                    bIsSeeking = false;
                    if (m_nextSeekTime > 0)
                    {
                        this.MediaPosition = m_nextSeekTime;
                        m_nextSeekTime     = -1;
                    }
                    //this.SpeedRatio = 1.0;
                }
            }
            base.OnMediaEvent(code);
        }
예제 #6
0
        public HResult Invoke(IMFAsyncResult result)
        {
            IMFMediaEvent  mediaEvent     = null;
            MediaEventType mediaEventType = MediaEventType.MEUnknown;
            bool           getNext        = true;

            try
            {
                _basePlayer.mf_MediaSession.EndGetEvent(result, out mediaEvent);
                mediaEvent.GetType(out mediaEventType);
                mediaEvent.GetStatus(out HResult errorCode);

                if (_basePlayer._playing)
                {
                    if (mediaEventType == MediaEventType.MEError ||
                        (_basePlayer._webcamMode && mediaEventType == MediaEventType.MEVideoCaptureDeviceRemoved) ||
                        (_basePlayer._micMode && mediaEventType == MediaEventType.MECaptureAudioSessionDeviceRemoved))
                    //if (errorCode < 0)
                    {
                        _basePlayer._lastError = errorCode;
                        errorCode = Player.NO_ERROR;
                        getNext   = false;
                    }

                    if (errorCode >= 0)
                    {
                        if (!getNext || mediaEventType == MediaEventType.MESessionEnded)
                        {
                            if (getNext)
                            {
                                _basePlayer._lastError = Player.NO_ERROR;
                                if (!_basePlayer._repeat)
                                {
                                    getNext = false;
                                }
                            }

                            Control control = _basePlayer._display;
                            if (control == null)
                            {
                                FormCollection forms = Application.OpenForms;
                                if (forms != null && forms.Count > 0)
                                {
                                    control = forms[0];
                                }
                            }
                            if (control != null)
                            {
                                control.BeginInvoke(CallEndOfMedia);
                            }
                            else
                            {
                                _basePlayer.AV_EndOfMedia();
                            }
                        }
                    }
                    else
                    {
                        _basePlayer._lastError = errorCode;
                    }
                }
                else
                {
                    _basePlayer._lastError = errorCode;
                }
            }
            finally
            {
                if (getNext && mediaEventType != MediaEventType.MESessionClosed)
                {
                    _basePlayer.mf_MediaSession.BeginGetEvent(this, null);
                }
                if (mediaEvent != null)
                {
                    Marshal.ReleaseComObject(mediaEvent);
                }

                if (_basePlayer.mf_AwaitCallback)
                {
                    _basePlayer.mf_AwaitCallback = false;
                    _basePlayer.WaitForEvent.Set();
                }
                _basePlayer.mf_AwaitDoEvents = false;
            }
            return(0);
        }
예제 #7
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);
        }
예제 #8
0
        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);
            }
        }
예제 #9
0
        /// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
        /// <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);
        }