I am trying to implement the voice chat using java multiCastSocket programming. Taking the input from the microphone and trying to listen it on the speaker. Problem is am not hear any voice back. I debugged through it wireshark and i am receiving the packets from the multicast group address. Looks like there is some problem while i am trying to get the sound out of speaker. Can someone please help me out. I have pasted my code below. Thanks in advance.
/*Sender Code*/ public class MulticastAudioSender { /** Port to use for IP Multicast sending. */ public static final int IPM_PORT = 7778; /** Address to use for IP Multicast sending. */ public static final String IPM_ADDRESS = "235.1.1.1"; /** Message text to send. */ public static final String IPM_MSG = "Hello UTD!!"; /** Time to wait between sending messages, in milliseconds. */ public static final int SLEEP_MILLISECS = 1000; /** UDP Socket object to use for networking. */ private MulticastSocket sock; /** UPD Packet object to use for sending message. */ private DatagramPacket sendPack; /** Size of receive buffer, in bytes. */ public static final int BUFFER_SIZE = 20000; /** Default constructor: sets up networking. */ public MulticastAudioSender(){ try { sock = new MulticastSocket(); } catch( Exception e ) { System.out.println( "Exception in creating socket or packet: " + e.getMessage() ); } } private void sendMessage(byte[] soundData, int length) { try { sendPack = new DatagramPacket( soundData, length, InetAddress.getByName( IPM_ADDRESS ), IPM_PORT ); sock.send( sendPack ); //System.out.println( "Message sent." ); } catch( Exception e ) { System.out.println( "Exception in sending packet: " + e.getMessage() ); } } public static void main( String[] args ) throws Exception{ MulticastAudioSender sender = new MulticastAudioSender(); AudioFormat af = new AudioFormat(8000.0f,8,1,true,false); DataLine.Info info = new DataLine.Info(TargetDataLine.class, af); TargetDataLine microphone = (TargetDataLine)AudioSystem.getLine(info); microphone.open(af); microphone.start(); int bytesRead = 0; byte[] soundData = new byte[1]; //sender.receiveMessage(); while(bytesRead != -1) { bytesRead = microphone.read(soundData, 0, soundData.length); if(bytesRead >= 0){ sender.sendMessage(soundData, soundData.length); } } } }
public class IPMulticastReceiver { /** Port to use for IP Multicast receiving. */ public static final int IPM_PORT = 7778; /** IP Multicast address to receive on. */ public static final String IPM_ADDRESS = "235.1.1.1"; /** Size of receive buffer, in bytes. */ public static final int BUFFER_SIZE = 200; /** IP Multicast Socket object to use. */ private MulticastSocket sock; /** UDP Packet object to use for receiving message. */ private DatagramPacket pack; /** Buffer to store received text in. */ private byte[] receiveBuffer; byte[] inSound; SourceDataLine inSpeaker = null; /** Default constructor: sets up IP MultiCast Receiving. */ public IPMulticastReceiver(){ try { sock = new MulticastSocket( IPM_PORT ); sock.joinGroup( InetAddress.getByName( IPM_ADDRESS ) ); AudioFormat af = new AudioFormat(8000.0f,8,1,true,false); DataLine.Info info = new DataLine.Info(SourceDataLine.class, af); inSpeaker = (SourceDataLine)AudioSystem.getLine(info); inSpeaker.open(af); } catch( Exception e ) { System.out.println( "Exception in creating Socket or Packet: " + e.getMessage() ); } } private void receiveMessage() { try { receiveBuffer = new byte[BUFFER_SIZE]; //sock.joinGroup(InetAddress.getByName(IPM_ADDRESS)); pack = new DatagramPacket( receiveBuffer, receiveBuffer.length ); sock.receive( pack ); /*Thread inThread = new Thread(new MultiCastSoundReceiver(receiveBuffer)); inThread.start();*/ inSpeaker.write(receiveBuffer, 0, receiveBuffer.length); inSpeaker.drain(); } catch( Exception e ) { e.printStackTrace(); System.out.println( "Exception in receiving packet: " + e.getMessage() ); } } public static void main( String[] args ) throws LineUnavailableException { IPMulticastReceiver receiver = new IPMulticastReceiver(); while( true ) { receiver.receiveMessage(); } } }