VOIP java program slow?

Thread Starter

Mathematics!

Joined Jul 21, 2008
1,036
So I wrote this streaming audio application in java sdk 1.4.2 api.
It basically reads audio from the mic port and sends it using UDP to the client which then the client sends it to his speakers. The problem is that it is very choppy audio. So I am wondering what is slowing down the process. Should I not have the drain method in the toSpeaker function or should I have a seperate thread for collecting the udp packets etc etc ...

Feel free to download the 2 programs below and compile/run.
Note when runing you muxt have the mic port unmuted and the speaker port unmute the application doesn't unmute a channel by itself. Also I set the testing ip and port to localhost (127.0.0.1) and port 8888 you can change if needed.

Thanks for any help. I would really like this to go. I am unsure why it's chop I am using udp which is connectionless so if a few packets are dropped it won't resent it just moves on alot quicker then tcp. Anyway I don't know what the lag time is from?? AHHHHHHHHHHHHHHH all stop rambleing you get the point.

Rich (BB code):
 /*
 * MicPlayer.java
 *
 * Created on October 5, 2009, 7:28 PM
 */

package AudioStreamer;
import javax.sound.sampled.* ;
import java.net.* ;
/**
 *
 * @author  
 */
public class MicPlayer {
    
    private static final String IP_TO_STREAM_TO   = "localhost" ;
    private static final int PORT_TO_STREAM_TO     = 8888 ;
    
    /** Creates a new instance of MicPlayer */
    public MicPlayer() {
    
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
    Mixer.Info minfo[] = AudioSystem.getMixerInfo() ;
    for( int i = 0 ; i < minfo.length ; i++ )
    {
     System.out.println( minfo ) ;    
    }
    
    
    if (AudioSystem.isLineSupported(Port.Info.MICROPHONE)) {
    try {
       
       
      DataLine.Info dataLineInfo = new DataLine.Info( TargetDataLine.class , getAudioFormat() ) ;
      TargetDataLine targetDataLine = (TargetDataLine)AudioSystem.getLine( dataLineInfo  ) ;
      targetDataLine.open( getAudioFormat() );
      targetDataLine.start();
      byte tempBuffer[] = new byte[1000] ;
      int cnt = 0 ;
      while( true )
      {
      targetDataLine.read( tempBuffer , 0 , tempBuffer.length );
      sendThruUDP( tempBuffer ) ;
      }
      
    }
    catch(Exception e )
    {
    System.out.println(" not correct " ) ;
    System.exit(0) ;
    }
    }


    
    }
    
    
    public static AudioFormat getAudioFormat(){
    float sampleRate = 8000.0F;
    //8000,11025,16000,22050,44100
    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 );
    }

   
    public static void sendThruUDP( byte soundpacket[] )
    {
       try
       {
       DatagramSocket sock = new DatagramSocket() ; 
       sock.send( new DatagramPacket( soundpacket , soundpacket.length , InetAddress.getByName( IP_TO_STREAM_TO ) , PORT_TO_STREAM_TO ) ) ; 
       sock.close() ;
       }
       catch( Exception e )
       {
       e.printStackTrace() ;
       System.out.println(" Unable to send soundpacket using UDP " ) ;   
       }
       
    }
    
    
}
Rich (BB code):
  /*
 * RadioReceiver.java
 *
 * Created on October 6, 2009, 9:15 PM
 */

package AudioStreamer;
import java.net.* ;
import javax.sound.sampled.* ;

/**
 *
 * @author  
 */
public class RadioReceiver extends Thread {
    
    private static final String IP_TO_STREAM_TO   = "localhost" ;
    private static final int PORT_TO_STREAM_TO     = 8888 ;
   
    /** Creates a new instance of RadioReceiver */
    public RadioReceiver() {
    }
    
    public void run()
    {
        byte b[] = null ;
        while( true )
        {
           b = receiveThruUDP() ; 
           toSpeaker( b ) ;
        }        
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
    
    RadioReceiver r = new RadioReceiver() ;
    r.start() ;
    
    }
    
    
    public static byte[] receiveThruUDP()
    {
       try
       {
       DatagramSocket sock = new DatagramSocket(PORT_TO_STREAM_TO) ; 
       byte soundpacket[] = new byte[1000] ;
       DatagramPacket datagram = new DatagramPacket( soundpacket , soundpacket.length , InetAddress.getByName( IP_TO_STREAM_TO ) , PORT_TO_STREAM_TO ) ;
       sock.receive( datagram ) ; 
       sock.close() ;
       return datagram.getData() ; // soundpacket ;
       }
       catch( Exception e )
       {
       System.out.println(" Unable to send soundpacket using UDP " ) ;   
       return null ;
       } 
        
    }
    
    
     public static void toSpeaker( byte soundbytes[] )
     {
     
      try{  
      DataLine.Info dataLineInfo = new DataLine.Info( SourceDataLine.class , getAudioFormat() ) ;
      SourceDataLine sourceDataLine = (SourceDataLine)AudioSystem.getLine( dataLineInfo );
      sourceDataLine.open( getAudioFormat() ) ;
      sourceDataLine.start();
      int cnt = 0;
      sourceDataLine.write( soundbytes , 0, soundbytes.length );
      sourceDataLine.drain() ;
      sourceDataLine.close() ;
      }
      catch(Exception e )
      {
      System.out.println("not working in speakers " ) ;
      }
            
    }
    
     
    public static AudioFormat getAudioFormat()
    {
    float sampleRate = 8000.0F;
    //8000,11025,16000,22050,44100
    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 );
    }
    
    
}
 
Last edited:
Voip Phones < VOIP java program slow?
So I wrote this streaming audio application in java sdk 1.4.2 api.
It basically reads audio from the mic port and sends it using UDP to the client which then the client sends it to his speakers. The problem is that it is very choppy audio. So I am wondering what is slowing down the process. Should I not have the drain method in the toSpeaker function or should I have a seperate thread for collecting the udp packets etc etc ...
You need to look at your CPU usage--- if it's operating efficiently---98-100%, the problem is likely your code. In any event, java isn't the best language to use with voip applications.....(in my opinion).
 
Last edited:
Top