-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsipua.c
350 lines (325 loc) · 10.5 KB
/
sipua.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#include <pjsua.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <pthread.h>
#define THIS_FILE "sip.c"
int bShutdown = 0;
/* define callbacks */
void on_reg_started(pjsua_acc_id acc_id, pj_bool_t renew)
{
printf("on register started\n");
}
void on_reg_state(pjsua_acc_id acc_id)
{
printf("on register status\n");
}
void on_reg_state2(pjsua_acc_id acc_id, pjsua_reg_info *info)
{
printf("on register status 2\n");
}
void on_call_state(pjsua_call_id call_id, pjsip_event *e)
{
pjsua_call_info call_info;
printf("on_call_status\n");
pjsua_call_get_info(call_id, &call_info);
if (call_info.state == PJSIP_INV_STATE_DISCONNECTED)
{
printf("PJSIP_INV_STATE_DISCONNECTED\n");
}
}
void on_incoming_call(pjsua_acc_id acc_id, pjsua_call_id call_id,
pjsip_rx_data *rdata)
{
printf("on_incoming_call:incoming call to acc[%d]\n", acc_id);
}
void on_pager2(pjsua_call_id call_id, const pj_str_t *from,
const pj_str_t *to, const pj_str_t *contact,
const pj_str_t *mime_type, const pj_str_t *body,
pjsip_rx_data *rdata, pjsua_acc_id acc_id)
{
printf("message from %s\n", from->ptr);
}
static void
signalHandler(int signo)
{
printf("system is going to shutdown!\n");
bShutdown = 1;
}
pjsip_redirect_op on_call_redirected(pjsua_call_id call_id,
const pjsip_uri *target,
const pjsip_event *e)
{
return PJSIP_REDIRECT_ACCEPT;
}
static pthread_cond_t _cond;
static pthread_mutex_t _mutex;
void *test_thread(void *param)
{
int aid = *(int *)param;
pj_thread_t *thread;
pj_thread_desc desc;
pj_status_t rc = pj_thread_register ( "test_thread",
desc,
&thread);
pthread_mutex_lock(&_mutex);
printf("*******************************\n");
printf("test thread started\n");
printf("*******************************\n");
pthread_cond_wait(&_cond, &_mutex);
pj_str_t to = pj_str("sip:[email protected]");
pj_str_t mime_type = pj_str("text/plain");
pj_str_t content = pj_str("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
"<SIP_XML EventType=\"TimeSync\"/>"
"<Item operation=\"query\" time=\"0-0-0 0:0:0\"/>"
"</SIP_XML>");
pjsua_im_send(aid, &to, &mime_type, &content, NULL, NULL);
printf("*******************************\n");
printf("pjsua_im_send ok @ test_thread\n");
printf("*******************************\n");
pthread_mutex_unlock(&_mutex);
return NULL;
}
//void make_call()
//{
// pj_status_t status;
// pj_str_t dst_uri;
// pjsua_msg_data msg_data;
// pjsua_call_setting call_setting;
//
// status = pjsua_verify_url(call_cfg->req_uri.ptr);
// if (status != PJ_SUCCESS)
// {
// PJ_LOG(1,(THIS_FILE, ">>> Error verify req_uri in xpjsip_call_make_call"));
// return -1;
// }
// dst_uri = pj_str(call_cfg->req_uri.ptr);
//
// pjsua_msg_data_init(&msg_data);
// if (call_cfg->msg_body.mime.len && call_cfg->msg_body.body.len)
// {
// pj_cstr(&msg_data.content_type, call_cfg->msg_body.mime.ptr);
// pj_cstr(&msg_data.msg_body, call_cfg->msg_body.body.ptr);
// }
//
// pjsua_call_setting_default(&call_setting);
// status = pjsua_call_make_call(acc_id, &dst_uri, &call_setting, NULL, &msg_data, call_id);
// if (status != PJ_SUCCESS)
// {
// PJ_LOG(1,(THIS_FILE, ">>> Error make call in xpjsip_call_make_call"));
// return -1;
// }
//
// return 0;
//}
int main()
{
/* system variables */
pj_status_t status;
pjsua_config ua_cfg;
pjsua_acc_config acc_cfg;
pjsua_logging_config log_cfg;
pjsua_media_config media_cfg;
pjsua_transport_config udp_cfg;
pjsua_transport_id transport_id;
pjsua_acc_id local_aid;
pjsua_acc_id user_aid;
// used to send message
pj_str_t to;
pj_str_t mime_type;
pj_str_t content;
bShutdown = 0;
pthread_cond_init(&_cond, NULL);
pthread_mutex_init(&_mutex, NULL);
if ( signal( SIGINT, signalHandler ) == SIG_ERR )
{
exit( -1 );
}
if ( signal( SIGTERM, signalHandler ) == SIG_ERR )
{
exit( -1 );
}
/**
* create pjsua first
*/
status = pjsua_create();
if(status != PJ_SUCCESS) {
pjsua_perror(THIS_FILE, "Error initializing pjsua", status);
return status;
}
/**
* init pjsua framework
*/
/* configuration of ua */
pjsua_config_default(&(ua_cfg));
/**
* init callback
* set callback functions you need
* all the prototype of call are declared in pjsua.h
*/
// /* call related handle */
// ua_cfg.cb.on_call_state = on_call_state;
// ua_cfg.cb.on_incoming_call = on_incoming_call;
// ua_cfg.cb.on_call_tsx_state = on_call_tsx_state;
// ua_cfg.cb.on_call_media_state = on_call_media_state;
// ua_cfg.cb.on_call_sdp_created = on_call_sdp_created;
// ua_cfg.cb.on_stream_created = on_stream_created;
// ua_cfg.cb.on_stream_destroyed = on_stream_destroyed;
// ua_cfg.cb.on_dtmf_digit = on_dtmf_digit;
// ua_cfg.cb.on_call_transfer_request = on_call_transfer_request;
// ua_cfg.cb.on_call_transfer_request2 = on_call_transfer_request2;
// ua_cfg.cb.on_call_transfer_status = on_call_transfer_status;
// ua_cfg.cb.on_call_replace_request = on_call_replace_request;
// ua_cfg.cb.on_call_replace_request2 = on_call_replace_request2;
// ua_cfg.cb.on_call_replaced = on_call_replaced;
// ua_cfg.cb.on_call_rx_offer = on_call_rx_offer;
// /* register handle */
ua_cfg.cb.on_reg_started = on_reg_started;
ua_cfg.cb.on_reg_state = on_reg_state;
ua_cfg.cb.on_reg_state2 = on_reg_state2;
// /* subscirbe handle */
// ua_cfg.cb.on_incoming_subscribe = on_incoming_subscribe;
// ua_cfg.cb.on_srv_subscribe_state = on_srv_subscribe_state;
// ua_cfg.cb.on_buddy_state = on_buddy_state;
// ua_cfg.cb.on_buddy_evsub_state = on_buddy_evsub_state;
// /* message handle */
// ua_cfg.cb.on_pager = on_pager;
ua_cfg.cb.on_pager2 = on_pager2;
// ua_cfg.cb.on_pager_status = on_pager_status;
// ua_cfg.cb.on_pager_status2 = on_pager_status2;
// ua_cfg.cb.on_typing = on_typing;
// ua_cfg.cb.on_typing2 = on_typing2;
// /* network handle */
// ua_cfg.cb.on_nat_detect = on_nat_detect;
ua_cfg.cb.on_call_redirected = on_call_redirected;
// ua_cfg.cb.on_mwi_state = on_mwi_state;
// ua_cfg.cb.on_mwi_info = on_mwi_info;
// ua_cfg.cb.on_transport_state = on_transport_state;
// ua_cfg.cb.on_call_media_transport_state = on_call_media_transport_state;
// ua_cfg.cb.on_ice_transport_error = on_ice_transport_error;
// ua_cfg.cb.on_snd_dev_operation = on_snd_dev_operation;
// /* media module handle */
// ua_cfg.cb.on_call_media_event = on_call_media_event;
// ua_cfg.cb.on_create_media_transport = on_create_media_transport;
/* configure logging */
pjsua_logging_config_default(&(log_cfg));
log_cfg.msg_logging = PJ_FALSE;
/* configure media */
pjsua_media_config_default(&(media_cfg));
status = pjsua_init(&(ua_cfg),
&(log_cfg),
NULL);
// &(media_cfg));
if(status != PJ_SUCCESS) {
pjsua_perror(THIS_FILE, "Error initializing pjsua", status);
return status;
}
status = pjsua_start();
if(status != PJ_SUCCESS) {
pjsua_perror(THIS_FILE, "Error starting pjsua", status);
return status;
}
/**
* Setup transport
*/
pjsua_transport_config_default(&udp_cfg);
udp_cfg.port = 5091;
status = pjsua_transport_create(PJSIP_TRANSPORT_UDP, &udp_cfg, &transport_id);
if(status != PJ_SUCCESS)
return -1;
/**
* add local account (sip:domain) which handle sip messages that don't matches any
* other accounts
*/
pjsua_acc_add_local(transport_id, PJ_TRUE, &local_aid);
/**
* add user account and set it to default account
*/
pjsua_acc_config_default(&acc_cfg);
acc_cfg.id = pj_str("sip:[email protected]");
acc_cfg.reg_uri = pj_str("sip:192.168.229.142");
acc_cfg.reg_timeout = 60;
acc_cfg.cred_count = 1;
acc_cfg.cred_info[0].realm = pj_str("192.168.229.142");
acc_cfg.cred_info[0].scheme = pj_str("digest");
acc_cfg.cred_info[0].username = pj_str("8888");
acc_cfg.cred_info[0].data_type = PJSIP_CRED_DATA_PLAIN_PASSWD;
acc_cfg.cred_info[0].data = pj_str("SipPwd");
acc_cfg.proxy_cnt = 1;
acc_cfg.proxy[0] = pj_str("sip:192.168.229.142;lr");
/**
* register_on_acc_add is true by default, add:
*
* acc_cfg.register_on_acc_add = PJ_FALSE;
*
* to disable register on add.add later you can call the following
* code to send a REGISTER.
*
* pjsua_acc_set_registration(id_of_the_account, PJ_TRUE);
*
*/
status = pjsua_acc_add(&acc_cfg, PJ_TRUE, &user_aid);
sleep(2);
/**
* unregister account before exit
*/
to = pj_str("sip:[email protected]");
to = pj_str("sip:[email protected]");
mime_type = pj_str("text/plain");
content = pj_str("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
"<SIP_XML EventType=\"TimeSync\">"
"<Item operation=\"query\" time=\"0-0-0 0:0:0\"/>"
"</SIP_XML>");
content = pj_str("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
"<SIP_XML EventType=\"MediaPlay\">"
"<User uri=\"sip:[email protected]\"/>"
"<Media type=\"1\" id=\"000001-001\"/>"
"</SIP_XML>");
content = pj_str("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
"<SIP_XML EventType=\"MediaTeardown\">"
"<User uri=\"sip:[email protected]\"/>"
"<Media type=\"1\" id=\"000001-001\"/>"
"</SIP_XML>");
{
pthread_t tid;
// pthread_create(&tid, NULL, test_thread, &user_aid);
}
// pjsua_im_send(user_aid, &to, &mime_type, &content, NULL, NULL);
sleep(1);
printf("*******************************\n");
printf("pjsua_im_send ok @ main\n");
printf("*******************************\n");
// pthread_mutex_lock(&_mutex);
// pthread_cond_signal(&_cond);
// pthread_mutex_unlock(&_mutex);
// pjsua_call_setting setting;
// pjsua_call_setting_default(&setting);
// setting.aud_cnt = 0;
// setting.vid_cnt = 0;
// pjsua_call_id cid;
// pjsua_msg_data msg_data;
// pjsua_msg_data_init(&msg_data);
// msg_data.content_type = pj_str("application/sdp");
// msg_data.msg_body = pj_str(
// "v=0\r\n"
// "o=adapter 0 0 IN IP4 0.0.0.0\r\n"
// "s=-\r\n"
// "c=IN IP4 192.168.1.147\r\n"
// "t=1371053400 1371057000\r\n"
// "m=video 6000 TCP 96\r\n"
// "a=rtpmap:98 L16/16000/2\r\n"
// "a=ResID:301002-0005\r\n"
// "a=WndIndex:1\r\n"
// "a=recvonly\r\n");
// sleep(2);
// if(pjsua_call_make_call(user_aid, &to, &setting, NULL, NULL, &cid) != PJ_SUCCESS)
// PJ_LOG(1, (__FILE__, "failed to make call\n"));
while(!bShutdown)
{
sleep(1);
}
// pjsua_acc_set_registration(user_aid, PJ_FALSE);
sleep(1);
return status;
}