Debian Codegraph @

Xpjproject/pjsip-apps/src/pjsua/pjsua_app.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3 * Copyright (C) 2003-2008 Benny Prijono <[email protected]>
4 *
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 #include "pjsua_app.h"
20
21 #define THIS_FILE "pjsua_app.c"
22
23 //#define STEREO_DEMO
24 //#define TRANSPORT_ADAPTER_SAMPLE
25 //#define HAVE_MULTIPART_TEST
26
27 /* Ringtones US UK */
28 #define RINGBACK_FREQ1 440 /* 400 */
29 #define RINGBACK_FREQ2 480 /* 450 */
30 #define RINGBACK_ON 2000 /* 400 */
31 #define RINGBACK_OFF 4000 /* 200 */
32 #define RINGBACK_CNT 1 /* 2 */
33 #define RINGBACK_INTERVAL 4000 /* 2000 */
34
35 #define RING_FREQ1 800
36 #define RING_FREQ2 640
37 #define RING_ON 200
38 #define RING_OFF 100
39 #define RING_CNT 3
40 #define RING_INTERVAL 3000
41
42 #define current_acc pjsua_acc_get_default()
43
44 #ifdef STEREO_DEMO
45 static void stereo_demo();
46 #endif
47
48 #ifdef USE_GUI
49 pj_bool_t showNotification(pjsua_call_id call_id);
50 pj_bool_t reportCallState(pjsua_call_id call_id);
51 #endif
52
53 static void ringback_start(pjsua_call_id call_id);
54 static void ring_start(pjsua_call_id call_id);
55 static void ring_stop(pjsua_call_id call_id);
56 static pj_status_t app_init(void);
57 static pj_status_t app_destroy(void);
58
59 static pjsua_app_cfg_t app_cfg;
60 pj_str_t uri_arg;
61 pj_bool_t app_running = PJ_FALSE;
62
63 /*****************************************************************************
64 * Configuration manipulation
65 */
66
67 /*****************************************************************************
68 * Callback
69 */
70 static void ringback_start(pjsua_call_id call_id)
71 {
72 if (app_config.no_tones)
73 return;
74
75 if (app_config.call_data[call_id].ringback_on)
76 return;
77
78 app_config.call_data[call_id].ringback_on = PJ_TRUE;
79
80 if (++app_config.ringback_cnt==1 &&
81 app_config.ringback_slot!=PJSUA_INVALID_ID)
82 {
83 pjsua_conf_connect(app_config.ringback_slot, 0);
84 }
85 }
86
87 static void ring_stop(pjsua_call_id call_id)
88 {
89 if (app_config.no_tones)
90 return;
91
92 if (app_config.call_data[call_id].ringback_on) {
93 app_config.call_data[call_id].ringback_on = PJ_FALSE;
94
95 pj_assert(app_config.ringback_cnt>0);
96 if (--app_config.ringback_cnt == 0 &&
97 app_config.ringback_slot!=PJSUA_INVALID_ID)
98 {
99 pjsua_conf_disconnect(app_config.ringback_slot, 0);
100 pjmedia_tonegen_rewind(app_config.ringback_port);
101 }
102 }
103
104 if (app_config.call_data[call_id].ring_on) {
105 app_config.call_data[call_id].ring_on = PJ_FALSE;
106
107 pj_assert(app_config.ring_cnt>0);
108 if (--app_config.ring_cnt == 0 &&
109 app_config.ring_slot!=PJSUA_INVALID_ID)
110 {
111 pjsua_conf_disconnect(app_config.ring_slot, 0);
112 pjmedia_tonegen_rewind(app_config.ring_port);
113 }
114 }
115 }
116
117 static void ring_start(pjsua_call_id call_id)
118 {
119 if (app_config.no_tones)
120 return;
121
122 if (app_config.call_data[call_id].ring_on)
123 return;
124
125 app_config.call_data[call_id].ring_on = PJ_TRUE;
126
127 if (++app_config.ring_cnt==1 &&
128 app_config.ring_slot!=PJSUA_INVALID_ID)
129 {
130 pjsua_conf_connect(app_config.ring_slot, 0);
131 }
132 }
133
134 /* Callback from timer when the maximum call duration has been
135 * exceeded.
136 */
137 static void call_timeout_callback(pj_timer_heap_t *timer_heap,
138 struct pj_timer_entry *entry)
139 {
140 pjsua_call_id call_id = entry->id;
141 pjsua_msg_data msg_data_;
142 pjsip_generic_string_hdr warn;
143 pj_str_t hname = pj_str("Warning");
144 pj_str_t hvalue = pj_str("399 pjsua \"Call duration exceeded\"");
145
146 PJ_UNUSED_ARG(timer_heap);
147
148 if (call_id == PJSUA_INVALID_ID) {
149 PJ_LOG(1,(THIS_FILE, "Invalid call ID in timer callback"));
150 return;
151 }
152
153 /* Add warning header */
154 pjsua_msg_data_init(&msg_data_);
155 pjsip_generic_string_hdr_init2(&warn, &hname, &hvalue);
156 pj_list_push_back(&msg_data_.hdr_list, &warn);
157
158 /* Call duration has been exceeded; disconnect the call */
159 PJ_LOG(3,(THIS_FILE, "Duration (%d seconds) has been exceeded "
160 "for call %d, disconnecting the call",
161 app_config.duration, call_id));
162 entry->id = PJSUA_INVALID_ID;
163 pjsua_call_hangup(call_id, 200, NULL, &msg_data_);
164 }
165
166 /*
167 * Handler when invite state has changed.
168 */
169 static void on_call_state(pjsua_call_id call_id, pjsip_event *e)
170 {
171 pjsua_call_info call_info;
172
173 PJ_UNUSED_ARG(e);
174
175 #ifdef USE_GUI
176 reportCallState(call_id);
177 #endif
178
179 pjsua_call_get_info(call_id, &call_info);
180
181 if (call_info.state == PJSIP_INV_STATE_DISCONNECTED) {
182
183 /* Stop all ringback for this call */
184 ring_stop(call_id);
185
186 /* Cancel duration timer, if any */
187 if (app_config.call_data[call_id].timer.id != PJSUA_INVALID_ID) {
188 app_call_data *cd = &app_config.call_data[call_id];
189 pjsip_endpoint *endpt = pjsua_get_pjsip_endpt();
190
191 cd->timer.id = PJSUA_INVALID_ID;
192 pjsip_endpt_cancel_timer(endpt, &cd->timer);
193 }
194
195 /* Rewind play file when hangup automatically,
196 * since file is not looped
197 */
198 if (app_config.auto_play_hangup)
199 pjsua_player_set_pos(app_config.wav_id, 0);
200
201 /* Cleanup dynamic playback if attached to this call */
202 if (app_config.dyn_player_active &&
203 app_config.dyn_player_call == call_id)
204 {
205 PJ_LOG(3, (THIS_FILE, "Call ended, stopping playback"));
206
207 /* Disconnect and destroy player if created */
208 if (app_config.dyn_player_id != PJSUA_INVALID_ID) {
209 /* Destroy player (connections to call's slot will be cleaned
210 * up automatically as the call is terminating) */
211 pjsua_player_destroy(app_config.dyn_player_id);
212 }
213
214 /* Reset state */
215 app_config.dyn_player_id = PJSUA_INVALID_ID;
216 app_config.dyn_player_port = PJSUA_INVALID_ID;
217 app_config.dyn_player_call = PJSUA_INVALID_ID;
218 app_config.dyn_player_active = PJ_FALSE;
219 app_config.dyn_play_filename[0] = '\0';
220 }
221
222 /* Cleanup dynamic recording if attached to this call */
223 if (app_config.dyn_rec_active &&
224 app_config.dyn_rec_call == call_id)
225 {
226 PJ_LOG(3, (THIS_FILE, "Call ended, stopping recording"));
227
228 /* Disconnect and destroy recorder if created */
229 if (app_config.dyn_rec_id != PJSUA_INVALID_ID) {
230 /* Disconnect microphone from recorder (mic is always slot 0)
231 * (call's slot is being torn down, so that disconnects automatically) */
232 pjsua_conf_disconnect(0, app_config.dyn_rec_port);
233
234 /* Destroy recorder */
235 pjsua_recorder_destroy(app_config.dyn_rec_id);
236 }
237
238 /* Reset state */
239 app_config.dyn_rec_id = PJSUA_INVALID_ID;
240 app_config.dyn_rec_port = PJSUA_INVALID_ID;
241 app_config.dyn_rec_call = PJSUA_INVALID_ID;
242 app_config.dyn_rec_active = PJ_FALSE;
243 app_config.dyn_rec_filename[0] = '\0';
244 }
245
246 PJ_LOG(3,(THIS_FILE, "Call %d is DISCONNECTED [reason=%d (%.*s)]",
247 call_id,
248 call_info.last_status,
249 (int)call_info.last_status_text.slen,
250 call_info.last_status_text.ptr));
251
252 if (call_id == current_call) {
253 find_next_call();
254 }
255
256 /* Dump media state upon disconnected.
257 * Now pjsua_media_channel_deinit() automatically log the call dump.
258 */
259 if (0) {
260 PJ_LOG(5,(THIS_FILE,
261 "Call %d disconnected, dumping media stats..",
262 call_id));
263 log_call_dump(call_id);
264 }
265
266 } else {
267
268 if (app_config.duration != PJSUA_APP_NO_LIMIT_DURATION &&
269 call_info.state == PJSIP_INV_STATE_CONFIRMED)
270 {
271 /* Schedule timer to hangup call after the specified duration */
272 app_call_data *cd = &app_config.call_data[call_id];
273 pjsip_endpoint *endpt = pjsua_get_pjsip_endpt();
274 pj_time_val delay;
275
276 cd->timer.id = call_id;
277 delay.sec = app_config.duration;
278 delay.msec = 0;
279 pjsip_endpt_schedule_timer(endpt, &cd->timer, &delay);
280 }
281
282 if (call_info.state == PJSIP_INV_STATE_EARLY) {
283 int code;
284 pj_str_t reason;
285 pjsip_msg *msg;
286
287 /* This can only occur because of TX or RX message */
288 pj_assert(e->type == PJSIP_EVENT_TSX_STATE);
289
290 if (e->body.tsx_state.type == PJSIP_EVENT_RX_MSG) {
291 msg = e->body.tsx_state.src.rdata->msg_info.msg;
292 } else {
293 msg = e->body.tsx_state.src.tdata->msg;
294 }
295
296 code = msg->line.status.code;
297 reason = msg->line.status.reason;
298
299 /* Start ringback for 180 for UAC unless there's SDP in 180 */
300 if (call_info.role==PJSIP_ROLE_UAC && code==180 &&
301 msg->body == NULL &&
302 call_info.media_status==PJSUA_CALL_MEDIA_NONE)
303 {
304 ringback_start(call_id);
305 }
306
307 PJ_LOG(3,(THIS_FILE, "Call %d state changed to %.*s (%d %.*s)",
308 call_id, (int)call_info.state_text.slen,
309 call_info.state_text.ptr, code,
310 (int)reason.slen, reason.ptr));
311 } else {
312 PJ_LOG(3,(THIS_FILE, "Call %d state changed to %.*s",
313 call_id,
314 (int)call_info.state_text.slen,
315 call_info.state_text.ptr));
316 }
317
318 if (current_call==PJSUA_INVALID_ID)
319 current_call = call_id;
320
321 }
322 }
323
324 /*
325 * Handler when audio stream is destroyed.
326 */
327 static void on_stream_destroyed(pjsua_call_id call_id,
328 pjmedia_stream *strm,
329 unsigned stream_idx)
330 {
331 PJ_UNUSED_ARG(strm);
332
333 /* Now pjsua_media_channel_deinit() automatically log the call dump. */
334 if (0) {
335 PJ_LOG(5,(THIS_FILE,
336 "Call %d stream %d destroyed, dumping media stats..",
337 call_id, stream_idx));
338 log_call_dump(call_id);
339 }
340 }
341
342 /**
343 * Handler when there is incoming call.
344 */
345 static void on_incoming_call(pjsua_acc_id acc_id, pjsua_call_id call_id,
346 pjsip_rx_data *rdata)
347 {
348 pjsua_call_info call_info;
349
350 PJ_UNUSED_ARG(acc_id);
351 PJ_UNUSED_ARG(rdata);
352
353 pjsua_call_get_info(call_id, &call_info);
354
355 if (current_call==PJSUA_INVALID_ID)
356 current_call = call_id;
357
358 #ifdef USE_GUI
359 showNotification(call_id);
360 #endif
361
362 /* Start ringback */
363 if (call_info.rem_aud_cnt)
364 ring_start(call_id);
365
366 if (app_config.auto_answer > 0) {
367 pjsua_call_setting opt;
368
369 pjsua_call_setting_default(&opt);
370 opt.aud_cnt = app_config.aud_cnt;
371 opt.vid_cnt = app_config.vid.vid_cnt;
372 opt.txt_cnt = app_config.txt_cnt;
373
374 pjsua_call_answer2(call_id, &opt, app_config.auto_answer, NULL,
375 NULL);
376 }
377
378 if (app_config.auto_answer < 200) {
379 char notif_st[80] = {0};
380
381 #if PJSUA_HAS_VIDEO
382 if (call_info.rem_offerer && call_info.rem_vid_cnt) {
383 snprintf(notif_st, sizeof(notif_st),
384 "To %s the video, type \"vid %s\" first, "
385 "before answering the call!\n",
386 (app_config.vid.vid_cnt? "reject":"accept"),
387 (app_config.vid.vid_cnt? "disable":"enable"));
388 }
389 #endif
390
391 PJ_LOG(3,(THIS_FILE,
392 "Incoming call for account %d!\n"
393 "Media count: %d audio & %d video & %d text\n"
394 "%s"
395 "From: %.*s\n"
396 "To: %.*s\n"
397 "Press %s to answer or %s to reject call",
398 acc_id,
399 call_info.rem_aud_cnt,
400 call_info.rem_vid_cnt,
401 call_info.rem_txt_cnt,
402 notif_st,
403 (int)call_info.remote_info.slen,
404 call_info.remote_info.ptr,
405 (int)call_info.local_info.slen,
406 call_info.local_info.ptr,
407 (app_config.use_cli?"ca a":"a"),
408 (app_config.use_cli?"g":"h")));
409 }
410 }
411
412 /* General processing for media state. "mi" is the media index */
413 static void on_call_generic_media_state(pjsua_call_info *ci, unsigned mi,
414 pj_bool_t *has_error)
415 {
416 const char *status_name[] = {
417 "None",
418 "Active",
419 "Local hold",
420 "Remote hold",
421 "Error"
422 };
423
424 PJ_UNUSED_ARG(has_error);
425
426 pj_assert(ci->media[mi].status <= PJ_ARRAY_SIZE(status_name));
427 pj_assert(PJSUA_CALL_MEDIA_ERROR == 4);
428
429 PJ_LOG(4,(THIS_FILE, "Call %d media %d [type=%s], status is %s",
430 ci->id, mi, pjmedia_type_name(ci->media[mi].type),
431 status_name[ci->media[mi].status]));
432 }
433
434 /* Process audio media state. "mi" is the media index. */
435 static void on_call_audio_state(pjsua_call_info *ci, unsigned mi,
436 pj_bool_t *has_error)
437 {
438 PJ_UNUSED_ARG(has_error);
439
440 /* Stop ringback */
441 ring_stop(ci->id);
442
443 /* Connect ports appropriately when media status is ACTIVE or REMOTE HOLD,
444 * otherwise we should NOT connect the ports.
445 */
446 if (ci->media[mi].status == PJSUA_CALL_MEDIA_ACTIVE ||
447 ci->media[mi].status == PJSUA_CALL_MEDIA_REMOTE_HOLD)
448 {
449 pj_bool_t connect_sound = PJ_TRUE;
450 pj_bool_t disconnect_mic = PJ_FALSE;
451 pjsua_conf_port_id call_conf_slot;
452
453 call_conf_slot = ci->media[mi].stream.aud.conf_slot;
454
455 /* Make sure conf slot is valid (e.g: media dir is not "inactive") */
456 if (call_conf_slot == PJSUA_INVALID_ID)
457 return;
458
459 /* Loopback sound, if desired */
460 if (app_config.auto_loop) {
461 pjsua_conf_connect(call_conf_slot, call_conf_slot);
462 connect_sound = PJ_FALSE;
463 }
464
465 /* Automatically record conversation, if desired */
466 if (app_config.auto_rec && app_config.rec_port != PJSUA_INVALID_ID) {
467 pjsua_conf_connect(call_conf_slot, app_config.rec_port);
468 }
469
470 /* Dynamic recording - start if queued */
471 if (app_config.dyn_rec_active &&
472 app_config.dyn_rec_id == PJSUA_INVALID_ID &&
473 app_config.dyn_rec_filename[0] != '\0')
474 {
475 pj_status_t status;
476 pjsua_recorder_id rec_id;
477 pjsua_conf_port_id rec_port;
478 pj_str_t rec_file;
479
480 /* Create recorder */
481 rec_file = pj_str(app_config.dyn_rec_filename);
482 status = pjsua_recorder_create(&rec_file, 0, NULL, 0, 0, &rec_id);
483 if (status == PJ_SUCCESS) {
484 rec_port = pjsua_recorder_get_conf_port(rec_id);
485
486 /* Connect call to recorder */
487 pjsua_conf_connect(call_conf_slot, rec_port);
488
489 /* Connect microphone to recorder */
490 pjsua_conf_connect(0, rec_port);
491
492 /* Save state */
493 app_config.dyn_rec_id = rec_id;
494 app_config.dyn_rec_port = rec_port;
495 app_config.dyn_rec_call = ci->id;
496
497 PJ_LOG(3, (THIS_FILE, "Dynamic recording auto-started"));
498 } else {
499 PJ_LOG(2, (THIS_FILE, "Failed to auto-start recording: %d", status));
500 app_config.dyn_rec_active = PJ_FALSE;
501 app_config.dyn_rec_filename[0] = '\0';
502 }
503 }
504
505 /* Record audio into AVI, if desired */
506 if (app_config.avi_auto_rec && app_config.avi_rec_audio &&
507 app_config.avi_aud_slot != PJSUA_INVALID_ID)
508 {
509 pjsua_conf_connect(call_conf_slot, app_config.avi_aud_slot);
510 }
511
512 /* Stream a file, if desired */
513 if ((app_config.auto_play || app_config.auto_play_hangup) &&
514 app_config.wav_port != PJSUA_INVALID_ID)
515 {
516 pjsua_conf_connect(app_config.wav_port, call_conf_slot);
517 connect_sound = PJ_FALSE;
518 }
519
520 /* Dynamic playback - start if queued */
521 if (app_config.dyn_player_active &&
522 app_config.dyn_player_id == PJSUA_INVALID_ID &&
523 app_config.dyn_play_filename[0] != '\0')
524 {
525 pj_status_t status;
526 pjsua_player_id player_id;
527 pjsua_conf_port_id player_port;
528 pj_str_t play_file;
529
530 /* Create player */
531 play_file = pj_str(app_config.dyn_play_filename);
532 status = pjsua_player_create(&play_file, 0, &player_id);
533 if (status == PJ_SUCCESS) {
534 player_port = pjsua_player_get_conf_port(player_id);
535
536 /* Connect player to call */
537 pjsua_conf_connect(player_port, call_conf_slot);
538
539 /* Disconnect microphone (like auto-play) */
540 pjsua_conf_disconnect(0, call_conf_slot);
541
542 /* Save state */
543 app_config.dyn_player_id = player_id;
544 app_config.dyn_player_port = player_port;
545 app_config.dyn_player_call = ci->id;
546
547 PJ_LOG(3, (THIS_FILE, "Dynamic playback auto-started"));
548 connect_sound = PJ_FALSE;
549 } else {
550 PJ_LOG(2, (THIS_FILE, "Failed to auto-start playback: %d", status));
551 app_config.dyn_player_active = PJ_FALSE;
552 app_config.dyn_play_filename[0] = '\0';
553 }
554 }
555
556 /* Stream AVI, if desired */
557 if (app_config.avi_auto_play &&
558 app_config.avi_def_idx != PJSUA_INVALID_ID &&
559 app_config.avi[app_config.avi_def_idx].slot != PJSUA_INVALID_ID)
560 {
561 pjsua_conf_connect(app_config.avi[app_config.avi_def_idx].slot,
562 call_conf_slot);
563 disconnect_mic = PJ_TRUE;
564 }
565
566 /* Put call in conference with other calls, if desired */
567 if (app_config.auto_conf) {
568 pjsua_call_id call_ids[PJSUA_MAX_CALLS];
569 unsigned call_cnt=PJ_ARRAY_SIZE(call_ids);
570 unsigned i;
571
572 /* Get all calls, and establish media connection between
573 * this call and other calls.
574 */
575 pjsua_enum_calls(call_ids, &call_cnt);
576
577 for (i=0; i<call_cnt; ++i) {
578 if (call_ids[i] == ci->id)
579 continue;
580
581 if (!pjsua_call_has_media(call_ids[i]))
582 continue;
583
584 pjsua_conf_connect(call_conf_slot,
585 pjsua_call_get_conf_port(call_ids[i]));
586 pjsua_conf_connect(pjsua_call_get_conf_port(call_ids[i]),
587 call_conf_slot);
588
589 /* Automatically record conversation, if desired */
590 if (app_config.auto_rec && app_config.rec_port !=
591 PJSUA_INVALID_ID)
592 {
593 pjsua_conf_connect(pjsua_call_get_conf_port(call_ids[i]),
594 app_config.rec_port);
595 }
596
597 }
598
599 /* Also connect call to local sound device */
600 connect_sound = PJ_TRUE;
601 }
602
603 /* Otherwise connect to sound device */
604 if (connect_sound) {
605 pjsua_conf_connect(call_conf_slot, 0);
606 if (!disconnect_mic)
607 pjsua_conf_connect(0, call_conf_slot);
608
609 /* Automatically record conversation, if desired */
610 if (app_config.auto_rec && app_config.rec_port != PJSUA_INVALID_ID)
611 {
612 pjsua_conf_connect(call_conf_slot, app_config.rec_port);
613 pjsua_conf_connect(0, app_config.rec_port);
614 }
615
616 /* Record audio into AVI, if desired */
617 if (app_config.avi_auto_rec && app_config.avi_rec_audio &&
618 app_config.avi_aud_slot != PJSUA_INVALID_ID)
619 {
620 pjsua_conf_connect(call_conf_slot, app_config.avi_aud_slot);
621 pjsua_conf_connect(0, app_config.avi_aud_slot);
622 }
623 }
624 }
625 }
626
627 /* Process video media state. "mi" is the media index. */
628 static void on_call_video_state(pjsua_call_info *ci, unsigned mi,
629 pj_bool_t *has_error)
630 {
631 if (ci->media[mi].status != PJSUA_CALL_MEDIA_ACTIVE)
632 return;
633
634 arrange_window(ci->media[mi].stream.vid.win_in);
635
636 #if PJMEDIA_HAS_VIDEO
637 if (app_config.avi_auto_rec &&
638 app_config.avi_vid_slot != PJSUA_INVALID_ID)
639 {
640 pjsua_conf_port_id pid;
641
642 pid = pjsua_call_get_vid_conf_port(ci->id, PJMEDIA_DIR_DECODING);
643 if (pid != PJSUA_INVALID_ID)
644 pjsua_vid_conf_connect(pid, app_config.avi_vid_slot, NULL);
645 }
646 #endif
647
648 PJ_UNUSED_ARG(has_error);
649 }
650
651 /*
652 * Callback on media state changed event.
653 * The action may connect the call to sound device, to file, or
654 * to loop the call.
655 */
656 static void on_call_media_state(pjsua_call_id call_id)
657 {
658 pjsua_call_info call_info;
659 unsigned mi;
660 pj_bool_t has_error = PJ_FALSE;
661
662 pjsua_call_get_info(call_id, &call_info);
663
664 for (mi=0; mi<call_info.media_cnt; ++mi) {
665 on_call_generic_media_state(&call_info, mi, &has_error);
666
667 switch (call_info.media[mi].type) {
668 case PJMEDIA_TYPE_AUDIO:
669 on_call_audio_state(&call_info, mi, &has_error);
670 break;
671 case PJMEDIA_TYPE_VIDEO:
672 on_call_video_state(&call_info, mi, &has_error);
673 break;
674 default:
675 /* Make gcc happy about enum not handled by switch/case */
676 break;
677 }
678 }
679
680 if (has_error) {
681 pj_str_t reason = pj_str("Media failed");
682 pjsua_call_hangup(call_id, 500, &reason, NULL);
683 }
684
685 #if PJSUA_HAS_VIDEO
686 /* Check if remote has just tried to enable video */
687 if (call_info.rem_offerer && call_info.rem_vid_cnt)
688 {
689 int vid_idx;
690
691 /* Check if there is active video */
692 vid_idx = pjsua_call_get_vid_stream_idx(call_id);
693 if (vid_idx == -1 || call_info.media[vid_idx].dir == PJMEDIA_DIR_NONE) {
694 PJ_LOG(3,(THIS_FILE,
695 "Just rejected incoming video offer on call %d, "
696 "use \"vid call enable %d\" or \"vid call add\" to "
697 "enable video!", call_id, vid_idx));
698 }
699 }
700 #endif
701 }
702
703 /*
704 * DTMF callback.
705 */
706 /*
707 static void call_on_dtmf_callback(pjsua_call_id call_id, int dtmf)
708 {
709 PJ_LOG(3,(THIS_FILE, "Incoming DTMF on call %d: %c", call_id, dtmf));
710 }
711 */
712
713 static void call_on_dtmf_callback2(pjsua_call_id call_id,
714 const pjsua_dtmf_info *info)
715 {
716 char duration[16];
717 char method[16];
718
719 duration[0] = '\0';
720
721 switch (info->method) {
722 case PJSUA_DTMF_METHOD_RFC2833:
723 pj_ansi_snprintf(method, sizeof(method), "RFC2833");
724 break;
725 case PJSUA_DTMF_METHOD_SIP_INFO:
726 pj_ansi_snprintf(method, sizeof(method), "SIP INFO");
727 pj_ansi_snprintf(duration, sizeof(duration), ":duration(%d)",
728 info->duration);
729 break;
730 };
731 PJ_LOG(3,(THIS_FILE, "Incoming DTMF on call %d: %c%s, using %s method",
732 call_id, info->digit, duration, method));
733 }
734
735 /* Incoming text stream callback. */
736 static void call_on_rx_text(pjsua_call_id call_id,
737 const pjsua_txt_stream_data *data)
738 {
739 if (data->text.slen == 0) {
740 PJ_LOG(4, (THIS_FILE, "Received empty T140 block with seq %d",
741 data->seq));
742 } else {
743 PJ_LOG(3, (THIS_FILE, "Incoming text on call %d, seq %d: %.*s "
744 "(%d bytes)", call_id, data->seq,
745 (int)data->text.slen, data->text.ptr,
746 (int)data->text.slen));
747 }
748 }
749
750 /*
751 * Redirection handler.
752 */
753 static pjsip_redirect_op call_on_redirected(pjsua_call_id call_id,
754 const pjsip_uri *target,
755 const pjsip_event *e)
756 {
757 PJ_UNUSED_ARG(e);
758
759 if (app_config.redir_op == PJSIP_REDIRECT_PENDING) {
760 char uristr[PJSIP_MAX_URL_SIZE];
761 int len;
762
763 len = pjsip_uri_print(PJSIP_URI_IN_FROMTO_HDR, target, uristr,
764 sizeof(uristr));
765 if (len < 1) {
766 pj_ansi_strxcpy(uristr, "--URI too long--", sizeof(uristr));
767 }
768
769 PJ_LOG(3,(THIS_FILE, "Call %d is being redirected to %.*s. "
770 "Press 'Ra' to accept+replace To header, 'RA' to accept, "
771 "'Rr' to reject, or 'Rd' to disconnect.",
772 call_id, len, uristr));
773 }
774
775 return app_config.redir_op;
776 }
777
778 /*
779 * Handler registration status has changed.
780 */
781 static void on_reg_state(pjsua_acc_id acc_id)
782 {
783 PJ_UNUSED_ARG(acc_id);
784
785 // Log already written.
786 }
787
788 /*
789 * Handler for incoming presence subscription request
790 */
791 static void on_incoming_subscribe(pjsua_acc_id acc_id,
792 pjsua_srv_pres *srv_pres,
793 pjsua_buddy_id buddy_id,
794 const pj_str_t *from,
795 pjsip_rx_data *rdata,
796 pjsip_status_code *code,
797 pj_str_t *reason,
798 pjsua_msg_data *msg_data_)
799 {
800 /* Just accept the request (the default behavior) */
801 PJ_UNUSED_ARG(acc_id);
802 PJ_UNUSED_ARG(srv_pres);
803 PJ_UNUSED_ARG(buddy_id);
804 PJ_UNUSED_ARG(from);
805 PJ_UNUSED_ARG(rdata);
806 PJ_UNUSED_ARG(code);
807 PJ_UNUSED_ARG(reason);
808 PJ_UNUSED_ARG(msg_data_);
809 }
810
811
812 /*
813 * Handler on buddy state changed.
814 */
815 static void on_buddy_state(pjsua_buddy_id buddy_id)
816 {
817 pjsua_buddy_info info;
818 pjsua_buddy_get_info(buddy_id, &info);
819
820 PJ_LOG(3,(THIS_FILE, "%.*s status is %.*s, subscription state is %s "
821 "(last termination reason code=%d %.*s)",
822 (int)info.uri.slen,
823 info.uri.ptr,
824 (int)info.status_text.slen,
825 info.status_text.ptr,
826 info.sub_state_name,
827 info.sub_term_code,
828 (int)info.sub_term_reason.slen,
829 info.sub_term_reason.ptr));
830 }
831
832
833 /*
834 * Handler on buddy dialog event state changed.
835 */
836 static void on_buddy_dlg_event_state(pjsua_buddy_id buddy_id)
837 {
838 pjsua_buddy_dlg_event_info info;
839 pjsua_buddy_get_dlg_event_info(buddy_id, &info);
840
841 PJ_LOG(3,(THIS_FILE, "%.*s dialog-info-state: %.*s, "
842 "dialog-info-entity: %.*s, dialog-id: %.*s, "
843 "dialog-call-id: %.*s, dialog-direction: %.*s, "
844 "dialog-state: %.*s, dialog-duration: %.*s, "
845 "local-identity: %.*s, local-target-uri: %.*s, "
846 "remote-identity: %.*s, remote-target-uri: %.*s, "
847 "dialog-local-tag: %.*s, dialog-remote-tag: %.*s, "
848 "subscription state: %s (last termination reason code=%d %.*s)",
849 (int)info.uri.slen, info.uri.ptr,
850 (int)info.dialog_info_state.slen, info.dialog_info_state.ptr,
851 (int)info.dialog_info_entity.slen, info.dialog_info_entity.ptr,
852 (int)info.dialog_id.slen, info.dialog_id.ptr,
853 (int)info.dialog_call_id.slen, info.dialog_call_id.ptr,
854 (int)info.dialog_direction.slen, info.dialog_direction.ptr,
855 (int)info.dialog_state.slen, info.dialog_state.ptr,
856 (int)info.dialog_duration.slen, info.dialog_duration.ptr,
857 (int)info.local_identity.slen, info.local_identity.ptr,
858 (int)info.local_target_uri.slen, info.local_target_uri.ptr,
859 (int)info.remote_identity.slen, info.remote_identity.ptr,
860 (int)info.remote_target_uri.slen, info.remote_target_uri.ptr,
861 (int)info.dialog_local_tag.slen, info.dialog_local_tag.ptr,
862 (int)info.dialog_remote_tag.slen, info.dialog_remote_tag.ptr,
863 info.sub_state_name, info.sub_term_code,
864 (int)info.sub_term_reason.slen, info.sub_term_reason.ptr));
865 }
866
867 /*
868 * Subscription state has changed.
869 */
870 static void on_buddy_evsub_state(pjsua_buddy_id buddy_id,
871 pjsip_evsub *sub,
872 pjsip_event *event)
873 {
874 char event_info[80];
875
876 PJ_UNUSED_ARG(sub);
877
878 event_info[0] = '\0';
879
880 if (event->type == PJSIP_EVENT_TSX_STATE &&
881 event->body.tsx_state.type == PJSIP_EVENT_RX_MSG)
882 {
883 pjsip_rx_data *rdata = event->body.tsx_state.src.rdata;
884 snprintf(event_info, sizeof(event_info),
885 " (RX %s)",
886 pjsip_rx_data_get_info(rdata));
887 }
888
889 PJ_LOG(4,(THIS_FILE,
890 "Buddy %d: subscription state: %s (event: %s%s)",
891 buddy_id, pjsip_evsub_get_state_name(sub),
892 pjsip_event_str(event->type),
893 event_info));
894
895 }
896
897 static void on_buddy_evsub_dlg_event_state(pjsua_buddy_id buddy_id,
898 pjsip_evsub *sub,
899 pjsip_event *event)
900 {
901 char event_info[80];
902
903 PJ_UNUSED_ARG(sub);
904
905 event_info[0] = '\0';
906
907 if (event->type == PJSIP_EVENT_TSX_STATE &&
908 event->body.tsx_state.type == PJSIP_EVENT_RX_MSG)
909 {
910 pjsip_rx_data *rdata = event->body.tsx_state.src.rdata;
911 snprintf(event_info, sizeof(event_info),
912 " (RX %s)",
913 pjsip_rx_data_get_info(rdata));
914 }
915
916 PJ_LOG(4,(THIS_FILE,
917 "Buddy %d: dialog event subscription state: %s (event: %s%s)",
918 buddy_id, pjsip_evsub_get_state_name(sub),
919 pjsip_event_str(event->type), event_info));
920 }
921
922
923 /**
924 * Incoming IM message (i.e. MESSAGE request)!
925 */
926 static void on_pager(pjsua_call_id call_id, const pj_str_t *from,
927 const pj_str_t *to, const pj_str_t *contact,
928 const pj_str_t *mime_type, const pj_str_t *text)
929 {
930 /* Note: call index may be -1 */
931 PJ_UNUSED_ARG(call_id);
932 PJ_UNUSED_ARG(to);
933 PJ_UNUSED_ARG(contact);
934 PJ_UNUSED_ARG(mime_type);
935
936 PJ_LOG(3,(THIS_FILE,"MESSAGE from %.*s: %.*s (%.*s)",
937 (int)from->slen, from->ptr,
938 (int)text->slen, text->ptr,
939 (int)mime_type->slen, mime_type->ptr));
940 }
941
942
943 /**
944 * Received typing indication
945 */
946 static void on_typing(pjsua_call_id call_id, const pj_str_t *from,
947 const pj_str_t *to, const pj_str_t *contact,
948 pj_bool_t is_typing)
949 {
950 PJ_UNUSED_ARG(call_id);
951 PJ_UNUSED_ARG(to);
952 PJ_UNUSED_ARG(contact);
953
954 PJ_LOG(3,(THIS_FILE, "IM indication: %.*s %s",
955 (int)from->slen, from->ptr,
956 (is_typing?"is typing..":"has stopped typing")));
957 }
958
959
960 /**
961 * Call transfer request status.
962 */
963 static void on_call_transfer_status(pjsua_call_id call_id,
964 int status_code,
965 const pj_str_t *status_text,
966 pj_bool_t final,
967 pj_bool_t *p_cont)
968 {
969 PJ_LOG(3,(THIS_FILE, "Call %d: transfer status=%d (%.*s) %s",
970 call_id, status_code,
971 (int)status_text->slen, status_text->ptr,
972 (final ? "[final]" : "")));
973
974 if (status_code/100 == 2) {
975 PJ_LOG(3,(THIS_FILE,
976 "Call %d: call transferred successfully, disconnecting call",
977 call_id));
978 pjsua_call_hangup(call_id, PJSIP_SC_GONE, NULL, NULL);
979 *p_cont = PJ_FALSE;
980 }
981 }
982
983
984 /*
985 * Notification that call is being replaced.
986 */
987 static void on_call_replaced(pjsua_call_id old_call_id,
988 pjsua_call_id new_call_id)
989 {
990 pjsua_call_info old_ci, new_ci;
991
992 pjsua_call_get_info(old_call_id, &old_ci);
993 pjsua_call_get_info(new_call_id, &new_ci);
994
995 PJ_LOG(3,(THIS_FILE, "Call %d with %.*s is being replaced by "
996 "call %d with %.*s",
997 old_call_id,
998 (int)old_ci.remote_info.slen, old_ci.remote_info.ptr,
999 new_call_id,
1000 (int)new_ci.remote_info.slen, new_ci.remote_info.ptr));
1001 }
1002
1003
1004 /*
1005 * NAT type detection callback.
1006 */
1007 static void on_nat_detect(const pj_stun_nat_detect_result *res)
1008 {
1009 if (res->status != PJ_SUCCESS) {
1010 pjsua_perror(THIS_FILE, "NAT detection failed", res->status);
1011 } else {
1012 PJ_LOG(3, (THIS_FILE, "NAT detected as %s", res->nat_type_name));
1013 }
1014 }
1015
1016
1017 /*
1018 * MWI indication
1019 */
1020 static void on_mwi_info(pjsua_acc_id acc_id, pjsua_mwi_info *mwi_info)
1021 {
1022 pj_str_t body;
1023
1024 PJ_LOG(3,(THIS_FILE, "Received MWI for acc %d:", acc_id));
1025
1026 if (mwi_info->rdata->msg_info.ctype) {
1027 const pjsip_ctype_hdr *ctype = mwi_info->rdata->msg_info.ctype;
1028
1029 PJ_LOG(3,(THIS_FILE, " Content-Type: %.*s/%.*s",
1030 (int)ctype->media.type.slen,
1031 ctype->media.type.ptr,
1032 (int)ctype->media.subtype.slen,
1033 ctype->media.subtype.ptr));
1034 }
1035
1036 if (!mwi_info->rdata->msg_info.msg->body) {
1037 PJ_LOG(3,(THIS_FILE, " no message body"));
1038 return;
1039 }
1040
1041 body.ptr = (char *)mwi_info->rdata->msg_info.msg->body->data;
1042 body.slen = mwi_info->rdata->msg_info.msg->body->len;
1043
1044 PJ_LOG(3,(THIS_FILE, " Body:\n%.*s", (int)body.slen, body.ptr));
1045 }
1046
1047
1048 /*
1049 * Transport status notification
1050 */
1051 static void on_transport_state(pjsip_transport *tp,
1052 pjsip_transport_state state,
1053 const pjsip_transport_state_info *info)
1054 {
1055 char host_port[128];
1056
1057 pj_addr_str_print(&tp->remote_name.host,
1058 tp->remote_name.port, host_port, sizeof(host_port), 1);
1059 switch (state) {
1060 case PJSIP_TP_STATE_CONNECTED:
1061 {
1062 PJ_LOG(3,(THIS_FILE, "SIP %s transport is connected to %s",
1063 tp->type_name, host_port));
1064 }
1065 break;
1066
1067 case PJSIP_TP_STATE_DISCONNECTED:
1068 {
1069 char buf[100];
1070 int len;
1071
1072 len = pj_ansi_snprintf(buf, sizeof(buf), "SIP %s transport is "
1073 "disconnected from %s", tp->type_name, host_port);
1074 PJ_CHECK_TRUNC_STR(len, buf, sizeof(buf));
1075 pjsua_perror(THIS_FILE, buf, info->status);
1076 }
1077 break;
1078
1079 default:
1080 break;
1081 }
1082
1083 #if defined(PJSIP_HAS_TLS_TRANSPORT) && PJSIP_HAS_TLS_TRANSPORT!=0
1084
1085 if (!pj_ansi_stricmp(tp->type_name, "tls") && info->ext_info &&
1086 (state == PJSIP_TP_STATE_CONNECTED ||
1087 ((pjsip_tls_state_info*)info->ext_info)->
1088 ssl_sock_info->verify_status != PJ_SUCCESS))
1089 {
1090 pjsip_tls_state_info *tls_info = (pjsip_tls_state_info*)info->ext_info;
1091 pj_ssl_sock_info *ssl_sock_info = tls_info->ssl_sock_info;
1092 char buf[2048];
1093 const char *verif_msgs[32];
1094 unsigned verif_msg_cnt;
1095
1096 /* Dump server TLS cipher */
1097 PJ_LOG(4,(THIS_FILE, "TLS cipher used: 0x%06X/%s",
1098 ssl_sock_info->cipher,
1099 pj_ssl_cipher_name(ssl_sock_info->cipher) ));
1100
1101 /* Dump server TLS certificate */
1102 pj_ssl_cert_info_dump(ssl_sock_info->remote_cert_info, " ",
1103 buf, sizeof(buf));
1104 PJ_LOG(4,(THIS_FILE, "TLS cert info of %s:\n%s", host_port, buf));
1105
1106 /* Dump server TLS certificate verification result */
1107 verif_msg_cnt = PJ_ARRAY_SIZE(verif_msgs);
1108 pj_ssl_cert_get_verify_status_strings(ssl_sock_info->verify_status,
1109 verif_msgs, &verif_msg_cnt);
1110 PJ_LOG(3,(THIS_FILE, "TLS cert verification result of %s : %s",
1111 host_port,
1112 (verif_msg_cnt == 1? verif_msgs[0]:"")));
1113 if (verif_msg_cnt > 1) {
1114 unsigned i;
1115 for (i = 0; i < verif_msg_cnt; ++i)
1116 PJ_LOG(3,(THIS_FILE, "- %s", verif_msgs[i]));
1117 }
1118
1119 if (ssl_sock_info->verify_status &&
1120 !app_config.udp_cfg.tls_setting.verify_server)
1121 {
1122 PJ_LOG(3,(THIS_FILE, "PJSUA is configured to ignore TLS cert "
1123 "verification errors"));
1124 }
1125 }
1126
1127 #endif
1128
1129 }
1130
1131 /*
1132 * Notification on ICE error.
1133 */
1134 static void on_ice_transport_error(int index, pj_ice_strans_op op,
1135 pj_status_t status, void *param)
1136 {
1137 PJ_UNUSED_ARG(op);
1138 PJ_UNUSED_ARG(param);
1139 PJ_PERROR(1,(THIS_FILE, status,
1140 "ICE keep alive failure for transport %d", index));
1141 }
1142
1143 /*
1144 * Notification on sound device operation.
1145 */
1146 static pj_status_t on_snd_dev_operation(int operation)
1147 {
1148 int cap_dev, play_dev;
1149
1150 pjsua_get_snd_dev(&cap_dev, &play_dev);
1151 PJ_LOG(3,(THIS_FILE, "Turning sound device %d %d %s", cap_dev, play_dev,
1152 (operation? "ON":"OFF")));
1153 return PJ_SUCCESS;
1154 }
1155
1156 static char *get_media_dir(pjmedia_dir dir) {
1157 switch (dir) {
1158 case PJMEDIA_DIR_ENCODING:
1159 return "TX";
1160 case PJMEDIA_DIR_DECODING:
1161 return "RX";
1162 case PJMEDIA_DIR_ENCODING+PJMEDIA_DIR_DECODING:
1163 return "TX+RX";
1164 default:
1165 return "unknown dir";
1166 }
1167 }
1168
1169 /* Callback on media events */
1170 static void on_call_media_event(pjsua_call_id call_id,
1171 unsigned med_idx,
1172 pjmedia_event *event)
1173 {
1174 char event_name[5];
1175
1176 PJ_LOG(5,(THIS_FILE, "Event %s",
1177 pjmedia_fourcc_name(event->type, event_name)));
1178
1179 if (event->type == PJMEDIA_EVENT_MEDIA_TP_ERR) {
1180 pjmedia_event_media_tp_err_data *err_data;
1181
1182 err_data = &event->data.med_tp_err;
1183 PJ_PERROR(3, (THIS_FILE, err_data->status,
1184 "Media transport error event (%s %s %s)",
1185 (err_data->type==PJMEDIA_TYPE_AUDIO)?"Audio":"Video",
1186 (err_data->is_rtp)?"RTP":"RTCP",
1187 get_media_dir(err_data->dir)));
1188 }
1189 #if PJSUA_HAS_VIDEO
1190 else if (event->type == PJMEDIA_EVENT_FMT_CHANGED) {
1191 /* Adjust renderer window size to original video size */
1192 pjsua_call_info ci;
1193
1194 pjsua_call_get_info(call_id, &ci);
1195
1196 if ((ci.media[med_idx].type == PJMEDIA_TYPE_VIDEO) &&
1197 (ci.media[med_idx].dir & PJMEDIA_DIR_DECODING))
1198 {
1199 pjsua_vid_win_id wid;
1200 pjmedia_rect_size size;
1201 pjsua_vid_win_info win_info;
1202
1203 wid = ci.media[med_idx].stream.vid.win_in;
1204 pjsua_vid_win_get_info(wid, &win_info);
1205
1206 size = event->data.fmt_changed.new_fmt.det.vid.size;
1207 if (size.w != win_info.size.w || size.h != win_info.size.h) {
1208 pjsua_vid_win_set_size(wid, &size);
1209
1210 /* Re-arrange video windows */
1211 arrange_window(PJSUA_INVALID_ID);
1212 }
1213 }
1214 }
1215 #else
1216 PJ_UNUSED_ARG(call_id);
1217 PJ_UNUSED_ARG(med_idx);
1218 #endif
1219 }
1220
1221 #ifdef TRANSPORT_ADAPTER_SAMPLE
1222 /*
1223 * This callback is called when media transport needs to be created.
1224 */
1225 static pjmedia_transport* on_create_media_transport(pjsua_call_id call_id,
1226 unsigned media_idx,
1227 pjmedia_transport *base_tp,
1228 unsigned flags)
1229 {
1230 pjmedia_transport *adapter;
1231 pj_status_t status;
1232
1233 /* Create the adapter */
1234 status = pjmedia_tp_adapter_create(pjsua_get_pjmedia_endpt(),
1235 NULL, base_tp,
1236 (flags & PJSUA_MED_TP_CLOSE_MEMBER),
1237 &adapter);
1238 if (status != PJ_SUCCESS) {
1239 PJ_PERROR(1,(THIS_FILE, status, "Error creating adapter"));
1240 return NULL;
1241 }
1242
1243 PJ_LOG(3,(THIS_FILE, "Media transport is created for call %d media %d",
1244 call_id, media_idx));
1245
1246 return adapter;
1247 }
1248 #endif
1249
1250 /* Playfile done notification, set timer to hangup calls */
1251 void on_playfile_done(pjmedia_port *port, void *usr_data)
1252 {
1253 pj_time_val delay;
1254
1255 PJ_UNUSED_ARG(port);
1256 PJ_UNUSED_ARG(usr_data);
1257
1258 /* Just rewind WAV when it is played outside of call */
1259 if (pjsua_call_get_count() == 0) {
1260 pjsua_player_set_pos(app_config.wav_id, 0);
1261 }
1262
1263 /* Timer is already active */
1264 if (app_config.auto_hangup_timer.id == 1)
1265 return;
1266
1267 app_config.auto_hangup_timer.id = 1;
1268 delay.sec = 0;
1269 delay.msec = 200; /* Give 200 ms before hangup */
1270 pjsip_endpt_schedule_timer(pjsua_get_pjsip_endpt(),
1271 &app_config.auto_hangup_timer,
1272 &delay);
1273 }
1274
1275 /* Callback to replace the generated SDP with a custom one (--custom-sdp).
1276 * Only compiled when PJSUA_MEDIA_HAS_PJMEDIA=0 (alt media backend). */
1277 #if !PJSUA_MEDIA_HAS_PJMEDIA
1278 static void on_call_sdp_created_cb(pjsua_call_id call_id,
1279 pjmedia_sdp_session *sdp,
1280 pj_pool_t *pool,
1281 const pjmedia_sdp_session *rem_sdp)
1282 {
1283 pjmedia_sdp_session *custom = NULL;
1284 pj_str_t sdp_str;
1285 pj_status_t status;
1286
1287 PJ_UNUSED_ARG(call_id);
1288 PJ_UNUSED_ARG(rem_sdp);
1289
1290 if (app_config.custom_sdp.slen == 0)
1291 return;
1292
1293 /* Make a writable copy since pjmedia_sdp_parse modifies the buffer */
1294 sdp_str.ptr = (char*)pj_pool_alloc(pool,
1295 (pj_size_t)(app_config.custom_sdp.slen + 1));
1296 pj_memcpy(sdp_str.ptr, app_config.custom_sdp.ptr,
1297 (pj_size_t)app_config.custom_sdp.slen);
1298 sdp_str.ptr[app_config.custom_sdp.slen] = '\0';
1299 sdp_str.slen = app_config.custom_sdp.slen;
1300
1301 status = pjmedia_sdp_parse(pool, sdp_str.ptr, (pj_size_t)sdp_str.slen,
1302 &custom);
1303 if (status != PJ_SUCCESS) {
1304 PJ_PERROR(1,(THIS_FILE, status,
1305 "Failed to parse custom SDP, using generated SDP"));
1306 return;
1307 }
1308
1309 pj_memcpy(sdp, custom, sizeof(*sdp));
1310 }
1311 #endif /* !PJSUA_MEDIA_HAS_PJMEDIA */
1312
1313 /* IP change progress callback. */
1314 void on_ip_change_progress(pjsua_ip_change_op op,
1315 pj_status_t status,
1316 const pjsua_ip_change_op_info *info)
1317 {
1318 char info_str[128];
1319 pjsua_acc_info acc_info;
1320 pjsua_transport_info tp_info;
1321
1322 if (status == PJ_SUCCESS) {
1323 switch (op) {
1324 case PJSUA_IP_CHANGE_OP_SHUTDOWN_TP:
1325 pj_ansi_snprintf(info_str, sizeof(info_str),
1326 "TCP/TLS transports shutdown");
1327 break;
1328
1329 case PJSUA_IP_CHANGE_OP_RESTART_LIS:
1330 pjsua_transport_get_info(info->lis_restart.transport_id, &tp_info);
1331 pj_ansi_snprintf(info_str, sizeof(info_str),
1332 "restart transport %.*s",
1333 (int)tp_info.info.slen, tp_info.info.ptr);
1334 break;
1335 case PJSUA_IP_CHANGE_OP_ACC_SHUTDOWN_TP:
1336 pjsua_acc_get_info(info->acc_shutdown_tp.acc_id, &acc_info);
1337
1338 pj_ansi_snprintf(info_str, sizeof(info_str),
1339 "transport shutdown for account %.*s",
1340 (int)acc_info.acc_uri.slen,
1341 acc_info.acc_uri.ptr);
1342 break;
1343 case PJSUA_IP_CHANGE_OP_ACC_UPDATE_CONTACT:
1344 pjsua_acc_get_info(info->acc_shutdown_tp.acc_id, &acc_info);
1345 if (info->acc_update_contact.code) {
1346 pj_ansi_snprintf(info_str, sizeof(info_str),
1347 "update contact for account %.*s, code[%d]",
1348 (int)acc_info.acc_uri.slen,
1349 acc_info.acc_uri.ptr,
1350 info->acc_update_contact.code);
1351 } else {
1352 pj_ansi_snprintf(info_str, sizeof(info_str),
1353 "update contact for account %.*s",
1354 (int)acc_info.acc_uri.slen,
1355 acc_info.acc_uri.ptr);
1356 }
1357 break;
1358 case PJSUA_IP_CHANGE_OP_ACC_HANGUP_CALLS:
1359 pjsua_acc_get_info(info->acc_shutdown_tp.acc_id, &acc_info);
1360 pj_ansi_snprintf(info_str, sizeof(info_str),
1361 "hangup call for account %.*s, call_id[%d]",
1362 (int)acc_info.acc_uri.slen, acc_info.acc_uri.ptr,
1363 info->acc_hangup_calls.call_id);
1364 break;
1365 case PJSUA_IP_CHANGE_OP_ACC_REINVITE_CALLS:
1366 pjsua_acc_get_info(info->acc_shutdown_tp.acc_id, &acc_info);
1367 pj_ansi_snprintf(info_str, sizeof(info_str),
1368 "reinvite call for account %.*s, call_id[%d]",
1369 (int)acc_info.acc_uri.slen, acc_info.acc_uri.ptr,
1370 info->acc_reinvite_calls.call_id);
1371 break;
1372 case PJSUA_IP_CHANGE_OP_COMPLETED:
1373 pj_ansi_snprintf(info_str, sizeof(info_str),
1374 "done");
1375 break;
1376 default:
1377 pj_ansi_snprintf(info_str, sizeof(info_str),
1378 "unknown-op");
1379 break;
1380 }
1381 PJ_LOG(3,(THIS_FILE, "IP change progress report : %s", info_str));
1382
1383 } else {
1384 PJ_PERROR(3,(THIS_FILE, status, "IP change progress fail"));
1385 }
1386 }
1387
1388 /* Auto hangup timer callback */
1389 static void hangup_timeout_callback(pj_timer_heap_t *timer_heap,
1390 struct pj_timer_entry *entry)
1391 {
1392 PJ_UNUSED_ARG(timer_heap);
1393 PJ_UNUSED_ARG(entry);
1394
1395 app_config.auto_hangup_timer.id = 0;
1396 pjsua_call_hangup_all();
1397 }
1398
1399 static void avi_writer_cb(pjsua_avi_rec_id id, void *user_data)
1400 {
1401 PJ_UNUSED_ARG(user_data);
1402
1403 PJ_LOG(4, (THIS_FILE, "AVI recorder %d: recording has completed", id));
1404 }
1405
1406 /*
1407 * A simple registrar, invoked by default_mod_on_rx_request()
1408 */
1409 static void simple_registrar(pjsip_rx_data *rdata)
1410 {
1411 pjsip_tx_data *tdata;
1412 const pjsip_expires_hdr *exp;
1413 const pjsip_hdr *h;
1414 pjsip_generic_string_hdr *srv;
1415 pj_status_t status;
1416
1417 status = pjsip_endpt_create_response(pjsua_get_pjsip_endpt(),
1418 rdata, 200, NULL, &tdata);
1419 if (status != PJ_SUCCESS)
1420 return;
1421
1422 exp = (pjsip_expires_hdr *)pjsip_msg_find_hdr(rdata->msg_info.msg,
1423 PJSIP_H_EXPIRES, NULL);
1424
1425 h = rdata->msg_info.msg->hdr.next;
1426 while (h != &rdata->msg_info.msg->hdr) {
1427 if (h->type == PJSIP_H_CONTACT) {
1428 const pjsip_contact_hdr *c = (const pjsip_contact_hdr*)h;
1429 unsigned e = c->expires;
1430
1431 if (e != PJSIP_EXPIRES_NOT_SPECIFIED) {
1432 if (exp)
1433 e = exp->ivalue;
1434 else
1435 e = 3600;
1436 }
1437
1438 if (e > 0) {
1439 pjsip_contact_hdr *nc = (pjsip_contact_hdr *)pjsip_hdr_clone(
1440 tdata->pool, h);
1441 nc->expires = e;
1442 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)nc);
1443 }
1444 }
1445 h = h->next;
1446 }
1447
1448 srv = pjsip_generic_string_hdr_create(tdata->pool, NULL, NULL);
1449 srv->name = pj_str("Server");
1450 srv->hvalue = pj_str("pjsua simple registrar");
1451 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)srv);
1452
1453 status = pjsip_endpt_send_response2(pjsua_get_pjsip_endpt(),
1454 rdata, tdata, NULL, NULL);
1455 if (status != PJ_SUCCESS) {
1456 pjsip_tx_data_dec_ref(tdata);
1457 }
1458
1459 }
1460
1461 /*****************************************************************************
1462 * A simple module to handle otherwise unhandled request. We will register
1463 * this with the lowest priority.
1464 */
1465
1466 /* Notification on incoming request */
1467 static pj_bool_t default_mod_on_rx_request(pjsip_rx_data *rdata)
1468 {
1469 pjsip_tx_data *tdata;
1470 pjsip_status_code status_code;
1471 pj_status_t status;
1472
1473 /* Don't respond to ACK! */
1474 if (pjsip_method_cmp(&rdata->msg_info.msg->line.req.method,
1475 &pjsip_ack_method) == 0)
1476 return PJ_TRUE;
1477
1478 /* Simple registrar */
1479 if (pjsip_method_cmp(&rdata->msg_info.msg->line.req.method,
1480 &pjsip_register_method) == 0)
1481 {
1482 simple_registrar(rdata);
1483 return PJ_TRUE;
1484 }
1485
1486 /* Create basic response. */
1487 if (pjsip_method_cmp(&rdata->msg_info.msg->line.req.method,
1488 &pjsip_notify_method) == 0)
1489 {
1490 /* Unsolicited NOTIFY's, send with Bad Request */
1491 status_code = PJSIP_SC_BAD_REQUEST;
1492 } else {
1493 /* Probably unknown method */
1494 status_code = PJSIP_SC_METHOD_NOT_ALLOWED;
1495 }
1496 status = pjsip_endpt_create_response(pjsua_get_pjsip_endpt(),
1497 rdata, status_code,
1498 NULL, &tdata);
1499 if (status != PJ_SUCCESS) {
1500 pjsua_perror(THIS_FILE, "Unable to create response", status);
1501 return PJ_TRUE;
1502 }
1503
1504 /* Add Allow if we're responding with 405 */
1505 if (status_code == PJSIP_SC_METHOD_NOT_ALLOWED) {
1506 const pjsip_hdr *cap_hdr;
1507 cap_hdr = pjsip_endpt_get_capability(pjsua_get_pjsip_endpt(),
1508 PJSIP_H_ALLOW, NULL);
1509 if (cap_hdr) {
1510 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr *)pjsip_hdr_clone(
1511 tdata->pool, cap_hdr));
1512 }
1513 }
1514
1515 /* Add User-Agent header */
1516 {
1517 pj_str_t user_agent;
1518 char tmp[80];
1519 const pj_str_t USER_AGENT = { "User-Agent", 10};
1520 pjsip_hdr *h;
1521
1522 pj_ansi_snprintf(tmp, sizeof(tmp), "PJSUA v%s/%s",
1523 pj_get_version(), PJ_OS_NAME);
1524 pj_strdup2_with_null(tdata->pool, &user_agent, tmp);
1525
1526 h = (pjsip_hdr*) pjsip_generic_string_hdr_create(tdata->pool,
1527 &USER_AGENT,
1528 &user_agent);
1529 pjsip_msg_add_hdr(tdata->msg, h);
1530 }
1531
1532 status = pjsip_endpt_send_response2(pjsua_get_pjsip_endpt(), rdata, tdata,
1533 NULL, NULL);
1534 if (status != PJ_SUCCESS) pjsip_tx_data_dec_ref(tdata);
1535
1536 return PJ_TRUE;
1537 }
1538
1539 /* The module instance. */
1540 static pjsip_module mod_default_handler =
1541 {
1542 NULL, NULL, /* prev, next. */
1543 { "mod-default-handler", 19 }, /* Name. */
1544 -1, /* Id */
1545 PJSIP_MOD_PRIORITY_APPLICATION+99, /* Priority */
1546 NULL, /* load() */
1547 NULL, /* start() */
1548 NULL, /* stop() */
1549 NULL, /* unload() */
1550 &default_mod_on_rx_request, /* on_rx_request() */
1551 NULL, /* on_rx_response() */
1552 NULL, /* on_tx_request. */
1553 NULL, /* on_tx_response() */
1554 NULL, /* on_tsx_state() */
1555
1556 };
1557
1558 /** CLI callback **/
1559
1560 /* Called on CLI (re)started, e.g: initial start, after iOS bg */
1561 void cli_on_started(pj_status_t status)
1562 {
1563 /* Notify app */
1564 if (app_cfg.on_started) {
1565 if (status == PJ_SUCCESS) {
1566 char info[128];
1567 cli_get_info(info, sizeof(info));
1568 if (app_cfg.on_started) {
1569 (*app_cfg.on_started)(status, info);
1570 }
1571 } else {
1572 if (app_cfg.on_started) {
1573 (*app_cfg.on_started)(status, NULL);
1574 }
1575 }
1576 }
1577 }
1578
1579 /* Called on CLI quit */
1580 void cli_on_stopped(pj_bool_t restart, int argc, char* argv[])
1581 {
1582 /* Notify app */
1583 if (app_cfg.on_stopped)
1584 (*app_cfg.on_stopped)(restart, argc, argv);
1585 }
1586
1587
1588 /* Called on pjsua legacy quit */
1589 void legacy_on_stopped(pj_bool_t restart)
1590 {
1591 /* Notify app */
1592 if (app_cfg.on_stopped)
1593 (*app_cfg.on_stopped)(restart, 1, NULL);
1594 }
1595
1596
1597 static void app_cleanup(pjsip_endpoint *endpt)
1598 {
1599 PJ_UNUSED_ARG(endpt);
1600 pj_pool_safe_release(&app_config.pool);
1601 }
1602
1603
1604 /*****************************************************************************
1605 * Public API
1606 */
1607
1608 int stdout_refresh_proc(void *arg)
1609 {
1610 extern char *stdout_refresh_text;
1611
1612 PJ_UNUSED_ARG(arg);
1613
1614 /* Set thread to lowest priority so that it doesn't clobber
1615 * stdout output
1616 */
1617 pj_thread_set_prio(pj_thread_this(),
1618 pj_thread_get_prio_min(pj_thread_this()));
1619
1620 while (!stdout_refresh_quit) {
1621 pj_thread_sleep(stdout_refresh * 1000);
1622 puts(stdout_refresh_text);
1623 fflush(stdout);
1624 }
1625
1626 return 0;
1627 }
1628
1629
1630 static pj_status_t app_init(void)
1631 {
1632 pjsua_transport_id transport_id = -1;
1633 pjsua_transport_config tcp_cfg;
1634 unsigned i;
1635 pj_pool_t *tmp_pool;
1636 pj_status_t status;
1637
1638 /** Create pjsua **/
1639 status = pjsua_create();
1640 if (status != PJ_SUCCESS)
1641 return status;
1642
1643 /* Create pool for application */
1644 app_config.pool = pjsua_pool_create("pjsua-app", 1000, 1000);
1645 tmp_pool = pjsua_pool_create("tmp-pjsua", 1000, 1000);
1646
1647 /* Queue pool release at PJLIB exit */
1648 pjsip_endpt_atexit(pjsua_get_pjsip_endpt(), &app_cleanup);
1649
1650 /* Init CLI & its FE settings */
1651 if (!app_running) {
1652 pj_cli_cfg_default(&app_config.cli_cfg.cfg);
1653 pj_cli_telnet_cfg_default(&app_config.cli_cfg.telnet_cfg);
1654 pj_cli_console_cfg_default(&app_config.cli_cfg.console_cfg);
1655 app_config.cli_cfg.telnet_cfg.on_started = cli_on_started;
1656 }
1657
1658 /** Parse args **/
1659 status = load_config(app_cfg.argc, app_cfg.argv, &uri_arg);
1660 if (status != PJ_SUCCESS) {
1661 pj_pool_release(tmp_pool);
1662 return status;
1663 }
1664
1665 /* Initialize application callbacks */
1666 app_config.cfg.cb.on_call_state = &on_call_state;
1667 app_config.cfg.cb.on_stream_destroyed = &on_stream_destroyed;
1668 app_config.cfg.cb.on_call_media_state = &on_call_media_state;
1669 app_config.cfg.cb.on_incoming_call = &on_incoming_call;
1670 app_config.cfg.cb.on_dtmf_digit2 = &call_on_dtmf_callback2;
1671 app_config.cfg.cb.on_call_rx_text = &call_on_rx_text;
1672 app_config.cfg.cb.on_call_redirected = &call_on_redirected;
1673 app_config.cfg.cb.on_reg_state = &on_reg_state;
1674 app_config.cfg.cb.on_incoming_subscribe = &on_incoming_subscribe;
1675 app_config.cfg.cb.on_buddy_state = &on_buddy_state;
1676 app_config.cfg.cb.on_buddy_dlg_event_state = &on_buddy_dlg_event_state;
1677 app_config.cfg.cb.on_buddy_evsub_state = &on_buddy_evsub_state;
1678 app_config.cfg.cb.on_buddy_evsub_dlg_event_state =
1679 &on_buddy_evsub_dlg_event_state;
1680 app_config.cfg.cb.on_pager = &on_pager;
1681 app_config.cfg.cb.on_typing = &on_typing;
1682 app_config.cfg.cb.on_call_transfer_status = &on_call_transfer_status;
1683 app_config.cfg.cb.on_call_replaced = &on_call_replaced;
1684 app_config.cfg.cb.on_nat_detect = &on_nat_detect;
1685 app_config.cfg.cb.on_mwi_info = &on_mwi_info;
1686 app_config.cfg.cb.on_transport_state = &on_transport_state;
1687 app_config.cfg.cb.on_ice_transport_error = &on_ice_transport_error;
1688 app_config.cfg.cb.on_snd_dev_operation = &on_snd_dev_operation;
1689 app_config.cfg.cb.on_call_media_event = &on_call_media_event;
1690 app_config.cfg.cb.on_ip_change_progress = &on_ip_change_progress;
1691 #if !PJSUA_MEDIA_HAS_PJMEDIA
1692 app_config.cfg.cb.on_call_sdp_created = &on_call_sdp_created_cb;
1693 #endif
1694 #ifdef TRANSPORT_ADAPTER_SAMPLE
1695 app_config.cfg.cb.on_create_media_transport = &on_create_media_transport;
1696 #endif
1697
1698 /* Set sound device latency */
1699 if (app_config.capture_lat > 0)
1700 app_config.media_cfg.snd_rec_latency = app_config.capture_lat;
1701 if (app_config.playback_lat)
1702 app_config.media_cfg.snd_play_latency = app_config.playback_lat;
1703
1704 if (app_cfg.on_config_init)
1705 (*app_cfg.on_config_init)(&app_config);
1706
1707 /* Initialize pjsua */
1708 status = pjsua_init(&app_config.cfg, &app_config.log_cfg,
1709 &app_config.media_cfg);
1710 if (status != PJ_SUCCESS) {
1711 pj_pool_release(tmp_pool);
1712 return status;
1713 }
1714
1715 /* Initialize our module to handle otherwise unhandled request */
1716 status = pjsip_endpt_register_module(pjsua_get_pjsip_endpt(),
1717 &mod_default_handler);
1718 if (status != PJ_SUCCESS)
1719 return status;
1720
1721 #ifdef STEREO_DEMO
1722 stereo_demo();
1723 #endif
1724
1725 /* Initialize calls data */
1726 for (i=0; i<PJ_ARRAY_SIZE(app_config.call_data); ++i) {
1727 app_config.call_data[i].timer.id = PJSUA_INVALID_ID;
1728 app_config.call_data[i].timer.cb = &call_timeout_callback;
1729 }
1730
1731 /* Optionally registers WAV file */
1732 for (i=0; i<app_config.wav_count; ++i) {
1733 pjsua_player_id wav_id;
1734 unsigned play_options = 0;
1735
1736 if (app_config.auto_play_hangup)
1737 play_options |= PJMEDIA_FILE_NO_LOOP;
1738
1739 status = pjsua_player_create(&app_config.wav_files[i], play_options,
1740 &wav_id);
1741 if (status != PJ_SUCCESS)
1742 goto on_error;
1743
1744 if (app_config.wav_id == PJSUA_INVALID_ID) {
1745 app_config.wav_id = wav_id;
1746 app_config.wav_port = pjsua_player_get_conf_port(app_config.wav_id);
1747 if (app_config.auto_play_hangup) {
1748 pjmedia_port *port;
1749
1750 pjsua_player_get_port(app_config.wav_id, &port);
1751 status = pjmedia_wav_player_set_eof_cb2(port, NULL,
1752 &on_playfile_done);
1753 if (status != PJ_SUCCESS)
1754 goto on_error;
1755
1756 pj_timer_entry_init(&app_config.auto_hangup_timer, 0, NULL,
1757 &hangup_timeout_callback);
1758 }
1759 }
1760 }
1761
1762 /* Optionally registers tone players */
1763 for (i=0; i<app_config.tone_count; ++i) {
1764 pjmedia_port *tport;
1765 char name[80];
1766 pj_str_t label;
1767 pj_status_t status2;
1768
1769 pj_ansi_snprintf(name, sizeof(name), "tone-%d,%d",
1770 app_config.tones[i].freq1,
1771 app_config.tones[i].freq2);
1772 label = pj_str(name);
1773 status2 = pjmedia_tonegen_create2(app_config.pool, &label,
1774 8000, 1, 160, 16,
1775 PJMEDIA_TONEGEN_LOOP, &tport);
1776 if (status2 != PJ_SUCCESS) {
1777 pjsua_perror(THIS_FILE, "Unable to create tone generator", status);
1778 goto on_error;
1779 }
1780
1781 status2 = pjsua_conf_add_port(app_config.pool, tport,
1782 &app_config.tone_slots[i]);
1783 pj_assert(status2 == PJ_SUCCESS);
1784
1785 status2 = pjmedia_tonegen_play(tport, 1, &app_config.tones[i], 0);
1786 pj_assert(status2 == PJ_SUCCESS);
1787 }
1788
1789 /* Optionally create recorder file, if any. */
1790 if (app_config.rec_file.slen) {
1791 status = pjsua_recorder_create(&app_config.rec_file, 0, NULL, 0, 0,
1792 &app_config.rec_id);
1793 if (status != PJ_SUCCESS)
1794 goto on_error;
1795
1796 app_config.rec_port = pjsua_recorder_get_conf_port(app_config.rec_id);
1797 }
1798
1799 pj_memcpy(&tcp_cfg, &app_config.udp_cfg, sizeof(tcp_cfg));
1800
1801 /* Create ringback tones */
1802 if (app_config.no_tones == PJ_FALSE) {
1803 unsigned samples_per_frame;
1804 pjmedia_tone_desc tone[RING_CNT+RINGBACK_CNT];
1805 pj_str_t name;
1806
1807 samples_per_frame = app_config.media_cfg.audio_frame_ptime *
1808 app_config.media_cfg.clock_rate *
1809 app_config.media_cfg.channel_count / 1000;
1810
1811 /* Ringback tone (call is ringing) */
1812 name = pj_str("ringback");
1813 status = pjmedia_tonegen_create2(app_config.pool, &name,
1814 app_config.media_cfg.clock_rate,
1815 app_config.media_cfg.channel_count,
1816 samples_per_frame,
1817 16, PJMEDIA_TONEGEN_LOOP,
1818 &app_config.ringback_port);
1819 if (status != PJ_SUCCESS)
1820 goto on_error;
1821
1822 pj_bzero(&tone, sizeof(tone));
1823 for (i=0; i<RINGBACK_CNT; ++i) {
1824 tone[i].freq1 = RINGBACK_FREQ1;
1825 tone[i].freq2 = RINGBACK_FREQ2;
1826 tone[i].on_msec = RINGBACK_ON;
1827 tone[i].off_msec = RINGBACK_OFF;
1828 }
1829 tone[RINGBACK_CNT-1].off_msec = RINGBACK_INTERVAL;
1830
1831 pjmedia_tonegen_play(app_config.ringback_port, RINGBACK_CNT, tone,
1832 PJMEDIA_TONEGEN_LOOP);
1833
1834
1835 status = pjsua_conf_add_port(app_config.pool, app_config.ringback_port,
1836 &app_config.ringback_slot);
1837 if (status != PJ_SUCCESS)
1838 goto on_error;
1839
1840 /* Ring (to alert incoming call) */
1841 name = pj_str("ring");
1842 status = pjmedia_tonegen_create2(app_config.pool, &name,
1843 app_config.media_cfg.clock_rate,
1844 app_config.media_cfg.channel_count,
1845 samples_per_frame,
1846 16, PJMEDIA_TONEGEN_LOOP,
1847 &app_config.ring_port);
1848 if (status != PJ_SUCCESS)
1849 goto on_error;
1850
1851 for (i=0; i<RING_CNT; ++i) {
1852 tone[i].freq1 = RING_FREQ1;
1853 tone[i].freq2 = RING_FREQ2;
1854 tone[i].on_msec = RING_ON;
1855 tone[i].off_msec = RING_OFF;
1856 }
1857 tone[RING_CNT-1].off_msec = RING_INTERVAL;
1858
1859 pjmedia_tonegen_play(app_config.ring_port, RING_CNT,
1860 tone, PJMEDIA_TONEGEN_LOOP);
1861
1862 status = pjsua_conf_add_port(app_config.pool, app_config.ring_port,
1863 &app_config.ring_slot);
1864 if (status != PJ_SUCCESS)
1865 goto on_error;
1866
1867 }
1868
1869 if (app_config.avi_rec.slen) {
1870 #if PJMEDIA_HAS_VIDEO
1871 status = pjsua_avi_recorder_create(&app_config.avi_rec,
1872 app_config.avi_rec_size,
1873 NULL, NULL, 0,
1874 &app_config.avi_rec_id);
1875 if (status != PJ_SUCCESS) {
1876 PJ_PERROR(1, (THIS_FILE, status, "Error creating AVI recorder"));
1877 goto on_error;
1878 }
1879
1880 app_config.avi_vid_slot = pjsua_avi_recorder_get_conf_port(
1881 app_config.avi_rec_id,
1882 PJMEDIA_TYPE_VIDEO);
1883 app_config.avi_aud_slot = pjsua_avi_recorder_get_conf_port(
1884 app_config.avi_rec_id,
1885 PJMEDIA_TYPE_AUDIO);
1886
1887 pjsua_avi_recorder_set_cb(app_config.avi_rec_id, NULL,
1888 &avi_writer_cb);
1889 #endif
1890 }
1891
1892 /* Create AVI player virtual devices */
1893 if (app_config.avi_cnt) {
1894 #if PJMEDIA_HAS_VIDEO && PJMEDIA_VIDEO_DEV_HAS_AVI
1895
1896 for (i=0; i<app_config.avi_cnt; ++i) {
1897 pjsua_avi_player_id avi_id;
1898 status = pjsua_avi_player_create(&app_config.avi[i].path, &avi_id);
1899
1900 if (status == PJ_SUCCESS) {
1901 app_config.avi[i].p_id = avi_id;
1902 app_config.avi[i].slot = pjsua_avi_player_get_conf_port(
1903 avi_id, PJMEDIA_TYPE_AUDIO, 0);
1904 app_config.avi[i].dev_id =
1905 pjsua_avi_player_get_vid_dev(avi_id);
1906
1907 if (app_config.avi_def_idx == PJSUA_INVALID_ID) {
1908 app_config.avi_def_idx = i;
1909 }
1910 }
1911 }
1912 #else
1913 for (i=0; i<app_config.avi_cnt; ++i) {
1914 app_config.avi[i].dev_id = PJMEDIA_VID_INVALID_DEV;
1915 app_config.avi[i].slot = PJSUA_INVALID_ID;
1916 }
1917
1918 PJ_LOG(2,(THIS_FILE,
1919 "Warning: --play-avi is ignored because AVI is disabled"));
1920 #endif /* PJMEDIA_VIDEO_DEV_HAS_AVI */
1921 }
1922
1923 /* Add UDP transport unless it's disabled. */
1924 if (!app_config.no_udp) {
1925 pjsua_acc_id aid;
1926 pjsip_transport_type_e type = PJSIP_TRANSPORT_UDP;
1927
1928 status = pjsua_transport_create(type,
1929 &app_config.udp_cfg,
1930 &transport_id);
1931 if (status != PJ_SUCCESS)
1932 goto on_error;
1933
1934 /* Add local account */
1935 pjsua_acc_add_local(transport_id, PJ_TRUE, &aid);
1936
1937 /* Adjust local account config based on pjsua app config */
1938 {
1939 pjsua_acc_config acc_cfg;
1940 pjsua_acc_get_config(aid, tmp_pool, &acc_cfg);
1941
1942 app_config_init_video(&acc_cfg);
1943 acc_cfg.txt_red_level = app_config.txt_red_level;
1944 acc_cfg.rtp_cfg = app_config.rtp_cfg;
1945 pjsua_acc_modify(aid, &acc_cfg);
1946 }
1947
1948 //pjsua_acc_set_transport(aid, transport_id);
1949 pjsua_acc_set_online_status(current_acc, PJ_TRUE);
1950
1951 if (app_config.udp_cfg.port == 0) {
1952 pjsua_transport_info ti;
1953 pj_sockaddr_in *a;
1954
1955 pjsua_transport_get_info(transport_id, &ti);
1956 a = (pj_sockaddr_in*)&ti.local_addr;
1957
1958 tcp_cfg.port = pj_ntohs(a->sin_port);
1959 }
1960 }
1961
1962 /* Add UDP IPv6 transport unless it's disabled. */
1963 if (!app_config.no_udp && app_config.ipv6) {
1964 pjsua_acc_id aid;
1965 pjsip_transport_type_e type = PJSIP_TRANSPORT_UDP6;
1966 pjsua_transport_config udp_cfg;
1967
1968 udp_cfg = app_config.udp_cfg;
1969 if (udp_cfg.port == 0)
1970 udp_cfg.port = 5060;
1971 else
1972 udp_cfg.port += 10;
1973 status = pjsua_transport_create(type,
1974 &udp_cfg,
1975 &transport_id);
1976 if (status != PJ_SUCCESS)
1977 goto on_error;
1978
1979 /* Add local account */
1980 pjsua_acc_add_local(transport_id, PJ_TRUE, &aid);
1981
1982 /* Adjust local account config based on pjsua app config */
1983 {
1984 pjsua_acc_config acc_cfg;
1985 pjsua_acc_get_config(aid, tmp_pool, &acc_cfg);
1986
1987 app_config_init_video(&acc_cfg);
1988 acc_cfg.txt_red_level = app_config.txt_red_level;
1989 acc_cfg.rtp_cfg = app_config.rtp_cfg;
1990 // acc_cfg.ipv6_media_use = PJSUA_IPV6_ENABLED;
1991 pjsua_acc_modify(aid, &acc_cfg);
1992 }
1993
1994 //pjsua_acc_set_transport(aid, transport_id);
1995 pjsua_acc_set_online_status(current_acc, PJ_TRUE);
1996
1997 if (app_config.udp_cfg.port == 0) {
1998 pjsua_transport_info ti;
1999
2000 pjsua_transport_get_info(transport_id, &ti);
2001 tcp_cfg.port = pj_sockaddr_get_port(&ti.local_addr);
2002 }
2003 }
2004
2005 /* Add TCP transport unless it's disabled */
2006 if (!app_config.no_tcp) {
2007 pjsua_acc_id aid;
2008
2009 status = pjsua_transport_create(PJSIP_TRANSPORT_TCP,
2010 &tcp_cfg,
2011 &transport_id);
2012 if (status != PJ_SUCCESS)
2013 goto on_error;
2014
2015 /* Add local account */
2016 pjsua_acc_add_local(transport_id, PJ_TRUE, &aid);
2017
2018 /* Adjust local account config based on pjsua app config */
2019 {
2020 pjsua_acc_config acc_cfg;
2021 pjsua_acc_get_config(aid, tmp_pool, &acc_cfg);
2022
2023 app_config_init_video(&acc_cfg);
2024 acc_cfg.txt_red_level = app_config.txt_red_level;
2025 acc_cfg.rtp_cfg = app_config.rtp_cfg;
2026 pjsua_acc_modify(aid, &acc_cfg);
2027 }
2028
2029 pjsua_acc_set_online_status(current_acc, PJ_TRUE);
2030
2031 }
2032
2033 /* Add TCP IPv6 transport unless it's disabled. */
2034 if (!app_config.no_tcp && app_config.ipv6) {
2035 pjsua_acc_id aid;
2036 pjsip_transport_type_e type = PJSIP_TRANSPORT_TCP6;
2037
2038 tcp_cfg.port += 10;
2039
2040 status = pjsua_transport_create(type,
2041 &tcp_cfg,
2042 &transport_id);
2043 if (status != PJ_SUCCESS)
2044 goto on_error;
2045
2046 /* Add local account */
2047 pjsua_acc_add_local(transport_id, PJ_TRUE, &aid);
2048
2049 /* Adjust local account config based on pjsua app config */
2050 {
2051 pjsua_acc_config acc_cfg;
2052 pjsua_acc_get_config(aid, tmp_pool, &acc_cfg);
2053
2054 app_config_init_video(&acc_cfg);
2055 acc_cfg.txt_red_level = app_config.txt_red_level;
2056 acc_cfg.rtp_cfg = app_config.rtp_cfg;
2057 // acc_cfg.ipv6_media_use = PJSUA_IPV6_ENABLED;
2058 pjsua_acc_modify(aid, &acc_cfg);
2059 }
2060
2061 //pjsua_acc_set_transport(aid, transport_id);
2062 pjsua_acc_set_online_status(current_acc, PJ_TRUE);
2063 }
2064
2065
2066 #if defined(PJSIP_HAS_TLS_TRANSPORT) && PJSIP_HAS_TLS_TRANSPORT!=0
2067 /* Add TLS transport when application wants one */
2068 if (app_config.use_tls) {
2069
2070 pjsua_acc_id acc_id;
2071
2072 /* Copy the QoS settings */
2073 tcp_cfg.tls_setting.qos_type = tcp_cfg.qos_type;
2074 pj_memcpy(&tcp_cfg.tls_setting.qos_params, &tcp_cfg.qos_params,
2075 sizeof(tcp_cfg.qos_params));
2076
2077 /* Set TLS port as TCP port+1 */
2078 tcp_cfg.port++;
2079 status = pjsua_transport_create(PJSIP_TRANSPORT_TLS,
2080 &tcp_cfg,
2081 &transport_id);
2082 tcp_cfg.port--;
2083 if (status != PJ_SUCCESS)
2084 goto on_error;
2085
2086 /* Add local account */
2087 pjsua_acc_add_local(transport_id, PJ_FALSE, &acc_id);
2088
2089 /* Adjust local account config based on pjsua app config */
2090 {
2091 pjsua_acc_config acc_cfg;
2092 pjsua_acc_get_config(acc_id, tmp_pool, &acc_cfg);
2093
2094 app_config_init_video(&acc_cfg);
2095 acc_cfg.txt_red_level = app_config.txt_red_level;
2096 acc_cfg.rtp_cfg = app_config.rtp_cfg;
2097 pjsua_acc_modify(acc_id, &acc_cfg);
2098 }
2099
2100 pjsua_acc_set_online_status(acc_id, PJ_TRUE);
2101 }
2102
2103 /* Add TLS IPv6 transport unless it's disabled. */
2104 if (app_config.use_tls && app_config.ipv6) {
2105 pjsua_acc_id aid;
2106 pjsip_transport_type_e type = PJSIP_TRANSPORT_TLS6;
2107
2108 tcp_cfg.port += 10;
2109
2110 status = pjsua_transport_create(type,
2111 &tcp_cfg,
2112 &transport_id);
2113 if (status != PJ_SUCCESS)
2114 goto on_error;
2115
2116 /* Add local account */
2117 pjsua_acc_add_local(transport_id, PJ_TRUE, &aid);
2118
2119 /* Adjust local account config based on pjsua app config */
2120 {
2121 pjsua_acc_config acc_cfg;
2122 pjsua_acc_get_config(aid, tmp_pool, &acc_cfg);
2123
2124 app_config_init_video(&acc_cfg);
2125 acc_cfg.txt_red_level = app_config.txt_red_level;
2126 acc_cfg.rtp_cfg = app_config.rtp_cfg;
2127 // acc_cfg.ipv6_media_use = PJSUA_IPV6_ENABLED;
2128 pjsua_acc_modify(aid, &acc_cfg);
2129 }
2130
2131 //pjsua_acc_set_transport(aid, transport_id);
2132 pjsua_acc_set_online_status(current_acc, PJ_TRUE);
2133 }
2134
2135 #endif
2136
2137 if (transport_id == -1) {
2138 PJ_LOG(1,(THIS_FILE, "Error: no transport is configured"));
2139 status = -1;
2140 goto on_error;
2141 }
2142
2143
2144 /* Add accounts */
2145 for (i=0; i<app_config.acc_cnt; ++i) {
2146 app_config.acc_cfg[i].rtp_cfg = app_config.rtp_cfg;
2147 app_config.acc_cfg[i].reg_retry_interval = 300;
2148 app_config.acc_cfg[i].reg_first_retry_interval = 60;
2149
2150 app_config_init_video(&app_config.acc_cfg[i]);
2151 app_config.acc_cfg[i].txt_red_level = app_config.txt_red_level;
2152
2153 status = pjsua_acc_add(&app_config.acc_cfg[i], PJ_TRUE, NULL);
2154 if (status != PJ_SUCCESS)
2155 goto on_error;
2156 pjsua_acc_set_online_status(current_acc, PJ_TRUE);
2157 }
2158
2159 /* Add buddies */
2160 for (i=0; i<app_config.buddy_cnt; ++i) {
2161 status = pjsua_buddy_add(&app_config.buddy_cfg[i], NULL);
2162 if (status != PJ_SUCCESS) {
2163 PJ_PERROR(1,(THIS_FILE, status, "Error adding buddy"));
2164 goto on_error;
2165 }
2166 }
2167
2168 /* Optionally disable some codec */
2169 for (i=0; i<app_config.codec_dis_cnt; ++i) {
2170 pjsua_codec_set_priority(&app_config.codec_dis[i],
2171 PJMEDIA_CODEC_PRIO_DISABLED);
2172 #if PJSUA_HAS_VIDEO
2173 pjsua_vid_codec_set_priority(&app_config.codec_dis[i],
2174 PJMEDIA_CODEC_PRIO_DISABLED);
2175 #endif
2176 }
2177
2178 /* Optionally set codec orders */
2179 for (i=0; i<app_config.codec_cnt; ++i) {
2180 pjsua_codec_set_priority(&app_config.codec_arg[i],
2181 (pj_uint8_t)(PJMEDIA_CODEC_PRIO_NORMAL+i+9));
2182 #if PJSUA_HAS_VIDEO
2183 pjsua_vid_codec_set_priority(&app_config.codec_arg[i],
2184 (pj_uint8_t)(PJMEDIA_CODEC_PRIO_NORMAL+i+9));
2185 #endif
2186 }
2187
2188 /* Use null sound device? */
2189 #ifndef STEREO_DEMO
2190 if (app_config.null_audio) {
2191 status = pjsua_set_null_snd_dev();
2192 if (status != PJ_SUCCESS)
2193 goto on_error;
2194 }
2195 #endif
2196
2197 if (app_config.capture_dev != PJSUA_INVALID_ID ||
2198 app_config.playback_dev != PJSUA_INVALID_ID)
2199 {
2200 status = pjsua_set_snd_dev(app_config.capture_dev,
2201 app_config.playback_dev);
2202 if (status != PJ_SUCCESS)
2203 goto on_error;
2204 }
2205
2206 /* Init call setting */
2207 pjsua_call_setting_default(&call_opt);
2208 call_opt.aud_cnt = app_config.aud_cnt;
2209 call_opt.vid_cnt = app_config.vid.vid_cnt;
2210 call_opt.txt_cnt = app_config.txt_cnt;
2211 if (app_config.enable_loam) {
2212 call_opt.flag |= PJSUA_CALL_NO_SDP_OFFER;
2213 }
2214
2215 #if defined(PJSIP_HAS_TLS_TRANSPORT) && PJSIP_HAS_TLS_TRANSPORT!=0
2216 /* Wipe out TLS key settings in transport configs */
2217 pjsip_tls_setting_wipe_keys(&app_config.udp_cfg.tls_setting);
2218 #endif
2219
2220 pj_pool_release(tmp_pool);
2221 return PJ_SUCCESS;
2222
2223 on_error:
2224
2225 #if defined(PJSIP_HAS_TLS_TRANSPORT) && PJSIP_HAS_TLS_TRANSPORT!=0
2226 /* Wipe out TLS key settings in transport configs */
2227 pjsip_tls_setting_wipe_keys(&app_config.udp_cfg.tls_setting);
2228 #endif
2229
2230 pj_pool_release(tmp_pool);
2231 app_destroy();
2232 return status;
2233 }
2234
2235 pj_status_t pjsua_app_init(const pjsua_app_cfg_t *cfg)
2236 {
2237 pj_status_t status;
2238 pj_memcpy(&app_cfg, cfg, sizeof(app_cfg));
2239
2240 status = app_init();
2241 if (status != PJ_SUCCESS)
2242 return status;
2243
2244 /* Init CLI if configured */
2245 if (app_config.use_cli) {
2246 status = cli_init();
2247 }
2248 return status;
2249 }
2250
2251 pj_status_t pjsua_app_run(pj_bool_t wait_telnet_cli)
2252 {
2253 pj_thread_t *stdout_refresh_thread = NULL;
2254 pj_status_t status;
2255
2256 /* Start console refresh thread */
2257 if (stdout_refresh > 0) {
2258 status = pj_thread_create(app_config.pool, "stdout",
2259 &stdout_refresh_proc,
2260 NULL, 0, 0, &stdout_refresh_thread);
2261 PJ_ASSERT_RETURN(status==PJ_SUCCESS, status);
2262 }
2263
2264 status = pjsua_start();
2265 if (status != PJ_SUCCESS)
2266 goto on_return;
2267
2268 if (app_config.use_cli && (app_config.cli_cfg.cli_fe & CLI_FE_TELNET)) {
2269 char info[128];
2270 cli_get_info(info, sizeof(info));
2271 if (app_cfg.on_started) {
2272 (*app_cfg.on_started)(status, info);
2273 }
2274 } else {
2275 if (app_cfg.on_started) {
2276 (*app_cfg.on_started)(status, "Ready");
2277 }
2278 }
2279
2280 /* If user specifies URI to call, then call the URI */
2281 if (uri_arg.slen) {
2282 pjsua_call_setting_default(&call_opt);
2283 call_opt.aud_cnt = app_config.aud_cnt;
2284 call_opt.vid_cnt = app_config.vid.vid_cnt;
2285 call_opt.txt_cnt = app_config.txt_cnt;
2286
2287 pjsua_call_make_call(current_acc, &uri_arg, &call_opt, NULL,
2288 NULL, NULL);
2289 }
2290
2291 app_running = PJ_TRUE;
2292
2293 if (app_config.use_cli)
2294 cli_main(wait_telnet_cli);
2295 else
2296 legacy_main();
2297
2298 status = PJ_SUCCESS;
2299
2300 on_return:
2301 if (stdout_refresh_thread) {
2302 stdout_refresh_quit = PJ_TRUE;
2303 pj_thread_join(stdout_refresh_thread);
2304 pj_thread_destroy(stdout_refresh_thread);
2305 stdout_refresh_quit = PJ_FALSE;
2306 }
2307 return status;
2308 }
2309
2310 static pj_status_t app_destroy(void)
2311 {
2312 pj_status_t status = PJ_SUCCESS;
2313 unsigned i;
2314 pj_bool_t use_cli = PJ_FALSE;
2315 int cli_fe = 0;
2316 pj_uint16_t cli_telnet_port = 0;
2317
2318 #ifdef STEREO_DEMO
2319 if (app_config.snd) {
2320 pjmedia_snd_port_destroy(app_config.snd);
2321 app_config.snd = NULL;
2322 }
2323 if (app_config.sc_ch1) {
2324 pjsua_conf_remove_port(app_config.sc_ch1_slot);
2325 app_config.sc_ch1_slot = PJSUA_INVALID_ID;
2326 pjmedia_port_destroy(app_config.sc_ch1);
2327 app_config.sc_ch1 = NULL;
2328 }
2329 if (app_config.sc) {
2330 pjmedia_port_destroy(app_config.sc);
2331 app_config.sc = NULL;
2332 }
2333 #endif
2334
2335 #if PJMEDIA_HAS_VIDEO && PJMEDIA_VIDEO_DEV_HAS_AVI
2336 /* Close avi player */
2337 for (i = 0; i < app_config.avi_cnt; ++i) {
2338 if (app_config.avi[i].p_id != PJSUA_INVALID_ID) {
2339 if (pjsua_avi_player_destroy(app_config.avi[i].p_id) == PJ_SUCCESS)
2340 app_config.avi[i].p_id = PJSUA_INVALID_ID;
2341 }
2342 }
2343 #endif
2344
2345 /* Close avi writer */
2346 #if PJMEDIA_HAS_VIDEO
2347 if (app_config.avi_rec_id != PJSUA_INVALID_ID) {
2348 pjsua_avi_recorder_destroy(app_config.avi_rec_id);
2349 app_config.avi_rec_id = PJSUA_INVALID_ID;
2350 }
2351 #endif
2352
2353 /* Close ringback port */
2354 if (app_config.ringback_port &&
2355 app_config.ringback_slot != PJSUA_INVALID_ID)
2356 {
2357 pjsua_conf_remove_port(app_config.ringback_slot);
2358 app_config.ringback_slot = PJSUA_INVALID_ID;
2359 pjmedia_port_destroy(app_config.ringback_port);
2360 app_config.ringback_port = NULL;
2361 }
2362
2363 /* Close ring port */
2364 if (app_config.ring_port && app_config.ring_slot != PJSUA_INVALID_ID) {
2365 pjsua_conf_remove_port(app_config.ring_slot);
2366 app_config.ring_slot = PJSUA_INVALID_ID;
2367 pjmedia_port_destroy(app_config.ring_port);
2368 app_config.ring_port = NULL;
2369 }
2370
2371 /* Close wav player */
2372 if (app_config.wav_id != PJSUA_INVALID_ID) {
2373 pjsua_player_destroy(app_config.wav_id);
2374 app_config.wav_id = PJSUA_INVALID_ID;
2375 app_config.wav_port = PJSUA_INVALID_ID;
2376 }
2377
2378 /* Close wav recorder */
2379 if (app_config.rec_id != PJSUA_INVALID_ID) {
2380 pjsua_recorder_destroy(app_config.rec_id);
2381 app_config.rec_id = PJSUA_INVALID_ID;
2382 app_config.rec_port = PJSUA_INVALID_ID;
2383 }
2384
2385 /* Close tone generators */
2386 for (i=0; i<app_config.tone_count; ++i) {
2387 if (app_config.tone_slots[i] != PJSUA_INVALID_ID) {
2388 pjsua_conf_remove_port(app_config.tone_slots[i]);
2389 app_config.tone_slots[i] = PJSUA_INVALID_ID;
2390 }
2391 }
2392
2393 #if defined(PJSIP_HAS_TLS_TRANSPORT) && PJSIP_HAS_TLS_TRANSPORT!=0
2394 /* Wipe out TLS key settings in transport configs */
2395 pjsip_tls_setting_wipe_keys(&app_config.udp_cfg.tls_setting);
2396 #endif
2397
2398 /* The pool release has been scheduled via pjsip_endpt_atexit().
2399 *
2400 * We can only release the pool after audio & video conference destroy.
2401 * Note that pjsua_conf_remove_port()/pjsua_vid_conf_remove_port()
2402 * is asynchronous, so when sound device is not active, PJMEDIA ports
2403 * have not been removed from the conference (and destroyed) yet
2404 * until the audio & video conferences are destroyed (in pjsua_destroy()).
2405 */
2406 //pj_pool_safe_release(&app_config.pool);
2407
2408 status = pjsua_destroy();
2409
2410 if (app_config.use_cli) {
2411 use_cli = app_config.use_cli;
2412 cli_fe = app_config.cli_cfg.cli_fe;
2413 cli_telnet_port = app_config.cli_cfg.telnet_cfg.port;
2414 }
2415
2416 /* Reset config */
2417 pj_bzero(&app_config, sizeof(app_config));
2418 app_config.wav_id = PJSUA_INVALID_ID;
2419 app_config.rec_id = PJSUA_INVALID_ID;
2420 app_config.avi_rec_id = PJSUA_INVALID_ID;
2421 app_config.avi_vid_slot = PJSUA_INVALID_ID;
2422 app_config.avi_aud_slot = PJSUA_INVALID_ID;
2423
2424 if (use_cli) {
2425 app_config.use_cli = use_cli;
2426 app_config.cli_cfg.cli_fe = cli_fe;
2427 app_config.cli_cfg.telnet_cfg.port = cli_telnet_port;
2428 }
2429
2430 return status;
2431 }
2432
2433 pj_status_t pjsua_app_destroy(void)
2434 {
2435 pj_status_t status;
2436
2437 status = app_destroy();
2438
2439 if (app_config.use_cli) {
2440 cli_destroy();
2441 }
2442
2443 return status;
2444 }
2445
2446 /** ======================= **/
2447
2448 #ifdef STEREO_DEMO
2449 /*
2450 * In this stereo demo, we open the sound device in stereo mode and
2451 * arrange the attachment to the PJSUA-LIB conference bridge as such
2452 * so that channel0/left channel of the sound device corresponds to
2453 * slot 0 in the bridge, and channel1/right channel of the sound
2454 * device corresponds to slot 1 in the bridge. Then user can independently
2455 * feed different media to/from the speakers/microphones channels, by
2456 * connecting them to slot 0 or 1 respectively.
2457 *
2458 * Here's how the connection looks like:
2459 *
2460 +-----------+ stereo +-----------------+ 2x mono +-----------+
2461 | AUDIO DEV |<------>| SPLITCOMB left|<------->|#0 BRIDGE |
2462 +-----------+ | right|<------->|#1 |
2463 +-----------------+ +-----------+
2464 */
2465 static void stereo_demo()
2466 {
2467 pjmedia_port *conf;
2468 pj_status_t status;
2469
2470 /* Disable existing sound device */
2471 conf = pjsua_set_no_snd_dev();
2472
2473 /* Create stereo-mono splitter/combiner */
2474 status = pjmedia_splitcomb_create(app_config.pool,
2475 PJMEDIA_PIA_SRATE(&conf->info) /* clock rate */,
2476 2 /* stereo */,
2477 2 * PJMEDIA_PIA_SPF(&conf->info),
2478 PJMEDIA_PIA_BITS(&conf->info),
2479 0 /* options */,
2480 &app_config.sc);
2481 pj_assert(status == PJ_SUCCESS);
2482
2483 /* Connect channel0 (left channel?) to conference port slot0 */
2484 status = pjmedia_splitcomb_set_channel(app_config.sc, 0 /* ch0 */,
2485 0 /*options*/,
2486 conf);
2487 pj_assert(status == PJ_SUCCESS);
2488
2489 /* Create reverse channel for channel1 (right channel?)... */
2490 status = pjmedia_splitcomb_create_rev_channel(app_config.pool,
2491 app_config.sc,
2492 1 /* ch1 */,
2493 0 /* options */,
2494 &app_config.sc_ch1);
2495 pj_assert(status == PJ_SUCCESS);
2496
2497 /* .. and register it to conference bridge (it would be slot1
2498 * if there's no other devices connected to the bridge)
2499 */
2500 status = pjsua_conf_add_port(app_config.pool, app_config.sc_ch1,
2501 &app_config.sc_ch1_slot);
2502 pj_assert(status == PJ_SUCCESS);
2503
2504 /* Create sound device */
2505 status = pjmedia_snd_port_create(app_config.pool, -1, -1,
2506 PJMEDIA_PIA_SRATE(&conf->info),
2507 2 /* stereo */,
2508 2 * PJMEDIA_PIA_SPF(&conf->info),
2509 PJMEDIA_PIA_BITS(&conf->info),
2510 0, &app_config.snd);
2511
2512 pj_assert(status == PJ_SUCCESS);
2513
2514
2515 /* Connect the splitter to the sound device */
2516 status = pjmedia_snd_port_connect(app_config.snd, app_config.sc);
2517 pj_assert(status == PJ_SUCCESS);
2518 }
2519 #endif