summaryrefslogtreecommitdiff
path: root/pjsip/include/pjsua-lib/pjsua.h
blob: 184671299e1ba0e980766c362a10da6b042c7680 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
/* $Id$ */
/* 
 * Copyright (C) 2003-2006 Benny Prijono <benny@prijono.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
 */
#ifndef __PJSUA_H__
#define __PJSUA_H__

/* Include all PJSIP core headers. */
#include <pjsip.h>

/* Include all PJMEDIA headers. */
#include <pjmedia.h>

/* Include all PJMEDIA-CODEC headers. */
#include <pjmedia-codec.h>

/* Include all PJSIP-UA headers */
#include <pjsip_ua.h>

/* Include all PJSIP-SIMPLE headers */
#include <pjsip_simple.h>

/* Include all PJLIB-UTIL headers. */
#include <pjlib-util.h>

/* Include all PJLIB headers. */
#include <pjlib.h>


PJ_BEGIN_DECL


/**
 * Max buddies in buddy list.
 */
#ifndef PJSUA_MAX_BUDDIES
#   define PJSUA_MAX_BUDDIES	    32
#endif


/**
 * Max simultaneous calls.
 */
#ifndef PJSUA_MAX_CALLS
#   define PJSUA_MAX_CALLS	    256
#endif


/**
 * Aditional ports to be allocated in the conference ports for non-call
 * streams.
 */
#define PJSUA_CONF_MORE_PORTS	    3


/**
 * Maximum accounts.
 */
#ifndef PJSUA_MAX_ACC
#   define PJSUA_MAX_ACC	    8
#endif


/**
 * Maximum credentials.
 */
#ifndef PJSUA_MAX_CRED
#   define PJSUA_MAX_CRED	    PJSUA_MAX_ACC
#endif


/** 
 * Structure to be attached to invite dialog. 
 * Given a dialog "dlg", application can retrieve this structure
 * by accessing dlg->mod_data[pjsua.mod.id].
 */
struct pjsua_call
{
    unsigned		 index;	    /**< Index in pjsua array.		    */
    pjsip_inv_session	*inv;	    /**< The invite session.		    */
    pj_time_val		 start_time;/**< First INVITE sent/received.	    */
    pj_time_val		 res_time;  /**< First response sent/received.	    */
    pj_time_val		 conn_time; /**< Connected/confirmed time.	    */
    pj_time_val		 dis_time;  /**< Disconnect time.		    */
    int			 acc_index; /**< Account index being used.	    */
    pjmedia_session	*session;   /**< The media session.		    */
    unsigned		 conf_slot; /**< Slot # in conference bridge.	    */
    pjsip_evsub		*xfer_sub;  /**< Xfer server subscription, if this
					 call was triggered by xfer.	    */
    pjmedia_sock_info	 skinfo;    /**< Preallocated media sockets.	    */
    void		*app_data;  /**< Application data.		    */
    pj_timer_entry	 refresh_tm;/**< Timer to send re-INVITE.	    */
    pj_timer_entry	 hangup_tm; /**< Timer to hangup call.		    */
};

typedef struct pjsua_call pjsua_call;


/**
 * Buddy data.
 */
struct pjsua_buddy
{
    pj_str_t		 uri;	    /**< Buddy URI		        */
    int			 acc_index; /**< Which account to use.		*/
    pj_bool_t		 monitor;   /**< Should we monitor?		*/
    pjsip_evsub		*sub;	    /**< Buddy presence subscription	*/
    pjsip_pres_status	 status;    /**< Buddy presence status.		*/
};

typedef struct pjsua_buddy pjsua_buddy;


/**
 * Server presence subscription list head.
 */
struct pjsua_srv_pres
{
    PJ_DECL_LIST_MEMBER(struct pjsua_srv_pres);
    pjsip_evsub	    *sub;
    char	    *remote;
};

typedef struct pjsua_srv_pres pjsua_srv_pres;


/**
 * Account
 */
struct pjsua_acc
{
    int		     index;	    /**< Index in accounts array.	*/
    pj_str_t	     local_uri;	    /**< Uri in From: header.		*/
    pj_str_t	     user_part;	    /**< User part of local URI.	*/
    pj_str_t	     host_part;	    /**< Host part of local URI.	*/
    pj_str_t	     contact_uri;   /**< Uri in Contact: header.	*/

    pj_str_t	     reg_uri;	    /**< Registrar URI.			*/
    pjsip_regc	    *regc;	    /**< Client registration session.   */
    pj_int32_t	     reg_timeout;   /**< Default timeout.		*/
    pj_timer_entry   reg_timer;	    /**< Registration timer.		*/
    pj_status_t	     reg_last_err;  /**< Last registration error.	*/
    int		     reg_last_code; /**< Last status last register.	*/

    pj_str_t	     proxy;	    /**< Proxy URL.			*/
    pjsip_route_hdr  route_set;	    /**< Route set.			*/

    pj_bool_t	     online_status; /**< Our online status.		*/
    pjsua_srv_pres   pres_srv_list; /**< Server subscription list.	*/

    void	    *app_data;	    /**< Application data.		*/
};


typedef struct pjsua_acc pjsua_acc;


/* PJSUA application variables. */
struct pjsua
{
    /* Control: */
    pj_caching_pool  cp;	    /**< Global pool factory.		*/
    pjsip_endpoint  *endpt;	    /**< Global endpoint.		*/
    pj_pool_t	    *pool;	    /**< pjsua's private pool.		*/
    pjsip_module     mod;	    /**< pjsua's PJSIP module.		*/
    

    /* Media:  */
    int		     start_rtp_port;/**< Start of RTP port to try.	*/
    pjmedia_endpt   *med_endpt;	    /**< Media endpoint.		*/
    unsigned	     clock_rate;    /**< Internal clock rate.		*/
    pjmedia_conf    *mconf;	    /**< Media conference.		*/
    pj_bool_t	     null_audio;    /**< Null audio flag.		*/
    pj_bool_t	     no_mic;	    /**< Disable microphone.		*/
    char	    *wav_file;	    /**< WAV file name to play.		*/
    unsigned	     wav_slot;	    /**< WAV player slot in bridge	*/
    pjmedia_port    *file_port;	    /**< WAV player port.		*/
    pjmedia_port    *null_port;	    /**< NULL port.			*/
    pj_bool_t	     auto_play;	    /**< Auto play file for calls?	*/
    pj_bool_t	     auto_loop;	    /**< Auto loop RTP stream?		*/
    pj_bool_t	     auto_conf;	    /**< Auto put to conference?	*/
    int		     complexity;    /**< Codec complexity.		*/
    int		     quality;	    /**< Codec quality.			*/
    int		     ptime;	    /**< Codec ptime in msec.		*/


    /* Codec arguments: */
    int		     codec_cnt;	    /**< Number of --add-codec args.	*/
    pj_str_t	     codec_arg[32]; /**< Array of --add-codec args.	*/
    pj_status_t	    (*codec_deinit[32])(void);	/**< Array of funcs.	*/

    /* User Agent behaviour: */
    int		     auto_answer;   /**< Automatically answer in calls.	*/
    int		     uas_refresh;   /**< Time to re-INVITE.		*/
    int		     uas_duration;  /**< Max call duration.		*/

    /* Account: */
    pj_bool_t	     has_acc;	    /**< Any --id cmdline?		*/
    int		     acc_cnt;	    /**< Number of client registrations	*/
    pjsua_acc	     acc[PJSUA_MAX_ACC];    /** Client regs array.	*/


    /* Authentication credentials: */

    int		     cred_count;    /**< Number of credentials.		*/
    pjsip_cred_info  cred_info[10]; /**< Array of credentials.		*/


    /* Threading (optional): */
    int		     thread_cnt;    /**< Thread count.			*/
    pj_thread_t	    *threads[8];    /**< Thread instances.		*/
    pj_bool_t	     quit_flag;	    /**< To signal thread to quit.	*/

    /* Transport (UDP): */
    pj_uint16_t	     sip_port;	    /**< SIP signaling port.		*/
    pj_sock_t	     sip_sock;	    /**< SIP UDP socket.		*/
    pj_sockaddr_in   sip_sock_name; /**< Public/STUN UDP socket addr.	*/

    pj_str_t	     outbound_proxy;/**< Outbound proxy.		*/


    /* STUN: */
    pj_str_t	     stun_srv1;
    int		     stun_port1;
    pj_str_t	     stun_srv2;
    int		     stun_port2;


    /* Logging: */    
    int		     log_level;	    /**< Logging verbosity.		*/
    int		     app_log_level; /**< stdout log verbosity.		*/
    unsigned	     log_decor;	    /**< Log decoration.		*/
    char	    *log_filename;  /**< Log filename.			*/


    /* PJSUA Calls: */
    pj_str_t	     uri_to_call;   /**< URI to call.			*/
    int		     max_calls;	    /**< Max nb of calls.		*/
    int		     call_cnt;	    /**< Number of calls.		*/
    pjsua_call	     calls[PJSUA_MAX_CALLS];	/** Calls array.	*/


    /* SIMPLE and buddy status: */
    int		     buddy_cnt;
    pjsua_buddy	     buddies[PJSUA_MAX_BUDDIES];
};


/** PJSUA instance. */
extern struct pjsua pjsua;



/*****************************************************************************
 * PJSUA API (defined in pjsua_core.c).
 */

/**
 * Initialize pjsua settings with default parameters.
 */
void pjsua_default(void);


/**
 * Display error message for the specified error code.
 */
void pjsua_perror(const char *sender, const char *title, 
		  pj_status_t status);


/**
 * Initialize pjsua application. Application can call this before parsing
 * application settings.
 *
 * This will initialize all libraries, create endpoint instance, and register
 * pjsip modules. Transport will NOT be created however.
 *
 * Application may register module after calling this function.
 */
pj_status_t pjsua_init(void);


/**
 * Start pjsua stack. Application calls this after pjsua settings has been
 * configured.
 *
 * This will start the transport, worker threads (if any), and registration 
 * process, if registration is configured.
 */
pj_status_t pjsua_start(void);


/**
 * Destroy pjsua.
 */
pj_status_t pjsua_destroy(void);


/**
 * Find account for incoming request.
 */
int pjsua_find_account_for_incoming(pjsip_rx_data *rdata);


/**
 * Find account for outgoing request.
 */
int pjsua_find_account_for_outgoing(const pj_str_t *url);


/*****************************************************************************
 * PJSUA Call API (defined in pjsua_call.c).
 */

/**
 * Init pjsua call module.
 */
pj_status_t pjsua_call_init(void);

/**
 * Make outgoing call.
 */
pj_status_t pjsua_make_call(int acc_index,
			    const char *cstr_dest_uri,
			    int *p_call_index);


/**
 * Handle incoming invite request.
 */
pj_bool_t pjsua_call_on_incoming(pjsip_rx_data *rdata);


/**
 * Answer call.
 */
void pjsua_call_answer(int call_index, int code);

/**
 * Hangup call.
 */
void pjsua_call_hangup(int call_index);


/**
 * Put call on-hold.
 */
void pjsua_call_set_hold(int call_index);


/**
 * Send re-INVITE (to release hold).
 */
void pjsua_call_reinvite(int call_index);


/**
 * Transfer call.
 */
void pjsua_call_xfer(int call_index, const char *dest);


/**
 * Send instant messaging inside INVITE session.
 */
void pjsua_call_send_im(int call_index, const char *text);


/**
 * Send IM typing indication inside INVITE session.
 */
void pjsua_call_typing(int call_index, pj_bool_t is_typing);

/**
 * Terminate all calls.
 */
void pjsua_call_hangup_all(void);


/*****************************************************************************
 * PJSUA Client Registration API (defined in pjsua_reg.c).
 */

/**
 * Initialize client registration session.
 *
 * @param app_callback	Optional callback
 */
pj_status_t pjsua_regc_init(int acc_index);

/**
 * Update registration or perform unregistration. If renew argument is zero,
 * this will start unregistration process.
 */
void pjsua_regc_update(int acc_index, pj_bool_t renew);




/*****************************************************************************
 * PJSUA Presence (pjsua_pres.c)
 */

/**
 * Init presence.
 */
pj_status_t pjsua_pres_init();

/**
 * Refresh both presence client and server subscriptions.
 */
void pjsua_pres_refresh(int acc_index);

/**
 * Terminate all subscriptions
 */
void pjsua_pres_shutdown(void);

/**
 * Dump presence subscriptions.
 */
void pjsua_pres_dump(pj_bool_t detail);


/*****************************************************************************
 * PJSUA Instant Messaging (pjsua_im.c)
 */

/**
 * The MESSAGE method (defined in pjsua_im.c)
 */
extern const pjsip_method pjsip_message_method;


/**
 * Init IM module handler to handle incoming MESSAGE outside dialog.
 */
pj_status_t pjsua_im_init();


/**
 * Create Accept header for MESSAGE.
 */
pjsip_accept_hdr* pjsua_im_create_accept(pj_pool_t *pool);

/**
 * Send IM outside dialog.
 */
pj_status_t pjsua_im_send(int acc_index, const char *dst_uri, 
			  const char *text);


/**
 * Send typing indication outside dialog.
 */
pj_status_t pjsua_im_typing(int acc_index, const char *dst_uri, 
			    pj_bool_t is_typing);


/**
 * Private: check if we can accept the message.
 *	    If not, then p_accept header will be filled with a valid
 *	    Accept header.
 */
pj_bool_t pjsua_im_accept_pager(pjsip_rx_data *rdata,
				pjsip_accept_hdr **p_accept_hdr);

/**
 * Private: process pager message.
 *	    This may trigger pjsua_ui_on_pager() or pjsua_ui_on_typing().
 */
void pjsua_im_process_pager(int call_id, const pj_str_t *from,
			    const pj_str_t *to, pjsip_rx_data *rdata);


/*****************************************************************************
 * User Interface API.
 *
 * The UI API specifies functions that will be called by pjsua upon
 * occurence of various events.
 */

/**
 * Notify UI when invite state has changed.
 */
void pjsua_ui_on_call_state(int call_index, pjsip_event *e);

/**
 * Notify UI when registration status has changed.
 */
void pjsua_ui_on_reg_state(int acc_index);

/**
 * Notify UI on incoming pager (i.e. MESSAGE request).
 * Argument call_index will be -1 if MESSAGE request is not related to an 
 * existing call.
 */
void pjsua_ui_on_pager(int call_index, const pj_str_t *from,
		       const pj_str_t *to, const pj_str_t *txt);


/**
 * Notify UI about typing indication.
 */
void pjsua_ui_on_typing(int call_index, const pj_str_t *from,
		        const pj_str_t *to, pj_bool_t is_typing);


/*****************************************************************************
 * Utilities.
 *
 */

/** String to describe invite session states */
extern const char *pjsua_inv_state_names[];

/**
 * Parse arguments (pjsua_opt.c).
 */
pj_status_t pjsua_parse_args(int argc, char *argv[]);

/**
 * Load settings from a file.
 */
pj_status_t pjsua_load_settings(const char *filename);

/**
 * Dump settings.
 */
int pjsua_dump_settings(char *buf, pj_size_t max);

/**
 * Save settings to a file.
 */
pj_status_t pjsua_save_settings(const char *filename);


/*
 * Verify that valid SIP url is given.
 * @return  PJ_SUCCESS if valid.
 */
pj_status_t pjsua_verify_sip_url(const char *c_url);

/*
 * Dump application states.
 */
void pjsua_dump(pj_bool_t detail);


PJ_END_DECL


#endif	/* __PJSUA_H__ */