如何将视频流从表单中的视频复制到另一种形式的另一种视频?

时间:2016-08-10 02:57:01

标签: c# winforms visual-studio media

我正在创建一个应用程序,允许用户在另一个名为BroadcastVideoForm.cs的表单中预览时,以名为MainForm.cs的表单(请参阅下面的代码)从网络摄像头实时查看视频(请参阅代码下面)。

所以这是怎么回事,当程序运行时,会弹出MainForm.cs

1。)用户可以选择视频和音频设备设置,然后点击buttonInit_Click(object sender, EventArgs e)方法中用MainForm.cs表示的开始预览视频。

2。)视频现在将使用网络摄像头显示在MainForm.cs,然后MainForm.cs中有一个广播按钮,由broadcast_Click(object sender, EventArgs e)方法表示。

3。)当用户点击该视频时,会弹出BroadcastVideoForm.cs并使用网络摄像头显示该视频,同时仍在MainForm.cs中显示该视频,但该视频无效,视频仅显示在MainForm.cs

我基本上希望视频能同时以两种形式显示。我怎样才能做到这一点?感谢。

当我尝试单独运行它们时它们都有效。但是当我正常运行时,只有MainForm.cs显示视频。我该如何解决?谢谢。

由于空间有限,MainForm.cs的某些部分。向下滚动到底部以查看broadcast_Click(object sender, EventArgs e)方法。

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using MPLATFORMLib;

namespace NetworkExample
{
    public partial class MainForm : Form
    {
        private MLiveClass m_objMLive;  //Live source class. Intednded for     receiving data from capture cards, cams and other devices
        private MRendererClass m_objRenderer; // Renderer class used for output

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        private static extern Int32 SendMessage(IntPtr hWnd, int msg, int wParam, [MarshalAs(UnmanagedType.LPWStr)]string lParam);
        public MainForm()
        {
            InitializeComponent();
            SendMessage(textBoxNDIWebRTCName.Handle, 0x1501, 0, "NDI/WebRTC Name");
        }

        /// <summary>
        /// Initialization of Medialooks objects and form controls
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                this.Text += " - MPlatform SDK " + CheckVersionClass.GetVersion();
            }
            catch { }

            CheckForIllegalCrossThreadCalls = false;

            try
            {
                //Initialize objects
                m_objMLive = new MLiveClass();
                m_objRenderer = new MRendererClass();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Can't crate MFile instance: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Application.Exit();
                return;
            }

            //Fill Video Device List
            FillCombo("video", comboBoxVideo);
            // Fill ext_audio
            FillCombo("ext_audio", comboBoxExAudio);

            //Initialize preview
            m_objMLive.PreviewWindowSet("", panelPreview.Handle.ToInt32());
            m_objMLive.PreviewEnable("", checkBoxAudio.Checked ? 1 : 0, checkBoxVideo.Checked ? 1 : 0);

            //Fill output formats
            FillOutputVideoFormats();

            //Look for renderers                      
            int nDevices = 0;
            m_objRenderer.DeviceGetCount(0, "renderer", out nDevices);
            if (nDevices > 0)
            {
                checkBoxOutput.Enabled = true;
                comboBoxRenderer.Enabled = true;
                for (int i = 0; i < nDevices; i++)
                {
                    string strName;
                    string strXML;
                    m_objRenderer.DeviceGetByIndex(0, "renderer", i, out strName, out strXML);
                    comboBoxRenderer.Items.Add(strName);
                }
                comboBoxRenderer.SelectedIndex = 0;
            }
            else
            {
                checkBoxOutput.Enabled = false;
                comboBoxRenderer.Enabled = false;
            }
            // Update Delay Props
            UpdateDelay();

        }

        /// <summary>
        /// Fill video output formats list
        /// </summary>
        private void FillOutputVideoFormats()
        {
            comboBoxVFOut.Items.Clear();
            comboBoxVFOut.Items.Add("<Auto>");
            comboBoxVFOut.Items.Add(eMVideoFormat.eMVF_NTSC);
            comboBoxVFOut.Items.Add(eMVideoFormat.eMVF_NTSC_16x9);
            comboBoxVFOut.Items.Add(eMVideoFormat.eMVF_PAL);
            comboBoxVFOut.Items.Add(eMVideoFormat.eMVF_PAL_16x9);

            comboBoxVFOut.Items.Add(eMVideoFormat.eMVF_HD720_50p);
            comboBoxVFOut.Items.Add(eMVideoFormat.eMVF_HD720_5994p);
            comboBoxVFOut.Items.Add(eMVideoFormat.eMVF_HD720_60p);

            comboBoxVFOut.Items.Add(eMVideoFormat.eMVF_HD1080_24p);
            comboBoxVFOut.Items.Add(eMVideoFormat.eMVF_HD1080_25p);
            comboBoxVFOut.Items.Add(eMVideoFormat.eMVF_HD1080_2997p);
            comboBoxVFOut.Items.Add(eMVideoFormat.eMVF_HD1080_30p);

            comboBoxVFOut.Items.Add(eMVideoFormat.eMVF_HD1080_50i);
            comboBoxVFOut.Items.Add(eMVideoFormat.eMVF_HD1080_50p);
            comboBoxVFOut.Items.Add(eMVideoFormat.eMVF_HD1080_5994i);
            comboBoxVFOut.Items.Add(eMVideoFormat.eMVF_HD1080_5994p);           

            comboBoxVFOut.Items.Add(eMVideoFormat.eMVF_HD1080_60i);
            comboBoxVFOut.Items.Add(eMVideoFormat.eMVF_HD1080_60p);

            comboBoxVFOut.SelectedIndex = 0;
        }



        /// <summary>
        /// Fill combo boxes (Audio/Video device and Audio/Video input line (if avilable))
        /// </summary>
        /// <param name="pDevice"></param>
        /// <param name="strType"></param>
        /// <param name="cbxType"></param>
        private void FillCombo(string strType, ComboBox cbxType)
        {
            lock (this)
            {
                cbxType.Items.Clear();
                cbxType.Tag = strType;
                int nCount = 0;
                //Get device count / input line count
                m_objMLive.DeviceGetCount(0, strType, out nCount);
                cbxType.Enabled = nCount > 0;
                if (nCount > 0)
                {
                    for (int i = 0; i < nCount; i++)
                    {
                        string strName;
                        string strDesc;
                        //Get deveice / input line
                        m_objMLive.DeviceGetByIndex(0, strType, i, out strName, out strDesc);
                        cbxType.Items.Add(strName);
                    }
                    string strCur = "";
                    string strParam = "";
                    int nIndex = 0;
                    try
                    {
                        //Check if there is already selected device / input line
                        m_objMLive.DeviceGet(strType, out strCur, out strParam, out nIndex);
                        cbxType.SelectedIndex = !string.IsNullOrEmpty(strCur) ? cbxType.FindStringExact(strCur) : 0;
                    }
                    catch
                    {
                        cbxType.SelectedIndex = 0;
                    }
                }
            }
        }

        /// <summary>
        /// Fill combo boxes (Audio / Video format)
        /// </summary>
        /// <param name="pDevice"></param>
        /// <param name="strType"></param>
        /// <param name="cbxTarget"></param>
        private void FillComboFomat(IMDevice pDevice, string strType, ComboBox cbxTarget)
        {
            if (strType == "video")
            {
                int nCount = 0;
                int nIndex;
                string strFormat;
                M_VID_PROPS vidProps;
                cbxTarget.Items.Clear();
                //Get video format count
                m_objMLive.FormatVideoGetCount(eMFormatType.eMFT_Input, out nCount);
                cbxTarget.Enabled = nCount > 0;
                if (nCount > 0)
                {
                    for (int i = 0; i < nCount; i++)
                    {
                        //Get format by index
                        m_objMLive.FormatVideoGetByIndex(eMFormatType.eMFT_Input, i, out vidProps, out strFormat);
//                        cbxTarget.Items.Add(vidProps.eVideoFormat);
                        cbxTarget.Items.Add(strFormat);

                    }
                    //Check if there is selected format
                    m_objMLive.FormatVideoGet(eMFormatType.eMFT_Input, out vidProps, out nIndex, out strFormat);
                    if (nIndex > 0)
                        cbxTarget.SelectedIndex = nIndex;
                    else cbxTarget.SelectedIndex = 0;
                }

            }
            else if (strType == "audio")
            {
                int nCount = 0;
                int nIndex;
                string strFormat;
                M_AUD_PROPS audProps;
                cbxTarget.Items.Clear();               
                //Get video format count
                m_objMLive.FormatAudioGetCount(eMFormatType.eMFT_Input, out nCount);
                cbxTarget.Enabled = nCount > 0;
                if (nCount > 0)
                {
                    for (int i = 0; i < nCount; i++)
                    {
                        //Get audio format
                        m_objMLive.FormatAudioGetByIndex(eMFormatType.eMFT_Input, i, out audProps, out strFormat);
                        cbxTarget.Items.Add(strFormat);
                    }
                    //Check if there is selected format
                    m_objMLive.FormatAudioGet(eMFormatType.eMFT_Input, out audProps, out nIndex, out strFormat);
                    if (nIndex > 0)
                        cbxTarget.SelectedIndex = nIndex;
                    else cbxTarget.SelectedIndex = 0;
                }
            }
            cbxTarget.Tag = strType;
        }

        /// <summary>
        /// Device / input line changed
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void comboBoxAV_SelectedIndexChanged(object sender, EventArgs e)
        {
            ComboBox cbxChanged = (ComboBox)sender;
            string strType = (string)cbxChanged.Tag;
            try
            {
                //Set device
                m_objMLive.DeviceSet(strType, (string)cbxChanged.SelectedItem, "");
            }
            catch
            {
                MessageBox.Show("Can't set this device, it isn't supported", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            if (strType == "video")
            {
                buttonNDIRefresh.Enabled = cbxChanged.SelectedItem.ToString().Contains("NDI");

                // Update audio
                FillCombo("audio", comboBoxAudio);
                // Update input lines
                FillCombo(strType + "::line-in", comboBoxVL);
                //Update Formats
                FillComboFomat(m_objMLive, strType, comboBoxVF);
            }
            else if (strType == "audio")
            {
                //Update Formats
                FillComboFomat(m_objMLive, strType, comboBoxAF);
            }
        }

        /// <summary>
        /// Format changed
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void comboBoxAVF_SelectedIndexChanged(object sender, EventArgs e)
        {
            ComboBox cbxChanged = (ComboBox)sender;
            string strType = (string)cbxChanged.Tag;

            if (strType == "video")
            {
                M_VID_PROPS vidProps = new M_VID_PROPS();
                string strFormat;
                m_objMLive.FormatVideoGetByIndex(eMFormatType.eMFT_Input, cbxChanged.SelectedIndex, out vidProps, out strFormat);
                //Set new video format
                m_objMLive.FormatVideoSet(eMFormatType.eMFT_Input, ref vidProps);
            }
            else if (strType == "audio")
            {
                M_AUD_PROPS audProps = new M_AUD_PROPS();
                string strFormat;
                m_objMLive.FormatAudioGetByIndex(eMFormatType.eMFT_Input, cbxChanged.SelectedIndex, out audProps, out strFormat);
                //Set new audio format
                m_objMLive.FormatAudioSet(eMFormatType.eMFT_Input, ref audProps);
            }
        }

        /// <summary>
        /// Close current live source
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonClose_Click(object sender, EventArgs e)
        {
            m_objMLive.ObjectClose();
        }

        /// <summary>
        /// Show video device properties (if available)
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonV_Click(object sender, EventArgs e)
        {
            try
            {
                m_objMLive.DeviceShowProps("video", "device", this.Handle.ToInt32());
            }
            catch { }
        }

        /// <summary>
        /// Show audio device properties (if available)
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonA_Click(object sender, EventArgs e)
        {
            try
            {
                m_objMLive.DeviceShowProps("audio", "device", this.Handle.ToInt32());
            }
            catch {}
        }

        /// <summary>
        /// Show video format properties (if available)
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonVF_Click(object sender, EventArgs e)
        {
            try
            {
                m_objMLive.DeviceShowProps("video", "stream", this.Handle.ToInt32());
            }
            catch { }
        }

        /// <summary>
        /// Show audio format properties(if available)
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonAF_Click(object sender, EventArgs e)
        {
            try
            {
                m_objMLive.DeviceShowProps("audio", "stream", this.Handle.ToInt32());
            }
            catch { }
        }

        private void buttonInit_Click(object sender, EventArgs e)
        {
            m_objMLive.ObjectStart(null);
        }

        /// <summary>
        /// Enable/desable the video preview
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void checkBoxVideo_CheckedChanged(object sender, EventArgs e)
        {
            if (m_objMLive != null)
            m_objMLive.PreviewEnable("", checkBoxAudio.Checked ? 1 : 0, checkBoxVideo.Checked ? 1 : 0);
        }

        /// <summary>
        /// Enable/desable the audio preview
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void checkBoxAudio_CheckedChanged(object sender, EventArgs e)
        {
            if (m_objMLive != null)
            m_objMLive.PreviewEnable("", checkBoxAudio.Checked ? 1 : 0, checkBoxVideo.Checked ? 1 : 0);
        }

        /// <summary>
        /// Set audio volume
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            // Volume in dB
            // 0 - full volume, -100 silence
            double dblPos = (double)trackBarVolume.Value / trackBarVolume.Maximum;
            m_objMLive.PreviewAudioVolumeSet("", -1, -30 * (1 - dblPos));
        }


        /// <summary>
        /// Refresh preview after resizing
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void comboBoxVFOut_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (comboBoxVFOut.SelectedIndex != 0)
                {
                    // Set video format
                    M_VID_PROPS vidProps = new M_VID_PROPS();
                    vidProps.eVideoFormat = (eMVideoFormat)comboBoxVFOut.SelectedItem;
                    m_objMLive.FormatVideoSet(eMFormatType.eMFT_Convert, ref vidProps);
                }
            }
            catch
            {
                throw;
            }
        }

        /// <summary>
        /// Enable/Disable Decklink output
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void checkBoxOutput_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxOutput.Checked)
            {
                try
                {
                    m_objRenderer.PropsSet("rate-control", "true");
                    m_objRenderer.DeviceSet("renderer", comboBoxRenderer.SelectedItem.ToString(), "");

                    if (textBoxNDIWebRTCName.Enabled && !String.IsNullOrEmpty(textBoxNDIWebRTCName.Text))
                        m_objRenderer.DeviceSet("renderer::line-out", textBoxNDIWebRTCName.Text, "");

                    m_objRenderer.ObjectStart(m_objMLive);
                }
                catch
                {
                    checkBoxOutput.Checked = false;
                    throw;
                }
            }
            else
            {
                try
                {
                    m_objRenderer.ObjectClose();
                }
                catch
                {
                    checkBoxOutput.Checked = false;
                    throw;
                }
            }
        }

        /// <summary>
        /// //Create/destroy virtual device
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void checkBoxVDev_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxVDev.Checked)
            {
                // Create virtual device, use default name 
                m_objMLive.ObjectVirtualSourceCreate(1, "", "");
            }
            else
            {
                // Destroy virtual source
                m_objMLive.ObjectVirtualSourceCreate(0, "", "");
            }
        }
        string m_strItemID;
        public MLCHARGENLib.CoMLCharGen m_objCharGen;

        private void checkBoxCG_CheckedChanged(object sender, EventArgs e)
        {
            if (m_objCharGen != null)
            {
                m_objMLive.PluginsRemove(m_objCharGen);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objCharGen);
                m_objCharGen = null;
                m_strItemID = "";
                CGProps.Enabled = false;
            }

            if (checkBoxCG.Checked)
            {
                m_objCharGen = new MLCHARGENLib.CoMLCharGen();
                m_objMLive.PluginsAdd(m_objCharGen, 0);

                m_objCharGen.AddNewItem("MediaLooks", 0.1, 0.1, 1, 1, ref m_strItemID);
                m_objCharGen.SetItemProperties(m_strItemID, "movement::speed-x", "1", "", 0);
                m_objCharGen.ShowItem(m_strItemID, 1, 1000);

                CGProps.Enabled = true;
            }

        }

        private void CGProps_Click(object sender, EventArgs e)
        {
            if (m_objCharGen != null)
                m_objCharGen.ShowPropertiesPage(Handle.ToInt32());

        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (m_objMLive != null) // <- ADD THIS HERE
                m_objMLive.ObjectClose();
            if (m_objRenderer != null) // <- ADD THIS HERE
                m_objRenderer.ObjectClose();
        }


        private void timerDelay_Tick(object sender, EventArgs e)
        {
            UpdatePos();
        }

        void UpdatePos()
        {
            try
            {
                IMProps pProps = (IMProps)m_objMLive;
                string sValue;

                pProps.PropsGet("object::mdelay.available", out sValue);
                numericDelayTime.Value = Decimal.Parse(sValue);
                trackBarSeek.Minimum = -1 * (int)Decimal.Parse(sValue);
                trackBarSeek.TickFrequency = -1 * trackBarSeek.Minimum / 20;

                pProps.PropsGet("object::mdelay.time", out sValue);
                trackBarSeek.Value = -1 * Int32.Parse(sValue);
            }
            catch (System.Exception) { }
        }

        void UpdateDelay()
        {
            try
            {
                IMProps pProps = (IMProps)m_objMLive;
                string sValue;
                pProps.PropsGet("object::mdelay.enabled", out sValue);
                if (sValue == "true" || sValue == "1")
                    checkBoxDelay.Checked = true;
                else
                    checkBoxDelay.Checked = false;

                pProps.PropsGet("object::mdelay.buffer_duration", out sValue);  // The value in seconds

                pProps.PropsGet("object::mdelay.quality", out sValue);
                numericDelayQuality.Value = Decimal.Parse(sValue);

                pProps.PropsGet("object::mdelay.available", out sValue);
                numericDelayTime.Value = Decimal.Parse(sValue);
                trackBarSeek.Minimum = -1 * (int)Decimal.Parse(sValue);
                trackBarSeek.TickFrequency = -1 * trackBarSeek.Minimum / 20;

                pProps.PropsGet("object::mdelay.time", out sValue);
                numericPos.Value = Decimal.Parse(sValue);
                trackBarSeek.Value = -1 * Int32.Parse(sValue);
            }
            catch (System.Exception) { }
        }

        private void numericDelayQuality_ValueChanged(object sender, EventArgs e)
        {
            try
            {
                IMProps pProps = (IMProps)m_objMLive;
                pProps.PropsSet("object::mdelay.quality", numericDelayQuality.Value.ToString());
            }
            catch (System.Exception) { }
        }

        private void numericPos_ValueChanged(object sender, EventArgs e)
        {
            try
            {
                IMProps pProps = (IMProps)m_objMLive;
                pProps.PropsSet("object::mdelay.time", numericPos.Value.ToString("0.0"));
                trackBarSeek.Value = -1 * (int)numericPos.Value;
            }
            catch (System.Exception) { }
        }

        private void trackBarSeek_Scroll(object sender, EventArgs e)
        {
            try
            {
                IMProps pProps = (IMProps)m_objMLive;
                pProps.PropsSet("object::mdelay.time", (-1 * trackBarSeek.Value).ToString());
                numericPos.Value = -1 * trackBarSeek.Value;
            }
            catch (System.Exception) { }
        }

        private void buttonNDIRefresh_Click(object sender, EventArgs e)
        {
            FillCombo("video::line-in", comboBoxVL);
        }


        private void broadcast_Click(object sender, EventArgs e)
        {
            BroadcastVideoForm video = new BroadcastVideoForm();
            video.Show();
        }
    }
}

BroadcastVideoForm.cs代码 -

using System;
using System.Windows.Forms;

namespace NetworkExample
{
    using VisioForge.Types;

    public partial class BroadcastVideoForm : Form
    {
        public BroadcastVideoForm()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            foreach (string device in VideoCapture1.Video_CaptureDevices)
            {
                cbVideoInputDevice1.Items.Add(device);
            }

            if (cbVideoInputDevice1.Items.Count > 0)
            {
                cbVideoInputDevice1.SelectedIndex = 0;
            }

            VideoCapture1.Mode = VFVideoCaptureMode.VideoPreview;
            VideoCapture1.Video_CaptureDevice = cbVideoInputDevice1.Text;
            VideoCapture1.Video_CaptureDevice_Format_UseBest = true;
            VideoCapture1.Audio_PlayAudio = false;

            VideoCapture1.OnError += delegate (object o, ErrorsEventArgs args)
            {
                edLog1.Text += args.Message + Environment.NewLine;
            };

            edLog1.Text = "";
            //VideoCapture1.Debug_Dir = "c:\\vf\\_rtsp1";
            //VideoCapture1.Debug_Mode = true;

            // network streaming
            VideoCapture1.Network_Streaming_Enabled = true;
            VideoCapture1.Network_Streaming_Format = VFNetworkStreamingFormat.RTSP_H264_AAC_SW;
            VideoCapture1.Network_Streaming_URL = "rtsp://localhost:" + edPort1.Text + "/vfstream";
            VideoCapture1.Network_Streaming_Audio_Enabled = false;

            // Video H264 settings
            VideoCapture1.MP4_Video_Profile = VFH264Profile.ProfileBaseline;
            VideoCapture1.MP4_Video_Level = VFH264Level.LevelAuto;
            VideoCapture1.MP4_Video_Target_Usage = VFH264TargetUsage.Auto;
            VideoCapture1.MP4_Video_PictureType = VFH264PictureType.Auto;
            VideoCapture1.MP4_Video_RateControl = VFH264RateControl.CBR;
            VideoCapture1.MP4_Video_MBEncoding = VFH264MBEncoding.CABAC;
            VideoCapture1.MP4_Video_GOP = true;
            VideoCapture1.MP4_Video_BitrateAuto = true;
            VideoCapture1.MP4_Video_IDR_Period = 15;
            VideoCapture1.MP4_Video_P_Period = 3;
            VideoCapture1.MP4_Video_Bitrate = 2000;

            // Audio AAC settings
            VideoCapture1.MP4_Audio_AAC_Bitrate = 192;
            VideoCapture1.MP4_Audio_AAC_Version = VFAACVersion.MPEG4;
            VideoCapture1.MP4_Audio_AAC_Output = VFAACOutput.ADTS;
            VideoCapture1.MP4_Audio_AAC_Object = VFAACObject.Main;

            VideoCapture1.Start();
        }


        private void btStop1_Click(object sender, EventArgs e)
        {
            VideoCapture1.Stop();
        } 
    }
}

0 个答案:

没有答案