Debian Codegraph @

Xpjproject/pjsip-apps/src/pygui/settings.py

raw symbols SCIP
1 #
2 # pjsua Python GUI Demo
3 #
4 # Copyright (C)2013 Teluu Inc. (http://www.teluu.com)
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #
20 import sys
21 if sys.version_info[0] >= 3: # Python 3
22 import tkinter as tk
23 from tkinter import ttk
24 from tkinter import messagebox as msgbox
25 else:
26 import Tkinter as tk
27 import tkMessageBox as msgbox
28 import ttk
29
30 import pjsua2 as pj
31 #import application
32
33 # Transport setting
34 class SipTransportConfig:
35 def __init__(self, type, enabled):
36 #pj.PersistentObject.__init__(self)
37 self.type = type
38 self.enabled = enabled
39 self.config = pj.TransportConfig()
40 def readObject(self, node):
41 child_node = node.readContainer("SipTransport")
42 self.type = child_node.readInt("type")
43 self.enabled = child_node.readBool("enabled")
44 self.config.readObject(child_node)
45 def writeObject(self, node):
46 child_node = node.writeNewContainer("SipTransport")
47 child_node.writeInt("type", self.type)
48 child_node.writeBool("enabled", self.enabled)
49 self.config.writeObject(child_node)
50
51 # Account setting with buddy list
52 class AccConfig:
53 def __init__(self):
54 self.enabled = True
55 self.config = pj.AccountConfig()
56 self.buddyConfigs = []
57 def readObject(self, node):
58 acc_node = node.readContainer("Account")
59 self.enabled = acc_node.readBool("enabled")
60 self.config.readObject(acc_node)
61 buddy_node = acc_node.readArray("buddies")
62 while buddy_node.hasUnread():
63 buddy_cfg = pj.BuddyConfig()
64 buddy_cfg.readObject(buddy_node)
65 self.buddyConfigs.append(buddy_cfg)
66 def writeObject(self, node):
67 acc_node = node.writeNewContainer("Account")
68 acc_node.writeBool("enabled", self.enabled)
69 self.config.writeObject(acc_node)
70 buddy_node = acc_node.writeNewArray("buddies")
71 for buddy in self.buddyConfigs:
72 buddy_node.writeObject(buddy)
73
74
75 # Master settings
76 class AppConfig:
77 def __init__(self):
78 self.epConfig = pj.EpConfig() # pj.EpConfig()
79 self.udp = SipTransportConfig(pj.PJSIP_TRANSPORT_UDP, True)
80 self.tcp = SipTransportConfig(pj.PJSIP_TRANSPORT_TCP, True)
81 self.tls = SipTransportConfig(pj.PJSIP_TRANSPORT_TLS, False)
82 self.accounts = [] # Array of AccConfig
83
84 def loadFile(self, file):
85 json = pj.JsonDocument()
86 json.loadFile(file)
87 root = json.getRootContainer()
88 self.epConfig = pj.EpConfig()
89 self.epConfig.readObject(root)
90
91 tp_node = root.readArray("transports")
92 self.udp.readObject(tp_node)
93 self.tcp.readObject(tp_node)
94 if tp_node.hasUnread():
95 self.tls.readObject(tp_node)
96
97 acc_node = root.readArray("accounts")
98 while acc_node.hasUnread():
99 acfg = AccConfig()
100 acfg.readObject(acc_node)
101 self.accounts.append(acfg)
102
103 def saveFile(self,file):
104 json = pj.JsonDocument()
105
106 # Write endpoint config
107 json.writeObject(self.epConfig)
108
109 # Write transport config
110 tp_node = json.writeNewArray("transports")
111 self.udp.writeObject(tp_node)
112 self.tcp.writeObject(tp_node)
113 self.tls.writeObject(tp_node)
114
115 # Write account configs
116 node = json.writeNewArray("accounts")
117 for acc in self.accounts:
118 acc.writeObject(node)
119
120 json.saveFile(file)
121
122
123 # Settings dialog
124 class Dialog(tk.Toplevel):
125 """
126 This implements account settings dialog to manipulate account settings.
127 """
128 def __init__(self, parent, cfg):
129 tk.Toplevel.__init__(self, parent)
130 self.transient(parent)
131 self.parent = parent
132 self.title('Settings')
133
134 self.frm = ttk.Frame(self)
135 self.frm.pack(expand='yes', fill='both')
136
137 self.isOk = False
138 self.cfg = cfg
139
140 self.createWidgets()
141
142 def doModal(self):
143 if self.parent:
144 self.parent.wait_window(self)
145 else:
146 self.wait_window(self)
147 return self.isOk
148
149 def createWidgets(self):
150 # The notebook
151 self.frm.rowconfigure(0, weight=1)
152 self.frm.rowconfigure(1, weight=0)
153 self.frm.columnconfigure(0, weight=1)
154 self.frm.columnconfigure(1, weight=1)
155 self.wTab = ttk.Notebook(self.frm)
156 self.wTab.grid(column=0, row=0, columnspan=2, padx=10, pady=10, ipadx=20, ipady=20, sticky=tk.N+tk.S+tk.W+tk.E)
157
158 # Main buttons
159 btnOk = ttk.Button(self.frm, text='Ok', command=self.onOk)
160 btnOk.grid(column=0, row=1, sticky=tk.E, padx=20, pady=10)
161 btnCancel = ttk.Button(self.frm, text='Cancel', command=self.onCancel)
162 btnCancel.grid(column=1, row=1, sticky=tk.W, padx=20, pady=10)
163
164 # Tabs
165 self.createBasicTab()
166 self.createNetworkTab()
167 self.createMediaTab()
168
169 def createBasicTab(self):
170 # Prepare the variables to set/receive values from GUI
171 self.cfgLogFile = tk.StringVar(value=self.cfg.epConfig.logConfig.filename)
172 self.cfgLogAppend = tk.BooleanVar(value=True if (self.cfg.epConfig.logConfig.fileFlags & pj.PJ_O_APPEND) else False)
173
174 # Build the tab page
175 frm = ttk.Frame(self.frm)
176 frm.columnconfigure(0, weight=1)
177 frm.columnconfigure(1, weight=2)
178 row = 0
179 ttk.Label(frm, text='User Agent:').grid(row=row, column=0, sticky=tk.E, pady=2, padx=8)
180 ttk.Label(frm, text=self.cfg.epConfig.uaConfig.userAgent).grid(row=row, column=1, sticky=tk.W, pady=2, padx=6)
181 row += 1
182 ttk.Label(frm, text='Max calls:').grid(row=row, column=0, sticky=tk.E, pady=2, padx=8)
183 ttk.Label(frm, text=str(self.cfg.epConfig.uaConfig.maxCalls)).grid(row=row, column=1, sticky=tk.W, pady=2, padx=6)
184 row += 1
185 ttk.Label(frm, text='Log file:').grid(row=row, column=0, sticky=tk.E, pady=2, padx=8)
186 ttk.Entry(frm, textvariable=self.cfgLogFile, width=32).grid(row=row, column=1, sticky=tk.W, padx=6)
187 row += 1
188 ttk.Checkbutton(frm, text='Append log file', variable=self.cfgLogAppend).grid(row=row, column=1, sticky=tk.W, padx=6, pady=2)
189
190 self.wTab.add(frm, text='Basic')
191
192 def createNetworkTab(self):
193 self.cfgNameserver = tk.StringVar()
194 if len(self.cfg.epConfig.uaConfig.nameserver):
195 self.cfgNameserver.set(self.cfg.epConfig.uaConfig.nameserver[0])
196 self.cfgStunServer = tk.StringVar()
197 if len(self.cfg.epConfig.uaConfig.stunServer):
198 self.cfgStunServer.set(self.cfg.epConfig.uaConfig.stunServer[0])
199 self.cfgStunIgnoreError = tk.BooleanVar(value=self.cfg.epConfig.uaConfig.stunIgnoreFailure)
200
201 self.cfgUdpEnabled = tk.BooleanVar(value=self.cfg.udp.enabled)
202 self.cfgUdpPort = tk.IntVar(value=self.cfg.udp.config.port)
203 self.cfgTcpEnabled = tk.BooleanVar(value=self.cfg.tcp.enabled)
204 self.cfgTcpPort = tk.IntVar(value=self.cfg.tcp.config.port)
205 self.cfgTlsEnabled = tk.BooleanVar(value=self.cfg.tls.enabled)
206 self.cfgTlsPort = tk.IntVar(value=self.cfg.tls.config.port)
207
208 self.cfgTlsCaFile = tk.StringVar(value=self.cfg.tls.config.tlsConfig.CaListFile)
209 self.cfgTlsCertFile = tk.StringVar(value=self.cfg.tls.config.tlsConfig.certFile)
210 self.cfgTlsVerifyClient = tk.BooleanVar(value=self.cfg.tls.config.tlsConfig.verifyClient)
211 self.cfgTlsVerifyServer = tk.BooleanVar(value=self.cfg.tls.config.tlsConfig.verifyServer)
212
213 # Build the tab page
214 frm = ttk.Frame(self.frm)
215 frm.columnconfigure(0, weight=1)
216 frm.columnconfigure(1, weight=2)
217 row = 0
218 #ttk.Label(frm, text='UDP transport:').grid(row=row, column=0, sticky=tk.E, pady=2, padx=8)
219 ttk.Checkbutton(frm, text='Enable UDP transport', variable=self.cfgUdpEnabled).grid(row=row, column=0, sticky=tk.W, padx=6, pady=2)
220 row += 1
221 ttk.Label(frm, text='UDP port:').grid(row=row, column=0, sticky=tk.E, pady=2, padx=8)
222 tk.Spinbox(frm, from_=0, to=65535, textvariable=self.cfgUdpPort, width=5).grid(row=row, column=1, sticky=tk.W, padx=6)
223 ttk.Label(frm, text='(0 for any)').grid(row=row, column=1, sticky=tk.E, pady=6, padx=6)
224 row += 1
225 #ttk.Label(frm, text='TCP transport:').grid(row=row, column=0, sticky=tk.E, pady=2, padx=8)
226 ttk.Checkbutton(frm, text='Enable TCP transport', variable=self.cfgTcpEnabled).grid(row=row, column=0, sticky=tk.W, padx=6, pady=2)
227 row += 1
228 ttk.Label(frm, text='TCP port:').grid(row=row, column=0, sticky=tk.E, pady=2, padx=8)
229 tk.Spinbox(frm, from_=0, to=65535, textvariable=self.cfgTcpPort, width=5).grid(row=row, column=1, sticky=tk.W, padx=6)
230 ttk.Label(frm, text='(0 for any)').grid(row=row, column=1, sticky=tk.E, pady=6, padx=6)
231 row += 1
232 #ttk.Label(frm, text='TLS transport:').grid(row=row, column=0, sticky=tk.E, pady=2, padx=8)
233 ttk.Checkbutton(frm, text='Enable TLS transport', variable=self.cfgTlsEnabled).grid(row=row, column=0, sticky=tk.W, padx=6, pady=2)
234 row += 1
235 ttk.Label(frm, text='TLS port:').grid(row=row, column=0, sticky=tk.E, pady=2, padx=8)
236 tk.Spinbox(frm, from_=0, to=65535, textvariable=self.cfgTlsPort, width=5).grid(row=row, column=1, sticky=tk.W, padx=6)
237 ttk.Label(frm, text='(0 for any)').grid(row=row, column=1, sticky=tk.E, pady=6, padx=6)
238 row += 1
239 ttk.Label(frm, text='TLS CA file:').grid(row=row, column=0, sticky=tk.E, pady=2, padx=8)
240 ttk.Entry(frm, textvariable=self.cfgTlsCaFile, width=32).grid(row=row, column=1, sticky=tk.W, padx=6)
241 row += 1
242 ttk.Label(frm, text='TLS cert file:').grid(row=row, column=0, sticky=tk.E, pady=2, padx=8)
243 ttk.Entry(frm, textvariable=self.cfgTlsCertFile, width=32).grid(row=row, column=1, sticky=tk.W, padx=6)
244 row += 1
245 ttk.Checkbutton(frm, text='TLS verify server', variable=self.cfgTlsVerifyServer).grid(row=row, column=1, sticky=tk.W, padx=6, pady=2)
246 row += 1
247 ttk.Checkbutton(frm, text='TLS verify client', variable=self.cfgTlsVerifyClient).grid(row=row, column=1, sticky=tk.W, padx=6, pady=2)
248 row += 1
249 ttk.Label(frm, text='DNS and STUN:').grid(row=row, column=0, sticky=tk.W, pady=2, padx=8)
250 row += 1
251 ttk.Label(frm, text='Nameserver:').grid(row=row, column=0, sticky=tk.E, pady=2, padx=8)
252 ttk.Entry(frm, textvariable=self.cfgNameserver, width=32).grid(row=row, column=1, sticky=tk.W, padx=6)
253 row += 1
254 ttk.Label(frm, text='STUN Server:').grid(row=row, column=0, sticky=tk.E, pady=2, padx=8)
255 ttk.Entry(frm, textvariable=self.cfgStunServer, width=32).grid(row=row, column=1, sticky=tk.W, padx=6)
256 row += 1
257 ttk.Checkbutton(frm, text='Ignore STUN failure at startup', variable=self.cfgStunIgnoreError).grid(row=row, column=1, sticky=tk.W, padx=6, pady=2)
258
259 self.wTab.add(frm, text='Network')
260
261 def createMediaTab(self):
262 self.cfgClockrate = tk.IntVar(value=self.cfg.epConfig.medConfig.clockRate)
263 self.cfgSndClockrate = tk.IntVar(value=self.cfg.epConfig.medConfig.sndClockRate)
264 self.cfgAudioPtime = tk.IntVar(value=self.cfg.epConfig.medConfig.audioFramePtime)
265 self.cfgMediaQuality = tk.IntVar(value=self.cfg.epConfig.medConfig.quality)
266 self.cfgCodecPtime = tk.IntVar(value=self.cfg.epConfig.medConfig.ptime)
267 self.cfgVad = tk.BooleanVar(value=not self.cfg.epConfig.medConfig.noVad)
268 self.cfgEcTailLen = tk.IntVar(value=self.cfg.epConfig.medConfig.ecTailLen)
269
270 # Build the tab page
271 frm = ttk.Frame(self.frm)
272 frm.columnconfigure(0, weight=1)
273 frm.columnconfigure(1, weight=2)
274 row = 0
275 ttk.Label(frm, text='Max media ports:').grid(row=row, column=0, sticky=tk.E, pady=2, padx=8)
276 ttk.Label(frm, text=str(self.cfg.epConfig.medConfig.maxMediaPorts)).grid(row=row, column=1, sticky=tk.W, pady=2, padx=6)
277 row += 1
278 ttk.Label(frm, text='Core clock rate:').grid(row=row, column=0, sticky=tk.E, pady=2, padx=8)
279 tk.Spinbox(frm, from_=8000, to=48000, increment=8000, textvariable=self.cfgClockrate, width=5).grid(row=row, column=1, sticky=tk.W, padx=6)
280 row += 1
281 ttk.Label(frm, text='Snd device clock rate:').grid(row=row, column=0, sticky=tk.E, pady=2, padx=8)
282 tk.Spinbox(frm, from_=0, to=48000, increment=8000, textvariable=self.cfgSndClockrate, width=5).grid(row=row, column=1, sticky=tk.W, padx=6)
283 ttk.Label(frm, text='(0: follow core)').grid(row=row, column=1, sticky=tk.E, pady=6, padx=6)
284 row += 1
285 ttk.Label(frm, text='Core ptime:').grid(row=row, column=0, sticky=tk.E, pady=2, padx=8)
286 tk.Spinbox(frm, from_=10, to=400, increment=10, textvariable=self.cfgAudioPtime, width=3).grid(row=row, column=1, sticky=tk.W, padx=6)
287 row += 1
288 ttk.Label(frm, text='RTP ptime:').grid(row=row, column=0, sticky=tk.E, pady=2, padx=8)
289 tk.Spinbox(frm, from_=20, to=400, increment=10, textvariable=self.cfgCodecPtime, width=3).grid(row=row, column=1, sticky=tk.W, padx=6)
290 row += 1
291 ttk.Label(frm, text='Media quality (1-10):').grid(row=row, column=0, sticky=tk.E, pady=2, padx=8)
292 tk.Spinbox(frm, from_=1, to=10, textvariable=self.cfgMediaQuality, width=5).grid(row=row, column=1, sticky=tk.W, padx=6)
293 row += 1
294 ttk.Label(frm, text='VAD:').grid(row=row, column=0, sticky=tk.E, pady=2, padx=8)
295 ttk.Checkbutton(frm, text='Enable', variable=self.cfgVad).grid(row=row, column=1, sticky=tk.W, padx=6, pady=2)
296 row += 1
297 ttk.Label(frm, text='Echo canceller tail length:').grid(row=row, column=0, sticky=tk.E, pady=2, padx=8)
298 tk.Spinbox(frm, from_=0, to=400, increment=10, textvariable=self.cfgEcTailLen, width=3).grid(row=row, column=1, sticky=tk.W, padx=6)
299 ttk.Label(frm, text='(ms, 0 to disable)').grid(row=row, column=1, sticky=tk.E, pady=6, padx=6)
300
301 self.wTab.add(frm, text='Media')
302
303 def onOk(self):
304 # Check basic settings
305 errors = "";
306 if errors:
307 msgbox.showerror("Error detected:", errors)
308 return
309
310 # Basic settings
311 self.cfg.epConfig.logConfig.filename = self.cfgLogFile.get()
312 flags = pj.PJ_O_APPEND if self.cfgLogAppend.get() else 0
313 self.cfg.epConfig.logConfig.fileFlags = self.cfg.epConfig.logConfig.fileFlags | flags
314
315 # Network settings
316 self.cfg.epConfig.uaConfig.nameserver.clear()
317 if len(self.cfgNameserver.get()):
318 self.cfg.epConfig.uaConfig.nameserver.append(self.cfgNameserver.get())
319 self.cfg.epConfig.uaConfig.stunServer.clear()
320 if len(self.cfgStunServer.get()):
321 self.cfg.epConfig.uaConfig.stunServer.append(self.cfgStunServer.get())
322
323 self.cfg.epConfig.uaConfig.stunIgnoreFailure = self.cfgStunIgnoreError.get()
324
325 self.cfg.udp.enabled = self.cfgUdpEnabled.get()
326 self.cfg.udp.config.port = self.cfgUdpPort.get()
327 self.cfg.tcp.enabled = self.cfgTcpEnabled.get()
328 self.cfg.tcp.config.port = self.cfgTcpPort.get()
329 self.cfg.tls.enabled = self.cfgTlsEnabled.get()
330 self.cfg.tls.config.port = self.cfgTlsPort.get()
331
332 self.cfg.tls.config.tlsConfig.CaListFile = self.cfgTlsCaFile.get()
333 self.cfg.tls.config.tlsConfig.certFile = self.cfgTlsCertFile.get()
334 self.cfg.tls.config.tlsConfig.verifyClient = self.cfgTlsVerifyClient.get()
335 self.cfg.tls.config.tlsConfig.verifyServer = self.cfgTlsVerifyServer.get()
336
337 # Media
338 self.cfg.epConfig.medConfig.clockRate = self.cfgClockrate.get()
339 self.cfg.epConfig.medConfig.sndClockRate = self.cfgSndClockrate.get()
340 self.cfg.epConfig.medConfig.audioFramePtime = self.cfgAudioPtime.get()
341 self.cfg.epConfig.medConfig.quality = self.cfgMediaQuality.get()
342 self.cfg.epConfig.medConfig.ptime = self.cfgCodecPtime.get()
343 self.cfg.epConfig.medConfig.noVad = not self.cfgVad.get()
344 self.cfg.epConfig.medConfig.ecTailLen = self.cfgEcTailLen.get()
345
346 self.isOk = True
347 self.destroy()
348
349 def onCancel(self):
350 self.destroy()
351
352
353 if __name__ == '__main__':
354 #application.main()
355 acfg = AppConfig()
356 acfg.loadFile('pygui.js')
357
358 dlg = Dialog(None, acfg)
359 if dlg.doModal():
360 acfg.saveFile('pygui.js')
361