المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Video chat... first one but does it look ok?



C# Programming
04-26-2009, 08:31 AM
Well I seem to have the video chat between two people working pretty good. I am going to post the code below to see what you guys think and if you see problems. I haven't tried it remotely, but on the internal network it seems to work well.

What I would like to do it enable multiple ones. Before I ask the question I am going to post what I have below:

For this project I used the modified DirectX.Capture provided by hpAng: DirectX Video Stream and frame capture (http://www.codeproject.com/KB/directx/DirXVidStrm.aspx)[^ (http://www.codeproject.com/KB/directx/DirXVidStrm.aspx)]


Here I am listening on port 50000. Now if I wanted to do multiple videos from different people, I could just take the data and place it in certain panels or picture boxes depending on the IP address it came from right? An idea I have but have not tried it yet. How will that affect me sending that much data on one port? I will show how I am sending below:

private void bwGetUdp_DoWork(object sender, DoWorkEventArgs e)
{
receiveClient = new UdpClient(50000);
receivePoint = new IPEndPoint(new IPAddress(0), 0);

while (true)
{
if (bwGetUdp.CancellationPending)
{
receiveClient.Close();
e.Cancel = true;
}
else
{
byte[] data = receiveClient.Receive(ref receivePoint);
MemoryStream ms = new MemoryStream(data);

Invoke(new SetStreamPictureDelegate(SetStreamPicture), new object[] { Image.FromStream(ms) });
Invoke(new ChangeReceivingLabelDelegate(ChangeReceivingLabel), new object[] { receivePoint.Address + ":" + receivePoint.Port });

ms.Flush();
ms.Close();
}
}
}


Here I am showing the video in a panel (DirectX.Capture) and starting the timer to capture the picture.
I am also setting the IPEndPoint to the users IP I get from the chat window I created.

Filters filters = new Filters();

cam = new Capture(filters.VideoInputDevices[0], filters.AudioInputDevices[0]);

cam.PreviewWindow = panelMyVideo;
cam.FrameEvent2 += new Capture.HeFrame(CaptureDone);

sendClient = new UdpClient(50001);
sendPoint = new IPEndPoint(IPAddress.Parse(this.toUserIP), 50000);
timerTakePic.Enabled = true;


Here I am just using the code provided by the article and sending it to the users IP

private delegate void SetStreamPictureDelegate(Image img);
private void SetStreamPicture(Image img)
{
pictureBoxOtherVideo.Image = img;
}

private void CaptureDone(Bitmap e)
{
MemoryStream ms = new MemoryStream();
e.Save(ms, ImageFormat.Jpeg);

Invoke(new ChangeSendingLabelDelegate(ChangeSendingLabel), new object[] { sendPoint.Address + "... " + Convert.ToInt32(ms.ToArray().Length) / 1024 + "KB" });
sendClient.Send(ms.ToArray(), Convert.ToInt32(ms.Length), sendPoint);

ms.Flush();
ms.Close();
}


Obviously I'm using UDP which I hear you supposed to... this is my first time really messing with UDP or TCP for that matter. The timer is ticking every 50ms.. Now my next step is voice then multiple videos.. maybe visa versa.

Is there a better way I should be doing this?