简体   繁体   English

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

[英]Video Streaming and Recording st the same time Using Directshow.Net in c#?

i want to record video through webcam using Directshow.Net.i can able to record the video using ASFWriter but along with recording i want to stream the video to an PC in the LAN..i tried this.. 我想使用Directshow.Net.net通过网络摄像头录制视频。我可以使用ASFWriter录制视频,但是与录制一起,我想将视频流传输到LAN中的PC。

i run project which i develop for recording the video this is the code 我运行我开发的用于录制视频的项目,这是代码

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;
        }
    }

this will start the video recording..after that i run this project exe from "Release" folder it will gives error like "media run failed" 这将开始视频录制..之后,我从“ Release”文件夹运行项目exe,它将显示诸如“ Media Run failed”的错误

Now My question is it possible to do recording and live streaming at the same time? 现在,我的问题是可以同时进行记录和实时流式传输吗?

If Yes,then please guide me through this..and also please guide me on my this post also 如果是,那么请你指导我this..and还请指导我对我这个职位

I think you should build the graph manually. 我认为您应该手动构建图形。 The graph should look like the follwing diagramm. 该图应看起来像下面的图表。 You can test the Graph using GraphEdt. 您可以使用GraphEdt测试Graph。 This also helps for getting the Guid and PinNames. 这也有助于获取Guid和PinName。

VideoSource -> SmartTee -> StreamingFilter
                        -> CaptureFilter

The DirectShowLib provides all functions you need to do build the graph. DirectShowLib提供构建图形所需的所有功能。

You can create the filter like you did in your example. 您可以像在示例中一样创建过滤器。 The SmartTee filter can be created directly. 可以直接创建SmartTee过滤器。

You should connect the filter using graph.Connect() method. 您应该使用graph.Connect()方法连接过滤器。 Using this, you can build the following graph using the SmartTee Filter. 使用此功能,您可以使用SmartTee筛选器构建以下图形。 SmartTee filter should be available on your system and provides two output pins, one for capturing and one for preview. SmartTee过滤器应该在您的系统上可用,并提供两个输出引脚,一个用于捕获,另一个用于预览。 You should use preveiw pin for the Streaming and the capture pin for the Capture filter. 您应将preveiw引脚用于流传输,将捕获引脚用于Capture过滤器。

You can get the required Pin for the connect method using the following function: 您可以使用以下功能获取连接方法所需的引脚:

    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;
    }

At the end, you have to start the graph and everything will work hopefully. 最后,您必须开始绘制图表,一切都会顺利进行。 It is important taht you check the hr code after every method call for error handling. 在每次方法调用后检查hr代码以进行错误处理,这一点很重要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM