-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solutions_Programming_Assignment_7.html
416 lines (338 loc) · 11.7 KB
/
Solutions_Programming_Assignment_7.html
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
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD><TITLE>Solutions to the Programming Assignments|Programming Assignment 7</TITLE>
</HEAD>
<BODY vLink=#666666 link=#000066 bgColor=#ffffff leftMargin=0 topMargin=0
MARGINHEIGHT="0" MARGINWIDTH="50">
<DL>
<DD>
<FONT
face=arial size=-1><BR>
<H2>Solutions for Programming Assignment 7</H2>
<P>Below are the solutions for the RTSP/RTP lab. There are two classes:
Client, RTPpacket.
<P><STRONG>Client.java</STRONG>
<P><FONT size=3><PRE>
import java.io.*;
import java.net.*;
import java.util.*;
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("Teardown");
JPanel mainPanel = new JPanel();
JPanel buttonPanel = new JPanel();
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
//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
int RTSPid = 0; //ID of the RTSP session (given by the RTSP Server)
final static String CRLF = "\r\n";
//Video constants:
//------------------
static int MJPEG_TYPE = 26; //RTP payload type for MJPEG video
//--------------------------
//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);
setupButton.addActionListener(new setupButtonListener());
playButton.addActionListener(new playButtonListener());
pauseButton.addActionListener(new pauseButtonListener());
tearButton.addActionListener(new tearButtonListener());
//Image display label
iconLabel.setIcon(null);
//frame layout
mainPanel.setLayout(null);
mainPanel.add(iconLabel);
mainPanel.add(buttonPanel);
iconLabel.setBounds(0,0,380,280);
buttonPanel.setBounds(0,280,380,50);
f.getContentPane().add(mainPanel, BorderLayout.CENTER);
f.setSize(new Dimension(390,370));
f.setVisible(true);
//init timer
//--------------------------
timer = new Timer(20, new timerListener());
timer.setInitialDelay(0);
timer.setCoalesce(true);
//allocate enough memory for the buffer used to receive data from the server
buf = new byte[15000];
}
//------------------------------------
//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];
InetAddress 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(ServerIPAddr, RTSP_server_port);
//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);
//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
send_RTSP_request("SETUP");
//Wait for the response
if (parse_server_response() != 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 !");
if (state == READY)
{
//increase RTSP sequence number
RTSPSeqNb++;
//Send PLAY message to the server
send_RTSP_request("PLAY");
//Wait for the response
if (parse_server_response() != 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();
}
}//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
send_RTSP_request("PAUSE");
//Wait for the response
if (parse_server_response() != 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();
}
}
//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
send_RTSP_request("TEARDOWN");
//Wait for the response
if (parse_server_response() != 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();
//exit
System.exit(0);
}
}
}
//------------------------------------
//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 :
RTPsocket.receive(rcvdp);
//create an RTPpacket object from the DP
RTPpacket rtp_packet = new RTPpacket(rcvdp.getData(), rcvdp.getLength());
//print important header fields of the RTP packet received:
System.out.println("Got RTP packet with SeqNum # "+rtp_packet.getsequencenumber()+" 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);
//get an Image object from the payload bitstream
Toolkit toolkit = Toolkit.getDefaultToolkit();
Image image = toolkit.createImage(payload, 0, payload_length);
//display the image as an ImageIcon object
icon = new ImageIcon(image);
iconLabel.setIcon(icon);
}
catch (InterruptedIOException iioe){
//System.out.println("Nothing to read");
}
catch (IOException ioe) {
System.out.println("Exception caught: "+ioe);
}
}
}
//------------------------------------
//Parse Server Response
//------------------------------------
private int parse_server_response()
{
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);
//if state == INIT gets the Session Id from the SessionLine
tokens = new StringTokenizer(SessionLine);
tokens.nextToken(); //skip over the Session:
RTSPid = Integer.parseInt(tokens.nextToken());
}
}
catch(Exception ex)
{
System.out.println("Exception caught : "+ex);
System.exit(0);
}
return(reply_code);
}
//------------------------------------
//Send RTSP Request
//------------------------------------
private void send_RTSP_request(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 ((new String(request_type)).compareTo("SETUP") == 0)
RTSPBufferedWriter.write("Transport: RTP/UDP; client_port= "+RTP_RCV_PORT+CRLF);
else
RTSPBufferedWriter.write("Session: "+RTSPid+"\n");
RTSPBufferedWriter.flush();
}
catch(Exception ex)
{
System.out.println("Exception caught : "+ex);
System.exit(0);
}
}
}
</PRE></FONT>
<P><STRONG>RTPpacket.java</STRONG>
<P><A
href="lab7-RTPpacket_java.txt"
target=_blank>Click to view as text file</A> (will not display properly in
Internet Explorer)
</DD>
</DL>
</BODY></HTML>