MyAV.cs
上传用户:lqb116
上传日期:2014-04-04
资源大小:2712k
文件大小:16k
源码类别:

P2P编程

开发平台:

C#

  1. using System;
  2. using System.Collections;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Data;
  6. using System.Windows.Forms;
  7. using LanMsg.AV;
  8. using System.Net;
  9. namespace LanMsg
  10. {
  11. /// <summary>
  12. /// MyAV 的摘要说明。
  13. /// </summary>
  14. public class MyAV : System.Windows.Forms.UserControl
  15. {
  16. private System.ComponentModel.IContainer components;
  17. private LanMsg.AV.AV av;
  18. private Mixer mixer;
  19. private Mixer.MixerControlDetail indtl,outdtl;
  20. private System.Windows.Forms.TrackBar trackBarOut;
  21. private System.Windows.Forms.TrackBar trackBarIn;
  22.         
  23. public System.Net.IPAddress serverIp;//对方的公网IP
  24. public int  serverPort;//对方的公网UDP端口
  25. private DevComponents.DotNetBar.Bar bar4;
  26. private DevComponents.DotNetBar.ButtonItem buttonItemNotice;
  27. private DevComponents.DotNetBar.LabelItem labelItem1;
  28. private System.Windows.Forms.Panel panel1;
  29. private System.Windows.Forms.Panel panelSelfAV;
  30. private System.Windows.Forms.PictureBox pictureBox2;
  31. private System.Windows.Forms.PictureBox pictureBox1;
  32. private System.Windows.Forms.Label label1;
  33. private System.Windows.Forms.Label label2;
  34. private System.Windows.Forms.GroupBox groupBox1;
  35. private System.Windows.Forms.LinkLabel linkLabelCancelAV;
  36. public System.Windows.Forms.LinkLabel linkLabelReceve;
  37. public System.Windows.Forms.Timer timer1;//对方的公网端口
  38.         public LanMsg.Controls.ClassMsg msg;
  39. private System.Windows.Forms.PictureBox picLocal;
  40. private System.Windows.Forms.PictureBox picRemoteBack;
  41. private System.Windows.Forms.PictureBox picRemote;//发送请求的消息
  42.         public int colorChange=0;
  43. #region  AV传输事件
  44. public delegate void AVReceveEventHandler(object sender,bool isSelf);//接收AV对话事件
  45. public  event AVReceveEventHandler AVReceve; 
  46. public delegate void AVCancelEventHandler(object sender,bool isSelf);//取消AV对话事件
  47. public  event AVCancelEventHandler AVCancel; 
  48.     
  49. #endregion
  50. public MyAV()
  51. {
  52. // 该调用是 Windows.Forms 窗体设计器所必需的。
  53. InitializeComponent();
  54. InitializeMixing();
  55.  
  56. // TODO: 在 InitializeComponent 调用后添加任何初始化
  57. }
  58. private void InitializeMixing()//初始化声卡麦克风
  59. {
  60. mixer=new Mixer(this);
  61. mixer.MixerControlChange +=new EventHandler(mixer_MixerControlChange); 
  62. this.outdtl=new Mixer.MixerControlDetail(mixer,Mixer.MIXERLINE_COMPONENTTYPE_DST_SPEAKERS);
  63. this.trackBarOut.Minimum=this.outdtl.Min;
  64. this.trackBarOut.Maximum=this.outdtl.Max;
  65. this.trackBarOut.Value=this.outdtl.Volume;
  66. this.indtl=new Mixer.MixerControlDetail(mixer,Mixer.MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE);
  67. this.trackBarIn.Minimum=indtl.Min;
  68. this.trackBarIn.Maximum=indtl.Max;
  69. this.trackBarIn.Value=this.indtl.Volume;
  70. }
  71. /// <summary> 
  72. /// 清理所有正在使用的资源。
  73. /// </summary>
  74. protected override void Dispose( bool disposing )
  75. {
  76. if( disposing )
  77. {
  78. mixer.MixerControlChange -=new EventHandler(mixer_MixerControlChange);
  79. try
  80. {
  81. this.av.AVChanel.Close();
  82. }
  83. catch{}
  84. try
  85. {
  86. this.av.Chanel.Close();
  87. }
  88. catch{}
  89. try
  90. {
  91. this.av.Dispose();
  92. }
  93. catch{}
  94. if(components != null)
  95. {
  96. components.Dispose();
  97. }
  98. }
  99. base.Dispose( disposing );
  100. }
  101. #region 组件设计器生成的代码
  102. /// <summary> 
  103. /// 设计器支持所需的方法 - 不要使用代码编辑器 
  104. /// 修改此方法的内容。
  105. /// </summary>
  106. private void InitializeComponent()
  107. {
  108. this.components = new System.ComponentModel.Container();
  109. System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(MyAV));
  110. this.trackBarIn = new System.Windows.Forms.TrackBar();
  111. this.trackBarOut = new System.Windows.Forms.TrackBar();
  112. this.bar4 = new DevComponents.DotNetBar.Bar();
  113. this.buttonItemNotice = new DevComponents.DotNetBar.ButtonItem();
  114. this.labelItem1 = new DevComponents.DotNetBar.LabelItem();
  115. this.panel1 = new System.Windows.Forms.Panel();
  116. this.picRemote = new System.Windows.Forms.PictureBox();
  117. this.picRemoteBack = new System.Windows.Forms.PictureBox();
  118. this.pictureBox2 = new System.Windows.Forms.PictureBox();
  119. this.panelSelfAV = new System.Windows.Forms.Panel();
  120. this.picLocal = new System.Windows.Forms.PictureBox();
  121. this.pictureBox1 = new System.Windows.Forms.PictureBox();
  122. this.label2 = new System.Windows.Forms.Label();
  123. this.label1 = new System.Windows.Forms.Label();
  124. this.linkLabelReceve = new System.Windows.Forms.LinkLabel();
  125. this.linkLabelCancelAV = new System.Windows.Forms.LinkLabel();
  126. this.groupBox1 = new System.Windows.Forms.GroupBox();
  127. this.timer1 = new System.Windows.Forms.Timer(this.components);
  128. ((System.ComponentModel.ISupportInitialize)(this.trackBarIn)).BeginInit();
  129. ((System.ComponentModel.ISupportInitialize)(this.trackBarOut)).BeginInit();
  130. ((System.ComponentModel.ISupportInitialize)(this.bar4)).BeginInit();
  131. this.panel1.SuspendLayout();
  132. this.panelSelfAV.SuspendLayout();
  133. this.groupBox1.SuspendLayout();
  134. this.SuspendLayout();
  135. // 
  136. // trackBarIn
  137. // 
  138. this.trackBarIn.Location = new System.Drawing.Point(56, 52);
  139. this.trackBarIn.Name = "trackBarIn";
  140. this.trackBarIn.Size = new System.Drawing.Size(56, 42);
  141. this.trackBarIn.TabIndex = 2;
  142. this.trackBarIn.Scroll += new System.EventHandler(this.trackBarOut_Scroll);
  143. // 
  144. // trackBarOut
  145. // 
  146. this.trackBarOut.Location = new System.Drawing.Point(56, 12);
  147. this.trackBarOut.Name = "trackBarOut";
  148. this.trackBarOut.Size = new System.Drawing.Size(56, 42);
  149. this.trackBarOut.TabIndex = 3;
  150. this.trackBarOut.Scroll += new System.EventHandler(this.trackBarOut_Scroll);
  151. // 
  152. // bar4
  153. // 
  154. this.bar4.Dock = System.Windows.Forms.DockStyle.Top;
  155. this.bar4.Items.AddRange(new DevComponents.DotNetBar.BaseItem[] {
  156. this.buttonItemNotice,
  157. this.labelItem1});
  158. this.bar4.Location = new System.Drawing.Point(0, 0);
  159. this.bar4.Name = "bar4";
  160. this.bar4.Size = new System.Drawing.Size(296, 25);
  161. this.bar4.Stretch = true;
  162. this.bar4.Style = DevComponents.DotNetBar.eDotNetBarStyle.Office2007;
  163. this.bar4.TabIndex = 42;
  164. this.bar4.TabStop = false;
  165. this.bar4.Text = "bar4";
  166. // 
  167. // buttonItemNotice
  168. // 
  169. this.buttonItemNotice.Icon = ((System.Drawing.Icon)(resources.GetObject("buttonItemNotice.Icon")));
  170. this.buttonItemNotice.Name = "buttonItemNotice";
  171. // 
  172. // labelItem1
  173. // 
  174. this.labelItem1.BorderType = DevComponents.DotNetBar.eBorderType.None;
  175. this.labelItem1.Name = "labelItem1";
  176. this.labelItem1.Text = "对方视频";
  177. // 
  178. // panel1
  179. // 
  180. this.panel1.BackColor = System.Drawing.Color.Cyan;
  181. this.panel1.Controls.Add(this.picRemote);
  182. this.panel1.Controls.Add(this.picRemoteBack);
  183. this.panel1.Controls.Add(this.pictureBox2);
  184. this.panel1.Dock = System.Windows.Forms.DockStyle.Top;
  185. this.panel1.Location = new System.Drawing.Point(0, 25);
  186. this.panel1.Name = "panel1";
  187. this.panel1.Size = new System.Drawing.Size(296, 223);
  188. this.panel1.TabIndex = 43;
  189. // 
  190. // picRemote
  191. // 
  192. this.picRemote.BackColor = System.Drawing.Color.WhiteSmoke;
  193. this.picRemote.Location = new System.Drawing.Point(68, 51);
  194. this.picRemote.Name = "picRemote";
  195. this.picRemote.Size = new System.Drawing.Size(160, 120);
  196. this.picRemote.TabIndex = 10;
  197. this.picRemote.TabStop = false;
  198. // 
  199. // picRemoteBack
  200. // 
  201. this.picRemoteBack.BackColor = System.Drawing.Color.WhiteSmoke;
  202. this.picRemoteBack.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
  203. this.picRemoteBack.Location = new System.Drawing.Point(8, 8);
  204. this.picRemoteBack.Name = "picRemoteBack";
  205. this.picRemoteBack.Size = new System.Drawing.Size(280, 208);
  206. this.picRemoteBack.SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage;
  207. this.picRemoteBack.TabIndex = 8;
  208. this.picRemoteBack.TabStop = false;
  209. // 
  210. // pictureBox2
  211. // 
  212. this.pictureBox2.Dock = System.Windows.Forms.DockStyle.Fill;
  213. this.pictureBox2.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox2.Image")));
  214. this.pictureBox2.Location = new System.Drawing.Point(0, 0);
  215. this.pictureBox2.Name = "pictureBox2";
  216. this.pictureBox2.Size = new System.Drawing.Size(296, 223);
  217. this.pictureBox2.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
  218. this.pictureBox2.TabIndex = 9;
  219. this.pictureBox2.TabStop = false;
  220. // 
  221. // panelSelfAV
  222. // 
  223. this.panelSelfAV.BackColor = System.Drawing.Color.Cyan;
  224. this.panelSelfAV.Controls.Add(this.picLocal);
  225. this.panelSelfAV.Controls.Add(this.pictureBox1);
  226. this.panelSelfAV.Location = new System.Drawing.Point(123, 248);
  227. this.panelSelfAV.Name = "panelSelfAV";
  228. this.panelSelfAV.Size = new System.Drawing.Size(170, 134);
  229. this.panelSelfAV.TabIndex = 45;
  230. // 
  231. // picLocal
  232. // 
  233. this.picLocal.BackColor = System.Drawing.Color.WhiteSmoke;
  234. this.picLocal.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
  235. this.picLocal.Location = new System.Drawing.Point(5, 8);
  236. this.picLocal.Name = "picLocal";
  237. this.picLocal.Size = new System.Drawing.Size(160, 120);
  238. this.picLocal.TabIndex = 8;
  239. this.picLocal.TabStop = false;
  240. // 
  241. // pictureBox1
  242. // 
  243. this.pictureBox1.Dock = System.Windows.Forms.DockStyle.Fill;
  244. this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
  245. this.pictureBox1.Location = new System.Drawing.Point(0, 0);
  246. this.pictureBox1.Name = "pictureBox1";
  247. this.pictureBox1.Size = new System.Drawing.Size(170, 134);
  248. this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
  249. this.pictureBox1.TabIndex = 47;
  250. this.pictureBox1.TabStop = false;
  251. // 
  252. // label2
  253. // 
  254. this.label2.AutoSize = true;
  255. this.label2.ForeColor = System.Drawing.Color.Purple;
  256. this.label2.Location = new System.Drawing.Point(8, 57);
  257. this.label2.Name = "label2";
  258. this.label2.Size = new System.Drawing.Size(54, 17);
  259. this.label2.TabIndex = 5;
  260. this.label2.Text = "麦克风:";
  261. // 
  262. // label1
  263. // 
  264. this.label1.AutoSize = true;
  265. this.label1.ForeColor = System.Drawing.Color.Purple;
  266. this.label1.Location = new System.Drawing.Point(8, 20);
  267. this.label1.Name = "label1";
  268. this.label1.Size = new System.Drawing.Size(42, 17);
  269. this.label1.TabIndex = 4;
  270. this.label1.Text = "声卡:";
  271. // 
  272. // linkLabelReceve
  273. // 
  274. this.linkLabelReceve.AutoSize = true;
  275. this.linkLabelReceve.Font = new System.Drawing.Font("宋体", 11F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
  276. this.linkLabelReceve.ForeColor = System.Drawing.Color.Purple;
  277. this.linkLabelReceve.LinkColor = System.Drawing.Color.DarkRed;
  278. this.linkLabelReceve.Location = new System.Drawing.Point(3, 359);
  279. this.linkLabelReceve.Name = "linkLabelReceve";
  280. this.linkLabelReceve.Size = new System.Drawing.Size(66, 20);
  281. this.linkLabelReceve.TabIndex = 47;
  282. this.linkLabelReceve.TabStop = true;
  283. this.linkLabelReceve.Text = "接收视频";
  284. this.linkLabelReceve.Visible = false;
  285. this.linkLabelReceve.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.linkLabelReceve_LinkClicked);
  286. // 
  287. // linkLabelCancelAV
  288. // 
  289. this.linkLabelCancelAV.AutoSize = true;
  290. this.linkLabelCancelAV.Font = new System.Drawing.Font("宋体", 11F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
  291. this.linkLabelCancelAV.ForeColor = System.Drawing.Color.Purple;
  292. this.linkLabelCancelAV.LinkColor = System.Drawing.Color.DarkRed;
  293. this.linkLabelCancelAV.Location = new System.Drawing.Point(75, 359);
  294. this.linkLabelCancelAV.Name = "linkLabelCancelAV";
  295. this.linkLabelCancelAV.Size = new System.Drawing.Size(36, 20);
  296. this.linkLabelCancelAV.TabIndex = 48;
  297. this.linkLabelCancelAV.TabStop = true;
  298. this.linkLabelCancelAV.Text = "取消";
  299. this.linkLabelCancelAV.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.linkLabelCancelAV_LinkClicked);
  300. // 
  301. // groupBox1
  302. // 
  303. this.groupBox1.Controls.Add(this.label1);
  304. this.groupBox1.Controls.Add(this.trackBarOut);
  305. this.groupBox1.Controls.Add(this.label2);
  306. this.groupBox1.Controls.Add(this.trackBarIn);
  307. this.groupBox1.FlatStyle = System.Windows.Forms.FlatStyle.System;
  308. this.groupBox1.ForeColor = System.Drawing.Color.Purple;
  309. this.groupBox1.Location = new System.Drawing.Point(3, 251);
  310. this.groupBox1.Name = "groupBox1";
  311. this.groupBox1.Size = new System.Drawing.Size(117, 96);
  312. this.groupBox1.TabIndex = 49;
  313. this.groupBox1.TabStop = false;
  314. this.groupBox1.Text = "音量调整";
  315. // 
  316. // timer1
  317. // 
  318. this.timer1.Interval = 56000;
  319. this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
  320. // 
  321. // MyAV
  322. // 
  323. this.BackColor = System.Drawing.Color.Cyan;
  324. this.Controls.Add(this.groupBox1);
  325. this.Controls.Add(this.linkLabelCancelAV);
  326. this.Controls.Add(this.linkLabelReceve);
  327. this.Controls.Add(this.panelSelfAV);
  328. this.Controls.Add(this.panel1);
  329. this.Controls.Add(this.bar4);
  330. this.Name = "MyAV";
  331. this.Size = new System.Drawing.Size(296, 392);
  332. ((System.ComponentModel.ISupportInitialize)(this.trackBarIn)).EndInit();
  333. ((System.ComponentModel.ISupportInitialize)(this.trackBarOut)).EndInit();
  334. ((System.ComponentModel.ISupportInitialize)(this.bar4)).EndInit();
  335. this.panel1.ResumeLayout(false);
  336. this.panelSelfAV.ResumeLayout(false);
  337. this.groupBox1.ResumeLayout(false);
  338. this.ResumeLayout(false);
  339. }
  340. #endregion
  341. public void SetParameter(System.Net.IPAddress ip,int port,LanMsg.Controls.ClassMsg msg)
  342. {
  343.             this.serverIp =ip;
  344.     this.serverPort=port;
  345. this.msg=msg;
  346. this.ReadyConnect();
  347. }
  348. public void ColseAV()//关闭当前正在进行的视频对话
  349. {
  350. IsAV=false;
  351. this.linkLabelReceve.Visible=false;
  352. try
  353. {
  354. this.av.AVChanel.Close();
  355. }
  356. catch{}
  357. try
  358. {
  359. this.av.Chanel.Close();
  360. }
  361. catch{}
  362. try
  363. {
  364. this.av.Dispose();
  365. }
  366. catch{}
  367. }
  368. private void mixer_MixerControlChange(object sender, EventArgs e)
  369. {
  370. this.trackBarIn.Value=this.indtl.Volume;
  371. this.trackBarOut.Value=this.outdtl.Volume;
  372. }
  373. private void trackBarOut_Scroll(object sender, System.EventArgs e)
  374. {
  375. this.outdtl.Volume=this.trackBarOut.Value;
  376. this.indtl.Volume=this.trackBarIn.Value;
  377. }
  378. public void Initialize()//初始化视频图片与sock
  379. {
  380. if(av!=null)this.av.Dispose();
  381. this.av=new  AV.AV(this.picLocal ,this.picRemote);
  382. this.av.IniChanel();
  383. }
  384. public AV.AV  AV
  385. {
  386. get{return this.av;}
  387. }
  388.         private bool IsAV=false;//表示当前是否正在视频中
  389. private void ReadyConnect()//准备连接,并告诉对方本地IP与端口,让其联接
  390. {
  391. if(!IsAV)
  392. {
  393. Initialize();
  394. this.av.ReadyStrat(new ClassSerializers().SerializeBinary(this.msg).ToArray() ,this.serverIp ,this.serverPort);
  395. }
  396. else
  397. {
  398. this.av.AVChanel.Close();
  399. this.av.Chanel.Close();
  400.   }
  401. IsAV=!IsAV;
  402. //UDP打洞
  403. }
  404. public void Connect(string ip,int port)//连接到对方并且开始发送AV数据
  405. {
  406. this.av.IniAudio();
  407. this.av.IniVideo();
  408. this.av.Start(ip,port);
  409. }
  410. public void Stop()
  411. {
  412. if(this.av!=null)this.av.Dispose();
  413. }
  414. private void timer1_Tick(object sender, System.EventArgs e)
  415. {
  416.     this.av.UDPBurrowNat(this.serverIp ,this.serverPort );//UDP打洞
  417. }
  418. private void linkLabelCancelAV_LinkClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e)
  419. {
  420.     IsAV=false;
  421. this.linkLabelReceve.Visible=false;
  422. try
  423. {
  424. this.av.AVChanel.Close();
  425. this.av.Chanel.Close();
  426. this.av.Dispose();
  427. }
  428. catch{}
  429. this.AVCancel(this,true);//触发终止事件
  430. }
  431. private void linkLabelReceve_LinkClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e)
  432. {
  433. this.linkLabelReceve.Visible=false;
  434. this.AVReceve(this,true);//触发AV接收事件
  435. Initialize();//初始化视频设备
  436. this.av.IniAudio();//初始化音频设备
  437. this.av.IniVideo();//初始化视频设备
  438.             this.AV.BeginAV(new ClassSerializers().SerializeBinary(new LanMsg.Controls.ClassMsg(14,this.msg.ID,null)).ToArray(),this.serverIp,this.serverPort);//告诉对方可以开始视频
  439. }
  440. }
  441. }