MyAV.cs
上传用户:lqb116
上传日期:2014-04-04
资源大小:2712k
文件大小:16k
- using System;
- using System.Collections;
- using System.ComponentModel;
- using System.Drawing;
- using System.Data;
- using System.Windows.Forms;
- using LanMsg.AV;
- using System.Net;
- namespace LanMsg
- {
- /// <summary>
- /// MyAV 的摘要说明。
- /// </summary>
- public class MyAV : System.Windows.Forms.UserControl
- {
- private System.ComponentModel.IContainer components;
- private LanMsg.AV.AV av;
- private Mixer mixer;
- private Mixer.MixerControlDetail indtl,outdtl;
- private System.Windows.Forms.TrackBar trackBarOut;
- private System.Windows.Forms.TrackBar trackBarIn;
-
- public System.Net.IPAddress serverIp;//对方的公网IP
- public int serverPort;//对方的公网UDP端口
- private DevComponents.DotNetBar.Bar bar4;
- private DevComponents.DotNetBar.ButtonItem buttonItemNotice;
- private DevComponents.DotNetBar.LabelItem labelItem1;
- private System.Windows.Forms.Panel panel1;
- private System.Windows.Forms.Panel panelSelfAV;
- private System.Windows.Forms.PictureBox pictureBox2;
- private System.Windows.Forms.PictureBox pictureBox1;
- private System.Windows.Forms.Label label1;
- private System.Windows.Forms.Label label2;
- private System.Windows.Forms.GroupBox groupBox1;
- private System.Windows.Forms.LinkLabel linkLabelCancelAV;
- public System.Windows.Forms.LinkLabel linkLabelReceve;
- public System.Windows.Forms.Timer timer1;//对方的公网端口
- public LanMsg.Controls.ClassMsg msg;
- private System.Windows.Forms.PictureBox picLocal;
- private System.Windows.Forms.PictureBox picRemoteBack;
- private System.Windows.Forms.PictureBox picRemote;//发送请求的消息
- public int colorChange=0;
- #region AV传输事件
-
- public delegate void AVReceveEventHandler(object sender,bool isSelf);//接收AV对话事件
- public event AVReceveEventHandler AVReceve;
- public delegate void AVCancelEventHandler(object sender,bool isSelf);//取消AV对话事件
- public event AVCancelEventHandler AVCancel;
-
- #endregion
- public MyAV()
- {
- // 该调用是 Windows.Forms 窗体设计器所必需的。
- InitializeComponent();
- InitializeMixing();
-
- // TODO: 在 InitializeComponent 调用后添加任何初始化
- }
- private void InitializeMixing()//初始化声卡麦克风
- {
- mixer=new Mixer(this);
- mixer.MixerControlChange +=new EventHandler(mixer_MixerControlChange);
-
- this.outdtl=new Mixer.MixerControlDetail(mixer,Mixer.MIXERLINE_COMPONENTTYPE_DST_SPEAKERS);
- this.trackBarOut.Minimum=this.outdtl.Min;
- this.trackBarOut.Maximum=this.outdtl.Max;
- this.trackBarOut.Value=this.outdtl.Volume;
-
- this.indtl=new Mixer.MixerControlDetail(mixer,Mixer.MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE);
- this.trackBarIn.Minimum=indtl.Min;
- this.trackBarIn.Maximum=indtl.Max;
- this.trackBarIn.Value=this.indtl.Volume;
- }
- /// <summary>
- /// 清理所有正在使用的资源。
- /// </summary>
- protected override void Dispose( bool disposing )
- {
- if( disposing )
- {
- mixer.MixerControlChange -=new EventHandler(mixer_MixerControlChange);
- try
- {
- this.av.AVChanel.Close();
- }
- catch{}
- try
- {
- this.av.Chanel.Close();
- }
- catch{}
- try
- {
- this.av.Dispose();
- }
- catch{}
- if(components != null)
- {
- components.Dispose();
- }
- }
- base.Dispose( disposing );
- }
- #region 组件设计器生成的代码
- /// <summary>
- /// 设计器支持所需的方法 - 不要使用代码编辑器
- /// 修改此方法的内容。
- /// </summary>
- private void InitializeComponent()
- {
- this.components = new System.ComponentModel.Container();
- System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(MyAV));
- this.trackBarIn = new System.Windows.Forms.TrackBar();
- this.trackBarOut = new System.Windows.Forms.TrackBar();
- this.bar4 = new DevComponents.DotNetBar.Bar();
- this.buttonItemNotice = new DevComponents.DotNetBar.ButtonItem();
- this.labelItem1 = new DevComponents.DotNetBar.LabelItem();
- this.panel1 = new System.Windows.Forms.Panel();
- this.picRemote = new System.Windows.Forms.PictureBox();
- this.picRemoteBack = new System.Windows.Forms.PictureBox();
- this.pictureBox2 = new System.Windows.Forms.PictureBox();
- this.panelSelfAV = new System.Windows.Forms.Panel();
- this.picLocal = new System.Windows.Forms.PictureBox();
- this.pictureBox1 = new System.Windows.Forms.PictureBox();
- this.label2 = new System.Windows.Forms.Label();
- this.label1 = new System.Windows.Forms.Label();
- this.linkLabelReceve = new System.Windows.Forms.LinkLabel();
- this.linkLabelCancelAV = new System.Windows.Forms.LinkLabel();
- this.groupBox1 = new System.Windows.Forms.GroupBox();
- this.timer1 = new System.Windows.Forms.Timer(this.components);
- ((System.ComponentModel.ISupportInitialize)(this.trackBarIn)).BeginInit();
- ((System.ComponentModel.ISupportInitialize)(this.trackBarOut)).BeginInit();
- ((System.ComponentModel.ISupportInitialize)(this.bar4)).BeginInit();
- this.panel1.SuspendLayout();
- this.panelSelfAV.SuspendLayout();
- this.groupBox1.SuspendLayout();
- this.SuspendLayout();
- //
- // trackBarIn
- //
- this.trackBarIn.Location = new System.Drawing.Point(56, 52);
- this.trackBarIn.Name = "trackBarIn";
- this.trackBarIn.Size = new System.Drawing.Size(56, 42);
- this.trackBarIn.TabIndex = 2;
- this.trackBarIn.Scroll += new System.EventHandler(this.trackBarOut_Scroll);
- //
- // trackBarOut
- //
- this.trackBarOut.Location = new System.Drawing.Point(56, 12);
- this.trackBarOut.Name = "trackBarOut";
- this.trackBarOut.Size = new System.Drawing.Size(56, 42);
- this.trackBarOut.TabIndex = 3;
- this.trackBarOut.Scroll += new System.EventHandler(this.trackBarOut_Scroll);
- //
- // bar4
- //
- this.bar4.Dock = System.Windows.Forms.DockStyle.Top;
- this.bar4.Items.AddRange(new DevComponents.DotNetBar.BaseItem[] {
- this.buttonItemNotice,
- this.labelItem1});
- this.bar4.Location = new System.Drawing.Point(0, 0);
- this.bar4.Name = "bar4";
- this.bar4.Size = new System.Drawing.Size(296, 25);
- this.bar4.Stretch = true;
- this.bar4.Style = DevComponents.DotNetBar.eDotNetBarStyle.Office2007;
- this.bar4.TabIndex = 42;
- this.bar4.TabStop = false;
- this.bar4.Text = "bar4";
- //
- // buttonItemNotice
- //
- this.buttonItemNotice.Icon = ((System.Drawing.Icon)(resources.GetObject("buttonItemNotice.Icon")));
- this.buttonItemNotice.Name = "buttonItemNotice";
- //
- // labelItem1
- //
- this.labelItem1.BorderType = DevComponents.DotNetBar.eBorderType.None;
- this.labelItem1.Name = "labelItem1";
- this.labelItem1.Text = "对方视频";
- //
- // panel1
- //
- this.panel1.BackColor = System.Drawing.Color.Cyan;
- this.panel1.Controls.Add(this.picRemote);
- this.panel1.Controls.Add(this.picRemoteBack);
- this.panel1.Controls.Add(this.pictureBox2);
- this.panel1.Dock = System.Windows.Forms.DockStyle.Top;
- this.panel1.Location = new System.Drawing.Point(0, 25);
- this.panel1.Name = "panel1";
- this.panel1.Size = new System.Drawing.Size(296, 223);
- this.panel1.TabIndex = 43;
- //
- // picRemote
- //
- this.picRemote.BackColor = System.Drawing.Color.WhiteSmoke;
- this.picRemote.Location = new System.Drawing.Point(68, 51);
- this.picRemote.Name = "picRemote";
- this.picRemote.Size = new System.Drawing.Size(160, 120);
- this.picRemote.TabIndex = 10;
- this.picRemote.TabStop = false;
- //
- // picRemoteBack
- //
- this.picRemoteBack.BackColor = System.Drawing.Color.WhiteSmoke;
- this.picRemoteBack.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
- this.picRemoteBack.Location = new System.Drawing.Point(8, 8);
- this.picRemoteBack.Name = "picRemoteBack";
- this.picRemoteBack.Size = new System.Drawing.Size(280, 208);
- this.picRemoteBack.SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage;
- this.picRemoteBack.TabIndex = 8;
- this.picRemoteBack.TabStop = false;
- //
- // pictureBox2
- //
- this.pictureBox2.Dock = System.Windows.Forms.DockStyle.Fill;
- this.pictureBox2.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox2.Image")));
- this.pictureBox2.Location = new System.Drawing.Point(0, 0);
- this.pictureBox2.Name = "pictureBox2";
- this.pictureBox2.Size = new System.Drawing.Size(296, 223);
- this.pictureBox2.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
- this.pictureBox2.TabIndex = 9;
- this.pictureBox2.TabStop = false;
- //
- // panelSelfAV
- //
- this.panelSelfAV.BackColor = System.Drawing.Color.Cyan;
- this.panelSelfAV.Controls.Add(this.picLocal);
- this.panelSelfAV.Controls.Add(this.pictureBox1);
- this.panelSelfAV.Location = new System.Drawing.Point(123, 248);
- this.panelSelfAV.Name = "panelSelfAV";
- this.panelSelfAV.Size = new System.Drawing.Size(170, 134);
- this.panelSelfAV.TabIndex = 45;
- //
- // picLocal
- //
- this.picLocal.BackColor = System.Drawing.Color.WhiteSmoke;
- this.picLocal.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
- this.picLocal.Location = new System.Drawing.Point(5, 8);
- this.picLocal.Name = "picLocal";
- this.picLocal.Size = new System.Drawing.Size(160, 120);
- this.picLocal.TabIndex = 8;
- this.picLocal.TabStop = false;
- //
- // pictureBox1
- //
- this.pictureBox1.Dock = System.Windows.Forms.DockStyle.Fill;
- this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
- this.pictureBox1.Location = new System.Drawing.Point(0, 0);
- this.pictureBox1.Name = "pictureBox1";
- this.pictureBox1.Size = new System.Drawing.Size(170, 134);
- this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
- this.pictureBox1.TabIndex = 47;
- this.pictureBox1.TabStop = false;
- //
- // label2
- //
- this.label2.AutoSize = true;
- this.label2.ForeColor = System.Drawing.Color.Purple;
- this.label2.Location = new System.Drawing.Point(8, 57);
- this.label2.Name = "label2";
- this.label2.Size = new System.Drawing.Size(54, 17);
- this.label2.TabIndex = 5;
- this.label2.Text = "麦克风:";
- //
- // label1
- //
- this.label1.AutoSize = true;
- this.label1.ForeColor = System.Drawing.Color.Purple;
- this.label1.Location = new System.Drawing.Point(8, 20);
- this.label1.Name = "label1";
- this.label1.Size = new System.Drawing.Size(42, 17);
- this.label1.TabIndex = 4;
- this.label1.Text = "声卡:";
- //
- // linkLabelReceve
- //
- this.linkLabelReceve.AutoSize = true;
- this.linkLabelReceve.Font = new System.Drawing.Font("宋体", 11F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
- this.linkLabelReceve.ForeColor = System.Drawing.Color.Purple;
- this.linkLabelReceve.LinkColor = System.Drawing.Color.DarkRed;
- this.linkLabelReceve.Location = new System.Drawing.Point(3, 359);
- this.linkLabelReceve.Name = "linkLabelReceve";
- this.linkLabelReceve.Size = new System.Drawing.Size(66, 20);
- this.linkLabelReceve.TabIndex = 47;
- this.linkLabelReceve.TabStop = true;
- this.linkLabelReceve.Text = "接收视频";
- this.linkLabelReceve.Visible = false;
- this.linkLabelReceve.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.linkLabelReceve_LinkClicked);
- //
- // linkLabelCancelAV
- //
- this.linkLabelCancelAV.AutoSize = true;
- this.linkLabelCancelAV.Font = new System.Drawing.Font("宋体", 11F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
- this.linkLabelCancelAV.ForeColor = System.Drawing.Color.Purple;
- this.linkLabelCancelAV.LinkColor = System.Drawing.Color.DarkRed;
- this.linkLabelCancelAV.Location = new System.Drawing.Point(75, 359);
- this.linkLabelCancelAV.Name = "linkLabelCancelAV";
- this.linkLabelCancelAV.Size = new System.Drawing.Size(36, 20);
- this.linkLabelCancelAV.TabIndex = 48;
- this.linkLabelCancelAV.TabStop = true;
- this.linkLabelCancelAV.Text = "取消";
- this.linkLabelCancelAV.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.linkLabelCancelAV_LinkClicked);
- //
- // groupBox1
- //
- this.groupBox1.Controls.Add(this.label1);
- this.groupBox1.Controls.Add(this.trackBarOut);
- this.groupBox1.Controls.Add(this.label2);
- this.groupBox1.Controls.Add(this.trackBarIn);
- this.groupBox1.FlatStyle = System.Windows.Forms.FlatStyle.System;
- this.groupBox1.ForeColor = System.Drawing.Color.Purple;
- this.groupBox1.Location = new System.Drawing.Point(3, 251);
- this.groupBox1.Name = "groupBox1";
- this.groupBox1.Size = new System.Drawing.Size(117, 96);
- this.groupBox1.TabIndex = 49;
- this.groupBox1.TabStop = false;
- this.groupBox1.Text = "音量调整";
- //
- // timer1
- //
- this.timer1.Interval = 56000;
- this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
- //
- // MyAV
- //
- this.BackColor = System.Drawing.Color.Cyan;
- this.Controls.Add(this.groupBox1);
- this.Controls.Add(this.linkLabelCancelAV);
- this.Controls.Add(this.linkLabelReceve);
- this.Controls.Add(this.panelSelfAV);
- this.Controls.Add(this.panel1);
- this.Controls.Add(this.bar4);
- this.Name = "MyAV";
- this.Size = new System.Drawing.Size(296, 392);
- ((System.ComponentModel.ISupportInitialize)(this.trackBarIn)).EndInit();
- ((System.ComponentModel.ISupportInitialize)(this.trackBarOut)).EndInit();
- ((System.ComponentModel.ISupportInitialize)(this.bar4)).EndInit();
- this.panel1.ResumeLayout(false);
- this.panelSelfAV.ResumeLayout(false);
- this.groupBox1.ResumeLayout(false);
- this.ResumeLayout(false);
- }
- #endregion
- public void SetParameter(System.Net.IPAddress ip,int port,LanMsg.Controls.ClassMsg msg)
- {
- this.serverIp =ip;
- this.serverPort=port;
- this.msg=msg;
- this.ReadyConnect();
- }
- public void ColseAV()//关闭当前正在进行的视频对话
- {
- IsAV=false;
- this.linkLabelReceve.Visible=false;
- try
- {
- this.av.AVChanel.Close();
- }
- catch{}
- try
- {
- this.av.Chanel.Close();
- }
- catch{}
- try
- {
- this.av.Dispose();
- }
- catch{}
- }
- private void mixer_MixerControlChange(object sender, EventArgs e)
- {
- this.trackBarIn.Value=this.indtl.Volume;
- this.trackBarOut.Value=this.outdtl.Volume;
- }
- private void trackBarOut_Scroll(object sender, System.EventArgs e)
- {
- this.outdtl.Volume=this.trackBarOut.Value;
- this.indtl.Volume=this.trackBarIn.Value;
- }
- public void Initialize()//初始化视频图片与sock
- {
- if(av!=null)this.av.Dispose();
- this.av=new AV.AV(this.picLocal ,this.picRemote);
- this.av.IniChanel();
- }
- public AV.AV AV
- {
- get{return this.av;}
- }
- private bool IsAV=false;//表示当前是否正在视频中
- private void ReadyConnect()//准备连接,并告诉对方本地IP与端口,让其联接
- {
- if(!IsAV)
- {
- Initialize();
- this.av.ReadyStrat(new ClassSerializers().SerializeBinary(this.msg).ToArray() ,this.serverIp ,this.serverPort);
- }
- else
- {
- this.av.AVChanel.Close();
- this.av.Chanel.Close();
- }
- IsAV=!IsAV;
- //UDP打洞
- }
- public void Connect(string ip,int port)//连接到对方并且开始发送AV数据
- {
- this.av.IniAudio();
- this.av.IniVideo();
- this.av.Start(ip,port);
- }
- public void Stop()
- {
- if(this.av!=null)this.av.Dispose();
- }
- private void timer1_Tick(object sender, System.EventArgs e)
- {
- this.av.UDPBurrowNat(this.serverIp ,this.serverPort );//UDP打洞
- }
- private void linkLabelCancelAV_LinkClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e)
- {
- IsAV=false;
- this.linkLabelReceve.Visible=false;
- try
- {
- this.av.AVChanel.Close();
- this.av.Chanel.Close();
- this.av.Dispose();
- }
- catch{}
- this.AVCancel(this,true);//触发终止事件
- }
- private void linkLabelReceve_LinkClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e)
- {
- this.linkLabelReceve.Visible=false;
- this.AVReceve(this,true);//触发AV接收事件
- Initialize();//初始化视频设备
- this.av.IniAudio();//初始化音频设备
- this.av.IniVideo();//初始化视频设备
- this.AV.BeginAV(new ClassSerializers().SerializeBinary(new LanMsg.Controls.ClassMsg(14,this.msg.ID,null)).ToArray(),this.serverIp,this.serverPort);//告诉对方可以开始视频
- }
- }
- }