Changeset 4476 for pjproject/trunk/pjsip-apps/src/pjsua/pjsua_app.c
- Timestamp:
- Apr 19, 2013 6:05:06 AM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
pjproject/trunk/pjsip-apps/src/pjsua/pjsua_app.c
r4465 r4476 18 18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 19 19 */ 20 #include "pjsua_ common.h"20 #include "pjsua_app.h" 21 21 22 22 #define THIS_FILE "pjsua_app.c" … … 52 52 static void ring_start(pjsua_call_id call_id); 53 53 static void ring_stop(pjsua_call_id call_id); 54 55 static pj_status_t receive_end_sig; 56 static pj_thread_t *sig_thread; 54 static pj_status_t pjsua_app_init(); 55 static pj_status_t pjsua_app_destroy(); 56 57 static app_cfg_t app_cfg; 57 58 pj_str_t uri_arg; 58 pj_bool_t app_restart;59 59 pj_bool_t app_running = PJ_FALSE; 60 pj_log_func *log_cb = NULL;61 62 /** Forward declaration **/63 /** Defined in pjsua_common.c **/64 void app_config_init_video(pjsua_acc_config *acc_cfg);65 /** Defined in pjsua_legacy.c **/66 void start_ui_main(pj_str_t *uri_to_call, pj_bool_t *app_restart);67 /** Defined in pjsua_cli.c **/68 void start_cli_main(pj_str_t *uri_to_call, pj_bool_t *app_restart);69 pj_status_t setup_cli(pj_bool_t with_console, pj_bool_t with_telnet,70 pj_uint16_t telnet_port,71 pj_cli_telnet_on_started on_started_cb,72 pj_cli_on_quit on_quit_cb,73 pj_cli_on_destroy on_destroy_cb,74 pj_cli_on_restart_pjsua on_restart_pjsua_cb);75 void destroy_cli(pj_bool_t app_restart);76 60 77 61 /***************************************************************************** … … 178 162 } 179 163 180 181 164 /* 182 165 * Handler when invite state has changed. … … 230 213 } else { 231 214 232 if (app_config.duration != NO_LIMIT_DURATION &&215 if (app_config.duration != PJSUA_APP_NO_LIMIT_DURATION && 233 216 call_info.state == PJSIP_INV_STATE_CONFIRMED) 234 217 { … … 315 298 call_opt.vid_cnt = app_config.vid.vid_cnt; 316 299 317 pjsua_call_answer2(call_id, &call_opt, app_config.auto_answer, NULL, NULL); 300 pjsua_call_answer2(call_id, &call_opt, app_config.auto_answer, NULL, 301 NULL); 318 302 } 319 303 … … 533 517 534 518 /* Automatically record conversation, if desired */ 535 if (app_config.auto_rec && app_config.rec_port != PJSUA_INVALID_ID) { 519 if (app_config.auto_rec && app_config.rec_port != 520 PJSUA_INVALID_ID) 521 { 536 522 pjsua_conf_connect(pjsua_call_get_conf_port(call_ids[i]), 537 523 app_config.rec_port); … … 551 537 552 538 /* Automatically record conversation, if desired */ 553 if (app_config.auto_rec && app_config.rec_port != PJSUA_INVALID_ID) { 539 if (app_config.auto_rec && app_config.rec_port != PJSUA_INVALID_ID) 540 { 554 541 pjsua_conf_connect(call_conf_slot, app_config.rec_port); 555 542 pjsua_conf_connect(0, app_config.rec_port); … … 616 603 PJ_LOG(3,(THIS_FILE, 617 604 "Just rejected incoming video offer on call %d, " 618 "use \"vid call enable %d\" or \"vid call add\" to enable video!",619 call_id, vid_idx));605 "use \"vid call enable %d\" or \"vid call add\" to " 606 "enable video!", call_id, vid_idx)); 620 607 } 621 608 } … … 896 883 char buf[100]; 897 884 898 snprintf(buf, sizeof(buf), "SIP %s transport is disconnected from %s",899 tp->type_name, host_port);885 snprintf(buf, sizeof(buf), "SIP %s transport is disconnected " 886 "from %s", tp->type_name, host_port); 900 887 pjsua_perror(THIS_FILE, buf, info->status); 901 888 } … … 1227 1214 }; 1228 1215 1229 #if defined(PJ_WIN32) && PJ_WIN32!=0 1230 #include <windows.h> 1231 1232 static pj_thread_desc handler_desc; 1233 1234 static BOOL WINAPI CtrlHandler(DWORD fdwCtrlType) 1235 { 1236 switch (fdwCtrlType) 1237 { 1238 // Handle the CTRL+C signal. 1239 1240 case CTRL_C_EVENT: 1241 case CTRL_CLOSE_EVENT: 1242 case CTRL_BREAK_EVENT: 1243 case CTRL_LOGOFF_EVENT: 1244 case CTRL_SHUTDOWN_EVENT: 1245 pj_thread_register("ctrlhandler", handler_desc, &sig_thread); 1246 PJ_LOG(3,(THIS_FILE, "Ctrl-C detected, quitting..")); 1247 receive_end_sig = PJ_TRUE; 1248 app_destroy(); 1249 ExitProcess(1); 1250 PJ_UNREACHED(return TRUE;) 1251 1252 default: 1253 1254 return FALSE; 1255 } 1256 } 1257 1258 static void setup_socket_signal() 1259 { 1260 } 1261 1262 #else 1263 #include <signal.h> 1264 1265 static void setup_socket_signal() 1266 { 1267 signal(SIGPIPE, SIG_IGN); 1268 } 1269 1270 #endif 1271 1272 static pj_status_t setup_pjsua() 1273 { 1274 pj_status_t status = pjsua_destroy(); 1275 if (status != PJ_SUCCESS) 1276 return status; 1277 1278 /* Create pjsua */ 1279 status = pjsua_create(); 1280 if (status != PJ_SUCCESS) 1281 return status; 1282 1283 /* Create pool for application */ 1284 app_config.pool = pjsua_pool_create("pjsua-app", 1000, 1000); 1285 1286 return status; 1287 } 1216 /** CLI callback **/ 1217 1218 /* Called on CLI (re)started, e.g: initial start, after iOS bg */ 1219 PJ_DEF(void) cli_on_started(pj_status_t status) 1220 { 1221 /* Notify app */ 1222 if (app_cfg.on_started) { 1223 if (status == PJ_SUCCESS) { 1224 char info[128]; 1225 cli_get_info(info, sizeof(info)); 1226 if (app_cfg.on_started) { 1227 (*app_cfg.on_started)(status, info); 1228 } 1229 } else { 1230 if (app_cfg.on_started) { 1231 (*app_cfg.on_started)(status, NULL); 1232 } 1233 } 1234 } 1235 } 1236 1237 /* Called on CLI quit */ 1238 PJ_DEF(pj_bool_t) cli_on_stopped(pj_bool_t restart, int argc, char* argv[]) 1239 { 1240 /* Notify app */ 1241 if (app_cfg.on_stopped) 1242 return (*app_cfg.on_stopped)(restart, argc, argv); 1243 1244 return PJ_SUCCESS; 1245 } 1246 1247 1248 /* Called on pjsua legacy quit */ 1249 PJ_DEF(pj_bool_t) legacy_on_stopped(pj_bool_t restart) 1250 { 1251 /* Notify app */ 1252 if (app_cfg.on_stopped) 1253 return (*app_cfg.on_stopped)(restart, 0, NULL); 1254 1255 return PJ_SUCCESS; 1256 } 1257 1258 1288 1259 1289 1260 /***************************************************************************** 1290 1261 * Public API 1291 1262 */ 1292 1293 #if defined(PJ_WIN32) && PJ_WIN32!=01294 PJ_DEF(void) setup_signal_handler(void)1295 {1296 SetConsoleCtrlHandler(&CtrlHandler, TRUE);1297 }1298 #else1299 PJ_DEF(void) setup_signal_handler(void)1300 {1301 }1302 #endif1303 1263 1304 1264 int stdout_refresh_proc(void *arg) … … 1323 1283 } 1324 1284 1325 PJ_DEF(pj_status_t) app_init(pj_cli_telnet_on_started on_started_cb, 1326 pj_cli_on_quit on_quit_cb, 1327 pj_cli_on_destroy on_destroy_cb, 1328 pj_cli_on_restart_pjsua on_restart_pjsua_cb) 1285 static pj_status_t pjsua_app_init() 1329 1286 { 1330 1287 pjsua_transport_id transport_id = -1; … … 1333 1290 pj_status_t status; 1334 1291 1335 /** Setuppjsua **/1336 status = setup_pjsua();1292 /** Create pjsua **/ 1293 status = pjsua_create(); 1337 1294 if (status != PJ_SUCCESS) 1338 1295 return status; 1339 1296 1340 /** Load config **/ 1341 status = load_config(&app_config, &uri_arg, app_running); 1297 /* Create pool for application */ 1298 app_config.pool = pjsua_pool_create("pjsua-app", 1000, 1000); 1299 1300 /* Init CLI & its FE settings */ 1301 if (!app_running) { 1302 pj_cli_cfg_default(&app_config.cli_cfg.cfg); 1303 pj_cli_telnet_cfg_default(&app_config.cli_cfg.telnet_cfg); 1304 pj_cli_console_cfg_default(&app_config.cli_cfg.console_cfg); 1305 app_config.cli_cfg.telnet_cfg.on_started = cli_on_started; 1306 } 1307 1308 /** Parse args **/ 1309 status = load_config(app_cfg.argc, app_cfg.argv, &uri_arg); 1342 1310 if (status != PJ_SUCCESS) 1343 return status; 1344 1345 #if defined(PJ_SYMBIAN) && PJ_SYMBIAN!=0 1346 /* Disable threading on Symbian */ 1347 app_config.cfg.thread_cnt = 0; 1348 app_config.media_cfg.thread_cnt = 0; 1349 app_config.media_cfg.has_ioqueue = PJ_FALSE; 1350 #endif 1311 return status; 1351 1312 1352 1313 /* Initialize application callbacks */ … … 1374 1335 app_config.cfg.cb.on_create_media_transport = &on_create_media_transport; 1375 1336 #endif 1376 app_config.log_cfg.cb = log_cb;1377 1337 1378 1338 /* Set sound device latency */ … … 1381 1341 if (app_config.playback_lat) 1382 1342 app_config.media_cfg.snd_play_latency = app_config.playback_lat; 1343 1344 if (app_cfg.on_config_init) 1345 (*app_cfg.on_config_init)(&app_config); 1383 1346 1384 1347 /* Initialize pjsua */ … … 1834 1797 /* Optionally disable some codec */ 1835 1798 for (i=0; i<app_config.codec_dis_cnt; ++i) { 1836 pjsua_codec_set_priority(&app_config.codec_dis[i],PJMEDIA_CODEC_PRIO_DISABLED); 1799 pjsua_codec_set_priority(&app_config.codec_dis[i], 1800 PJMEDIA_CODEC_PRIO_DISABLED); 1837 1801 #if PJSUA_HAS_VIDEO 1838 pjsua_vid_codec_set_priority(&app_config.codec_dis[i],PJMEDIA_CODEC_PRIO_DISABLED); 1802 pjsua_vid_codec_set_priority(&app_config.codec_dis[i], 1803 PJMEDIA_CODEC_PRIO_DISABLED); 1839 1804 #endif 1840 1805 } … … 1846 1811 #if PJSUA_HAS_VIDEO 1847 1812 pjsua_vid_codec_set_priority(&app_config.codec_arg[i], 1848 1813 (pj_uint8_t)(PJMEDIA_CODEC_PRIO_NORMAL+i+9)); 1849 1814 #endif 1850 1815 } … … 1871 1836 pjsua_call_setting_default(&call_opt); 1872 1837 call_opt.aud_cnt = app_config.aud_cnt; 1873 call_opt.vid_cnt = app_config.vid.vid_cnt; 1838 call_opt.vid_cnt = app_config.vid.vid_cnt; 1839 1840 return PJ_SUCCESS; 1841 1842 on_error: 1843 app_destroy(); 1844 return status; 1845 } 1846 1847 PJ_DEF(pj_status_t) app_init(const app_cfg_t *cfg) 1848 { 1849 pj_status_t status; 1850 pj_memcpy(&app_cfg, cfg, sizeof(app_cfg)); 1851 1852 status = pjsua_app_init(); 1853 if (status != PJ_SUCCESS) 1854 return status; 1874 1855 1875 1856 /* Init CLI if configured */ 1876 1857 if (app_config.use_cli) { 1877 if (app_restart) { 1878 pj_uint16_t port = (pj_uint16_t)app_config.cli_telnet_port; 1879 status = setup_cli(!app_config.disable_cli_console, 1880 app_config.cli_telnet_port >= 0, port, 1881 on_started_cb, on_quit_cb, on_destroy_cb, 1882 on_restart_pjsua_cb); 1883 if (status != PJ_SUCCESS) 1884 goto on_error; 1885 } 1886 } 1887 1888 return PJ_SUCCESS; 1889 1890 on_error: 1891 app_restart = PJ_FALSE; 1892 app_destroy(); 1858 status = cli_init(); 1859 } 1893 1860 return status; 1894 1861 } 1895 1862 1896 pj_status_t app_ main(void)1863 pj_status_t app_run(pj_bool_t wait_telnet_cli) 1897 1864 { 1898 1865 pj_thread_t *stdout_refresh_thread = NULL; 1866 pj_status_t status; 1899 1867 1900 1868 /* Start console refresh thread */ … … 1904 1872 } 1905 1873 1874 status = pjsua_start(); 1875 if (status != PJ_SUCCESS) 1876 goto on_return; 1877 1878 if (app_config.use_cli && (app_config.cli_cfg.cli_fe & CLI_FE_TELNET)) { 1879 char info[128]; 1880 cli_get_info(info, sizeof(info)); 1881 if (app_cfg.on_started) { 1882 (*app_cfg.on_started)(status, info); 1883 } 1884 } else { 1885 if (app_cfg.on_started) { 1886 (*app_cfg.on_started)(status, "Ready"); 1887 } 1888 } 1889 1890 /* If user specifies URI to call, then call the URI */ 1891 if (uri_arg.slen) { 1892 pjsua_call_setting_default(&call_opt); 1893 call_opt.aud_cnt = app_config.aud_cnt; 1894 call_opt.vid_cnt = app_config.vid.vid_cnt; 1895 1896 pjsua_call_make_call(current_acc, &uri_arg, &call_opt, NULL, 1897 NULL, NULL); 1898 } 1899 1900 app_running = PJ_TRUE; 1901 1906 1902 if (app_config.use_cli) 1907 start_cli_main(&uri_arg, &app_restart);1903 cli_main(wait_telnet_cli); 1908 1904 else 1909 start_ui_main(&uri_arg, &app_restart); 1910 1905 legacy_main(); 1906 1907 status = PJ_SUCCESS; 1908 1909 on_return: 1911 1910 if (stdout_refresh_thread) { 1912 1911 stdout_refresh_quit = PJ_TRUE; … … 1914 1913 pj_thread_destroy(stdout_refresh_thread); 1915 1914 } 1916 1917 return PJ_SUCCESS; 1918 } 1919 1920 pj_status_t app_destroy() 1921 { 1922 pj_status_t status; 1915 return status; 1916 } 1917 1918 static pj_status_t pjsua_app_destroy() 1919 { 1920 pj_status_t status = PJ_SUCCESS; 1923 1921 unsigned i; 1924 1922 … … 1973 1971 } 1974 1972 1975 if (app_config.use_cli) {1976 destroy_cli(app_restart);1977 }1978 1979 1973 if (app_config.pool) { 1980 1974 pj_pool_release(app_config.pool); 1981 1975 app_config.pool = NULL; 1982 1976 } 1983 1977 1984 1978 status = pjsua_destroy(); 1985 1986 if (!app_restart) 1987 pj_bzero(&app_config, sizeof(app_config)); 1979 1980 return status; 1981 } 1982 1983 pj_status_t app_destroy() 1984 { 1985 pj_status_t status; 1986 1987 status = pjsua_app_destroy(); 1988 1989 if (app_config.use_cli) { 1990 cli_destroy(); 1991 } 1988 1992 1989 1993 return status; 1990 1994 } 1991 1995 1992 /** === CLI Callback == **/1993 1994 static void cli_telnet_started(pj_cli_telnet_info *telnet_info)1995 {1996 PJ_LOG(3,(THIS_FILE, "Telnet daemon listening at %.*s:%d",1997 telnet_info->ip_address.slen, telnet_info->ip_address.ptr,1998 telnet_info->port));1999 }2000 2001 static void cli_on_quit (pj_bool_t is_restarted)2002 {2003 PJ_LOG(3,(THIS_FILE, "CLI quit, restart(%d)", is_restarted));2004 }2005 2006 static void cli_on_destroy(void)2007 {2008 PJ_LOG(3,(THIS_FILE, "CLI destroyed"));2009 }2010 2011 static void cli_on_restart_pjsua(void)2012 {2013 PJ_LOG(3,(THIS_FILE, "Restart pjsua"));2014 }2015 2016 pj_cli_telnet_on_started on_started_cb = &cli_telnet_started;2017 pj_cli_on_quit on_quit_cb = &cli_on_quit;2018 pj_cli_on_destroy on_destroy_cb = &cli_on_destroy;2019 pj_cli_on_restart_pjsua on_restart_pjsua_cb = &cli_on_restart_pjsua;2020 2021 1996 /** ======================= **/ 2022 2023 int main_func(int argc, char *argv[])2024 {2025 pj_status_t status;2026 2027 setup_socket_signal();2028 2029 receive_end_sig = PJ_FALSE;2030 app_restart = PJ_TRUE;2031 2032 add_startup_config(argc, argv);2033 2034 do {2035 if (app_restart) {2036 status = app_init(on_started_cb, on_quit_cb,2037 on_destroy_cb, on_restart_pjsua_cb);2038 if (status != PJ_SUCCESS)2039 return 1;2040 }2041 2042 app_running = PJ_TRUE;2043 2044 app_main();2045 if (!receive_end_sig) {2046 app_destroy();2047 2048 /* This is on purpose */2049 app_destroy();2050 } else {2051 pj_thread_join(sig_thread);2052 }2053 } while (app_restart);2054 return 0;2055 }2056 1997 2057 1998 #ifdef STEREO_DEMO
Note: See TracChangeset
for help on using the changeset viewer.