void BuildGraph()
        {
            int         hr;
            IBaseFilter ppFilter;

            DsDevice []   devs;
            IGraphBuilder graphBuilder = new FilterGraph() as IGraphBuilder;

            m_ROT = new DsROTEntry(graphBuilder);
            IFilterGraph2 ifg2 = graphBuilder as IFilterGraph2;

            devs = DsDevice.GetDevicesOfCat(FilterCategory.VideoInputDevice);
            DsDevice dev = devs[0];

            hr = ifg2.AddSourceFilterForMoniker(dev.Mon, null, dev.Name, out ppFilter);
            DsError.ThrowExceptionForHR(hr);

            m_idf = ppFilter as IAMDroppedFrames;

            IPin IPinOut = DsFindPin.ByDirection(ppFilter, PinDirection.Output, 0);

            hr = ifg2.Render(IPinOut);
            DsError.ThrowExceptionForHR(hr);

            m_imc = graphBuilder as IMediaControl;
            hr    = m_imc.Run();
            DsError.ThrowExceptionForHR(hr);
        }
Esempio n. 2
0
        public void InitializeGraph()
        {
            int hr = 0;

            graphBuilder = (IFilterGraph2) new FilterGraph();
            rot          = new DsROTEntry(graphBuilder);

            ICaptureGraphBuilder2 capBuilder = (ICaptureGraphBuilder2) new CaptureGraphBuilder2();

            capBuilder.SetFiltergraph(graphBuilder);

            // Get the BDA network provider specific for this given network type
            networkProvider = BDAUtils.GetNetworkProvider(networkType);

            hr = graphBuilder.AddFilter(networkProvider, "BDA Network Provider");
            DsError.ThrowExceptionForHR(hr);

            tuner = (ITuner)networkProvider;


            // found a BDA Tuner and a BDA Capture that can connect to this network provider
            BDAUtils.AddBDATunerAndDemodulatorToGraph(graphBuilder, networkProvider, out bdaTuner, out bdaCapture);

            if ((bdaTuner != null) && (bdaCapture != null))
            {
                // Create and add the mpeg2 demux
                mpeg2Demux = (IBaseFilter) new MPEG2Demultiplexer();

                hr = graphBuilder.AddFilter(mpeg2Demux, "MPEG2 Demultiplexer");
                DsError.ThrowExceptionForHR(hr);

                // connect it to the BDA Capture
                hr = capBuilder.RenderStream(null, null, bdaCapture, null, mpeg2Demux);
                DsError.ThrowExceptionForHR(hr);

                // Add the two mpeg2 transport stream helper filters
                BDAUtils.AddTransportStreamFiltersToGraph(graphBuilder, out bdaTIF, out bdaSecTab);

                if ((bdaTIF != null) && (bdaSecTab != null))
                {
                    // Render all the output pins of the demux (missing filters are added)
                    for (int i = 0; i < 5; i++)
                    {
                        IPin pin = DsFindPin.ByDirection(mpeg2Demux, PinDirection.Output, i);

                        hr = graphBuilder.Render(pin);
                        Marshal.ReleaseComObject(pin);
                    }
                }
                else
                {
                    throw new ApplicationException("Can't add the BDA support filters");
                }
            }
            else
            {
                throw new ApplicationException("Can't add a tuner and a demodulator");
            }
        }
        void BuildGraph()
        {
            int         hr;
            IBaseFilter ppFilter;

            DsDevice []   devs;
            IGraphBuilder graphBuilder = new FilterGraph() as IGraphBuilder;

            m_ROT = new DsROTEntry(graphBuilder);
            IFilterGraph2 ifg2 = graphBuilder as IFilterGraph2;

            devs = DsDevice.GetDevicesOfCat(FilterCategory.VideoInputDevice);
            DsDevice dev = devs[0];

            hr = ifg2.AddSourceFilterForMoniker(dev.Mon, null, dev.Name, out ppFilter);
            DsError.ThrowExceptionForHR(hr);

            m_ivc = ppFilter as IAMVideoControl;

            m_IPinOut = DsFindPin.ByDirection(ppFilter, PinDirection.Output, 0);

            hr = ifg2.Render(m_IPinOut);
            DsError.ThrowExceptionForHR(hr);

            ICaptureGraphBuilder2 captureGraphBuilder = new CaptureGraphBuilder2() as ICaptureGraphBuilder2;

            hr = captureGraphBuilder.SetFiltergraph(graphBuilder);

            object o;

            hr = captureGraphBuilder.FindInterface(null, null, ppFilter, typeof(IAMAnalogVideoDecoder).GUID, out o);
            DsError.ThrowExceptionForHR(hr);

            m_avd = o as IAMAnalogVideoDecoder;

            m_imc = graphBuilder as IMediaControl;
            hr    = m_imc.Run();
            DsError.ThrowExceptionForHR(hr);
        }
Esempio n. 4
0
        private void BuildGraph()
        {
            int hr = 0;

            graphBuilder = (IFilterGraph2) new FilterGraph();
            rot          = new DsROTEntry(graphBuilder);

            ICaptureGraphBuilder2 capBuilder = (ICaptureGraphBuilder2) new CaptureGraphBuilder2();

            capBuilder.SetFiltergraph(graphBuilder);

            // Get the BDA network provider specific for this given network type
            networkProvider = BDAUtils.GetNetworkProvider(networkType);

            hr = graphBuilder.AddFilter(networkProvider, "BDA Network Provider");
            DsError.ThrowExceptionForHR(hr);

            tuner = (ITuner)networkProvider;

            // Get a tuning space for this network type
            tuningSpace = BDAUtils.GetTuningSpace(networkType);

            hr = tuner.put_TuningSpace(tuningSpace);
            DsError.ThrowExceptionForHR(hr);

            // Create a tune request from this tuning space
            tuneRequest = BDAUtils.CreateTuneRequest(tuningSpace);

            // Is it okay ?
            hr = tuner.Validate(tuneRequest);
            if (hr == 0)
            {
                // Set it
                hr = tuner.put_TuneRequest(tuneRequest);
                DsError.ThrowExceptionForHR(hr);

                // found a BDA Tuner and a BDA Capture that can connect to this network provider
                BDAUtils.AddBDATunerAndDemodulatorToGraph(graphBuilder, networkProvider, out bdaTuner, out bdaCapture);

                if ((bdaTuner != null) && (bdaCapture != null))
                {
                    // Create and add the mpeg2 demux
                    mpeg2Demux = (IBaseFilter) new MPEG2Demultiplexer();

                    hr = graphBuilder.AddFilter(mpeg2Demux, "MPEG2 Demultiplexer");
                    DsError.ThrowExceptionForHR(hr);

                    // connect it to the BDA Capture
                    hr = capBuilder.RenderStream(null, null, bdaCapture, null, mpeg2Demux);
                    DsError.ThrowExceptionForHR(hr);

                    // Add the two mpeg2 transport stream helper filters
                    BDAUtils.AddTransportStreamFiltersToGraph(graphBuilder, out bdaTIF, out bdaSecTab);

                    if ((bdaTIF != null) && (bdaSecTab != null))
                    {
                        // Render all the output pins of the demux (missing filters are added)
                        for (int i = 0; i < 5; i++)
                        {
                            IPin pin = DsFindPin.ByDirection(mpeg2Demux, PinDirection.Output, i);

                            hr = graphBuilder.Render(pin);
                            Marshal.ReleaseComObject(pin);
                        }
                    }

                    IBDA_Topology topo      = (IBDA_Topology)bdaTuner;
                    int[]         nodeTypes = new int[10];
                    int           nodeTypesCount;

                    // Get all nodes in the BDA Tuner
                    hr = topo.GetNodeTypes(out nodeTypesCount, nodeTypes.Length, nodeTypes);
                    DsError.ThrowExceptionForHR(hr);

                    // For each nodes types
                    for (int i = 0; i < nodeTypesCount; i++)
                    {
                        Guid[] nodeGuid = new Guid[10];
                        int    nodeGuidCount;

                        // Get its exposed interfaces
                        hr = topo.GetNodeInterfaces(nodeTypes[i], out nodeGuidCount, nodeGuid.Length, nodeGuid);
                        DsError.ThrowExceptionForHR(hr);

                        // For each exposed interfaces
                        for (int j = 0; j < nodeGuidCount; j++)
                        {
                            Debug.WriteLine(string.Format("node {0}/{1} : {2}", i, j, nodeGuid[j]));
                            Console.WriteLine(string.Format("node {0}/{1} : {2}", i, j, nodeGuid[j]));

                            // Is IBDA_LNBInfo supported by this node ?
                            if (nodeGuid[j] == typeof(IBDA_LNBInfo).GUID)
                            {
                                // Yes, retrieve this node
                                object ctrlNode;
                                hr = topo.GetControlNode(0, 1, nodeTypes[i], out ctrlNode);
                                DsError.ThrowExceptionForHR(hr);

                                // Do the cast (it should not fail)
                                lnbInfo = ctrlNode as IBDA_LNBInfo;

                                // Exit the for j loop if found a SignalStatistics object
                                if (lnbInfo != null)
                                {
                                    break;
                                }
                            }
                        }

                        // If already found a SignalStatistics object, exit the for i loop
                        if (lnbInfo != null)
                        {
                            break;
                        }
                    }
                }
            }
        }
Esempio n. 5
0
        public bool Play(string fileName, Form form)
        {
            fileName += ".tsbuffer";
            Log.WriteFile("play:{0}", fileName);
            _graphBuilder = (IFilterGraph2) new FilterGraph();
            _rotEntry     = new DsROTEntry(_graphBuilder);


            Log.WriteFile("add tsfilesource");
            _tsFileSource = new TsFileSource();
            _graphBuilder.AddFilter((IBaseFilter)_tsFileSource, "TsFileSource");

            #region add mpeg-2 demux filter

            Log.WriteFile("add mpeg-2 demux");
            MPEG2Demultiplexer demux = new MPEG2Demultiplexer();
            _mpegDemux = (IBaseFilter)demux;
            int hr = _graphBuilder.AddFilter(_mpegDemux, "MPEG-2 Demultiplexer");

            #endregion

            #region create mpeg2 demux pins

            Log.WriteFile("create mpeg-2 demux pins");
            //create mpeg-2 demux output pins
            IMpeg2Demultiplexer demuxer = _mpegDemux as IMpeg2Demultiplexer;


            if (demuxer != null)
            {
                hr = demuxer.CreateOutputPin(GetAudioMpg2Media(), "Audio", out _pinAudio);
            }
            if (hr != 0)
            {
                Log.WriteFile("unable to create audio pin");
                return(false);
            }
            if (demuxer != null)
            {
                hr = demuxer.CreateOutputPin(GetVideoMpg2Media(), "Video", out _pinVideo);
            }
            if (hr != 0)
            {
                Log.WriteFile("unable to create video pin");
                return(false);
            }

            #endregion

            #region load file in tsfilesource

            Log.WriteFile("load file in tsfilesource");
            IFileSourceFilter interfaceFile = (IFileSourceFilter)_tsFileSource;
            if (interfaceFile == null)
            {
                Log.WriteFile("TSStreamBufferPlayer9:Failed to get IFileSourceFilter");
                return(false);
            }

            AMMediaType mpeg2ProgramStream = new AMMediaType();
            mpeg2ProgramStream.majorType = MediaType.Stream;
            mpeg2ProgramStream.subType   = MediaSubType.Mpeg2Program;

            mpeg2ProgramStream.unkPtr              = IntPtr.Zero;
            mpeg2ProgramStream.sampleSize          = 0;
            mpeg2ProgramStream.temporalCompression = false;
            mpeg2ProgramStream.fixedSizeSamples    = true;
            mpeg2ProgramStream.formatType          = FormatType.None;
            mpeg2ProgramStream.formatSize          = 0;
            mpeg2ProgramStream.formatPtr           = IntPtr.Zero;
            hr = interfaceFile.Load(fileName, mpeg2ProgramStream);

            if (hr != 0)
            {
                Log.WriteFile("TSStreamBufferPlayer9:Failed to load file");
                return(false);
            }

            #region connect tsfilesource->demux

            Log.WriteFile("connect tsfilesource->demux");
            Log.WriteFile("TSStreamBufferPlayer9:connect tsfilesource->mpeg2 demux");
            IPin pinTsOut = DsFindPin.ByDirection((IBaseFilter)_tsFileSource, PinDirection.Output, 0);
            if (pinTsOut == null)
            {
                Log.WriteFile("TSStreamBufferPlayer9:failed to find output pin of tsfilesource");
                return(false);
            }
            IPin pinDemuxIn = DsFindPin.ByDirection(_mpegDemux, PinDirection.Input, 0);
            if (pinDemuxIn == null)
            {
                Log.WriteFile("TSStreamBufferPlayer9:failed to find output pin of tsfilesource");
                return(false);
            }

            hr = _graphBuilder.Connect(pinTsOut, pinDemuxIn);
            if (hr != 0)
            {
                Log.WriteFile("TSStreamBufferPlayer9:failed to connect tsfilesource->mpeg2 demux:{0:X}", hr);
                return(false);
            }
            Release.ComObject(pinTsOut);
            Release.ComObject(pinDemuxIn);

            #endregion

            #region map demux pids

            Log.WriteFile("map mpeg2 pids");
            IMPEG2StreamIdMap pStreamId = (IMPEG2StreamIdMap)_pinVideo;
            hr = pStreamId.MapStreamId(0xe0, MPEG2Program.ElementaryStream, 0, 0);
            if (hr != 0)
            {
                Log.WriteFile("TSStreamBufferPlayer9: failed to map pid 0xe0->video pin");
                return(false);
            }
            pStreamId = (IMPEG2StreamIdMap)_pinAudio;
            hr        = pStreamId.MapStreamId(0xc0, MPEG2Program.ElementaryStream, 0, 0);
            if (hr != 0)
            {
                Log.WriteFile("TSStreamBufferPlayer9: failed  to map pid 0xc0->audio pin");
                return(false);
            }

            #endregion

            #region render demux audio/video pins

            Log.WriteFile("render pins");
            hr = _graphBuilder.Render(_pinAudio);
            if (hr != 0)
            {
                Log.WriteFile("TSStreamBufferPlayer9:failed to render video output pin:{0:X}", hr);
            }

            hr = _graphBuilder.Render(_pinVideo);
            if (hr != 0)
            {
                Log.WriteFile("TSStreamBufferPlayer9:failed to render audio output pin:{0:X}", hr);
            }

            #endregion

            #endregion

            _videoWin = _graphBuilder as IVideoWindow;
            if (_videoWin != null)
            {
                _videoWin.put_Visible(OABool.True);
                _videoWin.put_Owner(form.Handle);
                _videoWin.put_WindowStyle(
                    (WindowStyle)((int)WindowStyle.Child + (int)WindowStyle.ClipSiblings + (int)WindowStyle.ClipChildren));
                _videoWin.put_MessageDrain(form.Handle);
                _videoWin.SetWindowPosition(190, 250, 150, 150);
            }

            Log.WriteFile("run graph");
            _mediaCtrl = (IMediaControl)_graphBuilder;
            hr         = _mediaCtrl.Run();
            Log.WriteFile("TSStreamBufferPlayer9:running:{0:X}", hr);

            return(true);
        }
        private void SetupGraph(Control hWin)
        {
            if (theCaptureDevice == null) return;
            int hr;
            IBaseFilter ibfRenderer = null;
            ISampleGrabber sampGrabber = null;
            IBaseFilter capFilter = null;
            IPin iPinInFilter = null;
            IPin iPinOutFilter = null;
            IPin iPinInDest = null;
            // Get the graphbuilder object
            m_FilterGraph = new FilterGraph() as IFilterGraph2;
            try
            {
            // Add the video input device to the graph
            hr = m_FilterGraph.AddFilter(theCaptureDevice, "source filter");
            DsError.ThrowExceptionForHR(hr);

            if (captureDeviceCrossbar != null)
            {
            // Add the video inputs' crossbar to the graph
            hr = m_FilterGraph.AddFilter(captureDeviceCrossbar, "source crossbar");
            DsError.ThrowExceptionForHR(hr);

            //// Get crossbars's output
            IPin iPinOutCrossbar = DsFindPin.ByDirection(captureDeviceCrossbar, PinDirection.Output, 0);
            m_FilterGraph.Render(iPinOutCrossbar);
            }
            // Get input device's output
            iPinOutSource = DsFindPin.ByDirection(theCaptureDevice, PinDirection.Output, 0);
            ibfRenderer = (IBaseFilter)new VideoRendererDefault();
            // Add it to the graph
            hr = m_FilterGraph.AddFilter(ibfRenderer, "Ds.NET VideoRendererDefault");
            DsError.ThrowExceptionForHR(hr);
            iPinInDest = DsFindPin.ByDirection(ibfRenderer, PinDirection.Input, 0);
            // Connect the graph.  Many other filters automatically get added here
            hr = m_FilterGraph.Connect(iPinOutSource, iPinInDest);
            DsError.ThrowExceptionForHR(hr);
            IVideoWindow videoWindow = m_FilterGraph as IVideoWindow;
            hr = videoWindow.put_Owner(hWin.Handle);
            DsError.ThrowExceptionForHR(hr);
            hr = videoWindow.put_WindowStyle(WindowStyle.Child | WindowStyle.ClipChildren | WindowStyle.ClipSiblings);
            DsError.ThrowExceptionForHR(hr);
            hr = videoWindow.put_Visible(OABool.True);
            DsError.ThrowExceptionForHR(hr);
            Rectangle rc = hWin.ClientRectangle;
            hr = videoWindow.SetWindowPosition(0, 0, rc.Right, rc.Bottom);
            DsError.ThrowExceptionForHR(hr);
            }
            finally
            {
            if (theCaptureDevice != null)
            {
            Marshal.ReleaseComObject(theCaptureDevice);
            theCaptureDevice = null;
            }
            if (capFilter != null)
            {
            Marshal.ReleaseComObject(capFilter);
            capFilter = null;
            }
            if (sampGrabber != null)
            {
            Marshal.ReleaseComObject(sampGrabber);
            sampGrabber = null;
            }
            if (ibfRenderer != null)
            {
            Marshal.ReleaseComObject(ibfRenderer);
            ibfRenderer = null;
            }
            if (iPinInFilter != null)
            {
            Marshal.ReleaseComObject(iPinInFilter);
            iPinInFilter = null;
            }
            if (iPinOutFilter != null)
            {
            Marshal.ReleaseComObject(iPinOutFilter);
            iPinOutFilter = null;
            }
            if (iPinInDest != null)
            {
            Marshal.ReleaseComObject(iPinInDest);
            iPinInDest = null;
            }
            }
        }
Esempio n. 7
0
        protected virtual void ConnectAllOutputFiltersFrom(IBaseFilter fromFilter, IFilterGraph2 graph)
        {
            // Get the pin enumerator
            IEnumPins ppEnum;
            int hr = fromFilter.EnumPins(out ppEnum);
            DsError.ThrowExceptionForHR(hr);

            try
            {
                // Walk the pins looking for a match
                IPin[] pPins = new IPin[1];
                //22 int lFetched;
                //22 while ((ppEnum.Next(1, pPins, out lFetched) >= 0) && (lFetched == 1))
                while (ppEnum.Next(1, pPins, IntPtr.Zero) >= 0)
                {
                    // Read the direction
                    PinDirection ppindir;
                    hr = pPins[0].QueryDirection(out ppindir);
                    DsError.ThrowExceptionForHR(hr);

                    // Is it the right direction?
                    if (ppindir == PinDirection.Output)
                    {
                        if (pPins[0] != null)
                        {
                            hr = graph.Render(pPins[0]);
                            //DsError.ThrowExceptionForHR(hr);
                        }
                    }
                    Marshal.ReleaseComObject(pPins[0]);
                }
            }
            finally
            {
                Marshal.ReleaseComObject(ppEnum);
            }
        }
Esempio n. 8
0
        private void BuildGraph()
        {
            int hr = 0;

            graphBuilder = (IFilterGraph2) new FilterGraph();
            rot          = new DsROTEntry(graphBuilder);

            ICaptureGraphBuilder2 capBuilder = (ICaptureGraphBuilder2) new CaptureGraphBuilder2();

            capBuilder.SetFiltergraph(graphBuilder);

            // Get the BDA network provider specific for this given network type
            networkProvider = BDAUtils.GetNetworkProvider(networkType);

            hr = graphBuilder.AddFilter(networkProvider, "BDA Network Provider");
            DsError.ThrowExceptionForHR(hr);

            tuner = (ITuner)networkProvider;

            // Get a tuning space for this network type
            tuningSpace = BDAUtils.GetTuningSpace(networkType);

            hr = tuner.put_TuningSpace(tuningSpace);
            DsError.ThrowExceptionForHR(hr);

            // Create a tune request from this tuning space
            tuneRequest = BDAUtils.CreateTuneRequest(tuningSpace);

            // Is it okay ?
            hr = tuner.Validate(tuneRequest);
            if (hr == 0)
            {
                // Set it
                hr = tuner.put_TuneRequest(tuneRequest);
                DsError.ThrowExceptionForHR(hr);

                // found a BDA Tuner and a BDA Capture that can connect to this network provider
                BDAUtils.AddBDATunerAndDemodulatorToGraph(graphBuilder, networkProvider, out bdaTuner, out bdaCapture);

                if ((bdaTuner != null) && (bdaCapture != null))
                {
                    // Create and add the mpeg2 demux
                    mpeg2Demux = (IBaseFilter) new MPEG2Demultiplexer();

                    hr = graphBuilder.AddFilter(mpeg2Demux, "MPEG2 Demultiplexer");
                    DsError.ThrowExceptionForHR(hr);

                    // connect it to the BDA Capture
                    hr = capBuilder.RenderStream(null, null, bdaCapture, null, mpeg2Demux);
                    DsError.ThrowExceptionForHR(hr);

                    // Add the two mpeg2 transport stream helper filters
                    BDAUtils.AddTransportStreamFiltersToGraph(graphBuilder, out bdaTIF, out bdaSecTab);

                    if ((bdaTIF != null) && (bdaSecTab != null))
                    {
                        // Render all the output pins of the demux (missing filters are added)
                        for (int i = 0; i < 5; i++)
                        {
                            IPin pin = DsFindPin.ByDirection(mpeg2Demux, PinDirection.Output, i);

                            hr = graphBuilder.Render(pin);
                            Marshal.ReleaseComObject(pin);
                        }
                    }
                }
            }
        }
Esempio n. 9
0
    public bool Play(string fileName, Form form)
    {
      _form = form;
      Log.WriteFile("play:{0}", fileName);
      _graphBuilder = (IFilterGraph2)new FilterGraph();
      _rotEntry = new DsROTEntry(_graphBuilder);

      TsReader reader = new TsReader();
      _tsReader = (IBaseFilter)reader;
      Log.Info("TSReaderPlayer:add TsReader to graph");
      _graphBuilder.AddFilter(_tsReader, "TsReader");

      #region load file in TsReader

      Log.WriteFile("load file in Ts");
      IFileSourceFilter interfaceFile = (IFileSourceFilter)_tsReader;
      if (interfaceFile == null)
      {
        Log.WriteFile("TSReaderPlayer:Failed to get IFileSourceFilter");
        return false;
      }
      int hr = interfaceFile.Load(fileName, null);

      if (hr != 0)
      {
        Log.WriteFile("TSReaderPlayer:Failed to load file");
        return false;
      }

      #endregion

      #region render pin

      Log.Info("TSReaderPlayer:render TsReader outputs");
      IEnumPins enumPins;
      _tsReader.EnumPins(out enumPins);
      IPin[] pins = new IPin[2];
      int fetched;
      while (enumPins.Next(1, pins, out fetched) == 0)
      {
        if (fetched != 1) break;
        PinDirection direction;
        pins[0].QueryDirection(out direction);
        if (direction == PinDirection.Input)
        {
          Release.ComObject(pins[0]);
          continue;
        }
        _graphBuilder.Render(pins[0]);
        Release.ComObject(pins[0]);
      }
      Release.ComObject(enumPins);

      #endregion

      _videoWin = _graphBuilder as IVideoWindow;
      if (_videoWin != null)
      {
        _videoWin.put_Visible(OABool.True);
        _videoWin.put_Owner(form.Handle);
        _videoWin.put_WindowStyle(
          (WindowStyle)((int)WindowStyle.Child + (int)WindowStyle.ClipSiblings + (int)WindowStyle.ClipChildren));
        _videoWin.put_MessageDrain(form.Handle);

        _videoWin.SetWindowPosition(form.ClientRectangle.X, form.ClientRectangle.Y, form.ClientRectangle.Width,
                                    form.ClientRectangle.Height);
      }

      Log.WriteFile("run graph");
      _mediaCtrl = (IMediaControl)_graphBuilder;
      hr = _mediaCtrl.Run();
      Log.WriteFile("TSReaderPlayer:running:{0:X}", hr);

      return true;
    }
Esempio n. 10
0
        private void SetupGraph(Control hWin)
        {
            if (theCaptureDevice == null) return;

            int hr;

            IBaseFilter ibfRenderer = null;
            ISampleGrabber sampGrabber = null;
            IBaseFilter capFilter = null;
            IPin iPinInFilter = null;
            IPin iPinOutFilter = null;
            IPin iPinInDest = null;

            // Get the graphbuilder object
            m_FilterGraph = new FilterGraph() as IFilterGraph2;

            //#if DEBUG
            //            m_rot = new DsROTEntry(m_FilterGraph);
            //#endif

            try
            {
                // Add the video input device to the graph
                hr = m_FilterGraph.AddFilter(theCaptureDevice, "source filter");
                DsError.ThrowExceptionForHR(hr);

                if (captureDeviceCrossbar != null)
                {
                    // Add the video inputs' crossbar to the graph
                    hr = m_FilterGraph.AddFilter(captureDeviceCrossbar, "source crossbar");
                    DsError.ThrowExceptionForHR(hr);

                    //// Get crossbars's output
                    IPin iPinOutCrossbar = DsFindPin.ByDirection(captureDeviceCrossbar, PinDirection.Output, 0);
                    m_FilterGraph.Render(iPinOutCrossbar);
                }

                // Get input device's output
                iPinOutSource = DsFindPin.ByDirection(theCaptureDevice, PinDirection.Output, 0);
                //DisplayPropertyPageForCapturePin(IntPtr.Zero);
                //m_FilterGraph.Render(iPinOutSource);

                //// Get the SampleGrabber interface
                //sampGrabber = new SampleGrabber() as ISampleGrabber;
                //IBaseFilter baseGrabFlt = sampGrabber as IBaseFilter;
                //ConfigureSampleGrabber(sampGrabber);

                //iPinInFilter = DsFindPin.ByDirection(baseGrabFlt, PinDirection.Input, 0);
                //iPinOutFilter = DsFindPin.ByDirection(baseGrabFlt, PinDirection.Output, 0);

                //// Add the frame grabber to the graph
                //hr = m_FilterGraph.AddFilter(baseGrabFlt, "Ds.NET Grabber");
                //DsError.ThrowExceptionForHR(hr);

                //hr = m_FilterGraph.Connect(iPinOutSource, iPinInFilter);
                //DsError.ThrowExceptionForHR(hr);

                //m_FilterGraph.Render(iPinOutSource);

                ibfRenderer = (IBaseFilter)new VideoRendererDefault();

                // Add it to the graph
                hr = m_FilterGraph.AddFilter(ibfRenderer, "Ds.NET VideoRendererDefault");
                DsError.ThrowExceptionForHR(hr);
                iPinInDest = DsFindPin.ByDirection(ibfRenderer, PinDirection.Input, 0);

                // Connect the graph.  Many other filters automatically get added here
                hr = m_FilterGraph.Connect(iPinOutSource, iPinInDest);
                DsError.ThrowExceptionForHR(hr);

                IVideoWindow videoWindow = m_FilterGraph as IVideoWindow;
                hr = videoWindow.put_Owner(hWin.Handle);
                DsError.ThrowExceptionForHR(hr);

                hr = videoWindow.put_WindowStyle(WindowStyle.Child | WindowStyle.ClipChildren | WindowStyle.ClipSiblings);
                DsError.ThrowExceptionForHR(hr);

                hr = videoWindow.put_Visible(OABool.True);
                DsError.ThrowExceptionForHR(hr);

                Rectangle rc = hWin.ClientRectangle;
                hr = videoWindow.SetWindowPosition(0, 0, rc.Right, rc.Bottom);
                DsError.ThrowExceptionForHR(hr);

                //// Add the video source
                ////hr = m_FilterGraph.AddSourceFilter(FileName, "Ds.NET FileFilter", out capFilter);
                ////DsError.ThrowExceptionForHR(hr);

                //// Hopefully this will be the video pin
                //IPin iPinOutSource = DsFindPin.ByDirection(theCaptureDevice, PinDirection.Output, 0);

                //IBaseFilter baseGrabFlt = sampGrabber as IBaseFilter;
                //ConfigureSampleGrabber(sampGrabber);

                //iPinInFilter = DsFindPin.ByDirection(baseGrabFlt, PinDirection.Input, 0);
                //iPinOutFilter = DsFindPin.ByDirection(baseGrabFlt, PinDirection.Output, 0);

                //// Add the frame grabber to the graph
                //hr = m_FilterGraph.AddFilter(baseGrabFlt, "Ds.NET Grabber");
                //DsError.ThrowExceptionForHR(hr);

                //hr = m_FilterGraph.Connect(iPinOutSource, iPinInFilter);
                //DsError.ThrowExceptionForHR(hr);

                //// Get the default video renderer
                //ibfRenderer = (IBaseFilter)new VideoRendererDefault();

                //// Add it to the graph
                //hr = m_FilterGraph.AddFilter(ibfRenderer, "Ds.NET VideoRendererDefault");
                //DsError.ThrowExceptionForHR(hr);
                //iPinInDest = DsFindPin.ByDirection(ibfRenderer, PinDirection.Input, 0);

                //// Connect the graph.  Many other filters automatically get added here
                //hr = m_FilterGraph.Connect(iPinOutSource, iPinInDest);
                //DsError.ThrowExceptionForHR(hr);

                //SaveSizeInfo(sampGrabber);
            }
            finally
            {
                if (theCaptureDevice != null)
                {
                    Marshal.ReleaseComObject(theCaptureDevice);
                    theCaptureDevice = null;
                }
                if (capFilter != null)
                {
                    Marshal.ReleaseComObject(capFilter);
                    capFilter = null;
                }
                if (sampGrabber != null)
                {
                    Marshal.ReleaseComObject(sampGrabber);
                    sampGrabber = null;
                }
                if (ibfRenderer != null)
                {
                    Marshal.ReleaseComObject(ibfRenderer);
                    ibfRenderer = null;
                }
                if (iPinInFilter != null)
                {
                    Marshal.ReleaseComObject(iPinInFilter);
                    iPinInFilter = null;
                }
                if (iPinOutFilter != null)
                {
                    Marshal.ReleaseComObject(iPinOutFilter);
                    iPinOutFilter = null;
                }
                if (iPinInDest != null)
                {
                    Marshal.ReleaseComObject(iPinInDest);
                    iPinInDest = null;
                }
            }
        }
Esempio n. 11
0
        public bool Play(string fileName, Form form)
        {
            _form = form;
            Log.WriteFile("play:{0}", fileName);
            _graphBuilder = (IFilterGraph2) new FilterGraph();
            _rotEntry     = new DsROTEntry(_graphBuilder);

            TsReader reader = new TsReader();

            _tsReader = (IBaseFilter)reader;
            Log.Info("TSReaderPlayer:add TsReader to graph");
            _graphBuilder.AddFilter(_tsReader, "TsReader");

            #region load file in TsReader

            Log.WriteFile("load file in Ts");
            IFileSourceFilter interfaceFile = (IFileSourceFilter)_tsReader;
            if (interfaceFile == null)
            {
                Log.WriteFile("TSReaderPlayer:Failed to get IFileSourceFilter");
                return(false);
            }
            int hr = interfaceFile.Load(fileName, null);

            if (hr != 0)
            {
                Log.WriteFile("TSReaderPlayer:Failed to load file");
                return(false);
            }

            #endregion

            #region render pin

            Log.Info("TSReaderPlayer:render TsReader outputs");
            IEnumPins enumPins;
            _tsReader.EnumPins(out enumPins);
            IPin[] pins = new IPin[2];
            int    fetched;
            while (enumPins.Next(1, pins, out fetched) == 0)
            {
                if (fetched != 1)
                {
                    break;
                }
                PinDirection direction;
                pins[0].QueryDirection(out direction);
                if (direction == PinDirection.Input)
                {
                    Release.ComObject(pins[0]);
                    continue;
                }
                _graphBuilder.Render(pins[0]);
                Release.ComObject(pins[0]);
            }
            Release.ComObject(enumPins);

            #endregion

            _videoWin = _graphBuilder as IVideoWindow;
            if (_videoWin != null)
            {
                _videoWin.put_Visible(OABool.True);
                _videoWin.put_Owner(form.Handle);
                _videoWin.put_WindowStyle(
                    (WindowStyle)((int)WindowStyle.Child + (int)WindowStyle.ClipSiblings + (int)WindowStyle.ClipChildren));
                _videoWin.put_MessageDrain(form.Handle);

                _videoWin.SetWindowPosition(form.ClientRectangle.X, form.ClientRectangle.Y, form.ClientRectangle.Width,
                                            form.ClientRectangle.Height);
            }

            Log.WriteFile("run graph");
            _mediaCtrl = (IMediaControl)_graphBuilder;
            hr         = _mediaCtrl.Run();
            Log.WriteFile("TSReaderPlayer:running:{0:X}", hr);

            return(true);
        }
Esempio n. 12
0
        public bool Play(string fileName, Control parent, out string ErrorOrSplitter)
        {
            ErrorOrSplitter = "";
            int hr;

            _parentControl = parent;

            _graphBuilder = (IFilterGraph2) new FilterGraph();
            _rotEntry     = new DsROTEntry(_graphBuilder);

            // add the video renderer (evr does not seem to work here)
            IBaseFilter vmr9Renderer = DirectShowUtil.AddFilterToGraph(_graphBuilder, "Video Mixing Renderer 9");

            ((IVMRAspectRatioControl9)vmr9Renderer).SetAspectRatioMode(VMRAspectRatioMode.LetterBox);
            DirectShowUtil.ReleaseComObject(vmr9Renderer, 2000);

            // add the audio renderer
            IBaseFilter audioRenderer = DirectShowUtil.AddAudioRendererToGraph(_graphBuilder, MPSettings.Instance.GetValueAsString("movieplayer", "audiorenderer", "Default DirectSound Device"), false);

            DirectShowUtil.ReleaseComObject(audioRenderer, 2000);

            // add the source filter
            string sourceFilterName = OnlineVideos.MediaPortal1.Player.OnlineVideosPlayer.GetSourceFilterName(fileName);

            if (string.IsNullOrEmpty(sourceFilterName))
            {
                return(false);
            }
            IBaseFilter sourceFilter = null;

            try
            {
                sourceFilter = DirectShowUtil.AddFilterToGraph(_graphBuilder, sourceFilterName);
            }
            catch (Exception ex)
            {
                ErrorOrSplitter = ex.Message;
                return(false);
            }

            hr = ((IFileSourceFilter)sourceFilter).Load(fileName, null);

            if (hr != 0)
            {
                ErrorOrSplitter = DirectShowLib.DsError.GetErrorText(hr);
                DirectShowUtil.ReleaseComObject(sourceFilter, 2000);
                return(false);
            }

            // wait for our filter to buffer before rendering the pins
            OnlineVideos.MPUrlSourceFilter.IFilterState filterState = sourceFilter as OnlineVideos.MPUrlSourceFilter.IFilterState;

            if (filterState != null)
            {
                bool ready = false;

                while ((!ready) && (hr == 0))
                {
                    hr = filterState.IsFilterReadyToConnectPins(out ready);

                    System.Threading.Thread.Sleep(25);
                }
            }

            if (hr != 0)
            {
                ErrorOrSplitter = DirectShowLib.DsError.GetErrorText(hr);
                DirectShowUtil.ReleaseComObject(sourceFilter, 2000);
                return(false);
            }

            OnlineVideos.MediaPortal1.Player.OnlineVideosPlayer.AddPreferredFilters(_graphBuilder, sourceFilter);

            // try to connect the filters
            int       numConnected = 0;
            IEnumPins pinEnum;

            hr = sourceFilter.EnumPins(out pinEnum);
            if ((hr == 0) && (pinEnum != null))
            {
                pinEnum.Reset();
                IPin[] pins = new IPin[1];
                int    iFetched;
                int    iPinNo = 0;
                do
                {
                    iPinNo++;
                    hr = pinEnum.Next(1, pins, out iFetched);
                    if (hr == 0)
                    {
                        if (iFetched == 1 && pins[0] != null)
                        {
                            PinDirection pinDir;
                            pins[0].QueryDirection(out pinDir);
                            if (pinDir == PinDirection.Output)
                            {
                                hr = _graphBuilder.Render(pins[0]);
                                if (hr == 0)
                                {
                                    numConnected++;
                                    IPin connectedPin;
                                    if (pins[0].ConnectedTo(out connectedPin) == 0 && connectedPin != null)
                                    {
                                        PinInfo connectedPinInfo;
                                        connectedPin.QueryPinInfo(out connectedPinInfo);
                                        FilterInfo connectedFilterInfo;
                                        connectedPinInfo.filter.QueryFilterInfo(out connectedFilterInfo);
                                        DirectShowUtil.ReleaseComObject(connectedPin, 2000);
                                        IBaseFilter connectedFilter;
                                        if (connectedFilterInfo.pGraph.FindFilterByName(connectedFilterInfo.achName, out connectedFilter) == 0 && connectedFilter != null)
                                        {
                                            var codecInfo = GetCodecInfo(connectedFilter, connectedFilterInfo.achName);
                                            if (codecInfo != null)
                                            {
                                                if (string.IsNullOrEmpty(ErrorOrSplitter))
                                                {
                                                    ErrorOrSplitter = codecInfo.ToString();
                                                }
                                                else
                                                {
                                                    ErrorOrSplitter += ", " + codecInfo.ToString();
                                                }
                                            }
                                            DirectShowUtil.ReleaseComObject(connectedFilter);
                                        }
                                    }
                                }
                            }
                            DirectShowUtil.ReleaseComObject(pins[0], 2000);
                        }
                    }
                } while (iFetched == 1);
            }
            DirectShowUtil.ReleaseComObject(pinEnum, 2000);

            if (numConnected > 0)
            {
                _videoWin = _graphBuilder as IVideoWindow;
                if (_videoWin != null)
                {
                    _videoWin.put_Owner(_parentControl.Handle);
                    _videoWin.put_WindowStyle((WindowStyle)((int)WindowStyle.Child + (int)WindowStyle.ClipSiblings + (int)WindowStyle.ClipChildren));
                    _videoWin.SetWindowPosition(_parentControl.ClientRectangle.X, _parentControl.ClientRectangle.Y, _parentControl.ClientRectangle.Width, _parentControl.ClientRectangle.Height);
                    _videoWin.put_Visible(OABool.True);
                }

                _mediaCtrl = (IMediaControl)_graphBuilder;
                hr         = _mediaCtrl.Run();

                mediaEvents = (IMediaEventEx)_graphBuilder;
                // Have the graph signal event via window callbacks for performance
                mediaEvents.SetNotifyWindow(_parentControl.FindForm().Handle, WMGraphNotify, IntPtr.Zero);

                _parentControl.SizeChanged += _parentControl_SizeChanged;
                return(true);
            }
            else
            {
                ErrorOrSplitter = string.Format("Could not render output pins of {0}", sourceFilterName);
                DirectShowUtil.ReleaseComObject(sourceFilter, 2000);
                Stop();
                return(false);
            }
        }
Esempio n. 13
0
    public bool Play(string fileName, Form form)
    {
      fileName += ".tsbuffer";
      Log.WriteFile("play:{0}", fileName);
      _graphBuilder = (IFilterGraph2)new FilterGraph();
      _rotEntry = new DsROTEntry(_graphBuilder);


      Log.WriteFile("add tsfilesource");
      _tsFileSource = new TsFileSource();
      _graphBuilder.AddFilter((IBaseFilter)_tsFileSource, "TsFileSource");

      #region add mpeg-2 demux filter

      Log.WriteFile("add mpeg-2 demux");
      MPEG2Demultiplexer demux = new MPEG2Demultiplexer();
      _mpegDemux = (IBaseFilter)demux;
      int hr = _graphBuilder.AddFilter(_mpegDemux, "MPEG-2 Demultiplexer");

      #endregion

      #region create mpeg2 demux pins

      Log.WriteFile("create mpeg-2 demux pins");
      //create mpeg-2 demux output pins
      IMpeg2Demultiplexer demuxer = _mpegDemux as IMpeg2Demultiplexer;


      if (demuxer != null)
        hr = demuxer.CreateOutputPin(GetAudioMpg2Media(), "Audio", out _pinAudio);
      if (hr != 0)
      {
        Log.WriteFile("unable to create audio pin");
        return false;
      }
      if (demuxer != null)
        hr = demuxer.CreateOutputPin(GetVideoMpg2Media(), "Video", out _pinVideo);
      if (hr != 0)
      {
        Log.WriteFile("unable to create video pin");
        return false;
      }

      #endregion

      #region load file in tsfilesource

      Log.WriteFile("load file in tsfilesource");
      IFileSourceFilter interfaceFile = (IFileSourceFilter)_tsFileSource;
      if (interfaceFile == null)
      {
        Log.WriteFile("TSStreamBufferPlayer9:Failed to get IFileSourceFilter");
        return false;
      }

      AMMediaType mpeg2ProgramStream = new AMMediaType();
      mpeg2ProgramStream.majorType = MediaType.Stream;
      mpeg2ProgramStream.subType = MediaSubType.Mpeg2Program;

      mpeg2ProgramStream.unkPtr = IntPtr.Zero;
      mpeg2ProgramStream.sampleSize = 0;
      mpeg2ProgramStream.temporalCompression = false;
      mpeg2ProgramStream.fixedSizeSamples = true;
      mpeg2ProgramStream.formatType = FormatType.None;
      mpeg2ProgramStream.formatSize = 0;
      mpeg2ProgramStream.formatPtr = IntPtr.Zero;
      hr = interfaceFile.Load(fileName, mpeg2ProgramStream);

      if (hr != 0)
      {
        Log.WriteFile("TSStreamBufferPlayer9:Failed to load file");
        return false;
      }

      #region connect tsfilesource->demux

      Log.WriteFile("connect tsfilesource->demux");
      Log.WriteFile("TSStreamBufferPlayer9:connect tsfilesource->mpeg2 demux");
      IPin pinTsOut = DsFindPin.ByDirection((IBaseFilter)_tsFileSource, PinDirection.Output, 0);
      if (pinTsOut == null)
      {
        Log.WriteFile("TSStreamBufferPlayer9:failed to find output pin of tsfilesource");
        return false;
      }
      IPin pinDemuxIn = DsFindPin.ByDirection(_mpegDemux, PinDirection.Input, 0);
      if (pinDemuxIn == null)
      {
        Log.WriteFile("TSStreamBufferPlayer9:failed to find output pin of tsfilesource");
        return false;
      }

      hr = _graphBuilder.Connect(pinTsOut, pinDemuxIn);
      if (hr != 0)
      {
        Log.WriteFile("TSStreamBufferPlayer9:failed to connect tsfilesource->mpeg2 demux:{0:X}", hr);
        return false;
      }
      Release.ComObject(pinTsOut);
      Release.ComObject(pinDemuxIn);

      #endregion

      #region map demux pids

      Log.WriteFile("map mpeg2 pids");
      IMPEG2StreamIdMap pStreamId = (IMPEG2StreamIdMap)_pinVideo;
      hr = pStreamId.MapStreamId(0xe0, MPEG2Program.ElementaryStream, 0, 0);
      if (hr != 0)
      {
        Log.WriteFile("TSStreamBufferPlayer9: failed to map pid 0xe0->video pin");
        return false;
      }
      pStreamId = (IMPEG2StreamIdMap)_pinAudio;
      hr = pStreamId.MapStreamId(0xc0, MPEG2Program.ElementaryStream, 0, 0);
      if (hr != 0)
      {
        Log.WriteFile("TSStreamBufferPlayer9: failed  to map pid 0xc0->audio pin");
        return false;
      }

      #endregion

      #region render demux audio/video pins

      Log.WriteFile("render pins");
      hr = _graphBuilder.Render(_pinAudio);
      if (hr != 0)
      {
        Log.WriteFile("TSStreamBufferPlayer9:failed to render video output pin:{0:X}", hr);
      }

      hr = _graphBuilder.Render(_pinVideo);
      if (hr != 0)
      {
        Log.WriteFile("TSStreamBufferPlayer9:failed to render audio output pin:{0:X}", hr);
      }

      #endregion

      #endregion

      _videoWin = _graphBuilder as IVideoWindow;
      if (_videoWin != null)
      {
        _videoWin.put_Visible(OABool.True);
        _videoWin.put_Owner(form.Handle);
        _videoWin.put_WindowStyle(
          (WindowStyle)((int)WindowStyle.Child + (int)WindowStyle.ClipSiblings + (int)WindowStyle.ClipChildren));
        _videoWin.put_MessageDrain(form.Handle);
        _videoWin.SetWindowPosition(190, 250, 150, 150);
      }

      Log.WriteFile("run graph");
      _mediaCtrl = (IMediaControl)_graphBuilder;
      hr = _mediaCtrl.Run();
      Log.WriteFile("TSStreamBufferPlayer9:running:{0:X}", hr);

      return true;
    }
        public bool Play(string fileName, Control parent, out string ErrorOrSplitter)
        {
            ErrorOrSplitter = "";
            int hr;
            _parentControl = parent;

            _graphBuilder = (IFilterGraph2)new FilterGraph();
            _rotEntry = new DsROTEntry(_graphBuilder);

            // add the video renderer (evr does not seem to work here)
            IBaseFilter vmr9Renderer = DirectShowUtil.AddFilterToGraph(_graphBuilder, "Video Mixing Renderer 9");
            ((IVMRAspectRatioControl9)vmr9Renderer).SetAspectRatioMode(VMRAspectRatioMode.LetterBox);
            DirectShowUtil.ReleaseComObject(vmr9Renderer, 2000);

            // add the audio renderer
            IBaseFilter audioRenderer = DirectShowUtil.AddAudioRendererToGraph(_graphBuilder, MPSettings.Instance.GetValueAsString("movieplayer", "audiorenderer", "Default DirectSound Device"), false);
            DirectShowUtil.ReleaseComObject(audioRenderer, 2000);

            // add the source filter
            string sourceFilterName = OnlineVideos.MediaPortal1.Player.OnlineVideosPlayer.GetSourceFilterName(fileName);
			if (string.IsNullOrEmpty(sourceFilterName)) return false;
            IBaseFilter sourceFilter = null;
            try
            {
                sourceFilter = DirectShowUtil.AddFilterToGraph(_graphBuilder, sourceFilterName);
            }
            catch (Exception ex)
            {
                ErrorOrSplitter = ex.Message;
                return false;
            }

            hr = ((IFileSourceFilter)sourceFilter).Load(fileName, null);

            if (hr != 0)
            {
                ErrorOrSplitter = DirectShowLib.DsError.GetErrorText(hr);
                DirectShowUtil.ReleaseComObject(sourceFilter, 2000);
                return false;
            }

            // wait for our filter to buffer before rendering the pins
            OnlineVideos.MPUrlSourceFilter.IFilterState filterState = sourceFilter as OnlineVideos.MPUrlSourceFilter.IFilterState;

            if (filterState != null)
            {
                bool ready = false;

                while ((!ready) && (hr == 0))
                {
                    hr = filterState.IsFilterReadyToConnectPins(out ready);

                    System.Threading.Thread.Sleep(25);
                }
            }

            if (hr != 0)
            {
                ErrorOrSplitter = DirectShowLib.DsError.GetErrorText(hr);
                DirectShowUtil.ReleaseComObject(sourceFilter, 2000);
                return false;
            }

            OnlineVideos.MediaPortal1.Player.OnlineVideosPlayer.AddPreferredFilters(_graphBuilder, sourceFilter);

            // try to connect the filters
            int numConnected = 0;
            IEnumPins pinEnum;
            hr = sourceFilter.EnumPins(out pinEnum);
            if ((hr == 0) && (pinEnum != null))
            {
                pinEnum.Reset();
                IPin[] pins = new IPin[1];
                int iFetched;
                int iPinNo = 0;
                do
                {
                    iPinNo++;
                    hr = pinEnum.Next(1, pins, out iFetched);
                    if (hr == 0)
                    {
                        if (iFetched == 1 && pins[0] != null)
                        {
                            PinDirection pinDir;
                            pins[0].QueryDirection(out pinDir);
                            if (pinDir == PinDirection.Output)
                            {
                                hr = _graphBuilder.Render(pins[0]);
								if (hr == 0)
								{
									numConnected++;
									IPin connectedPin;
									if (pins[0].ConnectedTo(out connectedPin) == 0 && connectedPin != null)
									{
										PinInfo connectedPinInfo;
										connectedPin.QueryPinInfo(out connectedPinInfo);
										FilterInfo connectedFilterInfo;
										connectedPinInfo.filter.QueryFilterInfo(out connectedFilterInfo);
										DirectShowUtil.ReleaseComObject(connectedPin, 2000);
										IBaseFilter connectedFilter;
										if (connectedFilterInfo.pGraph.FindFilterByName(connectedFilterInfo.achName, out connectedFilter) == 0 && connectedFilter != null)
										{
											var codecInfo = GetCodecInfo(connectedFilter, connectedFilterInfo.achName);
											if (codecInfo != null)
											{
												if (string.IsNullOrEmpty(ErrorOrSplitter)) ErrorOrSplitter = codecInfo.ToString();
												else ErrorOrSplitter += ", " + codecInfo.ToString();
											}
											DirectShowUtil.ReleaseComObject(connectedFilter);
										}
									}
								}
                            }
                            DirectShowUtil.ReleaseComObject(pins[0], 2000);
                        }
                    }
                } while (iFetched == 1);
            }
            DirectShowUtil.ReleaseComObject(pinEnum, 2000);

            if (numConnected > 0)
            {
                _videoWin = _graphBuilder as IVideoWindow;
                if (_videoWin != null)
                {
                    _videoWin.put_Owner(_parentControl.Handle);
                    _videoWin.put_WindowStyle((WindowStyle)((int)WindowStyle.Child + (int)WindowStyle.ClipSiblings + (int)WindowStyle.ClipChildren));
                    _videoWin.SetWindowPosition(_parentControl.ClientRectangle.X, _parentControl.ClientRectangle.Y, _parentControl.ClientRectangle.Width, _parentControl.ClientRectangle.Height);
                    _videoWin.put_Visible(OABool.True);
                }

                _mediaCtrl = (IMediaControl)_graphBuilder;
                hr = _mediaCtrl.Run();

                mediaEvents = (IMediaEventEx)_graphBuilder;
                // Have the graph signal event via window callbacks for performance
                mediaEvents.SetNotifyWindow(_parentControl.FindForm().Handle, WMGraphNotify, IntPtr.Zero);

                _parentControl.SizeChanged += _parentControl_SizeChanged;
                return true;
            }
            else
            {
                ErrorOrSplitter = string.Format("Could not render output pins of {0}", sourceFilterName);
                DirectShowUtil.ReleaseComObject(sourceFilter, 2000);
                Stop();
                return false;
            }
        }
        public bool Initialize(string n, string p, string t)
        {
            int hr = 0;

            path = p;
            name = n;
            textureName = t;
            if (textureName == null)
            {
                textureName = Manager.TextureName(this);
            }
            log.InfoFormat("DirectShowCodec[{0}] Initialize: {1} ({2}), texture '{3}'", ID(), name, path, textureName);

            // check for DirectX-ness
            if (!(Root.Instance.RenderSystem is Axiom.RenderSystems.DirectX9.D3D9RenderSystem))
            {
                throw new Exception("DirectShow movie codec is DirectX only");
            }
            if ((Root.Instance.RenderSystem as D3D9RenderSystem).PrimaryWindow == null)
            {
                throw new Exception("Initializing before primary window has been created");
            }
            D3DRenderWindow rw = (Root.Instance.RenderSystem as D3D9RenderSystem).PrimaryWindow;

            // Create a DirectShow FilterGraph
            graphBuilder = (IFilterGraph2)new FilterGraph();

            // Add it in ROT for debug purpose
            rot = new DsROTEntry(graphBuilder);

            // Create a VMR9 object
            vmr9 = (IBaseFilter)new VideoMixingRenderer9();

            IVMRFilterConfig9 filterConfig = (IVMRFilterConfig9)vmr9;

            // We want the Renderless mode!
            hr = filterConfig.SetRenderingMode(VMR9Mode.Renderless);
            DsError.ThrowExceptionForHR(hr);

            // One stream is enough for this sample
            hr = filterConfig.SetNumberOfStreams(1);
            DsError.ThrowExceptionForHR(hr);

            // Create the Allocator / Presenter object
            Device device = (Device)rw.GetCustomAttribute("D3DDEVICE");
            allocator = new DirectShowAllocator(device, ID(), TextureName());

            IVMRSurfaceAllocatorNotify9 vmrSurfAllocNotify = (IVMRSurfaceAllocatorNotify9)vmr9;

            // Notify the VMR9 filter about our allocator
            hr = vmrSurfAllocNotify.AdviseSurfaceAllocator(IntPtr.Zero, allocator);
            DsError.ThrowExceptionForHR(hr);

            // Notify our allocator about the VMR9 filter
            hr = allocator.AdviseNotify(vmrSurfAllocNotify);
            DsError.ThrowExceptionForHR(hr);

            IVMRMixerControl9 mixerControl = (IVMRMixerControl9)vmr9;

            // Select the mixer mode : YUV or RGB
            hr = mixerControl.SetMixingPrefs(VMR9MixerPrefs.RenderTargetYUV | VMR9MixerPrefs.NoDecimation | VMR9MixerPrefs.ARAdjustXorY | VMR9MixerPrefs.BiLinearFiltering);
            DsError.ThrowExceptionForHR(hr);

            // Add the VMR-9 filter to the graph
            hr = graphBuilder.AddFilter(vmr9, "Video Mixing Renderer 9");
            DsError.ThrowExceptionForHR(hr);

            bool shouldStream = false;
            string url = path;
            #if false
            int maxRedirects = DirectShowWebReader.MAX_REDIRECTS;
            DirectShowWebReader reader = null;
            while (maxRedirects != 0)
            {
                if (url.StartsWith("http://") || url.StartsWith("https://"))
                {
                    try
                    {
                        // Start reading the data
                        reader = new DirectShowWebReader();
                        hr = reader.Load(url);
                        DsError.ThrowExceptionForHR(hr);
                        shouldStream = true;
                        break;
                    }
                    catch (DirectShowWebReader.RedirectException e)
                    {
                        log.Info("Redirect to (" + e.url + ")");
                        url = e.url;
                        maxRedirects--;
                        reader = null;
                    }
                }
                else
                {
                    shouldStream = false;
                    reader = null;
                    break;
                }
            }
            if (maxRedirects == 0)
            {
                log.Warn("Stopped redirection after " + DirectShowWebReader.MAX_REDIRECTS + " attempts.");
            }
            #else
            shouldStream = DirectShowCodec.ShouldStream(url);
            #endif
            if(shouldStream)
            {
            #if false
                // Add our source filter to the graph
                DirectShowStreamer strm = new DirectShowStreamer(reader);
                hr = graphBuilder.AddFilter(strm, DirectShowStreamer.FILTER_NAME);
                DsError.ThrowExceptionForHR(hr);
                streamer = strm;
                streamer.SetType(reader.MajorType, reader.MinorType);

                // Render the file
                hr = graphBuilder.Render(strm.GetOutputPin());
                DsError.ThrowExceptionForHR(hr);
            #else
                WMAsfReader asf = new WMAsfReader();
                IBaseFilter ibf = (asf as IBaseFilter);
                IFileSourceFilter ifs = (asf as IFileSourceFilter);
                hr = ifs.Load(url, IntPtr.Zero);
                DsError.ThrowExceptionForHR(hr);

                // XXXMLM - how to set buffer time?
                // IWMStreamConfig2 on the output pins?

                hr = graphBuilder.AddFilter(ibf, "WM ASF Reader");
                DsError.ThrowExceptionForHR(hr);

                IEnumPins ie;
                IPin[] pins = new IPin[1];
                hr = ibf.EnumPins(out ie);
                int fetched = 0;
                while (hr == 0)
                {
                    hr = ie.Next(1, pins, out fetched);
                    if (fetched != 0)
                    {
                        hr = graphBuilder.Render(pins[0]);
                        DsError.ThrowExceptionForHR(hr);
                    }
                }
            #endif
            }
            else
            {
                // Render the file
                hr = graphBuilder.RenderFile(url, null);
                DsError.ThrowExceptionForHR(hr);
            }

            #if true
            // Run the graph
            hr = (graphBuilder as IMediaControl).Run();
            DsError.ThrowExceptionForHR(hr);

            // Wait for ready
            hr = (graphBuilder as IMediaControl).Pause();
            DsError.ThrowExceptionForHR(hr);

            ps = PLAY_STATE.BUFFERING;
            #endif
            return true;
        }
Esempio n. 16
0
        /// <summary>;
        /// Starts grabbing images from the capture device
        /// </summary>
        public virtual void Start()
        {
            if (_captureTask != null)
            {
                Stop();
            }

            _captureTask = new Task(() =>
            {
                // Create new grabber
                _capGrabber = new CapGrabber();
                _capGrabber.PropertyChanged += capGrabber_PropertyChanged;
                _capGrabber.NewFrameArrived += capGrabber_NewFrameArrived;
                _stopSignal = new ManualResetEvent(false);

                _graph        = Activator.CreateInstance(Type.GetTypeFromCLSID(FilterGraph)) as IFilterGraph2;
                _sourceObject = FilterInfo.CreateFilter(_monikerString);

                var outputPin = _sourceObject.GetPin(PinCategory.Capture, 0);
                SelectWebcamResolution(outputPin);

                _grabber       = Activator.CreateInstance(Type.GetTypeFromCLSID(SampleGrabber)) as ISampleGrabber;
                _grabberObject = _grabber as IBaseFilter;

                if (_graph == null)
                {
                    return;
                }
                ;

                _graph.AddFilter(_sourceObject, "source");
                _graph.AddFilter(_grabberObject, "grabber");
                using (var mediaType = new AMMediaType())
                {
                    mediaType.MajorType = MediaTypes.Video;
                    mediaType.SubType   = MediaSubTypes.RGB32;
                    if (_grabber != null)
                    {
                        _grabber.SetMediaType(mediaType);


                        var inputPin = _grabberObject.GetPin(PinDirection.Input, 0);
                        if (_graph.Connect(outputPin, inputPin) >= 0)
                        {
                            if (_grabber.GetConnectedMediaType(mediaType) == 0)
                            {
                                var header         = (VideoInfoHeader)Marshal.PtrToStructure(mediaType.FormatPtr, typeof(VideoInfoHeader));
                                _capGrabber.Width  = header.BmiHeader.Width;
                                _capGrabber.Height = header.BmiHeader.Height;
                            }
                        }
                        _graph.Render(_grabberObject.GetPin(PinDirection.Output, 0));
                        _grabber.SetBufferSamples(false);
                        _grabber.SetOneShot(false);
                        _grabber.SetCallback(_capGrabber, 1);
                    }

                    // Get the video window
                    var wnd = (IVideoWindow)_graph;
                    wnd.put_AutoShow(false);

                    // Create the control and run
                    _control = (IMediaControl)_graph;

                    _control.Run();

                    // Wait for the stop signal
                    _stopSignal.WaitOne();
                    Cleanup();
                }
            });
            _captureTask.Start();
        }
Esempio n. 17
0
        public void convert(object obj)
        {
            string[] pair       = obj as string[];
            string   srcfile    = pair[0];
            string   targetfile = pair[1];
            int      hr;

            ibfSrcFile = (IBaseFilter) new AsyncReader();
            hr         = gb.AddFilter(ibfSrcFile, "Reader");
            DsError.ThrowExceptionForHR(hr);
            IFileSourceFilter ifileSource = (IFileSourceFilter)ibfSrcFile;

            hr = ifileSource.Load(srcfile, null);
            DsError.ThrowExceptionForHR(hr);
            // the guid is the one from ffdshow
            Type   fftype  = Type.GetTypeFromCLSID(new Guid("0F40E1E5-4F79-4988-B1A9-CC98794E6B55"));
            object ffdshow = Activator.CreateInstance(fftype);

            hr = gb.AddFilter((IBaseFilter)ffdshow, "ffdshow");
            DsError.ThrowExceptionForHR(hr);
            // the guid is the one from the WAV Dest sample in the SDK
            Type   type     = Type.GetTypeFromCLSID(new Guid("3C78B8E2-6C4D-11d1-ADE2-0000F8754B99"));
            object wavedest = Activator.CreateInstance(type);

            hr = gb.AddFilter((IBaseFilter)wavedest, "WAV Dest");
            DsError.ThrowExceptionForHR(hr);
            // manually tell the graph builder to try to hook up the pin that is left
            IPin pWaveDestOut = null;

            hr = icgb.FindPin(wavedest, PinDirection.Output, null, null, true, 0, out pWaveDestOut);
            DsError.ThrowExceptionForHR(hr);
            // render step 1
            hr = icgb.RenderStream(null, null, ibfSrcFile, (IBaseFilter)ffdshow, (IBaseFilter)wavedest);
            DsError.ThrowExceptionForHR(hr);
            // Configure the sample grabber
            IBaseFilter baseGrabFlt = sg as IBaseFilter;

            ConfigSampleGrabber(sg);
            IPin pGrabberIn  = DsFindPin.ByDirection(baseGrabFlt, PinDirection.Input, 0);
            IPin pGrabberOut = DsFindPin.ByDirection(baseGrabFlt, PinDirection.Output, 0);

            hr = gb.AddFilter((IBaseFilter)sg, "SampleGrabber");
            DsError.ThrowExceptionForHR(hr);
            AMMediaType mediatype = new AMMediaType();

            sg.GetConnectedMediaType(mediatype);
            hr = gb.Connect(pWaveDestOut, pGrabberIn);
            DsError.ThrowExceptionForHR(hr);
            // file writer
            FileWriter      file_writer = new FileWriter();
            IFileSinkFilter fs          = (IFileSinkFilter)file_writer;

            fs.SetFileName(targetfile, null);
            hr = gb.AddFilter((DirectShowLib.IBaseFilter)file_writer, "File Writer");
            DsError.ThrowExceptionForHR(hr);
            // render step 2
            AMMediaType mediatype2 = new AMMediaType();

            pWaveDestOut.ConnectionMediaType(mediatype2);
            gb.Render(pGrabberOut);
            // alternatively to the file writer use the NullRenderer() to just discard the rest
            // assign control
            m_mediaCtrl = gb as IMediaControl;
            // run
            hr = m_mediaCtrl.Run();
            DsError.ThrowExceptionForHR(hr);
        }