Old question but posting my answers here for anyone who might stumble on it:
Playing a ringtone
Assuming your ringtone is a wav file, you need to create a wav player and connect its port to your output device. The wav file will loop until you disconnect the port, then start again when you reconnect it.
Calling init_ringtone_player
should be done after calling pjsua_init
. ringtone_port_info
is a global struct to keep track of the port and ring state.
typedef struct _ringtone_port_info {
int ring_on;
int ring_slot;
pjmedia_port *ring_port;
pj_pool_t *pool;
} ringtone_port_info_t;
static ringtone_port_info_t ringtone_port_info;
static void init_ringtone_player() {
int file_slot;
pj_pool_t *pool;
pjmedia_port *file_port;
pj_status_t status;
pool = pjsua_pool_create("wav", 4000, 4000);
status = pjmedia_wav_player_port_create(pool, "ringtone.wav",
0, 0, 0, &file_port);
if (status != PJ_SUCCESS) {
error_exit("Error creating WAV player port", status);
return;
}
status = pjsua_conf_add_port(pool, file_port, &file_slot);
if (status != PJ_SUCCESS) {
error_exit("Error adding port to conference", status);
return;
}
ringtone_port_info = (ringtone_port_info_t) { .ring_on = 0,
.ring_slot = file_slot, .ring_port = file_port , .pool = pool };
}
Then, make functions to start and stop the ringtone as needed (i.e. during on_incoming_call
, on_call_state
or wherever). The important function call to note here is pjsua_conf_connect
.
pj_status_t start_ring() {
pj_status_t status;
if (ringtone_port_info.ring_on) {
printf("Ringtone port already connected\n");
return PJ_SUCCESS;
}
printf("Starting ringtone\n");
status = pjsua_conf_connect(ringtone_port_info.ring_slot, 0);
ringtone_port_info.ring_on = 1;
if (status != PJ_SUCCESS)
error_exit("Error connecting ringtone port", status);
return status;
}
pj_status_t stop_ring() {
pj_status_t status;
if (!ringtone_port_info.ring_on) {
printf("Ringtone port already disconnected\n");
return PJ_SUCCESS;
}
printf("Stopping ringtone\n");
status = pjsua_conf_disconnect(ringtone_port_info.ring_slot, 0);
ringtone_port_info.ring_on = 0;
if (status != PJ_SUCCESS)
error_exit("Error disconnecting ringtone port", status);
return status;
}
Make sure you call pjsua_destroy
when you're done to release the pool (or manually release it)
SIP response codes
See here for a list of status codes:
https://www.pjsip.org/pjsip/docs/html/group__PJSIP__MSG__LINE.htm#
You can use 200 to accept and 603 to decline (using pjsua_call_answer
)