-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form1.cs
290 lines (231 loc) · 7.99 KB
/
Form1.cs
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
using System;
using System.Windows.Forms;
using Ozeki.Media;
using Ozeki.VoIP;
namespace _14_Call_Assistant
{
/// <summary>
///
/// </summary>
public partial class FormCallAssistant : Form
{
ISoftPhone _softPhone;
IPhoneLine _phoneLine;
RegState _phoneLineState;
IPhoneCall _call;
Microphone _microphone;
Speaker _speaker;
MediaConnector _connector;
PhoneCallAudioSender _mediaSender;
PhoneCallAudioReceiver _mediaReceiver;
DatabaseManager _databaseManager;
UserInfo _otherParty;
bool _incomingCall;
public FormCallAssistant()
{
InitializeComponent();
}
void form_CallAssistant_Load(object sender, EventArgs e)
{
_microphone = Microphone.GetDefaultDevice();
_speaker = Speaker.GetDefaultDevice();
_connector = new MediaConnector();
_mediaSender = new PhoneCallAudioSender();
_mediaReceiver = new PhoneCallAudioReceiver();
_databaseManager = new DatabaseManager();
InitializeSoftphone();
}
void InitializeSoftphone()
{
try
{
_softPhone = SoftPhoneFactory.CreateSoftPhone(SoftPhoneFactory.GetLocalIP(), 5700, 5750);
SIPAccount sa = new SIPAccount(true, "1000", "1000", "1000", "1000", "192.168.115.100", 5060);
_phoneLine = _softPhone.CreatePhoneLine(sa);
_phoneLine.RegistrationStateChanged += _phoneLine_RegistrationStateChanged;
_softPhone.IncomingCall += _softPhone_IncomingCall;
_softPhone.RegisterPhoneLine(_phoneLine);
_incomingCall = false;
ConnectMedia();
}
catch (Exception ex)
{
InvokeGUIThread(() => { tb_Display.Text = ex.Message; });
}
}
void _phoneLine_RegistrationStateChanged(object sender, RegistrationStateChangedArgs e)
{
_phoneLineState = e.State;
if (_phoneLineState == RegState.RegistrationSucceeded)
{
InvokeGUIThread(() => { lbl_UserName.Text = _phoneLine.SIPAccount.UserName; });
InvokeGUIThread(() => { lbl_DomainHost.Text = _phoneLine.SIPAccount.DomainServerHost; });
}
}
void _softPhone_IncomingCall(object sender, VoIPEventArgs<IPhoneCall> e)
{
var userName = e.Item.DialInfo.Dialed;
InvokeGUIThread(() => { tb_Display.Text = "Ringing (" + userName + ")"; });
_call = e.Item;
WireUpCallEvents();
_incomingCall = true;
_otherParty = _databaseManager.GetOtherPartyInfos(userName);
ShowUserInfos(_otherParty);
}
void call_CallStateChanged(object sender, CallStateChangedArgs e)
{
InvokeGUIThread(() => { lbl_CallState.Text = e.State.ToString(); });
if (e.State == CallState.Answered)
{
StartDevices();
_mediaSender.AttachToCall(_call);
_mediaReceiver.AttachToCall(_call);
InvokeGUIThread(() => { tb_Display.Text = "In call with: " + ((IPhoneCall)sender).DialInfo.Dialed; });
}
else if (e.State == CallState.InCall)
{
StartDevices();
}
if (e.State == CallState.LocalHeld || e.State == CallState.InactiveHeld)
{
StopDevices();
InvokeGUIThread(() => { btn_Hold.Text = "Unhold"; });
}
else
{
InvokeGUIThread(() => { btn_Hold.Text = "Hold"; });
}
if (e.State.IsCallEnded())
{
StopDevices();
_mediaSender.Detach();
_mediaReceiver.Detach();
WireDownCallEvents();
_call = null;
InvokeGUIThread(() => { tb_Display.Text = String.Empty; });
ClearUserInfos();
}
}
void ShowUserInfos(UserInfo otherParty)
{
InvokeGUIThread(() =>
{
tb_OtherPartyUserName.Text = otherParty.UserName;
tb_OtherPartyRealName.Text = otherParty.RealName;
tb_OtherPartyCountry.Text = otherParty.Country;
tb_OtherPartyNote.Text = otherParty.Note;
});
}
void ClearUserInfos()
{
InvokeGUIThread(() =>
{
tb_OtherPartyUserName.Text = String.Empty;
tb_OtherPartyRealName.Text = String.Empty;
tb_OtherPartyCountry.Text = String.Empty;
tb_OtherPartyNote.Text = String.Empty;
});
}
void buttonKeyPadButton_Click(object sender, EventArgs e)
{
var btn = sender as Button;
if (_call != null)
return;
if (btn == null)
return;
tb_Display.Text += btn.Text.Trim();
}
void StartDevices()
{
if (_microphone != null)
_microphone.Start();
if (_speaker != null)
_speaker.Start();
}
void StopDevices()
{
if (_microphone != null)
_microphone.Stop();
if (_speaker != null)
_speaker.Stop();
}
void ConnectMedia()
{
if (_microphone != null)
_connector.Connect(_microphone, _mediaSender);
if (_speaker != null)
_connector.Connect(_mediaReceiver, _speaker);
}
void WireUpCallEvents()
{
_call.CallStateChanged += (call_CallStateChanged);
}
void WireDownCallEvents()
{
_call.CallStateChanged -= (call_CallStateChanged);
}
void InvokeGUIThread(Action action)
{
Invoke(action);
}
void btn_PickUp_Click(object sender, EventArgs e)
{
if (_incomingCall)
{
_incomingCall = false;
_call.Answer();
return;
}
if (_call != null)
return;
if (_phoneLineState != RegState.RegistrationSucceeded)
{
InvokeGUIThread(() => { tb_Display.Text = "OFFLINE! Please register."; });
return;
}
if (!String.IsNullOrEmpty(tb_Display.Text))
{
var userName = tb_Display.Text;
_call = _softPhone.CreateCallObject(_phoneLine, userName);
WireUpCallEvents();
_call.Start();
_otherParty = _databaseManager.GetOtherPartyInfos(userName);
ShowUserInfos(_otherParty);
}
}
void btn_HangUp_Click(object sender, EventArgs e)
{
if (_call != null)
{
if (_incomingCall && _call.CallState == CallState.Ringing)
{
_call.Reject();
}
else
{
_call.HangUp();
}
_incomingCall = false;
_call = null;
}
tb_Display.Text = string.Empty;
}
void btn_Transfer_Click(object sender, EventArgs e)
{
string transferTo = "1001";
if (_call == null)
return;
if (string.IsNullOrEmpty(transferTo))
return;
if (_call.CallState != CallState.InCall)
return;
_call.BlindTransfer(transferTo);
InvokeGUIThread(() => { tb_Display.Text = "Transfering to:" + transferTo; });
}
void btn_Hold_Click(object sender, EventArgs e)
{
if (_call != null)
_call.ToggleHold();
}
}
}