1
/* $Id: simple_pjsua.c 3553 2011-05-05 06:14:19Z nanang $ */
3
* Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4
* Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
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.
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.
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
24
* This is a very simple but fully featured SIP user agent, with the
25
* following capabilities:
27
* - Making and receiving call
28
* - Audio/media to sound device.
31
* - To make outgoing call, start simple_pjsua with the URL of remote
32
* destination to contact.
34
* simpleua sip:user@remote
36
* - Incoming calls will automatically be answered with 200.
38
* This program will quit once it has completed a single call.
41
#include <pjsua-lib/pjsua.h>
43
#define THIS_FILE "APP"
45
#define SIP_DOMAIN "example.com"
46
#define SIP_USER "alice"
47
#define SIP_PASSWD "secret"
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,
56
PJ_UNUSED_ARG(acc_id);
59
pjsua_call_get_info(call_id, &ci);
61
PJ_LOG(3,(THIS_FILE, "Incoming call from %.*s!!",
62
(int)ci.remote_info.slen,
65
/* Automatically answer incoming calls with 200/OK */
66
pjsua_call_answer(call_id, 200, NULL, NULL);
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)
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,
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)
87
pjsua_call_get_info(call_id, &ci);
89
if (ci.media_status == PJSUA_CALL_MEDIA_ACTIVE) {
90
// When media is active, connect call to sound device.
91
pjsua_conf_connect(ci.conf_slot, 0);
92
pjsua_conf_connect(0, ci.conf_slot);
96
/* Display error and exit application */
97
static void error_exit(const char *title, pj_status_t status)
99
pjsua_perror(THIS_FILE, title, status);
107
* argv[1] may contain URL to call.
109
int main(int argc, char *argv[])
114
/* Create pjsua first! */
115
status = pjsua_create();
116
if (status != PJ_SUCCESS) error_exit("Error in pjsua_create()", status);
118
/* If argument is specified, it's got to be a valid SIP URL */
120
status = pjsua_verify_url(argv[1]);
121
if (status != PJ_SUCCESS) error_exit("Invalid URL in argv", status);
127
pjsua_logging_config log_cfg;
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;
134
pjsua_logging_config_default(&log_cfg);
135
log_cfg.console_level = 4;
137
status = pjsua_init(&cfg, &log_cfg, NULL);
138
if (status != PJ_SUCCESS) error_exit("Error in pjsua_init()", status);
141
/* Add UDP transport. */
143
pjsua_transport_config cfg;
145
pjsua_transport_config_default(&cfg);
147
status = pjsua_transport_create(PJSIP_TRANSPORT_UDP, &cfg, NULL);
148
if (status != PJ_SUCCESS) error_exit("Error creating transport", status);
151
/* Initialization is done, now start pjsua */
152
status = pjsua_start();
153
if (status != PJ_SUCCESS) error_exit("Error starting pjsua", status);
155
/* Register to SIP server by creating SIP account. */
157
pjsua_acc_config cfg;
159
pjsua_acc_config_default(&cfg);
160
cfg.id = pj_str("sip:" SIP_USER "@" SIP_DOMAIN);
161
cfg.reg_uri = pj_str("sip:" SIP_DOMAIN);
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);
166
cfg.cred_info[0].data_type = PJSIP_CRED_DATA_PLAIN_PASSWD;
167
cfg.cred_info[0].data = pj_str(SIP_PASSWD);
169
status = pjsua_acc_add(&cfg, PJ_TRUE, &acc_id);
170
if (status != PJ_SUCCESS) error_exit("Error adding account", status);
173
/* If URL is specified, make call to the URL. */
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);
180
/* Wait until user press "q" to quit. */
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..");
190
if (option[0] == 'q')
193
if (option[0] == 'h')
194
pjsua_call_hangup_all();