simpleua.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089
  1. /*
  2. * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
  3. * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. /**
  20. * simpleua.c
  21. *
  22. * This is a very simple SIP user agent complete with media. The user
  23. * agent should do a proper SDP negotiation and start RTP media once
  24. * SDP negotiation has completed.
  25. *
  26. * This program does not register to SIP server.
  27. *
  28. * Capabilities to be demonstrated here:
  29. * - Basic call
  30. * - Should support IPv6 (not tested)
  31. * - UDP transport at port 5060 (hard coded)
  32. * - RTP socket at port 4000 (hard coded)
  33. * - proper SDP negotiation
  34. * - PCMA/PCMU codec only.
  35. * - Audio/media to sound device.
  36. *
  37. *
  38. * Usage:
  39. * - To make outgoing call, start simpleua with the URL of remote
  40. * destination to contact.
  41. * E.g.:
  42. * simpleua sip:user@remote
  43. *
  44. * - Incoming calls will automatically be answered with 180, then 200.
  45. *
  46. * This program does not disconnect call.
  47. *
  48. * This program will quit once it has completed a single call.
  49. */
  50. /* Include all headers. */
  51. #include <pjsip.h>
  52. #include <pjmedia.h>
  53. #include <pjmedia-codec.h>
  54. #include <pjsip_ua.h>
  55. #include <pjsip_simple.h>
  56. #include <pjlib-util.h>
  57. #include <pjlib.h>
  58. /* For logging purpose. */
  59. #define THIS_FILE "simpleua.c"
  60. #include "util.h"
  61. /* Settings */
  62. #define AF pj_AF_INET() /* Change to pj_AF_INET6() for IPv6.
  63. * PJ_HAS_IPV6 must be enabled and
  64. * your system must support IPv6. */
  65. #if 0
  66. #define SIP_PORT 5080 /* Listening SIP port */
  67. #define RTP_PORT 5000 /* RTP port */
  68. #else
  69. #define SIP_PORT 5060 /* Listening SIP port */
  70. #define RTP_PORT 4000 /* RTP port */
  71. #endif
  72. #define MAX_MEDIA_CNT 2 /* Media count, set to 1 for audio
  73. * only or 2 for audio and video */
  74. /*
  75. * Static variables.
  76. */
  77. static pj_bool_t g_complete; /* Quit flag. */
  78. static pjsip_endpoint *g_endpt; /* SIP endpoint. */
  79. static pj_caching_pool cp; /* Global pool factory. */
  80. static pjmedia_endpt *g_med_endpt; /* Media endpoint. */
  81. static pjmedia_transport_info g_med_tpinfo[MAX_MEDIA_CNT];
  82. /* Socket info for media */
  83. static pjmedia_transport *g_med_transport[MAX_MEDIA_CNT];
  84. /* Media stream transport */
  85. static pjmedia_sock_info g_sock_info[MAX_MEDIA_CNT];
  86. /* Socket info array */
  87. /* Call variables: */
  88. static pjsip_inv_session *g_inv; /* Current invite session. */
  89. static pjmedia_stream *g_med_stream; /* Call's audio stream. */
  90. static pjmedia_snd_port *g_snd_port; /* Sound device. */
  91. #if defined(PJMEDIA_HAS_VIDEO) && (PJMEDIA_HAS_VIDEO != 0)
  92. static pjmedia_vid_stream *g_med_vstream; /* Call's video stream. */
  93. static pjmedia_vid_port *g_vid_capturer;/* Call's video capturer. */
  94. static pjmedia_vid_port *g_vid_renderer;/* Call's video renderer. */
  95. #endif /* PJMEDIA_HAS_VIDEO */
  96. /*
  97. * Prototypes:
  98. */
  99. /* Callback to be called when SDP negotiation is done in the call: */
  100. static void call_on_media_update( pjsip_inv_session *inv,
  101. pj_status_t status);
  102. /* Callback to be called when invite session's state has changed: */
  103. static void call_on_state_changed( pjsip_inv_session *inv,
  104. pjsip_event *e);
  105. /* Callback to be called when dialog has forked: */
  106. static void call_on_forked(pjsip_inv_session *inv, pjsip_event *e);
  107. /* Callback to be called to handle incoming requests outside dialogs: */
  108. static pj_bool_t on_rx_request( pjsip_rx_data *rdata );
  109. /* This is a PJSIP module to be registered by application to handle
  110. * incoming requests outside any dialogs/transactions. The main purpose
  111. * here is to handle incoming INVITE request message, where we will
  112. * create a dialog and INVITE session for it.
  113. */
  114. static pjsip_module mod_simpleua =
  115. {
  116. NULL, NULL, /* prev, next. */
  117. { "mod-simpleua", 12 }, /* Name. */
  118. -1, /* Id */
  119. PJSIP_MOD_PRIORITY_APPLICATION, /* Priority */
  120. NULL, /* load() */
  121. NULL, /* start() */
  122. NULL, /* stop() */
  123. NULL, /* unload() */
  124. &on_rx_request, /* on_rx_request() */
  125. NULL, /* on_rx_response() */
  126. NULL, /* on_tx_request. */
  127. NULL, /* on_tx_response() */
  128. NULL, /* on_tsx_state() */
  129. };
  130. /* Notification on incoming messages */
  131. static pj_bool_t logging_on_rx_msg(pjsip_rx_data *rdata)
  132. {
  133. PJ_LOG(4,(THIS_FILE, "RX %d bytes %s from %s %s:%d:\n"
  134. "%.*s\n"
  135. "--end msg--",
  136. rdata->msg_info.len,
  137. pjsip_rx_data_get_info(rdata),
  138. rdata->tp_info.transport->type_name,
  139. rdata->pkt_info.src_name,
  140. rdata->pkt_info.src_port,
  141. (int)rdata->msg_info.len,
  142. rdata->msg_info.msg_buf));
  143. /* Always return false, otherwise messages will not get processed! */
  144. return PJ_FALSE;
  145. }
  146. /* Notification on outgoing messages */
  147. static pj_status_t logging_on_tx_msg(pjsip_tx_data *tdata)
  148. {
  149. /* Important note:
  150. * tp_info field is only valid after outgoing messages has passed
  151. * transport layer. So don't try to access tp_info when the module
  152. * has lower priority than transport layer.
  153. */
  154. PJ_LOG(4,(THIS_FILE, "TX %ld bytes %s to %s %s:%d:\n"
  155. "%.*s\n"
  156. "--end msg--",
  157. (tdata->buf.cur - tdata->buf.start),
  158. pjsip_tx_data_get_info(tdata),
  159. tdata->tp_info.transport->type_name,
  160. tdata->tp_info.dst_name,
  161. tdata->tp_info.dst_port,
  162. (int)(tdata->buf.cur - tdata->buf.start),
  163. tdata->buf.start));
  164. /* Always return success, otherwise message will not get sent! */
  165. return PJ_SUCCESS;
  166. }
  167. /* The module instance. */
  168. static pjsip_module msg_logger =
  169. {
  170. NULL, NULL, /* prev, next. */
  171. { "mod-msg-log", 13 }, /* Name. */
  172. -1, /* Id */
  173. PJSIP_MOD_PRIORITY_TRANSPORT_LAYER-1,/* Priority */
  174. NULL, /* load() */
  175. NULL, /* start() */
  176. NULL, /* stop() */
  177. NULL, /* unload() */
  178. &logging_on_rx_msg, /* on_rx_request() */
  179. &logging_on_rx_msg, /* on_rx_response() */
  180. &logging_on_tx_msg, /* on_tx_request. */
  181. &logging_on_tx_msg, /* on_tx_response() */
  182. NULL, /* on_tsx_state() */
  183. };
  184. /*
  185. * main()
  186. *
  187. * If called with argument, treat argument as SIP URL to be called.
  188. * Otherwise wait for incoming calls.
  189. */
  190. int main(int argc, char *argv[])
  191. {
  192. pj_pool_t *pool = NULL;
  193. pj_status_t status;
  194. unsigned i;
  195. /* Must init PJLIB first: */
  196. status = pj_init();
  197. PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
  198. pj_log_set_level(5);
  199. /* Then init PJLIB-UTIL: */
  200. status = pjlib_util_init();
  201. PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
  202. /* Must create a pool factory before we can allocate any memory. */
  203. pj_caching_pool_init(&cp, &pj_pool_factory_default_policy, 0);
  204. /* Create global endpoint: */
  205. {
  206. const pj_str_t *hostname;
  207. const char *endpt_name;
  208. /* Endpoint MUST be assigned a globally unique name.
  209. * The name will be used as the hostname in Warning header.
  210. */
  211. /* For this implementation, we'll use hostname for simplicity */
  212. hostname = pj_gethostname();
  213. endpt_name = hostname->ptr;
  214. /* Create the endpoint: */
  215. status = pjsip_endpt_create(&cp.factory, endpt_name,
  216. &g_endpt);
  217. PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
  218. }
  219. /*
  220. * Add UDP transport, with hard-coded port
  221. * Alternatively, application can use pjsip_udp_transport_attach() to
  222. * start UDP transport, if it already has an UDP socket (e.g. after it
  223. * resolves the address with STUN).
  224. */
  225. {
  226. pj_sockaddr addr;
  227. int af = AF;
  228. pj_sockaddr_init(af, &addr, NULL, (pj_uint16_t)SIP_PORT);
  229. if (af == pj_AF_INET()) {
  230. status = pjsip_udp_transport_start( g_endpt, &addr.ipv4, NULL,
  231. 1, NULL);
  232. } else if (af == pj_AF_INET6()) {
  233. status = pjsip_udp_transport_start6(g_endpt, &addr.ipv6, NULL,
  234. 1, NULL);
  235. } else {
  236. status = PJ_EAFNOTSUP;
  237. }
  238. if (status != PJ_SUCCESS) {
  239. app_perror(THIS_FILE, "Unable to start UDP transport", status);
  240. return 1;
  241. }
  242. }
  243. /*
  244. * Init transaction layer.
  245. * This will create/initialize transaction hash tables etc.
  246. */
  247. status = pjsip_tsx_layer_init_module(g_endpt);
  248. PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
  249. /*
  250. * Initialize UA layer module.
  251. * This will create/initialize dialog hash tables etc.
  252. */
  253. status = pjsip_ua_init_module( g_endpt, NULL );
  254. PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
  255. /*
  256. * Init invite session module.
  257. * The invite session module initialization takes additional argument,
  258. * i.e. a structure containing callbacks to be called on specific
  259. * occurence of events.
  260. *
  261. * The on_state_changed and on_new_session callbacks are mandatory.
  262. * Application must supply the callback function.
  263. *
  264. * We use on_media_update() callback in this application to start
  265. * media transmission.
  266. */
  267. {
  268. pjsip_inv_callback inv_cb;
  269. /* Init the callback for INVITE session: */
  270. pj_bzero(&inv_cb, sizeof(inv_cb));
  271. inv_cb.on_state_changed = &call_on_state_changed;
  272. inv_cb.on_new_session = &call_on_forked;
  273. inv_cb.on_media_update = &call_on_media_update;
  274. /* Initialize invite session module: */
  275. status = pjsip_inv_usage_init(g_endpt, &inv_cb);
  276. PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
  277. }
  278. /* Initialize 100rel support */
  279. status = pjsip_100rel_init_module(g_endpt);
  280. PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
  281. /*
  282. * Register our module to receive incoming requests.
  283. */
  284. status = pjsip_endpt_register_module( g_endpt, &mod_simpleua);
  285. PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
  286. /*
  287. * Register message logger module.
  288. */
  289. status = pjsip_endpt_register_module( g_endpt, &msg_logger);
  290. PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
  291. /*
  292. * Initialize media endpoint.
  293. * This will implicitly initialize PJMEDIA too.
  294. */
  295. #if PJ_HAS_THREADS
  296. status = pjmedia_endpt_create(&cp.factory, NULL, 1, &g_med_endpt);
  297. #else
  298. status = pjmedia_endpt_create(&cp.factory,
  299. pjsip_endpt_get_ioqueue(g_endpt),
  300. 0, &g_med_endpt);
  301. #endif
  302. PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
  303. /* Create pool. */
  304. pool = pjmedia_endpt_create_pool(g_med_endpt, "Media pool", 512, 512);
  305. /*
  306. * Add PCMA/PCMU codec to the media endpoint.
  307. */
  308. #if defined(PJMEDIA_HAS_G711_CODEC) && PJMEDIA_HAS_G711_CODEC!=0
  309. status = pjmedia_codec_g711_init(g_med_endpt);
  310. PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
  311. #endif
  312. #if defined(PJMEDIA_HAS_VIDEO) && (PJMEDIA_HAS_VIDEO != 0)
  313. /* Init video subsystem */
  314. status = pjmedia_video_format_mgr_create(pool, 64, 0, NULL);
  315. PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
  316. status = pjmedia_converter_mgr_create(pool, NULL);
  317. PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
  318. status = pjmedia_vid_codec_mgr_create(pool, NULL);
  319. PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
  320. status = pjmedia_vid_dev_subsys_init(&cp.factory);
  321. PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
  322. #if PJMEDIA_HAS_VIDEO && PJMEDIA_HAS_VID_TOOLBOX_CODEC
  323. status = pjmedia_codec_vid_toolbox_init(NULL, &cp.factory);
  324. PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
  325. #endif
  326. # if defined(PJMEDIA_HAS_OPENH264_CODEC) && PJMEDIA_HAS_OPENH264_CODEC != 0
  327. status = pjmedia_codec_openh264_vid_init(NULL, &cp.factory);
  328. PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
  329. # endif
  330. # if defined(PJMEDIA_HAS_VPX_CODEC) && PJMEDIA_HAS_VPX_CODEC != 0
  331. status = pjmedia_codec_vpx_vid_init(NULL, &cp.factory);
  332. PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
  333. # endif
  334. # if defined(PJMEDIA_HAS_FFMPEG_VID_CODEC) && PJMEDIA_HAS_FFMPEG_VID_CODEC!=0
  335. /* Init ffmpeg video codecs */
  336. status = pjmedia_codec_ffmpeg_vid_init(NULL, &cp.factory);
  337. PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
  338. # endif /* PJMEDIA_HAS_FFMPEG_VID_CODEC */
  339. #endif /* PJMEDIA_HAS_VIDEO */
  340. /* Create event manager */
  341. status = pjmedia_event_mgr_create(pool, 0, NULL);
  342. PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
  343. /*
  344. * Create media transport used to send/receive RTP/RTCP socket.
  345. * One media transport is needed for each call. Application may
  346. * opt to re-use the same media transport for subsequent calls.
  347. */
  348. for (i = 0; i < PJ_ARRAY_SIZE(g_med_transport); ++i) {
  349. status = pjmedia_transport_udp_create3(g_med_endpt, AF, NULL, NULL,
  350. RTP_PORT + i*2, 0,
  351. &g_med_transport[i]);
  352. if (status != PJ_SUCCESS) {
  353. app_perror(THIS_FILE, "Unable to create media transport", status);
  354. return 1;
  355. }
  356. /*
  357. * Get socket info (address, port) of the media transport. We will
  358. * need this info to create SDP (i.e. the address and port info in
  359. * the SDP).
  360. */
  361. pjmedia_transport_info_init(&g_med_tpinfo[i]);
  362. pjmedia_transport_get_info(g_med_transport[i], &g_med_tpinfo[i]);
  363. pj_memcpy(&g_sock_info[i], &g_med_tpinfo[i].sock_info,
  364. sizeof(pjmedia_sock_info));
  365. }
  366. /*
  367. * If URL is specified, then make call immediately.
  368. */
  369. if (argc > 1) {
  370. pj_sockaddr hostaddr;
  371. char hostip[PJ_INET6_ADDRSTRLEN+2];
  372. char temp[80];
  373. pj_str_t dst_uri = pj_str(argv[1]);
  374. pj_str_t local_uri;
  375. pjsip_dialog *dlg;
  376. pjmedia_sdp_session *local_sdp;
  377. pjsip_tx_data *tdata;
  378. if (pj_gethostip(AF, &hostaddr) != PJ_SUCCESS) {
  379. app_perror(THIS_FILE, "Unable to retrieve local host IP", status);
  380. return 1;
  381. }
  382. pj_sockaddr_print(&hostaddr, hostip, sizeof(hostip), 2);
  383. pj_ansi_snprintf(temp, sizeof(temp), "<sip:simpleuac@%s:%d>",
  384. hostip, SIP_PORT);
  385. local_uri = pj_str(temp);
  386. /* Create UAC dialog */
  387. status = pjsip_dlg_create_uac( pjsip_ua_instance(),
  388. &local_uri, /* local URI */
  389. &local_uri, /* local Contact */
  390. &dst_uri, /* remote URI */
  391. &dst_uri, /* remote target */
  392. &dlg); /* dialog */
  393. if (status != PJ_SUCCESS) {
  394. app_perror(THIS_FILE, "Unable to create UAC dialog", status);
  395. return 1;
  396. }
  397. /* If we expect the outgoing INVITE to be challenged, then we should
  398. * put the credentials in the dialog here, with something like this:
  399. *
  400. {
  401. pjsip_cred_info cred[1];
  402. cred[0].realm = pj_str("sip.server.realm");
  403. cred[0].scheme = pj_str("digest");
  404. cred[0].username = pj_str("theuser");
  405. cred[0].data_type = PJSIP_CRED_DATA_PLAIN_PASSWD;
  406. cred[0].data = pj_str("thepassword");
  407. pjsip_auth_clt_set_credentials( &dlg->auth_sess, 1, cred);
  408. }
  409. *
  410. */
  411. /* Get the SDP body to be put in the outgoing INVITE, by asking
  412. * media endpoint to create one for us.
  413. */
  414. status = pjmedia_endpt_create_sdp( g_med_endpt, /* the media endpt */
  415. dlg->pool, /* pool. */
  416. MAX_MEDIA_CNT, /* # of streams */
  417. g_sock_info, /* RTP sock info */
  418. &local_sdp); /* the SDP result */
  419. PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
  420. /* Create the INVITE session, and pass the SDP returned earlier
  421. * as the session's initial capability.
  422. */
  423. status = pjsip_inv_create_uac( dlg, local_sdp, 0, &g_inv);
  424. PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
  425. /* If we want the initial INVITE to travel to specific SIP proxies,
  426. * then we should put the initial dialog's route set here. The final
  427. * route set will be updated once a dialog has been established.
  428. * To set the dialog's initial route set, we do it with something
  429. * like this:
  430. *
  431. {
  432. pjsip_route_hdr route_set;
  433. pjsip_route_hdr *route;
  434. const pj_str_t hname = { "Route", 5 };
  435. char *uri = "sip:proxy.server;lr";
  436. pj_list_init(&route_set);
  437. route = pjsip_parse_hdr( dlg->pool, &hname,
  438. uri, strlen(uri),
  439. NULL);
  440. PJ_ASSERT_RETURN(route != NULL, 1);
  441. pj_list_push_back(&route_set, route);
  442. pjsip_dlg_set_route_set(dlg, &route_set);
  443. }
  444. *
  445. * Note that Route URI SHOULD have an ";lr" parameter!
  446. */
  447. /* Create initial INVITE request.
  448. * This INVITE request will contain a perfectly good request and
  449. * an SDP body as well.
  450. */
  451. status = pjsip_inv_invite(g_inv, &tdata);
  452. PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
  453. /* Send initial INVITE request.
  454. * From now on, the invite session's state will be reported to us
  455. * via the invite session callbacks.
  456. */
  457. status = pjsip_inv_send_msg(g_inv, tdata);
  458. PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
  459. } else {
  460. /* No URL to make call to */
  461. PJ_LOG(3,(THIS_FILE, "Ready to accept incoming calls..."));
  462. }
  463. /* Loop until one call is completed */
  464. for (;!g_complete;) {
  465. pj_time_val timeout = {0, 10};
  466. pjsip_endpt_handle_events(g_endpt, &timeout);
  467. }
  468. /* On exit, dump current memory usage: */
  469. dump_pool_usage(THIS_FILE, &cp);
  470. /* Destroy audio ports. Destroy the audio port first
  471. * before the stream since the audio port has threads
  472. * that get/put frames to the stream.
  473. */
  474. if (g_snd_port)
  475. pjmedia_snd_port_destroy(g_snd_port);
  476. #if defined(PJMEDIA_HAS_VIDEO) && (PJMEDIA_HAS_VIDEO != 0)
  477. /* Destroy video ports */
  478. if (g_vid_capturer)
  479. pjmedia_vid_port_destroy(g_vid_capturer);
  480. if (g_vid_renderer)
  481. pjmedia_vid_port_destroy(g_vid_renderer);
  482. #endif
  483. /* Destroy streams */
  484. if (g_med_stream)
  485. pjmedia_stream_destroy(g_med_stream);
  486. #if defined(PJMEDIA_HAS_VIDEO) && (PJMEDIA_HAS_VIDEO != 0)
  487. if (g_med_vstream)
  488. pjmedia_vid_stream_destroy(g_med_vstream);
  489. /* Deinit ffmpeg codec */
  490. # if defined(PJMEDIA_HAS_FFMPEG_VID_CODEC) && PJMEDIA_HAS_FFMPEG_VID_CODEC!=0
  491. pjmedia_codec_ffmpeg_vid_deinit();
  492. # endif
  493. # if defined(PJMEDIA_HAS_OPENH264_CODEC) && PJMEDIA_HAS_OPENH264_CODEC != 0
  494. pjmedia_codec_openh264_vid_deinit();
  495. # endif
  496. # if PJMEDIA_HAS_VIDEO && PJMEDIA_HAS_VID_TOOLBOX_CODEC
  497. pjmedia_codec_vid_toolbox_deinit();
  498. # endif
  499. # if defined(PJMEDIA_HAS_VPX_CODEC) && PJMEDIA_HAS_VPX_CODEC != 0
  500. pjmedia_codec_vpx_vid_deinit();
  501. # endif
  502. #endif
  503. /* Destroy media transports */
  504. for (i = 0; i < MAX_MEDIA_CNT; ++i) {
  505. if (g_med_transport[i])
  506. pjmedia_transport_close(g_med_transport[i]);
  507. }
  508. /* Destroy event manager */
  509. pjmedia_event_mgr_destroy(NULL);
  510. /* Deinit pjmedia endpoint */
  511. if (g_med_endpt)
  512. pjmedia_endpt_destroy(g_med_endpt);
  513. /* Deinit pjsip endpoint */
  514. if (g_endpt)
  515. pjsip_endpt_destroy(g_endpt);
  516. /* Release pool */
  517. if (pool)
  518. pj_pool_release(pool);
  519. return 0;
  520. }
  521. /*
  522. * Callback when INVITE session state has changed.
  523. * This callback is registered when the invite session module is initialized.
  524. * We mostly want to know when the invite session has been disconnected,
  525. * so that we can quit the application.
  526. */
  527. static void call_on_state_changed( pjsip_inv_session *inv,
  528. pjsip_event *e)
  529. {
  530. PJ_UNUSED_ARG(e);
  531. if (inv->state == PJSIP_INV_STATE_DISCONNECTED) {
  532. PJ_LOG(3,(THIS_FILE, "Call DISCONNECTED [reason=%d (%s)]",
  533. inv->cause,
  534. pjsip_get_status_text(inv->cause)->ptr));
  535. PJ_LOG(3,(THIS_FILE, "One call completed, application quitting..."));
  536. g_complete = 1;
  537. } else {
  538. PJ_LOG(3,(THIS_FILE, "Call state changed to %s",
  539. pjsip_inv_state_name(inv->state)));
  540. }
  541. }
  542. /* This callback is called when dialog has forked. */
  543. static void call_on_forked(pjsip_inv_session *inv, pjsip_event *e)
  544. {
  545. /* To be done... */
  546. PJ_UNUSED_ARG(inv);
  547. PJ_UNUSED_ARG(e);
  548. }
  549. /*
  550. * Callback when incoming requests outside any transactions and any
  551. * dialogs are received. We're only interested to hande incoming INVITE
  552. * request, and we'll reject any other requests with 500 response.
  553. */
  554. static pj_bool_t on_rx_request( pjsip_rx_data *rdata )
  555. {
  556. pj_sockaddr hostaddr;
  557. char temp[80], hostip[PJ_INET6_ADDRSTRLEN];
  558. pj_str_t local_uri;
  559. pjsip_dialog *dlg;
  560. pjmedia_sdp_session *local_sdp;
  561. pjsip_tx_data *tdata;
  562. unsigned options = 0;
  563. pj_status_t status;
  564. /*
  565. * Respond (statelessly) any non-INVITE requests with 500
  566. */
  567. if (rdata->msg_info.msg->line.req.method.id != PJSIP_INVITE_METHOD) {
  568. if (rdata->msg_info.msg->line.req.method.id != PJSIP_ACK_METHOD) {
  569. pj_str_t reason = pj_str("Simple UA unable to handle "
  570. "this request");
  571. pjsip_endpt_respond_stateless( g_endpt, rdata,
  572. 500, &reason,
  573. NULL, NULL);
  574. }
  575. return PJ_TRUE;
  576. }
  577. /*
  578. * Reject INVITE if we already have an INVITE session in progress.
  579. */
  580. if (g_inv) {
  581. pj_str_t reason = pj_str("Another call is in progress");
  582. pjsip_endpt_respond_stateless( g_endpt, rdata,
  583. 500, &reason,
  584. NULL, NULL);
  585. return PJ_TRUE;
  586. }
  587. /* Verify that we can handle the request. */
  588. status = pjsip_inv_verify_request(rdata, &options, NULL, NULL,
  589. g_endpt, NULL);
  590. if (status != PJ_SUCCESS) {
  591. pj_str_t reason = pj_str("Sorry Simple UA can not handle this INVITE");
  592. pjsip_endpt_respond_stateless( g_endpt, rdata,
  593. 500, &reason,
  594. NULL, NULL);
  595. return PJ_TRUE;
  596. }
  597. /*
  598. * Generate Contact URI
  599. */
  600. if (pj_gethostip(AF, &hostaddr) != PJ_SUCCESS) {
  601. app_perror(THIS_FILE, "Unable to retrieve local host IP", status);
  602. return PJ_TRUE;
  603. }
  604. pj_sockaddr_print(&hostaddr, hostip, sizeof(hostip), 2);
  605. pj_ansi_snprintf(temp, sizeof(temp), "<sip:simpleuas@%s:%d>",
  606. hostip, SIP_PORT);
  607. local_uri = pj_str(temp);
  608. /*
  609. * Create UAS dialog.
  610. */
  611. status = pjsip_dlg_create_uas_and_inc_lock( pjsip_ua_instance(),
  612. rdata,
  613. &local_uri, /* contact */
  614. &dlg);
  615. if (status != PJ_SUCCESS) {
  616. pjsip_endpt_respond_stateless(g_endpt, rdata, 500, NULL,
  617. NULL, NULL);
  618. return PJ_TRUE;
  619. }
  620. /*
  621. * Get media capability from media endpoint:
  622. */
  623. status = pjmedia_endpt_create_sdp( g_med_endpt, rdata->tp_info.pool,
  624. MAX_MEDIA_CNT, g_sock_info, &local_sdp);
  625. pj_assert(status == PJ_SUCCESS);
  626. if (status != PJ_SUCCESS) {
  627. pjsip_dlg_dec_lock(dlg);
  628. return PJ_TRUE;
  629. }
  630. /*
  631. * Create invite session, and pass both the UAS dialog and the SDP
  632. * capability to the session.
  633. */
  634. status = pjsip_inv_create_uas( dlg, rdata, local_sdp, 0, &g_inv);
  635. pj_assert(status == PJ_SUCCESS);
  636. if (status != PJ_SUCCESS) {
  637. pjsip_dlg_dec_lock(dlg);
  638. return PJ_TRUE;
  639. }
  640. /*
  641. * Invite session has been created, decrement & release dialog lock.
  642. */
  643. pjsip_dlg_dec_lock(dlg);
  644. /*
  645. * Initially send 180 response.
  646. *
  647. * The very first response to an INVITE must be created with
  648. * pjsip_inv_initial_answer(). Subsequent responses to the same
  649. * transaction MUST use pjsip_inv_answer().
  650. */
  651. status = pjsip_inv_initial_answer(g_inv, rdata,
  652. 180,
  653. NULL, NULL, &tdata);
  654. PJ_ASSERT_RETURN(status == PJ_SUCCESS, PJ_TRUE);
  655. /* Send the 180 response. */
  656. status = pjsip_inv_send_msg(g_inv, tdata);
  657. PJ_ASSERT_RETURN(status == PJ_SUCCESS, PJ_TRUE);
  658. /*
  659. * Now create 200 response.
  660. */
  661. status = pjsip_inv_answer( g_inv,
  662. 200, NULL, /* st_code and st_text */
  663. NULL, /* SDP already specified */
  664. &tdata);
  665. PJ_ASSERT_RETURN(status == PJ_SUCCESS, PJ_TRUE);
  666. /*
  667. * Send the 200 response.
  668. */
  669. status = pjsip_inv_send_msg(g_inv, tdata);
  670. PJ_ASSERT_RETURN(status == PJ_SUCCESS, PJ_TRUE);
  671. /* Done.
  672. * When the call is disconnected, it will be reported via the callback.
  673. */
  674. return PJ_TRUE;
  675. }
  676. /*
  677. * Callback when SDP negotiation has completed.
  678. * We are interested with this callback because we want to start media
  679. * as soon as SDP negotiation is completed.
  680. */
  681. static void call_on_media_update( pjsip_inv_session *inv,
  682. pj_status_t status)
  683. {
  684. pjmedia_stream_info stream_info;
  685. const pjmedia_sdp_session *local_sdp;
  686. const pjmedia_sdp_session *remote_sdp;
  687. pjmedia_port *media_port;
  688. if (status != PJ_SUCCESS) {
  689. app_perror(THIS_FILE, "SDP negotiation has failed", status);
  690. /* Here we should disconnect call if we're not in the middle
  691. * of initializing an UAS dialog and if this is not a re-INVITE.
  692. */
  693. return;
  694. }
  695. /* Get local and remote SDP.
  696. * We need both SDPs to create a media session.
  697. */
  698. status = pjmedia_sdp_neg_get_active_local(inv->neg, &local_sdp);
  699. status = pjmedia_sdp_neg_get_active_remote(inv->neg, &remote_sdp);
  700. /* Create stream info based on the media audio SDP. */
  701. status = pjmedia_stream_info_from_sdp(&stream_info, inv->dlg->pool,
  702. g_med_endpt,
  703. local_sdp, remote_sdp, 0);
  704. if (status != PJ_SUCCESS) {
  705. app_perror(THIS_FILE,"Unable to create audio stream info",status);
  706. return;
  707. }
  708. /* If required, we can also change some settings in the stream info,
  709. * (such as jitter buffer settings, codec settings, etc) before we
  710. * create the stream.
  711. */
  712. /* Create new audio media stream, passing the stream info, and also the
  713. * media socket that we created earlier.
  714. */
  715. status = pjmedia_stream_create(g_med_endpt, inv->dlg->pool, &stream_info,
  716. g_med_transport[0], NULL, &g_med_stream);
  717. if (status != PJ_SUCCESS) {
  718. app_perror( THIS_FILE, "Unable to create audio stream", status);
  719. return;
  720. }
  721. /* Start the audio stream */
  722. status = pjmedia_stream_start(g_med_stream);
  723. if (status != PJ_SUCCESS) {
  724. app_perror( THIS_FILE, "Unable to start audio stream", status);
  725. return;
  726. }
  727. /* Start the UDP media transport */
  728. status = pjmedia_transport_media_start(g_med_transport[0], 0, 0, 0, 0);
  729. if (status != PJ_SUCCESS) {
  730. app_perror( THIS_FILE, "Unable to start UDP media transport", status);
  731. return;
  732. }
  733. /* Get the media port interface of the audio stream.
  734. * Media port interface is basicly a struct containing get_frame() and
  735. * put_frame() function. With this media port interface, we can attach
  736. * the port interface to conference bridge, or directly to a sound
  737. * player/recorder device.
  738. */
  739. status = pjmedia_stream_get_port(g_med_stream, &media_port);
  740. if (status != PJ_SUCCESS) {
  741. app_perror( THIS_FILE, "Unable to create media port interface of the audio stream", status);
  742. return;
  743. }
  744. /* Create sound port */
  745. status = pjmedia_snd_port_create(inv->pool,
  746. PJMEDIA_AUD_DEFAULT_CAPTURE_DEV,
  747. PJMEDIA_AUD_DEFAULT_PLAYBACK_DEV,
  748. PJMEDIA_PIA_SRATE(&media_port->info),/* clock rate */
  749. PJMEDIA_PIA_CCNT(&media_port->info),/* channel count */
  750. PJMEDIA_PIA_SPF(&media_port->info), /* samples per frame*/
  751. PJMEDIA_PIA_BITS(&media_port->info),/* bits per sample */
  752. 0,
  753. &g_snd_port);
  754. if (status != PJ_SUCCESS) {
  755. app_perror( THIS_FILE, "Unable to create sound port", status);
  756. PJ_LOG(3,(THIS_FILE, "%d %d %d %d",
  757. PJMEDIA_PIA_SRATE(&media_port->info),/* clock rate */
  758. PJMEDIA_PIA_CCNT(&media_port->info),/* channel count */
  759. PJMEDIA_PIA_SPF(&media_port->info), /* samples per frame*/
  760. PJMEDIA_PIA_BITS(&media_port->info) /* bits per sample */
  761. ));
  762. return;
  763. }
  764. status = pjmedia_snd_port_connect(g_snd_port, media_port);
  765. /* Get the media port interface of the second stream in the session,
  766. * which is video stream. With this media port interface, we can attach
  767. * the port directly to a renderer/capture video device.
  768. */
  769. #if defined(PJMEDIA_HAS_VIDEO) && (PJMEDIA_HAS_VIDEO != 0)
  770. if (local_sdp->media_count > 1) {
  771. pjmedia_vid_stream_info vstream_info;
  772. pjmedia_vid_port_param vport_param;
  773. pjmedia_vid_port_param_default(&vport_param);
  774. /* Create stream info based on the media video SDP. */
  775. status = pjmedia_vid_stream_info_from_sdp(&vstream_info,
  776. inv->dlg->pool, g_med_endpt,
  777. local_sdp, remote_sdp, 1);
  778. if (status != PJ_SUCCESS) {
  779. app_perror(THIS_FILE,"Unable to create video stream info",status);
  780. return;
  781. }
  782. /* If required, we can also change some settings in the stream info,
  783. * (such as jitter buffer settings, codec settings, etc) before we
  784. * create the video stream.
  785. */
  786. /* Create new video media stream, passing the stream info, and also the
  787. * media socket that we created earlier.
  788. */
  789. status = pjmedia_vid_stream_create(g_med_endpt, NULL, &vstream_info,
  790. g_med_transport[1], NULL,
  791. &g_med_vstream);
  792. if (status != PJ_SUCCESS) {
  793. app_perror( THIS_FILE, "Unable to create video stream", status);
  794. return;
  795. }
  796. /* Start the video stream */
  797. status = pjmedia_vid_stream_start(g_med_vstream);
  798. if (status != PJ_SUCCESS) {
  799. app_perror( THIS_FILE, "Unable to start video stream", status);
  800. return;
  801. }
  802. /* Start the UDP media transport */
  803. pjmedia_transport_media_start(g_med_transport[1], 0, 0, 0, 0);
  804. if (vstream_info.dir & PJMEDIA_DIR_DECODING) {
  805. status = pjmedia_vid_dev_default_param(
  806. inv->pool, PJMEDIA_VID_DEFAULT_RENDER_DEV,
  807. &vport_param.vidparam);
  808. if (status != PJ_SUCCESS) {
  809. app_perror(THIS_FILE, "Unable to get default param of video "
  810. "renderer device", status);
  811. return;
  812. }
  813. /* Get video stream port for decoding direction */
  814. pjmedia_vid_stream_get_port(g_med_vstream, PJMEDIA_DIR_DECODING,
  815. &media_port);
  816. /* Set format */
  817. pjmedia_format_copy(&vport_param.vidparam.fmt,
  818. &media_port->info.fmt);
  819. vport_param.vidparam.dir = PJMEDIA_DIR_RENDER;
  820. vport_param.active = PJ_TRUE;
  821. /* Create renderer */
  822. status = pjmedia_vid_port_create(inv->pool, &vport_param,
  823. &g_vid_renderer);
  824. if (status != PJ_SUCCESS) {
  825. app_perror(THIS_FILE, "Unable to create video renderer device",
  826. status);
  827. return;
  828. }
  829. /* Connect renderer to media_port */
  830. status = pjmedia_vid_port_connect(g_vid_renderer, media_port,
  831. PJ_FALSE);
  832. if (status != PJ_SUCCESS) {
  833. app_perror(THIS_FILE, "Unable to connect renderer to stream",
  834. status);
  835. return;
  836. }
  837. }
  838. /* Create capturer */
  839. if (vstream_info.dir & PJMEDIA_DIR_ENCODING) {
  840. status = pjmedia_vid_dev_default_param(
  841. inv->pool, PJMEDIA_VID_DEFAULT_CAPTURE_DEV,
  842. &vport_param.vidparam);
  843. if (status != PJ_SUCCESS) {
  844. app_perror(THIS_FILE, "Unable to get default param of video "
  845. "capture device", status);
  846. return;
  847. }
  848. /* Get video stream port for decoding direction */
  849. pjmedia_vid_stream_get_port(g_med_vstream, PJMEDIA_DIR_ENCODING,
  850. &media_port);
  851. /* Get capturer format from stream info */
  852. pjmedia_format_copy(&vport_param.vidparam.fmt,
  853. &media_port->info.fmt);
  854. vport_param.vidparam.dir = PJMEDIA_DIR_CAPTURE;
  855. vport_param.active = PJ_TRUE;
  856. /* Create capturer */
  857. status = pjmedia_vid_port_create(inv->pool, &vport_param,
  858. &g_vid_capturer);
  859. if (status != PJ_SUCCESS) {
  860. app_perror(THIS_FILE, "Unable to create video capture device",
  861. status);
  862. return;
  863. }
  864. /* Connect capturer to media_port */
  865. status = pjmedia_vid_port_connect(g_vid_capturer, media_port,
  866. PJ_FALSE);
  867. if (status != PJ_SUCCESS) {
  868. app_perror(THIS_FILE, "Unable to connect capturer to stream",
  869. status);
  870. return;
  871. }
  872. }
  873. /* Start streaming */
  874. if (g_vid_renderer) {
  875. status = pjmedia_vid_port_start(g_vid_renderer);
  876. if (status != PJ_SUCCESS) {
  877. app_perror(THIS_FILE, "Unable to start video renderer",
  878. status);
  879. return;
  880. }
  881. }
  882. if (g_vid_capturer) {
  883. status = pjmedia_vid_port_start(g_vid_capturer);
  884. if (status != PJ_SUCCESS) {
  885. app_perror(THIS_FILE, "Unable to start video capturer",
  886. status);
  887. return;
  888. }
  889. }
  890. }
  891. #endif /* PJMEDIA_HAS_VIDEO */
  892. /* Done with media. */
  893. }