-
Notifications
You must be signed in to change notification settings - Fork 5
/
PjsuaManager.cpp
310 lines (261 loc) · 7.79 KB
/
PjsuaManager.cpp
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/**********************************************************\
Original Author: Andrew Ofisher (zaltar)
License: GNU General Public License, version 3.0
http://www.gnu.org/licenses/gpl-3.0.txt
Copyright 2012 Andrew Ofisher
\**********************************************************/
#include "PjsuaManager.h"
#include "Blabble.h"
#include "BlabbleAccount.h"
#include "BlabbleCall.h"
#include "BlabbleAudioManager.h"
#include "BlabbleLogging.h"
PjsuaManagerWeakPtr PjsuaManager::instance_;
PjsuaManagerPtr PjsuaManager::GetManager(const std::string& path, bool enableIce,
const std::string& stunServer)
{
PjsuaManagerPtr tmp = instance_.lock();
if(!tmp)
{
tmp = PjsuaManagerPtr(new PjsuaManager(path, enableIce, stunServer));
instance_ = boost::weak_ptr<PjsuaManager>(tmp);
}
return tmp;
}
PjsuaManager::PjsuaManager(const std::string& executionPath, bool enableIce,
const std::string& stunServer)
{
pj_status_t status;
pjsua_config cfg;
pjsua_logging_config log_cfg;
pjsua_media_config media_cfg;
pjsua_transport_config tran_cfg, tls_tran_cfg;
pjsua_transport_config_default(&tran_cfg);
pjsua_transport_config_default(&tls_tran_cfg);
pjsua_media_config_default(&media_cfg);
pjsua_config_default(&cfg);
pjsua_logging_config_default(&log_cfg);
cfg.max_calls = 511;
cfg.cb.on_incoming_call = &PjsuaManager::OnIncomingCall;
cfg.cb.on_call_media_state = &PjsuaManager::OnCallMediaState;
cfg.cb.on_call_state = &PjsuaManager::OnCallState;
cfg.cb.on_reg_state = &PjsuaManager::OnRegState;
cfg.cb.on_transport_state = &PjsuaManager::OnTransportState;
cfg.cb.on_call_transfer_status = &PjsuaManager::OnCallTransferStatus;
log_cfg.console_level = 4;
log_cfg.level = 4;
log_cfg.msg_logging = PJ_FALSE;
log_cfg.decor = PJ_LOG_HAS_SENDER | PJ_LOG_HAS_SPACE | PJ_LOG_HAS_LEVEL_TEXT;
log_cfg.cb = BlabbleLogging::blabbleLog;
tls_tran_cfg.port = 0;
//tran_cfg.tls_setting.verify_server = PJ_TRUE;
tls_tran_cfg.tls_setting.timeout.sec = 5;
tls_tran_cfg.tls_setting.method = PJSIP_TLSV1_METHOD;
tran_cfg.port = 0;
media_cfg.no_vad = 1;
media_cfg.enable_ice = enableIce ? PJ_TRUE : PJ_FALSE;
if (!stunServer.empty())
{
cfg.stun_srv_cnt = 1;
cfg.stun_srv[0] = pj_str(const_cast<char*>(stunServer.c_str()));
}
status = pjsua_create();
if (status != PJ_SUCCESS)
throw std::runtime_error("pjsua_create failed");
status = pjsua_init(&cfg, &log_cfg, &media_cfg);
if (status != PJ_SUCCESS)
throw std::runtime_error("Error in pjsua_init()");
try
{
status = pjsua_transport_create(PJSIP_TRANSPORT_TLS, &tls_tran_cfg, &this->tls_transport);
has_tls_ = status == PJ_SUCCESS;
if (!has_tls_) {
BLABBLE_LOG_DEBUG("Error in tls pjsua_transport_create. Tls will not be enabled");
this->tls_transport = -1;
}
status = pjsua_transport_create(PJSIP_TRANSPORT_UDP, &tran_cfg, &this->udp_transport);
if (status != PJ_SUCCESS)
throw std::runtime_error("Error in pjsua_transport_create for UDP transport");
status = pjsua_start();
if (status != PJ_SUCCESS)
throw std::runtime_error("Error in pjsua_start()");
std::string path = executionPath;
unsigned int tmp = path.find("plugins");
if (tmp != std::string::npos)
{
path = path.substr(0, tmp + 7);
}
tmp = path.find("npBlabblePhone.");
if (tmp != std::string::npos)
{
path = path.substr(0, tmp - 1);
}
audio_manager_ = boost::make_shared<BlabbleAudioManager>(path);
BLABBLE_LOG_DEBUG("PjsuaManager startup complete.");
}
catch (std::runtime_error& e)
{
BLABBLE_LOG_ERROR("Error in PjsuaManager. " << e.what());
pjsua_destroy();
throw e;
}
}
PjsuaManager::~PjsuaManager()
{
pjsua_call_hangup_all();
accounts_.clear();
if (audio_manager_)
audio_manager_.reset();
pjsua_destroy();
}
void PjsuaManager::AddAccount(const BlabbleAccountPtr &account)
{
if (account->id() == INVALID_ACCOUNT)
throw std::runtime_error("Attempt to add uninitialized account.");
accounts_[account->id()] = account;
}
void PjsuaManager::RemoveAccount(pjsua_acc_id acc_id)
{
accounts_.erase(acc_id);
}
BlabbleAccountPtr PjsuaManager::FindAcc(int acc_id)
{
if (pjsua_acc_is_valid(acc_id) == PJ_TRUE)
{
BlabbleAccountMap::iterator it = accounts_.find(acc_id);
if (it != accounts_.end())
{
return it->second;
}
}
return BlabbleAccountPtr();
}
//Event handlers
//Static
void PjsuaManager::OnTransportState(pjsip_transport *tp, pjsip_transport_state state,
const pjsip_transport_state_info *info)
{
if (state == PJSIP_TP_STATE_DISCONNECTED)
{
pjsip_tls_state_info *tmp = ((pjsip_tls_state_info*)info->ext_info);
if (tmp->ssl_sock_info->verify_status != PJ_SSL_CERT_ESUCCESS)
{
//bad cert
}
}
}
//Static
void PjsuaManager::OnIncomingCall(pjsua_acc_id acc_id, pjsua_call_id call_id, pjsip_rx_data *rdata)
{
BLABBLE_LOG_TRACE("OnIncomingCall called for PJSIP account id: " << acc_id <<
", PJSIP call id: " << call_id);
PjsuaManagerPtr manager = PjsuaManager::instance_.lock();
if (!manager)
{
//How is this even possible?
pjsua_call_hangup(call_id, 0, NULL, NULL);
return;
}
BlabbleAccountPtr acc = manager->FindAcc(acc_id);
if (acc && acc->OnIncomingCall(call_id, rdata))
{
return;
}
//Otherwise we respond busy if no one wants the call
pjsua_call_hangup(call_id, 486, NULL, NULL);
}
//Static
void PjsuaManager::OnCallMediaState(pjsua_call_id call_id)
{
PjsuaManagerPtr manager = PjsuaManager::instance_.lock();
if (!manager)
return;
pjsua_call_info info;
pj_status_t status;
if ((status = pjsua_call_get_info(call_id, &info)) == PJ_SUCCESS)
{
BLABBLE_LOG_TRACE("PjsuaManager::OnCallMediaState called with PJSIP call id: "
<< call_id << ", state: " << info.state);
BlabbleAccountPtr acc = manager->FindAcc(info.acc_id);
if (acc)
{
acc->OnCallMediaState(call_id);
}
}
else
{
BLABBLE_LOG_ERROR("PjsuaManager::OnCallMediaState failed to call pjsua_call_get_info for PJSIP call id: "
<< call_id << ", got status: " << status);
}
}
//Static
void PjsuaManager::OnCallState(pjsua_call_id call_id, pjsip_event *e)
{
PjsuaManagerPtr manager = PjsuaManager::instance_.lock();
if (!manager)
return;
pjsua_call_info info;
pj_status_t status;
if ((status = pjsua_call_get_info(call_id, &info)) == PJ_SUCCESS)
{
BLABBLE_LOG_TRACE("PjsuaManager::OnCallState called with PJSIP call id: "
<< call_id << ", state: " << info.state);
BlabbleAccountPtr acc = manager->FindAcc(info.acc_id);
if (acc)
{
acc->OnCallState(call_id, e);
}
if (info.state == PJSIP_INV_STATE_DISCONNECTED)
{
//Just make sure we get rid of the call
pjsua_call_hangup(call_id, 0, NULL, NULL);
}
}
else
{
BLABBLE_LOG_ERROR("PjsuaManager::OnCallState failed to call pjsua_call_get_info for PJSIP call id: "
<< call_id << ", got status: " << status);
}
}
//Static
void PjsuaManager::OnRegState(pjsua_acc_id acc_id)
{
PjsuaManagerPtr manager = PjsuaManager::instance_.lock();
if (!manager)
return;
BlabbleAccountPtr acc = manager->FindAcc(acc_id);
if (acc)
{
acc->OnRegState();
}
else
{
BLABBLE_LOG_ERROR("PjsuaManager::OnRegState failed to find account PJSIP account id: "
<< acc_id);
}
}
//Static
void PjsuaManager::OnCallTransferStatus(pjsua_call_id call_id, int st_code, const pj_str_t *st_text, pj_bool_t final, pj_bool_t *p_cont)
{
BLABBLE_LOG_TRACE("PjsuaManager::OnCallTransferState called with PJSIP call id: "
<< call_id << ", state: " << st_code);
PjsuaManagerPtr manager = PjsuaManager::instance_.lock();
if (!manager)
return;
pjsua_call_info info;
pj_status_t status;
if ((status = pjsua_call_get_info(call_id, &info)) == PJ_SUCCESS)
{
BlabbleAccountPtr acc = manager->FindAcc(info.acc_id);
if (acc)
{
if (acc->OnCallTransferStatus(call_id, st_code))
(*p_cont) = PJ_TRUE;
}
}
else
{
BLABBLE_LOG_ERROR("PjsuaManager::OnCallTransferStatus failed to call pjsua_call_get_info for PJSIP call id: "
<< call_id << ", got status: " << status);
}
}