diff --git a/Nuwan/Client.java b/Nuwan/Client.java index 4c42250..10c29cc 100644 --- a/Nuwan/Client.java +++ b/Nuwan/Client.java @@ -22,5 +22,6 @@ public static void main(String[] args) throws UnknownHostException, Interrupted ex.printStackTrace(); } + in.close(); } } diff --git a/Test/AudioSession.java b/Test/AudioSession.java index 2b2d533..5bb7045 100644 --- a/Test/AudioSession.java +++ b/Test/AudioSession.java @@ -19,188 +19,187 @@ public class AudioSession { - boolean stopCapture = false; - ByteArrayOutputStream byteArrayOutputStream; - AudioFormat audioFormat; - TargetDataLine targetDataLine; - AudioInputStream audioInputStream; - SourceDataLine sourceDataLine; - byte tempBuffer[] = new byte[500]; - Session peer; - - - public AudioSession(Session peer) { - this.peer = peer; - } - - private AudioFormat getAudioFormat() { - float sampleRate = 16000.0F; - int sampleSizeInBits = 16; - int channels = 2; - boolean signed = true; - boolean bigEndian = true; - return new AudioFormat(sampleRate, sampleSizeInBits, channels, signed, bigEndian); - } - - public void captureAudio() { - - try { - Mixer.Info[] mixerInfo = AudioSystem.getMixerInfo(); //get available mixers - System.out.println("Available mixers:"); - Mixer mixer = null; - for (int cnt = 0; cnt < mixerInfo.length; cnt++) { - System.out.println(cnt + " " + mixerInfo[cnt].getName()); - mixer = AudioSystem.getMixer(mixerInfo[cnt]); - - Line.Info[] lineInfos = mixer.getTargetLineInfo(); - if (lineInfos.length >= 1 && lineInfos[0].getLineClass().equals(TargetDataLine.class)) { - System.out.println(cnt + " Mic is supported!"); - break; - } + boolean stopCapture = false; + ByteArrayOutputStream byteArrayOutputStream; + AudioFormat audioFormat; + TargetDataLine targetDataLine; + AudioInputStream audioInputStream; + SourceDataLine sourceDataLine; + byte tempBuffer[] = new byte[500]; + Session peer; + + + public AudioSession(Session peer) { + this.peer = peer; + } + + private AudioFormat getAudioFormat() { + float sampleRate = 16000.0F; + int sampleSizeInBits = 16; + int channels = 2; + boolean signed = true; + boolean bigEndian = true; + return new AudioFormat(sampleRate, sampleSizeInBits, channels, signed, bigEndian); + } + + public void captureAudio() { + + try { + Mixer.Info[] mixerInfo = AudioSystem.getMixerInfo(); //get available mixers + System.out.println("Available mixers:"); + Mixer mixer = null; + for (int cnt = 0; cnt < mixerInfo.length; cnt++) { + System.out.println(cnt + " " + mixerInfo[cnt].getName()); + mixer = AudioSystem.getMixer(mixerInfo[cnt]); + + Line.Info[] lineInfos = mixer.getTargetLineInfo(); + if (lineInfos.length >= 1 && lineInfos[0].getLineClass().equals(TargetDataLine.class)) { + System.out.println(cnt + " Mic is supported!"); + break; + } + } + + audioFormat = getAudioFormat(); //get the audio format + DataLine.Info dataLineInfo = new DataLine.Info(TargetDataLine.class, audioFormat); + + targetDataLine = (TargetDataLine) mixer.getLine(dataLineInfo); + targetDataLine.open(audioFormat); + targetDataLine.start(); + + DataLine.Info dataLineInfo1 = new DataLine.Info(SourceDataLine.class, audioFormat); + sourceDataLine = (SourceDataLine) AudioSystem.getLine(dataLineInfo1); + sourceDataLine.open(audioFormat); + sourceDataLine.start(); + + //Setting the maximum volume + FloatControl control = (FloatControl)sourceDataLine.getControl(FloatControl.Type.MASTER_GAIN); + control.setValue(control.getMaximum()); + + } catch (LineUnavailableException e) { + System.out.println(e); + System.exit(0); + } + + } + public void capture() { + byteArrayOutputStream = new ByteArrayOutputStream(); + stopCapture = false; + + try { + int seq = 0; + //Record non-stop + while (!stopCapture) { + + //Read from mic and store in temp buffer + targetDataLine.read(tempBuffer, 0, tempBuffer.length); //capture sound into tempBuffer + seq = seq%16; + tempBuffer[499] = (byte)seq++; + System.out.println(tempBuffer[499]); + + //Send whats in buffer to the server using sockets + DatagramPacket packet = new DatagramPacket(tempBuffer, tempBuffer.length, peer.ip, peer.port); + peer.socket.send(packet); + } + byteArrayOutputStream.close(); + } catch (IOException e) { + System.out.println(e); + System.exit(0); } + } - audioFormat = getAudioFormat(); //get the audio format - DataLine.Info dataLineInfo = new DataLine.Info(TargetDataLine.class, audioFormat); - - targetDataLine = (TargetDataLine) mixer.getLine(dataLineInfo); - targetDataLine.open(audioFormat); - targetDataLine.start(); - - DataLine.Info dataLineInfo1 = new DataLine.Info(SourceDataLine.class, audioFormat); - sourceDataLine = (SourceDataLine) AudioSystem.getLine(dataLineInfo1); - sourceDataLine.open(audioFormat); - sourceDataLine.start(); - - //Setting the maximum volume - FloatControl control = (FloatControl)sourceDataLine.getControl(FloatControl.Type.MASTER_GAIN); - control.setValue(control.getMaximum()); - - } catch (LineUnavailableException e) { - System.out.println(e); - System.exit(0); - } - - } - public void capture() { - byteArrayOutputStream = new ByteArrayOutputStream(); - stopCapture = false; - - try { - int seq = 0; - //Record non-stop - while (!stopCapture) { - - //Read from mic and store in temp buffer - targetDataLine.read(tempBuffer, 0, tempBuffer.length); //capture sound into tempBuffer - seq = seq%16; - tempBuffer[499] = (byte)seq++; - System.out.println(tempBuffer[499]); - - //Send whats in buffer to the server using sockets - DatagramPacket packet = new DatagramPacket(tempBuffer, tempBuffer.length, peer.ip, peer.port); - peer.socket.send(packet); + private int getHashId(String ip, int port){ + int id = 1; + for(int i=0;i clientList = new HashMap(); - - try { - byte[] buffer=new byte[500]; - - //Play non-stop - while (!stopCapture) { - - DatagramPacket packet=new DatagramPacket(buffer, buffer.length); - - peer.socket0.receive(packet); - String sourceIp = packet.getAddress().getHostName(); - int sourcePort = packet.getPort(); - - int hashId = getHashId(sourceIp,sourcePort); - Client client; - - if(clientList.containsKey(hashId)){ - client = clientList.get(hashId); - } - else{ - client = new Client(); - clientList.put(hashId,client); - } - - int userId = client.userId; - buffer = packet.getData(); - - /*System.out.println(userId+": "+buffer[499]); - sourceDataLine.write(buffer, 0, 500); //playing audio available in tempBuffer*/ - - //Packet re-arranging algorithm - - //------------------------------------------------------------------------------------------------------ - if (buffer[499] >= 0 && buffer[499] <= 15) { - - int currentPacket = buffer[499]; - System.out.println("Expected("+userId+"): "+client.seq+" "+"Arrived: "+currentPacket); - if(currentPacket != client.seq) { - System.out.println("Not in Sequence"); - if(client.memBuffer[client.seq] == null) { - System.out.println("Not in Buffer"); - client.memBuffer[currentPacket] = Arrays.copyOf(buffer, 500); - ++client.packetLoss; - if(client.packetLoss > 3){ - client.packetLoss = 0; - continue; - } - else{ - continue; - } + return id%500; + } + + public void play() { + byteArrayOutputStream = new ByteArrayOutputStream(); + stopCapture = false; + HashMap clientList = new HashMap(); + + try { + byte[] buffer=new byte[500]; + + //Play non-stop + while (!stopCapture) { + + DatagramPacket packet=new DatagramPacket(buffer, buffer.length); + + peer.socket0.receive(packet); + String sourceIp = packet.getAddress().getHostName(); + int sourcePort = packet.getPort(); + + int hashId = getHashId(sourceIp,sourcePort); + Client client; + + if(clientList.containsKey(hashId)){ + client = clientList.get(hashId); } else{ - System.out.println("Exist in Buffer: "+client.seq); - buffer = Arrays.copyOf(client.memBuffer[client.seq], 500); - client.memBuffer[client.seq] = null; + client = new Client(); + clientList.put(hashId,client); + } + + int userId = client.userId; + buffer = packet.getData(); + + /*System.out.println(userId+": "+buffer[499]); + sourceDataLine.write(buffer, 0, 500); //playing audio available in tempBuffer*/ + //Packet re-arranging algorithm + //------------------------------------------------------------------------------------------------------ + + if (buffer[499] >= 0 && buffer[499] <= 15) { + + int currentPacket = buffer[499]; + System.out.println("Expected("+userId+"): "+client.seq+" "+"Arrived: "+currentPacket); + if(currentPacket != client.seq) { + System.out.println("Not in Sequence"); + if(client.memBuffer[client.seq] == null) { + System.out.println("Not in Buffer"); + client.memBuffer[currentPacket] = Arrays.copyOf(buffer, 500); + ++client.packetLoss; + if(client.packetLoss > 3){ + client.packetLoss = 0; + continue; + } + else{ + continue; + } + } + else{ + System.out.println("Exist in Buffer: "+client.seq); + buffer = Arrays.copyOf(client.memBuffer[client.seq], 500); + client.memBuffer[client.seq] = null; + } + } + //------------------------------------------------------------------------------------------------------ + + //Play data in temp buffer + byteArrayOutputStream.write(buffer, 0, 500); + System.out.println("Playing: "+buffer[499]); + sourceDataLine.write(buffer, 0, 500); //playing audio available in tempBuffer + + //-------------------------------------------------------------------------------------------------------- + ++client.seq; + client.seq %= 16; + if(client.seq == 0){ + client.memBuffer = client.initializeMemBuffer(); + System.out.println("User: "+userId+" clearing buffer"); + } } - } - //------------------------------------------------------------------------------------------------------ - - //Play data in temp buffer - byteArrayOutputStream.write(buffer, 0, 500); - System.out.println("Playing: "+buffer[499]); - sourceDataLine.write(buffer, 0, 500); //playing audio available in tempBuffer - - //-------------------------------------------------------------------------------------------------------- - ++client.seq; - client.seq %= 16; - if(client.seq == 0){ - client.memBuffer = client.initializeMemBuffer(); - System.out.println("User: "+userId+" clearing buffer"); - } - } + } + byteArrayOutputStream.close(); + } catch (IOException e) { + System.out.println(e); + System.exit(0); } - byteArrayOutputStream.close(); - } catch (IOException e) { - System.out.println(e); - System.exit(0); - } - } + } } diff --git a/Test/Peer.java b/Test/Peer.java new file mode 100644 index 0000000..63919c3 --- /dev/null +++ b/Test/Peer.java @@ -0,0 +1,42 @@ +import java.io.IOException; +import java.net.UnknownHostException; +import java.util.Scanner; + + +public class Peer{ + + + public static void main(String[] args) throws UnknownHostException, InterruptedException { + + Scanner in = new Scanner(System.in); + + UDPMulticastServer sender = new UDPMulticastServer(); + UDPMulticastClient receiver = new UDPMulticastClient(); + + Thread threadSender = new Thread(sender); + Thread threadReceiver = new Thread(receiver); + + threadSender.start(); + //threadReceiver.start(); + + try{ + while(true){ + System.out.print("\n\nEnter (1=start, 0=stop): "); + int cmd = in.nextInt(); + + if(cmd==1){ + System.out.println("Send Start"); + threadSender.start(); + }else{ + System.out.println("Send Stop"); + //ps.sendStop(); + Thread.sleep(50); + } + } + }catch (Exception ex) { + ex.printStackTrace(); + } + + in.close(); + } +} diff --git a/Test/UDPMulticastClient.java b/Test/UDPMulticastClient.java index d03a5b5..65ce931 100644 --- a/Test/UDPMulticastClient.java +++ b/Test/UDPMulticastClient.java @@ -4,32 +4,42 @@ import java.net.MulticastSocket; import java.util.*; +import java.net.UnknownHostException; + public class UDPMulticastClient implements Runnable { - public static void main(String[] args) { - Thread t=new Thread(new UDPMulticastClient()); - t.start(); - } + private MulticastSocket socket; + private Session peer; + AudioSession audio; + InetAddress group; + + public UDPMulticastClient() throws UnknownHostException, InterruptedException{ + + try{ + socket=new MulticastSocket(4321); + group = InetAddress.getByName("230.0.0.0"); + peer = new Session(socket, group); + audio = new AudioSession(peer); - @Override - public void run(){ + } catch (IOException ex) { + ex.printStackTrace(); + } - try { - MulticastSocket socket=new MulticastSocket(4321); - InetAddress group = InetAddress.getByName("230.0.0.0"); + } - Session peer = new Session(socket, group); - AudioSession audio = new AudioSession(peer); - socket.joinGroup(group); + @Override + public void run(){ + try { + socket.joinGroup(group); - audio.captureAudio(); - audio.play(); + audio.captureAudio(); + audio.play(); - socket.leaveGroup(group); - socket.close(); + socket.leaveGroup(group); + socket.close(); - }catch(IOException ex){ - ex.printStackTrace(); - } - } + }catch(IOException ex){ + ex.printStackTrace(); + } + } } diff --git a/Test/UDPMulticastServer.java b/Test/UDPMulticastServer.java index 1442491..2c28f05 100644 --- a/Test/UDPMulticastServer.java +++ b/Test/UDPMulticastServer.java @@ -4,21 +4,41 @@ import java.net.InetAddress; import java.util.*; -public class UDPMulticastServer { +import java.io.IOException; +import java.net.UnknownHostException; + +public class UDPMulticastServer implements Runnable{ + private Session peer; + private AudioSession audio; + private DatagramSocket socket; + + public UDPMulticastServer() throws UnknownHostException, InterruptedException{ + // throws IOException + + try{ + // Create a Datagram Socket which can push data into the multicast group + socket = new DatagramSocket(); + } catch (IOException ex) { + ex.printStackTrace(); + } - public static void main(String[] args) throws IOException { + InetAddress group = InetAddress.getByName("230.0.0.0"); + int port = 4321; - DatagramSocket socket = new DatagramSocket(); - InetAddress group = InetAddress.getByName("230.0.0.0"); - int port = 4321; + peer = new Session(socket, group, port); + audio = new AudioSession(peer); - Session peer = new Session(socket, group, port); - AudioSession audio = new AudioSession(peer); + /*audio.captureAudio(); + audio.capture(); + socket.close();*/ + } - audio.captureAudio(); - audio.capture(); + @Override + public void run(){ - socket.close(); - } + audio.captureAudio(); + audio.capture(); + socket.close(); + } }