DirectShow connect filters

579 views Asked by At

I need to overlay a text on video.

My change: Source --> SampleGrabber --> Compressor --> mux(out_file)

SetOutputFileName and add source:

public void Start(object compr)
        {
            ICaptureGraphBuilder2 gb = new CaptureGraphBuilder2() as ICaptureGraphBuilder2;
            IBaseFilter mux;
            IFileSinkFilter sink;
            Guid MEDIASUBTYPE_Avi = new Guid(0xe436eb88, 0x524f, 0x11ce, 0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70);
            gb.SetOutputFileName(MEDIASUBTYPE_Avi,
                    out_file, out mux, out sink);

            IGraphBuilder gbi;
            gb.GetFiltergraph(out gbi).Check();
            f_fg = gbi as IFilterGraph2;
            IBaseFilter src;
            f_fg.AddSourceFilter(path_file, "Source", out src).Check();

Add filter SampleGrabber and connect with Source:

        Type comType = Type.GetTypeFromCLSID(new Guid("C1F400A0-3F08-11d3-9F0B-006008039E37"));
        ISampleGrabber sampleGrabber = (ISampleGrabber)Activator.CreateInstance(comType);
        f_fg.AddFilter((IBaseFilter)sampleGrabber, "samplegrabber").Check();
        AMMediaType mediaType = new AMMediaType();
        mediaType.majorType = MediaType.Video;
        mediaType.subType = MediaSubType.RGB24;
        mediaType.formatType = FormatType.VideoInfo;
        sampleGrabber.SetMediaType(mediaType);
        sampleGrabber.SetCallback(this, 1);

        // connect filters src-->SampleGrabber
        gbi.ConnectFilters(src, (IBaseFilter)sampleGrabber);

Add filter Compressor and start RenderStream:

 IBaseFilter compressor = ((DShowFilterInfo)compr).CreateFilter();
    f_fg.AddFilter(compressor, "Compressor").Check();
gb.RenderStream(null, null, (IBaseFilter)sampleGrabber, compressor, mux).Check();

(f_fg as IMediaFilter).SetSyncSource(IntPtr.Zero).Check();
(f_fg as IMediaControl).Run();
}

public int BufferCB(double sampleTime, IntPtr buffer, int bufferLen)
{
    return 0;
}

But video out_file not open(play only 1 second and black window). Why?

1

There are 1 answers

0
Alex Chan On

The above code works correctly ... I just forgot after the render call:

(f_fg as IMediaControl).Stop();