WARNING: The online documentation has moved to https://docs.pjsip.org.

Visit the new documentation at https://docs.pjsip.org:

BLOG | DOCUMENTATION | GITHUB

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/*
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
40#include <pjsua-lib/pjsua.h>
41
42#define THIS_FILE "APP"
43
44#define SIP_DOMAIN "example.com"
45#define SIP_USER "alice"
46#define SIP_PASSWD "secret"
47
48
49/* Callback called by the library upon receiving incoming call */
50static void on_incoming_call(pjsua_acc_id acc_id, pjsua_call_id call_id,
51 pjsip_rx_data *rdata)
52{
54
55 PJ_UNUSED_ARG(acc_id);
56 PJ_UNUSED_ARG(rdata);
57
58 pjsua_call_get_info(call_id, &ci);
59
60 PJ_LOG(3,(THIS_FILE, "Incoming call from %.*s!!",
61 (int)ci.remote_info.slen,
62 ci.remote_info.ptr));
63
64 /* Automatically answer incoming calls with 200/OK */
65 pjsua_call_answer(call_id, 200, NULL, NULL);
66}
67
68/* Callback called by the library when call's state has changed */
69static void on_call_state(pjsua_call_id call_id, pjsip_event *e)
70{
72
74
75 pjsua_call_get_info(call_id, &ci);
76 PJ_LOG(3,(THIS_FILE, "Call %d state=%.*s", call_id,
77 (int)ci.state_text.slen,
78 ci.state_text.ptr));
79}
80
81/* Callback called by the library when call's media state has changed */
82static void on_call_media_state(pjsua_call_id call_id)
83{
85
86 pjsua_call_get_info(call_id, &ci);
87
89 // When media is active, connect call to sound device.
92 }
93}
94
95/* Display error and exit application */
96static void error_exit(const char *title, pj_status_t status)
97{
98 pjsua_perror(THIS_FILE, title, status);
100 exit(1);
101}
102
103/*
104 * main()
105 *
106 * argv[1] may contain URL to call.
107 */
108int main(int argc, char *argv[])
109{
110 pjsua_acc_id acc_id;
111 pj_status_t status;
112
113 /* Create pjsua first! */
114 status = pjsua_create();
115 if (status != PJ_SUCCESS) error_exit("Error in pjsua_create()", status);
116
117 /* If argument is specified, it's got to be a valid SIP URL */
118 if (argc > 1) {
119 status = pjsua_verify_url(argv[1]);
120 if (status != PJ_SUCCESS) error_exit("Invalid URL in argv", status);
121 }
122
123 /* Init pjsua */
124 {
125 pjsua_config cfg;
126 pjsua_logging_config log_cfg;
127
129 cfg.cb.on_incoming_call = &on_incoming_call;
130 cfg.cb.on_call_media_state = &on_call_media_state;
131 cfg.cb.on_call_state = &on_call_state;
132
134 log_cfg.console_level = 4;
135
136 status = pjsua_init(&cfg, &log_cfg, NULL);
137 if (status != PJ_SUCCESS) error_exit("Error in pjsua_init()", status);
138 }
139
140 /* Add UDP transport. */
141 {
143
145 cfg.port = 5060;
146 status = pjsua_transport_create(PJSIP_TRANSPORT_UDP, &cfg, NULL);
147 if (status != PJ_SUCCESS) error_exit("Error creating transport", status);
148 }
149
150 /* Initialization is done, now start pjsua */
151 status = pjsua_start();
152 if (status != PJ_SUCCESS) error_exit("Error starting pjsua", status);
153
154 /* Register to SIP server by creating SIP account. */
155 {
157
159 cfg.id = pj_str("sip:" SIP_USER "@" SIP_DOMAIN);
160 cfg.reg_uri = pj_str("sip:" SIP_DOMAIN);
161 cfg.cred_count = 1;
162 cfg.cred_info[0].realm = pj_str(SIP_DOMAIN);
163 cfg.cred_info[0].scheme = pj_str("digest");
164 cfg.cred_info[0].username = pj_str(SIP_USER);
166 cfg.cred_info[0].data = pj_str(SIP_PASSWD);
167
168 status = pjsua_acc_add(&cfg, PJ_TRUE, &acc_id);
169 if (status != PJ_SUCCESS) error_exit("Error adding account", status);
170 }
171
172 /* If URL is specified, make call to the URL. */
173 if (argc > 1) {
174 pj_str_t uri = pj_str(argv[1]);
175 status = pjsua_call_make_call(acc_id, &uri, 0, NULL, NULL, NULL);
176 if (status != PJ_SUCCESS) error_exit("Error making call", status);
177 }
178
179 /* Wait until user press "q" to quit. */
180 for (;;) {
181 char option[10];
182
183 puts("Press 'h' to hangup all calls, 'q' to quit");
184 if (fgets(option, sizeof(option), stdin) == NULL) {
185 puts("EOF while reading stdin, will quit now..");
186 break;
187 }
188
189 if (option[0] == 'q')
190 break;
191
192 if (option[0] == 'h')
194 }
195
196 /* Destroy pjsua */
198
199 return 0;
200}
@ PJSIP_CRED_DATA_PLAIN_PASSWD
Definition: sip_auth.h:55
@ PJSIP_TRANSPORT_UDP
Definition: sip_types.h:67
void pjsua_acc_config_default(pjsua_acc_config *cfg)
pj_status_t pjsua_acc_add(const pjsua_acc_config *acc_cfg, pj_bool_t is_default, pjsua_acc_id *p_acc_id)
int pjsua_acc_id
Definition: pjsua-lib/pjsua.h:265
pj_status_t pjsua_create(void)
void pjsua_perror(const char *sender, const char *title, pj_status_t status)
void pjsua_config_default(pjsua_config *cfg)
pj_status_t pjsua_verify_url(const char *url)
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_start(void)
void pjsua_logging_config_default(pjsua_logging_config *cfg)
pj_status_t pjsua_destroy(void)
int pjsua_call_id
Definition: pjsua-lib/pjsua.h:262
pj_status_t pjsua_call_get_info(pjsua_call_id call_id, pjsua_call_info *info)
pj_status_t pjsua_call_answer(pjsua_call_id call_id, unsigned code, const pj_str_t *reason, const pjsua_msg_data *msg_data)
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)
void pjsua_call_hangup_all(void)
@ PJSUA_CALL_MEDIA_ACTIVE
Definition: pjsua-lib/pjsua.h:4995
pj_status_t pjsua_conf_connect(pjsua_conf_port_id source, pjsua_conf_port_id sink)
pj_status_t pjsua_transport_create(pjsip_transport_type_e type, const pjsua_transport_config *cfg, pjsua_transport_id *p_id)
void pjsua_transport_config_default(pjsua_transport_config *cfg)
int pj_status_t
PJ_SUCCESS
PJ_TRUE
#define PJ_LOG(level, arg)
pj_str_t pj_str(char *str)
#define PJ_UNUSED_ARG(arg)
PJSUA API.
pj_ssize_t slen
char * ptr
pj_str_t username
Definition: sip_auth.h:117
int data_type
Definition: sip_auth.h:118
pj_str_t realm
Definition: sip_auth.h:113
pj_str_t data
Definition: sip_auth.h:119
pj_str_t scheme
Definition: sip_auth.h:116
Definition: sip_event.h:81
Definition: sip_transport.h:295
Definition: pjsua-lib/pjsua.h:3709
pj_str_t reg_uri
Definition: pjsua-lib/pjsua.h:3740
pjsip_cred_info cred_info[8]
Definition: pjsua-lib/pjsua.h:3947
pj_str_t id
Definition: pjsua-lib/pjsua.h:3731
unsigned cred_count
Definition: pjsua-lib/pjsua.h:3938
Definition: pjsua-lib/pjsua.h:5097
pjsua_call_media_status media_status
Definition: pjsua-lib/pjsua.h:5145
pj_str_t state_text
Definition: pjsua-lib/pjsua.h:5129
pj_str_t remote_info
Definition: pjsua-lib/pjsua.h:5114
pjsua_conf_port_id conf_slot
Definition: pjsua-lib/pjsua.h:5155
void(* on_call_media_state)(pjsua_call_id call_id)
Definition: pjsua-lib/pjsua.h:1137
void(* on_call_state)(pjsua_call_id call_id, pjsip_event *e)
Definition: pjsua-lib/pjsua.h:1099
void(* on_incoming_call)(pjsua_acc_id acc_id, pjsua_call_id call_id, pjsip_rx_data *rdata)
Definition: pjsua-lib/pjsua.h:1109
Definition: pjsua-lib/pjsua.h:2040
pjsua_callback cb
Definition: pjsua-lib/pjsua.h:2248
Definition: pjsua-lib/pjsua.h:474
unsigned console_level
Definition: pjsua-lib/pjsua.h:488
Definition: pjsua-lib/pjsua.h:3021
unsigned port
Definition: pjsua-lib/pjsua.h:3028

 


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