I've been working on some code which allows me to share a screen through UDP from my Game Engine (Unity) to my android phone.
The process is actually just taking screenshots, decode them to jpeg byte arrays and split those byte arrays into multiple packages (if needed).
When I run the project, it's actually sending the images, but some get distorted or are messed up on receiving.
So I played around a little with the buffer size, and I noticed that increasing the buffer size of the receiving UDP Client actually improved the data loss/corruption. But unfortunately, there is still too much corrupted data which a user just can't ignore.
After a while, the Host (My phone) just crashes. I assume this has something to do with the package buffer size, maybe it got over flooded?
Anyway, here is the piece of code that handles the connection. I'm currently only sending data in one way (Computer -> phone) but it's set-up to also send other info from my phone to my computer (but that's for later, I need to get this to work first).
public void OpenConnection(string hostname, int bufferSize, int port = 12005)
{
ep1 = new IPEndPoint(IPAddress.Any, port);
receiveThread = new Thread(new ThreadStart((delegate
{
receiveClient = new UdpClient();
receiveClient.Client.ReceiveBufferSize = bufferSize;
receiveClient.ExclusiveAddressUse = false;
receiveClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
receiveClient.Client.Bind(ep1);
while(true)
{
try
{
byte[] buffer = receiveClient.Receive(ref ep1);
if(buffer != null && onDataReceived != null) onDataReceived(buffer);
}
catch(Exception ex)
{
Debug.Log("Error: " + ex.ToString());
}
}
})));
sendClient = new UdpClient();
sendClient.Client.SendBufferSize = bufferSize;
sendClient.ExclusiveAddressUse = false;
sendClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
ep2 = new IPEndPoint(IPAddress.Parse(hostname), port);
sendClient.Client.Bind(ep1);
receiveThread.Start();
}
public void SendData(byte[] data)
{
if(sendClient != null){
try
{
sendClient.Send(data, data.Length, ep2);
}
catch(Exception ex)
{
Debug.Log("error: " + ex.ToString());
}
}
}
Currently I have the variable bufferSize
set to 20.000
, and the sliced image data packages are serialised classes (with frame id, package id, image data, etc) with a total size of 15.200
ish.
I know the consequences of UDP, but I really need the lowest latency I could get. Should I switch to TCP? The image sizes right now are just testing images of a cube with some particles on a blue background. I think when streaming more complex images, the JPEG compression file increases too.
Thanks!