mod_call.py 9.6 KB

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