BLOG | DOCUMENTATION | TRAC

Home --> Documentations --> PJSIP Reference

Samples: Simple PJSUA

Very simple SIP User Agent with registration, call, and media, all in under 200 lines of code.

1 /* $Id: simple_pjsua.c 3553 2011-05-05 06:14:19Z nanang $ */
2 /*
3  * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4  * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */
20 
41 #include <pjsua-lib/pjsua.h>
42 
43 #define THIS_FILE "APP"
44 
45 #define SIP_DOMAIN "example.com"
46 #define SIP_USER "alice"
47 #define SIP_PASSWD "secret"
48 
49 
50 /* Callback called by the library upon receiving incoming call */
51 static void on_incoming_call(pjsua_acc_id acc_id, pjsua_call_id call_id,
52  pjsip_rx_data *rdata)
53 {
54  pjsua_call_info ci;
55 
56  PJ_UNUSED_ARG(acc_id);
57  PJ_UNUSED_ARG(rdata);
58 
59  pjsua_call_get_info(call_id, &ci);
60 
61  PJ_LOG(3,(THIS_FILE, "Incoming call from %.*s!!",
62  (int)ci.remote_info.slen,
63  ci.remote_info.ptr));
64 
65  /* Automatically answer incoming calls with 200/OK */
66  pjsua_call_answer(call_id, 200, NULL, NULL);
67 }
68 
69 /* Callback called by the library when call's state has changed */
70 static void on_call_state(pjsua_call_id call_id, pjsip_event *e)
71 {
72  pjsua_call_info ci;
73 
74  PJ_UNUSED_ARG(e);
75 
76  pjsua_call_get_info(call_id, &ci);
77  PJ_LOG(3,(THIS_FILE, "Call %d state=%.*s", call_id,
78  (int)ci.state_text.slen,
79  ci.state_text.ptr));
80 }
81 
82 /* Callback called by the library when call's media state has changed */
83 static void on_call_media_state(pjsua_call_id call_id)
84 {
85  pjsua_call_info ci;
86 
87  pjsua_call_get_info(call_id, &ci);
88 
90  // When media is active, connect call to sound device.
93  }
94 }
95 
96 /* Display error and exit application */
97 static void error_exit(const char *title, pj_status_t status)
98 {
99  pjsua_perror(THIS_FILE, title, status);
100  pjsua_destroy();
101  exit(1);
102 }
103 
104 /*
105  * main()
106  *
107  * argv[1] may contain URL to call.
108  */
109 int main(int argc, char *argv[])
110 {
111  pjsua_acc_id acc_id;
112  pj_status_t status;
113 
114  /* Create pjsua first! */
115  status = pjsua_create();
116  if (status != PJ_SUCCESS) error_exit("Error in pjsua_create()", status);
117 
118  /* If argument is specified, it's got to be a valid SIP URL */
119  if (argc > 1) {
120  status = pjsua_verify_url(argv[1]);
121  if (status != PJ_SUCCESS) error_exit("Invalid URL in argv", status);
122  }
123 
124  /* Init pjsua */
125  {
126  pjsua_config cfg;
127  pjsua_logging_config log_cfg;
128 
129  pjsua_config_default(&cfg);
130  cfg.cb.on_incoming_call = &on_incoming_call;
131  cfg.cb.on_call_media_state = &on_call_media_state;
132  cfg.cb.on_call_state = &on_call_state;
133 
135  log_cfg.console_level = 4;
136 
137  status = pjsua_init(&cfg, &log_cfg, NULL);
138  if (status != PJ_SUCCESS) error_exit("Error in pjsua_init()", status);
139  }
140 
141  /* Add UDP transport. */
142  {
144 
146  cfg.port = 5060;
147  status = pjsua_transport_create(PJSIP_TRANSPORT_UDP, &cfg, NULL);
148  if (status != PJ_SUCCESS) error_exit("Error creating transport", status);
149  }
150 
151  /* Initialization is done, now start pjsua */
152  status = pjsua_start();
153  if (status != PJ_SUCCESS) error_exit("Error starting pjsua", status);
154 
155  /* Register to SIP server by creating SIP account. */
156  {
157  pjsua_acc_config cfg;
158 
160  cfg.id = pj_str("sip:" SIP_USER "@" SIP_DOMAIN);
161  cfg.reg_uri = pj_str("sip:" SIP_DOMAIN);
162  cfg.cred_count = 1;
163  cfg.cred_info[0].realm = pj_str(SIP_DOMAIN);
164  cfg.cred_info[0].scheme = pj_str("digest");
165  cfg.cred_info[0].username = pj_str(SIP_USER);
167  cfg.cred_info[0].data = pj_str(SIP_PASSWD);
168 
169  status = pjsua_acc_add(&cfg, PJ_TRUE, &acc_id);
170  if (status != PJ_SUCCESS) error_exit("Error adding account", status);
171  }
172 
173  /* If URL is specified, make call to the URL. */
174  if (argc > 1) {
175  pj_str_t uri = pj_str(argv[1]);
176  status = pjsua_call_make_call(acc_id, &uri, 0, NULL, NULL, NULL);
177  if (status != PJ_SUCCESS) error_exit("Error making call", status);
178  }
179 
180  /* Wait until user press "q" to quit. */
181  for (;;) {
182  char option[10];
183 
184  puts("Press 'h' to hangup all calls, 'q' to quit");
185  if (fgets(option, sizeof(option), stdin) == NULL) {
186  puts("EOF while reading stdin, will quit now..");
187  break;
188  }
189 
190  if (option[0] == 'q')
191  break;
192 
193  if (option[0] == 'h')
195  }
196 
197  /* Destroy pjsua */
198  pjsua_destroy();
199 
200  return 0;
201 }
pj_ssize_t slen
pjsua_callback cb
Definition: pjsua-lib/pjsua.h:1688
char * ptr
Definition: pjsua-lib/pjsua.h:397
Definition: pjsua-lib/pjsua.h:4150
int pjsua_acc_id
Definition: pjsua-lib/pjsua.h:266
pj_str_t data
Definition: sip_auth.h:117
pj_str_t username
Definition: sip_auth.h:115
Definition: pjsua-lib/pjsua.h:2916
void(* on_call_media_state)(pjsua_call_id call_id)
Definition: pjsua-lib/pjsua.h:713
#define PJ_LOG(level, arg)
void pjsua_perror(const char *sender, const char *title, pj_status_t status)
void(* on_call_state)(pjsua_call_id call_id, pjsip_event *e)
Definition: pjsua-lib/pjsua.h:676
int pj_status_t
void pjsua_logging_config_default(pjsua_logging_config *cfg)
pj_status_t pjsua_start(void)
Definition: pjsua-lib/pjsua.h:4061
int pjsua_call_id
Definition: pjsua-lib/pjsua.h:263
pj_status_t pjsua_init(const pjsua_config *ua_cfg, const pjsua_logging_config *log_cfg, const pjsua_media_config *media_cfg)
pj_status_t pjsua_verify_url(const char *url)
Definition: sip_event.h:81
pjsip_cred_info cred_info[8]
Definition: pjsua-lib/pjsua.h:3144
pj_str_t id
Definition: pjsua-lib/pjsua.h:2939
Definition: sip_transport.h:281
unsigned console_level
Definition: pjsua-lib/pjsua.h:412
void pjsua_transport_config_default(pjsua_transport_config *cfg)
pj_str_t state_text
Definition: pjsua-lib/pjsua.h:4183
void pjsua_acc_config_default(pjsua_acc_config *cfg)
pj_status_t pjsua_create(void)
pj_status_t pjsua_transport_create(pjsip_transport_type_e type, const pjsua_transport_config *cfg, pjsua_transport_id *p_id)
pj_str_t pj_str(char *str)
Definition: pjsua-lib/pjsua.h:1487
pj_str_t scheme
Definition: sip_auth.h:114
void(* on_incoming_call)(pjsua_acc_id acc_id, pjsua_call_id call_id, pjsip_rx_data *rdata)
Definition: pjsua-lib/pjsua.h:686
unsigned port
Definition: pjsua-lib/pjsua.h:2336
pj_status_t pjsua_call_answer(pjsua_call_id call_id, unsigned code, const pj_str_t *reason, const pjsua_msg_data *msg_data)
void pjsua_config_default(pjsua_config *cfg)
pj_str_t remote_info
Definition: pjsua-lib/pjsua.h:4168
pj_status_t pjsua_call_make_call(pjsua_acc_id acc_id, const pj_str_t *dst_uri, const pjsua_call_setting *opt, void *user_data, const pjsua_msg_data *msg_data, pjsua_call_id *p_call_id)
pjsua_conf_port_id conf_slot
Definition: pjsua-lib/pjsua.h:4198
Definition: sip_types.h:68
void pjsua_call_hangup_all(void)
pj_str_t realm
Definition: sip_auth.h:111
unsigned cred_count
Definition: pjsua-lib/pjsua.h:3135
pj_str_t reg_uri
Definition: pjsua-lib/pjsua.h:2948
Definition: sip_auth.h:53
pj_status_t pjsua_destroy(void)
pjsua_call_media_status media_status
Definition: pjsua-lib/pjsua.h:4192
Definition: pjsua-lib/pjsua.h:2328
pj_status_t pjsua_call_get_info(pjsua_call_id call_id, pjsua_call_info *info)
pj_status_t pjsua_acc_add(const pjsua_acc_config *acc_cfg, pj_bool_t is_default, pjsua_acc_id *p_acc_id)
#define PJ_UNUSED_ARG(arg)
int data_type
Definition: sip_auth.h:116
pj_status_t pjsua_conf_connect(pjsua_conf_port_id source, pjsua_conf_port_id sink)

 


PJSIP Open Source, high performance, small footprint, and very very portable SIP stack
Copyright (C) 2006-2008 Teluu Inc.