⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 av.cs

📁 该即时通讯系统系统能够实现像QQ一样的通讯功能
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;using System.IO;
using LanMsg.AV;
using System.Runtime.InteropServices;
using System.Net.Sockets;
using System.Net;
using System.Runtime.Serialization;

namespace LanMsg.AV
{
	/// <summary>
	/// Form1 的摘要说明。
	/// </summary>
	
	public class AV:System.IDisposable
	{

		public AV(Control local,Control remote)
		{
			this.local=local;//初始化本地视频控件
			this.remote=remote;//初始化远程视频控件
		}


		#region IDisposable 成员

		public void Dispose()
		{
			if(this.vc!=null)
			{
				this.vc.Preview=false;
				this.vc.Stop();
				this.vc.Disconnect();
				this.vc.Close();
			}
			if(this.chanel!=null)
			{
				try
				{
					this.chanel.Disconnect();
					this.chanel.Close();
				}
				catch(System.Exception ex)
				{
					MessageBox.Show(ex.Message);
				}
			}
			if(this.wi!=null)
			{
				this.wi.Stop();
			}
			if(this.dd!=null)
			{
				this.dd.Close();
			}
			if(this.wo!=null)
			{
				this.wo.Stop();
			}
			if(this.mixer!=null)
			{
				this.mixer.Close();
			}
		}

		#endregion

		private Mixer mixer;
		private Mixer.MixerControlDetail dt,dt2;
		private DrawDib dd;
		private AVChanel chanel;
		private LanMsg.AV.VideoCapturer vc;
		
		private WaveIn wi;
		private WaveOut wo;WAVEFORMATEX wf;
		private Control local,remote;

		#region initial

		public void Initial()
		{
			this.IniChanel();
			this.IniAudio();
			this.IniVideo();
		}

		#region audio
		public void IniAudio()
		{
		
				wf=new WAVEFORMATEX();
				wf.cbSize=0;
				wf.nChannels=1;
				wf.nSamplesPerSec=8000;
				wf.wBitsPerSample=16;
				wf.nBlockAlign=2;
				wf.nAvgBytesPerSec=16000;
				wf.wFormatTag=1;//pcm


				wo=new WaveOut(0,wf,4000,20);
				wo.WaveOutError+=new WaveErrorEventHandler(wo_WaveInError);
				wi=new WaveIn(0,wf,4000,20);
				wi.WaveInError+=new WaveErrorEventHandler(wo_WaveInError);
				wi.WaveCaptured+=new WaveBufferEventHandler(wi_WaveCaptured);
				this.wo.Start();
				this.wi.Start();

			
				this.mixer=new Mixer(this.local);
				dt=new Mixer.MixerControlDetail(this.mixer,Mixer.MIXERLINE_COMPONENTTYPE_DST_SPEAKERS);
				dt2=new Mixer.MixerControlDetail(this.mixer,0x00001000+3);
			try
			{}
			catch{}
		}
		#endregion
		#region video
		public void IniVideoCapture()
		{
				this.vc=new VideoCapturer(this.local,0);
				this.vc.ConnectDevice();
				this.vc.SetPreviewRate(66);
				this.vc.Preview=true;
				LanMsg.AV.CaptureParms cp=this.vc.CaptureParms;
				cp.fAbortLeftMouse=cp.fAbortRightMouse=false;
				cp.fYield=true;
				this.vc.CaptureParms=cp;
				LanMsg.AV.BITMAPINFO h=this.vc.BITMAPINFO;
				h.bmiHeader.biBitCount=24;
				h.bmiHeader.biWidth=320;
				h.bmiHeader.biHeight=240;
				this.vc.BITMAPINFO=h;
				this.vc.VideoCaptured+=new VideoCaptureEventHandler(vc_VideoCaptured);
				this.vc.CaptureWithOutFile();
			try
			{

			}
			catch{}
		}
		public void IniVideoRender()
		{
				BITMAPINFOHEADER bmi=new BITMAPINFOHEADER();

				bmi.biCompression =(int) BI.BI_RGB;
				bmi.biWidth =320;
				bmi.biHeight = 240;
				bmi.biPlanes = 1;
				bmi.biBitCount = 24;
				bmi.biXPelsPerMeter = 0;
				bmi.biYPelsPerMeter = 0;
				bmi.biClrUsed = 0;
				bmi.biClrImportant = 0;
				bmi.biSizeImage=230400;//;57600
				bmi.biSize=Marshal.SizeOf(bmi);
				dd=new DrawDib();
				dd.Control=this.remote;
				dd.BITMAPINFOHEADER=bmi;
				dd.Open();
			try
			{
			}
			catch{}
		}
		public void IniVideo()
		{
			this.IniVideoRender();
			this.IniVideoCapture();
		}
		#endregion
		#region
		public void IniChanel()
		{
			chanel=new AVChanel();
			this.chanel.AudioData+=new AVChanel.AVChanelEventHandler(chanel_AudioData);
			this.chanel.VideoData+=new AVChanel.AVChanelEventHandler(chanel_VideoData);
			this.chanel.Open();
		}

		#endregion
		#endregion
         
		public void BeginAV(byte[] buf,System.Net.IPAddress ServerIp,int ServerPort)//告诉对方开始视频
		{
			this.chanel.BeginAV(buf,ServerIp,ServerPort);
		}

		public void ReadyStrat(byte[] buf,System.Net.IPAddress ServerIp,int ServerPort)//发送请求到对方,要求视频对话
		{
            this.chanel.SendAVtoServerRequest(buf,ServerIp,ServerPort);
		}

		public void UDPBurrowNat(System.Net.IPAddress ServerIp,int ServerPort)//UDP打洞
		{
			this.chanel.UDPBurrowNat(ServerIp,ServerPort);
		}

		public void Start(string ip,int p)
		{
			this.chanel.Connect(ip,p);
		}

		public AVChanel Chanel
		{
			get{return this.chanel;}
		}

		//SetWindowPos(hCapWnd, 0, 25, 25, bmi.bmiHeader.biWidth,bmi.bmiHeader.biHeight,0);
		private void chanel_AudioData(object sender, AVChanel.AVChanelEventArgs e)//
		{
			if(this.wo!=null)this.wo.Write(e.Data);//输出音频
		}

		private void chanel_VideoData(object sender, AVChanel.AVChanelEventArgs e)//视频到达事件
		{
			//	byte[] decompressdata=this.dp.Process(e.Data);
			if(this.dd!=null)this.dd.Draw(e.Data,this.remote.Width,this.remote.Height);//输出视频
		}
		private void vc_VideoCaptured(object sender, VIDEOHDR hdr)//当捕获到视频
		{
			if(this.chanel==null || !this.chanel.Connected)return;
			try
			{
				byte[] data=new byte[hdr.dwBytesUsed];
				Marshal.Copy(hdr.lpData,data,0,hdr.dwBytesUsed);
				this.chanel.SendVideo(data);
			}
			catch(System.Exception ex)
			{
				System.Diagnostics.Trace.WriteLine(ex.Message+":"+ex.StackTrace);
			}
		}
		private void wi_WaveCaptured(object sender, WAVEHDR hdr)//捕获到音频
		{

			if(this.chanel==null ||!this.chanel.Connected)return;
			byte[] data=new byte[hdr.dwBytesRecorded];
			System.Runtime.InteropServices.Marshal.Copy(hdr.lpData,data,0,data.Length);
			UT.LowPassWave(this.wi.WAVEFORMATEX,data,data.Length,2500);
			this.chanel.SendAudio(data);

		}

		private void wo_WaveInError(object sender, AVException e)
		{

		}

		public int LocalPort
		{
			get{return this.chanel.LocalPort;}
		}

		public ProgressBar PlayBackVolumeControl;

		public AVChanel AVChanel
		{
			get{return this.chanel;}
		}
	}

	public class AVException:System.Exception
	{
		int m_num=0;
		public AVException():base(){}
		public AVException(int er):base(){this.m_num=er;}
		public AVException(string message):base(message){}
		public AVException(string message,int er):base(message){this.m_num=er;}
		public AVException(SerializationInfo info,StreamingContext context):base(info,context){}
		public int ErrorNumber
		{
			get{return m_num;}
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -