📄 winiphone.c
字号:
whin[i].dwBufferLength);
return -1;
}
if(!most_recent_answer->gsmout)
most_recent_answer->gsmout = gsm_create();
service_network(netfd,f); /* service network here for better performance */
/* encode the audio from the buffer into GSM format */
gsm_encode(most_recent_answer->gsmout, (short *) ((char *) whin[i].lpData), fo);
if(iax_send_voice(most_recent_answer->session,
AST_FORMAT_GSM, (char *)fo, sizeof(gsm_frame)) == -1)
puts("Failed to send voice!");
lastouttick = GetTickCount(); /* save time of last output */
/* unprepare (free) the header */
waveInUnprepareHeader(win,&whin[i],sizeof(WAVEHDR));
/* initialize the buffer */
memset(&whin[i],0,sizeof(WAVEHDR));
/* bump the serial number to look for the next time */
nextwhin++;
/* exit the loop so that we can start at lowest buffer again */
break;
}
}
if (i >= NWHIN) break; /* if all found, get out of loop */
}
}
}
return 0;
}
void
do_iax_event(FILE *f) {
int sessions = 0;
struct iax_event *e = 0;
struct peer *peer;
while ( (e = iax_get_event(0))) {
peer = find_peer(e->session);
if(peer) {
handle_event(f, e, peer);
} else {
if(e->etype != IAX_EVENT_CONNECT) {
fprintf(stderr, "Huh? This is an event for a non-existant session?\n");
}
sessions++;
if(sessions >= MAX_SESSIONS) {
fprintf(f, "Missed a call... too many sessions open.\n");
}
if(e->event.connect.callerid && e->event.connect.dnid)
fprintf(f, "Call from '%s' for '%s'", e->event.connect.callerid,
e->event.connect.dnid);
else if(e->event.connect.dnid) {
fprintf(f, "Call from '%s'", e->event.connect.dnid);
} else if(e->event.connect.callerid) {
fprintf(f, "Call from '%s'", e->event.connect.callerid);
} else printf("Call from");
fprintf(f, " (%s)\n", inet_ntoa(iax_get_peer_addr(e->session).sin_addr));
if(most_recent_answer) {
fprintf(f, "Incoming call ignored, there's already a call waiting for answer... \
please accept or reject first\n");
iax_reject(e->session, "Too many calls, we're busy!");
} else {
if ( !(peer = malloc(sizeof(struct peer)))) {
fprintf(f, "Warning: Unable to allocate memory!\n");
return;
}
peer->time = time(0);
peer->session = e->session;
peer->gsmin = 0;
peer->gsmout = 0;
peer->next = peers;
peers = peer;
iax_accept(peer->session);
iax_ring_announce(peer->session);
most_recent_answer = peer;
fprintf(f, "Incoming call!\n");
}
iax_event_free(e);
issue_prompt(f);
}
}
}
void
call(FILE *f, char *num)
{
struct peer *peer;
if(!newcall)
newcall = iax_session_new();
else {
fprintf(f, "Already attempting to call somewhere, please cancel first!\n");
return;
}
if ( !(peer = malloc(sizeof(struct peer)))) {
fprintf(f, "Warning: Unable to allocate memory!\n");
return;
}
peer->time = time(0);
peer->session = newcall;
peer->gsmin = 0;
peer->gsmout = 0;
peer->next = peers;
peers = peer;
most_recent_answer = peer;
iax_call(peer->session, num, 10);
}
void
answer_call(void)
{
if(most_recent_answer)
iax_answer(most_recent_answer->session);
printf("Answering call!\n");
answered_call = 1;
}
void
dump_call(void)
{
if(most_recent_answer)
{
iax_hangup(most_recent_answer->session,"");
free(most_recent_answer);
}
printf("Dumping call!\n");
answered_call = 0;
most_recent_answer = 0;
answered_call = 0;
peers = 0;
newcall = 0;
}
void
reject_call(void)
{
iax_reject(most_recent_answer->session, "Call rejected manually.");
most_recent_answer = 0;
}
void
handle_event(FILE *f, struct iax_event *e, struct peer *p)
{
int len,n;
WHOUT *wh,*wh1;
short fr[160];
static paused_xmit = 0;
switch(e->etype) {
case IAX_EVENT_HANGUP:
iax_hangup(most_recent_answer->session, "Byeee!");
fprintf(f, "Call disconnected by peer\n");
free(most_recent_answer);
most_recent_answer = 0;
answered_call = 0;
peers = 0;
newcall = 0;
break;
case IAX_EVENT_REJECT:
fprintf(f, "Authentication was rejected\n");
break;
case IAX_EVENT_ACCEPT:
fprintf(f, "Waiting for answer... RING RING\n");
issue_prompt(f);
break;
case IAX_EVENT_ANSWER:
answer_call();
break;
case IAX_EVENT_VOICE:
switch(e->event.voice.format) {
case AST_FORMAT_GSM:
if(e->event.voice.datalen % 33) {
fprintf(stderr, "Weird gsm frame, not a multiple of 33.\n");
break;
}
if (!p->gsmin)
p->gsmin = gsm_create();
len = 0;
while(len < e->event.voice.datalen) {
if(gsm_decode(p->gsmin, (char *) e->event.voice.data + len, fr)) {
fprintf(stderr, "Bad GSM data\n");
break;
} else { /* its an audio packet to be output to user */
/* get count of pending items in audio output queue */
n = 0;
if (outqueue)
{ /* determine number of pending out queue items */
for(wh = outqueue; wh != NULL; wh = wh->next)
{
if (!(wh->w.dwFlags & WHDR_DONE)) n++;
}
}
/* if not too many, send to user, otherwise chuck packet */
if (n <= OUT_DEPTH) /* if not to chuck packet */
{
/* malloc the memory for the queue item */
wh = (WHOUT *) malloc(sizeof(WHOUT));
if (wh == (WHOUT *) NULL) /* if error, bail */
{
fprintf(stderr,"Outa memory!!!!\n");
exit(255);
}
/* initialize the queue entry */
memset(wh,0,sizeof(WHOUT));
/* copy the PCM data from the gsm conversion buffer */
memcpy((char *)wh->data,(char *)fr,sizeof(fr));
/* set parameters for data */
wh->w.lpData = (char *) wh->data;
wh->w.dwBufferLength = 320;
/* prepare buffer for output */
if (waveOutPrepareHeader(wout,&wh->w,sizeof(WAVEHDR)))
{
fprintf(stderr,"Cannot prepare header for audio out\n");
exit(255);
}
/* if not currently transmitting, hold off a couple of packets for
smooth sounding output */
if ((!n) && (!paused_xmit))
{
/* pause output (before starting) */
waveOutPause(wout);
/* indicate as such */
paused_xmit = 1;
}
/* queue packet for output on audio device */
if (waveOutWrite(wout,&wh->w,sizeof(WAVEHDR)))
{
fprintf(stderr,"Cannot output to wave output device\n");
exit(255);
}
/* if we are paused, and we have enough packets, start audio */
if ((n > OUT_PAUSE_THRESHOLD) && paused_xmit)
{
/* start the output */
waveOutRestart(wout);
/* indicate as such */
paused_xmit = 0;
}
/* insert it onto tail of outqueue */
if (outqueue == NULL) /* if empty queue */
outqueue = wh; /* point queue to new entry */
else /* otherwise is non-empty queue */
{
wh1 = outqueue;
while(wh1->next) wh1 = wh1->next; /* find last entry in queue */
wh1->next = wh; /* point it to new entry */
}
}
#ifdef PRINTCHUCK
else printf("Chucking packet!!\n");
#endif
}
len += 33;
}
break;
default :
fprintf(f, "Don't know how to handle that format %d\n", e->event.voice.format);
}
break;
case IAX_EVENT_RINGA:
break;
default:
fprintf(f, "Unknown event: %d\n", e->etype);
break;
}
}
void
parse_cmd(FILE *f, int argc, char **argv)
{
_strupr(argv[0]);
if(!strcmp(argv[0], "HELP")) {
if(argc == 1)
dump_array(f, help);
else if(argc == 2) {
if(!strcmp(argv[1], "HELP"))
fprintf(f, "Help <Command>\t-\tDisplays general help or specific help on command if supplied an arguement\n");
else if(!strcmp(argv[1], "QUIT"))
fprintf(f, "Quit\t\t-\tShuts down the miniphone\n");
else fprintf(f, "No help available on %s\n", argv[1]);
} else {
fprintf(f, "Too many arguements for command help.\n");
}
} else if(!strcmp(argv[0], "STATUS")) {
if(argc == 1) {
int c = 0;
struct peer *peerptr = peers;
if(!peerptr)
fprintf(f, "No session matches found.\n");
else while(peerptr) {
fprintf(f, "Listing sessions:\n\n");
fprintf(f, "Session %d\n", ++c);
fprintf(f, "Session existed for %d seconds\n", (int)time(0)-peerptr->time);
if(answered_call)
fprintf(f, "Call answered.\n");
else fprintf(f, "Call ringing.\n");
peerptr = peerptr->next;
}
} else fprintf(f, "Too many arguments for command status.\n");
} else if(!strcmp(argv[0], "ANSWER")) {
if(argc > 1)
fprintf(f, "Too many arguements for command answer\n");
else answer_call();
} else if(!strcmp(argv[0], "REJECT")) {
if(argc > 1)
fprintf(f, "Too many arguements for command reject\n");
else {
fprintf(f, "Rejecting current phone call.\n");
reject_call();
}
} else if(!strcmp(argv[0], "CALL")) {
if(argc > 2)
fprintf(f, "Too many arguements for command call\n");
else {
call(f, argv[1]);
}
} else if(!strcmp(argv[0], "DUMP")) {
if(argc > 1)
fprintf(f, "Too many arguements for command dump\n");
else {
dump_call();
}
} else if(!strcmp(argv[0], "DTMF")) {
if(argc > 2)
{
fprintf(f, "Too many arguements for command dtmf\n");
return;
}
if (argc < 1)
{
fprintf(f, "Too many arguements for command dtmf\n");
return;
}
if(most_recent_answer)
iax_send_dtmf(most_recent_answer->session,*argv[1]);
} else if(!strcmp(argv[0], "QUIT")) {
if(argc > 1)
fprintf(f, "Too many arguements for command quit\n");
else {
fprintf(f, "Good bye!\n");
exit(1);
}
} else fprintf(f, "Unknown command of %s\n", argv[0]);
}
void
issue_prompt(FILE *f)
{
fprintf(f, "TeleClient> ");
fflush(f);
}
void
dump_array(FILE *f, char **array) {
while(*array)
fprintf(f, "%s\n", *array++);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -