-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSIPclient.java
More file actions
221 lines (183 loc) · 8.62 KB
/
SIPclient.java
File metadata and controls
221 lines (183 loc) · 8.62 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
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
import java.io.*;
import java.util.*;
import javax.sdp.*;
import javax.sip.*;
import javax.sip.address.*;
import javax.sip.header.*;
import javax.sip.message.*;
import java.util.concurrent.CountDownLatch;
public class SIPclient extends SIPua {
private CountDownLatch latch;
/****************************************************************************/
/* Constructor method */
/****************************************************************************/
public SIPclient (String localIP, int sipPort) throws Exception {
super("client", localIP, sipPort);
latch = new CountDownLatch(1);
}
public String getRtspURI() { return RTSP_URI; }
public int getRtspPort() { return RTSP_PORT; }
/****************************************************************************/
/* Generates SDP offer */
/****************************************************************************/
private Object createSDPOffer() throws Exception {
SessionDescription sessionDescription = sdpFactory.createSessionDescription();
// Sets formats for "m" line
int[] formats = new int[1];
formats[0] = 32;
// Creates an "m" line for RTP media
MediaDescription mediaDescription = sdpFactory.createMediaDescription("video", 0, 1, "RTSP", formats);
// Sets format attributes
Vector <Attribute> attributes = new Vector <Attribute> ();
Attribute a = sdpFactory.createAttribute("rtpmap", "32 MPV");
attributes.add(a);
// Sets directivity
a = sdpFactory.createAttribute("recvonly",null);
attributes.add(a);
// Ends with media description
mediaDescription.setAttributes(attributes);
// Sets a "c" line
Connection connection = sdpFactory.createConnection(LOCAL_IP);
mediaDescription.setConnection(connection);
// Sets the b parameter as 1Mbps
BandWidth bandwidth = sdpFactory.createBandwidth(BandWidth.AS, 1000);
Vector <BandWidth> bandwidths = new Vector <BandWidth> ();
bandwidths.add(bandwidth);
mediaDescription.setBandwidths(bandwidths);
// Creates an "m" line for RTSP
String[] formats2 = {"iptv_rtsp"};
MediaDescription mediaDescription2 = sdpFactory.createMediaDescription("application", 9, 1, "TCP", formats2);
// Sets TCP attributes
Vector <Attribute> attributes2 = new Vector <Attribute> ();
a = sdpFactory.createAttribute("setup", "active");
attributes2.add(a);
a = sdpFactory.createAttribute("connection", "new");
attributes2.add(a);
// Ends with media description
mediaDescription2.setAttributes(attributes2);
// Sets a "c" line
mediaDescription2.setConnection(connection);
Vector <MediaDescription> mediaDescriptions = new Vector <MediaDescription> ();
mediaDescriptions.add(mediaDescription);
mediaDescriptions.add(mediaDescription2);
sessionDescription.setMediaDescriptions(mediaDescriptions);
return sessionDescription;
}
/****************************************************************************/
/* Utility to get an unique tag */
/****************************************************************************/
private String getTag() throws Exception {
long number = generator.nextLong();
return Long.toHexString(number);
}
/****************************************************************************/
/* Processes a SIP response */
/****************************************************************************/
public void processResponse(ResponseEvent evt) {
try {
Response response = evt.getResponse();
dialog = evt.getDialog();
System.out.println("Received response:" + "\n" + response.toString());
// Processes a 200 OK response
if (response.getStatusCode() == Response.OK) {
CSeqHeader cseqHeader = (CSeqHeader) response.getHeader(CSeqHeader.NAME);
// Processes an OK response to an INVITE request
if (cseqHeader.getMethod().equals(Request.INVITE)) {
System.out.println(" OK response received for INVITE request. Sending ACK request");
//TO-DO create and send an ACK, using the object "dialog"
// ...
long cseq = dialog.getLocalSeqNumber();
Request ack = dialog.createAck(cseq);
dialog.sendAck(ack);
System.out.println("Session established");
processSDP(response.getRawContent());
}
latch.countDown();
}
} catch (Exception e) {
e.printStackTrace();
}
}
/****************************************************************************/
/* Processes the SDP answer to obtain the RTSP URI of the requested video */
/****************************************************************************/
private void processSDP(byte[] sdpAnswer) throws Exception{
SessionDescription sdpPayload = sdpFactory.createSessionDescription( new String(sdpAnswer) );
Vector <MediaDescription> mediaDescriptions = sdpPayload.getMediaDescriptions(true);
MediaDescription mediaDescription = mediaDescriptions.elementAt(1);
String aux = mediaDescription.getAttribute("fmtp");
RTSP_URI = aux.substring(aux.indexOf("=")+1);
RTSP_PORT = mediaDescription.getMedia().getMediaPort();
}
/****************************************************************************/
/* Creates an initial INVITE request */
/****************************************************************************/
private Request createINVITE(String to) throws Exception {
// Creates the From header
SipURI from = addressFactory.createSipURI(USER, SIP_DOMAIN);
Address fromNameAddress = addressFactory.createAddress(from);
fromNameAddress.setDisplayName(USER);
FromHeader fromHeader = headerFactory.createFromHeader(fromNameAddress, getTag());
// Creates the To header
String username = to.substring(0, to.indexOf("@"));
String address = to.substring(to.indexOf("@")+1);
SipURI toAddress = addressFactory.createSipURI(username, address);
Address toNameAddress = addressFactory.createAddress(toAddress);
toNameAddress.setDisplayName(username);
ToHeader toHeader = headerFactory.createToHeader(toNameAddress, null);
// TO-DO: create the Request-URI using the object "addressFactory"
// SipURI requestURI = ...
SipURI requestURI = toAddress;
// TO-DO: create the Via Header using the method "headerFactory.createViaHeader"
//ViaHeader viaHeader = ...
ArrayList viaHeaders = new ArrayList();
ViaHeader viaHeader = headerFactory.createViaHeader(LOCAL_IP, SIP_PORT,"udp","z9hG4bKnashds8");
viaHeaders.add(viaHeader);
// TO-DO: create the Call-ID header using the object "sipProvider"
// CallIdHeader callIdHeader = ...
CallIdHeader callIdHeader = sipProvider.getNewCallId();
// TO-DO: create CSeq header using the method "headerFactory.createCSeqHeader"
// CSeqHeader cSeqHeader = ...
CSeqHeader cSeqHeader = headerFactory.createCSeqHeader(123456789, Request.INVITE);
// TO-DO: create Max-Forwards header using the object "headerFactory"
// MaxForwardsHeader maxForwards = ...
MaxForwardsHeader maxForwards = headerFactory.createMaxForwardsHeader(70);
// Creates the request
Request request = messageFactory.createRequest(requestURI, Request.INVITE, callIdHeader, cSeqHeader, fromHeader, toHeader, viaHeaders, maxForwards);
// TO-DO: create the Contact header, similarly to the To header, using the object "headerFactory"
// ...
// ContactHeader contactHeader = ...fromNameAddress
ContactHeader contactHeader = headerFactory.createContactHeader(this.localAddress);
request.addHeader(contactHeader);
// Creates Content-type header
ContentTypeHeader contentTypeHeader = headerFactory.createContentTypeHeader("application", "sdp");
request.addHeader(contentTypeHeader);
request.setContent(createSDPOffer(), contentTypeHeader);
return request;
}
/****************************************************************************/
/* Initiates the session, by generating and sending an initial INVITE request/
/****************************************************************************/
public void initiateSession(String to) throws Exception {
System.out.print("Creating INVITE request... ");
Request invite = createINVITE(to);
System.out.println("Sending the INVITE request... \n" + invite.toString());
ClientTransaction clientTransaction = sipProvider.getNewClientTransaction(invite);
clientTransaction.sendRequest();
latch.await();
latch = new CountDownLatch(1);
}
/****************************************************************************/
/* Terminates the session */
/****************************************************************************/
public void terminateSession() throws Exception {
// Creates BYE request
Request bye = dialog.createRequest(Request.BYE);
System.out.print("Sending BYE request...");
ClientTransaction clientTransaction = sipProvider.getNewClientTransaction(bye);
dialog.sendRequest(clientTransaction);
System.out.println("done");
latch.await();
sipStack.deleteListeningPoint(udp);
}
}