Debian Codegraph @

Xpjproject/tests/pjsua/mod_call.py

raw symbols SCIP
1 import time
2 import sys
3 import inc_const as const
4 import inc_util as util
5 from inc_cfg import *
6
7 # Load configuration
8 cfg_file = util.load_module_from_file("cfg_file", ARGS[1])
9
10 # Trigger address switch for media flow between ua1 and ua2.
11 # When the receiver uses STUN while both sides are actually in the same
12 # private network, initial media packets may be sent to public IP address
13 # as specified in the receiver SDP and those packets may not be delivered
14 # if the NAT does not support hairpinning. This function will make both
15 # sides to send some initial packets to trigger destination address switch
16 # in media transport, so future packets will be delivered to the correct
17 # address (private IP address).
18 def hole_punch(ua1, ua2):
19 if ua1.use_telnet:
20 ua1.send("# 987")
21 else:
22 ua1.send("#")
23 ua1.expect("#")
24 ua1.send("987")
25
26 if ua2.use_telnet:
27 ua2.send("# 789")
28 else:
29 ua2.send("#")
30 ua2.expect("#")
31 ua2.send("789")
32
33 time.sleep(0.1)
34
35
36 # Check media flow between ua1 and ua2
37 def check_media(ua1, ua2):
38 if ua1.use_telnet:
39 ua1.send("# 1122")
40 else:
41 ua1.send("#")
42 ua1.expect("#")
43 ua1.send("1122")
44 ua2.expect(const.RX_DTMF + "1")
45 ua2.expect(const.RX_DTMF + "1")
46 ua2.expect(const.RX_DTMF + "2")
47 ua2.expect(const.RX_DTMF + "2")
48
49
50 # Test body function
51 def test_func(t):
52 callee = t.process[0]
53 caller = t.process[1]
54
55 # if have_reg then wait for couple of seconds for PUBLISH
56 # to complete (just in case pUBLISH is used)
57 if callee.inst_param.have_reg:
58 time.sleep(1)
59 if caller.inst_param.have_reg:
60 time.sleep(1)
61
62 # Check if ICE is used
63 use_ice = ("--use-ice" in caller.inst_param.arg) and ("--use-ice" in callee.inst_param.arg)
64
65 # Check if STUN is used (by either side)
66 use_stun = ("--stun-srv" in caller.inst_param.arg) or ("--stun-srv" in callee.inst_param.arg)
67
68 # Check if DTLS-SRTP is used
69 use_dtls_srtp = "--srtp-keying=1" in caller.inst_param.arg
70
71 # Caller making call
72 if caller.use_telnet:
73 caller.send("call new " + t.inst_params[0].uri)
74 else:
75 caller.send("m")
76 caller.send(t.inst_params[0].uri)
77 caller.expect(const.STATE_CALLING)
78
79 # Callee waits for call and answers with 180/Ringing
80 time.sleep(0.2)
81 callee.expect(const.EVENT_INCOMING_CALL)
82 if callee.use_telnet:
83 callee.send("call answer 180")
84 else:
85 callee.send("a")
86 callee.send("180")
87 callee.expect("SIP/2.0 180")
88 caller.expect("SIP/2.0 180")
89
90 # Synchronize stdout
91 caller.sync_stdout()
92 callee.sync_stdout()
93
94 # Callee answers with 200/OK
95 if callee.use_telnet:
96 callee.send("call answer 200")
97 else:
98 callee.send("a")
99 callee.send("200")
100
101 # Wait until call is connected in both endpoints
102 ##time.sleep(0.2)
103 caller.expect(const.STATE_CONFIRMED)
104 callee.expect(const.STATE_CONFIRMED)
105
106 # Synchronize stdout
107 caller.sync_stdout()
108 callee.sync_stdout()
109 ##time.sleep(0.1)
110 caller.sync_stdout()
111 callee.sync_stdout()
112
113 # Wait ICE nego before checking media
114 if use_ice:
115 # Unfortunately ICE nego may race with STATE_CONFIRMED (esp. on callee side), so let's just sleep
116 #caller.expect("ICE negotiation success")
117 #callee.expect("ICE negotiation success")
118 # Additional wait for ICE updating address (via UPDATE/re-INVITE)
119 time.sleep(0.5)
120
121 # Wait DTLS-SRTP nego before checking media
122 if use_dtls_srtp:
123 # Unfortunately DTLS-SRTP nego may race with STATE_CONFIRMED, so let's just sleep
124 #caller.expect("SRTP started, keying=DTLS-SRTP")
125 #callee.expect("SRTP started, keying=DTLS-SRTP")
126 time.sleep(0.5)
127
128 # Trigger address switch before checking media
129 if use_stun and not use_ice:
130 hole_punch(caller, callee)
131
132 # Test that media is okay
133 check_media(caller, callee)
134 check_media(callee, caller)
135
136 # Hold call by caller
137 if caller.use_telnet:
138 caller.send("call hold")
139 else:
140 caller.send("H")
141 caller.expect("INVITE sip:")
142 callee.expect("INVITE sip:")
143 callee.expect(const.MEDIA_HOLD)
144 caller.expect(const.MEDIA_HOLD)
145
146 # Synchronize stdout
147 caller.sync_stdout()
148 callee.sync_stdout()
149
150 # Release hold
151 ##time.sleep(0.5)
152 if caller.use_telnet:
153 caller.send("call reinvite")
154 else:
155 caller.send("v")
156 caller.expect("INVITE sip:")
157 callee.expect("INVITE sip:")
158 callee.expect(const.MEDIA_ACTIVE, title="waiting for media active after call hold")
159 caller.expect(const.MEDIA_ACTIVE, title="waiting for media active after call hold")
160
161 # Synchronize stdout
162 caller.sync_stdout()
163 callee.sync_stdout()
164
165 # Trigger address switch before checking media
166 if use_stun and not use_ice:
167 hole_punch(caller, callee)
168
169 # Test that media is okay
170 check_media(caller, callee)
171 check_media(callee, caller)
172
173 # Synchronize stdout
174 caller.sync_stdout()
175 callee.sync_stdout()
176
177 # Hold call by callee
178 if callee.use_telnet:
179 callee.send("call hold")
180 else:
181 callee.send("H")
182 callee.expect("INVITE sip:")
183 caller.expect("INVITE sip:")
184 caller.expect(const.MEDIA_HOLD)
185 callee.expect(const.MEDIA_HOLD)
186
187 # Synchronize stdout
188 caller.sync_stdout()
189 callee.sync_stdout()
190
191 # Release hold
192 ##time.sleep(0.1)
193 if callee.use_telnet:
194 callee.send("call reinvite")
195 else:
196 callee.send("v")
197 callee.expect("INVITE sip:")
198 caller.expect("INVITE sip:")
199 caller.expect(const.MEDIA_ACTIVE, title="waiting for media active after call hold")
200 callee.expect(const.MEDIA_ACTIVE, title="waiting for media active after call hold")
201
202 # Synchronize stdout
203 caller.sync_stdout()
204 callee.sync_stdout()
205
206 # Trigger address switch before checking media
207 if use_stun and not use_ice:
208 hole_punch(caller, callee)
209
210 # Test that media is okay
211 # Wait for some time for ICE negotiation
212 ##time.sleep(0.6)
213 check_media(caller, callee)
214 check_media(callee, caller)
215
216 # Synchronize stdout
217 caller.sync_stdout()
218 callee.sync_stdout()
219
220 # UPDATE (by caller)
221 if caller.use_telnet:
222 caller.send("call update")
223 else:
224 caller.send("U")
225 #caller.sync_stdout()
226 callee.expect(const.MEDIA_ACTIVE, title="waiting for media active with UPDATE")
227 caller.expect(const.MEDIA_ACTIVE, title="waiting for media active with UPDATE")
228
229 # Synchronize stdout
230 caller.sync_stdout()
231 callee.sync_stdout()
232
233 # Trigger address switch before checking media
234 if use_stun and not use_ice:
235 hole_punch(caller, callee)
236
237 # Test that media is okay
238 ##time.sleep(0.1)
239 check_media(caller, callee)
240 check_media(callee, caller)
241
242 # UPDATE (by callee)
243 if callee.use_telnet:
244 callee.send("call update")
245 else:
246 callee.send("U")
247 callee.expect("UPDATE sip:")
248 caller.expect("UPDATE sip:")
249 caller.expect(const.MEDIA_ACTIVE, title="waiting for media active with UPDATE")
250 callee.expect(const.MEDIA_ACTIVE, title="waiting for media active with UPDATE")
251
252 # Synchronize stdout
253 caller.sync_stdout()
254 callee.sync_stdout()
255
256 # Trigger address switch before checking media
257 if use_stun and not use_ice:
258 hole_punch(caller, callee)
259
260 # Test that media is okay
261 ##time.sleep(0.1)
262 check_media(caller, callee)
263 check_media(callee, caller)
264
265 # Synchronize stdout
266 caller.sync_stdout()
267 callee.sync_stdout()
268
269 # Set codecs in both caller and callee so that there is
270 # no common codec between them.
271 # In caller we only enable PCMU, in callee we only enable PCMA
272 if caller.use_telnet:
273 caller.send("Cp * 0")
274 caller.send("Cp")
275 caller.expect("PCMU/8000.* prio: 0")
276 caller.send("Cp PCMU 120")
277 caller.send("Cp")
278 caller.expect("PCMU/8000.* prio: 120")
279 else:
280 caller.send("Cp")
281 caller.expect("Enter codec")
282 caller.send("* 0")
283 caller.send("Cp")
284 caller.expect("Enter codec")
285 caller.send("pcmu 120")
286
287 if callee.use_telnet:
288 callee.send("Cp * 0")
289 callee.send("Cp")
290 callee.expect("PCMA/8000.* prio: 0")
291 callee.send("Cp PCMA 120")
292 callee.send("Cp")
293 callee.expect("PCMA/8000.* prio: 120")
294 else:
295 callee.send("Cp")
296 callee.expect("Enter codec")
297 callee.send("* 0")
298 callee.send("Cp")
299 callee.expect("Enter codec")
300 callee.send("pcma 120")
301
302 # Test when UPDATE fails (by callee)
303 if callee.use_telnet:
304 callee.send("call update")
305 else:
306 callee.send("U")
307 caller.expect("SIP/2.0 488")
308 callee.expect("SIP/2.0 488")
309 callee.sync_stdout()
310 caller.sync_stdout()
311
312 # Test that media is still okay
313 ##time.sleep(0.1)
314 check_media(caller, callee)
315 check_media(callee, caller)
316
317 # Test when UPDATE fails (by caller)
318 if caller.use_telnet:
319 caller.send("call update")
320 else:
321 caller.send("U")
322 caller.expect("UPDATE sip:")
323 callee.expect("UPDATE sip:")
324 callee.expect("SIP/2.0 488")
325 caller.expect("SIP/2.0 488")
326 caller.sync_stdout()
327 callee.sync_stdout()
328
329 # Test that media is still okay
330 ##time.sleep(0.1)
331 check_media(callee, caller)
332 check_media(caller, callee)
333
334 # Hangup call
335 ##time.sleep(0.1)
336 if caller.use_telnet:
337 caller.send("call hangup")
338 else:
339 caller.send("h")
340
341 # Wait until calls are cleared in both endpoints
342 caller.expect(const.STATE_DISCONNECTED)
343 callee.expect(const.STATE_DISCONNECTED)
344
345
346 # Here where it all comes together
347 test = cfg_file.test_param
348 test.test_func = test_func
349