-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRTSP.java
More file actions
181 lines (142 loc) · 5.76 KB
/
RTSP.java
File metadata and controls
181 lines (142 loc) · 5.76 KB
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
import java.io.*;
import java.net.*;
import java.util.*;
public class RTSP {
private String ServerHost;
private int RTP_PORT;
private int RTSP_PORT;
Socket RTSPsocket; //socket used to send/receive RTSP messages
//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
//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";
//--------------------------
//Constructor
//--------------------------
public RTSP(String ServerHost, int RTSP_PORT, int RTP_PORT, String VideoFileName) {
this.ServerHost = ServerHost;
this.RTSP_PORT = RTSP_PORT;
this.RTP_PORT = RTP_PORT;
this.VideoFileName = VideoFileName;
//first state
state = INIT;
try {
//Establish a TCP connection with the server to exchange RTSP messages
InetAddress ServerIPAddr = InetAddress.getByName(ServerHost);
RTSPsocket = new Socket(ServerIPAddr, RTSP_PORT);
//Set input and output stream filters:
RTSPBufferedReader = new BufferedReader(new InputStreamReader(RTSPsocket.getInputStream()) );
RTSPBufferedWriter = new BufferedWriter(new OutputStreamWriter(RTSPsocket.getOutputStream()) );
} catch (Exception e) {
System.out.println(e + " " + ServerHost + RTSP_PORT);
}
}
//------------------------------------
//Parse Server Response
//------------------------------------
private int parse_response() {
int reply_code = 0;
try {
//parse status line and extract the reply_code:
String StatusLine = RTSPBufferedReader.readLine();
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();
String SessionLine = RTSPBufferedReader.readLine();
//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 RTSP: " + ex);
System.exit(0);
}
return(reply_code);
}
//------------------------------------
//Send RTSP Request
//------------------------------------
//TO COMPLETE
//.............
public void send_request(String request_type) {
try {
//Check request_type and state variables to see if the RTSP message can be sent
if(((request_type).compareTo("SETUP") == 0) && (state == INIT)){
RTSPBufferedWriter.write("SETUP " + this.VideoFileName + " RTSP/1.0" + CRLF);
RTSPBufferedWriter.write("Cseq: " + this.RTSPSeqNb + CRLF);
RTSPBufferedWriter.write("Transport: RTP/UDP; client_port= " + this.RTP_PORT + CRLF);
RTSPBufferedWriter.flush();
this.RTSPSeqNb +=1;
if(parse_response() == 200){
state = READY;
}
}else if(((request_type).compareTo("TEARDOWN") == 0) && (state == READY)){
RTSPBufferedWriter.write("TEARDOWN " + this.VideoFileName + " RTSP/1.0"+ CRLF);
RTSPBufferedWriter.write("Cseq: " + this.RTSPSeqNb + CRLF);
RTSPBufferedWriter.write("Session: " + this.RTSPid + CRLF);
RTSPBufferedWriter.flush();
this.RTSPSeqNb +=1;
if(parse_response() == 200){
state = INIT;
}
}else if(((request_type).compareTo("PLAY") == 0) && (state == READY)){
RTSPBufferedWriter.write("PLAY " + this.VideoFileName + " RTSP/1.0"+ CRLF);
// RTSPBufferedWriter.write("Range: npt=0- " + CRLF);
RTSPBufferedWriter.write("Cseq: " + this.RTSPSeqNb + CRLF);
RTSPBufferedWriter.write("Session: " + this.RTSPid + CRLF);
RTSPBufferedWriter.flush();
this.RTSPSeqNb +=1;
if(parse_response() == 200){
state = PLAYING;
}
}else if(((request_type).compareTo("PAUSE") == 0) && (state == PLAYING)){
RTSPBufferedWriter.write("PAUSE " + this.VideoFileName + " RTSP/1.0 "+ CRLF);
RTSPBufferedWriter.write("Cseq: " + this.RTSPSeqNb + CRLF);
RTSPBufferedWriter.write("Session: " + this.RTSPid + CRLF);
RTSPBufferedWriter.flush();
this.RTSPSeqNb +=1;
if(parse_response() == 200){
state = READY;
}
}else if(((request_type).compareTo("TEARDOWN") == 0) && (state == PLAYING)){
RTSPBufferedWriter.write("TEARDOWN " + this.VideoFileName + " RTSP/1.0" + CRLF);
RTSPBufferedWriter.write("Cseq: " + this.RTSPSeqNb + CRLF);
RTSPBufferedWriter.write("Session: " + this.RTSPid + CRLF);
RTSPBufferedWriter.flush();
this.RTSPSeqNb +=1;
if(parse_response() == 200){
state = INIT;
}
}else{
}
//...
//Use the RTSPBufferedWriter to write to the RTSP socket
//write the request line:
//RTSPBufferedWriter.write(...);
//write the CSeq line:
//......
//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_PORT
//if ....
//otherwise, write the Session line from the RTSPid field
//else ....
//Wait for the response and, in case of success, update the state variable
//...
} catch (Exception ex) {
System.out.println("Exception caught: " + ex);
System.exit(0);
}
}
}//end of Class RTSP