-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPeer2Peer.java
242 lines (175 loc) · 7.79 KB
/
Peer2Peer.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*
* CO 324 - Network and Web Application Design
* Assignment - Sample Code
*/
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;
class Play extends Peer2Peer implements Runnable {
private final int packetsize = 100;
private final int port = 55001;
public void run() {
try {
// Construct the socket
DatagramSocket socket = new DatagramSocket(this.port);
System.out.println("The server is ready");
// Create a packet
DatagramPacket packet = new DatagramPacket(new byte[this.packetsize], (this.packetsize));
this.playAudio();
for (;;) {
try {
// Receive a packet (blocking)
socket.receive(packet);
// Print the packet
this.getSourceDataLine().write(packet.getData(), 0, this.packetsize); //playing the audio
} catch (Exception e) {
e.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
public class Peer2Peer implements Runnable {
boolean stopCapture = false;
AudioFormat audioFormat; //class that specifies a particular arrangement of data in a sound stream.
TargetDataLine targetDataLine; // a type of Dataline from which audio data is captured
AudioInputStream audioInputStream;
SourceDataLine sourceDataLine; //a data line to which data may be written.
byte tempBuffer[] = new byte[100];
int packetsize = 100;
//int port = 5200;
InetAddress host = null;
DatagramSocket socket = null;
ByteArrayOutputStream byteArrayOutputStream = null; //creates a buffer in memory and all the data sent to the stream is stored in the buffer.
public static void main(String[] args) {
// Check the whether the arguments are given
if (args.length != 1) {
System.out.println("Enter : java Peer2Peer <ip_address> ");
return;
}
// Peer2Peer playback = new Peer2Peer();
// playback.captureAudio();
try {
Thread cap = new Thread(new Peer2Peer(InetAddress.getByName(args[0])));
cap.start(); //start capturing the audio
Thread ply = new Thread(new Play());
ply.start(); //start playing the audio
} catch (Exception e) {
e.printStackTrace();
}
}
/******* Defines an audio format*********/
/*Constructs an AudioFormat with a linear PCM
encoding and the given parameters. The frame size is set to the
number of bytes required to contain one sample from each channel,
and the frame rate is set to the sample rate.*/
public AudioFormat getAudioFormat() { //gets the audio format
// *********fields**************
float sampleRate = 16000.0F; //The number of samples played or recorded per second, for sounds that have this format.
int sampleSizeInBits = 16; //The number of bits in each sample of a sound that has this format.
int channels = 2; //The number of audio channels in this format (1 for mono, 2 for stereo and so on).
boolean signed = true; //indicates whether the data is signed or unsigned
boolean bigEndian = true; // indicates whether the data for a single sample is stored in big-endian byte order (false means little-endian)
// refer to the order that a multi-byte quantity is stored in memory. Big endian places the most significant byte first.
return new AudioFormat(sampleRate, sampleSizeInBits, channels, signed, bigEndian);
}
public synchronized SourceDataLine getSourceDataLine() {
return this.sourceDataLine;
}
public synchronized TargetDataLine getTargetDataLine() {
return this.targetDataLine;
}
public void playAudio() {
try {
audioFormat = getAudioFormat(); //get the audio format
DataLine.Info dataLineInfo1 = new DataLine.Info(SourceDataLine.class, audioFormat);
sourceDataLine = (SourceDataLine) AudioSystem.getLine(dataLineInfo1);
sourceDataLine.open(audioFormat);
sourceDataLine.start(); //allow program to write data
//Setting the maximum volume
FloatControl control = (FloatControl) sourceDataLine.getControl(FloatControl.Type.MASTER_GAIN);
control.setValue(control.getMaximum()/2);
} catch (LineUnavailableException e) {
e.printStackTrace();
System.exit(0);
}
}
public synchronized 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();//start capturing
} catch (LineUnavailableException e) {
System.out.println(e);
System.exit(0);
}
}
private void captureAndSend() {
byteArrayOutputStream = new ByteArrayOutputStream();
stopCapture = false;
try {
int readCount;
int noOfPackets = 0;
while (!stopCapture) {
readCount = targetDataLine.read(tempBuffer, 0, tempBuffer.length); //capture sound into tempBuffer
if (readCount > 0) {
byteArrayOutputStream.write(tempBuffer, 0, readCount);
//sourceDataLine.write(tempBuffer, 0, 500); //playing audio available in tempBuffer
// Construct the datagram packet
DatagramPacket packet = new DatagramPacket(tempBuffer, tempBuffer.length, host,55001);
noOfPackets++;
System.out.println(noOfPackets);
// Send the packet
socket.send(packet);
}
}
byteArrayOutputStream.close();
} catch (IOException e) {
System.out.println(e);
//System.exit(0);
}
}
public void run() {
try {
this.socket = new DatagramSocket(5200);
this.captureAudio();
this.captureAndSend();
} catch (Exception e) {
e.printStackTrace();
// } finally {
this.socket.close();
}
}
public Peer2Peer(InetAddress host) {
this.host = host;
}
public Peer2Peer() {
super();
}
}