-
Notifications
You must be signed in to change notification settings - Fork 2
/
Peer.java
206 lines (170 loc) · 6.28 KB
/
Peer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import java.io.*;
import java.text.*;
import java.util.*;
import java.net.*;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.FloatControl;
import javax.sound.sampled.Line;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.Mixer;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.TargetDataLine;
// Server class
public class Peer
{
final DataInputStream dis;
final DataOutputStream dos;
final Socket s;
static byte[][] memBuffer = new byte[16][500];
static int packetCount = 0;
boolean stopCapture = false;
ByteArrayOutputStream byteArrayOutputStream;
AudioFormat audioFormat;
TargetDataLine targetDataLine;
AudioInputStream audioInputStream;
SourceDataLine sourceDataLine;
byte tempBuffer[] = new byte[500];
public Peer(Socket s, DataInputStream dis, DataOutputStream dos)
{
this.s = s;
this.dis = dis;
this.dos = dos;
}
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);
}
private 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());
captureAndPlay(); //playing the audio
} catch (LineUnavailableException e) {
System.out.println(e);
System.exit(0);
}
}
private void captureAndPlay() {
byteArrayOutputStream = new ByteArrayOutputStream();
stopCapture = false;
try {
int readCount;
//Play non-stop
while (!stopCapture) {
//Listen to sockets for any data. Record any incoming data to temp buffer
readCount = dis.read(tempBuffer);
//Packet re-arranging algorithm
//------------------------------------------------------------------------------------------------------
if (readCount > 0 && (tempBuffer[499] >= 0 && tempBuffer[499] <= 15)) {
int currentPacket = tempBuffer[499];
System.out.println("Expected: "+packetCount+" "+"Arrived: "+currentPacket);
if(currentPacket != packetCount){
System.out.println("Not in Sequence");
if(memBuffer[packetCount] == null){
System.out.println("Not in Buffer");
memBuffer[currentPacket] = Arrays.copyOf(tempBuffer, 500);
int packets=0;
do{
System.out.println("Wait packets for: " + packetCount);
readCount = dis.read(tempBuffer);
++packets;
if(readCount > 0 && (tempBuffer[499] >= 0 && tempBuffer[499] <= 15)){
currentPacket = tempBuffer[499];
memBuffer[currentPacket] = Arrays.copyOf(tempBuffer, 500);
System.out.println("Into buffer: " + currentPacket);
}
}while(currentPacket != packetCount && packets < 5);
if(packets == 5){
System.out.println("Packet Drop");
++packetCount;
packetCount %= 16;
continue;
}
}
else{
System.out.println("Exist in Buffer: "+packetCount);
tempBuffer = Arrays.copyOf(memBuffer[packetCount], 500);
memBuffer[packetCount] = null;
}
}
//------------------------------------------------------------------------------------------------------
//Play data in temp buffer
byteArrayOutputStream.write(tempBuffer, 0, 500);
System.out.println("Playing: "+tempBuffer[499]);
sourceDataLine.write(tempBuffer, 0, 500); //playing audio available in tempBuffer
//--------------------------------------------------------------------------------------------------------
++packetCount;
packetCount %= 16;
if(packetCount == 0){
initializeMemBuffer();
}
}
}
byteArrayOutputStream.close();
} catch (IOException e) {
System.out.println(e);
System.exit(0);
}
}
private static void initializeMemBuffer(){
for(int i=0;i<16;++i)
{
memBuffer[i] = null;
}
}
public static void main(String[] args) throws IOException
{
ServerSocket ss = new ServerSocket(50000);
Socket s = null;
try
{
s = ss.accept();
System.out.println("A new client is connected : " + s);
// obtaining input and out streams
DataInputStream dis = new DataInputStream(s.getInputStream());
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
System.out.println("Assigning new thread for this client");
Peer peer = new Peer(s,dis,dos);
initializeMemBuffer();
peer.captureAudio();
s.close();
dis.close();
dos.close();
}
catch (Exception e){
s.close();
e.printStackTrace();
}
}
}