blob: 004c1ca13f645b9dc3111c91b25a7aebfd2a72db [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/**
30 * Structure to be attached to invite dialog.
31 * Given a dialog "dlg", application can retrieve this structure
32 * by accessing dlg->mod_data[pjsua.mod.id].
33 */
34typedef struct pjsua_call
35{
36 unsigned index; /**< Index in pjsua array. */
37 pjsip_inv_session *inv; /**< The invite session. */
38 void *user_data; /**< User/application data. */
39 pjsip_status_code last_code; /**< Last status code seen. */
40 pj_str_t last_text; /**< Last status text seen. */
41 pj_time_val start_time;/**< First INVITE sent/received. */
42 pj_time_val res_time; /**< First response sent/received. */
43 pj_time_val conn_time; /**< Connected/confirmed time. */
44 pj_time_val dis_time; /**< Disconnect time. */
45 pjsua_acc_id acc_id; /**< Account index being used. */
46 pjsua_call_media_status media_st;/**< Media state. */
47 pjmedia_dir media_dir; /**< Media direction. */
48 pjmedia_session *session; /**< The media session. */
49 int conf_slot; /**< Slot # in conference bridge. */
50 pjsip_evsub *xfer_sub; /**< Xfer server subscription, if this
51 call was triggered by xfer. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +000052 pjmedia_transport *med_tp; /**< Media transport. */
53 pj_timer_entry refresh_tm;/**< Timer to send re-INVITE. */
54 pj_timer_entry hangup_tm; /**< Timer to hangup call. */
Benny Prijono91a6a172007-10-31 08:59:29 +000055 pj_stun_nat_type rem_nat_type; /**< NAT type of remote endpoint. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +000056
57 char last_text_buf_[128]; /**< Buffer for last_text. */
58
59} pjsua_call;
60
61
62/**
63 * Server presence subscription list head.
64 */
65typedef struct pjsua_srv_pres
66{
67 PJ_DECL_LIST_MEMBER(struct pjsua_srv_pres);
68 pjsip_evsub *sub;
69 char *remote;
70} pjsua_srv_pres;
71
72
73/**
74 * Account
75 */
76typedef struct pjsua_acc
77{
78 pjsua_acc_config cfg; /**< Account configuration. */
79 pj_bool_t valid; /**< Is this account valid? */
80
81 int index; /**< Index in accounts array. */
Benny Prijonoc570f2d2006-07-18 00:33:02 +000082 pj_str_t display; /**< Display name, if any. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +000083 pj_str_t user_part; /**< User part of local URI. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +000084
Benny Prijonob4a17c92006-07-10 14:40:21 +000085 pj_str_t srv_domain; /**< Host part of reg server. */
86 int srv_port; /**< Port number of reg server. */
87
Benny Prijonoeebe9af2006-06-13 22:57:13 +000088 pjsip_regc *regc; /**< Client registration session. */
89 pj_timer_entry reg_timer; /**< Registration timer. */
90 pj_status_t reg_last_err; /**< Last registration error. */
91 int reg_last_code; /**< Last status last register. */
92
93 pjsip_route_hdr route_set; /**< Complete route set inc. outbnd.*/
94
95 unsigned cred_cnt; /**< Number of credentials. */
96 pjsip_cred_info cred[PJSUA_ACC_MAX_PROXIES]; /**< Complete creds. */
97
98 pj_bool_t online_status; /**< Our online status. */
Benny Prijono4461c7d2007-08-25 13:36:15 +000099 pjrpid_element rpid; /**< RPID element information. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000100 pjsua_srv_pres pres_srv_list; /**< Server subscription list. */
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000101 pjsip_publishc *publish_sess; /**< Client publication session. */
102 pj_bool_t publish_state; /**< Last published online status */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000103
104} pjsua_acc;
105
106
107/**
108 *Transport.
109 */
Benny Prijono62c5c5b2007-01-13 23:22:40 +0000110typedef struct pjsua_transport_data
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000111{
Benny Prijonoe93e2872006-06-28 16:46:49 +0000112 int index;
113 pjsip_transport_type_e type;
114 pjsip_host_port local_name;
115
116 union {
117 pjsip_transport *tp;
118 pjsip_tpfactory *factory;
119 void *ptr;
120 } data;
121
Benny Prijono62c5c5b2007-01-13 23:22:40 +0000122} pjsua_transport_data;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000123
124
125/**
126 * Buddy data.
127 */
128typedef struct pjsua_buddy
129{
130 unsigned index; /**< Buddy index. */
131 pj_str_t uri; /**< Buddy URI. */
132 pj_str_t contact; /**< Contact learned from subscrp. */
133 pj_str_t name; /**< Buddy name. */
134 pj_str_t display; /**< Buddy display name. */
135 pj_str_t host; /**< Buddy host. */
136 unsigned port; /**< Buddy port. */
137 pj_bool_t monitor; /**< Should we monitor? */
Benny Prijonof9c40c32007-06-28 07:20:26 +0000138 pjsip_dialog *dlg; /**< The underlying dialog. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000139 pjsip_evsub *sub; /**< Buddy presence subscription */
140 pjsip_pres_status status; /**< Buddy presence status. */
141
142} pjsua_buddy;
143
144
145/**
146 * File player/recorder data.
147 */
148typedef struct pjsua_file_data
149{
Benny Prijonoa66c3312007-01-21 23:12:40 +0000150 pj_bool_t type; /* 0=player, 1=playlist */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000151 pjmedia_port *port;
Benny Prijonod5696da2007-07-17 16:25:45 +0000152 pj_pool_t *pool;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000153 unsigned slot;
154} pjsua_file_data;
155
156
157/**
158 * Additional parameters for conference bridge.
159 */
160typedef struct pjsua_conf_setting
161{
162 unsigned channel_count;
163 unsigned samples_per_frame;
164 unsigned bits_per_sample;
165} pjsua_conf_setting;
166
167
168/**
169 * Global pjsua application data.
170 */
171struct pjsua_data
172{
173
174 /* Control: */
175 pj_caching_pool cp; /**< Global pool factory. */
176 pj_pool_t *pool; /**< pjsua's private pool. */
177 pj_mutex_t *mutex; /**< Mutex protection for this data */
178
179 /* Logging: */
180 pjsua_logging_config log_cfg; /**< Current logging config. */
181 pj_oshandle_t log_file; /**<Output log file handle */
182
183 /* SIP: */
184 pjsip_endpoint *endpt; /**< Global endpoint. */
185 pjsip_module mod; /**< pjsua's PJSIP module. */
Benny Prijono62c5c5b2007-01-13 23:22:40 +0000186 pjsua_transport_data tpdata[8]; /**< Array of transports. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000187
188 /* Threading: */
189 pj_bool_t thread_quit_flag; /**< Thread quit flag. */
190 pj_thread_t *thread[4]; /**< Array of threads. */
191
Benny Prijonoc97608e2007-03-23 16:34:20 +0000192 /* STUN and resolver */
193 pj_stun_config stun_cfg; /**< Global STUN settings. */
194 pj_sockaddr stun_srv; /**< Resolved STUN server address */
195 pj_status_t stun_status; /**< STUN server status. */
196 pj_dns_resolver *resolver; /**< DNS resolver. */
197
Benny Prijono6ba8c542007-10-16 01:34:14 +0000198 /* Detected NAT type */
199 pj_stun_nat_type nat_type; /**< NAT type. */
200 pj_status_t nat_status; /**< Detection status. */
201 pj_bool_t nat_in_progress; /**< Detection in progress */
202
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000203 /* Account: */
204 unsigned acc_cnt; /**< Number of accounts. */
205 pjsua_acc_id default_acc; /**< Default account ID */
206 pjsua_acc acc[PJSUA_MAX_ACC]; /**< Account array. */
Benny Prijono093d3022006-09-24 00:07:11 +0000207 pjsua_acc_id acc_ids[PJSUA_MAX_ACC]; /**< Acc sorted by prio*/
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000208
209 /* Calls: */
210 pjsua_config ua_cfg; /**< UA config. */
211 unsigned call_cnt; /**< Call counter. */
212 pjsua_call calls[PJSUA_MAX_CALLS];/**< Calls array. */
213
214 /* Buddy; */
215 unsigned buddy_cnt; /**< Buddy count. */
216 pjsua_buddy buddy[PJSUA_MAX_BUDDIES]; /**< Buddy array. */
217
Benny Prijono7a5f5102007-05-29 00:33:09 +0000218 /* Presence: */
219 pj_timer_entry pres_timer;/**< Presence refresh timer. */
220
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000221 /* Media: */
222 pjsua_media_config media_cfg; /**< Media config. */
223 pjmedia_endpt *med_endpt; /**< Media endpoint. */
224 pjsua_conf_setting mconf_cfg; /**< Additionan conf. bridge. param */
225 pjmedia_conf *mconf; /**< Conference bridge. */
226 int cap_dev; /**< Capture device ID. */
227 int play_dev; /**< Playback device ID. */
Benny Prijonoe909eac2006-07-27 22:04:56 +0000228 pj_bool_t no_snd; /**< No sound (app will manage it) */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000229 pjmedia_snd_port *snd_port; /**< Sound port. */
230 pjmedia_master_port *null_snd; /**< Master port for null sound. */
231 pjmedia_port *null_port; /**< Null port. */
232
233
234 /* File players: */
235 unsigned player_cnt;/**< Number of file players. */
Benny Prijonocba59d92007-02-16 09:22:56 +0000236 pjsua_file_data player[PJSUA_MAX_PLAYERS];/**< Array of players.*/
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000237
238 /* File recorders: */
239 unsigned rec_cnt; /**< Number of file recorders. */
Benny Prijonocba59d92007-02-16 09:22:56 +0000240 pjsua_file_data recorder[PJSUA_MAX_RECORDERS];/**< Array of recs.*/
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000241};
242
243
244extern struct pjsua_data pjsua_var;
245
Benny Prijono44e88ea2007-10-30 15:42:35 +0000246/**
247 * Get the instance of pjsua
248 */
249PJ_DECL(struct pjsua_data*) pjsua_get_var(void);
250
251
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000252
253/**
254 * IM callback data.
255 */
256typedef struct pjsua_im_data
257{
258 pjsua_acc_id acc_id;
259 pjsua_call_id call_id;
260 pj_str_t to;
261 pj_str_t body;
262 void *user_data;
263} pjsua_im_data;
264
265
266/**
267 * Duplicate IM data.
268 */
269PJ_INLINE(pjsua_im_data*) pjsua_im_data_dup(pj_pool_t *pool,
270 const pjsua_im_data *src)
271{
272 pjsua_im_data *dst;
273
Benny Prijono07113c92006-11-21 08:38:00 +0000274 dst = (pjsua_im_data*) pj_pool_alloc(pool, sizeof(*dst));
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000275 dst->acc_id = src->acc_id;
276 dst->call_id = src->call_id;
277 pj_strdup_with_null(pool, &dst->to, &src->to);
278 dst->user_data = src->user_data;
279 pj_strdup_with_null(pool, &dst->body, &src->body);
280
281 return dst;
282}
283
284
Benny Prijono148c9dd2006-09-19 13:37:53 +0000285#if 1
286#define PJSUA_LOCK() pj_mutex_lock(pjsua_var.mutex)
287#define PJSUA_TRY_LOCK() pj_mutex_trylock(pjsua_var.mutex)
288#define PJSUA_UNLOCK() pj_mutex_unlock(pjsua_var.mutex)
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000289#else
290#define PJSUA_LOCK()
Benny Prijono148c9dd2006-09-19 13:37:53 +0000291#define PJSUA_TRY_LOCK() PJ_SUCCESS
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000292#define PJSUA_UNLOCK()
293#endif
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000294
295
Benny Prijonoc97608e2007-03-23 16:34:20 +0000296/**
297 * Resolve STUN server.
298 */
299pj_status_t pjsua_resolve_stun_server(pj_bool_t wait);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000300
301/**
302 * Handle incoming invite request.
303 */
304pj_bool_t pjsua_call_on_incoming(pjsip_rx_data *rdata);
305
Benny Prijonoc97608e2007-03-23 16:34:20 +0000306/*
307 * Media channel.
308 */
309pj_status_t pjsua_media_channel_init(pjsua_call_id call_id,
310 pjsip_role_e role);
311pj_status_t pjsua_media_channel_create_sdp(pjsua_call_id call_id,
312 pj_pool_t *pool,
313 pjmedia_sdp_session **p_sdp);
314pj_status_t pjsua_media_channel_update(pjsua_call_id call_id,
Benny Prijonodbce2cf2007-03-28 16:24:00 +0000315 const pjmedia_sdp_session *local_sdp,
316 const pjmedia_sdp_session *remote_sdp);
Benny Prijonoc97608e2007-03-23 16:34:20 +0000317pj_status_t pjsua_media_channel_deinit(pjsua_call_id call_id);
318
319
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000320/**
321 * Init presence.
322 */
323pj_status_t pjsua_pres_init();
324
325/*
326 * Start presence subsystem.
327 */
328pj_status_t pjsua_pres_start(void);
329
330/**
331 * Refresh presence subscriptions
332 */
333void pjsua_pres_refresh(void);
334
335/*
Benny Prijono4461c7d2007-08-25 13:36:15 +0000336 * Update server subscription (e.g. when our online status has changed)
337 */
338void pjsua_pres_update_acc(int acc_id, pj_bool_t force);
339
340/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000341 * Shutdown presence.
342 */
343void pjsua_pres_shutdown(void);
344
345/**
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000346 * Init presence for aoocunt.
347 */
348pj_status_t pjsua_pres_init_acc(int acc_id);
349
350/**
Benny Prijono8b6834f2007-02-24 13:29:22 +0000351 * Send PUBLISH
352 */
353pj_status_t pjsua_pres_init_publish_acc(int acc_id);
354
355/**
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000356 * Terminate server subscription for the account
357 */
358void pjsua_pres_delete_acc(int acc_id);
359
360/**
361 * Init IM module handler to handle incoming MESSAGE outside dialog.
362 */
363pj_status_t pjsua_im_init(void);
364
365/**
366 * Init call subsystem.
367 */
368pj_status_t pjsua_call_subsys_init(const pjsua_config *cfg);
369
370/**
371 * Start call subsystem.
372 */
373pj_status_t pjsua_call_subsys_start(void);
374
375/**
376 * Init media subsystems.
377 */
378pj_status_t pjsua_media_subsys_init(const pjsua_media_config *cfg);
379
380/**
381 * Start pjsua media subsystem.
382 */
383pj_status_t pjsua_media_subsys_start(void);
384
385/**
386 * Destroy pjsua media subsystem.
387 */
388pj_status_t pjsua_media_subsys_destroy(void);
389
390/**
391 * Private: check if we can accept the message.
392 * If not, then p_accept header will be filled with a valid
393 * Accept header.
394 */
395pj_bool_t pjsua_im_accept_pager(pjsip_rx_data *rdata,
396 pjsip_accept_hdr **p_accept_hdr);
397
398/**
399 * Private: process pager message.
400 * This may trigger pjsua_ui_on_pager() or pjsua_ui_on_typing().
401 */
402void pjsua_im_process_pager(int call_id, const pj_str_t *from,
403 const pj_str_t *to, pjsip_rx_data *rdata);
404
405
406/**
407 * Create Accept header for MESSAGE.
408 */
409pjsip_accept_hdr* pjsua_im_create_accept(pj_pool_t *pool);
410
411/*
412 * Add additional headers etc in msg_data specified by application
413 * when sending requests.
414 */
415void pjsua_process_msg_data(pjsip_tx_data *tdata,
416 const pjsua_msg_data *msg_data);
417
418
419/*
420 * Add route_set to outgoing requests
421 */
422void pjsua_set_msg_route_set( pjsip_tx_data *tdata,
423 const pjsip_route_hdr *route_set );
424
425
426/*
427 * Simple version of MIME type parsing (it doesn't support parameters)
428 */
429void pjsua_parse_media_type( pj_pool_t *pool,
430 const pj_str_t *mime,
431 pjsip_media_type *media_type);
432
433
Benny Prijono62c5c5b2007-01-13 23:22:40 +0000434/*
435 * Internal function to init transport selector from transport id.
436 */
437void pjsua_init_tpselector(pjsua_transport_id tp_id,
438 pjsip_tpselector *sel);
439
440
Benny Prijono627cbb42007-09-25 20:48:49 +0000441pj_status_t acquire_call(const char *title,
442 pjsua_call_id call_id,
443 pjsua_call **p_call,
444 pjsip_dialog **p_dlg);
445const char *good_number(char *buf, pj_int32_t val);
446void print_call(const char *title,
447 int call_id,
448 char *buf, pj_size_t size);
449
Benny Prijono62c5c5b2007-01-13 23:22:40 +0000450
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000451PJ_END_DECL
452
453#endif /* __PJSUA_INTERNAL_H__ */
454