Example #1
0
        public HResult Stop()
        {
            HResult hr = HResult.S_OK;

            lock (_spSource)
            {
                hr = CheckShutdown();
                if (MFError.Succeeded(hr))
                {
                    if (_eSourceState == SourceState.SourceState_Started)
                    {
                        _eSourceState = SourceState.SourceState_Stopped;
                        _tokens.Clear();
                        _samples.Clear();
                        // Inform the client that we've stopped.
                        hr = QueueEvent(MediaEventType.MEStreamStopped, Guid.Empty, HResult.S_OK, null);
                    }
                    else
                    {
                        hr = HResult.MF_E_INVALID_STATE_TRANSITION;
                    }
                }

                if (MFError.Failed(hr))
                {
                    HandleError(hr);
                }

                return(HResult.S_OK);
            }
        }
Example #2
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);
        }
Example #3
0
        public HResult Shutdown()
        {
            HResult hr = CheckShutdown();

            if (MFError.Succeeded(hr))
            {
                if (_spEventQueue != null)
                {
                    _spEventQueue.Shutdown();
                }
                if (_networkStreamAdapter != null)
                {
                    _networkStreamAdapter.Close();
                }

                foreach (var stream in _streams)
                {
                    (stream as MediaStream).Shutdown();
                }

                _eSourceState = SourceState.SourceState_Shutdown;
                _streams.Clear();
                _spEventQueue.Shutdown();
                _networkStreamAdapter = null;
            }

            return(hr);
        }
Example #4
0
 private void ThrowIfError(HResult hr)
 {
     if (!MFError.Succeeded(hr))
     {
         MFError.ThrowExceptionForHR(hr);
     }
 }
Example #5
0
        public HResult BeginGetEvent(IMFAsyncCallback pCallback, object punkState)
        {
            HResult hr = HResult.S_OK;

            hr = CheckShutdown();
            if (MFError.Succeeded(hr))
            {
                hr = _spEventQueue.BeginGetEvent(pCallback, punkState);
            }
            return(hr);
        }
Example #6
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 #7
0
        public HResult GetCharacteristics(out MFMediaSourceCharacteristics pdwCharacteristics)
        {
            pdwCharacteristics = MFMediaSourceCharacteristics.None;
            HResult hr = CheckShutdown();

            if (MFError.Succeeded(hr))
            {
                pdwCharacteristics = MFMediaSourceCharacteristics.IsLive;
            }

            return(hr);
        }
Example #8
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);
        }
Example #9
0
        public HResult Start(IMFPresentationDescriptor pPresentationDescriptor, Guid pguidTimeFormat, ConstPropVariant pvarStartPos)
        {
            HResult hr = HResult.S_OK;

            // Check parameters.

            // Start position and presentation descriptor cannot be NULL.
            if (pvarStartPos == null || pPresentationDescriptor == null)
            {
                return(HResult.E_INVALIDARG);
            }

            // Check the time format.
            if ((pguidTimeFormat != null) && (pguidTimeFormat != Guid.Empty))
            {
                // Unrecognized time format GUID.
                return(HResult.MF_E_UNSUPPORTED_TIME_FORMAT);
            }

            // Check the data type of the start position.
            if (pvarStartPos.GetVariantType() != VariantType.None && pvarStartPos.GetVariantType() != VariantType.Int64)
            {
                return(HResult.MF_E_UNSUPPORTED_TIME_FORMAT);
            }

            if (_eSourceState != SourceState.SourceState_Stopped && _eSourceState != SourceState.SourceState_Started)
            {
                hr = HResult.MF_E_INVALIDREQUEST;
            }

            if (MFError.Succeeded(hr))
            {
                // Check if the presentation description is valid.
                hr = ValidatePresentationDescriptor(pPresentationDescriptor);
            }

            if (MFError.Succeeded(hr))
            {
                CSourceOperation op = new CSourceOperation
                {
                    Type = SourceOperationType.Operation_Start,
                    PresentationDescriptor = pPresentationDescriptor,
                    Data = pvarStartPos
                };
                doStart(op);
            }
            return(hr);
        }
Example #10
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 #11
0
        public HResult CreatePresentationDescriptor(out IMFPresentationDescriptor ppPresentationDescriptor)
        {
            ppPresentationDescriptor = null;
            HResult hr = CheckShutdown();

            if (MFError.Succeeded(hr) && (_eSourceState == SourceState.SourceState_Opening || _eSourceState == SourceState.SourceState_Invalid || null == _spPresentationDescriptor))
            {
                hr = HResult.MF_E_NOT_INITIALIZED;
            }

            if (MFError.Succeeded(hr))
            {
                hr = _spPresentationDescriptor.Clone(out ppPresentationDescriptor);
            }

            return(hr);
        }
Example #12
0
        public HResult GetStreamDescriptor(out IMFStreamDescriptor ppStreamDescriptor)
        {
            HResult hr = HResult.S_OK;

            ppStreamDescriptor = null;

            lock (_spSource)
            {
                hr = CheckShutdown();

                if (MFError.Succeeded(hr))
                {
                    ppStreamDescriptor = _spStreamDescriptor;
                }

                return(hr);
            }
        }
Example #13
0
        public HResult GetMediaSource(out IMFMediaSource ppMediaSource)
        {
            HResult hr = HResult.S_OK;

            ppMediaSource = null;

            lock (_spSource)
            {
                hr = CheckShutdown();

                if (MFError.Succeeded(hr))
                {
                    ppMediaSource = _spSource;
                }

                return(hr);
            }
        }
Example #14
0
    public HResult EndCreateObject(IMFAsyncResult pResult,
                                   out MFObjectType pObjectType,
                                   out object ppObject)
    {
        pObjectType = MFObjectType.ByteStream;

        HResult hr = pResult.GetObject(out ppObject);

        if (MFError.Succeeded(hr))
        {
            IcyScheme icy = (IcyScheme)ppObject;

            // Wait for the HttpWebRequest to finish connecting.  This
            // includes sending the request, and processing the response
            // headers and caching some data (see m_FirstBlock).
            hr = icy.WaitForConnect();
        }

        return(hr);
    }
Example #15
0
    public HResult BeginRead(
        IntPtr pb, int cb, IMFAsyncCallback pCallback, object pUnkState)
    {
        // For reasons beyond my understanding, sometime we are asked to
        // return bytes at negative offsets.  In these cases, we change
        // the number of bytes requested to 0, which results in EndRead
        // returning 0 bytes.
        if (m_Position < 0)
        {
            cb = 0;
        }

        // This handles the case where SetCurrentPosition was called to
        // rewind the stream.
        if (m_Position == 0)
        {
            m_PositionReal = 0;
            m_SinceLast    = 0;
        }

        // Since BeginRead is async, caller could call GetCurrentPosition
        // while the read is running.  Spec says they get the new value.
        m_Position += cb;

        IMFAsyncResult pResult;
        ReadParams     rp = new ReadParams(pb, cb);

        HResult hr = MFExtern.MFCreateAsyncResult(
            rp, pCallback, pUnkState, out pResult);

        // If 2 BeginReads are called consecutively, might the Workqueue
        // run them concurrently?  I don't know, but that would be bad.
        if (MFError.Succeeded(hr))
        {
            hr = MFExtern.MFPutWorkItem(
                (int)MFASYNC_WORKQUEUE_TYPE.StandardWorkqueue,
                this, pResult);
        }

        return(hr);
    }
Example #16
0
        public HResult Shutdown()
        {
            lock (_spSource)
            {
                HResult hr = CheckShutdown();

                if (MFError.Succeeded(hr))
                {
                    Flush();
                    if (null != _spEventQueue)
                    {
                        _spEventQueue.Shutdown();
                    }

                    _spStreamDescriptor = null;
                    _eSourceState       = SourceState.SourceState_Shutdown;
                }

                return(hr);
            }
        }
Example #17
0
        internal HResult SetActive(bool fActive)
        {
            lock (_spSource)
            {
                HResult hr = CheckShutdown();

                if (MFError.Succeeded(hr))
                {
                    if (_eSourceState != SourceState.SourceState_Stopped && _eSourceState != SourceState.SourceState_Started)
                    {
                        hr = HResult.MF_E_INVALIDREQUEST;
                    }
                }

                if (MFError.Succeeded(hr))
                {
                    _fActive = fActive;
                }

                return(hr);
            }
        }
Example #18
0
    public HResult EndRead(IMFAsyncResult pResult, out int pcbRead)
    {
        // In theory, pcbRead might be less than what was requested. However
        // that seems to drive our caller nuts, so we only do that when
        // m_Position < 0.
        HResult hr;
        object  o;

        hr = pResult.GetObject(out o);

        if (MFError.Succeeded(hr))
        {
            ReadParams rp = (ReadParams)o;
            pcbRead = rp.cb;
        }
        else
        {
            pcbRead = 0;
        }

        return(hr);
    }
Example #19
0
        private HResult ValidatePresentationDescriptor(IMFPresentationDescriptor pPD)
        {
            HResult hr        = HResult.S_OK;
            bool    fSelected = false;
            int     cStreams  = 0;

            if (_streams.Count == 0)
            {
                return(HResult.E_UNEXPECTED);
            }

            // The caller's PD must have the same number of streams as ours.
            hr = pPD.GetStreamDescriptorCount(out cStreams);

            if (MFError.Succeeded(hr))
            {
                if (cStreams != _streams.Count)
                {
                    hr = HResult.E_INVALIDARG;
                }
            }

            // The caller must select at least one stream.
            if (MFError.Succeeded(hr))
            {
                for (int i = 0; i < cStreams; i++)
                {
                    IMFStreamDescriptor spSD;
                    hr = pPD.GetStreamDescriptorByIndex(i, out fSelected, out spSD);
                    if (MFError.Failed(hr))
                    {
                        break;
                    }
                }
            }

            return(hr);
        }