Exemplo n.º 1
0
 public static void FreeFormat(ref AMMediaType mt)
 {
     if (mt != null)
     {
         if (mt.formatPtr != IntPtr.Zero)
         {
             Marshal.FreeCoTaskMem(mt.formatPtr);
             mt.formatPtr = IntPtr.Zero;
         }
         mt.formatSize = 0;
     }
 }
Exemplo n.º 2
0
 public static bool IsValid(AMMediaType mt)
 {
     return (mt.majorType != null && mt.majorType != Guid.Empty);
 }
Exemplo n.º 3
0
 public static void SetFormat(ref AMMediaType mt, ref WaveFormatEx wfx)
 {
     if (wfx != null)
     {
         int cb = Marshal.SizeOf(wfx);
         IntPtr _ptr = Marshal.AllocCoTaskMem(cb);
         try
         {
             Marshal.StructureToPtr(wfx, _ptr, true);
             SetFormat(ref mt, _ptr, cb);
             if (mt != null)
             {
                 mt.formatType = FormatType.WaveEx;
             }
         }
         finally
         {
             Marshal.FreeCoTaskMem(_ptr);
         }
     }
 }
Exemplo n.º 4
0
 public static void SetFormat(ref AMMediaType mt, ref VideoInfoHeader vih)
 {
     if (vih != null)
     {
         int cb = Marshal.SizeOf(vih);
         IntPtr _ptr = Marshal.AllocCoTaskMem(cb);
         try
         {
             Marshal.StructureToPtr(vih, _ptr, true);
             SetFormat(ref mt, _ptr, cb);
             if (mt != null)
             {
                 mt.formatType = FormatType.VideoInfo;
             }
         }
         finally
         {
             Marshal.FreeCoTaskMem(_ptr);
         }
     }
 }
Exemplo n.º 5
0
        public int ConnectionMediaType(AMMediaType pmt)
        {
            if (m_pUnknown == IntPtr.Zero) return E_NOINTERFACE;

            ConnectionMediaTypeProc _Proc = GetProcDelegate<ConnectionMediaTypeProc>(7);

            if (_Proc == null) return E_UNEXPECTED;

            return (HRESULT)_Proc(
                        m_pUnknown,
                        pmt
                        );
        }
Exemplo n.º 6
0
        protected void CompleteAudioSampleGrabberIntialization()
        {
            _actualAudioFormat = null;
            if (sampleGrabber != null)
            {
                AMMediaType mtAudio = new AMMediaType();
                if (HRESULT.SUCCEEDED(sampleGrabber.GetConnectedMediaType(mtAudio)))
                {
                    _actualAudioFormat = (WaveFormatEx)Marshal.PtrToStructure(mtAudio.formatPtr, typeof(WaveFormatEx));

                    const int WAVEFORM_WNDSIZEFACTOR = 128;
                    const int VU_WNDSIZEFACTOR = 4096;
                    const int FFT_WNDSIZEFACTOR = 16;

                    int freq =
                        (MediaRenderer.DefaultInstance.ActualAudioFormat == null) ? 44100 :
                        MediaRenderer.DefaultInstance.ActualAudioFormat.nSamplesPerSec;

                    try
                    {
                        int k1 = 0, k2 = 0, k3 = 0;

                        while (freq / (1 << k1) > WAVEFORM_WNDSIZEFACTOR)
                            k1++;
                        while (freq / (1 << k2) > FFT_WNDSIZEFACTOR)
                            k2++;
                        while (freq / (1 << k3) > VU_WNDSIZEFACTOR)
                            k3++;

                        _waveformWindowSize = (1 << k1);
                        _fftWindowSize = (1 << k2);
                        _vuMeterWindowSize = (1 << k3);

                        _maxLevel =
                            (MediaRenderer.DefaultInstance.ActualAudioFormat != null) ?
                            (1 << (MediaRenderer.DefaultInstance.ActualAudioFormat.wBitsPerSample - 1)) - 1 :
                            short.MaxValue;
                    }
                    catch
                    {
                        _vuMeterWindowSize = 64;
                        _waveformWindowSize = 512;
                        _fftWindowSize = 4096;
                        _maxLevel = short.MaxValue;
                    }
                    finally
                    {
                        _maxLogLevel = Math.Log(_maxLevel);
                    }

                    sampleGrabberConfigured.Set();
                    return;
                }
            }
        }
Exemplo n.º 7
0
        public static void Copy(AMMediaType mt, ref AMMediaType _dest)
        {
            if (((object)_dest) == null)
            {
                _dest = new AMMediaType();
            }
            else
            {
                Free(ref _dest);
            }

            _dest.majorType = mt.majorType;
            _dest.subType = mt.subType;
            _dest.fixedSizeSamples = mt.fixedSizeSamples;
            _dest.temporalCompression = mt.temporalCompression;
            _dest.sampleSize = mt.sampleSize;
            _dest.formatType = mt.formatType;
            _dest.unkPtr = mt.unkPtr;
            _dest.formatPtr = IntPtr.Zero;
            _dest.formatSize = mt.formatSize;
            if (_dest.unkPtr != IntPtr.Zero)
            {
                Marshal.AddRef(_dest.unkPtr);
            }
            if (_dest.formatSize > 0)
            {
                _dest.formatPtr = Marshal.AllocCoTaskMem(_dest.formatSize);
                Kernel32.CopyMemory(_dest.formatPtr, mt.formatPtr, _dest.formatSize);
            }
        }
Exemplo n.º 8
0
        public int SetMediaType(AMMediaType pMediaType)
        {
            if (m_pUnknown == IntPtr.Zero) return E_NOINTERFACE;

            SetMediaTypeProc _Proc = GetProcDelegate<SetMediaTypeProc>(14);

            if (_Proc == null) return E_UNEXPECTED;

            return (HRESULT)_Proc(
                        m_pUnknown,
                        pMediaType
                        );
        }
Exemplo n.º 9
0
 public static bool IsPartiallySpecified(AMMediaType mt)
 {
     if ((mt.majorType == Guid.Empty) || (mt.formatType == Guid.Empty))
     {
         return true;
     }
     else
     {
         return false;
     }
 }
Exemplo n.º 10
0
 public static void Init(ref AMMediaType mt)
 {
     if (((object)mt) == null)
     {
         mt = new AMMediaType();
     }
     else
     {
         Free(ref mt);
     }
     mt.majorType = Guid.Empty;
     mt.subType = Guid.Empty;
     mt.fixedSizeSamples = true;
     mt.temporalCompression = false;
     mt.sampleSize = 0;
     mt.formatType = Guid.Empty;
     mt.unkPtr = IntPtr.Zero;
     mt.formatPtr = IntPtr.Zero;
     mt.formatSize = 0;
 }
Exemplo n.º 11
0
        public bool MatchesPartial(AMMediaType _dst)
        {
            if ((_dst.majorType != Guid.Empty) && (majorType != _dst.majorType))
            {
                return false;
            }
            if ((_dst.subType != Guid.Empty) && (subType != _dst.subType))
            {
                return false;
            }
            if (_dst.formatType != Guid.Empty)
            {
                if (formatType != _dst.formatType)
                {
                    return false;
                }
                if (formatSize != _dst.formatSize)
                {
                    return false;
                }
                if (formatSize > 0)
                {
                    byte[] _source = new byte[formatSize];
                    byte[] _dest = new byte[formatSize];
                    Marshal.Copy(formatPtr, _source, 0, _source.Length);
                    Marshal.Copy(_dst.formatPtr, _dest, 0, _dest.Length);
                    for (int i = 0; i < _source.Length; i++)
                    {
                        if (_dest[i] != _source[i]) return false;
                    }
                }

            }
            return true;
        }
Exemplo n.º 12
0
        public void Set(AMMediaType mt)
        {
            Free();

            majorType = mt.majorType;
            subType = mt.subType;
            fixedSizeSamples = mt.fixedSizeSamples;
            temporalCompression = mt.temporalCompression;
            sampleSize = mt.sampleSize;
            formatType = mt.formatType;
            unkPtr = mt.unkPtr;
            formatPtr = IntPtr.Zero;
            formatSize = mt.formatSize;
            if (unkPtr != IntPtr.Zero)
            {
                Marshal.AddRef(unkPtr);
            }
            if (formatSize > 0)
            {
                SetFormat(mt.formatPtr, formatSize);
            }
        }
Exemplo n.º 13
0
 public AMMediaType(AMMediaType mt)
     : this()
 {
     Set(mt);
 }
Exemplo n.º 14
0
 public static bool AreEquals(AMMediaType _src, AMMediaType _dst)
 {
     if ((_dst.majorType != _src.majorType))
     {
         return false;
     }
     if (_src.subType != _dst.subType)
     {
         return false;
     }
     if (_src.formatType != _dst.formatType)
     {
         return false;
     }
     if (_src.formatSize != _dst.formatSize)
     {
         return false;
     }
     if (_src.formatSize > 0)
     {
         byte[] _source = new byte[_src.formatSize];
         byte[] _dest = new byte[_src.formatSize];
         Marshal.Copy(_src.formatPtr, _source, 0, _source.Length);
         Marshal.Copy(_dst.formatPtr, _dest, 0, _dest.Length);
         for (int i = 0; i < _source.Length; i++)
         {
             if (_dest[i] != _source[i]) return false;
         }
     }
     return true;
 }
Exemplo n.º 15
0
 public static void Free(ref AMMediaType mt)
 {
     if (mt != null)
     {
         FreeFormat(ref mt);
         if (mt.unkPtr != IntPtr.Zero)
         {
             Marshal.Release(mt.unkPtr);
             mt.unkPtr = IntPtr.Zero;
         }
     }
 }
Exemplo n.º 16
0
        public int GetMediaType(out AMMediaType ppMediaType)
        {
            ppMediaType = null;
            if (m_pUnknown == IntPtr.Zero) return E_NOINTERFACE;

            GetMediaTypeProc _Proc = GetProcDelegate<GetMediaTypeProc>(13);

            if (_Proc == null) return E_UNEXPECTED;

            return (HRESULT)_Proc(
                        m_pUnknown,
                        out ppMediaType
                        );
        }
Exemplo n.º 17
0
 public static void AllocFormat(ref AMMediaType mt, int nSize)
 {
     FreeFormat(ref mt);
     if (mt != null && nSize > 0)
     {
         mt.formatPtr = Marshal.AllocCoTaskMem(nSize);
         mt.formatSize = nSize;
     }
 }
Exemplo n.º 18
0
        public int ReceiveConnection(IntPtr pReceivePin, AMMediaType pmt)
        {
            if (m_pUnknown == IntPtr.Zero) return E_NOINTERFACE;

            ReceiveConnectionProc _Proc = GetProcDelegate<ReceiveConnectionProc>(4);

            if (_Proc == null) return E_UNEXPECTED;

            PinInfo pi = new PinInfo();
            int hr = QueryPinInfo(out pi);

            PinInfo pi2 = new PinInfo();
            hr = (Marshal.GetObjectForIUnknown(pReceivePin) as IPin).QueryPinInfo(out pi2);

            hr = _Proc(
                        m_pUnknown,
                        pReceivePin, pmt
                        );

            //try
            //{
            //    DsError.ThrowExceptionForHR(hr);
            //}
            //catch (Exception ex)
            //{
            //}

            Marshal.ThrowExceptionForHR(hr);
            
            return hr;
        }
Exemplo n.º 19
0
 public static void SetFormat(ref AMMediaType mt, IntPtr pFormat, int nSize)
 {
     AllocFormat(ref mt, nSize);
     if (mt != null && pFormat != IntPtr.Zero)
     {
         Kernel32.CopyMemory(mt.formatPtr, pFormat, mt.formatSize);
     }
 }
Exemplo n.º 20
0
        public int QueryAccept(AMMediaType pmt)
        {
            if (m_pUnknown == IntPtr.Zero) return E_NOINTERFACE;

            QueryAcceptProc _Proc = GetProcDelegate<QueryAcceptProc>(11);

            if (_Proc == null) return E_UNEXPECTED;

            return (HRESULT)_Proc(
                        m_pUnknown,
                        pmt
                        );
        }
Exemplo n.º 21
0
        /*
        protected void InitAudioSampleGrabber()
        {
            // Get the graph builder
            IGraphBuilder graphBuilder = (mediaControl as IGraphBuilder);
            if (graphBuilder == null)
                return; 
            
            try
            {
                // Build the sample grabber
                sampleGrabber = Activator.CreateInstance(Type.GetTypeFromCLSID(Filters.SampleGrabber, true))
                    as ISampleGrabber;

                if (sampleGrabber == null)
                    return;

                // Add it to the filter graph
                int hr = graphBuilder.AddFilter(sampleGrabber as IBaseFilter, "ProTONE_SampleGrabber");
                DsError.ThrowExceptionForHR(hr);

                AMMediaType mtAudio = new AMMediaType();
                mtAudio.majorType = MediaType.Audio;
                mtAudio.subType = MediaSubType.PCM;
                mtAudio.formatPtr = IntPtr.Zero;

                _actualAudioFormat = null;

                hr = sampleGrabber.SetMediaType(mtAudio);
                DsError.ThrowExceptionForHR(hr);

                hr = sampleGrabber.SetBufferSamples(true);
                DsError.ThrowExceptionForHR(hr);

                hr = sampleGrabber.SetOneShot(false);
                DsError.ThrowExceptionForHR(hr);

                hr = sampleGrabber.SetCallback(this, 1);
                DsError.ThrowExceptionForHR(hr);

                sampleAnalyzerMustStop.Reset();
                sampleAnalyzerThread = new Thread(new ThreadStart(SampleAnalyzerLoop));
                sampleAnalyzerThread.Priority = ThreadPriority.Highest;
                sampleAnalyzerThread.Start();
            }
            catch(Exception ex)
            {
                Logger.LogException(ex);
            }

            rotEntry = new DsROTEntry(graphBuilder as IFilterGraph);
        }*/

        protected void InitAudioSampleGrabber_v2()
        {
            // Get the graph builder
            IGraphBuilder graphBuilder = (mediaControl as IGraphBuilder);
            if (graphBuilder == null)
                return;

            try
            {
                // Build the sample grabber
                sampleGrabber = Activator.CreateInstance(Type.GetTypeFromCLSID(Filters.SampleGrabber, true))
                    as ISampleGrabber;

                if (sampleGrabber == null)
                    return;

                // Add it to the filter graph
                int hr = graphBuilder.AddFilter(sampleGrabber as IBaseFilter, "ProTONE_SampleGrabber_v2");
                DsError.ThrowExceptionForHR(hr);

                IBaseFilter ffdAudioDecoder = null;

                IPin ffdAudioDecoderOutput = null;
                IPin soundDeviceInput = null;
                IPin sampleGrabberInput = null;
                IPin sampleGrabberOutput = null;
                IntPtr pSoundDeviceInput = IntPtr.Zero;

                // When using FFDShow, typically we'll find
                // a ffdshow Audio Decoder connected to the sound device filter
                // 
                // i.e. [ffdshow Audio Decoder] --> [DirectSound Device]
                //
                // Our audio sample grabber supports only PCM sample input and output.
                // Its entire processing is based on this assumption.
                // 
                // Thus need to insert the audio sample grabber between the ffdshow Audio Decoder and the sound device
                // because this is the only place where we can find PCM samples. The sound device only accepts PCM.
                //
                // So we need to turn this graph:
                //
                // .. -->[ffdshow Audio Decoder]-->[DirectSound Device] 
                //
                // into this:
                //
                // .. -->[ffdshow Audio Decoder]-->[Sample grabber]-->[DirectSound Device] 
                //
                // Actions to do to achieve the graph change:
                //
                // 1. Locate the ffdshow Audio Decoder in the graph
                // 2. Find its output pin and the pin that it's connected to
                // 3. Locate the input and output pins of sample grabber
                // 4. Disconnect the ffdshow Audio Decoder and its correspondent (sound device input pin)
                // 5. Connect the ffdshow Audio Decoder to sample grabber input
                // 6. Connect the sample grabber output to sound device input
                // that's all.

                // --------------
                // 1. Locate the ffdshow Audio Decoder in the graph
                hr = graphBuilder.FindFilterByName("ffdshow Audio Decoder", out ffdAudioDecoder);
                DsError.ThrowExceptionForHR(hr);

                // 2. Find its output pin and the pin that it's connected to
                hr = ffdAudioDecoder.FindPin("Out", out ffdAudioDecoderOutput);
                DsError.ThrowExceptionForHR(hr);

                hr = ffdAudioDecoderOutput.ConnectedTo(out pSoundDeviceInput);
                DsError.ThrowExceptionForHR(hr);

                soundDeviceInput = new DSPin(pSoundDeviceInput).Value;

                // 3. Locate the input and output pins of sample grabber
                hr = (sampleGrabber as IBaseFilter).FindPin("In", out sampleGrabberInput);
                DsError.ThrowExceptionForHR(hr);

                hr = (sampleGrabber as IBaseFilter).FindPin("Out", out sampleGrabberOutput);
                DsError.ThrowExceptionForHR(hr);

                // 4. Disconnect the ffdshow Audio Decoder and its correspondent (sound device input pin)
                hr = ffdAudioDecoderOutput.Disconnect();
                DsError.ThrowExceptionForHR(hr);

                hr = soundDeviceInput.Disconnect();
                DsError.ThrowExceptionForHR(hr);

                // 5. Connect the ffdshow Audio Decoder to sample grabber input
                hr = graphBuilder.Connect(ffdAudioDecoderOutput, sampleGrabberInput);
                DsError.ThrowExceptionForHR(hr);

                // 6. Connect the sample grabber output to sound device input
                hr = graphBuilder.Connect(sampleGrabberOutput, soundDeviceInput);
                DsError.ThrowExceptionForHR(hr);


                AMMediaType mtAudio = new AMMediaType();
                mtAudio.majorType = MediaType.Audio;
                mtAudio.subType = MediaSubType.PCM;
                mtAudio.formatPtr = IntPtr.Zero;

                _actualAudioFormat = null;

                sampleGrabber.SetMediaType(mtAudio);
                sampleGrabber.SetBufferSamples(true);
                sampleGrabber.SetOneShot(false);
                sampleGrabber.SetCallback(this, 1);

                sampleAnalyzerMustStop.Reset();
                sampleAnalyzerThread = new Thread(new ThreadStart(SampleAnalyzerLoop));
                sampleAnalyzerThread.Priority = ThreadPriority.Highest;
                sampleAnalyzerThread.Start();
            }
            catch (Exception ex)
            {
                Logger.LogException(ex);
            }

            rotEntry = new DsROTEntry(graphBuilder as IFilterGraph);
        }