blob: e45db7e27addc7dbe64f0ec9332ca3baf236d123 [file] [log] [blame]
Benny Prijonof77ba882007-01-08 01:20:07 +00001/* $Id$ */
2/*
Benny Prijonoa771a512007-02-19 01:13:53 +00003 * Copyright (C) 2003-2007 Benny Prijono <benny@prijono.org>
Benny Prijonof77ba882007-01-08 01:20:07 +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
20/**
21 * simple_pjsua.c
22 *
23 * This is a very simple but fully featured SIP user agent, with the
24 * following capabilities:
25 * - SIP registration
26 * - Making and receiving call
27 * - Audio/media to sound device.
28 *
29 * Usage:
30 * - To make outgoing call, start simple_pjsua with the URL of remote
31 * destination to contact.
32 * E.g.:
33 * simpleua sip:user@remote
34 *
35 * - Incoming calls will automatically be answered with 200.
36 *
37 * This program will quit once it has completed a single call.
38 */
39
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{
53 pjsua_call_info ci;
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{
71 pjsua_call_info ci;
72
73 PJ_UNUSED_ARG(e);
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{
84 pjsua_call_info ci;
85
86 pjsua_call_get_info(call_id, &ci);
87
88 if (ci.media_status == PJSUA_CALL_MEDIA_ACTIVE) {
89 // When media is active, connect call to sound device.
90 pjsua_conf_connect(ci.conf_slot, 0);
91 pjsua_conf_connect(0, ci.conf_slot);
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);
99 pjsua_destroy();
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_sip_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
128 pjsua_config_default(&cfg);
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
133 pjsua_logging_config_default(&log_cfg);
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 {
142 pjsua_transport_config cfg;
143
144 pjsua_transport_config_default(&cfg);
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 {
156 pjsua_acc_config cfg;
157
158 pjsua_acc_config_default(&cfg);
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);
165 cfg.cred_info[0].data_type = PJSIP_CRED_DATA_PLAIN_PASSWD;
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 fgets(option, sizeof(option), stdin);
185
186 if (option[0] == 'q')
187 break;
188
189 if (option[0] == 'h')
190 pjsua_call_hangup_all();
191 }
192
193 /* Destroy pjsua */
194 pjsua_destroy();
195
196 return 0;
197}