-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPlayStream.java
134 lines (90 loc) · 3.23 KB
/
PlayStream.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
/*
* PlayStream.java
*
* Version:
* 1.0
*
* Revisions:
* 0
*/
import java.io.*;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.UnknownHostException;
import javax.sound.sampled.*;
/**
* Class used to create the instance of a task to play the audio segment
* that was received over the network. This task is then supplied to a thread for
* concurrent execution.
*
* @author Siddharth Kalluru
* @author Vijaykumar Chandrashekar
*/
public class PlayStream implements Runnable{
boolean stopPlayback;
DatagramSocket objStreamSock;
AudioFormat objAudioFormat;
/**
* Constructor to initialize the instance of HostUI
*
* @param objStreamSock Datagramsocket to receive the voice stream
*
*/
public PlayStream(DatagramSocket objStreamSock) {
this.objStreamSock = objStreamSock;
stopPlayback = false;
//Format of audio to played - Sampled @ 16Khz, SampleSize(PCM), Mono
objAudioFormat = new AudioFormat(16000.0f, 16, 1, true, true);
}
@Override
public void run() {
try{
DatagramPacket recPkt;
InputStream ipStreamFrmDataPkt;
AudioInputStream audioIpStreamFrmDataPkt;
DataLine.Info objLineInfo;
SourceDataLine objSpeaker;
int i=0;
System.out.println("Stream playback begun.");
//Repeat till stop flag is set
while(!stopPlayback){
//Receive packet - blocks till the packet is received
recPkt = new DatagramPacket(new byte[3000], 3000);
this.objStreamSock.receive(recPkt);
System.out.println("Stream packet "+ ++i + "received");
//Extract data into a stream from the received pkt
ipStreamFrmDataPkt = new ByteArrayInputStream(recPkt.getData());
//Wrap the stream into an Audio Stream
audioIpStreamFrmDataPkt = new AudioInputStream(ipStreamFrmDataPkt,
objAudioFormat,
recPkt.getData().length / objAudioFormat.getFrameSize());
objAudioFormat = audioIpStreamFrmDataPkt.getFormat();
objLineInfo = new DataLine.Info(SourceDataLine.class, objAudioFormat);
int cnt = 0;
byte tempBuffer[] = new byte[10000];
try {
objSpeaker = (SourceDataLine) AudioSystem.getLine(objLineInfo);
objSpeaker.open(objAudioFormat);
objSpeaker.start();
while ((cnt = audioIpStreamFrmDataPkt.read(tempBuffer, 0,tempBuffer.length)) != -1) {
if (cnt > 0) {
//Write into the buffer of the speaker, that'll
//eventually be played
objSpeaker.write(tempBuffer, 0, cnt);
}
}
//Empty the buffer of the speaker
objSpeaker.drain();
objSpeaker.close();
}
catch(LineUnavailableException | IOException e1){
e1.printStackTrace();
}
}
System.out.println("Stream playback halted.");
}
catch(Exception e){
e.printStackTrace();
}
}
}