Commit 87cd4e6f authored by Till Hennig Kobbe's avatar Till Hennig Kobbe
Browse files

An java Audio Api angebunden. Sound wird übertragen und abgespielt

parent abfb1e0b
package voip.client;
public class Client {
public static void main(String args[]) {
import java.io.IOException;
import java.net.DatagramSocket;
import java.net.Socket;
public class Client implements Runnable {
private static final String HOST = System.getProperty("host", "localhost");
private static final int PORT = Integer.parseInt(System.getProperty("port", "8080"));
private Socket socket;
@Override
public void run() {
try {
socket = new Socket(HOST, PORT);
} catch (IOException exception) {
System.err.println("Socket creation failed!");
exception.printStackTrace();
System.exit(1);
}
if (socket != null && socket.isConnected()) {
System.out.println("Connected");
}
MicrophoneThread microphoneThread = new MicrophoneThread(socket);
microphoneThread.start();
SpeakerThread speakerThread = new SpeakerThread(socket);
speakerThread.start();
}
public static void main(String[] args) {
Client client = new Client();
new Thread(client).start();
}
}
package voip.client;
import javax.sound.sampled.*;
public class Microphone {
private TargetDataLine line;
private AudioFormat format;
public Microphone() {
format = new AudioFormat(8000.0f, 16, 1, true, false);
}
/**
* Opens the user microphone
* @return
*/
public boolean open() {
DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
boolean lineOpen = false;
try {
line = (TargetDataLine) AudioSystem.getLine(info);
line.open();
lineOpen = true;
} catch (LineUnavailableException e) {
lineOpen = false;
System.err.println("Error while opening your microphone");
}
return lineOpen;
}
public int read(byte[] buf, int off, int len) {
return line.read(buf, off, len);
}
public void start() {
line.start();
}
public void stop() {
line.stop();
line.flush();
}
public int getBufferSize() {
return line.getBufferSize();
}
}
package voip.client;
import java.io.IOException;
import java.net.Socket;
public class MicrophoneThread extends Thread {
private Microphone microphone;
private Socket socket;
public MicrophoneThread(Socket socket) {
this.socket = socket;
this.microphone = new Microphone();
}
@Override
public void run() {
if (!this.microphone.open()) return;
microphone.start();
while (socket.isConnected()) {
try {
byte[] buffer = new byte[microphone.getBufferSize() / 5];
int read = microphone.read(buffer, 0, buffer.length);
System.out.println("Sending data");
socket.getOutputStream().write(buffer, 0, read);
} catch (Exception exception) {
System.err.println("Writing failed. Maybe the server closed the connection.");
microphone.stop();
return;
}
}
microphone.stop();
}
}
package voip.client;
import javax.sound.sampled.*;
public class Speaker {
private SourceDataLine line;
private AudioFormat format;
public Speaker() {
format = new AudioFormat(8000.0f, 16, 1, true, false);
}
/**
* Opens the user microphone
* @return
*/
public boolean open() {
DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
boolean lineOpen = false;
try {
line = (SourceDataLine) AudioSystem.getLine(info);
line.open();
lineOpen = true;
} catch (LineUnavailableException e) {
lineOpen = false;
System.err.println("Error while opening your speakers");
}
return lineOpen;
}
public int write(byte[] buf, int off, int len) {
return line.write(buf, off, len);
}
public void start() {
line.start();
}
public void stop() {
line.stop();
line.flush();
}
public int getBufferSize() {
return line.getBufferSize();
}
}
package voip.client;
import java.io.IOException;
import java.net.Socket;
public class SpeakerThread extends Thread {
private Speaker speaker;
private Socket socket;
public SpeakerThread(Socket socket) {
this.socket = socket;
this.speaker = new Speaker();
}
@Override
public void run() {
if (!speaker.open()) return;
speaker.start();
while (socket.isConnected()) {
try {
byte[] buffer = new byte[speaker.getBufferSize()];
socket.getInputStream().read(buffer);
System.out.println("Received data");
speaker.write(buffer, 0, buffer.length);
} catch (IOException exception) {
System.err.println("Writing failed. Maybe the server closed the connection.");
speaker.stop();
return;
}
}
speaker.stop();
}
}
......@@ -2,6 +2,7 @@ package voip.server;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
......@@ -32,27 +33,22 @@ public class Server {
}
}
public static void main(String[] args) {
Server server = new Server();
try {
server.acceptConnection(Integer.parseInt(args[0]));
} catch (IOException exception) {
System.out.println("Fehler beim akzeptieren der Verbindung");
}
}
/**
* This function writes the message to all client sockets except the given id
* @param message
* @param buffer
*/
public void writeToAllOther(String message, int id) {
public void writeToAllOther(byte[] buffer, int id) {
for (ServerThread client : clients) {
if (id == client.getId()) continue;
Socket socket = client.getSocket();
DataOutputStream out = null;
if (!socket.isConnected()) {
clients.remove(client);
return;
}
OutputStream out;
try {
out = new DataOutputStream(socket.getOutputStream());
out.writeBytes(message + "\n\r");
out = socket.getOutputStream();
out.write(buffer, 0, buffer.length);
out.flush();
} catch (IOException exception) {
exception.printStackTrace();
......@@ -60,4 +56,15 @@ public class Server {
}
}
}
public static void main(String[] args) {
Server server = new Server();
try {
server.acceptConnection(Integer.parseInt(args[0]));
} catch (IOException exception) {
System.out.println("Fehler beim akzeptieren der Verbindung");
exception.printStackTrace();
System.exit(1);
}
}
}
......@@ -2,6 +2,8 @@ package voip.server;
import java.io.*;
import java.net.Socket;
import java.net.SocketException;
import java.nio.ByteBuffer;
/**
* This class handles incoming messages from the client
......@@ -34,16 +36,17 @@ public class ServerThread extends Thread {
@Override
public void run() {
BufferedReader reader = new BufferedReader(new InputStreamReader(this.inputStream));
String line = null;
while (true) {
byte[] buffer = null;
while (socket.isConnected()) {
try {
line = reader.readLine();
} catch (IOException exception) {
exception.printStackTrace();
buffer = new byte[this.inputStream.available()];
this.inputStream.read(buffer);
} catch (Exception exception) {
System.err.println("Error reading to buffer");
}
if (line != null) {
server.writeToAllOther(line, this.id);
if (buffer != null) {
System.out.println("Received data from " + id);
server.writeToAllOther(buffer, this.id);
}
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment