ixjasync.c
上传用户:sy_wanhua
上传日期:2013-07-25
资源大小:3048k
文件大小:7k
- #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 <errno.h>
- #include "ixjuser.h"
- void closeall(void);
- void getdata(int);
- int ixj1, ixj2, size1, size2, len, depth, frame, frame1, frame2;
- unsigned long write1, write2;
- char buff1[480], buff2[480];
- void usage(void);
- int main(int argc, char *argv[])
- {
- char pname1[80], pname2[80];
- int codec, oflags1, oflags2;
- if(argc >= 6)
- {
- frame = atoi(argv[5]);
- }
- else
- {
- usage();
- exit(-1);
- }
- sprintf(pname1, "/dev/ixj%s", argv[1]);
- sprintf(pname2, "/dev/ixj%s", argv[2]);
- ixj1 = open(pname1, O_RDWR);
- if(ixj1 < 0)
- {
- printf("%s open failed %d %sn" ,pname1, ixj1, strerror(errno));
- exit(-1);
- }
- ixj2 = open(pname2, O_RDWR);
- if(ixj2 < 0)
- {
- printf("%s open failed %d %sn" ,pname2, ixj2, strerror(errno));
- exit(-1);
- }
- if(argc >= 5)
- {
- depth = atoi(argv[4]);
- ioctl(ixj1, IXJCTL_PLAY_DEPTH, depth);
- ioctl(ixj1, IXJCTL_REC_DEPTH, depth);
- ioctl(ixj2, IXJCTL_PLAY_DEPTH, depth);
- ioctl(ixj2, IXJCTL_REC_DEPTH, depth);
- }
- frame1 = ioctl(ixj1, IXJCTL_FRAME, frame);
- frame2 = ioctl(ixj2, IXJCTL_FRAME, frame);
- 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:
- switch(frame)
- {
- case 30:
- len = 240;
- break;
- case 20:
- len = 160;
- break;
- case 10:
- len = 80;
- break;
- }
- break;
- case ALAW:
- switch(frame)
- {
- case 30:
- len = 240;
- break;
- case 20:
- len = 160;
- break;
- case 10:
- len = 80;
- break;
- }
- break;
- case LINEAR16:
- switch(frame)
- {
- case 30:
- len = 480;
- break;
- case 20:
- len = 320;
- break;
- case 10:
- len = 160;
- break;
- }
- break;
- case LINEAR8:
- switch(frame)
- {
- case 30:
- len = 240;
- break;
- case 20:
- len = 160;
- break;
- case 10:
- len = 80;
- break;
- }
- break;
- case WSS:
- switch(frame)
- {
- case 30:
- len = 240;
- break;
- case 20:
- len = 160;
- break;
- case 10:
- len = 80;
- break;
- }
- 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);
- printf("Frame1 = %d, Frame2 = %d, len = %d, depth = %d, codec = %dn",
- frame1, frame2, len, depth, codec);
- fflush(stdout);
-
- while(ioctl(ixj1, IXJCTL_HOOKSTATE) && ioctl(ixj2, IXJCTL_HOOKSTATE))
- {
- if(write1 == 500)
- {
- ioctl(ixj1, IXJCTL_AEC_START);
- ioctl(ixj2, IXJCTL_AEC_START);
- write1++;
- }
- }
- printf("Write1 = %ld, Write2 = %ldn",write1,write2);
- getchar();
- }
- void usage(void)
- {
- printf("ixjasync card1 card2 codec depth framen");
- printf("tcard1 and card2 - 0 = /dev/ixj0, 1 = /dev/ixj1 etc.n");
- printf("tcodec 1 = g.723.1 6.3n");
- printf("t 2 = g.723.1 5.3n");
- printf("t 3 = TrueSpeech 8.5n");
- printf("t 4 = TrueSpeech 4.8n");
- printf("t 5 = TrueSpeech 4.1n");
- printf("t 6 = g.728n");
- printf("t 7 = g.729n");
- printf("t 8 = uLawn");
- printf("t 9 = aLawn");
- printf("t 10 = Linear 16 Bitn");
- printf("t 11 = Linear 8 Bitn");
- printf("t 12 = Windows Sound Systemn");
- printf("tdepth - DSP buffer depth in framesn");
- printf("tframe - 10 = 10ms, 20 = 20ms, 30 = 30msn");
- }
- void closeall(void)
- {
- ioctl(ixj1, IXJCTL_AEC_STOP);
- ioctl(ixj2, IXJCTL_AEC_STOP);
- ioctl(ixj1, IXJCTL_REC_STOP);
- ioctl(ixj1, IXJCTL_PLAY_STOP);
- ioctl(ixj2, IXJCTL_REC_STOP);
- ioctl(ixj2, IXJCTL_PLAY_STOP);
- close(ixj1);
- close(ixj2);
- }
- void getdata(int x)
- {
- fd_set rfds,wfds,efds;
- struct timeval tv;
- IXJ_EXCEPTION ixje1, ixje2;
- int nmax, size;
- char buf[480], dtmf1, dtmf2;
- 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(ixj2,&wfds))
- {
- if(size1 > 0)
- {
- write(ixj2, buff1, len);
- write2++;
- size1 = 0;
- }
- }
- if(FD_ISSET(ixj2,&rfds))
- {
- size2 = read(ixj2, buff2, len);
- }
- if(FD_ISSET(ixj1,&wfds))
- {
- if(size2 > 0)
- {
- write(ixj1, buff2, len);
- write1++;
- size2 = 0;
- }
- }
- if(FD_ISSET(ixj1,&efds))
- {
- // Deal with DTMF
- ixje1.bytes = ioctl(ixj1, IXJCTL_EXCEPTION);
- if(ixje1.bits.dtmf_ready)
- {
- dtmf1 = ioctl(ixj1, IXJCTL_GET_DTMF_ASCII);
- switch(dtmf1)
- {
- case '1':
- ioctl(ixj1, IXJCTL_AEC_START);
- printf("AEC On card1.n");
- break;
- case '2':
- ioctl(ixj1, IXJCTL_AEC_STOP);
- printf("AEC Off card1.n");
- break;
- default:
- printf("DTMF from card 0 = %cn", dtmf1);
- break;
- }
- fflush(stdout);
- }
- }
- if(FD_ISSET(ixj2,&efds))
- {
- // Deal with DTMF
- ixje2.bytes = ioctl(ixj2, IXJCTL_EXCEPTION);
- if(ixje2.bits.dtmf_ready)
- {
- dtmf2 = ioctl(ixj2, IXJCTL_GET_DTMF_ASCII);
- switch(dtmf2)
- {
- case '1':
- ioctl(ixj2, IXJCTL_AEC_START);
- printf("AEC On card2.n");
- break;
- case '2':
- ioctl(ixj2, IXJCTL_AEC_STOP);
- printf("AEC Off card2.n");
- break;
- default:
- printf("DTMF from card 2 = %cn", dtmf2);
- break;
- }
- fflush(stdout);
- }
- }
- }