Exemple #1
0
        //-------------------------------------------------------------------
        // SetDevice
        //
        // Set up preview for a specified video capture device.
        //-------------------------------------------------------------------

        public int SetDevice(MfDevice pDevice, ref string format)
        {
            int            hr;
            IMFMediaSource pSource = null;

            lock (LockSync)
            {
                try
                {
                    // Release the current device, if any.
                    hr        = CloseDevice();
                    pActivate = pDevice.Activator;
                    object o = null;
                    if (Succeeded(hr))
                    {
                        // Create the media source for the device.
                        hr = pActivate.ActivateObject(typeof(IMFMediaSource).GUID, out o);
                    }

                    if (Succeeded(hr))
                    {
                        pSource = (IMFMediaSource)o;
                    }

                    // Get Symbolic device link
                    PwszSymbolicLink = pDevice.SymbolicName;

                    // Create the source reader.
                    if (Succeeded(hr))
                    {
                        hr = OpenMediaSource(pSource, ref PReader);
                    }

                    if (Succeeded(hr))
                    {
                        var index = GetOptimizedFormatIndex(ref format);
                        if (index >= 0)
                        {
                            hr = ConfigureSourceReader(index);
                        }
                    }

                    if (Failed(hr))
                    {
                        pSource?.Shutdown();
                        //pActivate.ShutdownObject();
                        // NOTE: The source reader shuts down the media source
                        // by default, but we might not have gotten that far.
                        CloseDevice();
                    }
                }
                finally
                {
                    SafeRelease(pSource);
                }
            }

            return(hr);
        }
Exemple #2
0
        /// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
        /// <summary>
        /// A centralized place to close down all media devices.
        /// </summary>
        /// <history>
        ///    01 Nov 18  Cynic - Started
        /// </history>
        private void CloseAllMediaDevices()
        {
            HResult hr;

            LogMessage("CloseAllMediaDevices");

            // close and release our call back handler
            if (mediaSessionAsyncCallbackHandler != null)
            {
                // stop any messaging or events in the call back handler
                mediaSessionAsyncCallbackHandler.ShutDown();
                mediaSessionAsyncCallbackHandler = null;
            }

            // close the session (this is NOT the same as shutting it down)
            if (mediaSession != null)
            {
                hr = mediaSession.Close();
                if (hr != HResult.S_OK)
                {
                    // just log it
                    LogMessage("CloseAllMediaDevices call to mediaSession.Close failed. Err=" + hr.ToString());
                }
            }

            // Shut down the media source
            if (mediaSource != null)
            {
                hr = mediaSource.Shutdown();
                if (hr != HResult.S_OK)
                {
                    // just log it
                    LogMessage("CloseAllMediaDevices call to mediaSource.Shutdown failed. Err=" + hr.ToString());
                }
                Marshal.ReleaseComObject(mediaSource);
                mediaSource = null;
            }

            // Shut down the media session (note we only closed it before).
            if (mediaSession != null)
            {
                hr = mediaSession.Shutdown();
                if (hr != HResult.S_OK)
                {
                    // just log it
                    LogMessage("CloseAllMediaDevices call to mediaSession.Shutdown failed. Err=" + hr.ToString());
                }
                Marshal.ReleaseComObject(mediaSession);
                mediaSession = null;
            }

            // close the media sink
            if (mediaSink != null)
            {
                Marshal.ReleaseComObject(mediaSink);
                mediaSink = null;
            }
        }
Exemple #3
0
        private void CloseAllMediaDevices()
        {
            HResult hr;

            Console.WriteLine("CloseAllMediaDevices");
            // close and release our call back handler
            if (mediaSessionAsyncCallbackHandler != null)
            {
                // stop any messaging or events in the call back handler
                mediaSessionAsyncCallbackHandler.ShutDown();
                mediaSessionAsyncCallbackHandler = null;
            }
            // Shut down the source reader
            if (StreamReader != null)
            {
                Marshal.ReleaseComObject(StreamReader);
                StreamReader = null;
            }
            // close the session (this is NOT the same as shutting it down)
            if (mediaSession != null)
            {
                hr = mediaSession.Close();
                if (hr != HResult.S_OK)
                {
                    // just log it
                    Console.WriteLine("CloseAllMediaDevices call to mediaSession.Close failed. Err=" + hr.ToString());
                }
            }
            // Shut down the media source
            if (MediaSource != null)
            {
                hr = MediaSource.Shutdown();
                if (hr != HResult.S_OK)
                {
                    // just log it
                    Console.WriteLine("CloseAllMediaDevices call to mediaSource.Shutdown failed. Err=" + hr.ToString());
                }
                Marshal.ReleaseComObject(MediaSource);
                MediaSource = null;
            }
            // Shut down the media session (note we only closed it before).
            if (mediaSession != null)
            {
                hr = mediaSession.Shutdown();
                if (hr != HResult.S_OK)
                {
                    // just log it
                    Console.WriteLine("CloseAllMediaDevices call to mediaSession.Shutdown failed. Err=" + hr.ToString());
                }
                Marshal.ReleaseComObject(mediaSession);
                mediaSession = null;
            }
            if (currentVideoMediaType != null)
            {
                Marshal.ReleaseComObject(currentVideoMediaType);
                currentVideoMediaType = null;
            }
        }
Exemple #4
0
        ///////////////////////////////////////////////////////////////////////
        //  Name: CreateWavFile
        //  Description:  Creates a .wav file from an input file.
        ///////////////////////////////////////////////////////////////////////

        static void CreateWavFile(string sURL, string sOutputFile)
        {
            IMFByteStream   pStream   = null;
            IMFMediaSinkAlt pSink     = null;
            IMFMediaSource  pSource   = null;
            IMFTopology     pTopology = null;

            WavSinkNS.CWavSink pObj = null;

            HResult hr = MFExtern.MFCreateFile(MFFileAccessMode.Write, MFFileOpenMode.DeleteIfExist, MFFileFlags.None, sOutputFile, out pStream);

            MFError.ThrowExceptionForHR(hr);

            try
            {
                pObj  = new WavSinkNS.CWavSink(pStream);
                pSink = pObj as IMFMediaSinkAlt;

                // Create the media source from the URL.
                CreateMediaSource(sURL, out pSource);

                // Create the topology.
                CreateTopology(pSource, pSink, out pTopology);

                // Run the media session.
                RunMediaSession(pTopology);

                hr = pSource.Shutdown();
                MFError.ThrowExceptionForHR(hr);
            }
            finally
            {
                if (pStream != null)
                {
                    Marshal.ReleaseComObject(pStream);
                }
                if (pSource != null)
                {
                    Marshal.ReleaseComObject(pSource);
                }
                if (pTopology != null)
                {
                    Marshal.ReleaseComObject(pTopology);
                }
                //pObj.Dispose();
            }
        }
        protected void CloseSession()
        {
            int hr;

            /*if (m_pVideoDisplay != null)
             * {
             *  Marshal.ReleaseComObject(m_pVideoDisplay);
             *  m_pVideoDisplay = null;
             * }
             */
            if (m_pSession != null)
            {
                hr = m_pSession.Close();
                MFError.ThrowExceptionForHR(hr);

                // Wait for the close operation to complete

                /*bool res = m_hCloseEvent.WaitOne(5000, true);
                 * if (!res)
                 * {
                 *  TRACE(("WaitForSingleObject timed out!"));
                 * }*/
            }

            // Complete shutdown operations

            // 1. Shut down the media source
            if (m_pSource != null)
            {
                hr = m_pSource.Shutdown();
                MFError.ThrowExceptionForHR(hr);
                COMBase.SafeRelease(m_pSource);
                m_pSource = null;
            }

            // 2. Shut down the media session. (Synchronous operation, no events.)
            if (m_pSession != null)
            {
                hr = m_pSession.Shutdown();
                Marshal.ReleaseComObject(m_pSession);
                m_pSession = null;
            }
        }
Exemple #6
0
        /// <summary>
        /// Close a Media Session
        /// </summary>
        protected void CloseSession()
        {
            if (m_pVideoDisplay != null)
            {
                Marshal.ReleaseComObject(m_pVideoDisplay);
                m_pVideoDisplay = null;
            }

            if (m_pSession != null)
            {
                m_pSession.Close();

                // Wait for the close operation to complete
                bool res = m_hCloseEvent.WaitOne(WAIT_TIMEOUT, true);
                if (!res)
                {
                    TRACE("WaitForSingleObject Timed Out !");
                }
            }

            // Complete shutdown operations
            // 1. Shut down the media source
            if (m_pSource != null)
            {
                m_pSource.Shutdown();
                SafeRelease(m_pSource);
                m_pSource = null;
            }

            // 2. Shut down the media session. (Synchronous operation, no events.)
            if (m_pSession != null)
            {
                m_pSession.Shutdown();
                Marshal.ReleaseComObject(m_pSession);
                m_pSession = null;
            }
        }
Exemple #7
0
        //-------------------------------------------------------------------
        // SetDevice
        //
        // Set up preview for a specified video capture device.
        //-------------------------------------------------------------------

        public HResult SetDevice(MFDevice pDevice)
        {
            HResult hr = HResult.S_OK;

            IMFActivate    pActivate   = pDevice.Activator;
            IMFMediaSource pSource     = null;
            IMFAttributes  pAttributes = null;
            object         o           = null;

            lock (this)
            {
                try
                {
                    // Release the current device, if any.
                    hr = CloseDevice();

                    if (Succeeded(hr))
                    {
                        // Create the media source for the device.
                        hr = pActivate.ActivateObject(typeof(IMFMediaSource).GUID, out o);
                    }

                    if (Succeeded(hr))
                    {
                        pSource = (IMFMediaSource)o;
                    }

                    // Get Symbolic device link
                    m_pwszSymbolicLink = pDevice.SymbolicName;

                    //
                    // Create the source reader.
                    //

                    // Create an attribute store to hold initialization settings.

                    if (Succeeded(hr))
                    {
                        hr = MFExtern.MFCreateAttributes(out pAttributes, 2);
                    }

                    if (Succeeded(hr))
                    {
                        hr = pAttributes.SetUINT32(MFAttributesClsid.MF_READWRITE_DISABLE_CONVERTERS, 1);
                    }

                    if (Succeeded(hr))
                    {
                        hr = pAttributes.SetUnknown(MFAttributesClsid.MF_SOURCE_READER_ASYNC_CALLBACK, this);
                    }

                    IMFSourceReader pRead = null;
                    if (Succeeded(hr))
                    {
                        hr = MFExtern.MFCreateSourceReaderFromMediaSource(pSource, pAttributes, out pRead);
                    }

                    if (Succeeded(hr))
                    {
                        m_pReader = (IMFSourceReaderAsync)pRead;
                    }

                    if (Succeeded(hr))
                    {
                        // Try to find a suitable output type.
                        for (int i = 0; ; i++)
                        {
                            IMFMediaType pType;
                            hr = m_pReader.GetNativeMediaType((int)MF_SOURCE_READER.FirstVideoStream, i, out pType);
                            if (Failed(hr))
                            {
                                break;
                            }

                            try
                            {
                                hr = TryMediaType(pType);
                                if (Succeeded(hr))
                                {
                                    // Found an output type.
                                    break;
                                }
                            }
                            finally
                            {
                                SafeRelease(pType);
                            }
                        }
                    }

                    if (Succeeded(hr))
                    {
                        // Ask for the first sample.
                        hr = m_pReader.ReadSample((int)MF_SOURCE_READER.FirstVideoStream, 0, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
                    }

                    if (Failed(hr))
                    {
                        if (pSource != null)
                        {
                            pSource.Shutdown();

                            // NOTE: The source reader shuts down the media source
                            // by default, but we might not have gotten that far.
                        }
                        CloseDevice();
                    }
                }
                finally
                {
                    SafeRelease(pSource);
                    SafeRelease(pAttributes);
                }
            }

            return(hr);
        }
Exemple #8
0
 protected override void PlatformDispose()
 {
     _audioSource.Shutdown();
     _audioSource.Release();
 }