📄 dspearly.cpp
字号:
#include <sys/types.h>#include <sys/stat.h>#include <sys/time.h>#include <sys/socket.h>#include <sys/stat.h>#include <sys/types.h>#include <arpa/inet.h>#include <ctype.h>#include <errno.h>#include <netdb.h>#include <netinet/in.h>#include <netinet/ip.h>#include <ctype.h>#include <string.h>#include <stdio.h>#include <fcntl.h>#include <unistd.h>#include <unistd.h>#include <stdio.h>#include <g711.h>#include <qsettings.h>#include <qtimer.h>#include "../dissipate2/sipuri.h"#include "../dissipate2/sipclient.h"#include "../dissipate2/udpmessagesocket.h"#include "rtpdataheader.h"#include "dspearly.h"#include "kstatics.h"DspEarly::DspEarly(void){ useExt=false; portnum=0; curseq=0; qlen=0; numunsend=0; fixedrtplen=160; deb_frag=0L; deb_rtp=0L; ssrc=10; ref_sec=0; ref_usec=0; dsize=4096; useStun=false; destroySocket=false; QSettings settings; sdebug = KStatics::debugLevel; packetbuf = new unsigned char[ dsize ]; setDeviceName("rtp"); min = 0; max = 0; codec = codecULAW; codecNum = 0; sendTimer = new QTimer(); connect( sendTimer, SIGNAL( timeout() ),this, SLOT( writeBuffer1() ) ); }DspEarly::~DspEarly( void ){ sendTimer->stop(); delete sendTimer; if (useExt) stopExt(); delete[] static_cast<unsigned char *>( packetbuf );}void DspEarly::start( const codecType newCodec, int newCodecNum, UDPMessageSocket *s, QString pipAddr,QString qpport ){socket=s;bool okd;int portnum = qpport.toInt(&okd,10);codec = newCodec;codecNum = newCodecNum;destroySocket = true;dsize = 4096; // initial max packet sizesocket->setHostname( pipAddr.latin1() );socket->connect( portnum );socket->SetTOS();devstate = DeviceOpened;if(sdebug >= 3)printf (" DspEarly::start tick\n");sendTimer->start( 100);}void DspEarly::startExt(const codecType newCodec, int newCodecNum,int socket, QString ipAddr, QString qrport, QString lipAddr,QString qsport ){ useExt=true; codec = newCodec; codecNum = newCodecNum; dsize = 4096; // initial max packet size int reuse = 1; unsigned char tos=IPTOS_LOWDELAY; bool okd; int sport = qsport.toInt(&okd,10); int rport = qrport.toInt(&okd,10);printf("****DspEarly::startExt %s,%d,%s,%d\n",ipAddr.latin1(),rport,lipAddr.latin1(),sport);// init the listening side of the socketstruct addrinfo hints;struct addrinfo *res1;struct addrinfo *res2; int errcode; void *ptr; void *ptr2; memset (&hints, 0, sizeof (hints)); hints.ai_family = PF_UNSPEC; errcode = getaddrinfo (lipAddr.latin1() , NULL, &hints, &res1); if (errcode != 0) { perror ("getaddrinfo"); } switch (res1->ai_family) { case AF_INET: printf("1. IPv4\n"); soal = sizeof( struct sockaddr_in ); socketaddress.soa4.sin_family = AF_INET; socketaddress.soa4.sin_port = htons(rport); ptr = &((struct sockaddr_in *) res1->ai_addr)->sin_addr; memcpy (&(socketaddress.soa4.sin_addr) ,ptr,4); memset( &(socketaddress.soa4.sin_zero),0, 8 ); break; default: break; } //------------------------------------------------------------ rsock=socket;// make it reusable if (setsockopt(rsock, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0) { perror("****DspEarly::startExt setsockopt SO_REUSEADDR"); }// speed the thing up if (setsockopt(rsock, SOL_IP, IP_TOS, &tos, 1) < 0) { perror("****DspEarly::startExt setsockopt IP_TOS"); }// as our mother had opened the socket, we must set us nonblocking int flags = fcntl(rsock, F_GETFL, 0); flags |= O_NONBLOCK|O_NDELAY; if (fcntl(rsock, F_SETFL, flags) == -1) { perror("****DspEarly::startExt fcntl: F_SETFL"); exit(1); }//--------------------------------------------------------------// init the sending side of the socket errcode = getaddrinfo (ipAddr.latin1() , NULL, &hints, &res2); if (errcode != 0) { perror ("getaddrinfo"); } switch (res2->ai_family) { case AF_INET: printf("****DspEarly::startExt 2. IPv4\n"); soal = sizeof( struct sockaddr_in ); remoteaddress.soa4.sin_family = AF_INET; remoteaddress.soa4.sin_port = htons(rport); ptr2 = &((struct sockaddr_in *) res2->ai_addr)->sin_addr; memcpy (&(remoteaddress.soa4.sin_addr) ,ptr2,4); memset( &(remoteaddress.soa4.sin_zero),0, 8 ); break; default: break; }devstate = DeviceOpened;if(sdebug >= 3)printf (" DspEarly::start tick\n");sendTimer->start( 100);}void DspEarly::stopExt( void ){ close( rsock );}#define PCMUCODEC 0#define GSMCODEC 3#define PCMACODEC 8typedef struct cb {unsigned char (*func)( int frombuf );} cb_t;void DspEarly::writeBuffer1( void ){ unsigned char *databuf; rtp_hdr_t *h = (rtp_hdr_t *) packetbuf; int tmpsize=160; int length; unsigned char *buf=(unsigned char *) "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa444bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb4444444444444444444444444444444444444444444123456789012345678901234567890123456789"; h->version = 2; h->p = 0; h->x = 0; h->cc = 0; h->m = 0; h->pt = PCMUCODEC; h->seq = htons( ++curseq ); h->ts = htonl(160); h->ssrc = ssrc; databuf = packetbuf + sizeof( rtp_hdr_t ); for( int i = 0; i < tmpsize ; ++i ) { databuf[ i ] = (unsigned char) buf[ i ]; } length = sizeof( rtp_hdr_t ) + tmpsize; if(useExt) { //printf("=====DspEarly::writeBuffer: useExt\n"); if(sendto( rsock,(char *) packetbuf, length, 0, &remoteaddress.soa, soal ) < 0 ) { if(sdebug >= 3) printf("=====DspEarly::writeBuffer: %s\n", strerror(errno)); return; } } else { if( socket->send( (char *) packetbuf, length) < 0 ) { if(sdebug >= 3) printf("=====DspEarly::writeBuffer: %s\n", strerror(errno)); return; } } return ;}bool DspEarly::setPortNum( int newport ){ portnum = newport; return true;}bool DspEarly::readBuffer( int bytes ){return true;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -