I've written a small java program (pc) and android app which is meant to stream audio from the pc to my phone. I am sending the audio data via UDP, and am having trouble determining the source of the delay which occurs. From client to server, about 1.5-2 seconds pass before the packets are processed.
My program buffers 512 bytes to send to the client, which are played back immediately upon reception.
I tested some different configurations, with anything (substantially) higher than this value, delays only increase. With lower values, there is no noticeable improvement in terms of latency, but there is noticeable quality loss.
The ping time between the devices is only 3ms according to windows, so I am assuming that the network connection is not the issue, though I am not positive.
My Client (PC)'s code is shown below.
byte[] buffer = new byte[512];
while (true) {
try {
audioInput.read(buffer, 0, buffer.length);
DatagramPacket data = new DatagramPacket(buffer, buffer.length, address, port);
dout.send(data);
System.out.println("Sent Packet #" + i++);
} catch (IOException e) {
e.printStackTrace();
}
}
The Server (Phone)'s code is as follows.
byte[] buffer = new byte[512];
DatagramPacket incomingPacket = new DatagramPacket(buffer, buffer.length);
while (true) {
try {
dgs.receive(incomingPacket);
buffer = incomingPacket.getData();
audioOutput.write(buffer, 0, buffer.length);
} catch (IOException e) {
e.printStackTrace();
}
}
I expected packets to arrive at a speed closer to the network delay of <5ms, but actually receive them only after ~1500ms.
I was hoping some of you may have experience with this sort of issue. I know that apps like Discord and Skype stream at higher bitrates, with much higher latency, but have a substantially lower delay, so I was hoping there might have been something that I missed.
byte[] buffer = new byte[512];
the 512 byte buffer you're talking about in the question? – Ryotsuiperf
to test your network latency between PC and phone. Also check the Wifi for frequency collisions with neighboring Wifi networks. – Robert