blob: b3aeb11bcb98e6b60aad8a0cde9a118b09731e70 [file] [log] [blame]
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001/* $Id$ */
2/*
Benny Prijonoa771a512007-02-19 01:13:53 +00003 * Copyright (C) 2003-2007 Benny Prijono <benny@prijono.org>
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004 *
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/**
23 * This is the private header used by pjsua library implementation.
24 * Applications should not include this file.
25 */
26
27PJ_BEGIN_DECL
28
29/**
Benny Prijono224b4e22008-06-19 14:10:28 +000030 * Media transport state.
31 */
32typedef enum pjsua_med_tp_st
33{
34 /** Not initialized */
35 PJSUA_MED_TP_IDLE,
36
37 /** Initialized (media_create() has been called) */
38 PJSUA_MED_TP_INIT,
39
40 /** Running (media_start() has been called) */
41 PJSUA_MED_TP_RUNNING
42
43} pjsua_med_tp_st;
44
45/**
Benny Prijonoeebe9af2006-06-13 22:57:13 +000046 * Structure to be attached to invite dialog.
47 * Given a dialog "dlg", application can retrieve this structure
48 * by accessing dlg->mod_data[pjsua.mod.id].
49 */
50typedef struct pjsua_call
51{
52 unsigned index; /**< Index in pjsua array. */
53 pjsip_inv_session *inv; /**< The invite session. */
54 void *user_data; /**< User/application data. */
55 pjsip_status_code last_code; /**< Last status code seen. */
56 pj_str_t last_text; /**< Last status text seen. */
57 pj_time_val start_time;/**< First INVITE sent/received. */
58 pj_time_val res_time; /**< First response sent/received. */
59 pj_time_val conn_time; /**< Connected/confirmed time. */
60 pj_time_val dis_time; /**< Disconnect time. */
61 pjsua_acc_id acc_id; /**< Account index being used. */
Benny Prijonodb844a42008-02-02 17:07:18 +000062 int secure_level;/**< Signaling security level. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +000063 pjsua_call_media_status media_st;/**< Media state. */
64 pjmedia_dir media_dir; /**< Media direction. */
65 pjmedia_session *session; /**< The media session. */
Benny Prijono8147f402007-11-21 14:50:07 +000066 pj_uint32_t ssrc; /**< RTP SSRC */
Benny Prijonoeebe9af2006-06-13 22:57:13 +000067 int conf_slot; /**< Slot # in conference bridge. */
68 pjsip_evsub *xfer_sub; /**< Xfer server subscription, if this
69 call was triggered by xfer. */
Benny Prijonod8179652008-01-23 20:39:07 +000070 pjmedia_transport *med_tp; /**< Current media transport. */
Benny Prijono224b4e22008-06-19 14:10:28 +000071 pj_status_t med_tp_ready;/**< Media transport status. */
Benny Prijonod8179652008-01-23 20:39:07 +000072 pjmedia_transport *med_orig; /**< Original media transport */
Benny Prijono224b4e22008-06-19 14:10:28 +000073 pjsua_med_tp_st med_tp_st; /**< Media transport state */
Benny Prijonoeebe9af2006-06-13 22:57:13 +000074 pj_timer_entry refresh_tm;/**< Timer to send re-INVITE. */
75 pj_timer_entry hangup_tm; /**< Timer to hangup call. */
Benny Prijono91a6a172007-10-31 08:59:29 +000076 pj_stun_nat_type rem_nat_type; /**< NAT type of remote endpoint. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +000077
78 char last_text_buf_[128]; /**< Buffer for last_text. */
79
80} pjsua_call;
81
82
83/**
84 * Server presence subscription list head.
85 */
86typedef struct pjsua_srv_pres
87{
88 PJ_DECL_LIST_MEMBER(struct pjsua_srv_pres);
89 pjsip_evsub *sub;
90 char *remote;
91} pjsua_srv_pres;
92
93
94/**
95 * Account
96 */
97typedef struct pjsua_acc
98{
99 pjsua_acc_config cfg; /**< Account configuration. */
100 pj_bool_t valid; /**< Is this account valid? */
101
102 int index; /**< Index in accounts array. */
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000103 pj_str_t display; /**< Display name, if any. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000104 pj_str_t user_part; /**< User part of local URI. */
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000105 pj_str_t contact; /**< Our Contact URI for REGISTER */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000106
Benny Prijonob4a17c92006-07-10 14:40:21 +0000107 pj_str_t srv_domain; /**< Host part of reg server. */
108 int srv_port; /**< Port number of reg server. */
109
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000110 pjsip_regc *regc; /**< Client registration session. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000111 pj_status_t reg_last_err; /**< Last registration error. */
112 int reg_last_code; /**< Last status last register. */
113
Benny Prijonobddef2c2007-10-31 13:28:08 +0000114 pj_timer_entry ka_timer; /**< Keep-alive timer for UDP. */
115 pjsip_transport *ka_transport; /**< Transport for keep-alive. */
116 pj_sockaddr ka_target; /**< Destination address for K-A */
117 unsigned ka_target_len; /**< Length of ka_target. */
118
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000119 pjsip_route_hdr route_set; /**< Complete route set inc. outbnd.*/
120
121 unsigned cred_cnt; /**< Number of credentials. */
122 pjsip_cred_info cred[PJSUA_ACC_MAX_PROXIES]; /**< Complete creds. */
123
124 pj_bool_t online_status; /**< Our online status. */
Benny Prijono4461c7d2007-08-25 13:36:15 +0000125 pjrpid_element rpid; /**< RPID element information. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000126 pjsua_srv_pres pres_srv_list; /**< Server subscription list. */
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000127 pjsip_publishc *publish_sess; /**< Client publication session. */
128 pj_bool_t publish_state; /**< Last published online status */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000129
130} pjsua_acc;
131
132
133/**
134 *Transport.
135 */
Benny Prijono62c5c5b2007-01-13 23:22:40 +0000136typedef struct pjsua_transport_data
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000137{
Benny Prijonoe93e2872006-06-28 16:46:49 +0000138 int index;
139 pjsip_transport_type_e type;
140 pjsip_host_port local_name;
141
142 union {
143 pjsip_transport *tp;
144 pjsip_tpfactory *factory;
145 void *ptr;
146 } data;
147
Benny Prijono62c5c5b2007-01-13 23:22:40 +0000148} pjsua_transport_data;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000149
150
151/**
152 * Buddy data.
153 */
154typedef struct pjsua_buddy
155{
156 unsigned index; /**< Buddy index. */
157 pj_str_t uri; /**< Buddy URI. */
158 pj_str_t contact; /**< Contact learned from subscrp. */
159 pj_str_t name; /**< Buddy name. */
160 pj_str_t display; /**< Buddy display name. */
161 pj_str_t host; /**< Buddy host. */
162 unsigned port; /**< Buddy port. */
163 pj_bool_t monitor; /**< Should we monitor? */
Benny Prijonof9c40c32007-06-28 07:20:26 +0000164 pjsip_dialog *dlg; /**< The underlying dialog. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000165 pjsip_evsub *sub; /**< Buddy presence subscription */
166 pjsip_pres_status status; /**< Buddy presence status. */
167
168} pjsua_buddy;
169
170
171/**
172 * File player/recorder data.
173 */
174typedef struct pjsua_file_data
175{
Benny Prijonoa66c3312007-01-21 23:12:40 +0000176 pj_bool_t type; /* 0=player, 1=playlist */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000177 pjmedia_port *port;
Benny Prijonod5696da2007-07-17 16:25:45 +0000178 pj_pool_t *pool;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000179 unsigned slot;
180} pjsua_file_data;
181
182
183/**
184 * Additional parameters for conference bridge.
185 */
186typedef struct pjsua_conf_setting
187{
188 unsigned channel_count;
189 unsigned samples_per_frame;
190 unsigned bits_per_sample;
191} pjsua_conf_setting;
192
193
194/**
195 * Global pjsua application data.
196 */
197struct pjsua_data
198{
199
200 /* Control: */
201 pj_caching_pool cp; /**< Global pool factory. */
202 pj_pool_t *pool; /**< pjsua's private pool. */
203 pj_mutex_t *mutex; /**< Mutex protection for this data */
204
205 /* Logging: */
206 pjsua_logging_config log_cfg; /**< Current logging config. */
207 pj_oshandle_t log_file; /**<Output log file handle */
208
209 /* SIP: */
210 pjsip_endpoint *endpt; /**< Global endpoint. */
211 pjsip_module mod; /**< pjsua's PJSIP module. */
Benny Prijono62c5c5b2007-01-13 23:22:40 +0000212 pjsua_transport_data tpdata[8]; /**< Array of transports. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000213
214 /* Threading: */
215 pj_bool_t thread_quit_flag; /**< Thread quit flag. */
216 pj_thread_t *thread[4]; /**< Array of threads. */
217
Benny Prijonoc97608e2007-03-23 16:34:20 +0000218 /* STUN and resolver */
219 pj_stun_config stun_cfg; /**< Global STUN settings. */
220 pj_sockaddr stun_srv; /**< Resolved STUN server address */
221 pj_status_t stun_status; /**< STUN server status. */
222 pj_dns_resolver *resolver; /**< DNS resolver. */
223
Benny Prijono6ba8c542007-10-16 01:34:14 +0000224 /* Detected NAT type */
225 pj_stun_nat_type nat_type; /**< NAT type. */
226 pj_status_t nat_status; /**< Detection status. */
227 pj_bool_t nat_in_progress; /**< Detection in progress */
228
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000229 /* Account: */
230 unsigned acc_cnt; /**< Number of accounts. */
231 pjsua_acc_id default_acc; /**< Default account ID */
232 pjsua_acc acc[PJSUA_MAX_ACC]; /**< Account array. */
Benny Prijono093d3022006-09-24 00:07:11 +0000233 pjsua_acc_id acc_ids[PJSUA_MAX_ACC]; /**< Acc sorted by prio*/
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000234
235 /* Calls: */
236 pjsua_config ua_cfg; /**< UA config. */
237 unsigned call_cnt; /**< Call counter. */
238 pjsua_call calls[PJSUA_MAX_CALLS];/**< Calls array. */
Benny Prijono5773cd62008-01-19 13:01:42 +0000239 pjsua_call_id next_call_id; /**< Next call id to use*/
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000240
241 /* Buddy; */
242 unsigned buddy_cnt; /**< Buddy count. */
243 pjsua_buddy buddy[PJSUA_MAX_BUDDIES]; /**< Buddy array. */
244
Benny Prijono7a5f5102007-05-29 00:33:09 +0000245 /* Presence: */
246 pj_timer_entry pres_timer;/**< Presence refresh timer. */
247
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000248 /* Media: */
249 pjsua_media_config media_cfg; /**< Media config. */
250 pjmedia_endpt *med_endpt; /**< Media endpoint. */
251 pjsua_conf_setting mconf_cfg; /**< Additionan conf. bridge. param */
252 pjmedia_conf *mconf; /**< Conference bridge. */
253 int cap_dev; /**< Capture device ID. */
254 int play_dev; /**< Playback device ID. */
Benny Prijonoe909eac2006-07-27 22:04:56 +0000255 pj_bool_t no_snd; /**< No sound (app will manage it) */
Nanang Izzuddin68559c32008-06-13 17:01:46 +0000256 pj_pool_t *snd_pool; /**< Sound's private pool. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000257 pjmedia_snd_port *snd_port; /**< Sound port. */
Nanang Izzuddin68559c32008-06-13 17:01:46 +0000258 pj_timer_entry snd_idle_timer;/**< Sound device idle timer. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000259 pjmedia_master_port *null_snd; /**< Master port for null sound. */
260 pjmedia_port *null_port; /**< Null port. */
261
262
263 /* File players: */
264 unsigned player_cnt;/**< Number of file players. */
Benny Prijonocba59d92007-02-16 09:22:56 +0000265 pjsua_file_data player[PJSUA_MAX_PLAYERS];/**< Array of players.*/
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000266
267 /* File recorders: */
268 unsigned rec_cnt; /**< Number of file recorders. */
Benny Prijonocba59d92007-02-16 09:22:56 +0000269 pjsua_file_data recorder[PJSUA_MAX_RECORDERS];/**< Array of recs.*/
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000270};
271
272
273extern struct pjsua_data pjsua_var;
274
Benny Prijono44e88ea2007-10-30 15:42:35 +0000275/**
276 * Get the instance of pjsua
277 */
278PJ_DECL(struct pjsua_data*) pjsua_get_var(void);
279
280
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000281
282/**
283 * IM callback data.
284 */
285typedef struct pjsua_im_data
286{
287 pjsua_acc_id acc_id;
288 pjsua_call_id call_id;
289 pj_str_t to;
290 pj_str_t body;
291 void *user_data;
292} pjsua_im_data;
293
294
295/**
296 * Duplicate IM data.
297 */
298PJ_INLINE(pjsua_im_data*) pjsua_im_data_dup(pj_pool_t *pool,
299 const pjsua_im_data *src)
300{
301 pjsua_im_data *dst;
302
Benny Prijono07113c92006-11-21 08:38:00 +0000303 dst = (pjsua_im_data*) pj_pool_alloc(pool, sizeof(*dst));
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000304 dst->acc_id = src->acc_id;
305 dst->call_id = src->call_id;
306 pj_strdup_with_null(pool, &dst->to, &src->to);
307 dst->user_data = src->user_data;
308 pj_strdup_with_null(pool, &dst->body, &src->body);
309
310 return dst;
311}
312
313
Benny Prijono148c9dd2006-09-19 13:37:53 +0000314#if 1
315#define PJSUA_LOCK() pj_mutex_lock(pjsua_var.mutex)
316#define PJSUA_TRY_LOCK() pj_mutex_trylock(pjsua_var.mutex)
317#define PJSUA_UNLOCK() pj_mutex_unlock(pjsua_var.mutex)
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000318#else
319#define PJSUA_LOCK()
Benny Prijono148c9dd2006-09-19 13:37:53 +0000320#define PJSUA_TRY_LOCK() PJ_SUCCESS
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000321#define PJSUA_UNLOCK()
322#endif
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000323
324
Benny Prijonoc97608e2007-03-23 16:34:20 +0000325/**
326 * Resolve STUN server.
327 */
328pj_status_t pjsua_resolve_stun_server(pj_bool_t wait);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000329
330/**
331 * Handle incoming invite request.
332 */
333pj_bool_t pjsua_call_on_incoming(pjsip_rx_data *rdata);
334
Benny Prijonoc97608e2007-03-23 16:34:20 +0000335/*
336 * Media channel.
337 */
338pj_status_t pjsua_media_channel_init(pjsua_call_id call_id,
Benny Prijonod8179652008-01-23 20:39:07 +0000339 pjsip_role_e role,
Benny Prijono25b2ea12008-01-24 19:20:54 +0000340 int security_level,
Benny Prijono224b4e22008-06-19 14:10:28 +0000341 pj_pool_t *tmp_pool,
342 const pjmedia_sdp_session *rem_sdp,
Benny Prijono25b2ea12008-01-24 19:20:54 +0000343 int *sip_err_code);
Benny Prijonoc97608e2007-03-23 16:34:20 +0000344pj_status_t pjsua_media_channel_create_sdp(pjsua_call_id call_id,
345 pj_pool_t *pool,
Benny Prijonod8179652008-01-23 20:39:07 +0000346 const pjmedia_sdp_session *rem_sdp,
Benny Prijono25b2ea12008-01-24 19:20:54 +0000347 pjmedia_sdp_session **p_sdp,
348 int *sip_err_code);
Benny Prijonoc97608e2007-03-23 16:34:20 +0000349pj_status_t pjsua_media_channel_update(pjsua_call_id call_id,
Benny Prijono224b4e22008-06-19 14:10:28 +0000350 const pjmedia_sdp_session *local_sdp,
Benny Prijonodbce2cf2007-03-28 16:24:00 +0000351 const pjmedia_sdp_session *remote_sdp);
Benny Prijonoc97608e2007-03-23 16:34:20 +0000352pj_status_t pjsua_media_channel_deinit(pjsua_call_id call_id);
353
354
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000355/**
356 * Init presence.
357 */
358pj_status_t pjsua_pres_init();
359
360/*
361 * Start presence subsystem.
362 */
363pj_status_t pjsua_pres_start(void);
364
365/**
366 * Refresh presence subscriptions
367 */
368void pjsua_pres_refresh(void);
369
370/*
Benny Prijono4461c7d2007-08-25 13:36:15 +0000371 * Update server subscription (e.g. when our online status has changed)
372 */
373void pjsua_pres_update_acc(int acc_id, pj_bool_t force);
374
375/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000376 * Shutdown presence.
377 */
378void pjsua_pres_shutdown(void);
379
380/**
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000381 * Init presence for aoocunt.
382 */
383pj_status_t pjsua_pres_init_acc(int acc_id);
384
385/**
Benny Prijono8b6834f2007-02-24 13:29:22 +0000386 * Send PUBLISH
387 */
388pj_status_t pjsua_pres_init_publish_acc(int acc_id);
389
390/**
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000391 * Terminate server subscription for the account
392 */
393void pjsua_pres_delete_acc(int acc_id);
394
395/**
396 * Init IM module handler to handle incoming MESSAGE outside dialog.
397 */
398pj_status_t pjsua_im_init(void);
399
400/**
401 * Init call subsystem.
402 */
403pj_status_t pjsua_call_subsys_init(const pjsua_config *cfg);
404
405/**
406 * Start call subsystem.
407 */
408pj_status_t pjsua_call_subsys_start(void);
409
410/**
411 * Init media subsystems.
412 */
413pj_status_t pjsua_media_subsys_init(const pjsua_media_config *cfg);
414
415/**
416 * Start pjsua media subsystem.
417 */
418pj_status_t pjsua_media_subsys_start(void);
419
420/**
421 * Destroy pjsua media subsystem.
422 */
423pj_status_t pjsua_media_subsys_destroy(void);
424
425/**
426 * Private: check if we can accept the message.
427 * If not, then p_accept header will be filled with a valid
428 * Accept header.
429 */
430pj_bool_t pjsua_im_accept_pager(pjsip_rx_data *rdata,
431 pjsip_accept_hdr **p_accept_hdr);
432
433/**
434 * Private: process pager message.
435 * This may trigger pjsua_ui_on_pager() or pjsua_ui_on_typing().
436 */
437void pjsua_im_process_pager(int call_id, const pj_str_t *from,
438 const pj_str_t *to, pjsip_rx_data *rdata);
439
440
441/**
442 * Create Accept header for MESSAGE.
443 */
444pjsip_accept_hdr* pjsua_im_create_accept(pj_pool_t *pool);
445
446/*
447 * Add additional headers etc in msg_data specified by application
448 * when sending requests.
449 */
450void pjsua_process_msg_data(pjsip_tx_data *tdata,
451 const pjsua_msg_data *msg_data);
452
453
454/*
455 * Add route_set to outgoing requests
456 */
457void pjsua_set_msg_route_set( pjsip_tx_data *tdata,
458 const pjsip_route_hdr *route_set );
459
460
461/*
462 * Simple version of MIME type parsing (it doesn't support parameters)
463 */
464void pjsua_parse_media_type( pj_pool_t *pool,
465 const pj_str_t *mime,
466 pjsip_media_type *media_type);
467
468
Benny Prijono62c5c5b2007-01-13 23:22:40 +0000469/*
470 * Internal function to init transport selector from transport id.
471 */
472void pjsua_init_tpselector(pjsua_transport_id tp_id,
473 pjsip_tpselector *sel);
474
475
Benny Prijono627cbb42007-09-25 20:48:49 +0000476pj_status_t acquire_call(const char *title,
477 pjsua_call_id call_id,
478 pjsua_call **p_call,
479 pjsip_dialog **p_dlg);
480const char *good_number(char *buf, pj_int32_t val);
481void print_call(const char *title,
482 int call_id,
483 char *buf, pj_size_t size);
484
Benny Prijono62c5c5b2007-01-13 23:22:40 +0000485
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000486PJ_END_DECL
487
488#endif /* __PJSUA_INTERNAL_H__ */
489