void init_ringback_tone(){ pj_status_t status; pj_str_t name; pjmedia_tone_desc tone[RINGBACK_CNT]; unsigned i; app_config.pool = pjsua_pool_create("pjsua-jni", 1000, 1000); app_config.ringback_slot=PJSUA_INVALID_ID; app_config.ringback_on = PJ_FALSE; app_config.ringback_cnt = 0; //Ringback name = pj_str((char *)"ringback"); status = pjmedia_tonegen_create2(app_config.pool, &name, 16000, 1, 320, 16, PJMEDIA_TONEGEN_LOOP, &app_config.ringback_port); if (status != PJ_SUCCESS){ goto on_error; } pj_bzero(&tone, sizeof(tone)); for (i=0; i<RINGBACK_CNT; ++i) { tone[i].freq1 = RINGBACK_FREQ1; tone[i].freq2 = RINGBACK_FREQ2; tone[i].on_msec = RINGBACK_ON; tone[i].off_msec = RINGBACK_OFF; } tone[RINGBACK_CNT-1].off_msec = RINGBACK_INTERVAL; pjmedia_tonegen_play(app_config.ringback_port, RINGBACK_CNT, tone, PJMEDIA_TONEGEN_LOOP); status = pjsua_conf_add_port(app_config.pool, app_config.ringback_port, &app_config.ringback_slot); if (status != PJ_SUCCESS){ goto on_error; } return; on_error :{ pj_pool_release(app_config.pool); } }
/***************************************************************************** * test: play simple ringback tone and hear it */ static void systest_play_tone(void) { /* Ringtones */ #define RINGBACK_FREQ1 440 /* 400 */ #define RINGBACK_FREQ2 480 /* 450 */ #define RINGBACK_ON 3000 /* 400 */ #define RINGBACK_OFF 4000 /* 200 */ #define RINGBACK_CNT 1 /* 2 */ #define RINGBACK_INTERVAL 4000 /* 2000 */ unsigned i, samples_per_frame; pjmedia_tone_desc tone[RINGBACK_CNT]; pj_pool_t *pool = NULL; pjmedia_port *ringback_port = NULL; enum gui_key key; int ringback_slot = -1; test_item_t *ti; pj_str_t name; const char *title = "Audio Tone Playback Test"; pj_status_t status; ti = systest_alloc_test_item(title); if (!ti) return; key = gui_msgbox(title, "This test will play simple ringback tone to " "the speaker. Please listen carefully for audio " "impairments such as stutter. You may need " "to let this test running for a while to " "make sure that everything is okay. Press " "OK to start, CANCEL to skip", WITH_OKCANCEL); if (key != KEY_OK) { ti->skipped = PJ_TRUE; return; } PJ_LOG(3,(THIS_FILE, "Running %s", title)); pool = pjsua_pool_create("ringback", 512, 512); samples_per_frame = systest.media_cfg.audio_frame_ptime * systest.media_cfg.clock_rate * systest.media_cfg.channel_count / 1000; /* Ringback tone (call is ringing) */ name = pj_str("ringback"); status = pjmedia_tonegen_create2(pool, &name, systest.media_cfg.clock_rate, systest.media_cfg.channel_count, samples_per_frame, 16, PJMEDIA_TONEGEN_LOOP, &ringback_port); if (status != PJ_SUCCESS) goto on_return; pj_bzero(&tone, sizeof(tone)); for (i=0; i<RINGBACK_CNT; ++i) { tone[i].freq1 = RINGBACK_FREQ1; tone[i].freq2 = RINGBACK_FREQ2; tone[i].on_msec = RINGBACK_ON; tone[i].off_msec = RINGBACK_OFF; } tone[RINGBACK_CNT-1].off_msec = RINGBACK_INTERVAL; status = pjmedia_tonegen_play(ringback_port, RINGBACK_CNT, tone, PJMEDIA_TONEGEN_LOOP); if (status != PJ_SUCCESS) goto on_return; status = pjsua_conf_add_port(pool, ringback_port, &ringback_slot); if (status != PJ_SUCCESS) goto on_return; status = pjsua_conf_connect(ringback_slot, 0); if (status != PJ_SUCCESS) goto on_return; key = gui_msgbox(title, "Ringback tone should be playing now in the " "speaker. Press OK to stop. ", WITH_OK); status = PJ_SUCCESS; on_return: if (ringback_slot != -1) pjsua_conf_remove_port(ringback_slot); if (ringback_port) pjmedia_port_destroy(ringback_port); if (pool) pj_pool_release(pool); if (status != PJ_SUCCESS) { systest_perror("Sorry we encounter error when initializing " "the tone generator: ", status); ti->success = PJ_FALSE; pj_strerror(status, ti->reason, sizeof(ti->reason)); } else { key = gui_msgbox(title, "Is the audio okay?", WITH_YESNO); ti->success = (key == KEY_YES); if (!ti->success) pj_ansi_strcpy(ti->reason, USER_ERROR); } return; }