📄 dspoutrtp.cpp
字号:
req.msgHdr.msgType = htons(BindRequestMsg); req.msgHdr.msgLength = htons( sizeof(StunRequestSimple)-sizeof(StunMsgHdr) ); for ( int i=0; i<16; ++i ) { req.msgHdr.id.octet[i]=0; } int id = rand(); req.msgHdr.id.octet[0] = id; req.msgHdr.id.octet[1] = id>>8; req.msgHdr.id.octet[2] = id>>16; req.msgHdr.id.octet[3] = id>>24; socketStun->send( (char *)&req, sizeof( req ) ); } else { return false; } return true;}unsigned int DspOutRtp::receiveStunResponse( UDPMessageSocket *socketStun ){ unsigned int port = 0; char inputbuf[ 8000 ]; int bytesread; struct timeval timeout; fd_set read_fds; int highest_fd; timeout.tv_sec = 5; timeout.tv_usec = 0; FD_ZERO( &read_fds ); FD_SET( socketStun->getFileDescriptor(), &read_fds ); highest_fd = socketStun->getFileDescriptor() + 1;retry: if( select( highest_fd, &read_fds, NULL, NULL, &timeout ) == -1 ) { if( errno == EINTR ) goto retry; perror( "SipClient::doSelect(): select() punted" ); exit( 1 ); } if( FD_ISSET( socketStun->getFileDescriptor(), &read_fds ) ) { printf( QObject::tr("SipClient: Receiving message...") + "\n" ); bytesread = read( socketStun->getFileDescriptor(), inputbuf, 8000 - 1 ); StunMsgHdr* hdr = reinterpret_cast<StunMsgHdr*>( inputbuf ); if( hdr->msgType == BindResponseMsg ) { printf( QObject::tr("SipClient: STUN response for RTP") + "\n" ); char* body = inputbuf + sizeof( StunMsgHdr ); unsigned int size = ntohs( hdr->msgLength ); while( size > 0 ) { StunAtrHdr* attr = reinterpret_cast<StunAtrHdr*>( body ); unsigned int hdrLen = ntohs( attr->length ); if( ntohs( attr->type ) == MappedAddress ) { StunAtrAddress* attribute = reinterpret_cast<StunAtrAddress*>( body ); if ( attribute->address.addrHdr.family == IPv4Family ) { StunAtrAddress4* atrAdd4 = reinterpret_cast<StunAtrAddress4*>( body ); if ( hdrLen == sizeof( StunAtrAddress4 ) - 4 ) { port = ntohs( atrAdd4->addrHdr.port ); } } } body += hdrLen+4; size -= hdrLen+4; } } } return port;}void DspOutRtp::setCodec( const codecType newCodec, int newCodecNum ){ codec = newCodec; codecNum = newCodecNum;}/************************************************************************//* GSM *//************************************************************************/int DspOutRtp::writeGSMBuffer( gsm gsmInst, unsigned char *input_buf, unsigned char *output_buf, unsigned char *tmp_buf, unsigned char *queue, int *qlen, int size ){ int outbufsize = 0; int inq, inx; short *ibuf; short *obuf; short *tbuf; short *qbuf; ibuf = (short *)input_buf; obuf = (short *)output_buf; qbuf = (short *)queue; inq = *qlen; inx = 0; if( inq >= GSM_DEC_SAMPLES ) { tbuf = (short *)tmp_buf; while( inx < GSM_DEC_SAMPLES ) { *tbuf = *qbuf; ++tbuf; ++qbuf; --inq; ++inx; } tbuf = (short *)tmp_buf; gsm_encode( gsmInst, (gsm_signal *)tbuf, (gsm_byte *)obuf); *qlen = inq; outbufsize = GSM_ENC_BYTES; } else if( inq + size >= GSM_DEC_SAMPLES ) { tbuf = (short *)tmp_buf; while( inq > 0 ) { *tbuf = *qbuf; ++tbuf; ++qbuf; --inq; ++inx; } *qlen = 0; while( inx < GSM_DEC_SAMPLES ) { *tbuf = *ibuf; ++tbuf; ++ibuf; --size; ++inx; } tbuf = (short *)tmp_buf; gsm_encode( gsmInst, (gsm_signal *)tbuf, (gsm_byte *)obuf); outbufsize = GSM_ENC_BYTES; } if( size > 0 ) { inq = *qlen; qbuf = (short *)(queue + ( inq * sizeof(short) ) ); while( size > 0 ) { *qbuf = *ibuf; ++inq; --size; ++qbuf; ++ibuf; } *qlen = inq; } return outbufsize;}int DspOutRtp::readGSMBuffer( gsm Gsm_Inst, unsigned char *input_buf, unsigned char *output_buf, int ignore ){ short *outbuf; short *ibuf; outbuf = (short *)output_buf; ibuf = (short *)input_buf; if( !ignore ) { gsm_decode( Gsm_Inst, (gsm_byte *)ibuf, (gsm_signal *)outbuf ); } return true;}/*************************************************************************//* iLBC 20ms *//*************************************************************************/int DspOutRtp::writeILBCBuffer_20( iLBC_Enc_Inst_t *Enc_Inst, unsigned char *input_buf, unsigned char *output_buf, unsigned char *tmp_buf, unsigned char *queue, int *qlen, int size ){ int outbufsize = 0; int inq, inx; short *ibuf; short *obuf; short *tbuf; short *qbuf; ibuf = (short *)input_buf; obuf = (short *)output_buf; qbuf = (short *)queue; inq = *qlen; inx = 0; if( inq >= BLOCKL_20MS ) { tbuf = (short *)tmp_buf; while( inx < BLOCKL_20MS ) { *tbuf = *qbuf; ++tbuf; ++qbuf; --inq; ++inx; } tbuf = (short *)tmp_buf; outbufsize = ILBCencode_20( Enc_Inst, obuf, tbuf); *qlen = inq; } else if( inq + size >= BLOCKL_20MS ) { tbuf = (short *)tmp_buf; while( inq > 0 ) { *tbuf = *qbuf; ++tbuf; ++qbuf; --inq; ++inx; } *qlen = 0; while( inx < BLOCKL_20MS ) { *tbuf = *ibuf; ++tbuf; ++ibuf; --size; ++inx; } tbuf = (short *)tmp_buf; outbufsize = ILBCencode_20( Enc_Inst, obuf, tbuf); } if( size > 0 ) { inq = *qlen; qbuf = (short *)(queue + ( inq * sizeof(short) ) ); while( size > 0 ) { *qbuf = *ibuf; ++inq; --size; ++qbuf; ++ibuf; } *qlen = inq; } return outbufsize;}int DspOutRtp::readILBCBuffer_20( iLBC_Dec_Inst_t *Dec_Inst, unsigned char *input_buf, unsigned char *output_buf, short mode, int ignore){ short *outbuf; short *ibuf; outbuf = (short *)output_buf; ibuf = (short *)input_buf; if( !ignore ) { return ILBCdecode_20( Dec_Inst, outbuf, ibuf, mode ); } return 0;}/*----------------------------------------------------------------* * Encoder interface function *---------------------------------------------------------------*/short ILBCencode_20( iLBC_Enc_Inst_t *iLBCenc_inst, short *encoded_data,short *data ){ float block[BLOCKL_20MS]; int k;/* convert signal to float */ for(k=0;k<BLOCKL_20MS;++k) block[k] = (float)data[k];/* do the actual encoding */ iLBC_encode((unsigned char *)encoded_data,block,iLBCenc_inst); return (short)(NO_OF_BYTES_20MS);}/*----------------------------------------------------------------* * Decoder interface function *---------------------------------------------------------------*/short ILBCdecode_20(iLBC_Dec_Inst_t *iLBCdec_inst, short *decoded_data, short *encoded_data, short mode){ int k; float decblock[BLOCKL_20MS], dtmp;/* check if mode is valid */ if(mode<0 || mode>1){ printf( "\nERROR - Wrong mode - 0, 1 allowed\n" ); exit(3); }/* do actual decoding of block */ iLBC_decode( decblock, (unsigned char *)encoded_data, iLBCdec_inst, mode);/* convert to short */ for(k=0;k<BLOCKL_20MS;++k){ dtmp=decblock[k]; if (dtmp<MIN_SAMPLE)dtmp=MIN_SAMPLE; else if (dtmp>MAX_SAMPLE)dtmp=MAX_SAMPLE; decoded_data[k] = (short) dtmp; } return (short)BLOCKL_20MS;}/*************************************************************************//* iLBC 30ms *//*************************************************************************/int DspOutRtp::writeILBCBuffer_30( iLBC_Enc_Inst_t *Enc_Inst, unsigned char *input_buf, unsigned char *output_buf, unsigned char *tmp_buf, unsigned char *queue, int *qlen, int size ){ int outbufsize = 0; int inq, inx; short *ibuf; short *obuf; short *tbuf; short *qbuf; ibuf = (short *)input_buf; obuf = (short *)output_buf; qbuf = (short *)queue; inq = *qlen; inx = 0; if( inq >= BLOCKL_30MS ) { tbuf = (short *)tmp_buf; while( inx < BLOCKL_30MS ) { *tbuf = *qbuf; ++tbuf; ++qbuf; --inq; ++inx; } tbuf = (short *)tmp_buf; outbufsize = ILBCencode_30( Enc_Inst, obuf, tbuf); *qlen = inq; } else if( inq + size >= BLOCKL_30MS ) { tbuf = (short *)tmp_buf; while( inq > 0 ) { *tbuf = *qbuf; ++tbuf; ++qbuf; --inq; ++inx; } *qlen = 0; while( inx < BLOCKL_30MS ) { *tbuf = *ibuf; ++tbuf; ++ibuf; --size; ++inx; } tbuf = (short *)tmp_buf; outbufsize = ILBCencode_30( Enc_Inst, obuf, tbuf); } if( size > 0 ) { inq = *qlen; qbuf = (short *)(queue + ( inq * sizeof(short) ) ); while( size > 0 ) { *qbuf = *ibuf; ++inq; --size; ++qbuf; ++ibuf; } *qlen = inq; } return outbufsize;}int DspOutRtp::readILBCBuffer_30( iLBC_Dec_Inst_t *Dec_Inst, unsigned char *input_buf, unsigned char *output_buf, short mode, int ignore){ short *outbuf; short *ibuf; outbuf = (short *)output_buf; ibuf = (short *)input_buf; if( !ignore ) { return ILBCdecode_30( Dec_Inst, outbuf, ibuf, mode ); } return 0;}/*----------------------------------------------------------------* * Encoder interface function *---------------------------------------------------------------*/short ILBCencode_30( iLBC_Enc_Inst_t *iLBCenc_inst, short *encoded_data,short *data ){ float block[BLOCKL_30MS]; int k;/* convert signal to float */ for(k=0;k<BLOCKL_30MS;++k) block[k] = (float)data[k];/* do the actual encoding */ iLBC_encode((unsigned char *)encoded_data,block,iLBCenc_inst); return (short)(NO_OF_BYTES_30MS);}/*----------------------------------------------------------------* * Decoder interface function *---------------------------------------------------------------*/short ILBCdecode_30(iLBC_Dec_Inst_t *iLBCdec_inst, short *decoded_data, short *encoded_data, short mode){ int k; float decblock[BLOCKL_30MS], dtmp;/* check if mode is valid */ if(mode<0 || mode>1){ printf( "\nERROR - Wrong mode - 0, 1 allowed\n" ); exit(3); }/* do actual decoding of block */ iLBC_decode( decblock, (unsigned char *)encoded_data, iLBCdec_inst, mode);/* convert to short */ for(k=0;k<BLOCKL_30MS;++k){ dtmp=decblock[k]; if (dtmp<MIN_SAMPLE)dtmp=MIN_SAMPLE; else if (dtmp>MAX_SAMPLE)dtmp=MAX_SAMPLE; decoded_data[k] = (short) dtmp; } return (short)BLOCKL_30MS;}void DspOutRtp::setStunSrv( QString newStunSrv ){ useStun = true; stunSrv = newStunSrv;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -