-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
217 lines (184 loc) · 6.72 KB
/
main.py
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
"""
@project: PJSIP-Lab
@author: sam
@file main.py
@ide: Visual Studio Code
@time: 2019-11-24 21:36:37
@blog: https://jiahaoplus.com
"""
import pjsua2 as pj
import tkinter as tk
from config import *
from call import Call
from tkinter import ttk
from buddy import Buddy
from account import Account
from chat import Chat
from endpoint import Endpoint
from login import Login
from tkinter import messagebox as msg
class Main(tk.Tk):
def __init__(self):
super().__init__()
"""
Initialize PJSUA2
"""
self.ep = Endpoint()
self.ep.libCreate()
self.ep_cfg = pj.EpConfig()
# self.ep_cfg.uaConfig.threadCnt = 0
# self.ep_cfg.uaConfig.mainThreadOnly = True
self.ep_cfg.logConfig.level = 1
self.ep.libInit(self.ep_cfg)
self.ts_cfg = pj.TransportConfig()
self.ep.transportCreate(pj.PJSIP_TRANSPORT_UDP, self.ts_cfg)
self.ep.libStart()
"""
Initialize Account
"""
self.acc = None
self.domain = DEFAULT_DOMAIN
self.buddy_list = {}
self.chat_list = {}
"""
Initialize UI
"""
self.title('SIP Client')
self.resizable(width=False, height=False)
self.geometry('+{}+{}'.format(int(self.winfo_screenwidth() / 2),
int(self.winfo_screenheight() / 2)))
self.buddy = tk.Entry(self, font=FONT_CONTENT, width=30)
self.buddy.grid(row=0, column=0, columnspan=3, padx=10, pady=10)
self.buddy.bind('<Return>', self._add_buddy)
self.buddy_view = ttk.Treeview(self, column=['1', '2'], show='headings',
selectmode='browse')
self.buddy_view.column('1', width=80, anchor='center')
self.buddy_view.column('2', width=200, anchor='center')
self.buddy_view.heading('1', text='Buddies')
self.buddy_view.heading('2', text='Status')
self.buddy_view.grid(row=1, column=0, columnspan=3, padx=10, pady=10)
self.buddy_view.bind('<Double-Button-1>', self._create_chat)
self.buddy_view.bind('<BackSpace>', self._delete_buddy)
tk.Button(self, text='Logout', font=FONT_CONTENT, width=8,
command=self._login).grid(row=2, column=0, padx=10, pady=10)
tk.Button(self, text='Exit', font=FONT_CONTENT, width=8,
command=self._exit).grid(row=2, column=2, padx=10, pady=10)
self._login()
# self._on_timer()
self.mainloop()
def _add_buddy(self, event):
iid = self.buddy.get()
if iid not in self.buddy_list:
# Initialize buddy
bud = Buddy(self, iid)
# Initialize configuration of buddy
bud_cfg = pj.BuddyConfig()
bud_cfg.uri = 'sip:' + iid + '@' + self.domain
# Create buddy
bud.cfg = bud_cfg
bud.create(self.acc, bud.cfg)
bud.subscribePresence(True)
# Push into buddy_list
self.buddy_list[iid] = bud
self.update_buddy(bud)
self.buddy.delete(0, 'end')
def _delete_buddy(self, event):
for key in self.buddy_view.selection():
self.buddy_view.delete(key)
self.buddy_list.pop(key)
def update_buddy(self, bud):
iid = bud.iid
if iid in self.buddy_list:
self.buddy_view.item(iid, value=(iid, bud.status()))
else:
self.buddy_view.insert(
'', 'end', iid=iid, values=(iid, bud.status()))
def update_account(self):
# TODO()
print(self.acc.status())
def incoming_call(self, prm):
call = Call(self.acc, call_id=prm.callId)
call_prm = pj.CallOpParam()
# Ringing State
call_prm.statusCode = pj.PJSIP_SC_RINGING
call.answer(call_prm)
# Set uri
call.uri = call.getInfo().remoteUri
iid = call.uri.split(':')[1].split('@')[0]
if msg.askquestion('Incoming Call', 'Accept call from ' + iid + ' ?', default=msg.YES):
# If not exist current buddy, then create
if iid not in self.buddy_list:
# Initialize buddy
bud = Buddy(self, iid)
# Initialize configuration of buddy
bud_cfg = pj.BuddyConfig()
bud_cfg.uri = 'sip:' + iid + '@' + self.domain
# Create buddy
bud.cfg = bud_cfg
bud.create(self.acc, bud.cfg)
bud.subscribePresence(True)
# Push into buddy_list
self.buddy_list[iid] = bud
self.update_buddy(bud)
# If not exist current chat dialog, then create
if iid not in self.chat_list:
self.chat_list[iid] = Chat(
self.acc, self.buddy_list[iid], self)
# Inform the corresponding chat
self.chat_list[iid].receive_call(call)
else:
call.hangup(call_prm)
def instant_message(self, uri, msg):
iid = uri[1: -1].split(':')[1].split('@')[0]
if iid in self.chat_list:
self.chat_list[iid].receive_message(msg)
else:
pass
def _create_chat(self, event):
for iid in self.buddy_view.selection():
self.chat_list[iid] = Chat(
self.acc, self.buddy_list[iid], self)
def delete_chat(self, iid):
self.chat_list.pop(iid)
def _login(self):
# Initialize configuration of account
acc_cfg = pj.AccountConfig()
# Create LoginDialog
dlg = Login(self, acc_cfg)
# Wait for response
if dlg.do_modal():
# Initialize account
self.acc = Account(self)
# Set configuration
self.acc.cfg = acc_cfg
self.acc.create(self.acc.cfg)
# Set online status
ps = pj.PresenceStatus()
ps.status = pj.PJSUA_BUDDY_STATUS_ONLINE
self.acc.setOnlineStatus(ps)
# Update title
self.title(self.acc.cfg.idUri)
# Get the uri of server
self.domain = self.acc.cfg.regConfig.registrarUri.split(':')[1]
# Reset all
self.buddy_list = {}
self.chat_list = {}
for key in self.buddy_view.get_children():
self.buddy_view.delete(key)
# If never login
if not self.acc:
# Then exit
self._exit()
def _exit(self):
self.ep.libDestroy()
self.ep = None
self.destroy()
# def _on_timer(self):
# """
# Important!!! Can't remove!!!
# """
# if self.ep:
# self.ep.libHandleEvents(10)
# self.after(50, self._on_timer)
if __name__ == '__main__':
main = Main()