-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
166 lines (127 loc) · 4.21 KB
/
main.c
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
#include <pjlib.h>
#include <pjlib-util.h>
#include <pjmedia.h>
#include <pjmedia-codec.h>
#include <pjsip.h>
#include <pjsip_simple.h>
#include <pjsip_ua.h>
#include <pjsua-lib/pjsua.h>
#define THIS_FILE "APP"
#define SIP_DOMAIN "127.0.0.1"
#define SIP_USER "6001"
#define SIP_PASSWD "user"
#define PORT 5038
/* Callback called by the library upon receiving incoming call */
static void on_incoming_call(pjsua_acc_id acc_id, pjsua_call_id call_id, pjsip_rx_data *rdata)
{
pjsua_call_info ci;
PJ_UNUSED_ARG(acc_id);
PJ_UNUSED_ARG(rdata);
pjsua_call_get_info(call_id, &ci);
PJ_LOG(3,(THIS_FILE, "Incoming call from %.*s!!",
(int)ci.remote_info.slen,
ci.remote_info.ptr));
/* Automatically answer incoming calls with 200/OK */
pjsua_call_answer(call_id, 200, NULL, NULL);
}
/* Callback called by the library when call's state has changed */
static void on_call_state(pjsua_call_id call_id, pjsip_event *e)
{
pjsua_call_info ci;
PJ_UNUSED_ARG(e);
pjsua_call_get_info(call_id, &ci);
PJ_LOG(3,(THIS_FILE, "Call %d state=%.*s", call_id,
(int)ci.state_text.slen,
ci.state_text.ptr));
}
/* Callback called by the library when call's media state has changed */
static void on_call_media_state(pjsua_call_id call_id)
{
pjsua_call_info ci;
pjsua_call_get_info(call_id, &ci);
if (ci.media_status == PJSUA_CALL_MEDIA_ACTIVE) {
// When media is active, connect call to sound device.
pjsua_conf_connect(ci.conf_slot, 0);
pjsua_conf_connect(0, ci.conf_slot);
}
}
/* Display error and exit application */
static void error_exit(const char *title, pj_status_t status)
{
pjsua_perror(THIS_FILE, title, status);
pjsua_destroy();
exit(1);
}
/*
* main()
*
* argv[1] may contain URL to call.
*/
int main(int argc, char *argv[])
{
pjsua_acc_id acc_id;
pj_status_t status;
pjsua_config cfg;
pjsua_logging_config log_cfg;
pjsua_transport_config UDPcfg;
/* Create pjsua first! */
status = pjsua_create();
if (status != PJ_SUCCESS) error_exit("Error in pjsua_create()", status);
/* If argument is specified, it's got to be a valid SIP URL */
if (argc > 1) {
status = pjsua_verify_url(argv[1]);
if (status != PJ_SUCCESS) error_exit("Invalid URL in argv", status);
}
/* Init pjsua */
pjsua_config_default(&cfg);
cfg.cb.on_incoming_call = &on_incoming_call;
cfg.cb.on_call_media_state = &on_call_media_state;
cfg.cb.on_call_state = &on_call_state;
pjsua_logging_config_default(&log_cfg);
log_cfg.console_level = 4;
status = pjsua_init(&cfg, &log_cfg, NULL);
if (status != PJ_SUCCESS) error_exit("Error in pjsua_init()", status);
/* Add UDP transport. */
pjsua_transport_config_default(&UDPcfg);
UDPcfg.port = PORT;
status = pjsua_transport_create(PJSIP_TRANSPORT_UDP, &UDPcfg, NULL);
if (status != PJ_SUCCESS) error_exit("Error creating transport", status);
/* Initialization is done, now start pjsua */
status = pjsua_start();
if (status != PJ_SUCCESS) error_exit("Error starting pjsua", status);
/* Register to SIP server by creating SIP account. */
pjsua_acc_config ACCcfg;
pjsua_acc_config_default(&ACCcfg);
ACCcfg.id = pj_str("sip:" SIP_USER "@" SIP_DOMAIN);
ACCcfg.reg_uri = pj_str("sip:" SIP_DOMAIN);
ACCcfg.cred_count = 1;
ACCcfg.cred_info[0].realm = pj_str(SIP_DOMAIN);
ACCcfg.cred_info[0].scheme = pj_str("digest");
ACCcfg.cred_info[0].username = pj_str(SIP_USER);
ACCcfg.cred_info[0].data_type = PJSIP_CRED_DATA_PLAIN_PASSWD;
ACCcfg.cred_info[0].data = pj_str(SIP_PASSWD);
status = pjsua_acc_add(&ACCcfg, PJ_TRUE, &acc_id);
if (status != PJ_SUCCESS) error_exit("Error adding account", status);
/* If URL is specified, make call to the URL. */
if (argc > 1) {
pj_str_t uri = pj_str(argv[1]);
status = pjsua_call_make_call(acc_id, &uri, 0, NULL, NULL, NULL);
if (status != PJ_SUCCESS) error_exit("Error making call", status);
}
/* Wait until user press "q" to quit. */
for (;;) {
char option[10];
puts("Press 'h' to hangup all calls, 'q' to quit");
if (fgets(option, sizeof(option), stdin) == NULL) {
puts("EOF while reading stdin, will quit now..");
break;
}
if (option[0] == 'q')
break;
if (option[0] == 'h')
pjsua_call_hangup_all();
}
/* Destroy pjsua */
pjsua_destroy();
return 0;
}