summaryrefslogtreecommitdiff
path: root/pjsip-apps/src/swig/python/test.py
blob: 24801ed6b6cf029dcef68137b43de403e2618b2e (plain)
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
import pjsua2 as pj
import sys
import time

#
# Basic data structure test, to make sure basic struct
# and array operations work
#
def ua_data_test():
	#
	# AuthCredInfo
	#
	print "UA data types test.."
	the_realm = "pjsip.org"
	ci = pj.AuthCredInfo()
	ci.realm = the_realm
	ci.dataType = 20
	
	ci2 = ci
	assert ci.dataType == 20
	assert ci2.realm == the_realm
	
	#
	# UaConfig
	# See here how we manipulate std::vector
	#
	uc = pj.UaConfig()
	uc.maxCalls = 10
	uc.userAgent = "Python"
	uc.nameserver = pj.StringVector(["10.0.0.1", "10.0.0.2"])
	uc.nameserver.append("NS1")
	
	uc2 = uc
	assert uc2.maxCalls == 10
	assert uc2.userAgent == "Python"
	assert len(uc2.nameserver) == 3
	assert uc2.nameserver[0] == "10.0.0.1"
	assert uc2.nameserver[1] == "10.0.0.2"
	assert uc2.nameserver[2] == "NS1"

	print "  Dumping nameservers: ",
	for s in uc2.nameserver:
		print s,
	print ""

#
# Exception test
#
def ua_run_test_exception():
	print "Exception test.."
	ep = pj.Endpoint()
	ep.libCreate()
	got_exception = False
	try:
		ep.natDetectType()
	except pj.Error, e:
		got_exception = True
		print "  Got exception: status=%u, reason=%s,\n  title=%s,\n  srcFile=%s, srcLine=%d" % \
			(e.status, e.reason, e.title, e.srcFile, e.srcLine)
		assert e.status == 370050
		assert e.reason.find("PJNATH_ESTUNINSERVER") >= 0
		assert e.title == "pjsua_detect_nat_type()"
	assert got_exception

#
# Custom log writer
#
class MyLogWriter(pj.LogWriter):
	def write(self, entry):
		print "This is Python:", entry.msg
		
#
# Testing log writer callback
#
def ua_run_log_test():
	print "Logging test.."
	ep_cfg = pj.EpConfig()
	
	lw = MyLogWriter()
	ep_cfg.logConfig.writer = lw
	ep_cfg.logConfig.decor = ep_cfg.logConfig.decor & ~(pj.PJ_LOG_HAS_CR | pj.PJ_LOG_HAS_NEWLINE) 
	
	ep = pj.Endpoint()
	ep.libCreate()
	ep.libInit(ep_cfg)
	ep.libDestroy()
	
#
# Simple create, init, start, and destroy sequence
#
def ua_run_ua_test():
	print "UA test run.."
	ep_cfg = pj.EpConfig()
	
	ep = pj.Endpoint()
	ep.libCreate()
	ep.libInit(ep_cfg)
	ep.libStart()
	
	print "************* Endpoint started ok, now shutting down... *************"
	ep.libDestroy()

#
# Tone generator
#
def ua_tonegen_test():
        print "UA tonegen test.."
        ep_cfg = pj.EpConfig()

        ep = pj.Endpoint()
        ep.libCreate()
        ep.libInit(ep_cfg)
        ep.libStart()
        
        tonegen = pj.ToneGenerator()
        tonegen.createToneGenerator()

        tone = pj.ToneDesc()
        tone.freq1 = 400
        tone.freq2 = 600
        tone.on_msec = 1000
        tone.off_msec = 1000
        tones = pj.ToneDescVector()
        tones.append(tone)

        digit = pj.ToneDigit()
        digit.digit = '0'
        digit.on_msec = 1000
        digit.off_msec = 1000
        digits = pj.ToneDigitVector()
        digits.append(digit)

        adm = ep.audDevManager()
        spk = adm.getPlaybackDevMedia()

        tonegen.play(tones, True)
        tonegen.startTransmit(spk)
        time.sleep(5)

	tonegen.stop()
        tonegen.playDigits(digits, True)
        time.sleep(5)

	dm = tonegen.getDigitMap()
	print dm[0].digit
	dm[0].freq1 = 400
	dm[0].freq2 = 600
	tonegen.setDigitMap(dm)
	
	tonegen.stop()
        tonegen.playDigits(digits, True)
        time.sleep(5)
	
        tonegen = None

        ep.libDestroy()

#
# main()
#
if __name__ == "__main__":
	ua_data_test()
	ua_run_test_exception()
	ua_run_log_test()
	ua_run_ua_test()
	ua_tonegen_test()
	sys.exit(0)