I have a server listening for connections. Client connects and both start streaming webcam images. This works fine as long as there is only one client connected(runni ng as a Thread). When another client connects, the new thread starts streaming as well. The camera is accessed in a mutual exclusive way by locking which fails some times and I get AccessViolation Exception. Below is the code executing in the thread.
The scenario is like this : One person sitting at a help desk and doing video conferencinf with multiple clients. Server hosted at the helpdesk itself.
The scenario is like this : One person sitting at a help desk and doing video conferencinf with multiple clients. Server hosted at the helpdesk itself.
Code:
try {
bytes = new byte[(ConfConsole.CAP.Stride * ConfConsole.CAP.Height)];
lock (lok) {
ptr = ConfConsole.CAP.GetBitMap(); //static object accessed by threads
Marshal.Copy(ptr, bytes, 0, bytes.Length); //error here sometimes
if (ptr != IntPtr.Zero) {
Marshal.FreeCoTaskMem(ptr);//error here sometimes
ptr = IntPtr.Zero;
}
}
bmp = (Bitmap)Utils.CopyDataToBitmap(
bytes, ConfConsole.CAP.Width, ConfConsole.CAP.Height);
bmp.RotateFlip(RotateFlipType.RotateNoneFlipY);
jpg = Utils.ResizeBitmap(bmp, 320, 240);
bmp.Dispose();
} catch (Exception e) {
l.log("error in capturing image + data : " + e.Message);
}
//code ripped from net
public static Bitmap CopyDataToBitmap(byte[] data, int width, int height) {
Bitmap bmp = new Bitmap(width, height, PixelFormat.Format24bppRgb);
//Create a BitmapData and Lock all pixels to be written
BitmapData bmpData = bmp.LockBits(
new Rectangle(0, 0, bmp.Width, bmp.Height),
ImageLockMode.WriteOnly, bmp.PixelFormat);
//Copy the data from the byte array into BitmapData.Scan0
Marshal.Copy(data, 0, bmpData.Scan0, data.Length);
//Unlock the pixels
bmp.UnlockBits(bmpData);
//Return the bitmap
return bmp;
}