簡體   English   中英

同時進行視頻流和錄制在C#中使用Directshow.Net?

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

我想使用Directshow.Net.net通過網絡攝像頭錄制視頻。我可以使用ASFWriter錄制視頻,但是與錄制一起,我想將視頻流傳輸到LAN中的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”文件夾運行項目exe,它將顯示諸如“ Media Run failed”的錯誤

現在,我的問題是可以同時進行記錄和實時流式傳輸嗎?

如果是,那么請你指導我this..and還請指導我對我這個職位

我認為您應該手動構建圖形。 該圖應看起來像下面的圖表。 您可以使用GraphEdt測試Graph。 這也有助於獲取Guid和PinName。

VideoSource -> SmartTee -> StreamingFilter
                        -> CaptureFilter

DirectShowLib提供構建圖形所需的所有功能。

您可以像在示例中一樣創建過濾器。 可以直接創建SmartTee過濾器。

您應該使用graph.Connect()方法連接過濾器。 使用此功能,您可以使用SmartTee篩選器構建以下圖形。 SmartTee過濾器應該在您的系統上可用,並提供兩個輸出引腳,一個用於捕獲,另一個用於預覽。 您應將preveiw引腳用於流傳輸,將捕獲引腳用於Capture過濾器。

您可以使用以下功能獲取連接方法所需的引腳:

    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代碼以進行錯誤處理,這一點很重要。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM