| 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 |
#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 |
|
| 50 |
static 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 |
|
| 65 |
pjsua_call_answer(call_id, 200, NULL, NULL); |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
static 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 |
|
| 82 |
static 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 |
|
| 90 |
pjsua_conf_connect(ci.conf_slot, 0); |
| 91 |
pjsua_conf_connect(0, ci.conf_slot); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
static 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 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
int main(int argc, char *argv[]) |
| 109 |
{ |
| 110 |
pjsua_acc_id acc_id; |
| 111 |
pj_status_t status; |
| 112 |
|
| 113 |
|
| 114 |
status = pjsua_create(); |
| 115 |
if (status != PJ_SUCCESS) error_exit("Error in pjsua_create()", status); |
| 116 |
|
| 117 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 151 |
status = pjsua_start(); |
| 152 |
if (status != PJ_SUCCESS) error_exit("Error starting pjsua", status); |
| 153 |
|
| 154 |
|
| 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 |
|
| 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 |
|
| 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') |
| 193 |
pjsua_call_hangup_all(); |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
pjsua_destroy(); |
| 198 |
|
| 199 |
return 0; |
| 200 |
} |