package voxo.client.threads;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.EnumSet;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.Mixer;
import javax.sound.sampled.TargetDataLine;
import voxo.common.Verbose;
import voxo.common.enums.EnumVerbose;
public class VoiceSender implements Runnable {
public static Verbose verbose;
@SuppressWarnings("unused")
private String destinationIp;
private boolean go = true;
private ByteArrayOutputStream byteArrayOutputStream;
private AudioFormat audioFormat;
private TargetDataLine targetDataLine;
private BufferedOutputStream out = null;
private Socket sock = null;
byte tempBuffer[] = new byte[10000];
/*
* Constructor
*/
public VoiceSender(String destinationIp) {
this.destinationIp = destinationIp;
try {
sock = new Socket(destinationIp, 5005); // Contacting the voice
// receiver
out = new BufferedOutputStream(sock.getOutputStream()); // UDP
// sending
// the voice
// bytes
audioFormat = getAudioFormat(); // Specify the default application
// audio format
DataLine.Info dataLineInfo = new DataLine.Info(TargetDataLine.class, audioFormat);
// Get the input mixer
Mixer mixer = getMixer();
targetDataLine = (TargetDataLine) mixer.getLine(dataLineInfo);
targetDataLine.open(audioFormat);
targetDataLine.start();
} catch (Exception e) {
verbose.addConsoleMsg(e, EnumSet.of(EnumVerbose.ToConsole, EnumVerbose.ToLog));
killSocket();
}
}
/*
* Audio Processing Methods
*/
private AudioFormat getAudioFormat() {
float sampleRate = 8000.0F;
int sampleSizeInBits = 16;
int channels = 1;
boolean signed = true;
boolean bigEndian = false;
return new AudioFormat(sampleRate, sampleSizeInBits, channels, signed, bigEndian);
}
private Mixer getMixer() {
Mixer.Info[] mixerInfo = AudioSystem.getMixerInfo();
int cnt;
Mixer m = null;
verbose.addConsoleMsg("=Available Mixers=", EnumSet.of(EnumVerbose.ToConsole));
for (cnt = 0; cnt < mixerInfo.length; cnt++) {
verbose.addConsoleMsg(mixerInfo[cnt].getName(), EnumSet.of(EnumVerbose.ToConsole));
}
verbose.addConsoleMsg("==================", EnumSet.of(EnumVerbose.ToConsole));
for (cnt = 0; cnt < mixerInfo.length; cnt++) {
// Windows default english audio input
if (mixerInfo[cnt].getName().equals("Primary Sound Capture Driver")) {
m = AudioSystem.getMixer(mixerInfo[cnt]);
verbose.addConsoleMsg("SELECTED MIXER name: " + mixerInfo[cnt].getName(), EnumSet.of(EnumVerbose.ToConsole));
break;
// French / English Mic input
} else if (mixerInfo[cnt].getName().startsWith("Microphone")) {
m = AudioSystem.getMixer(mixerInfo[cnt]);
verbose.addConsoleMsg("SELECTED MIXER name: " + mixerInfo[cnt].getName(), EnumSet.of(EnumVerbose.ToConsole));
break;
// Linux default audio input
} else if (mixerInfo[cnt].getName().startsWith("default")) {
m = AudioSystem.getMixer(mixerInfo[cnt]);
verbose.addConsoleMsg("SELECTED MIXER name: " + mixerInfo[cnt].getName(), EnumSet.of(EnumVerbose.ToConsole));
break;
}
}
return m;
}
/*
* Sender
*/
public void run() {
byteArrayOutputStream = new ByteArrayOutputStream();
go = true;
try {
while (go) {
int cnt = targetDataLine.read(tempBuffer, 0, tempBuffer.length);
out.write(tempBuffer);
if (cnt > 0) {
byteArrayOutputStream.write(tempBuffer, 0, cnt);
}
}
// Sortir gentilment de ce run
byteArrayOutputStream.close();
targetDataLine.stop();
targetDataLine.close();
try {
if (out != null)
out.close();
} catch (IOException e) {
verbose.addConsoleMsg(e, EnumSet.of(EnumVerbose.ToConsole, EnumVerbose.ToLog));
}
try {
if (sock != null)
sock.close();
} catch (IOException e) {
verbose.addConsoleMsg(e, EnumSet.of(EnumVerbose.ToConsole, EnumVerbose.ToLog));
}
} catch (Exception e) {
verbose.addConsoleMsg(e, EnumSet.of(EnumVerbose.ToConsole, EnumVerbose.ToLog));
}
}
/*
* Getters and Setters
*/
public boolean isGo() {
return go;
}
public void setGo(boolean stopCapture) {
this.go = stopCapture;
}
public void killSocket() {
go = false;
}
}