-
Notifications
You must be signed in to change notification settings - Fork 191
/
Client.java
619 lines (508 loc) · 21.8 KB
/
Client.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
/* ------------------
Client
usage: java Client [Server hostname] [Server RTSP listening port] [Video file requested]
---------------------- */
import java.io.*;
import java.net.*;
import java.util.*;
import java.text.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.Timer;
public class Client {
//GUI
//----
JFrame f = new JFrame("Client");
JButton setupButton = new JButton("Setup");
JButton playButton = new JButton("Play");
JButton pauseButton = new JButton("Pause");
JButton tearButton = new JButton("Close");
JButton describeButton = new JButton("Session");
JPanel mainPanel = new JPanel();
JPanel buttonPanel = new JPanel();
JLabel statLabel1 = new JLabel();
JLabel statLabel2 = new JLabel();
JLabel statLabel3 = new JLabel();
JLabel iconLabel = new JLabel();
ImageIcon icon;
//RTP variables:
//----------------
DatagramPacket rcvdp; //UDP packet received from the server
DatagramSocket RTPsocket; //socket to be used to send and receive UDP packets
static int RTP_RCV_PORT = 25000; //port where the client will receive the RTP packets
Timer timer; //timer used to receive data from the UDP socket
byte[] buf; //buffer used to store data received from the server
//RTSP variables
//----------------
//rtsp states
final static int INIT = 0;
final static int READY = 1;
final static int PLAYING = 2;
static int state; //RTSP state == INIT or READY or PLAYING
Socket RTSPsocket; //socket used to send/receive RTSP messages
InetAddress ServerIPAddr;
//input and output stream filters
static BufferedReader RTSPBufferedReader;
static BufferedWriter RTSPBufferedWriter;
static String VideoFileName; //video file to request to the server
int RTSPSeqNb = 0; //Sequence number of RTSP messages within the session
String RTSPid; // ID of the RTSP session (given by the RTSP Server)
final static String CRLF = "\r\n";
final static String DES_FNAME = "session_info.txt";
//RTCP variables
//----------------
DatagramSocket RTCPsocket; //UDP socket for sending RTCP packets
static int RTCP_RCV_PORT = 19001; //port where the client will receive the RTP packets
static int RTCP_PERIOD = 400; //How often to send RTCP packets
RtcpSender rtcpSender;
//Video constants:
//------------------
static int MJPEG_TYPE = 26; //RTP payload type for MJPEG video
//Statistics variables:
//------------------
double statDataRate; //Rate of video data received in bytes/s
int statTotalBytes; //Total number of bytes received in a session
double statStartTime; //Time in milliseconds when start is pressed
double statTotalPlayTime; //Time in milliseconds of video playing since beginning
float statFractionLost; //Fraction of RTP data packets from sender lost since the prev packet was sent
int statCumLost; //Number of packets lost
int statExpRtpNb; //Expected Sequence number of RTP messages within the session
int statHighSeqNb; //Highest sequence number received in session
FrameSynchronizer fsynch;
//--------------------------
//Constructor
//--------------------------
public Client() {
//build GUI
//--------------------------
//Frame
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
//Buttons
buttonPanel.setLayout(new GridLayout(1,0));
buttonPanel.add(setupButton);
buttonPanel.add(playButton);
buttonPanel.add(pauseButton);
buttonPanel.add(tearButton);
buttonPanel.add(describeButton);
setupButton.addActionListener(new setupButtonListener());
playButton.addActionListener(new playButtonListener());
pauseButton.addActionListener(new pauseButtonListener());
tearButton.addActionListener(new tearButtonListener());
describeButton.addActionListener(new describeButtonListener());
//Statistics
statLabel1.setText("Total Bytes Received: 0");
statLabel2.setText("Packets Lost: 0");
statLabel3.setText("Data Rate (bytes/sec): 0");
//Image display label
iconLabel.setIcon(null);
//frame layout
mainPanel.setLayout(null);
mainPanel.add(iconLabel);
mainPanel.add(buttonPanel);
mainPanel.add(statLabel1);
mainPanel.add(statLabel2);
mainPanel.add(statLabel3);
iconLabel.setBounds(0,0,380,280);
buttonPanel.setBounds(0,280,380,50);
statLabel1.setBounds(0,330,380,20);
statLabel2.setBounds(0,350,380,20);
statLabel3.setBounds(0,370,380,20);
f.getContentPane().add(mainPanel, BorderLayout.CENTER);
f.setSize(new Dimension(380,420));
f.setVisible(true);
//init timer
//--------------------------
timer = new Timer(20, new timerListener());
timer.setInitialDelay(0);
timer.setCoalesce(true);
//init RTCP packet sender
rtcpSender = new RtcpSender(400);
//allocate enough memory for the buffer used to receive data from the server
buf = new byte[15000];
//create the frame synchronizer
fsynch = new FrameSynchronizer(100);
}
//------------------------------------
//main
//------------------------------------
public static void main(String argv[]) throws Exception {
//Create a Client object
Client theClient = new Client();
//get server RTSP port and IP address from the command line
//------------------
int RTSP_server_port = Integer.parseInt(argv[1]);
String ServerHost = argv[0];
theClient.ServerIPAddr = InetAddress.getByName(ServerHost);
//get video filename to request:
VideoFileName = argv[2];
//Establish a TCP connection with the server to exchange RTSP messages
//------------------
theClient.RTSPsocket = new Socket(theClient.ServerIPAddr, RTSP_server_port);
//Establish a UDP connection with the server to exchange RTCP control packets
//------------------
//Set input and output stream filters:
RTSPBufferedReader = new BufferedReader(new InputStreamReader(theClient.RTSPsocket.getInputStream()));
RTSPBufferedWriter = new BufferedWriter(new OutputStreamWriter(theClient.RTSPsocket.getOutputStream()));
//init RTSP state:
state = INIT;
}
//------------------------------------
//Handler for buttons
//------------------------------------
//Handler for Setup button
//-----------------------
class setupButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e){
System.out.println("Setup Button pressed !");
if (state == INIT) {
//Init non-blocking RTPsocket that will be used to receive data
try {
//construct a new DatagramSocket to receive RTP packets from the server, on port RTP_RCV_PORT
RTPsocket = new DatagramSocket(RTP_RCV_PORT);
//UDP socket for sending QoS RTCP packets
RTCPsocket = new DatagramSocket();
//set TimeOut value of the socket to 5msec.
RTPsocket.setSoTimeout(5);
}
catch (SocketException se)
{
System.out.println("Socket exception: "+se);
System.exit(0);
}
//init RTSP sequence number
RTSPSeqNb = 1;
//Send SETUP message to the server
sendRequest("SETUP");
//Wait for the response
if (parseServerResponse() != 200)
System.out.println("Invalid Server Response");
else
{
//change RTSP state and print new state
state = READY;
System.out.println("New RTSP state: READY");
}
}
//else if state != INIT then do nothing
}
}
//Handler for Play button
//-----------------------
class playButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.out.println("Play Button pressed!");
//Start to save the time in stats
statStartTime = System.currentTimeMillis();
if (state == READY) {
//increase RTSP sequence number
RTSPSeqNb++;
//Send PLAY message to the server
sendRequest("PLAY");
//Wait for the response
if (parseServerResponse() != 200) {
System.out.println("Invalid Server Response");
}
else {
//change RTSP state and print out new state
state = PLAYING;
System.out.println("New RTSP state: PLAYING");
//start the timer
timer.start();
rtcpSender.startSend();
}
}
//else if state != READY then do nothing
}
}
//Handler for Pause button
//-----------------------
class pauseButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e){
System.out.println("Pause Button pressed!");
if (state == PLAYING)
{
//increase RTSP sequence number
RTSPSeqNb++;
//Send PAUSE message to the server
sendRequest("PAUSE");
//Wait for the response
if (parseServerResponse() != 200)
System.out.println("Invalid Server Response");
else
{
//change RTSP state and print out new state
state = READY;
System.out.println("New RTSP state: READY");
//stop the timer
timer.stop();
rtcpSender.stopSend();
}
}
//else if state != PLAYING then do nothing
}
}
//Handler for Teardown button
//-----------------------
class tearButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e){
System.out.println("Teardown Button pressed !");
//increase RTSP sequence number
RTSPSeqNb++;
//Send TEARDOWN message to the server
sendRequest("TEARDOWN");
//Wait for the response
if (parseServerResponse() != 200)
System.out.println("Invalid Server Response");
else {
//change RTSP state and print out new state
state = INIT;
System.out.println("New RTSP state: INIT");
//stop the timer
timer.stop();
rtcpSender.stopSend();
//exit
System.exit(0);
}
}
}
// Get information about the data stream
class describeButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.out.println("Sending DESCRIBE request");
//increase RTSP sequence number
RTSPSeqNb++;
//Send DESCRIBE message to the server
sendRequest("DESCRIBE");
//Wait for the response
if (parseServerResponse() != 200) {
System.out.println("Invalid Server Response");
}
else {
System.out.println("Received response for DESCRIBE");
}
}
}
//------------------------------------
//Handler for timer
//------------------------------------
class timerListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
//Construct a DatagramPacket to receive data from the UDP socket
rcvdp = new DatagramPacket(buf, buf.length);
try {
//receive the DP from the socket, save time for stats
RTPsocket.receive(rcvdp);
double curTime = System.currentTimeMillis();
statTotalPlayTime += curTime - statStartTime;
statStartTime = curTime;
//create an RTPpacket object from the DP
RTPpacket rtp_packet = new RTPpacket(rcvdp.getData(), rcvdp.getLength());
int seqNb = rtp_packet.getsequencenumber();
//this is the highest seq num received
//print important header fields of the RTP packet received:
System.out.println("Got RTP packet with SeqNum # " + seqNb
+ " TimeStamp " + rtp_packet.gettimestamp() + " ms, of type "
+ rtp_packet.getpayloadtype());
//print header bitstream:
rtp_packet.printheader();
//get the payload bitstream from the RTPpacket object
int payload_length = rtp_packet.getpayload_length();
byte [] payload = new byte[payload_length];
rtp_packet.getpayload(payload);
//compute stats and update the label in GUI
statExpRtpNb++;
if (seqNb > statHighSeqNb) {
statHighSeqNb = seqNb;
}
if (statExpRtpNb != seqNb) {
statCumLost++;
}
statDataRate = statTotalPlayTime == 0 ? 0 : (statTotalBytes / (statTotalPlayTime / 1000.0));
statFractionLost = (float)statCumLost / statHighSeqNb;
statTotalBytes += payload_length;
updateStatsLabel();
//get an Image object from the payload bitstream
Toolkit toolkit = Toolkit.getDefaultToolkit();
fsynch.addFrame(toolkit.createImage(payload, 0, payload_length), seqNb);
//display the image as an ImageIcon object
icon = new ImageIcon(fsynch.nextFrame());
iconLabel.setIcon(icon);
}
catch (InterruptedIOException iioe) {
System.out.println("Nothing to read");
}
catch (IOException ioe) {
System.out.println("Exception caught: "+ioe);
}
}
}
//------------------------------------
// Send RTCP control packets for QoS feedback
//------------------------------------
class RtcpSender implements ActionListener {
private Timer rtcpTimer;
int interval;
// Stats variables
private int numPktsExpected; // Number of RTP packets expected since the last RTCP packet
private int numPktsLost; // Number of RTP packets lost since the last RTCP packet
private int lastHighSeqNb; // The last highest Seq number received
private int lastCumLost; // The last cumulative packets lost
private float lastFractionLost; // The last fraction lost
Random randomGenerator; // For testing only
public RtcpSender(int interval) {
this.interval = interval;
rtcpTimer = new Timer(interval, this);
rtcpTimer.setInitialDelay(0);
rtcpTimer.setCoalesce(true);
randomGenerator = new Random();
}
public void run() {
System.out.println("RtcpSender Thread Running");
}
public void actionPerformed(ActionEvent e) {
// Calculate the stats for this period
numPktsExpected = statHighSeqNb - lastHighSeqNb;
numPktsLost = statCumLost - lastCumLost;
lastFractionLost = numPktsExpected == 0 ? 0f : (float)numPktsLost / numPktsExpected;
lastHighSeqNb = statHighSeqNb;
lastCumLost = statCumLost;
//To test lost feedback on lost packets
// lastFractionLost = randomGenerator.nextInt(10)/10.0f;
RTCPpacket rtcp_packet = new RTCPpacket(lastFractionLost, statCumLost, statHighSeqNb);
int packet_length = rtcp_packet.getlength();
byte[] packet_bits = new byte[packet_length];
rtcp_packet.getpacket(packet_bits);
try {
DatagramPacket dp = new DatagramPacket(packet_bits, packet_length, ServerIPAddr, RTCP_RCV_PORT);
RTCPsocket.send(dp);
} catch (InterruptedIOException iioe) {
System.out.println("Nothing to read");
} catch (IOException ioe) {
System.out.println("Exception caught: "+ioe);
}
}
// Start sending RTCP packets
public void startSend() {
rtcpTimer.start();
}
// Stop sending RTCP packets
public void stopSend() {
rtcpTimer.stop();
}
}
//------------------------------------
//Synchronize frames
//------------------------------------
class FrameSynchronizer {
private ArrayDeque<Image> queue;
private int bufSize;
private int curSeqNb;
private Image lastImage;
public FrameSynchronizer(int bsize) {
curSeqNb = 1;
bufSize = bsize;
queue = new ArrayDeque<Image>(bufSize);
}
//synchronize frames based on their sequence number
public void addFrame(Image image, int seqNum) {
if (seqNum < curSeqNb) {
queue.add(lastImage);
}
else if (seqNum > curSeqNb) {
for (int i = curSeqNb; i < seqNum; i++) {
queue.add(lastImage);
}
queue.add(image);
}
else {
queue.add(image);
}
}
//get the next synchronized frame
public Image nextFrame() {
curSeqNb++;
lastImage = queue.peekLast();
return queue.remove();
}
}
//------------------------------------
//Parse Server Response
//------------------------------------
private int parseServerResponse() {
int reply_code = 0;
try {
//parse status line and extract the reply_code:
String StatusLine = RTSPBufferedReader.readLine();
System.out.println("RTSP Client - Received from Server:");
System.out.println(StatusLine);
StringTokenizer tokens = new StringTokenizer(StatusLine);
tokens.nextToken(); //skip over the RTSP version
reply_code = Integer.parseInt(tokens.nextToken());
//if reply code is OK get and print the 2 other lines
if (reply_code == 200) {
String SeqNumLine = RTSPBufferedReader.readLine();
System.out.println(SeqNumLine);
String SessionLine = RTSPBufferedReader.readLine();
System.out.println(SessionLine);
tokens = new StringTokenizer(SessionLine);
String temp = tokens.nextToken();
//if state == INIT gets the Session Id from the SessionLine
if (state == INIT && temp.compareTo("Session:") == 0) {
RTSPid = tokens.nextToken();
}
else if (temp.compareTo("Content-Base:") == 0) {
// Get the DESCRIBE lines
String newLine;
for (int i = 0; i < 6; i++) {
newLine = RTSPBufferedReader.readLine();
System.out.println(newLine);
}
}
}
} catch(Exception ex) {
System.out.println("Exception caught: "+ex);
System.exit(0);
}
return(reply_code);
}
private void updateStatsLabel() {
DecimalFormat formatter = new DecimalFormat("###,###.##");
statLabel1.setText("Total Bytes Received: " + statTotalBytes);
statLabel2.setText("Packet Lost Rate: " + formatter.format(statFractionLost));
statLabel3.setText("Data Rate: " + formatter.format(statDataRate) + " bytes/s");
}
//------------------------------------
//Send RTSP Request
//------------------------------------
private void sendRequest(String request_type) {
try {
//Use the RTSPBufferedWriter to write to the RTSP socket
//write the request line:
RTSPBufferedWriter.write(request_type + " " + VideoFileName + " RTSP/1.0" + CRLF);
//write the CSeq line:
RTSPBufferedWriter.write("CSeq: " + RTSPSeqNb + CRLF);
//check if request_type is equal to "SETUP" and in this case write the
//Transport: line advertising to the server the port used to receive
//the RTP packets RTP_RCV_PORT
if (request_type == "SETUP") {
RTSPBufferedWriter.write("Transport: RTP/UDP; client_port= " + RTP_RCV_PORT + CRLF);
}
else if (request_type == "DESCRIBE") {
RTSPBufferedWriter.write("Accept: application/sdp" + CRLF);
}
else {
//otherwise, write the Session line from the RTSPid field
RTSPBufferedWriter.write("Session: " + RTSPid + CRLF);
}
RTSPBufferedWriter.flush();
} catch(Exception ex) {
System.out.println("Exception caught: "+ex);
System.exit(0);
}
}
}