Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Streaming using UDP

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Streaming using UDP

    I wrote this streaming audio application in java.
    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.

    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[i] ) ;    
        }
     
     
        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 " ) ;   
           }
     
        }
     
     
    }

    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 );
        }
     
     
    }


  2. #2
    Member
    Join Date
    Feb 2011
    Posts
    55
    My Mood
    Tolerant
    Thanks
    1
    Thanked 16 Times in 15 Posts

    Default Re: Streaming using UDP

    I haven't take a look at the code yet, but can you tell us why you chose udp? Because from what i understand, udp give no guarantee of the order of the streaming packets, thus, unless the code accounts for packet order, the audio may be playing the packets in the wrong order.
    example:
    Server sends 3 packets in order : A, B, C
    Client receives and plays packets in order : A, C, B

    You may want to try tcp, it may be slower, but it guarantees order. Or try to implement a strategy to keep the packets in order and drop the late packets, or buffer the data to try to reorder the data on the client side before it's played.

    Hope this helps.

  3. #3
    Junior Member
    Join Date
    Mar 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Streaming using UDP

    Quote Originally Posted by JJeng View Post
    I haven't take a look at the code yet, but can you tell us why you chose udp? Because from what i understand, udp give no guarantee of the order of the streaming packets, thus, unless the code accounts for packet order, the audio may be playing the packets in the wrong order.
    example:
    Server sends 3 packets in order : A, B, C
    Client receives and plays packets in order : A, C, B

    You may want to try tcp, it may be slower, but it guarantees order. Or try to implement a strategy to keep the packets in order and drop the late packets, or buffer the data to try to reorder the data on the client side before it's played.

    Hope this helps.

    I chose UDP because i send Multicast message

  4. #4
    Member
    Join Date
    Feb 2011
    Posts
    55
    My Mood
    Tolerant
    Thanks
    1
    Thanked 16 Times in 15 Posts

    Default Re: Streaming using UDP

    Well, I'm not to sure how to eliminate the choppyness, but increasing the size of the byte array definitely helps. Start around 32000 and play with it, the main reason for this is, the AudioFormat is set to 8000 samples per second at 16 bits or 2 bytes per sample, resulting in 16000 bytes per second.

    Also the RadioReceiver is not set up as a multicast. Easy to change though, change the ip to a valid multicast address and change the datagram socket to a MulticastSocket and call the joinGroup() method.

    Also, you seem to have a lot of local resource overhead, every packet calls for the creation of a new Datagram Socket/Packet and the constant opening and closing and allocation of those resources add to the choppyness and delay. Try making an initialization method to call the creation of the array, socket and packet and reuse them, the only thing that should be in your while loop should be sock.receive(datagram) on the Radio and targetDataLine.read and sock.send in the player.

    Good luck, I hope this is a step in a better direction.
    Other members, please chime in if you can help.

  5. #5
    Junior Member
    Join Date
    Feb 2019
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Streaming using UDP

    Hello ,

    I would like to know if you find a good solution for receiving the audio stream correctly.

    Thanks[COLOR="Silver"]

  6. #6
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: Streaming using UDP

    That conversation is about 8 years old. Don't hold your breath.

    Regards,
    Jim

Similar Threads

  1. Streaming Raw Data to a Client - huge message loss
    By MarkusTandy in forum Java Networking
    Replies: 0
    Last Post: February 19th, 2011, 07:41 PM
  2. RTSP Streaming
    By jskywalker in forum Java ME (Mobile Edition)
    Replies: 0
    Last Post: January 20th, 2011, 05:55 AM
  3. Need java / jar app for streaming shoutcast
    By Java_LoveR in forum Java ME (Mobile Edition)
    Replies: 1
    Last Post: October 22nd, 2010, 05:19 AM