CodeTV.GraphBuilderPlayer.BuildGraph C# (CSharp) Method

BuildGraph() public method

public BuildGraph ( ) : void
return void
        public override void BuildGraph()
        {
            try
            {
                useWPF = Settings.UseWPF;

                int hr = 0;

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

                string extension = Path.GetExtension(this.fileName).ToLower();

                if (extension == ".ts")
                {
                    TsFileSource fileSource = new TsFileSource();
                    this.filterSource = fileSource as IBaseFilter;
                    if (this.filterSource != null)
                    {
                        this.graphBuilder.AddFilter(this.filterSource, "TsFileSource");

                        IFileSourceFilter interFaceFile = (IFileSourceFilter)fileSource;
                        interFaceFile.Load(this.fileName, null);

                        ITSFileSource tsFileSource = fileSource as ITSFileSource;
                        ushort audioPid = 0;
                        tsFileSource.GetAudioPid(ref audioPid);

                        ushort videoPid = 0;
                        tsFileSource.GetVideoPid(ref videoPid);

                        byte[] videoPidTypeByteBuffer = new byte[16];
                        tsFileSource.GetVideoPidType(videoPidTypeByteBuffer);

                        int posCharZero = 0;
                        for (; posCharZero < videoPidTypeByteBuffer.Length; posCharZero++) if (videoPidTypeByteBuffer[posCharZero] == 0) break;

                        char[] videoPidTypeCharBuffer = new char[posCharZero];
                        Array.Copy(videoPidTypeByteBuffer, 0, videoPidTypeCharBuffer, 0, posCharZero);
                        string videoPidType = new string(videoPidTypeCharBuffer);
                        // "MPEG 2", "H.264"

                        AddMPEG2DemuxFilter();

                        //IMpeg2Demultiplexer mpeg2Demultiplexer = this.mpeg2Demux as IMpeg2Demultiplexer;

                        ////Log.WriteFile(Log.LogType.Log, false, "DVBGraphBDA: create mpg4 video pin");
                        //AMMediaType mediaMPG4 = new AMMediaType();
                        //mediaMPG4.majorType = MediaType.Video;
                        //mediaMPG4.subType = new Guid(0x8d2d71cb, 0x243f, 0x45e3, 0xb2, 0xd8, 0x5f, 0xd7, 0x96, 0x7e, 0xc0, 0x9b);
                        //mediaMPG4.sampleSize = 0;
                        //mediaMPG4.temporalCompression = false;
                        //mediaMPG4.fixedSizeSamples = false;
                        //mediaMPG4.unkPtr = IntPtr.Zero;
                        //mediaMPG4.formatType = FormatType.Mpeg2Video;
                        //mediaMPG4.formatSize = Mpeg2ProgramVideo.GetLength(0);
                        //mediaMPG4.formatPtr = System.Runtime.InteropServices.Marshal.AllocCoTaskMem(mediaMPG4.formatSize);
                        //System.Runtime.InteropServices.Marshal.Copy(Mpeg2ProgramVideo, 0, mediaMPG4.formatPtr, mediaMPG4.formatSize);

                        //int hr = mpeg2Demultiplexer.CreateOutputPin(mediaMPG4, "MPG4", out this.pinDemuxerVideoMPEG4);
                        //if (this.pinDemuxerVideoMPEG4 != null)
                        //{
                        //    IMPEG2PIDMap mpeg2PIDMap = this.pinDemuxerVideoMPEG4 as IMPEG2PIDMap;
                        //    if (mpeg2PIDMap != null)
                        //        hr = mpeg2PIDMap.MapPID(1, new int[] { 0x00a2 }, MediaSampleContent.ElementaryStream);
                        //    Marshal.ReleaseComObject(this.pinDemuxerVideoMPEG4);
                        //}
                        ////if (hr < 0 || this.pinDemuxerVideoMPEG4 == null)
                        ////{

                        ////    _lastError = String.Format("failed to add mpg4 video pin");
                        ////    Log.WriteFile(Log.LogType.Log, true, "DVBGraphBDA:FAILED to create MPG4 pin:0x{0:X}", hr);
                        ////}

                        //DsDevice[] tunDevices = DeviceEnumerator.GetH264Devices();
                        //if (tunDevices.Length > 0)
                        //{
                        //    IBaseFilter elecardMPEG4VideoDecoder;
                        //    hr = this.graphBuilder.AddSourceFilterForMoniker(tunDevices[0].Mon, null, tunDevices[0].Name, out elecardMPEG4VideoDecoder);
                        //    DsError.ThrowExceptionForHR(hr);
                        //}

                        AddRenderers();
                        if (!useWPF)
                            ConfigureVMR9InWindowlessMode(2);

                        //IVMRMixerControl9 vmrMixerControl9 = this.videoRenderer as IVMRMixerControl9;
                        //vmrMixerControl9.SetZOrder(0, 1);

                        //// Connect the MPEG-2 Demux output pin for the "BDA MPEG2 Transport Information Filter"
                        //IPin pinOut = DsFindPin.ByDirection(this.filterSource, PinDirection.Output, 0);
                        //if (pinOut != null)
                        //{
                        //    hr = this.graphBuilder.Render(pinOut);
                        //    //DsError.ThrowExceptionForHR(hr);
                        //    // In fact the last pin don't render since i havn't added the BDA MPE Filter...
                        //    Marshal.ReleaseComObject(pinOut);
                        //}

                        //ConnectFilters();
                        //IPin pinOut = DsFindPin.ByDirection(this.mpeg2Demux, PinDirection.Output, 0);
                        //if (pinOut != null)
                        //{
                        //    hr = this.graphBuilder.Render(pinOut);
                        //    //DsError.ThrowExceptionForHR(hr);
                        //    // In fact the last pin don't render since i havn't added the BDA MPE Filter...
                        //    Marshal.ReleaseComObject(pinOut);
                        //}

                        //pinOut = DsFindPin.ByDirection(this.mpeg2Demux, PinDirection.Output, 1);
                        //if (pinOut != null)
                        //{
                        //    hr = this.graphBuilder.Render(pinOut);
                        //    //DsError.ThrowExceptionForHR(hr);
                        //    // In fact the last pin don't render since i havn't added the BDA MPE Filter...
                        //    Marshal.ReleaseComObject(pinOut);
                        //}

                        IPin pinOut = DsFindPin.ByDirection(this.filterSource, PinDirection.Output, 0);
                        if (pinOut != null)
                        {
                            hr = this.graphBuilder.Render(pinOut);
                            //DsError.ThrowExceptionForHR(hr);
                            // In fact the last pin don't render since i havn't added the BDA MPE Filter...
                            Marshal.ReleaseComObject(pinOut);
                        }

                        AddAndConnectNullRendererForWPF();

                        this.hostingControl.CurrentGraphBuilder = this;

                        OnGraphStarted();

                        return;
                    }
                }

                AddRenderers();
                if (!useWPF)
                    ConfigureVMR9InWindowlessMode();

                this.graphBuilder.RenderFile(this.fileName, null);

                //AddAndConnectNullRendererForWPF();
                if (useWPF)
                {
                    // In order to keep the audio/video synchro, we need the NullRenderer
                    IBaseFilter nullRenderer = new NullRenderer() as IBaseFilter;
                    hr = graphBuilder.AddFilter(nullRenderer, "NullRenderer");
                    ThrowExceptionForHR("Adding the NullRenderer: ", hr);

                    IPin pinOutFromFilterOut = DsFindPin.ByDirection(this.videoRenderer, PinDirection.Output, 0);
                    if (pinOutFromFilterOut != null)
                    {
                        UnRender(pinOutFromFilterOut);
                        try
                        {
                            IPin pinInFromFilterOut = DsFindPin.ByDirection(nullRenderer, PinDirection.Input, 0);
                            if (pinInFromFilterOut != null)
                            {
                                try
                                {
                                    hr = this.graphBuilder.Connect(pinOutFromFilterOut, pinInFromFilterOut);
                                }
                                finally
                                {
                                    Marshal.ReleaseComObject(pinInFromFilterOut);
                                }
                            }
                        }
                        finally
                        {
                            Marshal.ReleaseComObject(pinOutFromFilterOut);
                        }
                    }

                    //IPin pinOutFromFilterOut = DsFindPin.ByDirection(this.videoRenderer, PinDirection.Output, 0);
                    //if (pinOutFromFilterOut != null)
                    //{
                    //    hr = this.graphBuilder.Render(pinOutFromFilterOut);
                    //    Marshal.ReleaseComObject(pinOutFromFilterOut);
                    //}

                    WpfUpdateVideoSize(); //WPF
                }

                this.hostingControl.CurrentGraphBuilder = this;

                OnGraphStarted();
            }
            catch (Exception ex)
            {
                Decompose();
                throw ex;
            }
        }