static void ui_add_buddy() { char buf[128]; pjsua_buddy_config buddy_cfg; pjsua_buddy_id buddy_id; pj_status_t status; if (!simple_input("Enter buddy's URI:", buf, sizeof(buf))) return; if (pjsua_verify_url(buf) != PJ_SUCCESS) { printf("Invalid URI '%s'\n", buf); return; } pj_bzero(&buddy_cfg, sizeof(pjsua_buddy_config)); buddy_cfg.uri = pj_str(buf); buddy_cfg.subscribe = PJ_TRUE; status = pjsua_buddy_add(&buddy_cfg, &buddy_id); if (status == PJ_SUCCESS) { printf("New buddy '%s' added at index %d\n", buf, buddy_id+1); } }
/* * main() * * argv[1] may contain URL to call. */ int main(int argc, char *argv[]) { pjsua_acc_id acc_id; pj_status_t status; /* Create pjsua first! */ status = pjsua_create(); if (status != PJ_SUCCESS) error_exit("Error in pjsua_create()", status); /* If argument is specified, it's got to be a valid SIP URL */ if (argc > 1) { status = pjsua_verify_url(argv[1]); if (status != PJ_SUCCESS) error_exit("Invalid URL in argv", status); } /* Init pjsua */ { pjsua_config cfg; pjsua_logging_config log_cfg; pjsua_config_default(&cfg); cfg.cb.on_incoming_call = &on_incoming_call; cfg.cb.on_call_media_state = &on_call_media_state; cfg.cb.on_call_state = &on_call_state; pjsua_logging_config_default(&log_cfg); log_cfg.console_level = 4; status = pjsua_init(&cfg, &log_cfg, NULL); if (status != PJ_SUCCESS) error_exit("Error in pjsua_init()", status); } /* Add UDP transport. */ { pjsua_transport_config cfg; pjsua_transport_config_default(&cfg); cfg.port = 5060; status = pjsua_transport_create(PJSIP_TRANSPORT_UDP, &cfg, NULL); if (status != PJ_SUCCESS) error_exit("Error creating transport", status); } /* Initialization is done, now start pjsua */ status = pjsua_start(); if (status != PJ_SUCCESS) error_exit("Error starting pjsua", status); /* Register to SIP server by creating SIP account. */ { pjsua_acc_config cfg; pjsua_acc_config_default(&cfg); cfg.id = pj_str("sip:" SIP_USER "@" SIP_DOMAIN); cfg.reg_uri = pj_str("sip:" SIP_DOMAIN); cfg.cred_count = 1; cfg.cred_info[0].realm = pj_str(SIP_DOMAIN); cfg.cred_info[0].scheme = pj_str("digest"); cfg.cred_info[0].username = pj_str(SIP_USER); cfg.cred_info[0].data_type = PJSIP_CRED_DATA_PLAIN_PASSWD; cfg.cred_info[0].data = pj_str(SIP_PASSWD); status = pjsua_acc_add(&cfg, PJ_TRUE, &acc_id); if (status != PJ_SUCCESS) error_exit("Error adding account", status); } /* If URL is specified, make call to the URL. */ if (argc > 1) { pj_str_t uri = pj_str(argv[1]); status = pjsua_call_make_call(acc_id, &uri, 0, NULL, NULL, NULL); if (status != PJ_SUCCESS) error_exit("Error making call", status); } /* Wait until user press "q" to quit. */ for (;;) { char option[10]; puts("Press 'h' to hangup all calls, 'q' to quit"); if (fgets(option, sizeof(option), stdin) == NULL) { puts("EOF while reading stdin, will quit now.."); break; } if (option[0] == 'q') break; if (option[0] == 'h') pjsua_call_hangup_all(); } /* Destroy pjsua */ pjsua_destroy(); return 0; }
/* * Input URL. */ static void ui_input_url(const char *title, char *buf, pj_size_t len, input_result *result) { result->nb_result = PJSUA_APP_NO_NB; result->uri_result = NULL; print_buddy_list(); printf("Choices:\n" " 0 For current dialog.\n" " -1 All %d buddies in buddy list\n" " [1 -%2d] Select from buddy list\n" " URL An URL\n" " <Enter> Empty input (or 'q') to cancel\n" , pjsua_get_buddy_count(), pjsua_get_buddy_count()); printf("%s: ", title); fflush(stdout); if (fgets(buf, (int)len, stdin) == NULL) return; len = strlen(buf); /* Left trim */ while (pj_isspace(*buf)) { ++buf; --len; } /* Remove trailing newlines */ while (len && (buf[len-1] == '\r' || buf[len-1] == '\n')) buf[--len] = '\0'; if (len == 0 || buf[0]=='q') return; if (pj_isdigit(*buf) || *buf=='-') { unsigned i; if (*buf=='-') i = 1; else i = 0; for (; i<len; ++i) { if (!pj_isdigit(buf[i])) { puts("Invalid input"); return; } } result->nb_result = my_atoi(buf); if (result->nb_result >= 0 && result->nb_result <= (int)pjsua_get_buddy_count()) { return; } if (result->nb_result == -1) return; puts("Invalid input"); result->nb_result = PJSUA_APP_NO_NB; return; } else { pj_status_t status; if ((status=pjsua_verify_url(buf)) != PJ_SUCCESS) { pjsua_perror(THIS_FILE, "Invalid URL", status); return; } result->uri_result = buf; } }