那位朋友能分享一下视频捕捉、压缩、传输代码,即视频会议用的代码,只提供一部分功能即可,谢谢了
那位朋友能分享一下视频捕捉、压缩、传输代码,即视频会议用的代码,只提供一部分功能即可,谢谢了
谢谢大家啦!
程序代码:using System;
using System.Runtime.InteropServices;
namespace VideoPreview.Utilities
{
public class CCodec
{
private int hic;
//private IntPtr hic;
private COMPVARS cp;
private BITMAPINFO cbiIn;
private BITMAPINFO cbiOut;
private int dehic;
//private IntPtr dehic;
private COMPVARS decp;
private BITMAPINFO decbiIn;
private BITMAPINFO decbiOut;
public bool InitCompress(BITMAPINFOHEADER bi)
{
this.cp = new COMPVARS();
cp.cbSize = Marshal.SizeOf(this.cp);
cp.dwFlags = 1; // ICMF_COMPVAES_VALID
cp.fccHandler = 2021026148;
cp.fccType = FOURCC.ICTYPE_VIDEO;
cp.lKey = 15;
cp.lQ = 10000;
this.hic = ICOpen(cp.fccType, cp.fccHandler, ICMODE.ICMODE_COMPRESS | ICMODE.ICMODE_DECOMPRESS);
if (this.hic != 0)
{
cp.hic = this.hic;
cbiIn = new BITMAPINFO();
cbiIn.bmiHeader = bi;
if (ICSendMessage(this.hic, ICM_COMPRESS_GET_FORMAT, ref this.cbiIn, ref this.cbiOut) == ICERR_OK)
{
return ICSeqCompressFrameStart(ref this.cp, ref this.cbiIn);
}
else
{
return false;
}
}
else
{
return false;
}
}
public byte[] CPProcess(byte[] data)
{
if (this.hic == 0)
{
return null;
}
bool key = false;
int size = 0;
try
{
lock (data)
{
IntPtr source = ICSeqCompressFrame(
ref this.cp,
0,
data,
ref key,
ref size);
if (size > 0)
{
byte[] cpdata = new byte[size];
try
{
Marshal.Copy(source, cpdata, 0, size);
}
catch
{
//System.Windows.Forms.MessageBox.Show("Data array over flow!", "CompressframeError", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);
}
return cpdata;
}
else
{
return null;
}
}
}
catch (System.Exception ex)
{
//System.Windows.Forms.MessageBox.Show("Compress Exception : " + ex.Message);
}
return null;
}
public bool InitDecompress()
{
if (this.cp.hic != 0)
{
this.decp = this.cp;
this.dehic = this.hic; // this.cp.hic;
}
else
{
return false;
}
//this.dehic = ICOpen(cp.fccType, cp.fccHandler, ICMODE.ICMODE_DECOMPRESS);
this.decbiIn = this.cbiOut;
this.decbiOut = this.cbiIn;
if (this.dehic != 0)
{
if (ICSendMessage(this.dehic, ICM_DECOMPRESS_BEGIN, ref this.decbiIn, ref this.decbiOut) == ICERR_OK)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
public byte[] DECCPProcess(byte[] data)
{
if (this.dehic == 0)
{
return null;
}
byte[] deccpdata = new byte[this.decbiOut.bmiHeader.biSizeImage];
try
{
if (ICDecompress(this.dehic, 0, ref this.decbiIn.bmiHeader, data, ref this.decbiOut.bmiHeader, deccpdata) != ICERR_OK)
{
return null;
}
}
catch (Exception ex)
{
//System.Windows.Forms.MessageBox.Show("Decompress Exception : " + ex.Message);
}
return deccpdata;
}
public void CCodecClose()
{
try
{
if (this.hic != 0)
{
try
{
ICSeqCompressFrameEnd(ref this.cp);
}
catch { }
ICClose(this.hic);
}
if (this.dehic != 0)
{
try
{
ICSendMessage(hic, ICM_USER + 14, 0, 0);
}
catch { }
ICClose(this.dehic);
}
}
catch { }
}
程序代码: #region
[DllImport("MSVFW32.dll")]
public static extern int ICCompressGetFormat(
//int hic,
IntPtr hic,
ref BITMAPINFO lpbiInput,
ref BITMAPINFO lpbiOutput
);
[DllImport("MSVFW32.dll")]
public static extern int ICDecompressBegin(
//int hic,
IntPtr hic,
ref BITMAPINFO lpbiInput,
ref BITMAPINFO lpbiOutput
);
[DllImport("VFW32.dll")]
public static extern int ICDecompressGetFormat(
//int hic,
IntPtr hic,
ref BITMAPINFO lpbiInput,
ref BITMAPINFO lpbiOutput
);
[DllImport("MSVFW32.dll")]
public static extern void ICSeqCompressFrameEnd(ref COMPVARS pc);
[DllImport("MSVFW32.dll")]
public static extern int ICCompressGetFormatSize(
int hic,
ref BITMAPINFO lpbiInput
);
[DllImport("MSVFW32.dll")]
public static extern bool ICSeqCompressFrameStart(
ref COMPVARS pc,
//ref BITMAPINFO lpbiIn
ref BITMAPINFO lpbiIn
);
[DllImport("MSVFW32.dll")]
public static extern IntPtr ICSeqCompressFrame(
ref COMPVARS pc,
uint uiFlags,
byte[] lpBits,
ref bool pfKey,
//ref ushort pfKey,
//ref long plSize
ref int plSize
);
[DllImport("MSVFW32.dll", CharSet = CharSet.Ansi)]
public static extern int ICGetInfo(
int hic,
ICINFO lpicinfo,
int cb
);
[DllImport("MSVFW32.dll")]
public static extern bool ICCompressorChoose(
IntPtr hwnd,
int uiFlags,
//int pvIn,
//int lpData,
IntPtr pvIn,
IntPtr lpData,
ref COMPVARS pc,
string lpszTitle
);
[DllImport("MSVFW32.dll")]
public static extern int ICLocate(
int fccType,
int fccHandler,
ref BITMAPINFOHEADER lpbiIn,
ref BITMAPINFOHEADER lpbiOut,
short wFlags
);
[DllImport("MSVFW32.dll"), PreserveSig]
public static extern int ICOpen(uint fccType, uint fccHandler, ICMODE wMode);
//public static extern IntPtr ICOpen(uint fccType, uint fccHandler, uint wmode); // ICMODE wMode);
[DllImport("MSVFW32.dll"), PreserveSig]
public static extern int ICDecompressOpen(
int fccType,
int fccHandler,
ref BITMAPINFOHEADER lpbiIn,
ref BITMAPINFOHEADER lpbiOut
);
[DllImport("MSVFW32.dll")]
public static extern int ICClose(int hic);
[DllImport("MSVFW32.dll")]
public static extern int ICCompress(
int hic,
int dwFlags, // flags
ref BITMAPINFOHEADER lpbiOutput, // output format
IntPtr lpData, // output data
ref BITMAPINFOHEADER lpbiInput, // format of frame to compress
IntPtr lpBits, // frame data to compress
int lpckid, // ckid for data in AVI file
int lpdwFlags, // flags in the AVI index.
int lFrameNum, // frame number of seq.
int dwFrameSize, // reqested size in bytes. (if non zero)
int dwQuality, // quality within one frame
int lpbiPrev, // format of previous frame
int lpPrev // previous frame
);
[DllImport("MSVFW32.dll")]
public static extern int ICDecompress(
int hic,
//IntPtr hic,
//int dwFlags,
uint dwFlags,
ref BITMAPINFOHEADER lpbiFormat,
byte[] lpData,
ref BITMAPINFOHEADER lpbi,
byte[] lpBits
);
[DllImport("MSVFW32.dll")]
public static extern int ICSendMessage(int hic, int msg, ref BITMAPINFO dw1, ref BITMAPINFO dw2);
[DllImport("MSVFW32.dll")]
public static extern int ICSendMessage(int hic, int msg, int dw1, int dw2);
[DllImport("MSVFW32.dll")]
public static extern int ICSendMessage(int hic, int msg, ICINFO dw1, int dw2);
public static readonly int DRV_USER = 0x4000;
public static readonly int ICM_USER = (DRV_USER + 0x0000);
public static readonly int ICM_COMPRESS_BEGIN = (ICM_USER + 7); // begin a series of compress calls.
public static readonly int ICM_COMPRESS = (ICM_USER + 8); // compress a frame
public static readonly int ICM_COMPRESS_END = (ICM_USER + 9); // end of a series of compress calls.
public static readonly int ICM_COMPRESS_GET_FORMAT = (ICM_USER + 4);
public static readonly int ICM_DECOMPRESS_BEGIN = (ICM_USER + 12); // start a series of decompress calls
public static readonly int ICM_DECOMPRESS = (ICM_USER + 13); // decompress a frame
public static readonly int ICM_DECOMPRESS_END = (ICM_USER + 14);
public static readonly int ICERR_OK = 0x0;
#endregion
}
}