ixjasync.c
上传用户:sy_wanhua
上传日期:2013-07-25
资源大小:3048k
文件大小:3k
- #include <sys/types.h>
- #include <sys/time.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <strings.h>
- #include <signal.h>
- #include <sys/ioctl.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <asm/errno.h>
- #include "ixjuser.h"
- void closeall(void);
- void getdata(int);
- int ixj1, ixj2, size1, size2, len;
- unsigned long write1, write2;
- char buff1[480], buff2[480];
- int main(int argc, char *argv[])
- {
- char pname1[80], pname2[80];
- int codec, oflags1, oflags2;
- sprintf(pname1, "/dev/ixj%s", argv[1]);
- sprintf(pname2, "/dev/ixj%s", argv[2]);
- ixj1 = open(pname1, O_RDWR);
- ixj2 = open(pname2, O_RDWR);
- atexit(closeall);
- if(!ioctl(ixj1, IXJCTL_RING))
- {
- exit(0);
- }
- if(!ioctl(ixj2, IXJCTL_RING))
- {
- exit(0);
- }
- codec = atoi(argv[3]);
- switch(codec)
- {
- case G723_63:
- len = 24;
- break;
- case G723_53:
- len = 20;
- break;
- case TS85:
- len = 32;
- break;
- case TS48:
- len = 18;
- break;
- case TS41:
- len = 16;
- break;
- case G728:
- len = 96;
- break;
- case G729:
- len = 36;
- break;
- case ULAW:
- len = 240;
- break;
- case ALAW:
- len = 240;
- break;
- case LINEAR16:
- len = 480;
- break;
- case LINEAR8:
- len = 240;
- break;
- case WSS:
- len = 240;
- break;
- }
- signal(SIGIO, &getdata);
- fcntl(ixj1, F_SETOWN, getpid());
- fcntl(ixj2, F_SETOWN, getpid());
- oflags1 = fcntl(ixj1, F_GETFL);
- fcntl(ixj1, F_SETFL, oflags1 | FASYNC);
- oflags2 = fcntl(ixj2, F_GETFL);
- fcntl(ixj2, F_SETFL, oflags2 | FASYNC);
- write1 = write2 = 0;
- ioctl(ixj1, IXJCTL_PLAY_CODEC, codec);
- ioctl(ixj1, IXJCTL_REC_CODEC, codec);
- ioctl(ixj2, IXJCTL_PLAY_CODEC, codec);
- ioctl(ixj2, IXJCTL_REC_CODEC, codec);
- ioctl(ixj1, IXJCTL_PLAY_START);
- ioctl(ixj2, IXJCTL_REC_START);
- ioctl(ixj2, IXJCTL_PLAY_START);
- ioctl(ixj1, IXJCTL_REC_START);
-
- while(ioctl(ixj1, IXJCTL_HOOKSTATE) && ioctl(ixj2, IXJCTL_HOOKSTATE))
- {
- if(write1 == 500)
- {
- ioctl(ixj1, IXJCTL_AEC_START);
- ioctl(ixj2, IXJCTL_AEC_START);
- printf("AEC On.n");
- write1++;
- }
- }
- printf("Write1 = %ld, Write2 = %ldn",write1,write2);
- getchar();
- }
- void closeall(void)
- {
- ioctl(ixj1, IXJCTL_REC_STOP);
- ioctl(ixj1, IXJCTL_PLAY_STOP);
- ioctl(ixj2, IXJCTL_REC_STOP);
- ioctl(ixj2, IXJCTL_PLAY_STOP);
- ioctl(ixj1, IXJCTL_AEC_STOP);
- ioctl(ixj2, IXJCTL_AEC_STOP);
- close(ixj1);
- close(ixj2);
- }
- void getdata(int x)
- {
- fd_set rfds,wfds,efds;
- struct timeval tv;
- int nmax, size;
- char buf[480];
- signal(SIGIO, &getdata);
- nmax = (ixj1 > ixj2 ? ixj1+1 : ixj2+1);
- FD_ZERO(&rfds);
- FD_ZERO(&wfds);
- FD_ZERO(&efds);
- FD_SET(ixj1, &rfds);
- FD_SET(ixj2, &rfds);
- FD_SET(ixj1, &wfds);
- FD_SET(ixj2, &wfds);
- FD_SET(ixj1, &efds);
- FD_SET(ixj2, &efds);
- tv.tv_sec = 0;
- tv.tv_usec = 300;
- select(nmax,&rfds, &wfds, &efds, &tv);
- if(FD_ISSET(ixj1,&rfds))
- {
- size1 = read(ixj1, buff1, len);
- }
- if(FD_ISSET(ixj1,&wfds))
- {
- if(size2 > 0)
- {
- write(ixj1, buff2, len);
- write1++;
- size2 = 0;
- }
- }
- // if(FD_ISSET(ixj1,&efds))
- {
- // Deal with DTMF
- printf("DTMF from card 0 = %d",ioctl(ixj1, IXJCTL_GET_DTMF));
- }
- if(FD_ISSET(ixj2,&rfds))
- {
- size2 = read(ixj2, buff2, len);
- }
- if(FD_ISSET(ixj2,&wfds))
- {
- if(size1 > 0)
- {
- write(ixj2, buff1, len);
- write2++;
- size1 = 0;
- }
- }
- if(FD_ISSET(ixj2,&efds))
- {
- // Deal with DTMF
- printf("DTMF from card 1 = %d",ioctl(ixj2, IXJCTL_GET_DTMF));
- }
- }