This commit is contained in:
JerryXiao 2022-11-11 01:24:38 +08:00
parent b07d69a385
commit 755cfb8a54
Signed by: Jerry
GPG key ID: 22618F758B5BE2E5
4 changed files with 110 additions and 16 deletions

View file

@ -9,7 +9,7 @@ $(PKG_CONFIG_PATH)/libpjproject.pc:
$(MAKE) -C $(PJSIP_DIR) install
d-modem: d-modem.c $(PKG_CONFIG_PATH)/libpjproject.pc
$(CC) -o $@ $< `PKG_CONFIG_PATH="$(PKG_CONFIG_PATH)" pkg-config --static --cflags --libs libpjproject`
$(CC) -o $@ $< `PKG_CONFIG_PATH="$(PKG_CONFIG_PATH)" pkg-config --static --cflags --libs libpjproject` -lpulse -lpulse-simple
slmodemd:
$(MAKE) -C slmodemd

112
d-modem.c
View file

@ -16,6 +16,7 @@
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <fcntl.h>
#include <unistd.h>
#include <stdbool.h>
#include <time.h>
@ -24,6 +25,19 @@
#include <errno.h>
// test
#include <pjsua-lib/pjsua.h>
#include <pjsua-lib/pjsua_internal.h>
#include <pulse/simple.h>
pa_simple *pa_s1;
pa_simple *pa_s2;
const pa_sample_spec pa_ss = {
.format = PA_SAMPLE_S16LE,
.rate = 9600,
.channels = 1
};
int wave_recv;
int wave_transmit;
#define SIGNATURE PJMEDIA_SIG_CLASS_PORT_AUD('D','M')
#define DMODEM_DIAL_MODE 0
@ -58,6 +72,8 @@ static pj_status_t dmodem_put_frame(pjmedia_port *this_port, pjmedia_frame *fram
int len;
if (frame->type == PJMEDIA_FRAME_TYPE_AUDIO) {
pa_simple_write(pa_s1, frame->buf, frame->size, NULL);
write(wave_recv, frame->buf, frame->size);
if ((len=write(sm->sock, frame->buf, frame->size)) != frame->size) {
error_exit("error writing frame",0);
}
@ -78,6 +94,8 @@ static pj_status_t dmodem_get_frame(pjmedia_port *this_port, pjmedia_frame *fram
frame->timestamp.u64 = sm->timestamp.u64;
frame->type = PJMEDIA_FRAME_TYPE_AUDIO;
sm->timestamp.u64 += PJMEDIA_PIA_SPF(&this_port->info);
pa_simple_write(pa_s2, frame->buf, frame->size, NULL);
write(wave_transmit, frame->buf, frame->size);
return PJ_SUCCESS;
}
@ -145,6 +163,40 @@ static void on_call_media_state(pjsua_call_id call_id) {
}
void start_pa() {
wave_recv = open("/tmp/dmodem.recv.s16le.9600hz.pcm", O_WRONLY | O_CREAT, 00644);
wave_transmit = open("/tmp/dmodem.transmit.s16le.9600hz.pcm", O_WRONLY | O_CREAT, 00644);
if (wave_recv <= 0 || wave_transmit <= 0)
error_exit("open wave files", 1);
pa_s1 = pa_simple_new(NULL, // Use the default server.
"dmodem", // Our application's name.
PA_STREAM_PLAYBACK,
NULL, // Use the default device.
"recv", // Description of our stream.
&pa_ss, // Our sample format.
NULL, // Use default channel map
NULL, // Use default buffering attributes.
NULL // Ignore error code.
);
pa_s2 = pa_simple_new(NULL, // Use the default server.
"dmodem", // Our application's name.
PA_STREAM_PLAYBACK,
NULL, // Use the default device.
"transmit", // Description of our stream.
&pa_ss, // Our sample format.
NULL, // Use default channel map
NULL, // Use default buffering attributes.
NULL // Ignore error code.
);
if (!pa_s1 || !pa_s2)
error_exit("pulseaudio", 1);
}
void stop_pa() {
pa_simple_free(pa_s1);
pa_simple_free(pa_s2);
close(wave_recv);
close(wave_transmit);
}
int main(int argc, char *argv[]) {
pjsua_acc_id acc_id;
pj_status_t status;
@ -160,10 +212,29 @@ int main(int argc, char *argv[]) {
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);
int has_sip_user = 1;
char *sip_user = getenv("SIP_LOGIN");
if (!sip_user) {
has_sip_user = 0;
printf("[!] SIP_LOGIN is empty, no registration will be attempted\n");
char sip_user_buf[40];
strcpy(sip_user_buf, "placeholder:placeholder@placeholder");
sip_user = sip_user_buf;
}
if (!sip_user) {
return -1;
}
char *sip_domain = strchr(sip_user,'@');
if (!sip_domain) {
return -1;
}
*sip_domain++ = '\0';
char *sip_pass = strchr(sip_user,':');
if (!sip_pass) {
return -1;
}
*sip_pass++ = '\0';
status = pjsua_create();
if (status != PJ_SUCCESS) error_exit("Error in pjsua_create()", status);
@ -189,7 +260,10 @@ int main(int argc, char *argv[]) {
med_cfg.ec_tail_len = 0;
med_cfg.jb_max = 2000;
// med_cfg.jb_init = 200;
med_cfg.audio_frame_ptime = 5;
/* // med_cfg.jb_init = 200 */
med_cfg.jb_init = 100;
/* med_cfg.audio_frame_ptime = 5; */
med_cfg.audio_frame_ptime = 10;
status = pjsua_init(&cfg, &log_cfg, &med_cfg);
if (status != PJ_SUCCESS) error_exit("Error in pjsua_init()", status);
@ -205,7 +279,7 @@ int main(int argc, char *argv[]) {
for (int i=0; i<count; i++) {
int pri = 0;
if (pj_strcmp2(&codecs[i].codec_id,"PCMU/8000/1") == 0) {
pri = 1;
pri = 0;
} else if (pj_strcmp2(&codecs[i].codec_id,"PCMA/8000/1") == 0) {
pri = 2;
}
@ -213,13 +287,18 @@ int main(int argc, char *argv[]) {
// printf("codec: %s %d\n",pj_strbuf(&codecs[i].codec_id),pri);
}
pjsua_transport_id transport_id;
/* 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 (mode)
cfg.port = 5060;
if (getenv("PJSIP_IPV6"))
status = pjsua_transport_create(PJSIP_TRANSPORT_UDP6, &cfg, &transport_id);
else
status = pjsua_transport_create(PJSIP_TRANSPORT_UDP, &cfg, &transport_id);
if (status != PJ_SUCCESS) error_exit("Error creating transport", status);
}
char buf[384];
@ -245,6 +324,7 @@ int main(int argc, char *argv[]) {
status = pjsua_start();
if (status != PJ_SUCCESS) error_exit("Error starting pjsua", status);
if (has_sip_user)
{
pjsua_acc_config cfg;
pjsua_acc_config_default(&cfg);
@ -263,9 +343,22 @@ int main(int argc, char *argv[]) {
status = pjsua_acc_add(&cfg, PJ_TRUE, &acc_id);
if (status != PJ_SUCCESS) error_exit("Error adding account", status);
}
else
{
printf("acc add local\n");
status == pjsua_acc_add_local(transport_id, 1, &acc_id);
if (status != PJ_SUCCESS) error_exit("Error adding local account", status);
}
pjsua_get_var()->tpdata[transport_id].has_bound_addr = PJ_TRUE;
if (getenv("PJSIP_IPV6"))
pjsua_get_var()->acc[acc_id].cfg.ipv6_media_use = PJSUA_IPV6_ENABLED;
start_pa();
if(mode == DMODEM_DIAL_MODE) {
snprintf(buf,sizeof(buf),"sip:%s@%s",dialstr,sip_domain);
if (has_sip_user)
snprintf(buf,sizeof(buf),"sip:%s@%s",dialstr,sip_domain);
else
snprintf(buf,sizeof(buf),"sip:%s",dialstr);
printf("calling %s\n",buf);
pj_str_t uri = pj_str(buf);
pjsua_call_id callid;
@ -294,5 +387,6 @@ int main(int argc, char *argv[]) {
nanosleep(&ts,NULL);
}
stop_pa();
return 0;
}

View file

@ -71,7 +71,7 @@ const char *modem_dev_name = NULL;
const char *modem_default_dev_name = "/dev/slamr0";
const char *modem_alsa_dev_name = "modem:1";
const char *modem_exec = NULL;
unsigned int need_realtime = 1;
unsigned int need_realtime = 0;
#ifdef MODEM_CONFIG_RING_DETECTOR
unsigned int ring_detector = 0;
#endif
@ -125,7 +125,7 @@ static struct opt {
{'s',"shortbuffer","use short buffer (4 periods length)"},
{'d',"debug","debug level (developers only, for ./sl...)",OPTIONAL,INTEGER,"0"},
{'l',"log","logging mode",OPTIONAL,INTEGER,"5"},
{'e',"exec","path to external application that transmits audio over the socket (required)",MANDATORY,STRING,""},
{'e',"exec","path to external application that transmits audio over the socket (required)",MANDATORY,STRING,"./d-modem"},
{}
};

View file

@ -87,7 +87,7 @@
#define DBG(fmt,args...) dprintf("main: " fmt, ##args)
#define SLMODEMD_USER "nobody"
//#define SLMODEMD_USER "nobody"
#define LOCKED_MEM_MIN_KB (8UL * 1024)
#define LOCKED_MEM_MIN (LOCKED_MEM_MIN_KB * 1024)
@ -1089,7 +1089,7 @@ int modem_main(const char *dev_name)
prop_dp_init();
modem_timer_init();
sprintf(link_name,"/dev/ttySL%d", device.num);
sprintf(link_name,"/tmp/ttySL%d", device.num);
m = modem_create(modem_driver,basename(dev_name));
m->name = basename(dev_name);
@ -1105,8 +1105,8 @@ int modem_main(const char *dev_name)
INFO("modem `%s' created. TTY is `%s'\n",
m->name, m->pty_name);
sprintf(path_name,"/var/lib/slmodem/data.%s",basename(dev_name));
datafile_load_info(path_name,&m->dsp_info);
// sprintf(path_name,"/var/lib/slmodem/data.%s",basename(dev_name));
// datafile_load_info(path_name,&m->dsp_info);
if (need_realtime) {
struct sched_param prm;