Sending packets from server to clients is not fast enough (code included)
Im writing a video conference application in C#, and im running into some video delay problems,
The client will take images from the webcame, 10 frames per second, and will send them to the server (one by one) using TCP (which soon will be converted to UDP). I use Socket.Send and the socket is Blocking. `
if (VideoSoc != null)
{
try
{
VideoSoc.SendBufferSize = picChunk.Length;
VideoSoc.Send(picChunk);
sendimage++;
}
catch (SocketException expp)
{
if (expp.ErrorCode == 10054)
{
//ConnectSockets();
//MessageBox.Show("Video has been disconnected.");
}
}
catch (Exception exppp) { }
}
on the server There is a Room class, and a User class, each User that connects to there will have a Queue of type byte[] to store the images that was recieved 开发者_如何转开发by this user.
List<byte> bl = new List<byte>(VideoPacket.Videobuffer);
bl.RemoveRange(iRx, VideoPacket.Videobuffer.Length - iRx);
byte[] nb = new byte[bl.Count];
bl.CopyTo(nb);
Rooms[VideoPacket.Index].UsersList[VideoPacket.Pos].VideoList.Enqueue(nb);
Then in a While(true) loop, on a different thread than the main thread, the server will go through the users list , get the image stored in the queue and send it to all other connected clients using a udp socket.
while (true)
{
try
{
for (int x = 0; x < Rooms[roomindex].UsersList.Count; x++)
{
try
{
// this is my temp solution to eliminate the delay.. if more than 10 frames are stacked up in the ques.. clear them, which will effect the smoothness of the video on the client side
if (Rooms[roomindex].UsersList[x].VideoList.Count >10)
{
Rooms[roomindex].UsersList[x].VideoList.Clear();
}
countt = Rooms[roomindex].UsersList[x].VideoList.Count;
if (Rooms[roomindex].UsersList[x].VideoList.Count > 0)
{
byte[] videodata = Rooms[roomindex].UsersList[x].VideoList.Dequeue();
for (int i = 0; i < Rooms[roomindex].UsersList.Count; i++)
{
if (i != x && Rooms[roomindex].UsersList[i].Username != "u" && Rooms[roomindex].UsersList[i].Ready)
{
try
{
Rooms[roomindex].UsersList[i].udpvideosocket.SendTo(videodata, Rooms[roomindex].UsersList[i].ep);
}
catch(Exception ex) { }
}
}
}
}
catch (Exception exo)
{
Rooms[roomindex].UsersList[x].VideoList.Clear();
}
}
}
catch (Exception VideoSendingException)
{
logerror(VideoSendingException);
}
Here i can see that each user video queue count is increasing when more and more when new clients connect, which drived the conclusion that the upd socket.send is not fast enough to send the data to 20 connected clients. and that is my problem.
I am not an expert on sockets, i did my best with the knowlde google search results has to offer a biggener. and i know my code is not the best, and its not optimized, so any advice or pointing in the right direction is welcomed. Please feel free to ask for any clarification.
Thank you.
because of the difficulties UDP faces when its behind NAT routers, im back to using TCP.
From MSDN Socket.SendTo:
If you are using a connectionless protocol in blocking mode, SendTo will block until the datagram is sent.
As I understand, this is exactly what you are doing.
精彩评论