Exemple #1
0
        ////////////////////////////////////////////////////////////////////////////////////////
        //  Name: CPlayer::HandleNotifyPresentationTime (Private)
        //  Description:
        //      Handles the media session's MESessionNotifyPresentationTime event
        //  Parameter:
        //      pEvent: [in] MESessionNotifyPresentationTime event
        ///////////////////////////////////////////////////////////////////////////////////////////

        private void HandleNotifyPresentationTime(IMFMediaEvent pEvent)
        {
            if (pEvent == null)
            {
                throw new COMException("null pointer", (int)HResult.E_POINTER);
            }

            if (m_phnsTimePairStart == null)
            {
                m_phnsTimePairStart = new TimePair();
                m_phnsTimePairStart.pNextTimePair = null;
                m_phnsTimePairEnd = m_phnsTimePairStart;
            }
            else
            {
                m_phnsTimePairEnd.pNextTimePair = new TimePair();
                m_phnsTimePairEnd = m_phnsTimePairEnd.pNextTimePair;
                m_phnsTimePairEnd.pNextTimePair = null;
            }

            HResult hr = pEvent.GetUINT64(
                MFAttributesClsid.MF_EVENT_START_PRESENTATION_TIME,
                out m_phnsTimePairEnd.hnsStartPresentationTime);

            MFError.ThrowExceptionForHR(hr);

            hr = pEvent.GetUINT64(
                MFAttributesClsid.MF_EVENT_PRESENTATION_TIME_OFFSET,
                out m_phnsTimePairEnd.hnsPresentationTimeOffset);
            MFError.ThrowExceptionForHR(hr);
        }
Exemple #2
0
        protected void OnSessionPaused(IMFMediaEvent pEvent)
        {
            TRACE("CPlayer::OnSessionPaused");

            m_state = PlayerState.Paused;
            NotifyState();
        }
Exemple #3
0
 public static extern void MFCreateMediaEvent(
     MediaEventType met,
     [In, MarshalAs(UnmanagedType.LPStruct)] Guid guidExtendedType,
     int hrStatus,
     [In, MarshalAs(UnmanagedType.LPStruct)] ConstPropVariant pvValue,
     out IMFMediaEvent ppEvent
     );
        /// <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);
        }
Exemple #5
0
        public HResult ProcessEvent(
            int dwInputStreamID,
            IMFMediaEvent pEvent
            )
        {
            HResult hr = HResult.S_OK;

            try
            {
                Trace("ProcessEvent");

                lock (m_TransformLockObject)
                {
                    // Events not supported.
                    hr = HResult.E_NOTIMPL;
                }
            }
            catch (Exception e)
            {
                hr = (HResult)Marshal.GetHRForException(e);
            }
            //finally
            {
                // While we *should* do this, we can't.  If the caller is c#, we
                // would destroy their copy.  Instead we have to leave this for
                // the GC.
                // SafeRelease(pEvent);
            }

            return(CheckReturn(hr));
        }
Exemple #6
0
        public HResult GetEvent(MFEventFlag dwFlags, out IMFMediaEvent ppEvent)
        {
            Debug.WriteLine("StreamSink:GetEvent");

            HResult hr;
            IMFMediaEventQueueAlt queue = null;

            ppEvent = null;

            lock (this)
            {
                hr = CheckShutdown();
                LogIfFailed(hr);

                if (Succeeded(hr))
                {
                    GenEventQueue();
                    queue = EventQueue;
                    Marshal.AddRef(Marshal.GetIUnknownForObject(queue));
                }
            }
            if (Succeeded(hr))
            {
                hr = queue.GetEvent(dwFlags, out ppEvent);
                LogIfFailed(hr);
            }

            SafeRelease(queue);

            return(hr);
        }
            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);
            }
Exemple #8
0
        /// <summary>
        /// Handle SessionPaused Event
        /// </summary>
        /// <param name="pEvent"></param>
        protected void OnSessionPaused(IMFMediaEvent pEvent)
        {
            TRACE("Media::OnSessionPaused");

            m_state = MediaState.Paused;
            NotifyState();
        }
Exemple #9
0
        public HResult GetEvent(MFEventFlag dwFlags, out IMFMediaEvent ppEvent)
        {
            // NOTE:
            // GetEvent can block indefinitely, so we don't hold the lock.
            // This requires some juggling with the event queue pointer.
            HResult            hr      = HResult.S_OK;
            IMFMediaEventQueue spQueue = null;

            ppEvent = null;

            lock (_spSource)
            {
                // Check shutdown
                hr = CheckShutdown();

                // Get the pointer to the event queue.
                if (MFError.Succeeded(hr))
                {
                    spQueue = _spEventQueue;
                }
            }

            // Now get the event.
            if (MFError.Succeeded(hr))
            {
                hr = spQueue.GetEvent(dwFlags, out ppEvent);
            }

            return(hr);
        }
Exemple #10
0
    public HResult GetEvent(MFEventFlag dwFlags, out IMFMediaEvent ppEvent)
    {
        HResult hr;

        hr = m_events.GetEvent(dwFlags, out ppEvent);

        return(hr);
    }
Exemple #11
0
        protected void OnSessionClosed(IMFMediaEvent pEvent)
        {
            TRACE("CPlayer::OnSessionClosed");

            // The application thread is waiting on this event, inside the
            // CPlayer::CloseSession method.
            m_hCloseEvent.Set();
        }
Exemple #12
0
    public HResult EndGetEvent(IMFAsyncResult pResult,
                               out IMFMediaEvent ppEvent)
    {
        HResult hr;

        hr = m_events.EndGetEvent(pResult, out ppEvent);

        return(hr);
    }
Exemple #13
0
        protected void OnPresentationEnded(IMFMediaEvent pEvent)
        {
            TRACE("CPlayer::OnPresentationEnded");

            // The session puts itself into the stopped state autmoatically.

            m_state = PlayerState.Ready;
            NotifyState();
        }
        public int OnSourceEvent(IMFMediaEvent pEvent)
        {
            m_iCount++;

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

            return 0;
        }
Exemple #15
0
        public HResult EndGetEvent(IMFAsyncResult pResult, out IMFMediaEvent ppEvent)
        {
            HResult hr = HResult.S_OK;

            ppEvent = null;
            hr      = CheckShutdown();
            if (MFError.Succeeded(hr))
            {
                hr = _spEventQueue.EndGetEvent(pResult, out ppEvent);
            }
            return(hr);
        }
Exemple #16
0
        /// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
        /// <summary>
        /// Called when the topology status changes to ready. This status change
        /// is generally signaled by the media session when it is fully configured.
        /// </summary>
        /// <param name="sender">the object sending the event</param>
        /// <param name="mediaEvent">the event generated by the media session. Do NOT release this here.</param>
        /// <param name="mediaEventType">the eventType</param>
        /// <param name="topoStatus">the topology status flag</param>
        /// <history>
        ///    01 Nov 18  Cynic - Originally Written
        /// </history>
        private void MediaSessionTopologyNowReady(IMFMediaEvent mediaEvent)
        {
            LogMessage("MediaSessionTopologyNowReady");

            try
            {
                StartFileCopy();
            }
            catch (Exception ex)
            {
                LogMessage("MediaSessionTopologyNowReady errored ex=" + ex.Message);
                OISMessageBox(ex.Message);
            }
        }
Exemple #17
0
        /// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
        /// <summary>
        /// Handles topology status changes reported by the media session TantaAsyncCallbackHandler
        /// </summary>
        /// <param name="sender">the object sending the event</param>
        /// <param name="mediaEvent">the event generated by the media session. Do NOT release this here.</param>
        /// <param name="mediaEventType">the eventType</param>
        /// <param name="topoStatus">the topology status flag</param>
        /// <history>
        ///    01 Nov 18  Cynic - Originally Written
        /// </history>
        private void HandleTopologyStatusChanged(IMFMediaEvent mediaEvent, MediaEventType mediaEventType, MFTopoStatus topoStatus)
        {
            LogMessage("HandleTopologyStatusChanged event type: " + mediaEventType.ToString() + ", topoStatus=" + topoStatus.ToString());

            if (topoStatus == MFTopoStatus.Ready)
            {
                MediaSessionTopologyNowReady(mediaEvent);
            }
            else
            {
                // we are not interested in any other status changes
                return;
            }
        }
Exemple #18
0
    // Media event handlers
    protected void OnTopologyReady(IMFMediaEvent pEvent)
    {
        HResult hr;
        object  o;

        TRACE("CPlayer::OnTopologyReady");

        // Ask for the IMFVideoDisplayControl interface.
        // This interface is implemented by the EVR and is
        // exposed by the media session as a service.

        // Note: This call is expected to fail if the source
        // does not have video.

        try
        {
            hr = MFExtern.MFGetService(
                m_pSession,
                MFServices.MR_VIDEO_RENDER_SERVICE,
                typeof(IMFVideoDisplayControl).GUID,
                out o
                );

            if (Succeeded(hr))
            {
                m_pVideoDisplay = o as IMFVideoDisplayControl;
            }
        }
        catch (InvalidCastException)
        {
            m_pVideoDisplay = null;
        }

        try
        {
            StartPlayback();
        }
        catch (Exception ce)
        {
            hr = (HResult)Marshal.GetHRForException(ce);
            NotifyError(hr);
        }

        // If we succeeded, the Start call is pending. Don't notify the app yet.
    }
Exemple #19
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);
        }
Exemple #21
0
        public HResult EndGetEvent(IntPtr pResult, out IMFMediaEvent ppEvent)
        {
            Debug.WriteLine("StreamSink:EndGetEvent");

            HResult hr;

            ppEvent = null;

            lock (this)
            {
                hr = CheckShutdown();
                if (Failed(hr))
                {
                    return(hr);
                }

                GenEventQueue();
                hr = EventQueue.EndGetEvent(pResult, out ppEvent);
                LogIfFailed(hr);
            }

            return(hr);
        }
Exemple #22
0
 public int OnEvent(int a, IMFMediaEvent b)
 {
     return S_Ok;
 }
Exemple #23
0
 /// <summary>
 /// Called when the source reader receives certain events from the media source.
 /// </summary>
 public void OnEvent(uint dwStreamIndex, IMFMediaEvent pEvent)
 {
     pEvent.GetType(out MediaEventType type);
 }
Exemple #24
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);
            }
        }
Exemple #25
0
        public int EndGetEvent(
            //IMFAsyncResult pResult,
            IntPtr pResult,
            out IMFMediaEvent ppEvent)
        {
            int hr;
            TRACE("CWavStream::EndGetEvent");

            lock (this)
            {
                CheckShutdown();

                hr = m_pEventQueue.EndGetEvent(pResult, out ppEvent);
                MFError.ThrowExceptionForHR(hr);
            }
            return S_Ok;
        }
        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);
        }
Exemple #27
0
 public HResult OnEvent(int dwStreamIndex, IMFMediaEvent pEvent)
 {
     return(HResult.S_OK);
 }
Exemple #28
0
        /// <summary>
        /// Handle SessionStarted Event
        /// </summary>
        /// <param name="pEvent"></param>
        protected void OnSessionStarted(IMFMediaEvent pEvent)
        {
            TRACE("Media::OnSessionStarted");

            m_state = MediaState.Started;
            NotifyState();
        }
Exemple #29
0
 public int OnEvent(int dwStreamIndex, IMFMediaEvent pEvent)
 {
     return(S_Ok);
 }
Exemple #30
0
 protected void OnMetadataChanged(IMFMediaEvent pEvent)
 {
     PostMessage(m_hwndEvent, WM_APP_META, IntPtr.Zero, IntPtr.Zero);
 }
Exemple #31
0
        protected void OnPresentationEnded(IMFMediaEvent pEvent)
        {
            TRACE("CPlayer::OnPresentationEnded");

            // The session puts itself into the stopped state autmoatically.

            m_state = PlayerState.Ready;
            NotifyState();
        }
Exemple #32
0
        public int EndGetEvent(IMFAsyncResult pResult, out IMFMediaEvent ppEvent)
        {
            // Make sure we *never* leave this entry point with an exception
            try
            {
                int hr;
                m_Log.WriteLine("-EndGetEvent");
                ppEvent = null;

                lock (this)
                {
                    CheckShutdown();
                    hr = m_pEventQueue.EndGetEvent(pResult, out ppEvent);
                }
                return S_Ok;
            }
            catch (Exception e)
            {
                ppEvent = null;
                return Marshal.GetHRForException(e);
            }
        }
Exemple #33
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);
        }
        /// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
        /// <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);
        }
Exemple #35
0
        public int GetEvent(MFEventFlag dwFlags, out IMFMediaEvent ppEvent)
        {
            // Make sure we *never* leave this entry point with an exception
            try
            {
                // NOTE: GetEvent can block indefinitely, so we don't hold the
                //       WavSource lock. This requires some juggling with the
                //       event queue pointer.

                int hr;
                m_Log.WriteLine("-GetEvent");
                ppEvent = null;

                IMFMediaEventQueue pQueue = null;

                lock (this)
                {
                    // Check shutdown
                    CheckShutdown();
                    pQueue = m_pEventQueue;
                }

                hr = pQueue.GetEvent(dwFlags, out ppEvent);
                MFError.ThrowExceptionForHR(hr);

                // not needed SAFE_RELEASE(pQueue);
                return S_Ok;
            }
            catch (Exception e)
            {
                ppEvent = null;
                return Marshal.GetHRForException(e);
            }
        }
Exemple #36
0
        ////////////////////////////////////////////////////////////////////////////////////////
        //  Name: CPlayer::HandleNotifyPresentationTime (Private)
        //  Description:
        //      Handles the media session's MESessionNotifyPresentationTime event
        //  Parameter:
        //      pEvent: [in] MESessionNotifyPresentationTime event
        ///////////////////////////////////////////////////////////////////////////////////////////
        private void HandleNotifyPresentationTime(IMFMediaEvent pEvent)
        {
            if (pEvent == null)
            {
                throw new COMException("null pointer", E_Pointer);
            }

            if (m_phnsTimePairStart == null)
            {
                m_phnsTimePairStart = new TimePair();
                m_phnsTimePairStart.pNextTimePair = null;
                m_phnsTimePairEnd = m_phnsTimePairStart;
            }
            else
            {
                m_phnsTimePairEnd.pNextTimePair = new TimePair();
                m_phnsTimePairEnd = m_phnsTimePairEnd.pNextTimePair;
                m_phnsTimePairEnd.pNextTimePair = null;
            }

            int hr = pEvent.GetUINT64(
                        MFAttributesClsid.MF_EVENT_START_PRESENTATION_TIME,
                        out m_phnsTimePairEnd.hnsStartPresentationTime);
            MFError.ThrowExceptionForHR(hr);

            hr = pEvent.GetUINT64(
                        MFAttributesClsid.MF_EVENT_PRESENTATION_TIME_OFFSET,
                        out m_phnsTimePairEnd.hnsPresentationTimeOffset);
            MFError.ThrowExceptionForHR(hr);
        }
 public HResult OnEvent(int a, IMFMediaEvent b)
 {
     return(HResult.S_OK);
 }
Exemple #38
0
        /// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
        /// <summary>
        /// Handles events reported by the media session TantaAsyncCallbackHandler
        /// </summary>
        /// <param name="sender">the object sending the event</param>
        /// <param name="mediaEvent">the event generated by the media session. Do NOT release this here.</param>
        /// <param name="mediaEventType">the eventType, this is just an enum</param>
        /// <history>
        ///    01 Nov 18  Cynic - Originally Written
        /// </history>
        private void HandleMediaSessionAsyncCallBackEvent(object sender, IMFMediaEvent pEvent, MediaEventType mediaEventType)
        {
            LogMessage("Media Event Type " + mediaEventType.ToString());

            switch (mediaEventType)
            {
            case MediaEventType.MESessionTopologyStatus:
                // Raised by the Media Session when the status of a topology changes.
                // Get the topology changed status code. This is an enum in the event
                int     i;
                HResult hr = pEvent.GetUINT32(MFAttributesClsid.MF_EVENT_TOPOLOGY_STATUS, out i);
                if (hr != HResult.S_OK)
                {
                    throw new Exception("HandleMediaSessionAsyncCallBackEvent call to pEvent to get the status code failed. Err=" + hr.ToString());
                }
                // the one we are most interested in is i == MFTopoStatus.Ready
                // which we get then the Topology is built and ready to run
                HandleTopologyStatusChanged(pEvent, mediaEventType, (MFTopoStatus)i);
                break;

            case MediaEventType.MESessionStarted:
                // Raised when the IMFMediaSession::Start method completes asynchronously.
                //       PlayerState = TantaEVRPlayerStateEnum.Started;
                break;

            case MediaEventType.MESessionPaused:
                // Raised when the IMFMediaSession::Pause method completes asynchronously.
                //        PlayerState = TantaEVRPlayerStateEnum.Paused;
                break;

            case MediaEventType.MESessionStopped:
                // Raised when the IMFMediaSession::Stop method completes asynchronously.
                break;

            case MediaEventType.MESessionClosed:
                // Raised when the IMFMediaSession::Close method completes asynchronously.
                break;

            case MediaEventType.MESessionCapabilitiesChanged:
                // Raised by the Media Session when the session capabilities change.
                // You can use IMFMediaEvent::GetValue to figure out what they are
                break;

            case MediaEventType.MESessionTopologySet:
                // Raised after the IMFMediaSession::SetTopology method completes asynchronously.
                // The Media Session raises this event after it resolves the topology into a full topology and queues the topology for playback.
                break;

            case MediaEventType.MESessionNotifyPresentationTime:
                // Raised by the Media Session when a new presentation starts.
                // This event indicates when the presentation will start and the offset between the presentation time and the source time.
                break;

            case MediaEventType.MEEndOfPresentation:
                // Raised by a media source when a presentation ends. This event signals that all streams
                // in the presentation are complete. The Media Session forwards this event to the application.

                // we cannot sucessfully .Finalize_ on the SinkWriter
                // if we call CloseAllMediaDevices directly from this thread
                // so we use an asynchronous method
                Task taskA = Task.Run(() => CloseAllMediaDevices());
                // we have to be on the form thread to update the screen
                ThreadSafeScreenUpdate(this, false, "Done");
                break;

            case MediaEventType.MESessionRateChanged:
                // Raised by the Media Session when the playback rate changes. This event is sent after the
                // IMFRateControl::SetRate method completes asynchronously.
                break;

            default:
                LogMessage("Unhandled Media Event Type " + mediaEventType.ToString());
                break;
            }
        }
Exemple #39
0
        protected void OnSessionStarted(IMFMediaEvent pEvent)
        {
            TRACE("CPlayer::OnSessionStarted");

            m_state = PlayerState.Started;
            NotifyState();
        }
Exemple #40
0
        protected void OnSessionClosed(IMFMediaEvent pEvent)
        {
            TRACE("CPlayer::OnSessionClosed");

            // The application thread is waiting on this event, inside the
            // CPlayer::CloseSession method.
            m_hCloseEvent.Set();
        }
Exemple #41
0
        public int GetEvent(MFEventFlag dwFlags, out IMFMediaEvent ppEvent)
        {
            int hr;
            TRACE("CWavStream::GetEvent");

            // NOTE:
            // GetEvent can block indefinitely, so we don't hold the lock.
            // This requires some juggling with the event queue pointer.

            IMFMediaEventQueueAlt pQueue;

            lock (this)
            {
                // Check shutdown
                CheckShutdown();

                // Get the pointer to the event queue.
                pQueue = m_pEventQueue;

            }   // release lock

            // Now get the event.
            hr = pQueue.GetEvent(dwFlags, out ppEvent);
            MFError.ThrowExceptionForHR(hr);

            //SAFE_RELEASE(pQueue);
            return S_Ok;
        }
Exemple #42
0
        // Media event handlers
        protected void OnTopologyReady(IMFMediaEvent pEvent)
        {
            object o;
            TRACE("CPlayer::OnTopologyReady");

            // Ask for the IMFVideoDisplayControl interface.
            // This interface is implemented by the EVR and is
            // exposed by the media session as a service.

            // Note: This call is expected to fail if the source
            // does not have video.

            try
            {
                MFExtern.MFGetService(
                    m_pSession,
                    MFServices.MR_VIDEO_RENDER_SERVICE,
                    typeof(IMFVideoDisplayControl).GUID,
                    out o
                    );

                m_pVideoDisplay = o as IMFVideoDisplayControl;
            }
            catch (InvalidCastException)
            {
                m_pVideoDisplay = null;
            }

            try
            {
                StartPlayback();
            }
            catch (Exception ce)
            {
                int hr = Marshal.GetHRForException(ce);
                NotifyError(hr);
            }

            // If we succeeded, the Start call is pending. Don't notify the app yet.
        }
Exemple #43
0
 public int OnEvent(int dwStreamIndex, IMFMediaEvent pEvent)
 {
     return S_Ok;
 }
Exemple #44
0
        public int GetEvent(MFEventFlag dwFlags, out IMFMediaEvent ppEvent)
        {
            // Make sure we *never* leave this entry point with an exception
            try
            {
                int hr;
                m_Log.WriteLine("-GetEvent");
                ppEvent = null;

                IMFMediaEventQueue pQueue = null;

                lock (this)
                {
                    CheckShutdown();
                    pQueue = (IMFMediaEventQueue)m_pEventQueue;
                }

                hr = pQueue.GetEvent(dwFlags, out ppEvent);
                MFError.ThrowExceptionForHR(hr);

                //not needed SAFE_RELEASE(pQueue);
                return S_Ok;
            }
            catch (Exception e)
            {
                ppEvent = null;
                return Marshal.GetHRForException(e);
            }
        }