0

We're currently writing VoIP code, and we're having lag issues, and we've noticed that it changes if we modify a few variables. We're new to working with audio, and we're curious as to why we're having this problem. I've posted code below.

Audio Class:

public class Audio
{
public static final int bufferSize = 320;

    public static AudioFormat getAudioFormat()
    {
    float sampleRate = 16000.0F;
    //8000,11025,16000,22050,44100,48000

    int sampleSizeInBits = 16;
    //8,16

    int channels = 1;
    //1,2

    boolean signed = true;
    //true,false

    boolean bigEndian = false;
    //true,false

    return new AudioFormat(sampleRate, sampleSizeInBits, channels, signed, bigEndian);
    }
}

Recorder Class:

public class Recorder implements Runnable
{
    Socket socket;

    public Recorder(Socket socket)
    {
        this.socket = socket;
    }

    public void run()
    {
        try
        {
            AudioFormat format = Audio.getAudioFormat();

            DataLine.Info dataLineInfo = new DataLine.Info(TargetDataLine.class, format);
            TargetDataLine targetDataLine = (TargetDataLine) AudioSystem.getLine(dataLineInfo);
            targetDataLine.open(format);
            targetDataLine.start();
            AudioInputStream ais = new AudioInputStream(targetDataLine);

            BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream());
            Pcm2SpeexAudioInputStream sin = new Pcm2SpeexAudioInputStream(0, 4, ais, format, -1);

            byte[] input = new byte[Audio.bufferSize];
            int count = 0;
            while ((count = sin.read(input)) != -1)
            {
                bos.write(input, 0, count);
            }

            sin.close();
            bos.close();
            socket.close();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}    

We're using JSpeex for our encoding. If anyone knows how to fine tune our variables, please let us know! 4 second lag isn't acceptable, and 10 second definitely isn't.

Kristoff
  • 167
  • 3
  • 13
  • Are you sure it's the audio, and not the network? Is your network having 4–10 second packet latency? is your network experiencing pcaket loss? Are you sending the data over UDP, or TCP? (If TCP, and you're suffering packet loss, then you're going to see high latency.) If UDP, how long do you wait for missed packets before dropping them? (Do you?) Does your client play received audio immediately, or does it induce a bit of lag? (It might be good to have the audio be a little behind, to allow for the occasional late packet.) – Thanatos Aug 25 '13 at 20:58
  • We're using TCP, however, I'm having my developer test it with UDP. It might be the network, because he's not testing via localhost, and its showing to be much less lag, but localhost shouldn't have 1.5 second lag either. Removing the network from the equation should have made it hardly noticeable. With UDP, we'd assume that any packetloss should just be lost, and disregarded. But, we still do want to find the ideal variables to use for our VoIP applications. – Kristoff Aug 25 '13 at 23:12
  • localhost should be lag free — you'll have to better pinpoint why that would be slow. Packets sent in UDP that are lost by the network are just gone. TCP will resend lost packets (if possible) but it will take time for TCP to realize it lost a packet. – Thanatos Aug 26 '13 at 03:31
  • Well, I understand how TCP and UDP work. But, I need to find the ideal audio configuration to keep this lag to a minimum. We're using localhost for testing right now, and still seeing that lag. – Kristoff Aug 26 '13 at 20:32
  • Is your "4-10 s" latency only initial? (It doesn't stutter once it gets going, and a CPU isn't pegged?) Modern processors should be able to encode audio in real time with little difficulty. Perhaps you have an audio buffer that needs to get filled up before it gets passed on to the next thing in whatever audio pipeline you have? A minimal test case would be great — is there some minimum code you have that can demonstrate this? – Thanatos Aug 27 '13 at 03:48
  • Actually, further on the above: You have `BufferedOutputStream`, no buffer size. I think the default is 8KiB. What bitrate is Speex outputting? According to http://www.speex.org/docs/manual/speex-manual/node10.html, it looks like the max bitrate for Speex NB is 24kpbs, or ~3 KiB/s. So it'd take just over 2s to fill your buffer, if you maintained the highest bitrate (but you've set quality=4, so probably not, which could bump you into the 4–10s range). You could just try removing it to see if it is the problem… you'd incur a lot of low-level calls (maybe try to batch them?), so not long term. – Thanatos Aug 27 '13 at 04:02

0 Answers0