pjsua_internal.h 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992
  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. #ifndef __PJSUA_INTERNAL_H__
  20. #define __PJSUA_INTERNAL_H__
  21. /**
  22. * This is the private header used by pjsua library implementation.
  23. * Applications should not include this file.
  24. */
  25. PJ_BEGIN_DECL
  26. /** Forward decl of pjsua call */
  27. typedef struct pjsua_call pjsua_call;
  28. /** Forward decl of pjsua call media */
  29. typedef struct pjsua_call_media pjsua_call_media;
  30. /**
  31. * Call's media stream.
  32. */
  33. struct pjsua_call_media
  34. {
  35. pjsua_call *call; /**< Parent call. */
  36. pjmedia_type type; /**< Media type. */
  37. unsigned idx; /**< This media index in parent call. */
  38. pj_str_t rem_mid; /**< Remote SDP "a=mid" attribute. */
  39. pjsua_call_media_status state; /**< Media state. */
  40. pjsua_call_media_status prev_state;/**< Previous media state. */
  41. pjmedia_dir def_dir; /**< Default media direction. */
  42. pjmedia_dir dir; /**< Current media direction. */
  43. /** The stream */
  44. struct {
  45. /** Audio stream */
  46. struct {
  47. pjmedia_stream *stream; /**< The audio stream. */
  48. pjmedia_port *media_port;/**< The media port. */
  49. pj_bool_t destroy_port;/**< Destroy the media port? */
  50. int conf_slot; /**< Slot # in conference bridge. */
  51. } a;
  52. /** Video stream */
  53. struct {
  54. pjmedia_vid_stream *stream; /**< The video stream. */
  55. pjsua_conf_port_id strm_enc_slot; /**< Stream encode slot */
  56. pjsua_conf_port_id strm_dec_slot; /**< Stream decode slot */
  57. pjsua_vid_win_id cap_win_id;/**< The video capture window */
  58. pjsua_vid_win_id rdr_win_id;/**< The video render window */
  59. pjmedia_vid_dev_index cap_dev; /**< The video capture device */
  60. pjmedia_vid_dev_index rdr_dev; /**< The video-in render device */
  61. } v;
  62. } strm;
  63. pj_uint32_t ssrc; /**< RTP SSRC */
  64. pj_uint32_t rtp_tx_ts; /**< Initial RTP timestamp for sender. */
  65. pj_uint16_t rtp_tx_seq;/**< Initial RTP sequence for sender. */
  66. pj_uint8_t rtp_tx_seq_ts_set;
  67. /**< Bitmask flags if initial RTP sequence
  68. and/or timestamp for sender are set.
  69. bit 0/LSB : sequence flag
  70. bit 1 : timestamp flag */
  71. pjmedia_type prev_type; /**< Previous media type */
  72. pj_sockaddr prev_local_addr; /**< Prev media local address*/
  73. pj_sockaddr prev_rem_addr; /**< Prev media remote addr */
  74. pj_bool_t prev_srtp_use; /**< Prev SRTP use */
  75. pjmedia_srtp_info prev_srtp_info; /**< Prev SRTP transport info*/
  76. pj_bool_t prev_ice_use; /**< Prev ICE use */
  77. pjmedia_ice_transport_info prev_ice_info; /**< Prev ICE transport info */
  78. pjmedia_transport *tp; /**< Current media transport (can be 0) */
  79. pj_status_t tp_ready; /**< Media transport status. */
  80. pj_status_t tp_result; /**< Media transport creation result. */
  81. pjmedia_transport *tp_orig; /**< Original media transport */
  82. pj_bool_t tp_auto_del; /**< May delete media transport */
  83. pjsua_med_tp_st tp_st; /**< Media transport state */
  84. pj_bool_t use_custom_med_tp;/**< Use custom media transport? */
  85. pj_bool_t enable_rtcp_mux;/**< Enable RTP& RTCP multiplexing?*/
  86. pj_sockaddr rtp_addr; /**< Current RTP source address
  87. (used to update ICE default
  88. address) */
  89. pjmedia_srtp_use rem_srtp_use; /**< Remote's SRTP usage policy. */
  90. pj_timestamp last_req_keyframe;/**< Last TX keyframe request. */
  91. pj_bool_t use_upnp; /**< Use UPnP? */
  92. pj_sockaddr mapped_addr[2]; /* UPnP mapped address. */
  93. pjsua_med_tp_state_cb med_init_cb;/**< Media transport
  94. initialization callback. */
  95. /** Media transport creation callback. */
  96. pj_status_t (*med_create_cb)(pjsua_call_media *call_med,
  97. pj_status_t status, int security_level,
  98. int *sip_err_code);
  99. };
  100. /**
  101. * Maximum number of SDP "m=" lines to be supported.
  102. */
  103. #define PJSUA_MAX_CALL_MEDIA PJMEDIA_MAX_SDP_MEDIA
  104. /* Call answer's list. */
  105. typedef struct call_answer
  106. {
  107. PJ_DECL_LIST_MEMBER(struct call_answer);
  108. pjsua_msg_data *msg_data; /**< Answer's headers list. */
  109. pj_str_t *reason; /**< Answer's reason phrase. */
  110. unsigned code; /**< Answer's status code. */
  111. pjsua_call_setting *opt; /**< Answer's call setting. */
  112. } call_answer;
  113. /* Generic states */
  114. typedef enum pjsua_op_state {
  115. PJSUA_OP_STATE_NULL,
  116. PJSUA_OP_STATE_READY,
  117. PJSUA_OP_STATE_RUNNING,
  118. PJSUA_OP_STATE_DONE,
  119. } pjsua_op_state;
  120. /**
  121. * Structure to be attached to invite dialog.
  122. * Given a dialog "dlg", application can retrieve this structure
  123. * by accessing dlg->mod_data[pjsua.mod.id].
  124. */
  125. struct pjsua_call
  126. {
  127. unsigned index; /**< Index in pjsua array. */
  128. pjsua_call_setting opt; /**< Call setting. */
  129. pj_bool_t opt_inited;/**< Initial call setting has been set,
  130. to avoid different opt in answer. */
  131. pjsip_inv_session *inv; /**< The invite session. */
  132. void *user_data; /**< User/application data. */
  133. pjsip_status_code last_code; /**< Last status code seen. */
  134. pj_str_t last_text; /**< Last status text seen. */
  135. pj_time_val start_time;/**< First INVITE sent/received. */
  136. pj_time_val res_time; /**< First response sent/received. */
  137. pj_time_val conn_time; /**< Connected/confirmed time. */
  138. pj_time_val dis_time; /**< Disconnect time. */
  139. pjsua_acc_id acc_id; /**< Account index being used. */
  140. int secure_level;/**< Signaling security level. */
  141. pjsua_call_hold_type call_hold_type; /**< How to do call hold. */
  142. pj_bool_t local_hold;/**< Flag for call-hold by local. */
  143. void *hold_msg; /**< Outgoing hold tx_data. */
  144. pj_str_t cname; /**< RTCP CNAME. */
  145. char cname_buf[16];/**< cname buffer. */
  146. unsigned med_cnt; /**< Number of media in SDP. */
  147. pjsua_call_media media[PJSUA_MAX_CALL_MEDIA]; /**< Array of media */
  148. unsigned med_prov_cnt;/**< Number of provisional media. */
  149. pjsua_call_media media_prov[PJSUA_MAX_CALL_MEDIA];
  150. /**< Array of provisional media. */
  151. pj_bool_t med_update_success;
  152. /**< Is media update successful? */
  153. pj_bool_t hanging_up;/**< Is call in the process of hangup? */
  154. int audio_idx; /**< First active audio media. */
  155. pj_mutex_t *med_ch_mutex;/**< Media channel callback's mutex. */
  156. pjsua_med_tp_state_cb med_ch_cb;/**< Media channel callback. */
  157. pjsua_med_tp_state_info med_ch_info;/**< Media channel info. */
  158. pjsip_evsub *xfer_sub; /**< Xfer server subscription, if this
  159. call was triggered by xfer. */
  160. pj_stun_nat_type rem_nat_type; /**< NAT type of remote endpoint. */
  161. char last_text_buf_[128]; /**< Buffer for last_text. */
  162. struct {
  163. int retry_cnt; /**< Retry count. */
  164. } lock_codec; /**< Data for codec locking when answer
  165. contains multiple codecs. */
  166. struct {
  167. pjsip_dialog *dlg; /**< Call dialog. */
  168. pjmedia_sdp_session *rem_sdp;/**< Remote SDP. */
  169. pj_pool_t *pool_prov;/**< Provisional pool. */
  170. pj_bool_t med_ch_deinit;/**< Media channel de-init-ed? */
  171. union {
  172. struct {
  173. pjsua_msg_data *msg_data;/**< Headers for outgoing INVITE. */
  174. pj_bool_t hangup; /**< Call is hangup? */
  175. } out_call;
  176. struct {
  177. call_answer answers;/**< A list of call answers. */
  178. pj_bool_t hangup;/**< Call is hangup? */
  179. pjsip_dialog *replaced_dlg; /**< Replaced dialog. */
  180. } inc_call;
  181. } call_var;
  182. } async_call; /**< Temporary storage for async
  183. outgoing/incoming call. */
  184. pj_bool_t rem_offerer; /**< Was remote SDP offerer? */
  185. unsigned rem_aud_cnt; /**< No of active audio in last remote
  186. offer. */
  187. unsigned rem_vid_cnt; /**< No of active video in last remote
  188. offer. */
  189. pj_bool_t rx_reinv_async;/**< on_call_rx_reinvite() async. */
  190. pj_timer_entry reinv_timer; /**< Reinvite retry timer. */
  191. pj_bool_t reinv_pending;/**< Pending until CONFIRMED state. */
  192. pj_bool_t reinv_ice_sent;/**< Has reinvite for ICE upd sent? */
  193. pjsip_rx_data *incoming_data;/**< Cloned incoming call rdata.
  194. On pjsua2, when handling incoming
  195. call, onCreateMediaTransport() will
  196. not be called since the call isn't
  197. created yet. This temporary
  198. variable is used to handle such
  199. case, see ticket #1916. */
  200. struct {
  201. pj_bool_t enabled;
  202. pj_bool_t remote_sup;
  203. pj_bool_t remote_dlg_est;
  204. pjsua_op_state trickling;
  205. int retrans18x_count;
  206. pj_bool_t pending_info;
  207. pj_timer_entry timer;
  208. } trickle_ice;
  209. pj_timer_entry hangup_timer; /**< Hangup retry timer. */
  210. unsigned hangup_retry; /**< Number of hangup retries. */
  211. unsigned hangup_code; /**< Hangup code. */
  212. pj_str_t hangup_reason; /**< Hangup reason. */
  213. pjsua_msg_data *hangup_msg_data;/**< Hangup message data. */
  214. };
  215. /**
  216. * Server presence subscription list head.
  217. */
  218. struct pjsua_srv_pres
  219. {
  220. PJ_DECL_LIST_MEMBER(struct pjsua_srv_pres);
  221. pjsip_evsub *sub; /**< The evsub. */
  222. char *remote; /**< Remote URI. */
  223. int acc_id; /**< Account ID. */
  224. pjsip_dialog *dlg; /**< Dialog. */
  225. unsigned expires; /**< "expires" value in the request,
  226. PJSIP_EXPIRES_NOT_SPECIFIED
  227. if not present. */
  228. };
  229. /**
  230. * Account
  231. */
  232. typedef struct pjsua_acc
  233. {
  234. pj_pool_t *pool; /**< Pool for this account. */
  235. pjsua_acc_config cfg; /**< Account configuration. */
  236. pj_bool_t valid; /**< Is this account valid? */
  237. int index; /**< Index in accounts array. */
  238. pj_str_t display; /**< Display name, if any. */
  239. pj_str_t user_part; /**< User part of local URI. */
  240. pj_bool_t is_sips; /**< Local URI uses "sips"? */
  241. pj_str_t contact; /**< Our Contact header. */
  242. pj_str_t reg_contact; /**< Contact header for REGISTER.
  243. It may be different than acc
  244. contact if outbound is used */
  245. pj_bool_t contact_rewritten;
  246. /**< Contact rewrite has been done? */
  247. pjsip_host_port via_addr; /**< Address for Via header */
  248. pjsip_transport *via_tp; /**< Transport associated with
  249. the Via address */
  250. pj_str_t srv_domain; /**< Host part of reg server. */
  251. int srv_port; /**< Port number of reg server. */
  252. pjsip_regc *regc; /**< Client registration session. */
  253. pj_status_t reg_last_err; /**< Last registration error. */
  254. int reg_last_code; /**< Last status last register. */
  255. pj_str_t reg_mapped_addr;/**< Our addr as seen by reg srv.
  256. Only if allow_sdp_nat_rewrite
  257. is set */
  258. struct {
  259. pj_bool_t active; /**< Flag of reregister status. */
  260. pj_timer_entry timer; /**< Timer for reregistration. */
  261. void *reg_tp; /**< Transport for registration. */
  262. unsigned attempt_cnt; /**< Attempt counter. */
  263. } auto_rereg; /**< Reregister/reconnect data. */
  264. pj_timer_entry ka_timer; /**< Keep-alive timer for UDP. */
  265. pjsip_transport *ka_transport; /**< Transport for keep-alive. */
  266. pj_sockaddr ka_target; /**< Destination address for K-A */
  267. unsigned ka_target_len; /**< Length of ka_target. */
  268. pjsip_route_hdr route_set; /**< Complete route set inc. outbnd.*/
  269. pj_uint32_t global_route_crc; /** CRC of global route setting. */
  270. pj_uint32_t local_route_crc; /** CRC of account route setting.*/
  271. unsigned rfc5626_status;/**< SIP outbound status:
  272. 0: not used
  273. 1: requested
  274. 2: acknowledged by servers */
  275. pj_str_t rfc5626_instprm;/**< SIP outbound instance param. */
  276. pj_str_t rfc5626_regprm;/**< SIP outbound reg param. */
  277. unsigned rfc5626_flowtmr;/**< SIP outbound flow timer. */
  278. unsigned cred_cnt; /**< Number of credentials. */
  279. pjsip_cred_info cred[PJSUA_ACC_MAX_PROXIES]; /**< Complete creds. */
  280. pj_bool_t online_status; /**< Our online status. */
  281. pjrpid_element rpid; /**< RPID element information. */
  282. pjsua_srv_pres pres_srv_list; /**< Server subscription list. */
  283. pjsip_publishc *publish_sess; /**< Client publication session. */
  284. pj_bool_t publish_state; /**< Last published online status */
  285. pjsip_evsub *mwi_sub; /**< MWI client subscription */
  286. pjsip_dialog *mwi_dlg; /**< Dialog for MWI sub. */
  287. pj_uint16_t next_rtp_port; /**< Next RTP port to be used. */
  288. pjsip_transport_type_e tp_type; /**< Transport type (for local acc or
  289. transport binding) */
  290. pjsua_ip_change_op ip_change_op;/**< IP change process progress. */
  291. } pjsua_acc;
  292. /**
  293. *Transport.
  294. */
  295. typedef struct pjsua_transport_data
  296. {
  297. int index;
  298. pjsip_transport_type_e type;
  299. pjsip_host_port local_name;
  300. pj_sockaddr pub_addr;
  301. union {
  302. pjsip_transport *tp;
  303. pjsip_tpfactory *factory;
  304. void *ptr;
  305. } data;
  306. pj_bool_t is_restarting;
  307. pj_status_t restart_status;
  308. pj_bool_t has_cfg_addr;
  309. } pjsua_transport_data;
  310. /** Maximum length of subscription termination reason. */
  311. #define PJSUA_BUDDY_SUB_TERM_REASON_LEN 32
  312. /**
  313. * Buddy data.
  314. */
  315. typedef struct pjsua_buddy
  316. {
  317. pj_pool_t *pool; /**< Pool for this buddy. */
  318. unsigned index; /**< Buddy index. */
  319. void *user_data; /**< Application data. */
  320. pj_str_t uri; /**< Buddy URI. */
  321. pj_str_t contact; /**< Contact learned from subscrp. */
  322. pj_str_t name; /**< Buddy name. */
  323. pj_str_t display; /**< Buddy display name. */
  324. pj_str_t host; /**< Buddy host. */
  325. unsigned port; /**< Buddy port. */
  326. pj_bool_t monitor; /**< Should we monitor? */
  327. pjsip_dialog *dlg; /**< The underlying dialog. */
  328. pjsip_evsub *sub; /**< Buddy subscription */
  329. pj_bool_t presence; /**< Presence subscription? */
  330. unsigned term_code; /**< Subscription termination code */
  331. pj_str_t term_reason;/**< Subscription termination reason */
  332. pjsip_pres_status status; /**< Buddy presence status. */
  333. pjsip_dlg_event_status dlg_ev_status;/**< Buddy dialog event status */
  334. pj_timer_entry timer; /**< Resubscription timer */
  335. } pjsua_buddy;
  336. /**
  337. * File player/recorder data.
  338. */
  339. typedef struct pjsua_file_data
  340. {
  341. pj_bool_t type; /* 0=player, 1=playlist */
  342. pjmedia_port *port;
  343. pj_pool_t *pool;
  344. unsigned slot;
  345. } pjsua_file_data;
  346. /**
  347. * Additional parameters for conference bridge.
  348. */
  349. typedef struct pjsua_conf_setting
  350. {
  351. unsigned channel_count;
  352. unsigned samples_per_frame;
  353. unsigned bits_per_sample;
  354. } pjsua_conf_setting;
  355. typedef struct pjsua_stun_resolve
  356. {
  357. PJ_DECL_LIST_MEMBER(struct pjsua_stun_resolve);
  358. pj_pool_t *pool; /**< Pool */
  359. int ref_cnt; /**< Reference count */
  360. pj_bool_t destroy_flag; /**< To be destroyed */
  361. pj_bool_t has_result;
  362. unsigned count; /**< # of entries */
  363. pj_str_t *srv; /**< Array of entries */
  364. unsigned idx; /**< Current index */
  365. void *token; /**< App token */
  366. pj_stun_resolve_cb cb; /**< App callback */
  367. pj_bool_t blocking; /**< Blocking? */
  368. pj_thread_t *waiter; /**< Waiting thread */
  369. pj_timer_entry timer; /**< Destroy timer */
  370. pj_status_t status; /**< Session status */
  371. pj_sockaddr addr; /**< Result */
  372. pj_stun_sock *stun_sock; /**< Testing STUN sock */
  373. int af; /**< Address family */
  374. pj_bool_t async_wait;/**< Async resolution
  375. of STUN entry */
  376. } pjsua_stun_resolve;
  377. /* See also pjsua_vid_win_type_name() */
  378. typedef enum pjsua_vid_win_type
  379. {
  380. PJSUA_WND_TYPE_NONE,
  381. PJSUA_WND_TYPE_PREVIEW,
  382. PJSUA_WND_TYPE_STREAM
  383. } pjsua_vid_win_type;
  384. typedef struct pjsua_vid_win
  385. {
  386. pjsua_vid_win_type type; /**< Type. */
  387. pj_pool_t *pool; /**< Own pool. */
  388. unsigned ref_cnt; /**< Reference counter. */
  389. pjmedia_vid_port *vp_cap; /**< Capture vidport. */
  390. pjmedia_vid_port *vp_rend; /**< Renderer vidport */
  391. pjsua_conf_port_id cap_slot; /**< Capturer conf slot */
  392. pjsua_conf_port_id rend_slot; /**< Renderer conf slot */
  393. pjmedia_vid_dev_index preview_cap_id;/**< Capture dev id */
  394. pj_bool_t preview_running;/**< Preview is started*/
  395. pj_bool_t is_native; /**< Preview is by dev */
  396. } pjsua_vid_win;
  397. typedef struct pjsua_timer_list
  398. {
  399. PJ_DECL_LIST_MEMBER(struct pjsua_timer_list);
  400. pj_timer_entry entry;
  401. void (*cb)(void *user_data);
  402. void *user_data;
  403. } pjsua_timer_list;
  404. typedef struct pjsua_event_list
  405. {
  406. PJ_DECL_LIST_MEMBER(struct pjsua_event_list);
  407. pjmedia_event event;
  408. pjsua_call_id call_id;
  409. unsigned med_idx;
  410. } pjsua_event_list;
  411. /**
  412. * Global pjsua application data.
  413. */
  414. struct pjsua_data
  415. {
  416. /* Control: */
  417. pj_caching_pool cp; /**< Global pool factory. */
  418. pj_pool_t *pool; /**< pjsua's private pool. */
  419. pj_pool_t *timer_pool;/**< pjsua's timer pool. */
  420. pj_mutex_t *mutex; /**< Mutex protection for this data */
  421. unsigned mutex_nesting_level; /**< Mutex nesting level. */
  422. pj_thread_t *mutex_owner; /**< Mutex owner. */
  423. pjsua_state state; /**< Library state. */
  424. /* Logging: */
  425. pjsua_logging_config log_cfg; /**< Current logging config. */
  426. pj_oshandle_t log_file; /**<Output log file handle */
  427. /* SIP: */
  428. pjsip_endpoint *endpt; /**< Global endpoint. */
  429. pjsip_module mod; /**< pjsua's PJSIP module. */
  430. pjsua_transport_data tpdata[8]; /**< Array of transports. */
  431. pjsip_tp_state_callback old_tp_cb; /**< Old transport callback. */
  432. /* Threading: */
  433. pj_bool_t thread_quit_flag; /**< Thread quit flag. */
  434. pj_thread_t *thread[4]; /**< Array of threads. */
  435. /* STUN and resolver */
  436. pj_stun_config stun_cfg; /**< Global STUN settings. */
  437. pj_sockaddr stun_srv; /**< Resolved STUN server address */
  438. pj_status_t stun_status; /**< STUN server status. */
  439. pjsua_stun_resolve stun_res; /**< List of pending STUN resolution*/
  440. unsigned stun_srv_idx; /**< Resolved STUN server index */
  441. unsigned stun_opt; /**< STUN resolution option. */
  442. pj_dns_resolver *resolver; /**< DNS resolver. */
  443. /* UPnP */
  444. pj_status_t upnp_status; /**< UPnP status. */
  445. /* Detected NAT type */
  446. pj_stun_nat_type nat_type; /**< NAT type. */
  447. pj_status_t nat_status; /**< Detection status. */
  448. pj_bool_t nat_in_progress; /**< Detection in progress */
  449. /* List of outbound proxies: */
  450. pjsip_route_hdr outbound_proxy;
  451. /* Account: */
  452. unsigned acc_cnt; /**< Number of accounts. */
  453. pjsua_acc_id default_acc; /**< Default account ID */
  454. pjsua_acc acc[PJSUA_MAX_ACC]; /**< Account array. */
  455. pjsua_acc_id acc_ids[PJSUA_MAX_ACC]; /**< Acc sorted by prio*/
  456. /* Calls: */
  457. pjsua_config ua_cfg; /**< UA config. */
  458. unsigned call_cnt; /**< Call counter. */
  459. pjsua_call calls[PJSUA_MAX_CALLS];/**< Calls array. */
  460. pjsua_call_id next_call_id; /**< Next call id to use*/
  461. /* Buddy; */
  462. unsigned buddy_cnt; /**< Buddy count. */
  463. pjsua_buddy buddy[PJSUA_MAX_BUDDIES]; /**< Buddy array. */
  464. /* Presence: */
  465. pj_timer_entry pres_timer;/**< Presence refresh timer. */
  466. /* Media: */
  467. pjsua_media_config media_cfg; /**< Media config. */
  468. pjmedia_endpt *med_endpt; /**< Media endpoint. */
  469. pjsua_conf_setting mconf_cfg; /**< Additionan conf. bridge. param */
  470. pjmedia_conf *mconf; /**< Conference bridge. */
  471. pj_bool_t is_mswitch;/**< Are we using audio switchboard
  472. (a.k.a APS-Direct) */
  473. /* Sound device */
  474. pjmedia_aud_dev_index cap_dev; /**< Capture device ID. */
  475. pjmedia_aud_dev_index play_dev; /**< Playback device ID. */
  476. pj_uint32_t aud_svmask;/**< Which settings to save */
  477. pjmedia_aud_param aud_param; /**< User settings to sound dev */
  478. pj_bool_t aud_open_cnt;/**< How many # device is opened */
  479. pj_bool_t no_snd; /**< No sound (app will manage it) */
  480. pj_pool_t *snd_pool; /**< Sound's private pool. */
  481. pjmedia_snd_port *snd_port; /**< Sound port. */
  482. pj_timer_entry snd_idle_timer;/**< Sound device idle timer. */
  483. pjmedia_master_port *null_snd; /**< Master port for null sound. */
  484. pjmedia_port *null_port; /**< Null port. */
  485. pj_bool_t snd_is_on; /**< Media flow is currently active */
  486. unsigned snd_mode; /**< Sound device mode. */
  487. /* Video device */
  488. pjmedia_vid_dev_index vcap_dev; /**< Capture device ID. */
  489. pjmedia_vid_dev_index vrdr_dev; /**< Playback device ID. */
  490. /* For keeping video device settings */
  491. #if PJSUA_HAS_VIDEO
  492. pjmedia_vid_conf *vid_conf;
  493. pj_uint32_t vid_caps[PJMEDIA_VID_DEV_MAX_DEVS];
  494. pjmedia_vid_dev_param vid_param[PJMEDIA_VID_DEV_MAX_DEVS];
  495. #endif
  496. /* File players: */
  497. unsigned player_cnt;/**< Number of file players. */
  498. pjsua_file_data player[PJSUA_MAX_PLAYERS];/**< Array of players.*/
  499. /* File recorders: */
  500. unsigned rec_cnt; /**< Number of file recorders. */
  501. pjsua_file_data recorder[PJSUA_MAX_RECORDERS];/**< Array of recs.*/
  502. /* Video windows */
  503. #if PJSUA_HAS_VIDEO
  504. pjsua_vid_win win[PJSUA_MAX_VID_WINS]; /**< Array of windows */
  505. #endif
  506. /* Timer entry and event list */
  507. pjsua_timer_list active_timer_list;
  508. pjsua_timer_list timer_list;
  509. pjsua_event_list event_list;
  510. pj_mutex_t *timer_mutex;
  511. };
  512. extern struct pjsua_data pjsua_var;
  513. /**
  514. * Get the instance of pjsua
  515. */
  516. PJ_DECL(struct pjsua_data*) pjsua_get_var(void);
  517. /**
  518. * IM callback data.
  519. */
  520. typedef struct pjsua_im_data
  521. {
  522. pjsua_acc_id acc_id;
  523. pjsua_call_id call_id;
  524. pj_str_t to;
  525. pj_str_t body;
  526. void *user_data;
  527. } pjsua_im_data;
  528. pj_status_t pjsua_media_apply_xml_control(pjsua_call_id call_id,
  529. const pj_str_t *xml_st);
  530. /**
  531. * Duplicate IM data.
  532. */
  533. PJ_INLINE(pjsua_im_data*) pjsua_im_data_dup(pj_pool_t *pool,
  534. const pjsua_im_data *src)
  535. {
  536. pjsua_im_data *dst;
  537. dst = (pjsua_im_data*) pj_pool_alloc(pool, sizeof(*dst));
  538. dst->acc_id = src->acc_id;
  539. dst->call_id = src->call_id;
  540. pj_strdup_with_null(pool, &dst->to, &src->to);
  541. dst->user_data = src->user_data;
  542. pj_strdup_with_null(pool, &dst->body, &src->body);
  543. return dst;
  544. }
  545. #if 1
  546. PJ_INLINE(void) PJSUA_LOCK()
  547. {
  548. pj_mutex_lock(pjsua_var.mutex);
  549. pjsua_var.mutex_owner = pj_thread_this();
  550. ++pjsua_var.mutex_nesting_level;
  551. }
  552. PJ_INLINE(void) PJSUA_UNLOCK()
  553. {
  554. if (--pjsua_var.mutex_nesting_level == 0)
  555. pjsua_var.mutex_owner = NULL;
  556. pj_mutex_unlock(pjsua_var.mutex);
  557. }
  558. PJ_INLINE(pj_status_t) PJSUA_TRY_LOCK()
  559. {
  560. pj_status_t status;
  561. status = pj_mutex_trylock(pjsua_var.mutex);
  562. if (status == PJ_SUCCESS) {
  563. pjsua_var.mutex_owner = pj_thread_this();
  564. ++pjsua_var.mutex_nesting_level;
  565. }
  566. return status;
  567. }
  568. PJ_INLINE(pj_bool_t) PJSUA_LOCK_IS_LOCKED()
  569. {
  570. return pjsua_var.mutex_owner == pj_thread_this();
  571. }
  572. /* Release all locks currently held by this thread. */
  573. PJ_INLINE(unsigned) PJSUA_RELEASE_LOCK()
  574. {
  575. unsigned num_locks = 0;
  576. while (PJSUA_LOCK_IS_LOCKED()) {
  577. num_locks++;
  578. PJSUA_UNLOCK();
  579. }
  580. return num_locks;
  581. }
  582. /* Re-acquire all the locks released by PJSUA_RELEASE_LOCK(). */
  583. PJ_INLINE(void) PJSUA_RELOCK(unsigned num_locks)
  584. {
  585. for (; num_locks > 0; num_locks--)
  586. PJSUA_LOCK();
  587. }
  588. #else
  589. #define PJSUA_LOCK()
  590. #define PJSUA_TRY_LOCK() PJ_SUCCESS
  591. #define PJSUA_UNLOCK()
  592. #define PJSUA_LOCK_IS_LOCKED() PJ_TRUE
  593. #endif
  594. /* Core */
  595. void pjsua_set_state(pjsua_state new_state);
  596. /******
  597. * STUN resolution
  598. */
  599. /* Resolve the STUN server */
  600. pj_status_t resolve_stun_server(pj_bool_t wait, pj_bool_t retry_if_cur_error,
  601. unsigned options);
  602. /**
  603. * Normalize route URI (check for ";lr" and append one if it doesn't
  604. * exist and pjsua_config.force_lr is set.
  605. */
  606. pj_status_t normalize_route_uri(pj_pool_t *pool, pj_str_t *uri);
  607. /* acc use stun? */
  608. pj_bool_t pjsua_sip_acc_is_using_stun(pjsua_acc_id acc_id);
  609. pj_bool_t pjsua_media_acc_is_using_stun(pjsua_acc_id acc_id);
  610. /* acc use UPnP? */
  611. pj_bool_t pjsua_sip_acc_is_using_upnp(pjsua_acc_id acc_id);
  612. pj_bool_t pjsua_media_acc_is_using_upnp(pjsua_acc_id acc_id);
  613. /* acc use IPv6? */
  614. pj_bool_t pjsua_sip_acc_is_using_ipv6(pjsua_acc_id acc_id);
  615. /* Get local transport address suitable to be used for Via or Contact address
  616. * to send request to the specified destination URI.
  617. */
  618. pj_status_t pjsua_acc_get_uac_addr(pjsua_acc_id acc_id,
  619. pj_pool_t *pool,
  620. const pj_str_t *dst_uri,
  621. pjsip_host_port *addr,
  622. pjsip_transport_type_e *p_tp_type,
  623. int *p_secure,
  624. const void **p_tp);
  625. /**
  626. * Handle incoming invite request.
  627. */
  628. pj_bool_t pjsua_call_on_incoming(pjsip_rx_data *rdata);
  629. /*
  630. * Media channel.
  631. */
  632. pj_status_t pjsua_media_channel_init(pjsua_call_id call_id,
  633. pjsip_role_e role,
  634. int security_level,
  635. pj_pool_t *tmp_pool,
  636. const pjmedia_sdp_session *rem_sdp,
  637. int *sip_err_code,
  638. pj_bool_t async,
  639. pjsua_med_tp_state_cb cb);
  640. pj_status_t pjsua_media_channel_create_sdp(pjsua_call_id call_id,
  641. pj_pool_t *pool,
  642. const pjmedia_sdp_session *rem_sdp,
  643. pjmedia_sdp_session **p_sdp,
  644. int *sip_err_code);
  645. pj_status_t pjsua_media_channel_update(pjsua_call_id call_id,
  646. const pjmedia_sdp_session *local_sdp,
  647. const pjmedia_sdp_session *remote_sdp);
  648. pj_status_t pjsua_media_channel_deinit(pjsua_call_id call_id);
  649. void pjsua_ice_check_start_trickling(pjsua_call *call,
  650. pj_bool_t forceful,
  651. pjsip_event *e);
  652. /*
  653. * Error message when media operation is requested while another is in progress
  654. */
  655. #define ERR_MEDIA_CHANGING " because another media operation is in progress"
  656. pj_bool_t pjsua_call_media_is_changing(pjsua_call *call);
  657. pj_status_t pjsua_call_media_init(pjsua_call_media *call_med,
  658. pjmedia_type type,
  659. const pjmedia_sdp_session *rem_sdp,
  660. const pjsua_transport_config *tcfg,
  661. int security_level,
  662. int *sip_err_code,
  663. pj_bool_t async,
  664. pjsua_med_tp_state_cb cb);
  665. void pjsua_call_cleanup_flag(pjsua_call_setting *opt);
  666. void pjsua_set_media_tp_state(pjsua_call_media *call_med, pjsua_med_tp_st tp_st);
  667. void pjsua_media_prov_clean_up(pjsua_call_id call_id);
  668. void pjsua_media_prov_revert(pjsua_call_id call_id);
  669. /* Callback to receive media events */
  670. pj_status_t on_media_event(pjmedia_event *event, void *user_data);
  671. void call_med_event_cb(void *user_data);
  672. pj_status_t call_media_on_event(pjmedia_event *event,
  673. void *user_data);
  674. /**
  675. * Init presence.
  676. */
  677. pj_status_t pjsua_pres_init(void);
  678. /*
  679. * Start presence subsystem.
  680. */
  681. pj_status_t pjsua_pres_start(void);
  682. /**
  683. * Refresh presence subscriptions
  684. */
  685. void pjsua_pres_refresh(void);
  686. /*
  687. * Update server subscription (e.g. when our online status has changed)
  688. */
  689. void pjsua_pres_update_acc(int acc_id, pj_bool_t force);
  690. /*
  691. * Shutdown presence.
  692. */
  693. void pjsua_pres_shutdown(unsigned flags);
  694. /**
  695. * Init presence for aoocunt.
  696. */
  697. pj_status_t pjsua_pres_init_acc(int acc_id);
  698. /**
  699. * Send PUBLISH
  700. */
  701. pj_status_t pjsua_pres_init_publish_acc(int acc_id);
  702. /**
  703. * Send un-PUBLISH
  704. */
  705. void pjsua_pres_unpublish(pjsua_acc *acc, unsigned flags);
  706. /**
  707. * Terminate server subscription for the account
  708. */
  709. void pjsua_pres_delete_acc(int acc_id, unsigned flags);
  710. /**
  711. * Init IM module handler to handle incoming MESSAGE outside dialog.
  712. */
  713. pj_status_t pjsua_im_init(void);
  714. /**
  715. * Start MWI subscription
  716. */
  717. pj_status_t pjsua_start_mwi(pjsua_acc_id acc_id, pj_bool_t force_renew);
  718. /**
  719. * Init call subsystem.
  720. */
  721. pj_status_t pjsua_call_subsys_init(const pjsua_config *cfg);
  722. /**
  723. * Start call subsystem.
  724. */
  725. pj_status_t pjsua_call_subsys_start(void);
  726. /**
  727. * Init media subsystems.
  728. */
  729. pj_status_t pjsua_media_subsys_init(const pjsua_media_config *cfg);
  730. /**
  731. * Start pjsua media subsystem.
  732. */
  733. pj_status_t pjsua_media_subsys_start(void);
  734. /**
  735. * Destroy pjsua media subsystem.
  736. */
  737. pj_status_t pjsua_media_subsys_destroy(unsigned flags);
  738. /**
  739. * Private: check if we can accept the message.
  740. * If not, then p_accept header will be filled with a valid
  741. * Accept header.
  742. */
  743. pj_bool_t pjsua_im_accept_pager(pjsip_rx_data *rdata,
  744. pjsip_accept_hdr **p_accept_hdr);
  745. /**
  746. * Private: process pager message.
  747. * This may trigger pjsua_ui_on_pager() or pjsua_ui_on_typing().
  748. */
  749. void pjsua_im_process_pager(int call_id, const pj_str_t *from,
  750. const pj_str_t *to, pjsip_rx_data *rdata);
  751. /**
  752. * Create Accept header for MESSAGE.
  753. */
  754. pjsip_accept_hdr* pjsua_im_create_accept(pj_pool_t *pool);
  755. /*
  756. * Add additional headers etc in msg_data specified by application
  757. * when sending requests.
  758. */
  759. void pjsua_process_msg_data(pjsip_tx_data *tdata,
  760. const pjsua_msg_data *msg_data);
  761. /*
  762. * Add route_set to outgoing requests
  763. */
  764. void pjsua_set_msg_route_set( pjsip_tx_data *tdata,
  765. const pjsip_route_hdr *route_set );
  766. /*
  767. * Simple version of MIME type parsing (it doesn't support parameters)
  768. */
  769. void pjsua_parse_media_type( pj_pool_t *pool,
  770. const pj_str_t *mime,
  771. pjsip_media_type *media_type);
  772. /*
  773. * Internal function to init transport selector from transport id.
  774. */
  775. void pjsua_init_tpselector(pjsua_transport_id tp_id,
  776. pjsip_tpselector *sel);
  777. pjsip_dialog* on_dlg_forked(pjsip_dialog *first_set, pjsip_rx_data *res);
  778. pj_status_t acquire_call(const char *title,
  779. pjsua_call_id call_id,
  780. pjsua_call **p_call,
  781. pjsip_dialog **p_dlg);
  782. const char *good_number(char *buf, unsigned buf_size, pj_int32_t val);
  783. void print_call(const char *title,
  784. int call_id,
  785. char *buf, pj_size_t size);
  786. /*
  787. * Audio
  788. */
  789. pj_status_t pjsua_aud_subsys_init(void);
  790. pj_status_t pjsua_aud_subsys_start(void);
  791. pj_status_t pjsua_aud_subsys_destroy(void);
  792. void pjsua_aud_stop_stream(pjsua_call_media *call_med);
  793. pj_status_t pjsua_aud_channel_update(pjsua_call_media *call_med,
  794. pj_pool_t *tmp_pool,
  795. pjmedia_stream_info *si,
  796. const pjmedia_sdp_session *local_sdp,
  797. const pjmedia_sdp_session *remote_sdp);
  798. void pjsua_check_snd_dev_idle(void);
  799. /*
  800. * Video
  801. */
  802. pj_status_t pjsua_vid_subsys_init(void);
  803. pj_status_t pjsua_vid_subsys_start(void);
  804. pj_status_t pjsua_vid_subsys_destroy(void);
  805. void pjsua_vid_stop_stream(pjsua_call_media *call_med);
  806. pj_status_t pjsua_vid_channel_init(pjsua_call_media *call_med);
  807. pj_status_t pjsua_vid_channel_update(pjsua_call_media *call_med,
  808. pj_pool_t *tmp_pool,
  809. pjmedia_vid_stream_info *si,
  810. const pjmedia_sdp_session *local_sdp,
  811. const pjmedia_sdp_session *remote_sdp);
  812. #if PJSUA_HAS_VIDEO
  813. void pjsua_vid_win_reset(pjsua_vid_win_id wid);
  814. #else
  815. # define pjsua_vid_win_reset(wid)
  816. #endif
  817. /*
  818. * Schedule check for the need of re-INVITE/UPDATE after media update
  819. */
  820. void pjsua_call_schedule_reinvite_check(pjsua_call *call, unsigned delay_ms);
  821. /*
  822. * Update contact per account on IP change process.
  823. */
  824. pj_status_t pjsua_acc_update_contact_on_ip_change(pjsua_acc *acc);
  825. /*
  826. * Call handling per account on IP change process.
  827. */
  828. pj_status_t pjsua_acc_handle_call_on_ip_change(pjsua_acc *acc);
  829. /*
  830. * End IP change process per account.
  831. */
  832. void pjsua_acc_end_ip_change(pjsua_acc *acc);
  833. PJ_END_DECL
  834. #endif /* __PJSUA_INTERNAL_H__ */