Example #1
0
        private void SelectStreams(IMFPresentationDescriptor pPD)
        {
            for (int nStream = 0; nStream < _streams.Count; ++nStream)
            {
                IMFStreamDescriptor spSD;
                MediaStream         spStream;
                int  nStreamId;
                bool fSelected;

                // Get next stream descriptor
                ThrowIfError(pPD.GetStreamDescriptorByIndex(nStream, out fSelected, out spSD));

                // Get stream id
                ThrowIfError(spSD.GetStreamIdentifier(out nStreamId));

                // Get simple net media stream
                ThrowIfError(GetStreamById(nStreamId, out spStream));

                // Remember if stream was selected
                bool fWasSelected = spStream.IsActive;
                ThrowIfError(spStream.SetActive(fSelected));

                if (fSelected)
                {
                    // Choose event type to send
                    MediaEventType met = fWasSelected ? MediaEventType.MEUpdatedStream : MediaEventType.MENewStream;
                    ThrowIfError(_spEventQueue.QueueEventParamUnk(met, Guid.Empty, HResult.S_OK, spStream));

                    // Start the stream. The stream will send the appropriate event.
                    ThrowIfError(spStream.Start());
                }
            }
        }
Example #2
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
     );
Example #3
0
    private void SendEvent(MediaEventType met)
    {
        IMFMediaEvent pEvent;

        MFError throwonhr = MFExtern.MFCreateMediaEvent(
            met,
            Guid.Empty, HResult.S_OK, null, out pEvent);

        throwonhr = m_events.QueueEvent(pEvent);
    }
Example #4
0
        public HResult QueueEvent(MediaEventType met, Guid guidExtendedType, HResult hrStatus, ConstPropVariant pvValue)
        {
            HResult hr = HResult.S_OK;

            hr = CheckShutdown();
            if (MFError.Succeeded(hr))
            {
                hr = _spEventQueue.QueueEventParamVar(met, guidExtendedType, hrStatus, pvValue);
            }
            return(hr);
        }
Example #5
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;
            }
        }
Example #6
0
    public HResult QueueEvent(MediaEventType met,
                              Guid guidExtendedType, HResult hrStatus, ConstPropVariant pvValue)
    {
        IMFMediaEvent pEvent;
        HResult       hr;

        hr = MFExtern.MFCreateMediaEvent(met,
                                         Guid.Empty, HResult.S_OK, null, out pEvent);

        if (MFError.Succeeded(hr))
        {
            hr = m_events.QueueEvent(pEvent);
        }

        return(hr);
    }
Example #7
0
        public HResult QueueEvent(MediaEventType met, Guid guidExtendedType, HResult hrStatus, ConstPropVariant pvValue)
        {
            Debug.WriteLine("StreamSink:QueueEvent");

            HResult hr;

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

                GenEventQueue();
                hr = EventQueue.QueueEventParamVar(met, guidExtendedType, hrStatus, pvValue);
                LogIfFailed(hr);
            }
            return(hr);
        }
Example #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);
            }
        }
Example #9
0
        ///////////////////////////////////////////////////////////////////////
        //  Name: RunMediaSession
        //  Description:
        //  Queues the specified topology on the media session and runs the
        //  media session until the MESessionEnded event is received.
        ///////////////////////////////////////////////////////////////////////

        static void RunMediaSession(IMFTopology pTopology)
        {
            HResult         hr;
            IMFMediaSession pSession;

            bool        bGetAnotherEvent = true;
            PropVariant varStartPosition = new PropVariant();

            hr = MFExtern.MFCreateMediaSession(null, out pSession);
            MFError.ThrowExceptionForHR(hr);

            try
            {
                hr = pSession.SetTopology(0, pTopology);
                MFError.ThrowExceptionForHR(hr);

                while (bGetAnotherEvent)
                {
                    HResult        hrStatus = 0;
                    IMFMediaEvent  pEvent;
                    MediaEventType meType = MediaEventType.MEUnknown;

                    int TopoStatus = (int)MFTopoStatus.Invalid; // Used with MESessionTopologyStatus event.

                    hr = pSession.GetEvent(MFEventFlag.None, out pEvent);
                    MFError.ThrowExceptionForHR(hr);

                    try
                    {
                        hr = pEvent.GetStatus(out hrStatus);
                        MFError.ThrowExceptionForHR(hr);

                        hr = pEvent.GetType(out meType);
                        MFError.ThrowExceptionForHR(hr);

                        if (hrStatus >= 0)
                        {
                            switch (meType)
                            {
                            case MediaEventType.MESessionTopologySet:
                                Debug.WriteLine("MESessionTopologySet");
                                break;

                            case MediaEventType.MESessionTopologyStatus:
                                // Get the status code.
                                hr = pEvent.GetUINT32(MFAttributesClsid.MF_EVENT_TOPOLOGY_STATUS, out TopoStatus);
                                MFError.ThrowExceptionForHR(hr);
                                switch ((MFTopoStatus)TopoStatus)
                                {
                                case MFTopoStatus.Ready:
                                    Debug.WriteLine("MESessionTopologyStatus: MF_TOPOSTATUS_READY");
                                    pSession.Start(Guid.Empty, varStartPosition);
                                    break;

                                case MFTopoStatus.Ended:
                                    Debug.WriteLine("MESessionTopologyStatus: MF_TOPOSTATUS_ENDED");
                                    break;
                                }
                                break;

                            case MediaEventType.MESessionStarted:
                                Debug.WriteLine("MESessionStarted");
                                break;

                            case MediaEventType.MESessionEnded:
                                Debug.WriteLine("MESessionEnded");
                                hr = pSession.Stop();
                                break;

                            case MediaEventType.MESessionStopped:
                                Debug.WriteLine("MESessionStopped");
                                Console.WriteLine("Attempting to close the media session.");
                                hr = pSession.Close();
                                MFError.ThrowExceptionForHR(hr);
                                break;

                            case MediaEventType.MESessionClosed:
                                Debug.WriteLine("MESessionClosed");
                                bGetAnotherEvent = false;
                                break;

                            default:
                                Debug.WriteLine(string.Format("Media session event: {0}", meType));
                                break;
                            }
                        }
                        else
                        {
                            Debug.WriteLine(string.Format("HRStatus: {0}", hrStatus));
                            bGetAnotherEvent = false;
                        }
                    }
                    finally
                    {
                        if (pEvent != null)
                        {
                            Marshal.ReleaseComObject(pEvent);
                        }
                    }
                }

                Debug.WriteLine("Shutting down the media session.");
                hr = pSession.Shutdown();
                MFError.ThrowExceptionForHR(hr);
            }
            finally
            {
                if (pSession != null)
                {
                    Marshal.ReleaseComObject(pSession);
                }
            }
        }
Example #10
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);
        }
        public void QueueNews(string newsConcentrate, string outcome = null, bool highlight = false, MediaEventType eventType = MediaEventType.Mundane)
        {
            string formattedReason = string.Format(newsConcentrate, this.Name);

            string[] parts = formattedReason.Split('\n');
            if (parts.Length < 2)
            {
                parts = new string[] { formattedReason, "Ha, the editor is on vacation and I can say whatever I want" }
            }
            ;
            QueueNews(parts[0], parts[1], outcome, highlight, eventType);
        }
Example #12
0
    ///////////////////////////////////////////////////////////////////////
    //  Name: Invoke
    //  Description:  Callback for asynchronous BeginGetEvent method.
    //
    //  pAsyncResult: Pointer to the result.
    /////////////////////////////////////////////////////////////////////////

    HResult IMFAsyncCallback.Invoke(IMFAsyncResult pAsyncResult)
    {
        // Make sure we *never* leave this entry point with an exception
        try
        {
            HResult        hr;
            IMFMediaEvent  pEvent;
            MediaEventType meType       = MediaEventType.MEUnknown; // Event type
            PropVariant    varEventData = new PropVariant();        // Event data

            // Get the event from the event queue.
            hr = m_pMEG.EndGetEvent(pAsyncResult, out pEvent);
            MFError.ThrowExceptionForHR(hr);

            // Get the event type.
            hr = pEvent.GetType(out meType);
            MFError.ThrowExceptionForHR(hr);

            // Get the event status. If the operation that triggered the event did
            // not succeed, the status is a failure code.
            hr = pEvent.GetStatus(out m_hrStatus);
            MFError.ThrowExceptionForHR(hr);

            if ((int)m_hrStatus == 862022) // NS_S_DRM_MONITOR_CANCELLED
            {
                m_hrStatus = HResult.MF_E_OPERATION_CANCELLED;
                m_state    = Enabler.Complete;
            }

            // Get the event data.
            hr = pEvent.GetValue(varEventData);
            MFError.ThrowExceptionForHR(hr);

            // For the MEEnablerCompleted action, notify the application.
            // Otherwise, request another event.
            Debug.WriteLine(string.Format("Content enabler event: {0}", meType.ToString()));

            if (meType == MediaEventType.MEEnablerCompleted)
            {
                PostMessage(m_hwnd, WM_APP_CONTENT_ENABLER, IntPtr.Zero, IntPtr.Zero);
            }
            else
            {
                if (meType == MediaEventType.MEEnablerProgress)
                {
                    if (varEventData.GetVariantType() == PropVariant.VariantType.String)
                    {
                        Debug.WriteLine(string.Format("Progress: {0}", varEventData.GetString()));
                    }
                }
                hr = m_pMEG.BeginGetEvent(this, null);
                MFError.ThrowExceptionForHR(hr);
            }

            // Clean up.
            varEventData.Clear();
            SafeRelease(pEvent);

            return(HResult.S_OK);
        }
        catch (Exception e)
        {
            return((HResult)Marshal.GetHRForException(e));
        }
    }
Example #13
0
        public int QueueEvent(MediaEventType met, Guid guidExtendedType, int hrStatus, ConstPropVariant pvValue)
        {
            int hr;
            TRACE(string.Format("CWavStream::QueueEvent ({0})", met.ToString()));

            lock (this)
            {
                CheckShutdown();

                hr = m_pEventQueue.QueueEventParamVar(met, guidExtendedType, hrStatus, pvValue);
                MFError.ThrowExceptionForHR(hr);
            }
            return S_Ok;
        }
Example #14
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;
            }
        }
Example #15
0
 private void HandleTopologyStatusChanged(MediaEventType mediaEventType, MFTopoStatus topoStatus)
 {
     Console.WriteLine("HandleTopologyStatusChanged event type: " + mediaEventType.ToString() + ", topoStatus=" + topoStatus.ToString());
     // we currently arent paying attention to these
     return;
 }
Example #16
0
        //////////////////////////////////////////////////////////////////////////
        //  Name: CPlayer::Invoke
        //  Description:
        //      Implementation of CAsyncCallback::Invoke.
        //      Callback for asynchronous BeginGetEvent method.
        //  Parameter:
        //      pAsyncResult: Pointer to the result.
        //
        /////////////////////////////////////////////////////////////////////////

        HResult IMFAsyncCallback.Invoke(IMFAsyncResult pAsyncResult)
        {
            MediaEventType eventType = MediaEventType.MEUnknown;
            IMFMediaEvent  pEvent;
            PropVariant    eventData = null;
            Exception      excpt     = null;
            HResult        hr        = HResult.S_OK;

            try
            {
                HResult eventStatus = 0;             // Event status
                eventData = new PropVariant();       // Event data

                // Get the event from the event queue.
                hr = m_pMediaSession.EndGetEvent(pAsyncResult, out pEvent);
                MFError.ThrowExceptionForHR(hr);

                // Get the event type.
                hr = pEvent.GetType(out eventType);
                MFError.ThrowExceptionForHR(hr);

                // Get the event data
                hr = pEvent.GetValue(eventData);
                MFError.ThrowExceptionForHR(hr);

                // Get the event status. If the operation that triggered the event
                // did not succeed, the status is a failure code.
                hr = pEvent.GetStatus(out eventStatus);
                MFError.ThrowExceptionForHR(hr);

                // Switch on the event type. Update the internal state of the CPlayer
                // as needed.

                switch (eventType)
                {
                // Session events
                case MediaEventType.MESessionStarted:
                {
                    Debug.WriteLine(string.Format("{0}: MESessionStarted, Status: 0x{1:x}", eventType.ToString(), eventStatus));

                    m_State = PlayerState.Playing;
                    PostMessage(m_hWnd, Form1.WM_NOTIFY_APP, new IntPtr((int)eventType), new IntPtr((int)m_State));

                    break;
                }

                case MediaEventType.MESessionPaused:
                {
                    Debug.WriteLine(string.Format("{0}: MESessionPaused, Status: 0x{1:x}", eventType.ToString(), eventStatus));

                    m_State = PlayerState.Paused;
                    PostMessage(m_hWnd, Form1.WM_NOTIFY_APP, new IntPtr((int)eventType), new IntPtr((int)m_State));

                    break;
                }

                case MediaEventType.MESessionStopped:
                {
                    Debug.WriteLine(string.Format("{0}: MESessionStopped, Status: 0x{1:x}", eventType.ToString(), eventStatus));

                    m_State = PlayerState.Stopped;
                    PostMessage(m_hWnd, Form1.WM_NOTIFY_APP, new IntPtr((int)eventType), new IntPtr((int)m_State));

                    break;
                }

                case MediaEventType.MESessionTopologyStatus:
                {
                    Debug.WriteLine(string.Format("{0}: MESessionTopologyStatus, Status: 0x{1:x}", eventType.ToString(), eventStatus));

                    int value = 0;

                    hr = pEvent.GetUINT32(MFAttributesClsid.MF_EVENT_TOPOLOGY_STATUS, out value);
                    MFError.ThrowExceptionForHR(hr);
                    int  SegmentID = 0;
                    long ID;

                    //Get information about the new segment
                    IMFTopology pTopology;

                    pTopology = (IMFTopology)eventData.GetIUnknown();

                    try
                    {
                        hr = pTopology.GetTopologyID(out ID);
                        MFError.ThrowExceptionForHR(hr);
                        m_Segments.GetSegmentIDByTopoID(ID, out SegmentID);

                        topostat.iTopologyStatusType = (MFTopoStatus)value;
                        topostat.iSegmentId          = SegmentID;

                        switch (topostat.iTopologyStatusType)
                        {
                        case MFTopoStatus.StartedSource:
                            m_ActiveSegment = SegmentID;
                            break;

                        case MFTopoStatus.Ended:
                            m_ActiveSegment = -1;
                            break;
                        }

                        GCHandle gc = GCHandle.Alloc(topostat);

                        PostMessage(m_hWnd, Form1.WM_NOTIFY_APP, new IntPtr((int)MediaEventType.MESessionTopologyStatus), GCHandle.ToIntPtr(gc));
                    }
                    finally
                    {
                        SafeRelease(pTopology);
                    }

                    break;
                }

                case MediaEventType.MENewPresentation:
                {
                    Debug.WriteLine(string.Format("{0}: MENewPresentation, Status: 0x{1:x}", eventType.ToString(), eventStatus));

                    IMFPresentationDescriptor pPresentationDescriptor;

                    int SegmentId = 0;

                    pPresentationDescriptor = (IMFPresentationDescriptor)eventData.GetIUnknown();

                    try
                    {
                        //Queue the next segment on the media session
                        QueueNextSegment(pPresentationDescriptor, out SegmentId);
                    }
                    finally
                    {
                        SafeRelease(pPresentationDescriptor);
                    }

                    PostMessage(m_hWnd, Form1.WM_NOTIFY_APP, new IntPtr((int)eventType), new IntPtr(SegmentId));

                    break;
                }

                case MediaEventType.MEEndOfPresentation:
                {
                    Debug.WriteLine(string.Format("{0}: MEEndOfPresentation, Status: 0x{1:x}", eventType.ToString(), eventStatus));

                    int value = 0;

                    try
                    {
                        hr = pEvent.GetUINT32(MFAttributesClsid.MF_EVENT_SOURCE_TOPOLOGY_CANCELED, out value);
                        MFError.ThrowExceptionForHR(hr);
                    }
                    catch { }

                    PostMessage(m_hWnd, Form1.WM_NOTIFY_APP, new IntPtr((int)eventType), new IntPtr(value));

                    break;
                }

                case MediaEventType.MEEndOfPresentationSegment:
                {
                    Debug.WriteLine(string.Format("{0}: MEEndOfPresentationSegment, Status: 0x{1:x}", eventType.ToString(), eventStatus));

                    int value = 0;

                    try
                    {
                        hr = pEvent.GetUINT32(MFAttributesClsid.MF_EVENT_SOURCE_TOPOLOGY_CANCELED, out value);
                        MFError.ThrowExceptionForHR(hr);
                    }
                    catch { }

                    PostMessage(m_hWnd, Form1.WM_NOTIFY_APP, new IntPtr((int)eventType), new IntPtr(value));

                    break;
                }

                case MediaEventType.MESessionNotifyPresentationTime:
                {
                    Debug.WriteLine(string.Format("{0}: MESessionNotifyPresentationTime, Status: 0x{1:x}", eventType.ToString(), eventStatus));

                    HandleNotifyPresentationTime(pEvent);
                    break;
                }

                case MediaEventType.MESessionClosed:
                {
                    Debug.WriteLine(string.Format("{0}: MESessionClosed, Status: 0x{1:x}", eventType.ToString(), eventStatus));

                    m_hCloseEvent.Set();

                    break;
                }

                default:
                    Debug.WriteLine(string.Format("{0}: Event", eventType.ToString()));
                    break;
                }
            }
            catch (Exception e)
            {
                excpt = e;
            }
            finally
            {
                if (eventData != null)
                {
                    eventData.Clear();
                }
            }

            // Request another event.
            if (eventType != MediaEventType.MESessionClosed)
            {
                hr = m_pMediaSession.BeginGetEvent(this, null);
                MFError.ThrowExceptionForHR(hr);
            }

            if (excpt == null)
            {
                hr = HResult.S_OK;
            }
            else
            {
                hr = (HResult)Marshal.GetHRForException(excpt);
            }

            return(hr);
        }
        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);
        }
        // News
        public void QueueNews(string blurb, string subBlurb, string outcome, bool highlight = false, MediaEventType eventType = MediaEventType.Mundane)
        {
            MediaEvent me = new MediaEvent(this, blurb, subBlurb, outcome, highlight);

            me.EventType = eventType;
            Parent.NextRoundNews.Add(me);
        }
Example #19
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);
        }
Example #20
0
        public int QueueEvent(MediaEventType met, Guid guidExtendedType, int hrStatus, ConstPropVariant pvValue)
        {
            // Make sure we *never* leave this entry point with an exception
            try
            {
                int hr;
                m_Log.WriteLine("-QueueEvent");

                lock (this)
                {
                    CheckShutdown();
                    hr = m_pEventQueue.QueueEventParamVar(met, guidExtendedType, hrStatus, pvValue);
                    MFError.ThrowExceptionForHR(hr);
                }
                return S_Ok;
            }
            catch (Exception e)
            {
                return Marshal.GetHRForException(e);
            }
        }