Benny Prijono | b9b32ab | 2006-06-01 12:28:44 +0000 | [diff] [blame^] | 1 | // App.cpp : Implementation of CApp |
| 2 | #include "stdafx.h" |
| 3 | #include <pj/types.h> |
| 4 | #include <pjsua-lib/pjsua.h> |
| 5 | #include "activex-pjsua.h" |
| 6 | #include "app.h" |
| 7 | |
| 8 | |
| 9 | ///////////////////////////////////////////////////////////////////////////// |
| 10 | // CApp |
| 11 | |
| 12 | // {9CE3052A-7A32-4229-B31C-5E02E0667A77} |
| 13 | static const GUID IID_Pjsip_Cred_Info = |
| 14 | { 0x9ce3052a, 0x7a32, 0x4229, { 0xb3, 0x1c, 0x5e, 0x2, 0xe0, 0x66, 0x7a, 0x77 } }; |
| 15 | |
| 16 | // {3B12B04F-6E48-46a7-B9E0-6C4BF1594A96} |
| 17 | static const GUID IID_Pjsua_Acc_Config = |
| 18 | { 0x3b12b04f, 0x6e48, 0x46a7, { 0xb9, 0xe0, 0x6c, 0x4b, 0xf1, 0x59, 0x4a, 0x96 } }; |
| 19 | |
| 20 | // {E4B6573D-CF5E-484d-863F-ADAD5947FBE4} |
| 21 | static const GUID IID_Pjsua_Config = |
| 22 | { 0xe4b6573d, 0xcf5e, 0x484d, { 0x86, 0x3f, 0xad, 0xad, 0x59, 0x47, 0xfb, 0xe4 } }; |
| 23 | |
| 24 | // {5043AC9E-A417-4f03-927E-D7AE52DDD064} |
| 25 | static const GUID IID_Pjsua_Call_Info = |
| 26 | { 0x5043ac9e, 0xa417, 0x4f03, { 0x92, 0x7e, 0xd7, 0xae, 0x52, 0xdd, 0xd0, 0x64 } }; |
| 27 | |
| 28 | // {2729F0BC-8A5E-4f3f-BC29-C1740A86393A} |
| 29 | static const GUID IID_Pjsua_Buddy_Info = |
| 30 | { 0x2729f0bc, 0x8a5e, 0x4f3f, { 0xbc, 0x29, 0xc1, 0x74, 0xa, 0x86, 0x39, 0x3a } }; |
| 31 | |
| 32 | // {8D345956-10B7-4450-8A06-A80D2F319EFD} |
| 33 | static const GUID IID_Pjsua_Acc_Info = |
| 34 | { 0x8d345956, 0x10b7, 0x4450, { 0x8a, 0x6, 0xa8, 0xd, 0x2f, 0x31, 0x9e, 0xfd } }; |
| 35 | |
| 36 | #define SA_SIZE(lbound,ubound) (ubound-lbound) |
| 37 | |
| 38 | |
| 39 | class Temp_Pool |
| 40 | { |
| 41 | public: |
| 42 | Temp_Pool() |
| 43 | { |
| 44 | pool_ = pjsip_endpt_create_pool( pjsua_get_pjsip_endpt(), "ActivePJSUA", |
| 45 | 4000, 4000); |
| 46 | } |
| 47 | ~Temp_Pool() |
| 48 | { |
| 49 | pj_pool_release(pool_); |
| 50 | } |
| 51 | |
| 52 | pj_pool_t *get_pool() |
| 53 | { |
| 54 | return pool_; |
| 55 | } |
| 56 | |
| 57 | private: |
| 58 | pj_pool_t *pool_; |
| 59 | }; |
| 60 | |
| 61 | static pj_str_t Pj_str(pj_pool_t *pool, Pj_String s) |
| 62 | { |
| 63 | pj_str_t ret; |
| 64 | unsigned len; |
| 65 | |
| 66 | len = wcslen(s); |
| 67 | if (len) { |
| 68 | ret.ptr = (char*)pj_pool_alloc(pool, len+1); |
| 69 | ret.slen = len; |
| 70 | pj_unicode_to_ansi(s, len, ret.ptr, len+1); |
| 71 | ret.ptr[ret.slen] = '\0'; |
| 72 | } else { |
| 73 | ret.ptr = NULL; |
| 74 | ret.slen = 0; |
| 75 | } |
| 76 | |
| 77 | return ret; |
| 78 | } |
| 79 | |
| 80 | BSTR str2bstr(const char *str, unsigned len) |
| 81 | { |
| 82 | if (len == 0) { |
| 83 | return SysAllocString(L""); |
| 84 | } else { |
| 85 | OLECHAR *tmp; |
| 86 | BSTR result; |
| 87 | tmp = (OLECHAR*) malloc((len+1) * sizeof(OLECHAR)); |
| 88 | pj_ansi_to_unicode(str, len, tmp, len+1); |
| 89 | result = SysAllocString(tmp); |
| 90 | free(tmp); |
| 91 | return result; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | #define Cp(d,s) Cp2(&d,s) |
| 96 | static void Cp2(BSTR *dst, const pj_str_t *src) |
| 97 | { |
| 98 | *dst = str2bstr(src->ptr, src->slen); |
| 99 | } |
| 100 | |
| 101 | |
| 102 | |
| 103 | static void SafeStringArray2pjstrarray(pj_pool_t *pool, |
| 104 | SAFEARRAY *sa, unsigned *count, |
| 105 | pj_str_t a[]) |
| 106 | { |
| 107 | if (!sa) |
| 108 | *count = 0; |
| 109 | else { |
| 110 | HRESULT hr; |
| 111 | long lbound; |
| 112 | unsigned i; |
| 113 | |
| 114 | hr = SafeArrayGetLBound(sa, 1, &lbound); |
| 115 | if (FAILED(hr)) |
| 116 | *count = 0; |
| 117 | else { |
| 118 | *count = 0; |
| 119 | for (i=0; i<sa->cbElements; ++i) { |
| 120 | BSTR str; |
| 121 | long rg = lbound + i; |
| 122 | hr = SafeArrayGetElement(sa, &rg, &str); |
| 123 | if (FAILED(hr)) |
| 124 | break; |
| 125 | a[*count] = Pj_str(pool, str); |
| 126 | *count = *count + 1; |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | static void pjstrarray2SafeStringArray(unsigned count, const pj_str_t a[], |
| 133 | SAFEARRAY **psa) |
| 134 | { |
| 135 | unsigned i; |
| 136 | SAFEARRAY *sa; |
| 137 | |
| 138 | sa = SafeArrayCreateVector( VT_BSTR, 0, count); |
| 139 | |
| 140 | for (i=0; i<count; ++i) { |
| 141 | BSTR value; |
| 142 | Cp(value, &a[i]); |
| 143 | long rg = i; |
| 144 | SafeArrayPutElement(sa, &rg, value); |
| 145 | } |
| 146 | |
| 147 | *psa = sa; |
| 148 | } |
| 149 | |
| 150 | static void AccConfig2accconfig(pj_pool_t *pool, |
| 151 | Pjsua_Acc_Config *c1, |
| 152 | pjsua_acc_config *c2) |
| 153 | { |
| 154 | pj_memset(c2, 0, sizeof(pjsua_acc_config)); |
| 155 | |
| 156 | c2->id = Pj_str(pool, c1->acc_uri); |
| 157 | c2->reg_uri = Pj_str(pool, c1->reg_uri); |
| 158 | c2->contact = Pj_str(pool, c1->contact_uri); |
| 159 | c2->proxy = Pj_str(pool, c1->proxy_uri); |
| 160 | c2->reg_timeout = c1->reg_timeout; |
| 161 | |
| 162 | if (c1->cred_info == NULL) { |
| 163 | c2->cred_count = 0; |
| 164 | } else { |
| 165 | unsigned i; |
| 166 | long lbound; |
| 167 | HRESULT hr; |
| 168 | |
| 169 | hr = SafeArrayGetLBound(c1->cred_info, 1, &lbound); |
| 170 | if (FAILED(hr)) { |
| 171 | c2->cred_count = 0; |
| 172 | } else { |
| 173 | c2->cred_count = 0; |
| 174 | for (i=0; i<c1->cred_info->cbElements; ++i) { |
| 175 | Pjsip_Cred_Info cred_info; |
| 176 | long rg = lbound + i; |
| 177 | hr = SafeArrayGetElement(c1->cred_info, &rg, &cred_info); |
| 178 | if (FAILED(hr)) |
| 179 | break; |
| 180 | c2->cred_info[i].realm = Pj_str(pool, cred_info.realm); |
| 181 | c2->cred_info[i].scheme = Pj_str(pool, cred_info.scheme); |
| 182 | c2->cred_info[i].username = Pj_str(pool, cred_info.username); |
| 183 | c2->cred_info[i].data_type = cred_info.hashed; |
| 184 | c2->cred_info[i].data = Pj_str(pool, cred_info.data); |
| 185 | } |
| 186 | c2->cred_count = i; |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | static HRESULT accconfig2AccConfig(pjsua_acc_config *c1, |
| 192 | Pjsua_Acc_Config *c2) |
| 193 | { |
| 194 | unsigned i; |
| 195 | |
| 196 | //pj_memset(c2, 0, sizeof(Pjsua_Acc_Config)); |
| 197 | |
| 198 | Cp(c2->acc_uri, &c1->id); |
| 199 | Cp(c2->reg_uri, &c1->reg_uri); |
| 200 | Cp(c2->contact_uri, &c1->contact); |
| 201 | Cp(c2->proxy_uri, &c1->proxy); |
| 202 | c2->reg_timeout = c1->reg_timeout; |
| 203 | |
| 204 | |
| 205 | IRecordInfo *pUdtRecordInfo = NULL; |
| 206 | HRESULT hr = GetRecordInfoFromGuids( LIBID_ACTIVEPJSUALib, |
| 207 | 1, 0, |
| 208 | 0, |
| 209 | IID_Pjsip_Cred_Info, |
| 210 | &pUdtRecordInfo ); |
| 211 | if( FAILED( hr ) ) { |
| 212 | return( hr ); //Return original HRESULT hr2 is for debug only |
| 213 | } |
| 214 | |
| 215 | SAFEARRAYBOUND rgsabound[1]; |
| 216 | rgsabound[0].lLbound = 0; |
| 217 | rgsabound[0].cElements = c1->cred_count; |
| 218 | |
| 219 | c2->cred_info = ::SafeArrayCreateEx( VT_RECORD, 1, rgsabound, pUdtRecordInfo ); |
| 220 | |
| 221 | pUdtRecordInfo->Release(); //do not forget to release the interface |
| 222 | |
| 223 | for (i=0; i<c1->cred_count; ++i) { |
| 224 | Pjsip_Cred_Info cred_info; |
| 225 | |
| 226 | Cp(cred_info.realm, &c1->cred_info[i].realm); |
| 227 | Cp(cred_info.scheme, &c1->cred_info[i].scheme); |
| 228 | Cp(cred_info.username, &c1->cred_info[i].username); |
| 229 | cred_info.hashed = (c1->cred_info[i].data_type != 0); |
| 230 | Cp(cred_info.data, &c1->cred_info[i].data); |
| 231 | |
| 232 | long rg = i; |
| 233 | SafeArrayPutElement(c2->cred_info, &rg, &cred_info); |
| 234 | } |
| 235 | |
| 236 | return S_OK; |
| 237 | } |
| 238 | |
| 239 | static HRESULT Config2config(pj_pool_t *pool, Pjsua_Config *c1, pjsua_config *c2) |
| 240 | { |
| 241 | pj_memset(c2, 0, sizeof(pjsua_config)); |
| 242 | |
| 243 | c2->udp_port = c1->udp_port; |
| 244 | c2->sip_host = Pj_str(pool, c1->sip_host); |
| 245 | c2->sip_port = c1->sip_port; |
| 246 | c2->start_rtp_port = c1->rtp_port; |
| 247 | c2->max_calls = c1->max_calls; |
| 248 | c2->conf_ports = c1->conf_ports; |
| 249 | c2->thread_cnt = c1->thread_cnt; |
| 250 | c2->stun_srv1 = Pj_str(pool, c1->stun_srv1); |
| 251 | c2->stun_port1 = c1->stun_port1; |
| 252 | c2->stun_srv2 = Pj_str(pool, c1->stun_srv2); |
| 253 | c2->stun_port2 = c1->stun_port2; |
| 254 | c2->snd_player_id = c1->snd_player_id; |
| 255 | c2->snd_capture_id = c1->snd_capture_id; |
| 256 | c2->clock_rate = c1->clock_rate; |
| 257 | c2->null_audio = c1->null_audio; |
| 258 | c2->quality = c1->quality; |
| 259 | c2->complexity = c1->complexity; |
| 260 | |
| 261 | SafeStringArray2pjstrarray(pool, c1->codec_arg, &c2->codec_cnt, c2->codec_arg); |
| 262 | |
| 263 | c2->auto_answer = c1->auto_answer; |
| 264 | c2->uas_refresh = c1->uas_refresh; |
| 265 | c2->outbound_proxy = Pj_str(pool, c1->outbound_proxy); |
| 266 | |
| 267 | if (!c1->acc_config) |
| 268 | c2->acc_cnt = 0; |
| 269 | else { |
| 270 | HRESULT hr; |
| 271 | long lbound; |
| 272 | unsigned i; |
| 273 | |
| 274 | hr = SafeArrayGetLBound(c1->acc_config, 1, &lbound); |
| 275 | if (FAILED(hr)) |
| 276 | c2->acc_cnt = 0; |
| 277 | else { |
| 278 | c2->acc_cnt = 0; |
| 279 | |
| 280 | for (i=0; i<c1->acc_config->cbElements; ++i) { |
| 281 | Pjsua_Acc_Config acc_config; |
| 282 | long rg = lbound + i; |
| 283 | hr = SafeArrayGetElement(c1->acc_config, &rg, &acc_config); |
| 284 | if (FAILED(hr)) |
| 285 | break; |
| 286 | AccConfig2accconfig(pool, &acc_config, &c2->acc_config[i]); |
| 287 | } |
| 288 | |
| 289 | c2->acc_cnt = i; |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | c2->log_level = c1->log_level; |
| 294 | c2->app_log_level = c1->app_log_level; |
| 295 | c2->log_decor = c1->log_decor; |
| 296 | c2->log_filename = Pj_str(pool, c1->log_filename); |
| 297 | |
| 298 | SafeStringArray2pjstrarray(pool, c1->buddy_uri, &c2->buddy_cnt, c2->buddy_uri); |
| 299 | |
| 300 | return S_OK; |
| 301 | } |
| 302 | |
| 303 | static HRESULT config2Config(pjsua_config *c1, Pjsua_Config *c2) |
| 304 | { |
| 305 | unsigned i; |
| 306 | HRESULT hr; |
| 307 | |
| 308 | //pj_memset(c2, 0, sizeof(Pjsua_Config)); |
| 309 | |
| 310 | c2->udp_port = c1->udp_port; |
| 311 | Cp(c2->sip_host, &c1->sip_host); |
| 312 | c2->sip_port = c1->sip_port; |
| 313 | c2->rtp_port = c1->start_rtp_port; |
| 314 | c2->max_calls = c1->max_calls; |
| 315 | c2->conf_ports = c1->conf_ports; |
| 316 | c2->thread_cnt = c1->thread_cnt; |
| 317 | Cp(c2->stun_srv1, &c1->stun_srv1); |
| 318 | c2->stun_port1 = c1->stun_port1; |
| 319 | Cp(c2->stun_srv2, &c1->stun_srv2); |
| 320 | c2->stun_port2 = c1->stun_port2; |
| 321 | c2->snd_player_id = c1->snd_player_id; |
| 322 | c2->snd_capture_id = c1->snd_capture_id; |
| 323 | c2->clock_rate = c1->clock_rate; |
| 324 | c2->null_audio = c1->null_audio; |
| 325 | c2->quality = c1->quality; |
| 326 | c2->complexity = c1->complexity; |
| 327 | |
| 328 | pjstrarray2SafeStringArray(c1->codec_cnt, c1->codec_arg, &c2->codec_arg); |
| 329 | |
| 330 | c2->auto_answer = c1->auto_answer; |
| 331 | c2->uas_refresh = c1->uas_refresh; |
| 332 | |
| 333 | Cp(c2->outbound_proxy, &c1->outbound_proxy); |
| 334 | |
| 335 | IRecordInfo *pUdtRecordInfo = NULL; |
| 336 | hr = GetRecordInfoFromGuids( LIBID_ACTIVEPJSUALib, |
| 337 | 1, 0, |
| 338 | 0, |
| 339 | IID_Pjsua_Acc_Config, |
| 340 | &pUdtRecordInfo ); |
| 341 | if( FAILED( hr ) ) { |
| 342 | return( hr ); //Return original HRESULT hr2 is for debug only |
| 343 | } |
| 344 | |
| 345 | SAFEARRAYBOUND rgsabound[1]; |
| 346 | rgsabound[0].lLbound = 0; |
| 347 | rgsabound[0].cElements = c1->acc_cnt; |
| 348 | |
| 349 | c2->acc_config = ::SafeArrayCreateEx( VT_RECORD, 1, rgsabound, pUdtRecordInfo ); |
| 350 | |
| 351 | pUdtRecordInfo->Release(); //do not forget to release the interface |
| 352 | |
| 353 | for (i=0; i<c1->acc_cnt; ++i) { |
| 354 | Pjsua_Acc_Config acc_cfg; |
| 355 | |
| 356 | hr = accconfig2AccConfig(&c1->acc_config[i], &acc_cfg); |
| 357 | if (FAILED(hr)) |
| 358 | return hr; |
| 359 | |
| 360 | long rg = i; |
| 361 | SafeArrayPutElement(c2->acc_config, &rg, &acc_cfg); |
| 362 | } |
| 363 | |
| 364 | |
| 365 | c2->log_level = c1->log_level; |
| 366 | c2->app_log_level = c1->app_log_level; |
| 367 | c2->log_decor = c1->log_decor; |
| 368 | |
| 369 | Cp(c2->log_filename, &c1->log_filename); |
| 370 | |
| 371 | pjstrarray2SafeStringArray(c1->buddy_cnt, c1->buddy_uri, &c2->buddy_uri); |
| 372 | |
| 373 | return S_OK; |
| 374 | } |
| 375 | |
| 376 | static void callinfo2CallInfo(pjsua_call_info *c1, Pjsua_Call_Info *c2) |
| 377 | { |
| 378 | pj_memset(c2, 0, sizeof(Pjsua_Call_Info)); |
| 379 | |
| 380 | c2->index = c1->index; |
| 381 | c2->active = c1->active; |
| 382 | c2->is_uac = (c1->role == PJSIP_ROLE_UAC); |
| 383 | Cp(c2->local_info, &c1->local_info); |
| 384 | Cp(c2->remote_info, &c1->remote_info); |
| 385 | c2->state = (Pjsua_Call_State)c1->state; |
| 386 | Cp(c2->state_text, &c1->state_text); |
| 387 | c2->connect_duration = c1->connect_duration.sec; |
| 388 | c2->total_duration = c1->total_duration.sec; |
| 389 | c2->cause = c1->cause; |
| 390 | Cp(c2->cause_text, &c1->cause_text); |
| 391 | c2->has_media = c1->has_media; |
| 392 | c2->conf_slot = c1->conf_slot; |
| 393 | } |
| 394 | |
| 395 | static void accinfo2AccInfo(pjsua_acc_info *info1, Pjsua_Acc_Info *info2) |
| 396 | { |
| 397 | pj_memset(info2, 0, sizeof(Pjsua_Acc_Info)); |
| 398 | |
| 399 | info2->index = info1->index; |
| 400 | Cp(info2->acc_id, &info1->acc_id); |
| 401 | info2->has_registration = info1->has_registration; |
| 402 | info2->expires = info1->expires; |
| 403 | info2->status_code = info1->status; |
| 404 | Cp(info2->status_text, &info1->status_text); |
| 405 | info2->online_status = info1->online_status; |
| 406 | } |
| 407 | |
| 408 | static void buddyinfo2BuddyInfo(pjsua_buddy_info *info1, Pjsua_Buddy_Info *info2) |
| 409 | { |
| 410 | pj_memset(info2, 0, sizeof(Pjsua_Buddy_Info)); |
| 411 | |
| 412 | info2->index = info1->index; |
| 413 | info2->is_valid = info1->is_valid; |
| 414 | Cp(info2->name, &info1->name); |
| 415 | Cp(info2->display, &info1->display_name); |
| 416 | Cp(info2->host, &info1->host); |
| 417 | info2->port = info1->port; |
| 418 | Cp(info2->uri, &info1->uri); |
| 419 | info2->status = (Pjsua_Buddy_State)info1->status; |
| 420 | Cp(info2->status_text, &info1->status_text); |
| 421 | info2->monitor = info1->monitor; |
| 422 | info2->acc_index = info1->acc_index; |
| 423 | } |
| 424 | |
| 425 | static CApp *CApp_Instance; |
| 426 | |
| 427 | CApp::CApp() |
| 428 | { |
| 429 | CApp_Instance = this; |
| 430 | } |
| 431 | |
| 432 | STDMETHODIMP CApp::app_create(Pj_Status *ret) |
| 433 | { |
| 434 | *ret = pjsua_create(); |
| 435 | return S_OK; |
| 436 | } |
| 437 | |
| 438 | STDMETHODIMP CApp::app_default_config(Pjsua_Config *pConfig) |
| 439 | { |
| 440 | pjsua_config cfg; |
| 441 | pjsua_default_config(&cfg); |
| 442 | return config2Config(&cfg, pConfig); |
| 443 | } |
| 444 | |
| 445 | STDMETHODIMP CApp::app_test_config(Pjsua_Config *pConfig, BSTR *retmsg) |
| 446 | { |
| 447 | pjsua_config cfg; |
| 448 | HRESULT hr; |
| 449 | Temp_Pool tp; |
| 450 | char errmsg[PJ_ERR_MSG_SIZE]; |
| 451 | |
| 452 | hr = Config2config(tp.get_pool(), pConfig, &cfg); |
| 453 | if (FAILED(hr)) |
| 454 | return hr; |
| 455 | |
| 456 | pjsua_test_config(&cfg, errmsg, sizeof(errmsg)); |
| 457 | *retmsg = str2bstr(errmsg, strlen(errmsg)); |
| 458 | return S_OK; |
| 459 | } |
| 460 | |
| 461 | static void on_call_state(int call_index, pjsip_event *e) |
| 462 | { |
| 463 | pjsua_call_info call_info; |
| 464 | Pjsua_Call_Info *Call_Info = new Pjsua_Call_Info; |
| 465 | |
| 466 | pjsua_get_call_info(call_index, &call_info); |
| 467 | callinfo2CallInfo(&call_info, Call_Info); |
| 468 | |
| 469 | CApp_Instance->Fire_OnCallState(call_index, Call_Info); |
| 470 | } |
| 471 | |
| 472 | static void on_reg_state(int acc_index) |
| 473 | { |
| 474 | CApp_Instance->Fire_OnRegState(acc_index); |
| 475 | } |
| 476 | |
| 477 | static void on_buddy_state(int buddy_index) |
| 478 | { |
| 479 | CApp_Instance->Fire_OnBuddyState(buddy_index); |
| 480 | } |
| 481 | |
| 482 | static void on_pager(int call_index, const pj_str_t *from, |
| 483 | const pj_str_t *to, const pj_str_t *txt) |
| 484 | { |
| 485 | BSTR fromURI, toURI, imText; |
| 486 | |
| 487 | Cp2(&fromURI, from); |
| 488 | Cp2(&toURI, to); |
| 489 | Cp2(&imText, txt); |
| 490 | |
| 491 | CApp_Instance->Fire_OnIncomingPager(call_index, fromURI, toURI, imText); |
| 492 | } |
| 493 | |
| 494 | static void on_typing(int call_index, const pj_str_t *from, |
| 495 | const pj_str_t *to, pj_bool_t is_typing) |
| 496 | { |
| 497 | BSTR fromURI, toURI; |
| 498 | |
| 499 | Cp2(&fromURI, from); |
| 500 | Cp2(&toURI, to); |
| 501 | |
| 502 | CApp_Instance->Fire_OnTypingIndication(call_index, fromURI, toURI, is_typing); |
| 503 | } |
| 504 | |
| 505 | |
| 506 | STDMETHODIMP CApp::app_init(Pjsua_Config *pConfig, Pj_Status *pStatus) |
| 507 | { |
| 508 | pjsua_config cfg; |
| 509 | pjsua_callback cb; |
| 510 | Temp_Pool tp; |
| 511 | HRESULT hr; |
| 512 | |
| 513 | pj_memset(&cb, 0, sizeof(cb)); |
| 514 | cb.on_call_state = &on_call_state; |
| 515 | cb.on_reg_state = &on_reg_state; |
| 516 | cb.on_buddy_state = &on_buddy_state; |
| 517 | cb.on_pager = &on_pager; |
| 518 | cb.on_typing = &on_typing; |
| 519 | |
| 520 | hr = Config2config(tp.get_pool(), pConfig, &cfg); |
| 521 | if (FAILED(hr)) |
| 522 | return hr; |
| 523 | |
| 524 | *pStatus = pjsua_init(&cfg, &cb); |
| 525 | return S_OK; |
| 526 | } |
| 527 | |
| 528 | STDMETHODIMP CApp::app_start(Pj_Status *retStatus) |
| 529 | { |
| 530 | *retStatus = pjsua_start(); |
| 531 | return S_OK; |
| 532 | } |
| 533 | |
| 534 | STDMETHODIMP CApp::app_destroy(Pj_Status *retStatus) |
| 535 | { |
| 536 | *retStatus = pjsua_destroy(); |
| 537 | return S_OK; |
| 538 | } |
| 539 | |
| 540 | STDMETHODIMP CApp::call_get_max_count(int *retCount) |
| 541 | { |
| 542 | *retCount = pjsua_get_max_calls(); |
| 543 | return S_OK; |
| 544 | } |
| 545 | |
| 546 | STDMETHODIMP CApp::call_get_count(int *retCount) |
| 547 | { |
| 548 | *retCount = pjsua_get_call_count(); |
| 549 | return S_OK; |
| 550 | } |
| 551 | |
| 552 | STDMETHODIMP CApp::call_is_active(int call_index, Pj_Bool *retVal) |
| 553 | { |
| 554 | *retVal = pjsua_call_is_active(call_index); |
| 555 | return S_OK; |
| 556 | } |
| 557 | |
| 558 | STDMETHODIMP CApp::call_has_media(int call_index, Pj_Bool *pRet) |
| 559 | { |
| 560 | *pRet = pjsua_call_has_media(call_index); |
| 561 | return S_OK; |
| 562 | } |
| 563 | |
| 564 | STDMETHODIMP CApp::call_get_info(int call_index, Pjsua_Call_Info *pInfo, Pj_Status *pRet) |
| 565 | { |
| 566 | pjsua_call_info info; |
| 567 | *pRet = pjsua_get_call_info(call_index, &info); |
| 568 | callinfo2CallInfo(&info, pInfo); |
| 569 | return S_OK; |
| 570 | } |
| 571 | |
| 572 | STDMETHODIMP CApp::call_make_call(int acc_index, Pj_String dst_uri, int *call_index, Pj_Status *pRet) |
| 573 | { |
| 574 | Temp_Pool tp; |
| 575 | pj_str_t tmp = Pj_str(tp.get_pool(), dst_uri); |
| 576 | |
| 577 | *pRet = pjsua_make_call(acc_index, &tmp, call_index); |
| 578 | return S_OK; |
| 579 | } |
| 580 | |
| 581 | STDMETHODIMP CApp::call_answer(int call_index, int status_code, Pj_Status *pRet) |
| 582 | { |
| 583 | pjsua_call_answer(call_index, status_code); |
| 584 | *pRet = PJ_SUCCESS; |
| 585 | return S_OK; |
| 586 | } |
| 587 | |
| 588 | STDMETHODIMP CApp::call_hangup(int call_index, Pj_Status *pRet) |
| 589 | { |
| 590 | pjsua_call_hangup(call_index); |
| 591 | *pRet = PJ_SUCCESS; |
| 592 | return S_OK; |
| 593 | } |
| 594 | |
| 595 | STDMETHODIMP CApp::call_set_hold(int call_index, Pj_Status *pRet) |
| 596 | { |
| 597 | pjsua_call_set_hold(call_index); |
| 598 | *pRet = PJ_SUCCESS; |
| 599 | return S_OK; |
| 600 | } |
| 601 | |
| 602 | STDMETHODIMP CApp::call_release_hold(int call_index, Pj_Status *pRet) |
| 603 | { |
| 604 | pjsua_call_reinvite(call_index); |
| 605 | *pRet = PJ_SUCCESS; |
| 606 | return S_OK; |
| 607 | } |
| 608 | |
| 609 | STDMETHODIMP CApp::call_xfer(int call_index, Pj_String dst_uri, Pj_Status *pRet) |
| 610 | { |
| 611 | Temp_Pool tp; |
| 612 | pj_str_t tmp = Pj_str(tp.get_pool(), dst_uri); |
| 613 | pjsua_call_xfer(call_index, &tmp); |
| 614 | *pRet = PJ_SUCCESS; |
| 615 | return S_OK; |
| 616 | } |
| 617 | |
| 618 | STDMETHODIMP CApp::call_dial_dtmf(int call_index, Pj_String digits, Pj_Status *pRet) |
| 619 | { |
| 620 | Temp_Pool tp; |
| 621 | pj_str_t tmp = Pj_str(tp.get_pool(), digits); |
| 622 | *pRet = pjsua_call_dial_dtmf(call_index, &tmp); |
| 623 | return S_OK; |
| 624 | } |
| 625 | |
| 626 | STDMETHODIMP CApp::call_send_im(int call_index, Pj_String text, Pj_Status *pRet) |
| 627 | { |
| 628 | Temp_Pool tp; |
| 629 | pj_str_t tmp = Pj_str(tp.get_pool(), text); |
| 630 | pjsua_call_send_im(call_index, &tmp); |
| 631 | *pRet = PJ_SUCCESS; |
| 632 | return S_OK; |
| 633 | } |
| 634 | |
| 635 | STDMETHODIMP CApp::call_typing(int call_index, int is_typing, Pj_Status *pRet) |
| 636 | { |
| 637 | pjsua_call_typing(call_index, is_typing); |
| 638 | *pRet = PJ_SUCCESS; |
| 639 | return S_OK; |
| 640 | } |
| 641 | |
| 642 | STDMETHODIMP CApp::call_hangup_all() |
| 643 | { |
| 644 | pjsua_call_hangup_all(); |
| 645 | return S_OK; |
| 646 | } |
| 647 | |
| 648 | STDMETHODIMP CApp::call_get_textstat(int call_index, BSTR *textstat) |
| 649 | { |
| 650 | char buf[1024]; |
| 651 | pjsua_dump_call(call_index, 1, buf, sizeof(buf), ""); |
| 652 | |
| 653 | OLECHAR wbuf[1024]; |
| 654 | pj_ansi_to_unicode(buf, strlen(buf), wbuf, PJ_ARRAY_SIZE(wbuf)); |
| 655 | *textstat = SysAllocString(wbuf); |
| 656 | return S_OK; |
| 657 | } |
| 658 | |
| 659 | |
| 660 | STDMETHODIMP CApp::acc_get_count(int *pCount) |
| 661 | { |
| 662 | *pCount = pjsua_get_acc_count(); |
| 663 | return S_OK; |
| 664 | } |
| 665 | |
| 666 | STDMETHODIMP CApp::acc_get_info(int acc_index, Pjsua_Acc_Info *pInfo, Pj_Status *pRet) |
| 667 | { |
| 668 | pjsua_acc_info info; |
| 669 | *pRet = pjsua_acc_get_info(acc_index, &info); |
| 670 | accinfo2AccInfo(&info, pInfo); |
| 671 | return S_OK; |
| 672 | } |
| 673 | |
| 674 | STDMETHODIMP CApp::acc_add(Pjsua_Acc_Config *pConfig, int *pAcc_Index, Pj_Status *pRet) |
| 675 | { |
| 676 | Temp_Pool tp; |
| 677 | pjsua_acc_config config; |
| 678 | AccConfig2accconfig(tp.get_pool(), pConfig, &config); |
| 679 | *pRet = pjsua_acc_add(&config, pAcc_Index); |
| 680 | return S_OK; |
| 681 | } |
| 682 | |
| 683 | STDMETHODIMP CApp::acc_set_online_status(int acc_index, int is_online, Pj_Status *pRet) |
| 684 | { |
| 685 | *pRet = pjsua_acc_set_online_status(acc_index, is_online); |
| 686 | return S_OK; |
| 687 | } |
| 688 | |
| 689 | STDMETHODIMP CApp::acc_set_registration(int acc_index, int reg_active, Pj_Status *pRet) |
| 690 | { |
| 691 | *pRet = pjsua_acc_set_registration(acc_index, reg_active); |
| 692 | return S_OK; |
| 693 | } |
| 694 | |
| 695 | STDMETHODIMP CApp::buddy_get_count(int *pCount) |
| 696 | { |
| 697 | *pCount = pjsua_get_buddy_count(); |
| 698 | return S_OK; |
| 699 | } |
| 700 | |
| 701 | STDMETHODIMP CApp::buddy_get_info(int buddy_index, Pjsua_Buddy_Info *pInfo, Pj_Status *pRet) |
| 702 | { |
| 703 | pjsua_buddy_info info; |
| 704 | *pRet = pjsua_buddy_get_info(buddy_index, &info); |
| 705 | buddyinfo2BuddyInfo(&info, pInfo); |
| 706 | return S_OK; |
| 707 | } |
| 708 | |
| 709 | STDMETHODIMP CApp::buddy_add(Pj_String uri, int *pBuddy_Index, Pj_Status *pRet) |
| 710 | { |
| 711 | Temp_Pool tp; |
| 712 | pj_str_t tmp = Pj_str(tp.get_pool(), uri); |
| 713 | *pRet = pjsua_buddy_add(&tmp, pBuddy_Index); |
| 714 | return S_OK; |
| 715 | } |
| 716 | |
| 717 | STDMETHODIMP CApp::buddy_subscribe_pres(int buddy_index, int subscribe, Pj_Status *pRet) |
| 718 | { |
| 719 | *pRet = pjsua_buddy_subscribe_pres(buddy_index, subscribe); |
| 720 | pjsua_pres_refresh(); |
| 721 | return S_OK; |
| 722 | } |
| 723 | |
| 724 | STDMETHODIMP CApp::im_send_text(int acc_index, Pj_String dst_uri, Pj_String text, Pj_Status *pRet) |
| 725 | { |
| 726 | Temp_Pool tp; |
| 727 | pj_str_t tmp_uri = Pj_str(tp.get_pool(), dst_uri); |
| 728 | pj_str_t tmp_text = Pj_str(tp.get_pool(), text); |
| 729 | *pRet = pjsua_im_send(acc_index, &tmp_uri, &tmp_text); |
| 730 | return S_OK; |
| 731 | } |
| 732 | |
| 733 | STDMETHODIMP CApp::im_typing(int acc_index, Pj_URI dst_uri, int is_typing, Pj_Status *pRet) |
| 734 | { |
| 735 | Temp_Pool tp; |
| 736 | pj_str_t tmp_uri = Pj_str(tp.get_pool(), dst_uri); |
| 737 | *pRet = pjsua_im_typing(acc_index, &tmp_uri, is_typing); |
| 738 | return S_OK; |
| 739 | } |
| 740 | |
| 741 | STDMETHODIMP CApp::conf_connect(int src_port, int sink_port, Pj_Status *pRet) |
| 742 | { |
| 743 | *pRet = pjsua_conf_connect(src_port, sink_port); |
| 744 | return S_OK; |
| 745 | } |
| 746 | |
| 747 | STDMETHODIMP CApp::conf_disconnect(int src_port, int sink_port, Pj_Status *pRet) |
| 748 | { |
| 749 | *pRet = pjsua_conf_disconnect(src_port, sink_port); |
| 750 | return S_OK; |
| 751 | } |
| 752 | |
| 753 | STDMETHODIMP CApp::player_create(Pj_String filename, int *pPlayer_Id, Pj_Status *pRet) |
| 754 | { |
| 755 | Temp_Pool tp; |
| 756 | pj_str_t tmp = Pj_str(tp.get_pool(), filename); |
| 757 | *pRet = pjsua_player_create(&tmp, pPlayer_Id); |
| 758 | return S_OK; |
| 759 | } |
| 760 | |
| 761 | STDMETHODIMP CApp::player_get_conf_port(int player_id, int *pPort) |
| 762 | { |
| 763 | *pPort = pjsua_player_get_conf_port(player_id); |
| 764 | return S_OK; |
| 765 | } |
| 766 | |
| 767 | STDMETHODIMP CApp::player_set_pos(int player_id, int pos, Pj_Status *pRet) |
| 768 | { |
| 769 | *pRet = pjsua_player_set_pos(player_id, pos); |
| 770 | return S_OK; |
| 771 | } |
| 772 | |
| 773 | STDMETHODIMP CApp::player_destroy(int player_id, Pj_Status *pRet) |
| 774 | { |
| 775 | *pRet = pjsua_player_destroy(player_id); |
| 776 | return S_OK; |
| 777 | } |
| 778 | |
| 779 | STDMETHODIMP CApp::recorder_create(Pj_String filename, int *pRecorder_Id, Pj_Status *pRet) |
| 780 | { |
| 781 | Temp_Pool tp; |
| 782 | pj_str_t tmp = Pj_str(tp.get_pool(), filename); |
| 783 | *pRet = pjsua_recorder_create(&tmp, pRecorder_Id); |
| 784 | return S_OK; |
| 785 | } |
| 786 | |
| 787 | STDMETHODIMP CApp::recorder_get_conf_port(int recorder_id, int *pPort) |
| 788 | { |
| 789 | *pPort = pjsua_recorder_get_conf_port(recorder_id); |
| 790 | return S_OK; |
| 791 | } |
| 792 | |
| 793 | STDMETHODIMP CApp::recorder_destroy(int recorder_id, Pj_Status *pRet) |
| 794 | { |
| 795 | *pRet = pjsua_recorder_destroy(recorder_id); |
| 796 | return S_OK; |
| 797 | } |
| 798 | |
| 799 | STDMETHODIMP CApp::app_load_config(Pj_String filename, Pjsua_Config *pConfig, Pj_Status *pRet) |
| 800 | { |
| 801 | pjsua_config config; |
| 802 | Temp_Pool tp; |
| 803 | pj_str_t tmp = Pj_str(tp.get_pool(), filename); |
| 804 | pjsua_default_config(&config); |
| 805 | *pRet = pjsua_load_settings(tmp.ptr, &config); |
| 806 | if (*pRet == PJ_SUCCESS) |
| 807 | *pRet = config2Config(&config, pConfig); |
| 808 | return S_OK; |
| 809 | } |
| 810 | |
| 811 | STDMETHODIMP CApp::app_save_config(Pj_String filename, Pjsua_Config *pConfig, Pj_Status *pRet) |
| 812 | { |
| 813 | Temp_Pool tp; |
| 814 | pjsua_config config; |
| 815 | pj_str_t tmp = Pj_str(tp.get_pool(), filename); |
| 816 | HRESULT hr; |
| 817 | |
| 818 | hr = Config2config(tp.get_pool(), pConfig, &config); |
| 819 | if (FAILED(hr)) |
| 820 | return hr; |
| 821 | |
| 822 | *pRet = pjsua_save_settings(tmp.ptr, &config); |
| 823 | return S_OK; |
| 824 | } |
| 825 | |
| 826 | STDMETHODIMP CApp::app_get_current_config(Pjsua_Config *pConfig) |
| 827 | { |
| 828 | pjsua_config *config; |
| 829 | config = (pjsua_config*) pjsua_get_config(); |
| 830 | return config2Config(config, pConfig); |
| 831 | } |
| 832 | |
| 833 | STDMETHODIMP CApp::app_get_error_msg(Pj_Status status, BSTR * pRet) |
| 834 | { |
| 835 | char errmsg[PJ_ERR_MSG_SIZE]; |
| 836 | OLECHAR werrmsg[PJ_ERR_MSG_SIZE]; |
| 837 | pj_strerror(status, errmsg, sizeof(errmsg)); |
| 838 | pj_ansi_to_unicode(errmsg, strlen(errmsg), werrmsg, PJ_ARRAY_SIZE(werrmsg)); |
| 839 | *pRet = SysAllocString(werrmsg); |
| 840 | return S_OK; |
| 841 | } |
| 842 | |
| 843 | STDMETHODIMP CApp::app_verify_sip_url(Pj_String uri, Pj_Status *pRet) |
| 844 | { |
| 845 | Temp_Pool tp; |
| 846 | pj_str_t tmp = Pj_str(tp.get_pool(), uri); |
| 847 | *pRet = pjsua_verify_sip_url(tmp.ptr); |
| 848 | return S_OK; |
| 849 | } |
| 850 | |
| 851 | STDMETHODIMP CApp::app_handle_events(int msec_timeout, int *pEvCount) |
| 852 | { |
| 853 | if (msec_timeout < 0) |
| 854 | msec_timeout = 0; |
| 855 | |
| 856 | *pEvCount = pjsua_handle_events(msec_timeout); |
| 857 | return S_OK; |
| 858 | } |