| 1 | /* |
| 2 | * Copyright (C) 2008-2013 Teluu Inc. (http://www.teluu.com) |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | */ |
| 18 | #include <pjsua2.hpp> |
| 19 | #include <iostream> |
| 20 | #include <pj/file_access.h> |
| 21 | |
| 22 | #define THIS_FILE "pjsua2_demo.cpp" |
| 23 | |
| 24 | using namespace pj; |
| 25 | |
| 26 | /* Valid test number: |
| 27 | * 0: JSON account config test |
| 28 | * 1: call test |
| 29 | * 2: JSON endpoint config test |
| 30 | * 3: media player and recorder test |
| 31 | * 4: simple registration test |
| 32 | */ |
| 33 | #define USE_TEST 1 |
| 34 | |
| 35 | class MyEndpoint : public Endpoint |
| 36 | { |
| 37 | public: |
| 38 | MyEndpoint() : Endpoint() {}; |
| 39 | virtual pj_status_t onCredAuth(OnCredAuthParam &prm) |
| 40 | { |
| 41 | PJ_UNUSED_ARG(prm); |
| 42 | std::cout << "*** Callback onCredAuth called ***" << std::endl; |
| 43 | /* Return PJ_ENOTSUP to use |
| 44 | * pjsip_auth_create_aka_response()/<b>libmilenage</b> (default), |
| 45 | * if PJSIP_HAS_DIGEST_AKA_AUTH is defined. |
| 46 | */ |
| 47 | return PJ_ENOTSUP; |
| 48 | } |
| 49 | }; |
| 50 | |
| 51 | class MyAccount; |
| 52 | |
| 53 | class MyAudioMediaPort: public AudioMediaPort |
| 54 | { |
| 55 | virtual void onFrameRequested(MediaFrame &frame) |
| 56 | { |
| 57 | // Give the input frame here |
| 58 | frame.type = PJMEDIA_FRAME_TYPE_AUDIO; |
| 59 | // frame.buf.assign(frame.size, 'c'); |
| 60 | } |
| 61 | |
| 62 | virtual void onFrameReceived(MediaFrame &frame) |
| 63 | { |
| 64 | PJ_UNUSED_ARG(frame); |
| 65 | // Process the incoming frame here |
| 66 | } |
| 67 | }; |
| 68 | |
| 69 | |
| 70 | class MyCall : public Call |
| 71 | { |
| 72 | private: |
| 73 | MyAccount *myAcc; |
| 74 | AudioMediaPlayer *wav_player; |
| 75 | AudioMediaPort *med_port; |
| 76 | |
| 77 | public: |
| 78 | MyCall(Account &acc, int call_id = PJSUA_INVALID_ID) |
| 79 | : Call(acc, call_id) |
| 80 | { |
| 81 | wav_player = NULL; |
| 82 | med_port = NULL; |
| 83 | myAcc = (MyAccount *)&acc; |
| 84 | } |
| 85 | |
| 86 | ~MyCall() |
| 87 | { |
| 88 | if (wav_player) |
| 89 | delete wav_player; |
| 90 | if (med_port) |
| 91 | delete med_port; |
| 92 | } |
| 93 | |
| 94 | virtual void onCallState(OnCallStateParam &prm); |
| 95 | virtual void onCallTransferRequest(OnCallTransferRequestParam &prm); |
| 96 | virtual void onCallReplaceRequest(OnCallReplaceRequestParam &prm); |
| 97 | virtual void onCallMediaState(OnCallMediaStateParam &prm); |
| 98 | |
| 99 | virtual void onCallRxText(OnCallRxTextParam &prm); |
| 100 | }; |
| 101 | |
| 102 | class MyAccount : public Account |
| 103 | { |
| 104 | public: |
| 105 | std::vector<Call *> calls; |
| 106 | |
| 107 | public: |
| 108 | MyAccount() |
| 109 | {} |
| 110 | |
| 111 | ~MyAccount() |
| 112 | { |
| 113 | std::cout << "*** Account is being deleted: No of calls=" |
| 114 | << calls.size() << std::endl; |
| 115 | |
| 116 | for (std::vector<Call *>::iterator it = calls.begin(); |
| 117 | it != calls.end(); ) |
| 118 | { |
| 119 | delete (*it); |
| 120 | it = calls.erase(it); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | void removeCall(Call *call) |
| 125 | { |
| 126 | for (std::vector<Call *>::iterator it = calls.begin(); |
| 127 | it != calls.end(); ++it) |
| 128 | { |
| 129 | if (*it == call) { |
| 130 | calls.erase(it); |
| 131 | break; |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | virtual void onRegState(OnRegStateParam &prm) |
| 137 | { |
| 138 | AccountInfo ai = getInfo(); |
| 139 | std::cout << (ai.regIsActive? "*** Register: code=" : "*** Unregister: code=") |
| 140 | << prm.code << std::endl; |
| 141 | } |
| 142 | |
| 143 | virtual void onIncomingCall(OnIncomingCallParam &iprm) |
| 144 | { |
| 145 | Call *call = new MyCall(*this, iprm.callId); |
| 146 | CallInfo ci = call->getInfo(); |
| 147 | CallOpParam prm; |
| 148 | |
| 149 | std::cout << "*** Incoming Call: " << ci.remoteUri << " [" |
| 150 | << ci.stateText << "]" << std::endl; |
| 151 | |
| 152 | calls.push_back(call); |
| 153 | prm.statusCode = (pjsip_status_code)200; |
| 154 | call->answer(prm); |
| 155 | } |
| 156 | }; |
| 157 | |
| 158 | void MyCall::onCallState(OnCallStateParam &prm) |
| 159 | { |
| 160 | PJ_UNUSED_ARG(prm); |
| 161 | |
| 162 | CallInfo ci = getInfo(); |
| 163 | std::cout << "*** Call: " << ci.remoteUri << " [" << ci.stateText |
| 164 | << "]" << std::endl; |
| 165 | |
| 166 | if (ci.state == PJSIP_INV_STATE_DISCONNECTED) { |
| 167 | //myAcc->removeCall(this); |
| 168 | /* Delete the call */ |
| 169 | //delete this; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | void MyCall::onCallMediaState(OnCallMediaStateParam &prm) |
| 174 | { |
| 175 | PJ_UNUSED_ARG(prm); |
| 176 | |
| 177 | CallInfo ci = getInfo(); |
| 178 | AudioMedia aud_med; |
| 179 | AudioMedia& play_dev_med = |
| 180 | MyEndpoint::instance().audDevManager().getPlaybackDevMedia(); |
| 181 | |
| 182 | std::cout << "Getting playback dev media" << std::endl; |
| 183 | |
| 184 | try { |
| 185 | // Get the first audio media |
| 186 | aud_med = getAudioMedia(-1); |
| 187 | } catch(...) { |
| 188 | std::cout << "Failed to get audio media" << std::endl; |
| 189 | return; |
| 190 | } |
| 191 | |
| 192 | if (!wav_player) { |
| 193 | wav_player = new AudioMediaPlayer(); |
| 194 | try { |
| 195 | wav_player->createPlayer( |
| 196 | "../../../../tests/pjsua/wavs/input.16.wav", 0); |
| 197 | } catch (...) { |
| 198 | std::cout << "Failed opening wav file" << std::endl; |
| 199 | delete wav_player; |
| 200 | wav_player = NULL; |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | // This will connect the wav file to the call audio media |
| 205 | if (wav_player) |
| 206 | wav_player->startTransmit(aud_med); |
| 207 | |
| 208 | if (!med_port) { |
| 209 | med_port = new MyAudioMediaPort(); |
| 210 | |
| 211 | MediaFormatAudio fmt; |
| 212 | fmt.init(PJMEDIA_FORMAT_PCM, 16000, 1, 20000, 16); |
| 213 | |
| 214 | med_port->createPort("med_port", fmt); |
| 215 | |
| 216 | // Connect the media port to the call audio media in both directions |
| 217 | med_port->startTransmit(aud_med); |
| 218 | aud_med.startTransmit(*med_port); |
| 219 | } |
| 220 | |
| 221 | // And this will connect the call audio media to the sound device/speaker |
| 222 | aud_med.startTransmit(play_dev_med); |
| 223 | } |
| 224 | |
| 225 | void MyCall::onCallTransferRequest(OnCallTransferRequestParam &prm) |
| 226 | { |
| 227 | /* Create new Call for call transfer */ |
| 228 | prm.newCall = new MyCall(*myAcc); |
| 229 | } |
| 230 | |
| 231 | void MyCall::onCallReplaceRequest(OnCallReplaceRequestParam &prm) |
| 232 | { |
| 233 | /* Create new Call for call replace */ |
| 234 | prm.newCall = new MyCall(*myAcc); |
| 235 | } |
| 236 | |
| 237 | void MyCall::onCallRxText(OnCallRxTextParam &prm) |
| 238 | { |
| 239 | if (prm.text.empty()) { |
| 240 | std::cout << "Received empty T140 block with seq " << prm.seq; |
| 241 | std::cout << std::endl; |
| 242 | } else { |
| 243 | std::cout << "Incoming text with seq " << prm.seq << ": " << prm.text; |
| 244 | std::cout << std::endl; |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | |
| 249 | #if USE_TEST == 1 |
| 250 | static void mainProg1(MyEndpoint &ep) |
| 251 | { |
| 252 | // Init library |
| 253 | EpConfig ep_cfg; |
| 254 | ep_cfg.logConfig.level = 4; |
| 255 | ep.libInit( ep_cfg ); |
| 256 | |
| 257 | // Transport |
| 258 | TransportConfig tcfg; |
| 259 | tcfg.port = 6000; |
| 260 | ep.transportCreate(PJSIP_TRANSPORT_UDP, tcfg); |
| 261 | |
| 262 | // Start library |
| 263 | ep.libStart(); |
| 264 | std::cout << "*** PJSUA2 STARTED ***" << std::endl; |
| 265 | |
| 266 | // Add account |
| 267 | AccountConfig acc_cfg; |
| 268 | acc_cfg.idUri = "sip:[email protected]"; |
| 269 | acc_cfg.regConfig.registrarUri = "sip:sip.pjsip.org"; |
| 270 | |
| 271 | #if PJSIP_HAS_DIGEST_AKA_AUTH |
| 272 | AuthCredInfo aci("Digest", "*", "test", PJSIP_CRED_DATA_EXT_AKA | PJSIP_CRED_DATA_PLAIN_PASSWD, "passwd"); |
| 273 | aci.akaK = "passwd"; |
| 274 | #else |
| 275 | AuthCredInfo aci("digest", "*", "test", 0, "test"); |
| 276 | #endif |
| 277 | |
| 278 | acc_cfg.sipConfig.authCreds.push_back(PJSUA2_MOVE(aci)); |
| 279 | MyAccount *acc(new MyAccount); |
| 280 | try { |
| 281 | acc->create(acc_cfg); |
| 282 | } catch (...) { |
| 283 | std::cout << "Adding account failed" << std::endl; |
| 284 | } |
| 285 | |
| 286 | pj_thread_sleep(2000); |
| 287 | |
| 288 | // Make outgoing call |
| 289 | Call *call = new MyCall(*acc); |
| 290 | acc->calls.push_back(call); |
| 291 | CallOpParam prm(true); |
| 292 | prm.opt.audioCount = 1; |
| 293 | prm.opt.videoCount = 0; |
| 294 | prm.opt.textCount = 1; |
| 295 | call->makeCall("sip:[email protected]", prm); |
| 296 | |
| 297 | pj_thread_sleep(1000); |
| 298 | CallSendTextParam param; |
| 299 | param.text = "PJSIP"; |
| 300 | call->sendText(param); |
| 301 | |
| 302 | // Hangup all calls |
| 303 | pj_thread_sleep(4000); |
| 304 | ep.hangupAllCalls(); |
| 305 | pj_thread_sleep(4000); |
| 306 | |
| 307 | // Destroy library |
| 308 | std::cout << "*** PJSUA2 SHUTTING DOWN ***" << std::endl; |
| 309 | delete acc; /* Will delete all calls too */ |
| 310 | } |
| 311 | #endif |
| 312 | |
| 313 | |
| 314 | #if USE_TEST == 2 |
| 315 | static void mainProg2() |
| 316 | { |
| 317 | string json_str; |
| 318 | { |
| 319 | EpConfig epCfg; |
| 320 | JsonDocument jDoc; |
| 321 | |
| 322 | epCfg.uaConfig.maxCalls = 61; |
| 323 | epCfg.uaConfig.userAgent = "Just JSON Test"; |
| 324 | epCfg.uaConfig.stunServer.push_back("stun1.pjsip.org"); |
| 325 | epCfg.uaConfig.stunServer.push_back("stun2.pjsip.org"); |
| 326 | epCfg.logConfig.filename = "THE.LOG"; |
| 327 | |
| 328 | jDoc.writeObject(epCfg); |
| 329 | json_str = jDoc.saveString(); |
| 330 | std::cout << json_str << std::endl << std::endl; |
| 331 | } |
| 332 | |
| 333 | { |
| 334 | EpConfig epCfg; |
| 335 | JsonDocument rDoc; |
| 336 | string output; |
| 337 | |
| 338 | rDoc.loadString(json_str); |
| 339 | rDoc.readObject(epCfg); |
| 340 | |
| 341 | JsonDocument wDoc; |
| 342 | |
| 343 | wDoc.writeObject(epCfg); |
| 344 | json_str = wDoc.saveString(); |
| 345 | std::cout << json_str << std::endl << std::endl; |
| 346 | |
| 347 | wDoc.saveFile("jsontest.js"); |
| 348 | } |
| 349 | |
| 350 | { |
| 351 | EpConfig epCfg; |
| 352 | JsonDocument rDoc; |
| 353 | |
| 354 | rDoc.loadFile("jsontest.js"); |
| 355 | rDoc.readObject(epCfg); |
| 356 | pj_file_delete("jsontest.js"); |
| 357 | } |
| 358 | } |
| 359 | #endif |
| 360 | |
| 361 | |
| 362 | #if USE_TEST == 3 |
| 363 | static void mainProg3(MyEndpoint &ep) |
| 364 | { |
| 365 | const char *paths[] = { "../../../../tests/pjsua/wavs/input.16.wav", |
| 366 | "../../tests/pjsua/wavs/input.16.wav", |
| 367 | "input.16.wav"}; |
| 368 | unsigned i; |
| 369 | const char *filename = NULL; |
| 370 | |
| 371 | // Init library |
| 372 | EpConfig ep_cfg; |
| 373 | ep.libInit( ep_cfg ); |
| 374 | |
| 375 | for (i=0; i<PJ_ARRAY_SIZE(paths); ++i) { |
| 376 | if (pj_file_exists(paths[i])) { |
| 377 | filename = paths[i]; |
| 378 | break; |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | if (!filename) { |
| 383 | PJSUA2_RAISE_ERROR3(PJ_ENOTFOUND, "mainProg3()", |
| 384 | "Could not locate input.16.wav"); |
| 385 | } |
| 386 | |
| 387 | // Start library |
| 388 | ep.libStart(); |
| 389 | std::cout << "*** PJSUA2 STARTED ***" << std::endl; |
| 390 | |
| 391 | /* Use Null Audio Device as main media clock. This is useful for improving |
| 392 | * media clock (see also https://trac.pjsip.org/repos/wiki/FAQ#tx-timing) |
| 393 | * especially when sound device clock is jittery. |
| 394 | */ |
| 395 | ep.audDevManager().setNullDev(); |
| 396 | |
| 397 | /* And install sound device using Extra Audio Device */ |
| 398 | ExtraAudioDevice auddev2(-1, -1); |
| 399 | try { |
| 400 | auddev2.open(); |
| 401 | } catch (...) { |
| 402 | std::cout << "Extra sound device failed" << std::endl; |
| 403 | } |
| 404 | |
| 405 | // Create player and recorder |
| 406 | { |
| 407 | AudioMediaPlayer amp; |
| 408 | amp.createPlayer(filename); |
| 409 | |
| 410 | AudioMediaRecorder amr; |
| 411 | amr.createRecorder("recorder_test_output.wav"); |
| 412 | |
| 413 | amp.startTransmit(amr); |
| 414 | if (auddev2.isOpened()) |
| 415 | amp.startTransmit(auddev2); |
| 416 | |
| 417 | pj_thread_sleep(5000); |
| 418 | } |
| 419 | } |
| 420 | #endif |
| 421 | |
| 422 | |
| 423 | #if USE_TEST == 0 |
| 424 | static void mainProg(MyEndpoint &) |
| 425 | { |
| 426 | string json_str; |
| 427 | |
| 428 | { |
| 429 | JsonDocument jdoc; |
| 430 | AccountConfig accCfg; |
| 431 | |
| 432 | accCfg.idUri = "\"Just Test\" <sip:[email protected]>"; |
| 433 | accCfg.regConfig.registrarUri = "sip:sip.pjsip.org"; |
| 434 | SipHeader h; |
| 435 | h.hName = "X-Header"; |
| 436 | h.hValue = "User header"; |
| 437 | accCfg.regConfig.headers.push_back(PJSUA2_MOVE(h)); |
| 438 | |
| 439 | accCfg.sipConfig.proxies.push_back("<sip:sip.pjsip.org;transport=tcp>"); |
| 440 | accCfg.sipConfig.proxies.push_back("<sip:sip.pjsip.org;transport=tls>"); |
| 441 | |
| 442 | accCfg.mediaConfig.transportConfig.tlsConfig.ciphers.push_back(1); |
| 443 | accCfg.mediaConfig.transportConfig.tlsConfig.ciphers.push_back(2); |
| 444 | accCfg.mediaConfig.transportConfig.tlsConfig.ciphers.push_back(3); |
| 445 | |
| 446 | AuthCredInfo aci; |
| 447 | aci.scheme = "digest"; |
| 448 | aci.username = "test"; |
| 449 | aci.data = "passwd"; |
| 450 | aci.realm = "*"; |
| 451 | aci.dataType = PJSIP_CRED_DATA_PLAIN_PASSWD; |
| 452 | #if PJSIP_HAS_DIGEST_AKA_AUTH |
| 453 | aci.dataType |= PJSIP_CRED_DATA_EXT_AKA; |
| 454 | aci.akaK = "key"; |
| 455 | #endif |
| 456 | accCfg.sipConfig.authCreds.push_back(PJSUA2_MOVE(aci)); |
| 457 | |
| 458 | jdoc.writeObject(accCfg); |
| 459 | json_str = jdoc.saveString(); |
| 460 | std::cout << "Original:" << std::endl; |
| 461 | std::cout << json_str << std::endl << std::endl; |
| 462 | } |
| 463 | |
| 464 | { |
| 465 | JsonDocument rdoc; |
| 466 | |
| 467 | rdoc.loadString(json_str); |
| 468 | AccountConfig accCfg; |
| 469 | rdoc.readObject(accCfg); |
| 470 | |
| 471 | JsonDocument wdoc; |
| 472 | wdoc.writeObject(accCfg); |
| 473 | json_str = wdoc.saveString(); |
| 474 | |
| 475 | std::cout << "Parsed:" << std::endl; |
| 476 | std::cout << json_str << std::endl << std::endl; |
| 477 | } |
| 478 | } |
| 479 | #endif |
| 480 | |
| 481 | |
| 482 | #if USE_TEST == 4 |
| 483 | static void mainProg4(MyEndpoint &ep) |
| 484 | { |
| 485 | // Init library |
| 486 | EpConfig ep_cfg; |
| 487 | ep.libInit( ep_cfg ); |
| 488 | |
| 489 | // Create transport |
| 490 | TransportConfig tcfg; |
| 491 | tcfg.port = 5060; |
| 492 | ep.transportCreate(PJSIP_TRANSPORT_UDP, tcfg); |
| 493 | ep.transportCreate(PJSIP_TRANSPORT_TCP, tcfg); |
| 494 | |
| 495 | // Add account |
| 496 | AccountConfig acc_cfg; |
| 497 | acc_cfg.idUri = "sip:localhost"; |
| 498 | MyAccount *acc(new MyAccount); |
| 499 | acc->create(acc_cfg); |
| 500 | |
| 501 | // Start library |
| 502 | ep.libStart(); |
| 503 | std::cout << "*** PJSUA2 STARTED ***" << std::endl; |
| 504 | |
| 505 | // Just wait for ENTER key |
| 506 | std::cout << "Press ENTER to quit..." << std::endl; |
| 507 | std::cin.get(); |
| 508 | |
| 509 | delete acc; |
| 510 | } |
| 511 | #endif |
| 512 | |
| 513 | |
| 514 | extern "C" |
| 515 | int main() |
| 516 | { |
| 517 | int ret = 0; |
| 518 | MyEndpoint ep; |
| 519 | |
| 520 | try { |
| 521 | ep.libCreate(); |
| 522 | |
| 523 | #if USE_TEST == 0 |
| 524 | mainProg(ep); |
| 525 | #endif |
| 526 | #if USE_TEST == 1 |
| 527 | mainProg1(ep); |
| 528 | #endif |
| 529 | #if USE_TEST == 2 |
| 530 | mainProg2(ep); |
| 531 | #endif |
| 532 | #if USE_TEST == 3 |
| 533 | mainProg3(ep); |
| 534 | #endif |
| 535 | #if USE_TEST == 4 |
| 536 | mainProg4(ep); |
| 537 | #endif |
| 538 | |
| 539 | ret = PJ_SUCCESS; |
| 540 | } catch (Error & err) { |
| 541 | std::cout << "Exception: " << err.info() << std::endl; |
| 542 | ret = 1; |
| 543 | } |
| 544 | |
| 545 | try { |
| 546 | ep.libDestroy(); |
| 547 | } catch(Error &err) { |
| 548 | std::cout << "Exception: " << err.info() << std::endl; |
| 549 | ret = 1; |
| 550 | } |
| 551 | |
| 552 | if (ret == PJ_SUCCESS) { |
| 553 | std::cout << "Success" << std::endl; |
| 554 | } else { |
| 555 | std::cout << "Error Found" << std::endl; |
| 556 | } |
| 557 | |
| 558 | return ret; |
| 559 | } |
| 560 | |
| 561 |