VOIP java program slow?

Discussion in 'Programmer's Corner' started by Mathematics!, Oct 9, 2009.

  1. Mathematics!

    Thread Starter Senior Member

    Jul 21, 2008
    1,022
    4
    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.

    Code ( (Unknown Language)):
    1.  
    2. [COLOR=Red]
    3. [/COLOR] [FONT=Verdana, Arial, Helvetica][COLOR=Red][FONT=Verdana, Arial, Helvetica][SIZE=2][FONT=courier][SIZE=2]/*
    4.  * MicPlayer.java
    5.  *
    6.  * Created on October 5, 2009, 7:28 PM
    7.  */
    8.  
    9. package AudioStreamer;
    10. import javax.sound.sampled.* ;
    11. import java.net.* ;
    12. /**
    13.  *
    14.  * @author  
    15.  */
    16. public class MicPlayer {
    17.    
    18.     private static final String IP_TO_STREAM_TO   = "localhost" ;
    19.     private static final int PORT_TO_STREAM_TO     = 8888 ;
    20.    
    21.     /** Creates a new instance of MicPlayer */
    22.     public MicPlayer() {
    23.    
    24.     }
    25.    
    26.     /**
    27.      * @param args the command line arguments
    28.      */
    29.     public static void main(String[] args) {
    30.     Mixer.Info minfo[] = AudioSystem.getMixerInfo() ;
    31.     for( int i = 0 ; i < minfo.length ; i++ )
    32.     {
    33.      System.out.println( minfo[i] ) ;    
    34.     }
    35.    
    36.    
    37.     if (AudioSystem.isLineSupported(Port.Info.MICROPHONE)) {
    38.     try {
    39.        
    40.        
    41.       DataLine.Info dataLineInfo = new DataLine.Info( TargetDataLine.class , getAudioFormat() ) ;
    42.       TargetDataLine targetDataLine = (TargetDataLine)AudioSystem.getLine( dataLineInfo  ) ;
    43.       targetDataLine.open( getAudioFormat() );
    44.       targetDataLine.start();
    45.       byte tempBuffer[] = new byte[1000] ;
    46.       int cnt = 0 ;
    47.       while( true )
    48.       {
    49.       targetDataLine.read( tempBuffer , 0 , tempBuffer.length );
    50.       sendThruUDP( tempBuffer ) ;
    51.       }
    52.      
    53.     }
    54.     catch(Exception e )
    55.     {
    56.     System.out.println(" not correct " ) ;
    57.     System.exit(0) ;
    58.     }
    59.     }
    60.  
    61.  
    62.    
    63.     }
    64.    
    65.    
    66.     public static AudioFormat getAudioFormat(){
    67.     float sampleRate = 8000.0F;
    68.     //8000,11025,16000,22050,44100
    69.     int sampleSizeInBits = 16;
    70.     //8,16
    71.     int channels = 1;
    72.     //1,2
    73.     boolean signed = true;
    74.     //true,false
    75.     boolean bigEndian = false;
    76.     //true,false
    77.     return new AudioFormat( sampleRate, sampleSizeInBits, channels, signed, bigEndian );
    78.     }
    79.  
    80.    
    81.     public static void sendThruUDP( byte soundpacket[] )
    82.     {
    83.        try
    84.        {
    85.        DatagramSocket sock = new DatagramSocket() ;
    86.        sock.send( new DatagramPacket( soundpacket , soundpacket.length , InetAddress.getByName( IP_TO_STREAM_TO ) , PORT_TO_STREAM_TO ) ) ;
    87.        sock.close() ;
    88.        }
    89.        catch( Exception e )
    90.        {
    91.        e.printStackTrace() ;
    92.        System.out.println(" Unable to send soundpacket using UDP " ) ;  
    93.        }
    94.        
    95.     }
    96.    
    97.    
    98. }[/i][/SIZE][i][/i][/FONT][i][/i][/SIZE][i][/i][/FONT][i][/i][/COLOR][i][/i][/FONT][i]
    99. [/i]
    Code ( (Unknown Language)):
    1. [COLOR=Red]
    2.  
    3. [/COLOR]  [FONT=Verdana, Arial, Helvetica][COLOR=Red][FONT=Verdana, Arial, Helvetica][SIZE=2][FONT=courier][SIZE=2]/*
    4.  * RadioReceiver.java
    5.  *
    6.  * Created on October 6, 2009, 9:15 PM
    7.  */
    8.  
    9. package AudioStreamer;
    10. import java.net.* ;
    11. import javax.sound.sampled.* ;
    12.  
    13. /**
    14.  *
    15.  * @author  
    16.  */
    17. public class RadioReceiver extends Thread {
    18.    
    19.     private static final String IP_TO_STREAM_TO   = "localhost" ;
    20.     private static final int PORT_TO_STREAM_TO     = 8888 ;
    21.    
    22.     /** Creates a new instance of RadioReceiver */
    23.     public RadioReceiver() {
    24.     }
    25.    
    26.     public void run()
    27.     {
    28.         byte b[] = null ;
    29.         while( true )
    30.         {
    31.            b = receiveThruUDP() ;
    32.            toSpeaker( b ) ;
    33.         }        
    34.     }
    35.    
    36.     /**
    37.      * @param args the command line arguments
    38.      */
    39.     public static void main(String[] args) {
    40.    
    41.     RadioReceiver r = new RadioReceiver() ;
    42.     r.start() ;
    43.    
    44.     }
    45.    
    46.    
    47.     public static byte[] receiveThruUDP()
    48.     {
    49.        try
    50.        {
    51.        DatagramSocket sock = new DatagramSocket(PORT_TO_STREAM_TO) ;
    52.        byte soundpacket[] = new byte[1000] ;
    53.        DatagramPacket datagram = new DatagramPacket( soundpacket , soundpacket.length , InetAddress.getByName( IP_TO_STREAM_TO ) , PORT_TO_STREAM_TO ) ;
    54.        sock.receive( datagram ) ;
    55.        sock.close() ;
    56.        return datagram.getData() ; // soundpacket ;
    57.        }
    58.        catch( Exception e )
    59.        {
    60.        System.out.println(" Unable to send soundpacket using UDP " ) ;  
    61.        return null ;
    62.        }
    63.        
    64.     }
    65.    
    66.    
    67.      public static void toSpeaker( byte soundbytes[] )
    68.      {
    69.      
    70.       try{  
    71.       DataLine.Info dataLineInfo = new DataLine.Info( SourceDataLine.class , getAudioFormat() ) ;
    72.       SourceDataLine sourceDataLine = (SourceDataLine)AudioSystem.getLine( dataLineInfo );
    73.       sourceDataLine.open( getAudioFormat() ) ;
    74.       sourceDataLine.start();
    75.       int cnt = 0;
    76.       sourceDataLine.write( soundbytes , 0, soundbytes.length );
    77.       sourceDataLine.drain() ;
    78.       sourceDataLine.close() ;
    79.       }
    80.       catch(Exception e )
    81.       {
    82.       System.out.println("not working in speakers " ) ;
    83.       }
    84.            
    85.     }
    86.    
    87.      
    88.     public static AudioFormat getAudioFormat()
    89.     {
    90.     float sampleRate = 8000.0F;
    91.     //8000,11025,16000,22050,44100
    92.     int sampleSizeInBits = 16;
    93.     //8,16
    94.     int channels = 1;
    95.     //1,2
    96.     boolean signed = true;
    97.     //true,false
    98.     boolean bigEndian = false;
    99.     //true,false
    100.     return new AudioFormat( sampleRate, sampleSizeInBits, channels, signed, bigEndian );
    101.     }
    102.    
    103.    
    104. }[/SIZE][/FONT][/SIZE][/FONT][/COLOR][/FONT]
    105.  
    106.  
     
    Last edited: Oct 9, 2009
  2. CalvinBarns

    New Member

    Jan 10, 2010
    1
    0
    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: Jan 31, 2010
Loading...