视频流和录制同时在c#中使用Directshow.Net?

时间:2012-06-12 10:21:18

标签: c# directshow.net

我想通过网络摄像头录制视频使用Directshow.Net.i能够使用ASFWriter录制视频,但随着录制我希望将视频流传输到局域网中的PC ..我试过这个..

我运行的项目我开发用于录制视频这是代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DirectShowLib;
using DirectShowLib.DMO;
using System.Runtime.InteropServices;
using System.Drawing.Drawing2D;
using System.IO;

namespace Cam_Recording_V1_directshow.net_
{
public partial class Form1 : Form
{
    string captureDeviceName = string.Empty;
    IFilterGraph2 Graph = null;
    IMediaControl m_mediaCtrl = null;
    public List<DsDevice> AvailableVideoInputDevices { get; private set; }
    IAMVideoProcAmp vpa;
    [DllImport("olepro32.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
    private static extern int OleCreatePropertyFrame(IntPtr hwndOwner, int x, int y,
        string lpszCaption, int cObjects,
        [In, MarshalAs(UnmanagedType.Interface)] ref object ppUnk,
        int cPages, IntPtr pPageClsID, int lcid, int dwReserved, IntPtr pvReserved);
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

        IBaseFilter capFilter = null;
        IBaseFilter asfWriter = null;
        IFileSinkFilter pTmpSink = null;
        ICaptureGraphBuilder2 captureGraph = null;
        object o;

        //
        //Get list of video devices
        //
        AvailableVideoInputDevices = new List<DsDevice>();
        DsDevice[] videoInputDevices = DsDevice.GetDevicesOfCat(FilterCategory.VideoInputDevice);
        AvailableVideoInputDevices.AddRange(videoInputDevices);
        if (AvailableVideoInputDevices.Count > 0)
        {
            //
            //init capture graph
            //
            Graph = (IFilterGraph2)new FilterGraph();
            captureGraph = (ICaptureGraphBuilder2)new CaptureGraphBuilder2();

            //
            //sets filter object from graph
            //
            captureGraph.SetFiltergraph(Graph);
            //
            //which device will use graph setting
            //
            Graph.AddSourceFilterForMoniker(AvailableVideoInputDevices.First().Mon, null, AvailableVideoInputDevices.First().Name, out capFilter);
            captureDeviceName = AvailableVideoInputDevices.First().Name;
            #region WMV
            //
            //sets output file name,and file type
            //
            captureGraph.SetOutputFileName(MediaSubType.Asf, /*DateTime.Now.Ticks.ToString()  +".wmv"*/ "test.wmv", out asfWriter, out pTmpSink);
            //
            //configure which video setting is used by graph
            //                
            IConfigAsfWriter lConfig = asfWriter as IConfigAsfWriter;
            Guid cat = new Guid("8C45B4C7-4AEB-4f78-A5EC-88420B9DADEF");
            lConfig.ConfigureFilterUsingProfileGuid(cat);
            #endregion                      
            captureGraph.RenderStream(PinCategory.Preview, MediaType.Video, capFilter, null, null);

            captureGraph.RenderStream(PinCategory.Capture, MediaType.Video, capFilter, null, asfWriter);
            m_mediaCtrl = Graph as IMediaControl;
            m_mediaCtrl.Run();
        }
        else
        {
            MessageBox.Show("Video Capture Device Not Found!!");
            button1.Visible = false;
        }
    }

这将启动视频录制。之后我从“Release”文件夹运行this项目exe,它将出现“媒体运行失败”等错误

现在我的问题是否可以同时进行录制和直播?

如果是,那么请引导我完成这个......还请指导我这个post

1 个答案:

答案 0 :(得分:1)

我认为您应该手动构建图表。该图应该看起来像下面的图表。您可以使用GraphEdt测试图表。这也有助于获得Guid和PinNames。

VideoSource -> SmartTee -> StreamingFilter
                        -> CaptureFilter

DirectShowLib提供了构建图形所需的所有功能。

您可以像在示例中一样创建过滤器。可以直接创建SmartTee过滤器。

您应该使用graph.Connect()方法连接过滤器。使用此功能,您可以使用SmartTee过滤器构建以下图形。 SmartTee过滤器应该在您的系统上可用,并提供两个输出引脚,一个用于捕获,另一个用于预览。您应该使用preveiw引脚作为Streaming,使用捕获引脚作为Capture过滤器。

您可以使用以下函数获取连接方法所需的Pin:

    public IPin GetPin(IBaseFilter filter, string pinname)
    {
        IEnumPins epins;
        int hr = filter.EnumPins(out epins);
        if(hr < 0)
            return null;
        IntPtr fetched = Marshal.AllocCoTaskMem(4);
        IPin[] pins = new IPin[1];
        epins.Reset();
        while (epins.Next(1, pins, fetched) == 0)
        {
            PinInfo pinfo;
            pins[0].QueryPinInfo(out pinfo);
            bool found = (pinfo.name == pinname);
            DsUtils.FreePinInfo(pinfo);
            if (found)
                return pins[0];
        }
        return null;
    }

最后,你必须启动图表,一切都会有希望。在每次调用错误处理的方法之后检查hr代码是很重要的。