/* * Copyright (C) 2021 Aon plc * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include #include #include #include #include #include // test #include #define SIGNATURE PJMEDIA_SIG_CLASS_PORT_AUD('D','M') #define DMODEM_DIAL_MODE 0 #define DMODEM_ANSWER_MODE 2 #define DMODEM_RING_DETECT_MODE 3 uint8_t mode = DMODEM_DIAL_MODE; uint8_t ringing = 0; pjsua_call_id incoming; struct dmodem { pjmedia_port base; pj_timestamp timestamp; pj_sock_t sock; }; static struct dmodem port; static bool destroying = false; static pj_pool_t *pool; static void error_exit(const char *title, pj_status_t status) { pjsua_perror(__FILE__, title, status); if (!destroying) { destroying = true; pjsua_destroy(); exit(1); } } static pj_status_t dmodem_put_frame(pjmedia_port *this_port, pjmedia_frame *frame) { struct dmodem *sm = (struct dmodem *)this_port; int len; if (frame->type == PJMEDIA_FRAME_TYPE_AUDIO) { if ((len=write(sm->sock, frame->buf, frame->size)) != frame->size) { error_exit("error writing frame",0); } } return PJ_SUCCESS; } static pj_status_t dmodem_get_frame(pjmedia_port *this_port, pjmedia_frame *frame) { struct dmodem *sm = (struct dmodem *)this_port; frame->size = PJMEDIA_PIA_AVG_FSZ(&this_port->info); // MAX? what is int len; if ((len=read(sm->sock, frame->buf, frame->size)) != frame->size) { error_exit("error reading frame",0); } frame->timestamp.u64 = sm->timestamp.u64; frame->type = PJMEDIA_FRAME_TYPE_AUDIO; sm->timestamp.u64 += PJMEDIA_PIA_SPF(&this_port->info); return PJ_SUCCESS; } static pj_status_t dmodem_on_destroy(pjmedia_port *this_port) { printf("destroy\n"); exit(-1); } /* Callback called by the library when call's state has changed */ static void on_call_state(pjsua_call_id call_id, pjsip_event *e) { pjsua_call_info ci; PJ_UNUSED_ARG(e); pjsua_call_get_info(call_id, &ci); PJ_LOG(3,(__FILE__, "Call %d state=%.*s", call_id, (int)ci.state_text.slen, ci.state_text.ptr)); if (ci.state == PJSIP_INV_STATE_DISCONNECTED) { if(mode != DMODEM_RING_DETECT_MODE) { close(port.sock); if (!destroying) { destroying = true; pjsua_destroy(); exit(0); } } ringing = 0; } } static void on_incoming_call(pjsua_acc_id acc_id, pjsua_call_id call_id, pjsip_rx_data *rdata) { pjsua_call_info ci; pjsua_call_get_info(call_id, &ci); char* tmp = malloc(pj_strlen(&ci.remote_contact)+1); strcpy(tmp, ci.remote_contact.ptr); tmp[pj_strlen(&ci.remote_contact)] = '\0'; printf("Incoming call from: %s\n", tmp); free(tmp); incoming = call_id; ringing = 1; } /* Callback called by the library when call's media state has changed */ static void on_call_media_state(pjsua_call_id call_id) { pjsua_call_info ci; pjsua_conf_port_id port_id; static int done=0; pjsua_call_get_info(call_id, &ci); // printf("media_status %d media_cnt %d ci.conf_slot %d aud.conf_slot %d\n",ci.media_status,ci.media_cnt,ci.conf_slot,ci.media[0].stream.aud.conf_slot); if (ci.media_status == PJSUA_CALL_MEDIA_ACTIVE) { if (!done) { pjsua_conf_add_port(pool, &port.base, &port_id); pjsua_conf_connect(ci.conf_slot, port_id); pjsua_conf_connect(port_id, ci.conf_slot); done = 1; } } else { done = 0; } } int main(int argc, char *argv[]) { pjsua_acc_id acc_id; pj_status_t status; if (argc != 3) { return -1; } ringing = 0; if(!strncmp(argv[1], "++", 2)) { mode = DMODEM_ANSWER_MODE; } else if(!strncmp(argv[1], "rr", 2)) { mode = DMODEM_RING_DETECT_MODE; } signal(SIGPIPE,SIG_IGN); char *dialstr = argv[1]; char *sip_user = "dialupuser"; char *sip_domain = "192.168.1.2"; char *sip_pass = "pppasswdModem1"; printf("sip data: user: %s, passwd: %s, server: %s\nMODE: %d\n", sip_user, sip_pass, sip_domain, mode); status = pjsua_create(); if (status != PJ_SUCCESS) error_exit("Error in pjsua_create()", status); /* Init pjsua */ { pjsua_config cfg; pjsua_logging_config log_cfg; pjsua_media_config med_cfg; pjsua_config_default(&cfg); cfg.cb.on_call_media_state = &on_call_media_state; cfg.cb.on_call_state = &on_call_state; if(mode == DMODEM_RING_DETECT_MODE) { cfg.cb.on_incoming_call = &on_incoming_call; } pjsua_logging_config_default(&log_cfg); log_cfg.console_level = 4; pjsua_media_config_default(&med_cfg); med_cfg.no_vad = true; med_cfg.ec_tail_len = 0; med_cfg.jb_max = 2000; // med_cfg.jb_init = 200; med_cfg.audio_frame_ptime = 5; status = pjsua_init(&cfg, &log_cfg, &med_cfg); if (status != PJ_SUCCESS) error_exit("Error in pjsua_init()", status); } pjsua_set_ec(0,0); // maybe? pjsua_set_null_snd_dev(); /* g711 only */ pjsua_codec_info codecs[32]; unsigned count = sizeof(codecs)/sizeof(*codecs); pjsua_enum_codecs(codecs,&count); for (int i=0; i