Ignore:
Timestamp:
Apr 19, 2013 6:05:06 AM (11 years ago)
Author:
riza
Message:

Re #1643: Code restructure + modification to handle non blocking mode

File:
1 edited

Legend:

Unmodified
Added
Removed
  • pjproject/trunk/pjsip-apps/src/pjsua/pjsua_app.c

    r4465 r4476  
    1818 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA  
    1919 */ 
    20 #include "pjsua_common.h" 
     20#include "pjsua_app.h" 
    2121 
    2222#define THIS_FILE       "pjsua_app.c" 
     
    5252static void ring_start(pjsua_call_id call_id); 
    5353static void ring_stop(pjsua_call_id call_id); 
    54  
    55 static pj_status_t          receive_end_sig; 
    56 static pj_thread_t          *sig_thread; 
     54static pj_status_t pjsua_app_init(); 
     55static pj_status_t pjsua_app_destroy(); 
     56 
     57static app_cfg_t app_cfg; 
    5758pj_str_t                    uri_arg; 
    58 pj_bool_t                   app_restart; 
    5959pj_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); 
    7660 
    7761/***************************************************************************** 
     
    178162} 
    179163 
    180  
    181164/* 
    182165 * Handler when invite state has changed. 
     
    230213    } else { 
    231214 
    232         if (app_config.duration != NO_LIMIT_DURATION &&  
     215        if (app_config.duration != PJSUA_APP_NO_LIMIT_DURATION &&  
    233216            call_info.state == PJSIP_INV_STATE_CONFIRMED)  
    234217        { 
     
    315298        call_opt.vid_cnt = app_config.vid.vid_cnt; 
    316299 
    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); 
    318302    } 
    319303     
     
    533517 
    534518                /* 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                { 
    536522                    pjsua_conf_connect(pjsua_call_get_conf_port(call_ids[i]),  
    537523                                       app_config.rec_port); 
     
    551537 
    552538            /* 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            { 
    554541                pjsua_conf_connect(call_conf_slot, app_config.rec_port); 
    555542                pjsua_conf_connect(0, app_config.rec_port); 
     
    616603            PJ_LOG(3,(THIS_FILE, 
    617604                      "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)); 
    620607        } 
    621608    } 
     
    896883            char buf[100]; 
    897884 
    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); 
    900887            pjsua_perror(THIS_FILE, buf, info->status); 
    901888        } 
     
    12271214}; 
    12281215 
    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 */ 
     1219PJ_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 */ 
     1238PJ_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 */ 
     1249PJ_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 
    12881259 
    12891260/***************************************************************************** 
    12901261 * Public API 
    12911262 */ 
    1292  
    1293 #if defined(PJ_WIN32) && PJ_WIN32!=0 
    1294 PJ_DEF(void) setup_signal_handler(void) 
    1295 { 
    1296     SetConsoleCtrlHandler(&CtrlHandler, TRUE); 
    1297 } 
    1298 #else 
    1299 PJ_DEF(void) setup_signal_handler(void) 
    1300 { 
    1301 } 
    1302 #endif 
    13031263 
    13041264int stdout_refresh_proc(void *arg) 
     
    13231283} 
    13241284 
    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) 
     1285static pj_status_t pjsua_app_init() 
    13291286{ 
    13301287    pjsua_transport_id transport_id = -1; 
     
    13331290    pj_status_t status; 
    13341291 
    1335     /** Setup pjsua **/ 
    1336     status = setup_pjsua(); 
     1292    /** Create pjsua **/ 
     1293    status = pjsua_create(); 
    13371294    if (status != PJ_SUCCESS) 
    13381295        return status; 
    13391296 
    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); 
    13421310    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; 
    13511312 
    13521313    /* Initialize application callbacks */ 
     
    13741335    app_config.cfg.cb.on_create_media_transport = &on_create_media_transport; 
    13751336#endif 
    1376     app_config.log_cfg.cb = log_cb; 
    13771337 
    13781338    /* Set sound device latency */ 
     
    13811341    if (app_config.playback_lat) 
    13821342        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); 
    13831346 
    13841347    /* Initialize pjsua */ 
     
    18341797    /* Optionally disable some codec */ 
    18351798    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); 
    18371801#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); 
    18391804#endif 
    18401805    } 
     
    18461811#if PJSUA_HAS_VIDEO 
    18471812        pjsua_vid_codec_set_priority(&app_config.codec_arg[i], 
    1848                                      (pj_uint8_t)(PJMEDIA_CODEC_PRIO_NORMAL+i+9)); 
     1813                                   (pj_uint8_t)(PJMEDIA_CODEC_PRIO_NORMAL+i+9)); 
    18491814#endif 
    18501815    } 
     
    18711836    pjsua_call_setting_default(&call_opt); 
    18721837    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 
     1842on_error: 
     1843    app_destroy(); 
     1844    return status; 
     1845} 
     1846 
     1847PJ_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; 
    18741855 
    18751856    /* Init CLI if configured */     
    18761857    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    }  
    18931860    return status; 
    18941861} 
    18951862 
    1896 pj_status_t app_main(void) 
     1863pj_status_t app_run(pj_bool_t wait_telnet_cli) 
    18971864{ 
    18981865    pj_thread_t *stdout_refresh_thread = NULL; 
     1866    pj_status_t status; 
    18991867 
    19001868    /* Start console refresh thread */ 
     
    19041872    } 
    19051873 
     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 
    19061902    if (app_config.use_cli) 
    1907         start_cli_main(&uri_arg, &app_restart);  
     1903        cli_main(wait_telnet_cli);       
    19081904    else 
    1909         start_ui_main(&uri_arg, &app_restart); 
    1910  
     1905        legacy_main(); 
     1906 
     1907    status = PJ_SUCCESS; 
     1908 
     1909on_return: 
    19111910    if (stdout_refresh_thread) { 
    19121911        stdout_refresh_quit = PJ_TRUE; 
     
    19141913        pj_thread_destroy(stdout_refresh_thread); 
    19151914    } 
    1916  
    1917     return PJ_SUCCESS; 
    1918 } 
    1919  
    1920 pj_status_t app_destroy() 
    1921 { 
    1922     pj_status_t status; 
     1915    return status; 
     1916} 
     1917 
     1918static pj_status_t pjsua_app_destroy() 
     1919{ 
     1920    pj_status_t status = PJ_SUCCESS; 
    19231921    unsigned i; 
    19241922 
     
    19731971    } 
    19741972 
    1975     if (app_config.use_cli) {    
    1976         destroy_cli(app_restart); 
    1977     } 
    1978  
    19791973    if (app_config.pool) { 
    19801974        pj_pool_release(app_config.pool); 
    19811975        app_config.pool = NULL; 
    19821976    } 
    1983      
     1977 
    19841978    status = pjsua_destroy(); 
    1985      
    1986     if (!app_restart) 
    1987         pj_bzero(&app_config, sizeof(app_config)); 
     1979 
     1980    return status; 
     1981} 
     1982 
     1983pj_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    } 
    19881992     
    19891993    return status; 
    19901994} 
    19911995 
    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  
    20211996/** ======================= **/ 
    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 } 
    20561997 
    20571998#ifdef STEREO_DEMO 
Note: See TracChangeset for help on using the changeset viewer.