Rename neteq4 folder to neteq
Keep the old neteq4/audio_decoder_unittests.isolate while waiting for
a hard-coded reference to change.
This CL effectively reverts r6257 "Rename neteq4 folder to neteq".
BUG=2996
TBR=tina.legrand@webrtc.org
Review URL: https://webrtc-codereview.appspot.com/21629004
git-svn-id: http://webrtc.googlecode.com/svn/trunk@6367 4adac7df-926f-26a2-2b94-8c16560cd09d
diff --git a/webrtc/modules/audio_coding/neteq/test/NETEQTEST_DummyRTPpacket.cc b/webrtc/modules/audio_coding/neteq/test/NETEQTEST_DummyRTPpacket.cc
new file mode 100644
index 0000000..e175091
--- /dev/null
+++ b/webrtc/modules/audio_coding/neteq/test/NETEQTEST_DummyRTPpacket.cc
@@ -0,0 +1,204 @@
+/*
+ * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
+ *
+ * Use of this source code is governed by a BSD-style license
+ * that can be found in the LICENSE file in the root of the source
+ * tree. An additional intellectual property rights grant can be found
+ * in the file PATENTS. All contributing project authors may
+ * be found in the AUTHORS file in the root of the source tree.
+ */
+
+#include "NETEQTEST_DummyRTPpacket.h"
+
+#include <assert.h>
+#include <stdio.h>
+#include <string.h>
+
+#ifdef WIN32
+#include <winsock2.h>
+#else
+#include <netinet/in.h> // for htons, htonl, etc
+#endif
+
+int NETEQTEST_DummyRTPpacket::readFromFile(FILE *fp)
+{
+ if (!fp)
+ {
+ return -1;
+ }
+
+ uint16_t length, plen;
+ uint32_t offset;
+ int packetLen;
+
+ bool readNextPacket = true;
+ while (readNextPacket) {
+ readNextPacket = false;
+ if (fread(&length, 2, 1, fp) == 0)
+ {
+ reset();
+ return -2;
+ }
+ length = ntohs(length);
+
+ if (fread(&plen, 2, 1, fp) == 0)
+ {
+ reset();
+ return -1;
+ }
+ packetLen = ntohs(plen);
+
+ if (fread(&offset, 4, 1, fp) == 0)
+ {
+ reset();
+ return -1;
+ }
+ // Store in local variable until we have passed the reset below.
+ uint32_t receiveTime = ntohl(offset);
+
+ // Use length here because a plen of 0 specifies rtcp.
+ length = (uint16_t) (length - _kRDHeaderLen);
+
+ // check buffer size
+ if (_datagram && _memSize < length + 1)
+ {
+ reset();
+ }
+
+ if (!_datagram)
+ {
+ // Add one extra byte, to be able to fake a dummy payload of 1 byte.
+ _datagram = new uint8_t[length + 1];
+ _memSize = length + 1;
+ }
+ memset(_datagram, 0, length + 1);
+
+ if (length == 0)
+ {
+ _datagramLen = 0;
+ _rtpParsed = false;
+ return packetLen;
+ }
+
+ // Read basic header
+ if (fread((unsigned short *) _datagram, 1, _kBasicHeaderLen, fp)
+ != (size_t)_kBasicHeaderLen)
+ {
+ reset();
+ return -1;
+ }
+ _receiveTime = receiveTime;
+ _datagramLen = _kBasicHeaderLen;
+
+ // Parse the basic header
+ webrtc::WebRtcRTPHeader tempRTPinfo;
+ int P, X, CC;
+ parseBasicHeader(&tempRTPinfo, &P, &X, &CC);
+
+ // Check if we have to extend the header
+ if (X != 0 || CC != 0)
+ {
+ int newLen = _kBasicHeaderLen + CC * 4 + X * 4;
+ assert(_memSize >= newLen);
+
+ // Read extension from file
+ size_t readLen = newLen - _kBasicHeaderLen;
+ if (fread(&_datagram[_kBasicHeaderLen], 1, readLen, fp) != readLen)
+ {
+ reset();
+ return -1;
+ }
+ _datagramLen = newLen;
+
+ if (X != 0)
+ {
+ int totHdrLen = calcHeaderLength(X, CC);
+ assert(_memSize >= totHdrLen);
+
+ // Read extension from file
+ size_t readLen = totHdrLen - newLen;
+ if (fread(&_datagram[newLen], 1, readLen, fp) != readLen)
+ {
+ reset();
+ return -1;
+ }
+ _datagramLen = totHdrLen;
+ }
+ }
+ _datagramLen = length;
+
+ if (!_blockList.empty() && _blockList.count(payloadType()) > 0)
+ {
+ readNextPacket = true;
+ }
+ }
+
+ _rtpParsed = false;
+ assert(_memSize > _datagramLen);
+ _payloadLen = 1; // Set the length to 1 byte.
+ return packetLen;
+
+}
+
+int NETEQTEST_DummyRTPpacket::writeToFile(FILE *fp)
+{
+ if (!fp)
+ {
+ return -1;
+ }
+
+ uint16_t length, plen;
+ uint32_t offset;
+
+ // length including RTPplay header
+ length = htons(_datagramLen + _kRDHeaderLen);
+ if (fwrite(&length, 2, 1, fp) != 1)
+ {
+ return -1;
+ }
+
+ // payload length
+ plen = htons(_datagramLen);
+ if (fwrite(&plen, 2, 1, fp) != 1)
+ {
+ return -1;
+ }
+
+ // offset (=receive time)
+ offset = htonl(_receiveTime);
+ if (fwrite(&offset, 4, 1, fp) != 1)
+ {
+ return -1;
+ }
+
+ // Figure out the length of the RTP header.
+ int headerLen;
+ if (_datagramLen == 0)
+ {
+ // No payload at all; we are done writing to file.
+ headerLen = 0;
+ }
+ else
+ {
+ parseHeader();
+ headerLen = _payloadPtr - _datagram;
+ assert(headerLen >= 0);
+ }
+
+ // write RTP header
+ if (fwrite((unsigned short *) _datagram, 1, headerLen, fp) !=
+ static_cast<size_t>(headerLen))
+ {
+ return -1;
+ }
+
+ return (headerLen + _kRDHeaderLen); // total number of bytes written
+
+}
+
+void NETEQTEST_DummyRTPpacket::parseHeader() {
+ NETEQTEST_RTPpacket::parseHeader();
+ // Change _payloadLen to 1 byte. The memory should always be big enough.
+ assert(_memSize > _datagramLen);
+ _payloadLen = 1;
+}
diff --git a/webrtc/modules/audio_coding/neteq/test/NETEQTEST_DummyRTPpacket.h b/webrtc/modules/audio_coding/neteq/test/NETEQTEST_DummyRTPpacket.h
new file mode 100644
index 0000000..9f09c94
--- /dev/null
+++ b/webrtc/modules/audio_coding/neteq/test/NETEQTEST_DummyRTPpacket.h
@@ -0,0 +1,23 @@
+/*
+ * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
+ *
+ * Use of this source code is governed by a BSD-style license
+ * that can be found in the LICENSE file in the root of the source
+ * tree. An additional intellectual property rights grant can be found
+ * in the file PATENTS. All contributing project authors may
+ * be found in the AUTHORS file in the root of the source tree.
+ */
+
+#ifndef NETEQTEST_DUMMYRTPPACKET_H
+#define NETEQTEST_DUMMYRTPPACKET_H
+
+#include "NETEQTEST_RTPpacket.h"
+
+class NETEQTEST_DummyRTPpacket : public NETEQTEST_RTPpacket {
+ public:
+ virtual int readFromFile(FILE* fp) OVERRIDE;
+ virtual int writeToFile(FILE* fp) OVERRIDE;
+ virtual void parseHeader() OVERRIDE;
+};
+
+#endif // NETEQTEST_DUMMYRTPPACKET_H
diff --git a/webrtc/modules/audio_coding/neteq/test/NETEQTEST_RTPpacket.cc b/webrtc/modules/audio_coding/neteq/test/NETEQTEST_RTPpacket.cc
new file mode 100644
index 0000000..22f18ef
--- /dev/null
+++ b/webrtc/modules/audio_coding/neteq/test/NETEQTEST_RTPpacket.cc
@@ -0,0 +1,875 @@
+/*
+ * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
+ *
+ * Use of this source code is governed by a BSD-style license
+ * that can be found in the LICENSE file in the root of the source
+ * tree. An additional intellectual property rights grant can be found
+ * in the file PATENTS. All contributing project authors may
+ * be found in the AUTHORS file in the root of the source tree.
+ */
+
+#include "NETEQTEST_RTPpacket.h"
+
+#include <assert.h>
+#include <stdlib.h> // rand
+#include <string.h>
+
+#ifdef WIN32
+#include <winsock2.h>
+#else
+#include <netinet/in.h> // for htons, htonl, etc
+#endif
+
+const int NETEQTEST_RTPpacket::_kRDHeaderLen = 8;
+const int NETEQTEST_RTPpacket::_kBasicHeaderLen = 12;
+
+NETEQTEST_RTPpacket::NETEQTEST_RTPpacket()
+:
+_datagram(NULL),
+_payloadPtr(NULL),
+_memSize(0),
+_datagramLen(-1),
+_payloadLen(0),
+_rtpParsed(false),
+_receiveTime(0),
+_lost(false)
+{
+ memset(&_rtpInfo, 0, sizeof(_rtpInfo));
+ _blockList.clear();
+}
+
+NETEQTEST_RTPpacket::~NETEQTEST_RTPpacket()
+{
+ if(_datagram)
+ {
+ delete [] _datagram;
+ }
+}
+
+void NETEQTEST_RTPpacket::reset()
+{
+ if(_datagram) {
+ delete [] _datagram;
+ }
+ _datagram = NULL;
+ _memSize = 0;
+ _datagramLen = -1;
+ _payloadLen = 0;
+ _payloadPtr = NULL;
+ _receiveTime = 0;
+ memset(&_rtpInfo, 0, sizeof(_rtpInfo));
+ _rtpParsed = false;
+
+}
+
+int NETEQTEST_RTPpacket::skipFileHeader(FILE *fp)
+{
+ if (!fp) {
+ return -1;
+ }
+
+ const int kFirstLineLength = 40;
+ char firstline[kFirstLineLength];
+ if (fgets(firstline, kFirstLineLength, fp) == NULL) {
+ return -1;
+ }
+ if (strncmp(firstline, "#!rtpplay", 9) == 0) {
+ if (strncmp(firstline, "#!rtpplay1.0", 12) != 0) {
+ return -1;
+ }
+ }
+ else if (strncmp(firstline, "#!RTPencode", 11) == 0) {
+ if (strncmp(firstline, "#!RTPencode1.0", 14) != 0) {
+ return -1;
+ }
+ }
+ else
+ {
+ return -1;
+ }
+
+ const int kRtpDumpHeaderSize = 4 + 4 + 4 + 2 + 2;
+ if (fseek(fp, kRtpDumpHeaderSize, SEEK_CUR) != 0)
+ {
+ return -1;
+ }
+ return 0;
+}
+
+int NETEQTEST_RTPpacket::readFromFile(FILE *fp)
+{
+ if(!fp)
+ {
+ return(-1);
+ }
+
+ uint16_t length, plen;
+ uint32_t offset;
+ int packetLen;
+
+ bool readNextPacket = true;
+ while (readNextPacket) {
+ readNextPacket = false;
+ if (fread(&length,2,1,fp)==0)
+ {
+ reset();
+ return(-2);
+ }
+ length = ntohs(length);
+
+ if (fread(&plen,2,1,fp)==0)
+ {
+ reset();
+ return(-1);
+ }
+ packetLen = ntohs(plen);
+
+ if (fread(&offset,4,1,fp)==0)
+ {
+ reset();
+ return(-1);
+ }
+ // store in local variable until we have passed the reset below
+ uint32_t receiveTime = ntohl(offset);
+
+ // Use length here because a plen of 0 specifies rtcp
+ length = (uint16_t) (length - _kRDHeaderLen);
+
+ // check buffer size
+ if (_datagram && _memSize < length)
+ {
+ reset();
+ }
+
+ if (!_datagram)
+ {
+ _datagram = new uint8_t[length];
+ _memSize = length;
+ }
+
+ if (fread((unsigned short *) _datagram,1,length,fp) != length)
+ {
+ reset();
+ return(-1);
+ }
+
+ _datagramLen = length;
+ _receiveTime = receiveTime;
+
+ if (!_blockList.empty() && _blockList.count(payloadType()) > 0)
+ {
+ readNextPacket = true;
+ }
+ }
+
+ _rtpParsed = false;
+ return(packetLen);
+
+}
+
+
+int NETEQTEST_RTPpacket::readFixedFromFile(FILE *fp, size_t length)
+{
+ if (!fp)
+ {
+ return -1;
+ }
+
+ // check buffer size
+ if (_datagram && _memSize < static_cast<int>(length))
+ {
+ reset();
+ }
+
+ if (!_datagram)
+ {
+ _datagram = new uint8_t[length];
+ _memSize = length;
+ }
+
+ if (fread(_datagram, 1, length, fp) != length)
+ {
+ reset();
+ return -1;
+ }
+
+ _datagramLen = length;
+ _receiveTime = 0;
+
+ if (!_blockList.empty() && _blockList.count(payloadType()) > 0)
+ {
+ // discard this payload
+ return readFromFile(fp);
+ }
+
+ _rtpParsed = false;
+ return length;
+
+}
+
+
+int NETEQTEST_RTPpacket::writeToFile(FILE *fp)
+{
+ if (!fp)
+ {
+ return -1;
+ }
+
+ uint16_t length, plen;
+ uint32_t offset;
+
+ // length including RTPplay header
+ length = htons(_datagramLen + _kRDHeaderLen);
+ if (fwrite(&length, 2, 1, fp) != 1)
+ {
+ return -1;
+ }
+
+ // payload length
+ plen = htons(_datagramLen);
+ if (fwrite(&plen, 2, 1, fp) != 1)
+ {
+ return -1;
+ }
+
+ // offset (=receive time)
+ offset = htonl(_receiveTime);
+ if (fwrite(&offset, 4, 1, fp) != 1)
+ {
+ return -1;
+ }
+
+
+ // write packet data
+ if (fwrite(_datagram, 1, _datagramLen, fp) !=
+ static_cast<size_t>(_datagramLen))
+ {
+ return -1;
+ }
+
+ return _datagramLen + _kRDHeaderLen; // total number of bytes written
+
+}
+
+
+void NETEQTEST_RTPpacket::blockPT(uint8_t pt)
+{
+ _blockList[pt] = true;
+}
+
+
+void NETEQTEST_RTPpacket::parseHeader()
+{
+ if (_rtpParsed)
+ {
+ // nothing to do
+ return;
+ }
+
+ if (_datagramLen < _kBasicHeaderLen)
+ {
+ // corrupt packet?
+ return;
+ }
+
+ _payloadLen = parseRTPheader(&_payloadPtr);
+
+ _rtpParsed = true;
+
+ return;
+
+}
+
+void NETEQTEST_RTPpacket::parseHeader(webrtc::WebRtcRTPHeader* rtp_header) {
+ if (!_rtpParsed) {
+ parseHeader();
+ }
+ if (rtp_header) {
+ rtp_header->header.markerBit = _rtpInfo.header.markerBit;
+ rtp_header->header.payloadType = _rtpInfo.header.payloadType;
+ rtp_header->header.sequenceNumber = _rtpInfo.header.sequenceNumber;
+ rtp_header->header.timestamp = _rtpInfo.header.timestamp;
+ rtp_header->header.ssrc = _rtpInfo.header.ssrc;
+ }
+}
+
+const webrtc::WebRtcRTPHeader* NETEQTEST_RTPpacket::RTPinfo() const
+{
+ if (_rtpParsed)
+ {
+ return &_rtpInfo;
+ }
+ else
+ {
+ return NULL;
+ }
+}
+
+uint8_t * NETEQTEST_RTPpacket::datagram() const
+{
+ if (_datagramLen > 0)
+ {
+ return _datagram;
+ }
+ else
+ {
+ return NULL;
+ }
+}
+
+uint8_t * NETEQTEST_RTPpacket::payload() const
+{
+ if (_payloadLen > 0)
+ {
+ return _payloadPtr;
+ }
+ else
+ {
+ return NULL;
+ }
+}
+
+int16_t NETEQTEST_RTPpacket::payloadLen()
+{
+ parseHeader();
+ return _payloadLen;
+}
+
+int16_t NETEQTEST_RTPpacket::dataLen() const
+{
+ return _datagramLen;
+}
+
+bool NETEQTEST_RTPpacket::isParsed() const
+{
+ return _rtpParsed;
+}
+
+bool NETEQTEST_RTPpacket::isLost() const
+{
+ return _lost;
+}
+
+uint8_t NETEQTEST_RTPpacket::payloadType() const
+{
+ webrtc::WebRtcRTPHeader tempRTPinfo;
+
+ if(_datagram && _datagramLen >= _kBasicHeaderLen)
+ {
+ parseRTPheader(&tempRTPinfo);
+ }
+ else
+ {
+ return 0;
+ }
+
+ return tempRTPinfo.header.payloadType;
+}
+
+uint16_t NETEQTEST_RTPpacket::sequenceNumber() const
+{
+ webrtc::WebRtcRTPHeader tempRTPinfo;
+
+ if(_datagram && _datagramLen >= _kBasicHeaderLen)
+ {
+ parseRTPheader(&tempRTPinfo);
+ }
+ else
+ {
+ return 0;
+ }
+
+ return tempRTPinfo.header.sequenceNumber;
+}
+
+uint32_t NETEQTEST_RTPpacket::timeStamp() const
+{
+ webrtc::WebRtcRTPHeader tempRTPinfo;
+
+ if(_datagram && _datagramLen >= _kBasicHeaderLen)
+ {
+ parseRTPheader(&tempRTPinfo);
+ }
+ else
+ {
+ return 0;
+ }
+
+ return tempRTPinfo.header.timestamp;
+}
+
+uint32_t NETEQTEST_RTPpacket::SSRC() const
+{
+ webrtc::WebRtcRTPHeader tempRTPinfo;
+
+ if(_datagram && _datagramLen >= _kBasicHeaderLen)
+ {
+ parseRTPheader(&tempRTPinfo);
+ }
+ else
+ {
+ return 0;
+ }
+
+ return tempRTPinfo.header.ssrc;
+}
+
+uint8_t NETEQTEST_RTPpacket::markerBit() const
+{
+ webrtc::WebRtcRTPHeader tempRTPinfo;
+
+ if(_datagram && _datagramLen >= _kBasicHeaderLen)
+ {
+ parseRTPheader(&tempRTPinfo);
+ }
+ else
+ {
+ return 0;
+ }
+
+ return tempRTPinfo.header.markerBit;
+}
+
+
+
+int NETEQTEST_RTPpacket::setPayloadType(uint8_t pt)
+{
+
+ if (_datagramLen < 12)
+ {
+ return -1;
+ }
+
+ if (!_rtpParsed)
+ {
+ _rtpInfo.header.payloadType = pt;
+ }
+
+ _datagram[1]=(unsigned char)(pt & 0xFF);
+
+ return 0;
+
+}
+
+int NETEQTEST_RTPpacket::setSequenceNumber(uint16_t sn)
+{
+
+ if (_datagramLen < 12)
+ {
+ return -1;
+ }
+
+ if (!_rtpParsed)
+ {
+ _rtpInfo.header.sequenceNumber = sn;
+ }
+
+ _datagram[2]=(unsigned char)((sn>>8)&0xFF);
+ _datagram[3]=(unsigned char)((sn)&0xFF);
+
+ return 0;
+
+}
+
+int NETEQTEST_RTPpacket::setTimeStamp(uint32_t ts)
+{
+
+ if (_datagramLen < 12)
+ {
+ return -1;
+ }
+
+ if (!_rtpParsed)
+ {
+ _rtpInfo.header.timestamp = ts;
+ }
+
+ _datagram[4]=(unsigned char)((ts>>24)&0xFF);
+ _datagram[5]=(unsigned char)((ts>>16)&0xFF);
+ _datagram[6]=(unsigned char)((ts>>8)&0xFF);
+ _datagram[7]=(unsigned char)(ts & 0xFF);
+
+ return 0;
+
+}
+
+int NETEQTEST_RTPpacket::setSSRC(uint32_t ssrc)
+{
+
+ if (_datagramLen < 12)
+ {
+ return -1;
+ }
+
+ if (!_rtpParsed)
+ {
+ _rtpInfo.header.ssrc = ssrc;
+ }
+
+ _datagram[8]=(unsigned char)((ssrc>>24)&0xFF);
+ _datagram[9]=(unsigned char)((ssrc>>16)&0xFF);
+ _datagram[10]=(unsigned char)((ssrc>>8)&0xFF);
+ _datagram[11]=(unsigned char)(ssrc & 0xFF);
+
+ return 0;
+
+}
+
+int NETEQTEST_RTPpacket::setMarkerBit(uint8_t mb)
+{
+
+ if (_datagramLen < 12)
+ {
+ return -1;
+ }
+
+ if (_rtpParsed)
+ {
+ _rtpInfo.header.markerBit = mb;
+ }
+
+ if (mb)
+ {
+ _datagram[0] |= 0x01;
+ }
+ else
+ {
+ _datagram[0] &= 0xFE;
+ }
+
+ return 0;
+
+}
+
+int NETEQTEST_RTPpacket::setRTPheader(const webrtc::WebRtcRTPHeader* RTPinfo)
+{
+ if (_datagramLen < 12)
+ {
+ // this packet is not ok
+ return -1;
+ }
+
+ makeRTPheader(_datagram,
+ RTPinfo->header.payloadType,
+ RTPinfo->header.sequenceNumber,
+ RTPinfo->header.timestamp,
+ RTPinfo->header.ssrc,
+ RTPinfo->header.markerBit);
+
+ return 0;
+}
+
+
+int NETEQTEST_RTPpacket::splitStereo(NETEQTEST_RTPpacket* slaveRtp,
+ enum stereoModes mode)
+{
+ // if mono, do nothing
+ if (mode == stereoModeMono)
+ {
+ return 0;
+ }
+
+ // check that the RTP header info is parsed
+ parseHeader();
+
+ // start by copying the main rtp packet
+ *slaveRtp = *this;
+
+ if(_payloadLen == 0)
+ {
+ // do no more
+ return 0;
+ }
+
+ if(_payloadLen%2 != 0)
+ {
+ // length must be a factor of 2
+ return -1;
+ }
+
+ switch(mode)
+ {
+ case stereoModeSample1:
+ {
+ // sample based codec with 1-byte samples
+ splitStereoSample(slaveRtp, 1 /* 1 byte/sample */);
+ break;
+ }
+ case stereoModeSample2:
+ {
+ // sample based codec with 2-byte samples
+ splitStereoSample(slaveRtp, 2 /* 2 bytes/sample */);
+ break;
+ }
+ case stereoModeFrame:
+ {
+ // frame based codec
+ splitStereoFrame(slaveRtp);
+ break;
+ }
+ case stereoModeDuplicate:
+ {
+ // frame based codec, send the whole packet to both master and slave
+ splitStereoDouble(slaveRtp);
+ break;
+ }
+ case stereoModeMono:
+ {
+ assert(false);
+ return -1;
+ }
+ }
+
+ return 0;
+}
+
+
+void NETEQTEST_RTPpacket::makeRTPheader(unsigned char* rtp_data, uint8_t payloadType, uint16_t seqNo, uint32_t timestamp, uint32_t ssrc, uint8_t markerBit) const
+{
+ rtp_data[0]=(unsigned char)0x80;
+ if (markerBit)
+ {
+ rtp_data[0] |= 0x01;
+ }
+ else
+ {
+ rtp_data[0] &= 0xFE;
+ }
+ rtp_data[1]=(unsigned char)(payloadType & 0xFF);
+ rtp_data[2]=(unsigned char)((seqNo>>8)&0xFF);
+ rtp_data[3]=(unsigned char)((seqNo)&0xFF);
+ rtp_data[4]=(unsigned char)((timestamp>>24)&0xFF);
+ rtp_data[5]=(unsigned char)((timestamp>>16)&0xFF);
+
+ rtp_data[6]=(unsigned char)((timestamp>>8)&0xFF);
+ rtp_data[7]=(unsigned char)(timestamp & 0xFF);
+
+ rtp_data[8]=(unsigned char)((ssrc>>24)&0xFF);
+ rtp_data[9]=(unsigned char)((ssrc>>16)&0xFF);
+
+ rtp_data[10]=(unsigned char)((ssrc>>8)&0xFF);
+ rtp_data[11]=(unsigned char)(ssrc & 0xFF);
+}
+
+uint16_t
+ NETEQTEST_RTPpacket::parseRTPheader(webrtc::WebRtcRTPHeader* RTPinfo,
+ uint8_t **payloadPtr) const
+{
+ int16_t *rtp_data = (int16_t *) _datagram;
+ int i_P, i_X, i_CC;
+
+ assert(_datagramLen >= 12);
+ parseBasicHeader(RTPinfo, &i_P, &i_X, &i_CC);
+
+ int i_startPosition = calcHeaderLength(i_X, i_CC);
+
+ int i_padlength = calcPadLength(i_P);
+
+ if (payloadPtr)
+ {
+ *payloadPtr = (uint8_t*) &rtp_data[i_startPosition >> 1];
+ }
+
+ return (uint16_t) (_datagramLen - i_startPosition - i_padlength);
+}
+
+
+void NETEQTEST_RTPpacket::parseBasicHeader(webrtc::WebRtcRTPHeader* RTPinfo,
+ int *i_P, int *i_X, int *i_CC) const
+{
+ int16_t *rtp_data = (int16_t *) _datagram;
+ if (_datagramLen < 12)
+ {
+ assert(false);
+ return;
+ }
+
+ *i_P=(((uint16_t)(rtp_data[0] & 0x20))>>5); /* Extract the P bit */
+ *i_X=(((uint16_t)(rtp_data[0] & 0x10))>>4); /* Extract the X bit */
+ *i_CC=(uint16_t)(rtp_data[0] & 0xF); /* Get the CC number */
+ /* Get the marker bit */
+ RTPinfo->header.markerBit = (uint8_t) ((rtp_data[0] >> 15) & 0x01);
+ /* Get the coder type */
+ RTPinfo->header.payloadType = (uint8_t) ((rtp_data[0] >> 8) & 0x7F);
+ /* Get the packet number */
+ RTPinfo->header.sequenceNumber =
+ ((( ((uint16_t)rtp_data[1]) >> 8) & 0xFF) |
+ ( ((uint16_t)(rtp_data[1] & 0xFF)) << 8));
+ /* Get timestamp */
+ RTPinfo->header.timestamp = ((((uint16_t)rtp_data[2]) & 0xFF) << 24) |
+ ((((uint16_t)rtp_data[2]) & 0xFF00) << 8) |
+ ((((uint16_t)rtp_data[3]) >> 8) & 0xFF) |
+ ((((uint16_t)rtp_data[3]) & 0xFF) << 8);
+ /* Get the SSRC */
+ RTPinfo->header.ssrc = ((((uint16_t)rtp_data[4]) & 0xFF) << 24) |
+ ((((uint16_t)rtp_data[4]) & 0xFF00) << 8) |
+ ((((uint16_t)rtp_data[5]) >> 8) & 0xFF) |
+ ((((uint16_t)rtp_data[5]) & 0xFF) << 8);
+}
+
+int NETEQTEST_RTPpacket::calcHeaderLength(int i_X, int i_CC) const
+{
+ int i_extlength = 0;
+ int16_t *rtp_data = (int16_t *) _datagram;
+
+ if (i_X == 1)
+ {
+ // Extension header exists.
+ // Find out how many int32_t it consists of.
+ assert(_datagramLen > 2 * (7 + 2 * i_CC));
+ if (_datagramLen > 2 * (7 + 2 * i_CC))
+ {
+ i_extlength = (((((uint16_t) rtp_data[7 + 2 * i_CC]) >> 8)
+ & 0xFF) | (((uint16_t) (rtp_data[7 + 2 * i_CC] & 0xFF))
+ << 8)) + 1;
+ }
+ }
+
+ return 12 + 4 * i_extlength + 4 * i_CC;
+}
+
+int NETEQTEST_RTPpacket::calcPadLength(int i_P) const
+{
+ int16_t *rtp_data = (int16_t *) _datagram;
+ if (i_P == 1)
+ {
+ /* Padding exists. Find out how many bytes the padding consists of. */
+ if (_datagramLen & 0x1)
+ {
+ /* odd number of bytes => last byte in higher byte */
+ return rtp_data[_datagramLen >> 1] & 0xFF;
+ }
+ else
+ {
+ /* even number of bytes => last byte in lower byte */
+ return ((uint16_t) rtp_data[(_datagramLen >> 1) - 1]) >> 8;
+ }
+ }
+ return 0;
+}
+
+void NETEQTEST_RTPpacket::splitStereoSample(NETEQTEST_RTPpacket* slaveRtp,
+ int stride)
+{
+ if(!_payloadPtr || !slaveRtp || !slaveRtp->_payloadPtr
+ || _payloadLen <= 0 || slaveRtp->_memSize < _memSize)
+ {
+ return;
+ }
+
+ uint8_t *readDataPtr = _payloadPtr;
+ uint8_t *writeDataPtr = _payloadPtr;
+ uint8_t *slaveData = slaveRtp->_payloadPtr;
+
+ while (readDataPtr - _payloadPtr < _payloadLen)
+ {
+ // master data
+ for (int ix = 0; ix < stride; ix++) {
+ *writeDataPtr = *readDataPtr;
+ writeDataPtr++;
+ readDataPtr++;
+ }
+
+ // slave data
+ for (int ix = 0; ix < stride; ix++) {
+ *slaveData = *readDataPtr;
+ slaveData++;
+ readDataPtr++;
+ }
+ }
+
+ _payloadLen /= 2;
+ slaveRtp->_payloadLen = _payloadLen;
+}
+
+
+void NETEQTEST_RTPpacket::splitStereoFrame(NETEQTEST_RTPpacket* slaveRtp)
+{
+ if(!_payloadPtr || !slaveRtp || !slaveRtp->_payloadPtr
+ || _payloadLen <= 0 || slaveRtp->_memSize < _memSize)
+ {
+ return;
+ }
+
+ memmove(slaveRtp->_payloadPtr, _payloadPtr + _payloadLen/2, _payloadLen/2);
+
+ _payloadLen /= 2;
+ slaveRtp->_payloadLen = _payloadLen;
+}
+void NETEQTEST_RTPpacket::splitStereoDouble(NETEQTEST_RTPpacket* slaveRtp)
+{
+ if(!_payloadPtr || !slaveRtp || !slaveRtp->_payloadPtr
+ || _payloadLen <= 0 || slaveRtp->_memSize < _memSize)
+ {
+ return;
+ }
+
+ memcpy(slaveRtp->_payloadPtr, _payloadPtr, _payloadLen);
+ slaveRtp->_payloadLen = _payloadLen;
+}
+
+// Get the RTP header for the RED payload indicated by argument index.
+// The first RED payload is index = 0.
+int NETEQTEST_RTPpacket::extractRED(int index, webrtc::WebRtcRTPHeader& red)
+{
+//
+// 0 1 2 3
+// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+// |1| block PT | timestamp offset | block length |
+// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+// |1| ... |
+// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+// |0| block PT |
+// +-+-+-+-+-+-+-+-+
+//
+
+ parseHeader();
+
+ uint8_t* ptr = payload();
+ uint8_t* payloadEndPtr = ptr + payloadLen();
+ int num_encodings = 0;
+ int total_len = 0;
+
+ while ((ptr < payloadEndPtr) && (*ptr & 0x80))
+ {
+ int len = ((ptr[2] & 0x03) << 8) + ptr[3];
+ if (num_encodings == index)
+ {
+ // Header found.
+ red.header.payloadType = ptr[0] & 0x7F;
+ uint32_t offset = (ptr[1] << 6) + ((ptr[2] & 0xFC) >> 2);
+ red.header.sequenceNumber = sequenceNumber();
+ red.header.timestamp = timeStamp() - offset;
+ red.header.markerBit = markerBit();
+ red.header.ssrc = SSRC();
+ return len;
+ }
+ ++num_encodings;
+ total_len += len;
+ ptr += 4;
+ }
+ if ((ptr < payloadEndPtr) && (num_encodings == index))
+ {
+ // Last header.
+ red.header.payloadType = ptr[0] & 0x7F;
+ red.header.sequenceNumber = sequenceNumber();
+ red.header.timestamp = timeStamp();
+ red.header.markerBit = markerBit();
+ red.header.ssrc = SSRC();
+ ++ptr;
+ return payloadLen() - (ptr - payload()) - total_len;
+ }
+ return -1;
+}
+
+// Randomize the payload, not the RTP header.
+void NETEQTEST_RTPpacket::scramblePayload(void)
+{
+ parseHeader();
+
+ for (int i = 0; i < _payloadLen; ++i)
+ {
+ _payloadPtr[i] = static_cast<uint8_t>(rand());
+ }
+}
diff --git a/webrtc/modules/audio_coding/neteq/test/NETEQTEST_RTPpacket.h b/webrtc/modules/audio_coding/neteq/test/NETEQTEST_RTPpacket.h
new file mode 100644
index 0000000..8a31274
--- /dev/null
+++ b/webrtc/modules/audio_coding/neteq/test/NETEQTEST_RTPpacket.h
@@ -0,0 +1,105 @@
+/*
+ * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
+ *
+ * Use of this source code is governed by a BSD-style license
+ * that can be found in the LICENSE file in the root of the source
+ * tree. An additional intellectual property rights grant can be found
+ * in the file PATENTS. All contributing project authors may
+ * be found in the AUTHORS file in the root of the source tree.
+ */
+
+#ifndef NETEQTEST_RTPPACKET_H
+#define NETEQTEST_RTPPACKET_H
+
+#include <map>
+#include <stdio.h>
+#include "webrtc/typedefs.h"
+#include "webrtc/modules/interface/module_common_types.h"
+
+enum stereoModes {
+ stereoModeMono,
+ stereoModeSample1,
+ stereoModeSample2,
+ stereoModeFrame,
+ stereoModeDuplicate
+};
+
+class NETEQTEST_RTPpacket
+{
+public:
+ NETEQTEST_RTPpacket();
+ bool operator !() const { return (dataLen() < 0); };
+ virtual ~NETEQTEST_RTPpacket();
+ void reset();
+ static int skipFileHeader(FILE *fp);
+ virtual int readFromFile(FILE *fp);
+ int readFixedFromFile(FILE *fp, size_t len);
+ virtual int writeToFile(FILE *fp);
+ void blockPT(uint8_t pt);
+ //int16_t payloadType();
+ virtual void parseHeader();
+ void parseHeader(webrtc::WebRtcRTPHeader* rtp_header);
+ const webrtc::WebRtcRTPHeader* RTPinfo() const;
+ uint8_t * datagram() const;
+ uint8_t * payload() const;
+ int16_t payloadLen();
+ int16_t dataLen() const;
+ bool isParsed() const;
+ bool isLost() const;
+ uint32_t time() const { return _receiveTime; };
+
+ uint8_t payloadType() const;
+ uint16_t sequenceNumber() const;
+ uint32_t timeStamp() const;
+ uint32_t SSRC() const;
+ uint8_t markerBit() const;
+
+ int setPayloadType(uint8_t pt);
+ int setSequenceNumber(uint16_t sn);
+ int setTimeStamp(uint32_t ts);
+ int setSSRC(uint32_t ssrc);
+ int setMarkerBit(uint8_t mb);
+ void setTime(uint32_t receiveTime) { _receiveTime = receiveTime; };
+
+ int setRTPheader(const webrtc::WebRtcRTPHeader* RTPinfo);
+
+ int splitStereo(NETEQTEST_RTPpacket* slaveRtp, enum stereoModes mode);
+
+ int extractRED(int index, webrtc::WebRtcRTPHeader& red);
+
+ void scramblePayload(void);
+
+ uint8_t * _datagram;
+ uint8_t * _payloadPtr;
+ int _memSize;
+ int16_t _datagramLen;
+ int16_t _payloadLen;
+ webrtc::WebRtcRTPHeader _rtpInfo;
+ bool _rtpParsed;
+ uint32_t _receiveTime;
+ bool _lost;
+ std::map<uint8_t, bool> _blockList;
+
+protected:
+ static const int _kRDHeaderLen;
+ static const int _kBasicHeaderLen;
+
+ void parseBasicHeader(webrtc::WebRtcRTPHeader* RTPinfo, int *i_P, int *i_X,
+ int *i_CC) const;
+ int calcHeaderLength(int i_X, int i_CC) const;
+
+private:
+ void makeRTPheader(unsigned char* rtp_data, uint8_t payloadType,
+ uint16_t seqNo, uint32_t timestamp,
+ uint32_t ssrc, uint8_t markerBit) const;
+ uint16_t parseRTPheader(webrtc::WebRtcRTPHeader* RTPinfo,
+ uint8_t **payloadPtr = NULL) const;
+ uint16_t parseRTPheader(uint8_t **payloadPtr = NULL)
+ { return parseRTPheader(&_rtpInfo, payloadPtr);};
+ int calcPadLength(int i_P) const;
+ void splitStereoSample(NETEQTEST_RTPpacket* slaveRtp, int stride);
+ void splitStereoFrame(NETEQTEST_RTPpacket* slaveRtp);
+ void splitStereoDouble(NETEQTEST_RTPpacket* slaveRtp);
+};
+
+#endif //NETEQTEST_RTPPACKET_H
diff --git a/webrtc/modules/audio_coding/neteq/test/PayloadTypes.h b/webrtc/modules/audio_coding/neteq/test/PayloadTypes.h
new file mode 100644
index 0000000..f6cc3da
--- /dev/null
+++ b/webrtc/modules/audio_coding/neteq/test/PayloadTypes.h
@@ -0,0 +1,77 @@
+/*
+ * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
+ *
+ * Use of this source code is governed by a BSD-style license
+ * that can be found in the LICENSE file in the root of the source
+ * tree. An additional intellectual property rights grant can be found
+ * in the file PATENTS. All contributing project authors may
+ * be found in the AUTHORS file in the root of the source tree.
+ */
+
+/* PayloadTypes.h */
+/* Used by NetEqRTPplay application */
+
+/* RTP defined codepoints */
+#define NETEQ_CODEC_PCMU_PT 0
+#define NETEQ_CODEC_GSMFR_PT 3
+#define NETEQ_CODEC_G723_PT 4
+#define NETEQ_CODEC_DVI4_PT 125 // 8 kHz version
+//#define NETEQ_CODEC_DVI4_16_PT 6 // 16 kHz version
+#define NETEQ_CODEC_PCMA_PT 8
+#define NETEQ_CODEC_G722_PT 9
+#define NETEQ_CODEC_CN_PT 13
+//#define NETEQ_CODEC_G728_PT 15
+//#define NETEQ_CODEC_DVI4_11_PT 16 // 11.025 kHz version
+//#define NETEQ_CODEC_DVI4_22_PT 17 // 22.050 kHz version
+#define NETEQ_CODEC_G729_PT 18
+
+/* Dynamic RTP codepoints as defined in VoiceEngine (file VEAPI.cpp) */
+#define NETEQ_CODEC_IPCMWB_PT 97
+#define NETEQ_CODEC_SPEEX8_PT 98
+#define NETEQ_CODEC_SPEEX16_PT 99
+#define NETEQ_CODEC_EG711U_PT 100
+#define NETEQ_CODEC_EG711A_PT 101
+#define NETEQ_CODEC_ILBC_PT 102
+#define NETEQ_CODEC_ISAC_PT 103
+#define NETEQ_CODEC_ISACLC_PT 119
+#define NETEQ_CODEC_ISACSWB_PT 104
+#define NETEQ_CODEC_AVT_PT 106
+#define NETEQ_CODEC_G722_1_16_PT 108
+#define NETEQ_CODEC_G722_1_24_PT 109
+#define NETEQ_CODEC_G722_1_32_PT 110
+#define NETEQ_CODEC_SC3_PT 111
+#define NETEQ_CODEC_AMR_PT 112
+#define NETEQ_CODEC_GSMEFR_PT 113
+//#define NETEQ_CODEC_ILBCRCU_PT 114
+#define NETEQ_CODEC_G726_16_PT 115
+#define NETEQ_CODEC_G726_24_PT 116
+#define NETEQ_CODEC_G726_32_PT 121
+#define NETEQ_CODEC_RED_PT 117
+#define NETEQ_CODEC_G726_40_PT 118
+//#define NETEQ_CODEC_ENERGY_PT 120
+#define NETEQ_CODEC_CN_WB_PT 105
+#define NETEQ_CODEC_CN_SWB_PT 126
+#define NETEQ_CODEC_G729_1_PT 107
+#define NETEQ_CODEC_G729D_PT 123
+#define NETEQ_CODEC_MELPE_PT 124
+#define NETEQ_CODEC_CELT32_PT 114
+
+/* Extra dynamic codepoints */
+#define NETEQ_CODEC_AMRWB_PT 120
+#define NETEQ_CODEC_PCM16B_PT 93
+#define NETEQ_CODEC_PCM16B_WB_PT 94
+#define NETEQ_CODEC_PCM16B_SWB32KHZ_PT 95
+#define NETEQ_CODEC_PCM16B_SWB48KHZ_PT 96
+#define NETEQ_CODEC_MPEG4AAC_PT 122
+
+
+/* Not default in VoiceEngine */
+#define NETEQ_CODEC_G722_1C_24_PT 84
+#define NETEQ_CODEC_G722_1C_32_PT 85
+#define NETEQ_CODEC_G722_1C_48_PT 86
+
+#define NETEQ_CODEC_SILK_8_PT 80
+#define NETEQ_CODEC_SILK_12_PT 81
+#define NETEQ_CODEC_SILK_16_PT 82
+#define NETEQ_CODEC_SILK_24_PT 83
+
diff --git a/webrtc/modules/audio_coding/neteq/test/RTPcat.cc b/webrtc/modules/audio_coding/neteq/test/RTPcat.cc
new file mode 100644
index 0000000..f06b574
--- /dev/null
+++ b/webrtc/modules/audio_coding/neteq/test/RTPcat.cc
@@ -0,0 +1,75 @@
+/*
+ * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
+ *
+ * Use of this source code is governed by a BSD-style license
+ * that can be found in the LICENSE file in the root of the source
+ * tree. An additional intellectual property rights grant can be found
+ * in the file PATENTS. All contributing project authors may
+ * be found in the AUTHORS file in the root of the source tree.
+ */
+
+#include <stdio.h>
+
+#include <algorithm>
+#include <vector>
+
+#include "gtest/gtest.h"
+#include "webrtc/modules/audio_coding/neteq/test/NETEQTEST_RTPpacket.h"
+
+#define FIRSTLINELEN 40
+
+int main(int argc, char* argv[]) {
+ if (argc < 3) {
+ printf("Usage: RTPcat in1.rtp int2.rtp [...] out.rtp\n");
+ exit(1);
+ }
+
+ FILE* in_file = fopen(argv[1], "rb");
+ if (!in_file) {
+ printf("Cannot open input file %s\n", argv[1]);
+ return -1;
+ }
+
+ FILE* out_file = fopen(argv[argc - 1], "wb"); // Last parameter is out file.
+ if (!out_file) {
+ printf("Cannot open output file %s\n", argv[argc - 1]);
+ return -1;
+ }
+ printf("Output RTP file: %s\n\n", argv[argc - 1]);
+
+ // Read file header and write directly to output file.
+ char firstline[FIRSTLINELEN];
+ const unsigned int kRtpDumpHeaderSize = 4 + 4 + 4 + 2 + 2;
+ EXPECT_TRUE(fgets(firstline, FIRSTLINELEN, in_file) != NULL);
+ EXPECT_GT(fputs(firstline, out_file), 0);
+ EXPECT_EQ(kRtpDumpHeaderSize, fread(firstline, 1, kRtpDumpHeaderSize,
+ in_file));
+ EXPECT_EQ(kRtpDumpHeaderSize, fwrite(firstline, 1, kRtpDumpHeaderSize,
+ out_file));
+
+ // Close input file and re-open it later (easier to write the loop below).
+ fclose(in_file);
+
+ for (int i = 1; i < argc - 1; i++) {
+ in_file = fopen(argv[i], "rb");
+ if (!in_file) {
+ printf("Cannot open input file %s\n", argv[i]);
+ return -1;
+ }
+ printf("Input RTP file: %s\n", argv[i]);
+
+ NETEQTEST_RTPpacket::skipFileHeader(in_file);
+ NETEQTEST_RTPpacket packet;
+ int pack_len = packet.readFromFile(in_file);
+ if (pack_len < 0) {
+ exit(1);
+ }
+ while (pack_len >= 0) {
+ packet.writeToFile(out_file);
+ pack_len = packet.readFromFile(in_file);
+ }
+ fclose(in_file);
+ }
+ fclose(out_file);
+ return 0;
+}
diff --git a/webrtc/modules/audio_coding/neteq/test/RTPchange.cc b/webrtc/modules/audio_coding/neteq/test/RTPchange.cc
new file mode 100644
index 0000000..54395c0
--- /dev/null
+++ b/webrtc/modules/audio_coding/neteq/test/RTPchange.cc
@@ -0,0 +1,133 @@
+/*
+ * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
+ *
+ * Use of this source code is governed by a BSD-style license
+ * that can be found in the LICENSE file in the root of the source
+ * tree. An additional intellectual property rights grant can be found
+ * in the file PATENTS. All contributing project authors may
+ * be found in the AUTHORS file in the root of the source tree.
+ */
+
+#include <stdio.h>
+
+#include <algorithm>
+#include <vector>
+
+#include "webrtc/modules/audio_coding/neteq/test/NETEQTEST_DummyRTPpacket.h"
+#include "webrtc/modules/audio_coding/neteq/test/NETEQTEST_RTPpacket.h"
+
+#define FIRSTLINELEN 40
+//#define WEBRTC_DUMMY_RTP
+
+static bool pktCmp(NETEQTEST_RTPpacket *a, NETEQTEST_RTPpacket *b) {
+ return (a->time() < b->time());
+}
+
+int main(int argc, char* argv[]) {
+ FILE* in_file = fopen(argv[1], "rb");
+ if (!in_file) {
+ printf("Cannot open input file %s\n", argv[1]);
+ return -1;
+ }
+ printf("Input RTP file: %s\n", argv[1]);
+
+ FILE* stat_file = fopen(argv[2], "rt");
+ if (!stat_file) {
+ printf("Cannot open timing file %s\n", argv[2]);
+ return -1;
+ }
+ printf("Timing file: %s\n", argv[2]);
+
+ FILE* out_file = fopen(argv[3], "wb");
+ if (!out_file) {
+ printf("Cannot open output file %s\n", argv[3]);
+ return -1;
+ }
+ printf("Output RTP file: %s\n\n", argv[3]);
+
+ // Read all statistics and insert into map.
+ // Read first line.
+ char temp_str[100];
+ if (fgets(temp_str, 100, stat_file) == NULL) {
+ printf("Failed to read timing file %s\n", argv[2]);
+ return -1;
+ }
+ // Define map.
+ std::map<std::pair<uint16_t, uint32_t>, uint32_t> packet_stats;
+ uint16_t seq_no;
+ uint32_t ts;
+ uint32_t send_time;
+
+ while (fscanf(stat_file,
+ "%hu %u %u %*i %*i\n", &seq_no, &ts, &send_time) == 3) {
+ std::pair<uint16_t, uint32_t>
+ temp_pair = std::pair<uint16_t, uint32_t>(seq_no, ts);
+
+ packet_stats[temp_pair] = send_time;
+ }
+
+ fclose(stat_file);
+
+ // Read file header and write directly to output file.
+ char first_line[FIRSTLINELEN];
+ if (fgets(first_line, FIRSTLINELEN, in_file) == NULL) {
+ printf("Failed to read first line of input file %s\n", argv[1]);
+ return -1;
+ }
+ fputs(first_line, out_file);
+ // start_sec + start_usec + source + port + padding
+ const unsigned int kRtpDumpHeaderSize = 4 + 4 + 4 + 2 + 2;
+ if (fread(first_line, 1, kRtpDumpHeaderSize, in_file)
+ != kRtpDumpHeaderSize) {
+ printf("Failed to read RTP dump header from input file %s\n", argv[1]);
+ return -1;
+ }
+ if (fwrite(first_line, 1, kRtpDumpHeaderSize, out_file)
+ != kRtpDumpHeaderSize) {
+ printf("Failed to write RTP dump header to output file %s\n", argv[3]);
+ return -1;
+ }
+
+ std::vector<NETEQTEST_RTPpacket *> packet_vec;
+
+ while (1) {
+ // Insert in vector.
+#ifdef WEBRTC_DUMMY_RTP
+ NETEQTEST_RTPpacket *new_packet = new NETEQTEST_DummyRTPpacket();
+#else
+ NETEQTEST_RTPpacket *new_packet = new NETEQTEST_RTPpacket();
+#endif
+ if (new_packet->readFromFile(in_file) < 0) {
+ // End of file.
+ break;
+ }
+
+ // Look for new send time in statistics vector.
+ std::pair<uint16_t, uint32_t> temp_pair =
+ std::pair<uint16_t, uint32_t>(new_packet->sequenceNumber(),
+ new_packet->timeStamp());
+
+ uint32_t new_send_time = packet_stats[temp_pair];
+ new_packet->setTime(new_send_time); // Set new send time.
+ packet_vec.push_back(new_packet); // Insert in vector.
+ }
+
+ // Sort the vector according to send times.
+ std::sort(packet_vec.begin(), packet_vec.end(), pktCmp);
+
+ std::vector<NETEQTEST_RTPpacket *>::iterator it;
+ for (it = packet_vec.begin(); it != packet_vec.end(); it++) {
+ // Write to out file.
+ if ((*it)->writeToFile(out_file) < 0) {
+ printf("Error writing to file\n");
+ return -1;
+ }
+ // Delete packet.
+ delete *it;
+ }
+
+ fclose(in_file);
+ fclose(out_file);
+
+ return 0;
+}
diff --git a/webrtc/modules/audio_coding/neteq/test/RTPencode.cc b/webrtc/modules/audio_coding/neteq/test/RTPencode.cc
new file mode 100644
index 0000000..93b366b
--- /dev/null
+++ b/webrtc/modules/audio_coding/neteq/test/RTPencode.cc
@@ -0,0 +1,1826 @@
+/*
+ * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
+ *
+ * Use of this source code is governed by a BSD-style license
+ * that can be found in the LICENSE file in the root of the source
+ * tree. An additional intellectual property rights grant can be found
+ * in the file PATENTS. All contributing project authors may
+ * be found in the AUTHORS file in the root of the source tree.
+ */
+
+//TODO(hlundin): Reformat file to meet style guide.
+
+/* header includes */
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#ifdef WIN32
+#include <winsock2.h>
+#endif
+#ifdef WEBRTC_LINUX
+#include <netinet/in.h>
+#endif
+
+#include <assert.h>
+
+#include "webrtc/typedefs.h"
+// needed for NetEqDecoder
+#include "webrtc/modules/audio_coding/neteq/interface/audio_decoder.h"
+#include "webrtc/modules/audio_coding/neteq/interface/neteq.h"
+
+/************************/
+/* Define payload types */
+/************************/
+
+#include "PayloadTypes.h"
+
+
+
+/*********************/
+/* Misc. definitions */
+/*********************/
+
+#define STOPSENDTIME 3000
+#define RESTARTSENDTIME 0 //162500
+#define FIRSTLINELEN 40
+#define CHECK_NOT_NULL(a) if((a)==0){printf("\n %s \n line: %d \nerror at %s\n",__FILE__,__LINE__,#a );return(-1);}
+
+//#define MULTIPLE_SAME_TIMESTAMP
+#define REPEAT_PACKET_DISTANCE 17
+#define REPEAT_PACKET_COUNT 1 // number of extra packets to send
+
+//#define INSERT_OLD_PACKETS
+#define OLD_PACKET 5 // how many seconds too old should the packet be?
+
+//#define TIMESTAMP_WRAPAROUND
+
+//#define RANDOM_DATA
+//#define RANDOM_PAYLOAD_DATA
+#define RANDOM_SEED 10
+
+//#define INSERT_DTMF_PACKETS
+//#define NO_DTMF_OVERDUB
+#define DTMF_PACKET_INTERVAL 2000
+#define DTMF_DURATION 500
+
+#define STEREO_MODE_FRAME 0
+#define STEREO_MODE_SAMPLE_1 1 //1 octet per sample
+#define STEREO_MODE_SAMPLE_2 2 //2 octets per sample
+
+/*************************/
+/* Function declarations */
+/*************************/
+
+void NetEQTest_GetCodec_and_PT(char * name, webrtc::NetEqDecoder *codec, int *PT, int frameLen, int *fs, int *bitrate, int *useRed);
+int NetEQTest_init_coders(webrtc::NetEqDecoder coder, int enc_frameSize, int bitrate, int sampfreq , int vad, int numChannels);
+void defineCodecs(webrtc::NetEqDecoder *usedCodec, int *noOfCodecs );
+int NetEQTest_free_coders(webrtc::NetEqDecoder coder, int numChannels);
+int NetEQTest_encode(int coder, int16_t *indata, int frameLen, unsigned char * encoded,int sampleRate , int * vad, int useVAD, int bitrate, int numChannels);
+void makeRTPheader(unsigned char* rtp_data, int payloadType, int seqNo, uint32_t timestamp, uint32_t ssrc);
+int makeRedundantHeader(unsigned char* rtp_data, int *payloadType, int numPayloads, uint32_t *timestamp, uint16_t *blockLen,
+ int seqNo, uint32_t ssrc);
+int makeDTMFpayload(unsigned char* payload_data, int Event, int End, int Volume, int Duration);
+void stereoDeInterleave(int16_t* audioSamples, int numSamples);
+void stereoInterleave(unsigned char* data, int dataLen, int stride);
+
+/*********************/
+/* Codec definitions */
+/*********************/
+
+#include "webrtc_vad.h"
+
+#if ((defined CODEC_PCM16B)||(defined NETEQ_ARBITRARY_CODEC))
+ #include "pcm16b.h"
+#endif
+#ifdef CODEC_G711
+ #include "g711_interface.h"
+#endif
+#ifdef CODEC_G729
+ #include "G729Interface.h"
+#endif
+#ifdef CODEC_G729_1
+ #include "G729_1Interface.h"
+#endif
+#ifdef CODEC_AMR
+ #include "AMRInterface.h"
+ #include "AMRCreation.h"
+#endif
+#ifdef CODEC_AMRWB
+ #include "AMRWBInterface.h"
+ #include "AMRWBCreation.h"
+#endif
+#ifdef CODEC_ILBC
+ #include "ilbc.h"
+#endif
+#if (defined CODEC_ISAC || defined CODEC_ISAC_SWB)
+ #include "isac.h"
+#endif
+#ifdef NETEQ_ISACFIX_CODEC
+ #include "isacfix.h"
+ #ifdef CODEC_ISAC
+ #error Cannot have both ISAC and ISACfix defined. Please de-select one in the beginning of RTPencode.cpp
+ #endif
+#endif
+#ifdef CODEC_G722
+ #include "g722_interface.h"
+#endif
+#ifdef CODEC_G722_1_24
+ #include "G722_1Interface.h"
+#endif
+#ifdef CODEC_G722_1_32
+ #include "G722_1Interface.h"
+#endif
+#ifdef CODEC_G722_1_16
+ #include "G722_1Interface.h"
+#endif
+#ifdef CODEC_G722_1C_24
+ #include "G722_1Interface.h"
+#endif
+#ifdef CODEC_G722_1C_32
+ #include "G722_1Interface.h"
+#endif
+#ifdef CODEC_G722_1C_48
+ #include "G722_1Interface.h"
+#endif
+#ifdef CODEC_G726
+ #include "G726Creation.h"
+ #include "G726Interface.h"
+#endif
+#ifdef CODEC_GSMFR
+ #include "GSMFRInterface.h"
+ #include "GSMFRCreation.h"
+#endif
+#if (defined(CODEC_CNGCODEC8) || defined(CODEC_CNGCODEC16) || \
+ defined(CODEC_CNGCODEC32) || defined(CODEC_CNGCODEC48))
+ #include "webrtc_cng.h"
+#endif
+#if ((defined CODEC_SPEEX_8)||(defined CODEC_SPEEX_16))
+ #include "SpeexInterface.h"
+#endif
+#ifdef CODEC_CELT_32
+#include "celt_interface.h"
+#endif
+
+
+/***********************************/
+/* Global codec instance variables */
+/***********************************/
+
+WebRtcVadInst *VAD_inst[2];
+
+#ifdef CODEC_G722
+ G722EncInst *g722EncState[2];
+#endif
+
+#ifdef CODEC_G722_1_24
+ G722_1_24_encinst_t *G722_1_24enc_inst[2];
+#endif
+#ifdef CODEC_G722_1_32
+ G722_1_32_encinst_t *G722_1_32enc_inst[2];
+#endif
+#ifdef CODEC_G722_1_16
+ G722_1_16_encinst_t *G722_1_16enc_inst[2];
+#endif
+#ifdef CODEC_G722_1C_24
+ G722_1C_24_encinst_t *G722_1C_24enc_inst[2];
+#endif
+#ifdef CODEC_G722_1C_32
+ G722_1C_32_encinst_t *G722_1C_32enc_inst[2];
+#endif
+#ifdef CODEC_G722_1C_48
+ G722_1C_48_encinst_t *G722_1C_48enc_inst[2];
+#endif
+#ifdef CODEC_G726
+ G726_encinst_t *G726enc_inst[2];
+#endif
+#ifdef CODEC_G729
+ G729_encinst_t *G729enc_inst[2];
+#endif
+#ifdef CODEC_G729_1
+ G729_1_inst_t *G729_1_inst[2];
+#endif
+#ifdef CODEC_AMR
+ AMR_encinst_t *AMRenc_inst[2];
+ int16_t AMR_bitrate;
+#endif
+#ifdef CODEC_AMRWB
+ AMRWB_encinst_t *AMRWBenc_inst[2];
+ int16_t AMRWB_bitrate;
+#endif
+#ifdef CODEC_ILBC
+ iLBC_encinst_t *iLBCenc_inst[2];
+#endif
+#ifdef CODEC_ISAC
+ ISACStruct *ISAC_inst[2];
+#endif
+#ifdef NETEQ_ISACFIX_CODEC
+ ISACFIX_MainStruct *ISAC_inst[2];
+#endif
+#ifdef CODEC_ISAC_SWB
+ ISACStruct *ISACSWB_inst[2];
+#endif
+#ifdef CODEC_GSMFR
+ GSMFR_encinst_t *GSMFRenc_inst[2];
+#endif
+#if (defined(CODEC_CNGCODEC8) || defined(CODEC_CNGCODEC16) || \
+ defined(CODEC_CNGCODEC32) || defined(CODEC_CNGCODEC48))
+ CNG_enc_inst *CNGenc_inst[2];
+#endif
+#ifdef CODEC_SPEEX_8
+ SPEEX_encinst_t *SPEEX8enc_inst[2];
+#endif
+#ifdef CODEC_SPEEX_16
+ SPEEX_encinst_t *SPEEX16enc_inst[2];
+#endif
+#ifdef CODEC_CELT_32
+ CELT_encinst_t *CELT32enc_inst[2];
+#endif
+#ifdef CODEC_G711
+ void *G711state[2]={NULL, NULL};
+#endif
+
+
+int main(int argc, char* argv[])
+{
+ int packet_size, fs;
+ webrtc::NetEqDecoder usedCodec;
+ int payloadType;
+ int bitrate = 0;
+ int useVAD, vad;
+ int useRed=0;
+ int len, enc_len;
+ int16_t org_data[4000];
+ unsigned char rtp_data[8000];
+ int16_t seqNo=0xFFF;
+ uint32_t ssrc=1235412312;
+ uint32_t timestamp=0xAC1245;
+ uint16_t length, plen;
+ uint32_t offset;
+ double sendtime = 0;
+ int red_PT[2] = {0};
+ uint32_t red_TS[2] = {0};
+ uint16_t red_len[2] = {0};
+ int RTPheaderLen=12;
+ unsigned char red_data[8000];
+#ifdef INSERT_OLD_PACKETS
+ uint16_t old_length, old_plen;
+ int old_enc_len;
+ int first_old_packet=1;
+ unsigned char old_rtp_data[8000];
+ int packet_age=0;
+#endif
+#ifdef INSERT_DTMF_PACKETS
+ int NTone = 1;
+ int DTMFfirst = 1;
+ uint32_t DTMFtimestamp;
+ bool dtmfSent = false;
+#endif
+ bool usingStereo = false;
+ int stereoMode = 0;
+ int numChannels = 1;
+
+ /* check number of parameters */
+ if ((argc != 6) && (argc != 7)) {
+ /* print help text and exit */
+ printf("Application to encode speech into an RTP stream.\n");
+ printf("The program reads a PCM file and encodes is using the specified codec.\n");
+ printf("The coded speech is packetized in RTP packest and written to the output file.\n");
+ printf("The format of the RTP stream file is simlilar to that of rtpplay,\n");
+ printf("but with the receive time euqal to 0 for all packets.\n");
+ printf("Usage:\n\n");
+ printf("%s PCMfile RTPfile frameLen codec useVAD bitrate\n", argv[0]);
+ printf("where:\n");
+
+ printf("PCMfile : PCM speech input file\n\n");
+
+ printf("RTPfile : RTP stream output file\n\n");
+
+ printf("frameLen : 80...960... Number of samples per packet (limit depends on codec)\n\n");
+
+ printf("codecName\n");
+#ifdef CODEC_PCM16B
+ printf(" : pcm16b 16 bit PCM (8kHz)\n");
+#endif
+#ifdef CODEC_PCM16B_WB
+ printf(" : pcm16b_wb 16 bit PCM (16kHz)\n");
+#endif
+#ifdef CODEC_PCM16B_32KHZ
+ printf(" : pcm16b_swb32 16 bit PCM (32kHz)\n");
+#endif
+#ifdef CODEC_PCM16B_48KHZ
+ printf(" : pcm16b_swb48 16 bit PCM (48kHz)\n");
+#endif
+#ifdef CODEC_G711
+ printf(" : pcma g711 A-law (8kHz)\n");
+#endif
+#ifdef CODEC_G711
+ printf(" : pcmu g711 u-law (8kHz)\n");
+#endif
+#ifdef CODEC_G729
+ printf(" : g729 G729 (8kHz and 8kbps) CELP (One-Three frame(s)/packet)\n");
+#endif
+#ifdef CODEC_G729_1
+ printf(" : g729.1 G729.1 (16kHz) variable rate (8--32 kbps)\n");
+#endif
+#ifdef CODEC_G722_1_16
+ printf(" : g722.1_16 G722.1 coder (16kHz) (g722.1 with 16kbps)\n");
+#endif
+#ifdef CODEC_G722_1_24
+ printf(" : g722.1_24 G722.1 coder (16kHz) (the 24kbps version)\n");
+#endif
+#ifdef CODEC_G722_1_32
+ printf(" : g722.1_32 G722.1 coder (16kHz) (the 32kbps version)\n");
+#endif
+#ifdef CODEC_G722_1C_24
+ printf(" : g722.1C_24 G722.1 C coder (32kHz) (the 24kbps version)\n");
+#endif
+#ifdef CODEC_G722_1C_32
+ printf(" : g722.1C_32 G722.1 C coder (32kHz) (the 32kbps version)\n");
+#endif
+#ifdef CODEC_G722_1C_48
+ printf(" : g722.1C_48 G722.1 C coder (32kHz) (the 48kbps)\n");
+#endif
+
+#ifdef CODEC_G726
+ printf(" : g726_16 G726 coder (8kHz) 16kbps\n");
+ printf(" : g726_24 G726 coder (8kHz) 24kbps\n");
+ printf(" : g726_32 G726 coder (8kHz) 32kbps\n");
+ printf(" : g726_40 G726 coder (8kHz) 40kbps\n");
+#endif
+#ifdef CODEC_AMR
+ printf(" : AMRXk Adaptive Multi Rate CELP codec (8kHz)\n");
+ printf(" X = 4.75, 5.15, 5.9, 6.7, 7.4, 7.95, 10.2 or 12.2\n");
+#endif
+#ifdef CODEC_AMRWB
+ printf(" : AMRwbXk Adaptive Multi Rate Wideband CELP codec (16kHz)\n");
+ printf(" X = 7, 9, 12, 14, 16, 18, 20, 23 or 24\n");
+#endif
+#ifdef CODEC_ILBC
+ printf(" : ilbc iLBC codec (8kHz and 13.8kbps)\n");
+#endif
+#ifdef CODEC_ISAC
+ printf(" : isac iSAC (16kHz and 32.0 kbps). To set rate specify a rate parameter as last parameter\n");
+#endif
+#ifdef CODEC_ISAC_SWB
+ printf(" : isacswb iSAC SWB (32kHz and 32.0-52.0 kbps). To set rate specify a rate parameter as last parameter\n");
+#endif
+#ifdef CODEC_GSMFR
+ printf(" : gsmfr GSM FR codec (8kHz and 13kbps)\n");
+#endif
+#ifdef CODEC_G722
+ printf(" : g722 g722 coder (16kHz) (the 64kbps version)\n");
+#endif
+#ifdef CODEC_SPEEX_8
+ printf(" : speex8 speex coder (8 kHz)\n");
+#endif
+#ifdef CODEC_SPEEX_16
+ printf(" : speex16 speex coder (16 kHz)\n");
+#endif
+#ifdef CODEC_CELT_32
+ printf(" : celt32 celt coder (32 kHz)\n");
+#endif
+#ifdef CODEC_RED
+#ifdef CODEC_G711
+ printf(" : red_pcm Redundancy RTP packet with 2*G711A frames\n");
+#endif
+#ifdef CODEC_ISAC
+ printf(" : red_isac Redundancy RTP packet with 2*iSAC frames\n");
+#endif
+#endif
+ printf("\n");
+
+#if (defined(CODEC_CNGCODEC8) || defined(CODEC_CNGCODEC16) || \
+ defined(CODEC_CNGCODEC32) || defined(CODEC_CNGCODEC48))
+ printf("useVAD : 0 Voice Activity Detection is switched off\n");
+ printf(" : 1 Voice Activity Detection is switched on\n\n");
+#else
+ printf("useVAD : 0 Voice Activity Detection switched off (on not supported)\n\n");
+#endif
+ printf("bitrate : Codec bitrate in bps (only applies to vbr codecs)\n\n");
+
+ return(0);
+ }
+
+ FILE* in_file=fopen(argv[1],"rb");
+ CHECK_NOT_NULL(in_file);
+ printf("Input file: %s\n",argv[1]);
+ FILE* out_file=fopen(argv[2],"wb");
+ CHECK_NOT_NULL(out_file);
+ printf("Output file: %s\n\n",argv[2]);
+ packet_size=atoi(argv[3]);
+ CHECK_NOT_NULL(packet_size);
+ printf("Packet size: %i\n",packet_size);
+
+ // check for stereo
+ if(argv[4][strlen(argv[4])-1] == '*') {
+ // use stereo
+ usingStereo = true;
+ numChannels = 2;
+ argv[4][strlen(argv[4])-1] = '\0';
+ }
+
+ NetEQTest_GetCodec_and_PT(argv[4], &usedCodec, &payloadType, packet_size, &fs, &bitrate, &useRed);
+
+ if(useRed) {
+ RTPheaderLen = 12 + 4 + 1; /* standard RTP = 12; 4 bytes per redundant payload, except last one which is 1 byte */
+ }
+
+ useVAD=atoi(argv[5]);
+#if !(defined(CODEC_CNGCODEC8) || defined(CODEC_CNGCODEC16) || \
+ defined(CODEC_CNGCODEC32) || defined(CODEC_CNGCODEC48))
+ if (useVAD!=0) {
+ printf("Error: this simulation does not support VAD/DTX/CNG\n");
+ }
+#endif
+
+ // check stereo type
+ if(usingStereo)
+ {
+ switch(usedCodec)
+ {
+ // sample based codecs
+ case webrtc::kDecoderPCMu:
+ case webrtc::kDecoderPCMa:
+ case webrtc::kDecoderG722:
+ {
+ // 1 octet per sample
+ stereoMode = STEREO_MODE_SAMPLE_1;
+ break;
+ }
+ case webrtc::kDecoderPCM16B:
+ case webrtc::kDecoderPCM16Bwb:
+ case webrtc::kDecoderPCM16Bswb32kHz:
+ case webrtc::kDecoderPCM16Bswb48kHz:
+ {
+ // 2 octets per sample
+ stereoMode = STEREO_MODE_SAMPLE_2;
+ break;
+ }
+
+ // fixed-rate frame codecs (with internal VAD)
+ default:
+ {
+ printf("Cannot use codec %s as stereo codec\n", argv[4]);
+ exit(0);
+ }
+ }
+ }
+
+ if ((usedCodec == webrtc::kDecoderISAC) || (usedCodec == webrtc::kDecoderISACswb))
+ {
+ if (argc != 7)
+ {
+ if (usedCodec == webrtc::kDecoderISAC)
+ {
+ bitrate = 32000;
+ printf(
+ "Running iSAC at default bitrate of 32000 bps (to specify explicitly add the bps as last parameter)\n");
+ }
+ else // (usedCodec==webrtc::kDecoderISACswb)
+ {
+ bitrate = 56000;
+ printf(
+ "Running iSAC at default bitrate of 56000 bps (to specify explicitly add the bps as last parameter)\n");
+ }
+ }
+ else
+ {
+ bitrate = atoi(argv[6]);
+ if (usedCodec == webrtc::kDecoderISAC)
+ {
+ if ((bitrate < 10000) || (bitrate > 32000))
+ {
+ printf(
+ "Error: iSAC bitrate must be between 10000 and 32000 bps (%i is invalid)\n",
+ bitrate);
+ exit(0);
+ }
+ printf("Running iSAC at bitrate of %i bps\n", bitrate);
+ }
+ else // (usedCodec==webrtc::kDecoderISACswb)
+ {
+ if ((bitrate < 32000) || (bitrate > 56000))
+ {
+ printf(
+ "Error: iSAC SWB bitrate must be between 32000 and 56000 bps (%i is invalid)\n",
+ bitrate);
+ exit(0);
+ }
+ }
+ }
+ }
+ else
+ {
+ if (argc == 7)
+ {
+ printf(
+ "Error: Bitrate parameter can only be specified for iSAC, G.723, and G.729.1\n");
+ exit(0);
+ }
+ }
+
+ if(useRed) {
+ printf("Redundancy engaged. ");
+ }
+ printf("Used codec: %i\n",usedCodec);
+ printf("Payload type: %i\n",payloadType);
+
+ NetEQTest_init_coders(usedCodec, packet_size, bitrate, fs, useVAD, numChannels);
+
+ /* write file header */
+ //fprintf(out_file, "#!RTPencode%s\n", "1.0");
+ fprintf(out_file, "#!rtpplay%s \n", "1.0"); // this is the string that rtpplay needs
+ uint32_t dummy_variable = 0; // should be converted to network endian format, but does not matter when 0
+ if (fwrite(&dummy_variable, 4, 1, out_file) != 1) {
+ return -1;
+ }
+ if (fwrite(&dummy_variable, 4, 1, out_file) != 1) {
+ return -1;
+ }
+ if (fwrite(&dummy_variable, 4, 1, out_file) != 1) {
+ return -1;
+ }
+ if (fwrite(&dummy_variable, 2, 1, out_file) != 1) {
+ return -1;
+ }
+ if (fwrite(&dummy_variable, 2, 1, out_file) != 1) {
+ return -1;
+ }
+
+#ifdef TIMESTAMP_WRAPAROUND
+ timestamp = 0xFFFFFFFF - fs*10; /* should give wrap-around in 10 seconds */
+#endif
+#if defined(RANDOM_DATA) | defined(RANDOM_PAYLOAD_DATA)
+ srand(RANDOM_SEED);
+#endif
+
+ /* if redundancy is used, the first redundant payload is zero length */
+ red_len[0] = 0;
+
+ /* read first frame */
+ len=fread(org_data,2,packet_size * numChannels,in_file) / numChannels;
+
+ /* de-interleave if stereo */
+ if ( usingStereo )
+ {
+ stereoDeInterleave(org_data, len * numChannels);
+ }
+
+ while (len==packet_size) {
+
+#ifdef INSERT_DTMF_PACKETS
+ dtmfSent = false;
+
+ if ( sendtime >= NTone * DTMF_PACKET_INTERVAL ) {
+ if ( sendtime < NTone * DTMF_PACKET_INTERVAL + DTMF_DURATION ) {
+ // tone has not ended
+ if (DTMFfirst==1) {
+ DTMFtimestamp = timestamp; // save this timestamp
+ DTMFfirst=0;
+ }
+ makeRTPheader(rtp_data, NETEQ_CODEC_AVT_PT, seqNo,DTMFtimestamp, ssrc);
+ enc_len = makeDTMFpayload(&rtp_data[12], NTone % 12, 0, 4, (int) (sendtime - NTone * DTMF_PACKET_INTERVAL)*(fs/1000) + len);
+ }
+ else {
+ // tone has ended
+ makeRTPheader(rtp_data, NETEQ_CODEC_AVT_PT, seqNo,DTMFtimestamp, ssrc);
+ enc_len = makeDTMFpayload(&rtp_data[12], NTone % 12, 1, 4, DTMF_DURATION*(fs/1000));
+ NTone++;
+ DTMFfirst=1;
+ }
+
+ /* write RTP packet to file */
+ length = htons(12 + enc_len + 8);
+ plen = htons(12 + enc_len);
+ offset = (uint32_t) sendtime; //(timestamp/(fs/1000));
+ offset = htonl(offset);
+ if (fwrite(&length, 2, 1, out_file) != 1) {
+ return -1;
+ }
+ if (fwrite(&plen, 2, 1, out_file) != 1) {
+ return -1;
+ }
+ if (fwrite(&offset, 4, 1, out_file) != 1) {
+ return -1;
+ }
+ if (fwrite(rtp_data, 12 + enc_len, 1, out_file) != 1) {
+ return -1;
+ }
+
+ dtmfSent = true;
+ }
+#endif
+
+#ifdef NO_DTMF_OVERDUB
+ /* If DTMF is sent, we should not send any speech packets during the same time */
+ if (dtmfSent) {
+ enc_len = 0;
+ }
+ else {
+#endif
+ /* encode frame */
+ enc_len=NetEQTest_encode(usedCodec, org_data, packet_size, &rtp_data[12] ,fs,&vad, useVAD, bitrate, numChannels);
+ if (enc_len==-1) {
+ printf("Error encoding frame\n");
+ exit(0);
+ }
+
+ if ( usingStereo &&
+ stereoMode != STEREO_MODE_FRAME &&
+ vad == 1 )
+ {
+ // interleave the encoded payload for sample-based codecs (not for CNG)
+ stereoInterleave(&rtp_data[12], enc_len, stereoMode);
+ }
+#ifdef NO_DTMF_OVERDUB
+ }
+#endif
+
+ if (enc_len > 0 && (sendtime <= STOPSENDTIME || sendtime > RESTARTSENDTIME)) {
+ if(useRed) {
+ if(red_len[0] > 0) {
+ memmove(&rtp_data[RTPheaderLen+red_len[0]], &rtp_data[12], enc_len);
+ memcpy(&rtp_data[RTPheaderLen], red_data, red_len[0]);
+
+ red_len[1] = enc_len;
+ red_TS[1] = timestamp;
+ if(vad)
+ red_PT[1] = payloadType;
+ else
+ red_PT[1] = NETEQ_CODEC_CN_PT;
+
+ makeRedundantHeader(rtp_data, red_PT, 2, red_TS, red_len, seqNo++, ssrc);
+
+
+ enc_len += red_len[0] + RTPheaderLen - 12;
+ }
+ else { // do not use redundancy payload for this packet, i.e., only last payload
+ memmove(&rtp_data[RTPheaderLen-4], &rtp_data[12], enc_len);
+ //memcpy(&rtp_data[RTPheaderLen], red_data, red_len[0]);
+
+ red_len[1] = enc_len;
+ red_TS[1] = timestamp;
+ if(vad)
+ red_PT[1] = payloadType;
+ else
+ red_PT[1] = NETEQ_CODEC_CN_PT;
+
+ makeRedundantHeader(rtp_data, red_PT, 2, red_TS, red_len, seqNo++, ssrc);
+
+
+ enc_len += red_len[0] + RTPheaderLen - 4 - 12; // 4 is length of redundancy header (not used)
+ }
+ }
+ else {
+
+ /* make RTP header */
+ if (vad) // regular speech data
+ makeRTPheader(rtp_data, payloadType, seqNo++,timestamp, ssrc);
+ else // CNG data
+ makeRTPheader(rtp_data, NETEQ_CODEC_CN_PT, seqNo++,timestamp, ssrc);
+
+ }
+#ifdef MULTIPLE_SAME_TIMESTAMP
+ int mult_pack=0;
+ do {
+#endif //MULTIPLE_SAME_TIMESTAMP
+ /* write RTP packet to file */
+ length = htons(12 + enc_len + 8);
+ plen = htons(12 + enc_len);
+ offset = (uint32_t) sendtime;
+ //(timestamp/(fs/1000));
+ offset = htonl(offset);
+ if (fwrite(&length, 2, 1, out_file) != 1) {
+ return -1;
+ }
+ if (fwrite(&plen, 2, 1, out_file) != 1) {
+ return -1;
+ }
+ if (fwrite(&offset, 4, 1, out_file) != 1) {
+ return -1;
+ }
+#ifdef RANDOM_DATA
+ for (int k=0; k<12+enc_len; k++) {
+ rtp_data[k] = rand() + rand();
+ }
+#endif
+#ifdef RANDOM_PAYLOAD_DATA
+ for (int k=12; k<12+enc_len; k++) {
+ rtp_data[k] = rand() + rand();
+ }
+#endif
+ if (fwrite(rtp_data, 12 + enc_len, 1, out_file) != 1) {
+ return -1;
+ }
+#ifdef MULTIPLE_SAME_TIMESTAMP
+ } while ( (seqNo%REPEAT_PACKET_DISTANCE == 0) && (mult_pack++ < REPEAT_PACKET_COUNT) );
+#endif //MULTIPLE_SAME_TIMESTAMP
+
+#ifdef INSERT_OLD_PACKETS
+ if (packet_age >= OLD_PACKET*fs) {
+ if (!first_old_packet) {
+ // send the old packet
+ if (fwrite(&old_length, 2, 1,
+ out_file) != 1) {
+ return -1;
+ }
+ if (fwrite(&old_plen, 2, 1,
+ out_file) != 1) {
+ return -1;
+ }
+ if (fwrite(&offset, 4, 1,
+ out_file) != 1) {
+ return -1;
+ }
+ if (fwrite(old_rtp_data, 12 + old_enc_len,
+ 1, out_file) != 1) {
+ return -1;
+ }
+ }
+ // store current packet as old
+ old_length=length;
+ old_plen=plen;
+ memcpy(old_rtp_data,rtp_data,12+enc_len);
+ old_enc_len=enc_len;
+ first_old_packet=0;
+ packet_age=0;
+
+ }
+ packet_age += packet_size;
+#endif
+
+ if(useRed) {
+ /* move data to redundancy store */
+#ifdef CODEC_ISAC
+ if(usedCodec==webrtc::kDecoderISAC)
+ {
+ assert(!usingStereo); // Cannot handle stereo yet
+ red_len[0] = WebRtcIsac_GetRedPayload(ISAC_inst[0], (int16_t*)red_data);
+ }
+ else
+ {
+#endif
+ memcpy(red_data, &rtp_data[RTPheaderLen+red_len[0]], enc_len);
+ red_len[0]=red_len[1];
+#ifdef CODEC_ISAC
+ }
+#endif
+ red_TS[0]=red_TS[1];
+ red_PT[0]=red_PT[1];
+ }
+
+ }
+
+ /* read next frame */
+ len=fread(org_data,2,packet_size * numChannels,in_file) / numChannels;
+ /* de-interleave if stereo */
+ if ( usingStereo )
+ {
+ stereoDeInterleave(org_data, len * numChannels);
+ }
+
+ if (payloadType==NETEQ_CODEC_G722_PT)
+ timestamp+=len>>1;
+ else
+ timestamp+=len;
+
+ sendtime += (double) len/(fs/1000);
+ }
+
+ NetEQTest_free_coders(usedCodec, numChannels);
+ fclose(in_file);
+ fclose(out_file);
+ printf("Done!\n");
+
+ return(0);
+}
+
+
+
+
+/****************/
+/* Subfunctions */
+/****************/
+
+void NetEQTest_GetCodec_and_PT(char * name, webrtc::NetEqDecoder *codec, int *PT, int frameLen, int *fs, int *bitrate, int *useRed) {
+
+ *bitrate = 0; /* Default bitrate setting */
+ *useRed = 0; /* Default no redundancy */
+
+ if(!strcmp(name,"pcmu")){
+ *codec=webrtc::kDecoderPCMu;
+ *PT=NETEQ_CODEC_PCMU_PT;
+ *fs=8000;
+ }
+ else if(!strcmp(name,"pcma")){
+ *codec=webrtc::kDecoderPCMa;
+ *PT=NETEQ_CODEC_PCMA_PT;
+ *fs=8000;
+ }
+ else if(!strcmp(name,"pcm16b")){
+ *codec=webrtc::kDecoderPCM16B;
+ *PT=NETEQ_CODEC_PCM16B_PT;
+ *fs=8000;
+ }
+ else if(!strcmp(name,"pcm16b_wb")){
+ *codec=webrtc::kDecoderPCM16Bwb;
+ *PT=NETEQ_CODEC_PCM16B_WB_PT;
+ *fs=16000;
+ }
+ else if(!strcmp(name,"pcm16b_swb32")){
+ *codec=webrtc::kDecoderPCM16Bswb32kHz;
+ *PT=NETEQ_CODEC_PCM16B_SWB32KHZ_PT;
+ *fs=32000;
+ }
+ else if(!strcmp(name,"pcm16b_swb48")){
+ *codec=webrtc::kDecoderPCM16Bswb48kHz;
+ *PT=NETEQ_CODEC_PCM16B_SWB48KHZ_PT;
+ *fs=48000;
+ }
+ else if(!strcmp(name,"g722")){
+ *codec=webrtc::kDecoderG722;
+ *PT=NETEQ_CODEC_G722_PT;
+ *fs=16000;
+ }
+ else if((!strcmp(name,"ilbc"))&&((frameLen%240==0)||(frameLen%160==0))){
+ *fs=8000;
+ *codec=webrtc::kDecoderILBC;
+ *PT=NETEQ_CODEC_ILBC_PT;
+ }
+ else if(!strcmp(name,"isac")){
+ *fs=16000;
+ *codec=webrtc::kDecoderISAC;
+ *PT=NETEQ_CODEC_ISAC_PT;
+ }
+ else if(!strcmp(name,"isacswb")){
+ *fs=32000;
+ *codec=webrtc::kDecoderISACswb;
+ *PT=NETEQ_CODEC_ISACSWB_PT;
+ }
+ else if(!strcmp(name,"celt32")){
+ *fs=32000;
+ *codec=webrtc::kDecoderCELT_32;
+ *PT=NETEQ_CODEC_CELT32_PT;
+ }
+ else if(!strcmp(name,"red_pcm")){
+ *codec=webrtc::kDecoderPCMa;
+ *PT=NETEQ_CODEC_PCMA_PT; /* this will be the PT for the sub-headers */
+ *fs=8000;
+ *useRed = 1;
+ } else if(!strcmp(name,"red_isac")){
+ *codec=webrtc::kDecoderISAC;
+ *PT=NETEQ_CODEC_ISAC_PT; /* this will be the PT for the sub-headers */
+ *fs=16000;
+ *useRed = 1;
+ } else {
+ printf("Error: Not a supported codec (%s)\n", name);
+ exit(0);
+ }
+
+}
+
+
+
+
+int NetEQTest_init_coders(webrtc::NetEqDecoder coder, int enc_frameSize, int bitrate, int sampfreq , int vad, int numChannels){
+
+ int ok=0;
+
+ for (int k = 0; k < numChannels; k++)
+ {
+ ok=WebRtcVad_Create(&VAD_inst[k]);
+ if (ok!=0) {
+ printf("Error: Couldn't allocate memory for VAD instance\n");
+ exit(0);
+ }
+ ok=WebRtcVad_Init(VAD_inst[k]);
+ if (ok==-1) {
+ printf("Error: Initialization of VAD struct failed\n");
+ exit(0);
+ }
+
+
+#if (defined(CODEC_CNGCODEC8) || defined(CODEC_CNGCODEC16) || \
+ defined(CODEC_CNGCODEC32) || defined(CODEC_CNGCODEC48))
+ ok=WebRtcCng_CreateEnc(&CNGenc_inst[k]);
+ if (ok!=0) {
+ printf("Error: Couldn't allocate memory for CNG encoding instance\n");
+ exit(0);
+ }
+ if(sampfreq <= 16000) {
+ ok=WebRtcCng_InitEnc(CNGenc_inst[k],sampfreq, 200, 5);
+ if (ok==-1) {
+ printf("Error: Initialization of CNG struct failed. Error code %d\n",
+ WebRtcCng_GetErrorCodeEnc(CNGenc_inst[k]));
+ exit(0);
+ }
+ }
+#endif
+
+ switch (coder) {
+#ifdef CODEC_PCM16B
+ case webrtc::kDecoderPCM16B :
+#endif
+#ifdef CODEC_PCM16B_WB
+ case webrtc::kDecoderPCM16Bwb :
+#endif
+#ifdef CODEC_PCM16B_32KHZ
+ case webrtc::kDecoderPCM16Bswb32kHz :
+#endif
+#ifdef CODEC_PCM16B_48KHZ
+ case webrtc::kDecoderPCM16Bswb48kHz :
+#endif
+#ifdef CODEC_G711
+ case webrtc::kDecoderPCMu :
+ case webrtc::kDecoderPCMa :
+#endif
+ // do nothing
+ break;
+#ifdef CODEC_G729
+ case webrtc::kDecoderG729:
+ if (sampfreq==8000) {
+ if ((enc_frameSize==80)||(enc_frameSize==160)||(enc_frameSize==240)||(enc_frameSize==320)||(enc_frameSize==400)||(enc_frameSize==480)) {
+ ok=WebRtcG729_CreateEnc(&G729enc_inst[k]);
+ if (ok!=0) {
+ printf("Error: Couldn't allocate memory for G729 encoding instance\n");
+ exit(0);
+ }
+ } else {
+ printf("\nError: g729 only supports 10, 20, 30, 40, 50 or 60 ms!!\n\n");
+ exit(0);
+ }
+ WebRtcG729_EncoderInit(G729enc_inst[k], vad);
+ if ((vad==1)&&(enc_frameSize!=80)) {
+ printf("\nError - This simulation only supports VAD for G729 at 10ms packets (not %dms)\n", (enc_frameSize>>3));
+ }
+ } else {
+ printf("\nError - g729 is only developed for 8kHz \n");
+ exit(0);
+ }
+ break;
+#endif
+#ifdef CODEC_G729_1
+ case webrtc::kDecoderG729_1:
+ if (sampfreq==16000) {
+ if ((enc_frameSize==320)||(enc_frameSize==640)||(enc_frameSize==960)
+ ) {
+ ok=WebRtcG7291_Create(&G729_1_inst[k]);
+ if (ok!=0) {
+ printf("Error: Couldn't allocate memory for G.729.1 codec instance\n");
+ exit(0);
+ }
+ } else {
+ printf("\nError: G.729.1 only supports 20, 40 or 60 ms!!\n\n");
+ exit(0);
+ }
+ if (!(((bitrate >= 12000) && (bitrate <= 32000) && (bitrate%2000 == 0)) || (bitrate == 8000))) {
+ /* must be 8, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, or 32 kbps */
+ printf("\nError: G.729.1 bitrate must be 8000 or 12000--32000 in steps of 2000 bps\n");
+ exit(0);
+ }
+ WebRtcG7291_EncoderInit(G729_1_inst[k], bitrate, 0 /* flag8kHz*/, 0 /*flagG729mode*/);
+ } else {
+ printf("\nError - G.729.1 input is always 16 kHz \n");
+ exit(0);
+ }
+ break;
+#endif
+#ifdef CODEC_SPEEX_8
+ case webrtc::kDecoderSPEEX_8 :
+ if (sampfreq==8000) {
+ if ((enc_frameSize==160)||(enc_frameSize==320)||(enc_frameSize==480)) {
+ ok=WebRtcSpeex_CreateEnc(&SPEEX8enc_inst[k], sampfreq);
+ if (ok!=0) {
+ printf("Error: Couldn't allocate memory for Speex encoding instance\n");
+ exit(0);
+ }
+ } else {
+ printf("\nError: Speex only supports 20, 40, and 60 ms!!\n\n");
+ exit(0);
+ }
+ if ((vad==1)&&(enc_frameSize!=160)) {
+ printf("\nError - This simulation only supports VAD for Speex at 20ms packets (not %dms)\n", (enc_frameSize>>3));
+ vad=0;
+ }
+ ok=WebRtcSpeex_EncoderInit(SPEEX8enc_inst[k], 0/*vbr*/, 3 /*complexity*/, vad);
+ if (ok!=0) exit(0);
+ } else {
+ printf("\nError - Speex8 called with sample frequency other than 8 kHz.\n\n");
+ }
+ break;
+#endif
+#ifdef CODEC_SPEEX_16
+ case webrtc::kDecoderSPEEX_16 :
+ if (sampfreq==16000) {
+ if ((enc_frameSize==320)||(enc_frameSize==640)||(enc_frameSize==960)) {
+ ok=WebRtcSpeex_CreateEnc(&SPEEX16enc_inst[k], sampfreq);
+ if (ok!=0) {
+ printf("Error: Couldn't allocate memory for Speex encoding instance\n");
+ exit(0);
+ }
+ } else {
+ printf("\nError: Speex only supports 20, 40, and 60 ms!!\n\n");
+ exit(0);
+ }
+ if ((vad==1)&&(enc_frameSize!=320)) {
+ printf("\nError - This simulation only supports VAD for Speex at 20ms packets (not %dms)\n", (enc_frameSize>>4));
+ vad=0;
+ }
+ ok=WebRtcSpeex_EncoderInit(SPEEX16enc_inst[k], 0/*vbr*/, 3 /*complexity*/, vad);
+ if (ok!=0) exit(0);
+ } else {
+ printf("\nError - Speex16 called with sample frequency other than 16 kHz.\n\n");
+ }
+ break;
+#endif
+#ifdef CODEC_CELT_32
+ case webrtc::kDecoderCELT_32 :
+ if (sampfreq==32000) {
+ if (enc_frameSize==320) {
+ ok=WebRtcCelt_CreateEnc(&CELT32enc_inst[k], 1 /*mono*/);
+ if (ok!=0) {
+ printf("Error: Couldn't allocate memory for Celt encoding instance\n");
+ exit(0);
+ }
+ } else {
+ printf("\nError: Celt only supports 10 ms!!\n\n");
+ exit(0);
+ }
+ ok=WebRtcCelt_EncoderInit(CELT32enc_inst[k], 1 /*mono*/, 48000 /*bitrate*/);
+ if (ok!=0) exit(0);
+ } else {
+ printf("\nError - Celt32 called with sample frequency other than 32 kHz.\n\n");
+ }
+ break;
+#endif
+
+#ifdef CODEC_G722_1_16
+ case webrtc::kDecoderG722_1_16 :
+ if (sampfreq==16000) {
+ ok=WebRtcG7221_CreateEnc16(&G722_1_16enc_inst[k]);
+ if (ok!=0) {
+ printf("Error: Couldn't allocate memory for G.722.1 instance\n");
+ exit(0);
+ }
+ if (enc_frameSize==320) {
+ } else {
+ printf("\nError: G722.1 only supports 20 ms!!\n\n");
+ exit(0);
+ }
+ WebRtcG7221_EncoderInit16((G722_1_16_encinst_t*)G722_1_16enc_inst[k]);
+ } else {
+ printf("\nError - G722.1 is only developed for 16kHz \n");
+ exit(0);
+ }
+ break;
+#endif
+#ifdef CODEC_G722_1_24
+ case webrtc::kDecoderG722_1_24 :
+ if (sampfreq==16000) {
+ ok=WebRtcG7221_CreateEnc24(&G722_1_24enc_inst[k]);
+ if (ok!=0) {
+ printf("Error: Couldn't allocate memory for G.722.1 instance\n");
+ exit(0);
+ }
+ if (enc_frameSize==320) {
+ } else {
+ printf("\nError: G722.1 only supports 20 ms!!\n\n");
+ exit(0);
+ }
+ WebRtcG7221_EncoderInit24((G722_1_24_encinst_t*)G722_1_24enc_inst[k]);
+ } else {
+ printf("\nError - G722.1 is only developed for 16kHz \n");
+ exit(0);
+ }
+ break;
+#endif
+#ifdef CODEC_G722_1_32
+ case webrtc::kDecoderG722_1_32 :
+ if (sampfreq==16000) {
+ ok=WebRtcG7221_CreateEnc32(&G722_1_32enc_inst[k]);
+ if (ok!=0) {
+ printf("Error: Couldn't allocate memory for G.722.1 instance\n");
+ exit(0);
+ }
+ if (enc_frameSize==320) {
+ } else {
+ printf("\nError: G722.1 only supports 20 ms!!\n\n");
+ exit(0);
+ }
+ WebRtcG7221_EncoderInit32((G722_1_32_encinst_t*)G722_1_32enc_inst[k]);
+ } else {
+ printf("\nError - G722.1 is only developed for 16kHz \n");
+ exit(0);
+ }
+ break;
+#endif
+#ifdef CODEC_G722_1C_24
+ case webrtc::kDecoderG722_1C_24 :
+ if (sampfreq==32000) {
+ ok=WebRtcG7221C_CreateEnc24(&G722_1C_24enc_inst[k]);
+ if (ok!=0) {
+ printf("Error: Couldn't allocate memory for G.722.1C instance\n");
+ exit(0);
+ }
+ if (enc_frameSize==640) {
+ } else {
+ printf("\nError: G722.1 C only supports 20 ms!!\n\n");
+ exit(0);
+ }
+ WebRtcG7221C_EncoderInit24((G722_1C_24_encinst_t*)G722_1C_24enc_inst[k]);
+ } else {
+ printf("\nError - G722.1 C is only developed for 32kHz \n");
+ exit(0);
+ }
+ break;
+#endif
+#ifdef CODEC_G722_1C_32
+ case webrtc::kDecoderG722_1C_32 :
+ if (sampfreq==32000) {
+ ok=WebRtcG7221C_CreateEnc32(&G722_1C_32enc_inst[k]);
+ if (ok!=0) {
+ printf("Error: Couldn't allocate memory for G.722.1C instance\n");
+ exit(0);
+ }
+ if (enc_frameSize==640) {
+ } else {
+ printf("\nError: G722.1 C only supports 20 ms!!\n\n");
+ exit(0);
+ }
+ WebRtcG7221C_EncoderInit32((G722_1C_32_encinst_t*)G722_1C_32enc_inst[k]);
+ } else {
+ printf("\nError - G722.1 C is only developed for 32kHz \n");
+ exit(0);
+ }
+ break;
+#endif
+#ifdef CODEC_G722_1C_48
+ case webrtc::kDecoderG722_1C_48 :
+ if (sampfreq==32000) {
+ ok=WebRtcG7221C_CreateEnc48(&G722_1C_48enc_inst[k]);
+ if (ok!=0) {
+ printf("Error: Couldn't allocate memory for G.722.1C instance\n");
+ exit(0);
+ }
+ if (enc_frameSize==640) {
+ } else {
+ printf("\nError: G722.1 C only supports 20 ms!!\n\n");
+ exit(0);
+ }
+ WebRtcG7221C_EncoderInit48((G722_1C_48_encinst_t*)G722_1C_48enc_inst[k]);
+ } else {
+ printf("\nError - G722.1 C is only developed for 32kHz \n");
+ exit(0);
+ }
+ break;
+#endif
+#ifdef CODEC_G722
+ case webrtc::kDecoderG722 :
+ if (sampfreq==16000) {
+ if (enc_frameSize%2==0) {
+ } else {
+ printf("\nError - g722 frames must have an even number of enc_frameSize\n");
+ exit(0);
+ }
+ WebRtcG722_CreateEncoder(&g722EncState[k]);
+ WebRtcG722_EncoderInit(g722EncState[k]);
+ } else {
+ printf("\nError - g722 is only developed for 16kHz \n");
+ exit(0);
+ }
+ break;
+#endif
+#ifdef CODEC_AMR
+ case webrtc::kDecoderAMR :
+ if (sampfreq==8000) {
+ ok=WebRtcAmr_CreateEnc(&AMRenc_inst[k]);
+ if (ok!=0) {
+ printf("Error: Couldn't allocate memory for AMR encoding instance\n");
+ exit(0);
+ }if ((enc_frameSize==160)||(enc_frameSize==320)||(enc_frameSize==480)) {
+ } else {
+ printf("\nError - AMR must have a multiple of 160 enc_frameSize\n");
+ exit(0);
+ }
+ WebRtcAmr_EncoderInit(AMRenc_inst[k], vad);
+ WebRtcAmr_EncodeBitmode(AMRenc_inst[k], AMRBandwidthEfficient);
+ AMR_bitrate = bitrate;
+ } else {
+ printf("\nError - AMR is only developed for 8kHz \n");
+ exit(0);
+ }
+ break;
+#endif
+#ifdef CODEC_AMRWB
+ case webrtc::kDecoderAMRWB :
+ if (sampfreq==16000) {
+ ok=WebRtcAmrWb_CreateEnc(&AMRWBenc_inst[k]);
+ if (ok!=0) {
+ printf("Error: Couldn't allocate memory for AMRWB encoding instance\n");
+ exit(0);
+ }
+ if (((enc_frameSize/320)<0)||((enc_frameSize/320)>3)||((enc_frameSize%320)!=0)) {
+ printf("\nError - AMRwb must have frameSize of 20, 40 or 60ms\n");
+ exit(0);
+ }
+ WebRtcAmrWb_EncoderInit(AMRWBenc_inst[k], vad);
+ if (bitrate==7000) {
+ AMRWB_bitrate = AMRWB_MODE_7k;
+ } else if (bitrate==9000) {
+ AMRWB_bitrate = AMRWB_MODE_9k;
+ } else if (bitrate==12000) {
+ AMRWB_bitrate = AMRWB_MODE_12k;
+ } else if (bitrate==14000) {
+ AMRWB_bitrate = AMRWB_MODE_14k;
+ } else if (bitrate==16000) {
+ AMRWB_bitrate = AMRWB_MODE_16k;
+ } else if (bitrate==18000) {
+ AMRWB_bitrate = AMRWB_MODE_18k;
+ } else if (bitrate==20000) {
+ AMRWB_bitrate = AMRWB_MODE_20k;
+ } else if (bitrate==23000) {
+ AMRWB_bitrate = AMRWB_MODE_23k;
+ } else if (bitrate==24000) {
+ AMRWB_bitrate = AMRWB_MODE_24k;
+ }
+ WebRtcAmrWb_EncodeBitmode(AMRWBenc_inst[k], AMRBandwidthEfficient);
+
+ } else {
+ printf("\nError - AMRwb is only developed for 16kHz \n");
+ exit(0);
+ }
+ break;
+#endif
+#ifdef CODEC_ILBC
+ case webrtc::kDecoderILBC :
+ if (sampfreq==8000) {
+ ok=WebRtcIlbcfix_EncoderCreate(&iLBCenc_inst[k]);
+ if (ok!=0) {
+ printf("Error: Couldn't allocate memory for iLBC encoding instance\n");
+ exit(0);
+ }
+ if ((enc_frameSize==160)||(enc_frameSize==240)||(enc_frameSize==320)||(enc_frameSize==480)) {
+ } else {
+ printf("\nError - iLBC only supports 160, 240, 320 and 480 enc_frameSize (20, 30, 40 and 60 ms)\n");
+ exit(0);
+ }
+ if ((enc_frameSize==160)||(enc_frameSize==320)) {
+ /* 20 ms version */
+ WebRtcIlbcfix_EncoderInit(iLBCenc_inst[k], 20);
+ } else {
+ /* 30 ms version */
+ WebRtcIlbcfix_EncoderInit(iLBCenc_inst[k], 30);
+ }
+ } else {
+ printf("\nError - iLBC is only developed for 8kHz \n");
+ exit(0);
+ }
+ break;
+#endif
+#ifdef CODEC_ISAC
+ case webrtc::kDecoderISAC:
+ if (sampfreq==16000) {
+ ok=WebRtcIsac_Create(&ISAC_inst[k]);
+ if (ok!=0) {
+ printf("Error: Couldn't allocate memory for iSAC instance\n");
+ exit(0);
+ }if ((enc_frameSize==480)||(enc_frameSize==960)) {
+ } else {
+ printf("\nError - iSAC only supports frameSize (30 and 60 ms)\n");
+ exit(0);
+ }
+ WebRtcIsac_EncoderInit(ISAC_inst[k],1);
+ if ((bitrate<10000)||(bitrate>32000)) {
+ printf("\nError - iSAC bitrate has to be between 10000 and 32000 bps (not %i)\n", bitrate);
+ exit(0);
+ }
+ WebRtcIsac_Control(ISAC_inst[k], bitrate, enc_frameSize>>4);
+ } else {
+ printf("\nError - iSAC only supports 480 or 960 enc_frameSize (30 or 60 ms)\n");
+ exit(0);
+ }
+ break;
+#endif
+#ifdef NETEQ_ISACFIX_CODEC
+ case webrtc::kDecoderISAC:
+ if (sampfreq==16000) {
+ ok=WebRtcIsacfix_Create(&ISAC_inst[k]);
+ if (ok!=0) {
+ printf("Error: Couldn't allocate memory for iSAC instance\n");
+ exit(0);
+ }if ((enc_frameSize==480)||(enc_frameSize==960)) {
+ } else {
+ printf("\nError - iSAC only supports frameSize (30 and 60 ms)\n");
+ exit(0);
+ }
+ WebRtcIsacfix_EncoderInit(ISAC_inst[k],1);
+ if ((bitrate<10000)||(bitrate>32000)) {
+ printf("\nError - iSAC bitrate has to be between 10000 and 32000 bps (not %i)\n", bitrate);
+ exit(0);
+ }
+ WebRtcIsacfix_Control(ISAC_inst[k], bitrate, enc_frameSize>>4);
+ } else {
+ printf("\nError - iSAC only supports 480 or 960 enc_frameSize (30 or 60 ms)\n");
+ exit(0);
+ }
+ break;
+#endif
+#ifdef CODEC_ISAC_SWB
+ case webrtc::kDecoderISACswb:
+ if (sampfreq==32000) {
+ ok=WebRtcIsac_Create(&ISACSWB_inst[k]);
+ if (ok!=0) {
+ printf("Error: Couldn't allocate memory for iSAC SWB instance\n");
+ exit(0);
+ }if (enc_frameSize==960) {
+ } else {
+ printf("\nError - iSAC SWB only supports frameSize 30 ms\n");
+ exit(0);
+ }
+ ok = WebRtcIsac_SetEncSampRate(ISACSWB_inst[k], 32000);
+ if (ok!=0) {
+ printf("Error: Couldn't set sample rate for iSAC SWB instance\n");
+ exit(0);
+ }
+ WebRtcIsac_EncoderInit(ISACSWB_inst[k],1);
+ if ((bitrate<32000)||(bitrate>56000)) {
+ printf("\nError - iSAC SWB bitrate has to be between 32000 and 56000 bps (not %i)\n", bitrate);
+ exit(0);
+ }
+ WebRtcIsac_Control(ISACSWB_inst[k], bitrate, enc_frameSize>>5);
+ } else {
+ printf("\nError - iSAC SWB only supports 960 enc_frameSize (30 ms)\n");
+ exit(0);
+ }
+ break;
+#endif
+#ifdef CODEC_GSMFR
+ case webrtc::kDecoderGSMFR:
+ if (sampfreq==8000) {
+ ok=WebRtcGSMFR_CreateEnc(&GSMFRenc_inst[k]);
+ if (ok!=0) {
+ printf("Error: Couldn't allocate memory for GSM FR encoding instance\n");
+ exit(0);
+ }
+ if ((enc_frameSize==160)||(enc_frameSize==320)||(enc_frameSize==480)) {
+ } else {
+ printf("\nError - GSM FR must have a multiple of 160 enc_frameSize\n");
+ exit(0);
+ }
+ WebRtcGSMFR_EncoderInit(GSMFRenc_inst[k], 0);
+ } else {
+ printf("\nError - GSM FR is only developed for 8kHz \n");
+ exit(0);
+ }
+ break;
+#endif
+ default :
+ printf("Error: unknown codec in call to NetEQTest_init_coders.\n");
+ exit(0);
+ break;
+ }
+
+ if (ok != 0) {
+ return(ok);
+ }
+ } // end for
+
+ return(0);
+}
+
+
+
+
+int NetEQTest_free_coders(webrtc::NetEqDecoder coder, int numChannels) {
+
+ for (int k = 0; k < numChannels; k++)
+ {
+ WebRtcVad_Free(VAD_inst[k]);
+#if (defined(CODEC_CNGCODEC8) || defined(CODEC_CNGCODEC16) || \
+ defined(CODEC_CNGCODEC32) || defined(CODEC_CNGCODEC48))
+ WebRtcCng_FreeEnc(CNGenc_inst[k]);
+#endif
+
+ switch (coder)
+ {
+#ifdef CODEC_PCM16B
+ case webrtc::kDecoderPCM16B :
+#endif
+#ifdef CODEC_PCM16B_WB
+ case webrtc::kDecoderPCM16Bwb :
+#endif
+#ifdef CODEC_PCM16B_32KHZ
+ case webrtc::kDecoderPCM16Bswb32kHz :
+#endif
+#ifdef CODEC_PCM16B_48KHZ
+ case webrtc::kDecoderPCM16Bswb48kHz :
+#endif
+#ifdef CODEC_G711
+ case webrtc::kDecoderPCMu :
+ case webrtc::kDecoderPCMa :
+#endif
+ // do nothing
+ break;
+#ifdef CODEC_G729
+ case webrtc::kDecoderG729:
+ WebRtcG729_FreeEnc(G729enc_inst[k]);
+ break;
+#endif
+#ifdef CODEC_G729_1
+ case webrtc::kDecoderG729_1:
+ WebRtcG7291_Free(G729_1_inst[k]);
+ break;
+#endif
+#ifdef CODEC_SPEEX_8
+ case webrtc::kDecoderSPEEX_8 :
+ WebRtcSpeex_FreeEnc(SPEEX8enc_inst[k]);
+ break;
+#endif
+#ifdef CODEC_SPEEX_16
+ case webrtc::kDecoderSPEEX_16 :
+ WebRtcSpeex_FreeEnc(SPEEX16enc_inst[k]);
+ break;
+#endif
+#ifdef CODEC_CELT_32
+ case webrtc::kDecoderCELT_32 :
+ WebRtcCelt_FreeEnc(CELT32enc_inst[k]);
+ break;
+#endif
+
+#ifdef CODEC_G722_1_16
+ case webrtc::kDecoderG722_1_16 :
+ WebRtcG7221_FreeEnc16(G722_1_16enc_inst[k]);
+ break;
+#endif
+#ifdef CODEC_G722_1_24
+ case webrtc::kDecoderG722_1_24 :
+ WebRtcG7221_FreeEnc24(G722_1_24enc_inst[k]);
+ break;
+#endif
+#ifdef CODEC_G722_1_32
+ case webrtc::kDecoderG722_1_32 :
+ WebRtcG7221_FreeEnc32(G722_1_32enc_inst[k]);
+ break;
+#endif
+#ifdef CODEC_G722_1C_24
+ case webrtc::kDecoderG722_1C_24 :
+ WebRtcG7221C_FreeEnc24(G722_1C_24enc_inst[k]);
+ break;
+#endif
+#ifdef CODEC_G722_1C_32
+ case webrtc::kDecoderG722_1C_32 :
+ WebRtcG7221C_FreeEnc32(G722_1C_32enc_inst[k]);
+ break;
+#endif
+#ifdef CODEC_G722_1C_48
+ case webrtc::kDecoderG722_1C_48 :
+ WebRtcG7221C_FreeEnc48(G722_1C_48enc_inst[k]);
+ break;
+#endif
+#ifdef CODEC_G722
+ case webrtc::kDecoderG722 :
+ WebRtcG722_FreeEncoder(g722EncState[k]);
+ break;
+#endif
+#ifdef CODEC_AMR
+ case webrtc::kDecoderAMR :
+ WebRtcAmr_FreeEnc(AMRenc_inst[k]);
+ break;
+#endif
+#ifdef CODEC_AMRWB
+ case webrtc::kDecoderAMRWB :
+ WebRtcAmrWb_FreeEnc(AMRWBenc_inst[k]);
+ break;
+#endif
+#ifdef CODEC_ILBC
+ case webrtc::kDecoderILBC :
+ WebRtcIlbcfix_EncoderFree(iLBCenc_inst[k]);
+ break;
+#endif
+#ifdef CODEC_ISAC
+ case webrtc::kDecoderISAC:
+ WebRtcIsac_Free(ISAC_inst[k]);
+ break;
+#endif
+#ifdef NETEQ_ISACFIX_CODEC
+ case webrtc::kDecoderISAC:
+ WebRtcIsacfix_Free(ISAC_inst[k]);
+ break;
+#endif
+#ifdef CODEC_ISAC_SWB
+ case webrtc::kDecoderISACswb:
+ WebRtcIsac_Free(ISACSWB_inst[k]);
+ break;
+#endif
+#ifdef CODEC_GSMFR
+ case webrtc::kDecoderGSMFR:
+ WebRtcGSMFR_FreeEnc(GSMFRenc_inst[k]);
+ break;
+#endif
+ default :
+ printf("Error: unknown codec in call to NetEQTest_init_coders.\n");
+ exit(0);
+ break;
+ }
+ }
+
+ return(0);
+}
+
+
+
+
+
+
+int NetEQTest_encode(int coder, int16_t *indata, int frameLen, unsigned char * encoded,int sampleRate ,
+ int * vad, int useVAD, int bitrate, int numChannels){
+
+ short cdlen = 0;
+ int16_t *tempdata;
+ static int first_cng=1;
+ int16_t tempLen;
+
+ *vad =1;
+
+ // check VAD first
+ if(useVAD)
+ {
+ *vad = 0;
+
+ for (int k = 0; k < numChannels; k++)
+ {
+ tempLen = frameLen;
+ tempdata = &indata[k*frameLen];
+ int localVad=0;
+ /* Partition the signal and test each chunk for VAD.
+ All chunks must be VAD=0 to produce a total VAD=0. */
+ while (tempLen >= 10*sampleRate/1000) {
+ if ((tempLen % 30*sampleRate/1000) == 0) { // tempLen is multiple of 30ms
+ localVad |= WebRtcVad_Process(VAD_inst[k] ,sampleRate, tempdata, 30*sampleRate/1000);
+ tempdata += 30*sampleRate/1000;
+ tempLen -= 30*sampleRate/1000;
+ }
+ else if (tempLen >= 20*sampleRate/1000) { // tempLen >= 20ms
+ localVad |= WebRtcVad_Process(VAD_inst[k] ,sampleRate, tempdata, 20*sampleRate/1000);
+ tempdata += 20*sampleRate/1000;
+ tempLen -= 20*sampleRate/1000;
+ }
+ else { // use 10ms
+ localVad |= WebRtcVad_Process(VAD_inst[k] ,sampleRate, tempdata, 10*sampleRate/1000);
+ tempdata += 10*sampleRate/1000;
+ tempLen -= 10*sampleRate/1000;
+ }
+ }
+
+ // aggregate all VAD decisions over all channels
+ *vad |= localVad;
+ }
+
+ if(!*vad){
+ // all channels are silent
+ cdlen = 0;
+ for (int k = 0; k < numChannels; k++)
+ {
+ WebRtcCng_Encode(CNGenc_inst[k],&indata[k*frameLen], (frameLen <= 640 ? frameLen : 640) /* max 640 */,
+ encoded,&tempLen,first_cng);
+ encoded += tempLen;
+ cdlen += tempLen;
+ }
+ *vad=0;
+ first_cng=0;
+ return(cdlen);
+ }
+ }
+
+
+ // loop over all channels
+ int totalLen = 0;
+
+ for (int k = 0; k < numChannels; k++)
+ {
+ /* Encode with the selected coder type */
+ if (coder==webrtc::kDecoderPCMu) { /*g711 u-law */
+#ifdef CODEC_G711
+ cdlen = WebRtcG711_EncodeU(G711state[k], indata, frameLen, (int16_t*) encoded);
+#endif
+ }
+ else if (coder==webrtc::kDecoderPCMa) { /*g711 A-law */
+#ifdef CODEC_G711
+ cdlen = WebRtcG711_EncodeA(G711state[k], indata, frameLen, (int16_t*) encoded);
+ }
+#endif
+#ifdef CODEC_PCM16B
+ else if ((coder==webrtc::kDecoderPCM16B)||(coder==webrtc::kDecoderPCM16Bwb)||
+ (coder==webrtc::kDecoderPCM16Bswb32kHz)||(coder==webrtc::kDecoderPCM16Bswb48kHz)) { /*pcm16b (8kHz, 16kHz, 32kHz or 48kHz) */
+ cdlen = WebRtcPcm16b_EncodeW16(indata, frameLen, (int16_t*) encoded);
+ }
+#endif
+#ifdef CODEC_G722
+ else if (coder==webrtc::kDecoderG722) { /*g722 */
+ cdlen=WebRtcG722_Encode(g722EncState[k], indata, frameLen, (int16_t*)encoded);
+ assert(cdlen == frameLen>>1);
+ }
+#endif
+#ifdef CODEC_ILBC
+ else if (coder==webrtc::kDecoderILBC) { /*iLBC */
+ cdlen=WebRtcIlbcfix_Encode(iLBCenc_inst[k], indata,frameLen,(int16_t*)encoded);
+ }
+#endif
+#if (defined(CODEC_ISAC) || defined(NETEQ_ISACFIX_CODEC)) // TODO(hlundin): remove all NETEQ_ISACFIX_CODEC
+ else if (coder==webrtc::kDecoderISAC) { /*iSAC */
+ int noOfCalls=0;
+ cdlen=0;
+ while (cdlen<=0) {
+#ifdef CODEC_ISAC /* floating point */
+ cdlen=WebRtcIsac_Encode(ISAC_inst[k],&indata[noOfCalls*160],(int16_t*)encoded);
+#else /* fixed point */
+ cdlen=WebRtcIsacfix_Encode(ISAC_inst[k],&indata[noOfCalls*160],(int16_t*)encoded);
+#endif
+ noOfCalls++;
+ }
+ }
+#endif
+#ifdef CODEC_ISAC_SWB
+ else if (coder==webrtc::kDecoderISACswb) { /* iSAC SWB */
+ int noOfCalls=0;
+ cdlen=0;
+ while (cdlen<=0) {
+ cdlen=WebRtcIsac_Encode(ISACSWB_inst[k],&indata[noOfCalls*320],(int16_t*)encoded);
+ noOfCalls++;
+ }
+ }
+#endif
+#ifdef CODEC_CELT_32
+ else if (coder==webrtc::kDecoderCELT_32) { /* Celt */
+ int encodedLen = 0;
+ cdlen = 0;
+ while (cdlen <= 0) {
+ cdlen = WebRtcCelt_Encode(CELT32enc_inst[k], &indata[encodedLen], encoded);
+ encodedLen += 10*32; /* 10 ms */
+ }
+ if( (encodedLen != frameLen) || cdlen < 0) {
+ printf("Error encoding Celt frame!\n");
+ exit(0);
+ }
+ }
+#endif
+
+ indata += frameLen;
+ encoded += cdlen;
+ totalLen += cdlen;
+
+ } // end for
+
+ first_cng=1;
+ return(totalLen);
+}
+
+
+
+void makeRTPheader(unsigned char* rtp_data, int payloadType, int seqNo, uint32_t timestamp, uint32_t ssrc){
+
+ rtp_data[0]=(unsigned char)0x80;
+ rtp_data[1]=(unsigned char)(payloadType & 0xFF);
+ rtp_data[2]=(unsigned char)((seqNo>>8)&0xFF);
+ rtp_data[3]=(unsigned char)((seqNo)&0xFF);
+ rtp_data[4]=(unsigned char)((timestamp>>24)&0xFF);
+ rtp_data[5]=(unsigned char)((timestamp>>16)&0xFF);
+
+ rtp_data[6]=(unsigned char)((timestamp>>8)&0xFF);
+ rtp_data[7]=(unsigned char)(timestamp & 0xFF);
+
+ rtp_data[8]=(unsigned char)((ssrc>>24)&0xFF);
+ rtp_data[9]=(unsigned char)((ssrc>>16)&0xFF);
+
+ rtp_data[10]=(unsigned char)((ssrc>>8)&0xFF);
+ rtp_data[11]=(unsigned char)(ssrc & 0xFF);
+}
+
+
+int makeRedundantHeader(unsigned char* rtp_data, int *payloadType, int numPayloads, uint32_t *timestamp, uint16_t *blockLen,
+ int seqNo, uint32_t ssrc)
+{
+
+ int i;
+ unsigned char *rtpPointer;
+ uint16_t offset;
+
+ /* first create "standard" RTP header */
+ makeRTPheader(rtp_data, NETEQ_CODEC_RED_PT, seqNo, timestamp[numPayloads-1], ssrc);
+
+ rtpPointer = &rtp_data[12];
+
+ /* add one sub-header for each redundant payload (not the primary) */
+ for(i=0; i<numPayloads-1; i++) { /* |0 1 2 3 4 5 6 7| */
+ if(blockLen[i] > 0) {
+ offset = (uint16_t) (timestamp[numPayloads-1] - timestamp[i]);
+
+ rtpPointer[0] = (unsigned char) ( 0x80 | (0x7F & payloadType[i]) ); /* |F| block PT | */
+ rtpPointer[1] = (unsigned char) ((offset >> 6) & 0xFF); /* | timestamp- | */
+ rtpPointer[2] = (unsigned char) ( ((offset & 0x3F)<<2) |
+ ( (blockLen[i]>>8) & 0x03 ) ); /* | -offset |bl-| */
+ rtpPointer[3] = (unsigned char) ( blockLen[i] & 0xFF ); /* | -ock length | */
+
+ rtpPointer += 4;
+ }
+ }
+
+ /* last sub-header */
+ rtpPointer[0]= (unsigned char) (0x00 | (0x7F&payloadType[numPayloads-1]));/* |F| block PT | */
+ rtpPointer += 1;
+
+ return(rtpPointer - rtp_data); /* length of header in bytes */
+}
+
+
+
+int makeDTMFpayload(unsigned char* payload_data, int Event, int End, int Volume, int Duration) {
+ unsigned char E,R,V;
+ R=0;
+ V=(unsigned char)Volume;
+ if (End==0) {
+ E = 0x00;
+ } else {
+ E = 0x80;
+ }
+ payload_data[0]=(unsigned char)Event;
+ payload_data[1]=(unsigned char)(E|R|V);
+ //Duration equals 8 times time_ms, default is 8000 Hz.
+ payload_data[2]=(unsigned char)((Duration>>8)&0xFF);
+ payload_data[3]=(unsigned char)(Duration&0xFF);
+ return(4);
+}
+
+void stereoDeInterleave(int16_t* audioSamples, int numSamples)
+{
+
+ int16_t *tempVec;
+ int16_t *readPtr, *writeL, *writeR;
+
+ if (numSamples <= 0)
+ return;
+
+ tempVec = (int16_t *) malloc(sizeof(int16_t) * numSamples);
+ if (tempVec == NULL) {
+ printf("Error allocating memory\n");
+ exit(0);
+ }
+
+ memcpy(tempVec, audioSamples, numSamples*sizeof(int16_t));
+
+ writeL = audioSamples;
+ writeR = &audioSamples[numSamples/2];
+ readPtr = tempVec;
+
+ for (int k = 0; k < numSamples; k += 2)
+ {
+ *writeL = *readPtr;
+ readPtr++;
+ *writeR = *readPtr;
+ readPtr++;
+ writeL++;
+ writeR++;
+ }
+
+ free(tempVec);
+
+}
+
+
+void stereoInterleave(unsigned char* data, int dataLen, int stride)
+{
+
+ unsigned char *ptrL, *ptrR;
+ unsigned char temp[10];
+
+ if (stride > 10)
+ {
+ exit(0);
+ }
+
+ if (dataLen%1 != 0)
+ {
+ // must be even number of samples
+ printf("Error: cannot interleave odd sample number\n");
+ exit(0);
+ }
+
+ ptrL = data + stride;
+ ptrR = &data[dataLen/2];
+
+ while (ptrL < ptrR) {
+ // copy from right pointer to temp
+ memcpy(temp, ptrR, stride);
+
+ // shift data between pointers
+ memmove(ptrL + stride, ptrL, ptrR - ptrL);
+
+ // copy from temp to left pointer
+ memcpy(ptrL, temp, stride);
+
+ // advance pointers
+ ptrL += stride*2;
+ ptrR += stride;
+ }
+
+}
diff --git a/webrtc/modules/audio_coding/neteq/test/RTPjitter.cc b/webrtc/modules/audio_coding/neteq/test/RTPjitter.cc
new file mode 100644
index 0000000..eeb4c90
--- /dev/null
+++ b/webrtc/modules/audio_coding/neteq/test/RTPjitter.cc
@@ -0,0 +1,217 @@
+/*
+ * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
+ *
+ * Use of this source code is governed by a BSD-style license
+ * that can be found in the LICENSE file in the root of the source
+ * tree. An additional intellectual property rights grant can be found
+ * in the file PATENTS. All contributing project authors may
+ * be found in the AUTHORS file in the root of the source tree.
+ */
+
+//TODO(hlundin): Reformat file to meet style guide.
+
+/* header includes */
+#include <float.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#ifdef WIN32
+#include <winsock2.h>
+#include <io.h>
+#endif
+#ifdef WEBRTC_LINUX
+#include <netinet/in.h>
+#endif
+
+#include <assert.h>
+
+#include "gtest/gtest.h"
+#include "webrtc/typedefs.h"
+
+/*********************/
+/* Misc. definitions */
+/*********************/
+
+#define FIRSTLINELEN 40
+#define CHECK_NOT_NULL(a) if((a)==NULL){fprintf(stderr,"\n %s \n line: %d \nerror at %s\n",__FILE__,__LINE__,#a );return(-1);}
+
+struct arr_time {
+ float time;
+ uint32_t ix;
+};
+
+int filelen(FILE *fid)
+{
+ fpos_t cur_pos;
+ int len;
+
+ if (!fid || fgetpos(fid, &cur_pos)) {
+ return(-1);
+ }
+
+ fseek(fid, 0, SEEK_END);
+ len = ftell(fid);
+
+ fsetpos(fid, &cur_pos);
+
+ return (len);
+}
+
+int compare_arr_time(const void *x, const void *y);
+
+int main(int argc, char* argv[])
+{
+ unsigned int dat_len, rtp_len, Npack, k;
+ arr_time *time_vec;
+ char firstline[FIRSTLINELEN];
+ unsigned char* rtp_vec = NULL;
+ unsigned char** packet_ptr = NULL;
+ unsigned char* temp_packet = NULL;
+ const unsigned int kRtpDumpHeaderSize = 4 + 4 + 4 + 2 + 2;
+ uint16_t len;
+ uint32_t *offset;
+
+/* check number of parameters */
+ if (argc != 4) {
+ /* print help text and exit */
+ printf("Apply jitter on RTP stream.\n");
+ printf("The program reads an RTP stream and packet timing from two files.\n");
+ printf("The RTP stream is modified to have the same jitter as described in the timing files.\n");
+ printf("The format of the RTP stream file should be the same as for rtpplay,\n");
+ printf("and can be obtained e.g., from Ethereal by using\n");
+ printf("Statistics -> RTP -> Show All Streams -> [select a stream] -> Save As\n\n");
+ printf("Usage:\n\n");
+ printf("%s RTP_infile dat_file RTP_outfile\n", argv[0]);
+ printf("where:\n");
+
+ printf("RTP_infile : RTP stream input file\n\n");
+
+ printf("dat_file : file with packet arrival times in ms\n\n");
+
+ printf("RTP_outfile : RTP stream output file\n\n");
+
+ return(0);
+ }
+
+ FILE* in_file=fopen(argv[1],"rb");
+ CHECK_NOT_NULL(in_file);
+ printf("Input file: %s\n",argv[1]);
+ FILE* dat_file=fopen(argv[2],"rb");
+ CHECK_NOT_NULL(dat_file);
+ printf("Dat-file: %s\n",argv[2]);
+ FILE* out_file=fopen(argv[3],"wb");
+ CHECK_NOT_NULL(out_file);
+ printf("Output file: %s\n\n",argv[3]);
+
+ time_vec = (arr_time *) malloc(sizeof(arr_time)*(filelen(dat_file)/sizeof(float)) + 1000); // add 1000 bytes to avoid (rare) strange error
+ if (time_vec==NULL) {
+ fprintf(stderr, "Error: could not allocate memory for reading dat file\n");
+ goto closing;
+ }
+
+ dat_len=0;
+ while(fread(&(time_vec[dat_len].time),sizeof(float),1,dat_file)>0) {
+ time_vec[dat_len].ix=dat_len;
+ dat_len++;
+ }
+
+ if (dat_len == 0) {
+ fprintf(stderr, "Error: dat_file is empty, no arrival time is given.\n");
+ goto closing;
+ }
+
+ qsort(time_vec,dat_len,sizeof(arr_time),compare_arr_time);
+
+
+ rtp_vec = (unsigned char *) malloc(sizeof(unsigned char)*filelen(in_file));
+ if (rtp_vec==NULL) {
+ fprintf(stderr,"Error: could not allocate memory for reading rtp file\n");
+ goto closing;
+ }
+
+ // read file header and write directly to output file
+ EXPECT_TRUE(fgets(firstline, FIRSTLINELEN, in_file) != NULL);
+ EXPECT_GT(fputs(firstline, out_file), 0);
+ EXPECT_EQ(kRtpDumpHeaderSize, fread(firstline, 1, kRtpDumpHeaderSize,
+ in_file));
+ EXPECT_EQ(kRtpDumpHeaderSize, fwrite(firstline, 1, kRtpDumpHeaderSize,
+ out_file));
+
+ // read all RTP packets into vector
+ rtp_len=0;
+ Npack=0;
+ len=(uint16_t) fread(&rtp_vec[rtp_len], sizeof(unsigned char), 2, in_file); // read length of first packet
+ while(len==2) {
+ len = ntohs(*((uint16_t *)(rtp_vec + rtp_len)));
+ rtp_len += 2;
+ if(fread(&rtp_vec[rtp_len], sizeof(unsigned char), len-2, in_file)!=(unsigned) (len-2)) {
+ fprintf(stderr,"Error: currupt packet length\n");
+ goto closing;
+ }
+ rtp_len += len-2;
+ Npack++;
+ len=(uint16_t) fread(&rtp_vec[rtp_len], sizeof(unsigned char), 2, in_file); // read length of next packet
+ }
+
+ if (Npack == 0) {
+ fprintf(stderr, "Error: No RTP packet found.\n");
+ goto closing;
+ }
+
+ packet_ptr = (unsigned char **) malloc(Npack*sizeof(unsigned char*));
+
+ packet_ptr[0]=rtp_vec;
+ k=1;
+ while(k<Npack) {
+ len = ntohs(*((uint16_t *) packet_ptr[k-1]));
+ packet_ptr[k]=packet_ptr[k-1]+len;
+ k++;
+ }
+
+ for(k=0; k<dat_len && k<Npack; k++) {
+ if(time_vec[k].time < FLT_MAX && time_vec[k].ix < Npack){
+ temp_packet = packet_ptr[time_vec[k].ix];
+ offset = (uint32_t *) (temp_packet+4);
+ if ( time_vec[k].time >= 0 ) {
+ *offset = htonl((uint32_t) time_vec[k].time);
+ }
+ else {
+ *offset = htonl((uint32_t) 0);
+ fprintf(stderr, "Warning: negative receive time in dat file transformed to 0.\n");
+ }
+
+ // write packet to file
+ if (fwrite(temp_packet, sizeof(unsigned char),
+ ntohs(*((uint16_t*) temp_packet)),
+ out_file) !=
+ ntohs(*((uint16_t*) temp_packet))) {
+ return -1;
+ }
+ }
+ }
+
+
+closing:
+ free(time_vec);
+ free(rtp_vec);
+ if (packet_ptr != NULL) {
+ free(packet_ptr);
+ }
+ fclose(in_file);
+ fclose(dat_file);
+ fclose(out_file);
+
+ return(0);
+}
+
+
+
+int compare_arr_time(const void *xp, const void *yp) {
+
+ if(((arr_time *)xp)->time == ((arr_time *)yp)->time)
+ return(0);
+ else if(((arr_time *)xp)->time > ((arr_time *)yp)->time)
+ return(1);
+
+ return(-1);
+}
diff --git a/webrtc/modules/audio_coding/neteq/test/RTPtimeshift.cc b/webrtc/modules/audio_coding/neteq/test/RTPtimeshift.cc
new file mode 100644
index 0000000..15ffdf6
--- /dev/null
+++ b/webrtc/modules/audio_coding/neteq/test/RTPtimeshift.cc
@@ -0,0 +1,99 @@
+/*
+ * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
+ *
+ * Use of this source code is governed by a BSD-style license
+ * that can be found in the LICENSE file in the root of the source
+ * tree. An additional intellectual property rights grant can be found
+ * in the file PATENTS. All contributing project authors may
+ * be found in the AUTHORS file in the root of the source tree.
+ */
+
+#include <algorithm>
+#include <stdio.h>
+#include <vector>
+
+#include "NETEQTEST_RTPpacket.h"
+#include "gtest/gtest.h"
+
+/*********************/
+/* Misc. definitions */
+/*********************/
+
+#define FIRSTLINELEN 40
+
+
+int main(int argc, char* argv[])
+{
+ if(argc < 4 || argc > 6)
+ {
+ printf("Usage: RTPtimeshift in.rtp out.rtp newStartTS [newStartSN [newStartArrTime]]\n");
+ exit(1);
+ }
+
+ FILE *inFile=fopen(argv[1],"rb");
+ if (!inFile)
+ {
+ printf("Cannot open input file %s\n", argv[1]);
+ return(-1);
+ }
+ printf("Input RTP file: %s\n",argv[1]);
+
+ FILE *outFile=fopen(argv[2],"wb");
+ if (!outFile)
+ {
+ printf("Cannot open output file %s\n", argv[2]);
+ return(-1);
+ }
+ printf("Output RTP file: %s\n\n",argv[2]);
+
+ // read file header and write directly to output file
+ const unsigned int kRtpDumpHeaderSize = 4 + 4 + 4 + 2 + 2;
+ char firstline[FIRSTLINELEN];
+ EXPECT_TRUE(fgets(firstline, FIRSTLINELEN, inFile) != NULL);
+ EXPECT_GT(fputs(firstline, outFile), 0);
+ EXPECT_EQ(kRtpDumpHeaderSize,
+ fread(firstline, 1, kRtpDumpHeaderSize, inFile));
+ EXPECT_EQ(kRtpDumpHeaderSize,
+ fwrite(firstline, 1, kRtpDumpHeaderSize, outFile));
+ NETEQTEST_RTPpacket packet;
+ int packLen = packet.readFromFile(inFile);
+ if (packLen < 0)
+ {
+ exit(1);
+ }
+
+ // get new start TS and start SeqNo from arguments
+ uint32_t TSdiff = atoi(argv[3]) - packet.timeStamp();
+ uint16_t SNdiff = 0;
+ uint32_t ATdiff = 0;
+ if (argc > 4)
+ {
+ int startSN = atoi(argv[4]);
+ if (startSN >= 0)
+ SNdiff = startSN - packet.sequenceNumber();
+ if (argc > 5)
+ {
+ int startTS = atoi(argv[5]);
+ if (startTS >= 0)
+ ATdiff = startTS - packet.time();
+ }
+ }
+
+ while (packLen >= 0)
+ {
+
+ packet.setTimeStamp(packet.timeStamp() + TSdiff);
+ packet.setSequenceNumber(packet.sequenceNumber() + SNdiff);
+ packet.setTime(packet.time() + ATdiff);
+
+ packet.writeToFile(outFile);
+
+ packLen = packet.readFromFile(inFile);
+
+ }
+
+ fclose(inFile);
+ fclose(outFile);
+
+ return 0;
+}
diff --git a/webrtc/modules/audio_coding/neteq/test/audio_classifier_test.cc b/webrtc/modules/audio_coding/neteq/test/audio_classifier_test.cc
new file mode 100644
index 0000000..aa2b61d
--- /dev/null
+++ b/webrtc/modules/audio_coding/neteq/test/audio_classifier_test.cc
@@ -0,0 +1,105 @@
+/*
+ * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
+ *
+ * Use of this source code is governed by a BSD-style license
+ * that can be found in the LICENSE file in the root of the source
+ * tree. An additional intellectual property rights grant can be found
+ * in the file PATENTS. All contributing project authors may
+ * be found in the AUTHORS file in the root of the source tree.
+ */
+
+#include "webrtc/modules/audio_coding/neteq/audio_classifier.h"
+
+#include <math.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <string>
+#include <iostream>
+
+#include "webrtc/system_wrappers/interface/scoped_ptr.h"
+
+int main(int argc, char* argv[]) {
+ if (argc != 5) {
+ std::cout << "Usage: " << argv[0] <<
+ " channels output_type <input file name> <output file name> "
+ << std::endl << std::endl;
+ std::cout << "Where channels can be 1 (mono) or 2 (interleaved stereo),";
+ std::cout << " outputs can be 1 (classification (boolean)) or 2";
+ std::cout << " (classification and music probability (float)),"
+ << std::endl;
+ std::cout << "and the sampling frequency is assumed to be 48 kHz."
+ << std::endl;
+ return -1;
+ }
+
+ const int kFrameSizeSamples = 960;
+ int channels = atoi(argv[1]);
+ if (channels < 1 || channels > 2) {
+ std::cout << "Disallowed number of channels " << channels << std::endl;
+ return -1;
+ }
+
+ int outputs = atoi(argv[2]);
+ if (outputs < 1 || outputs > 2) {
+ std::cout << "Disallowed number of outputs " << outputs << std::endl;
+ return -1;
+ }
+
+ const int data_size = channels * kFrameSizeSamples;
+ webrtc::scoped_ptr<int16_t[]> in(new int16_t[data_size]);
+
+ std::string input_filename = argv[3];
+ std::string output_filename = argv[4];
+
+ std::cout << "Input file: " << input_filename << std::endl;
+ std::cout << "Output file: " << output_filename << std::endl;
+
+ FILE* in_file = fopen(input_filename.c_str(), "rb");
+ if (!in_file) {
+ std::cout << "Cannot open input file " << input_filename << std::endl;
+ return -1;
+ }
+
+ FILE* out_file = fopen(output_filename.c_str(), "wb");
+ if (!out_file) {
+ std::cout << "Cannot open output file " << output_filename << std::endl;
+ return -1;
+ }
+
+ webrtc::AudioClassifier classifier;
+ int frame_counter = 0;
+ int music_counter = 0;
+ while (fread(in.get(), sizeof(*in.get()),
+ data_size, in_file) == (size_t) data_size) {
+ bool is_music = classifier.Analysis(in.get(), data_size, channels);
+ if (!fwrite(&is_music, sizeof(is_music), 1, out_file)) {
+ std::cout << "Error writing." << std::endl;
+ return -1;
+ }
+ if (is_music) {
+ music_counter++;
+ }
+ std::cout << "frame " << frame_counter << " decision " << is_music;
+ if (outputs == 2) {
+ float music_prob = classifier.music_probability();
+ if (!fwrite(&music_prob, sizeof(music_prob), 1, out_file)) {
+ std::cout << "Error writing." << std::endl;
+ return -1;
+ }
+ std::cout << " music prob " << music_prob;
+ }
+ std::cout << std::endl;
+ frame_counter++;
+ }
+ std::cout << frame_counter << " frames processed." << std::endl;
+ if (frame_counter > 0) {
+ float music_percentage = music_counter / static_cast<float>(frame_counter);
+ std::cout << music_percentage << " percent music." << std::endl;
+ }
+
+ fclose(in_file);
+ fclose(out_file);
+ return 0;
+}
diff --git a/webrtc/modules/audio_coding/neteq/test/delay_tool/parse_delay_file.m b/webrtc/modules/audio_coding/neteq/test/delay_tool/parse_delay_file.m
new file mode 100644
index 0000000..77b394f
--- /dev/null
+++ b/webrtc/modules/audio_coding/neteq/test/delay_tool/parse_delay_file.m
@@ -0,0 +1,191 @@
+function outStruct = parse_delay_file(file)
+
+fid = fopen(file, 'rb');
+if fid == -1
+ error('Cannot open file %s', file);
+end
+
+textline = fgetl(fid);
+if ~strncmp(textline, '#!NetEQ_Delay_Logging', 21)
+ error('Wrong file format');
+end
+
+ver = sscanf(textline, '#!NetEQ_Delay_Logging%d.%d');
+if ~all(ver == [2; 0])
+ error('Wrong version of delay logging function')
+end
+
+
+start_pos = ftell(fid);
+fseek(fid, -12, 'eof');
+textline = fgetl(fid);
+if ~strncmp(textline, 'End of file', 21)
+ error('File ending is not correct. Seems like the simulation ended abnormally.');
+end
+
+fseek(fid,-12-4, 'eof');
+Npackets = fread(fid, 1, 'int32');
+fseek(fid, start_pos, 'bof');
+
+rtpts = zeros(Npackets, 1);
+seqno = zeros(Npackets, 1);
+pt = zeros(Npackets, 1);
+plen = zeros(Npackets, 1);
+recin_t = nan*ones(Npackets, 1);
+decode_t = nan*ones(Npackets, 1);
+playout_delay = zeros(Npackets, 1);
+optbuf = zeros(Npackets, 1);
+
+fs_ix = 1;
+clock = 0;
+ts_ix = 1;
+ended = 0;
+late_packets = 0;
+fs_now = 8000;
+last_decode_k = 0;
+tot_expand = 0;
+tot_accelerate = 0;
+tot_preemptive = 0;
+
+while not(ended)
+ signal = fread(fid, 1, '*int32');
+
+ switch signal
+ case 3 % NETEQ_DELAY_LOGGING_SIGNAL_CLOCK
+ clock = fread(fid, 1, '*float32');
+
+ % keep on reading batches of M until the signal is no longer "3"
+ % read int32 + float32 in one go
+ % this is to save execution time
+ temp = [3; 0];
+ M = 120;
+ while all(temp(1,:) == 3)
+ fp = ftell(fid);
+ temp = fread(fid, [2 M], '*int32');
+ end
+
+ % back up to last clock event
+ fseek(fid, fp - ftell(fid) + ...
+ (find(temp(1,:) ~= 3, 1 ) - 2) * 2 * 4 + 4, 'cof');
+ % read the last clock value
+ clock = fread(fid, 1, '*float32');
+
+ case 1 % NETEQ_DELAY_LOGGING_SIGNAL_RECIN
+ temp_ts = fread(fid, 1, 'uint32');
+
+ if late_packets > 0
+ temp_ix = ts_ix - 1;
+ while (temp_ix >= 1) && (rtpts(temp_ix) ~= temp_ts)
+ % TODO(hlundin): use matlab vector search instead?
+ temp_ix = temp_ix - 1;
+ end
+
+ if temp_ix >= 1
+ % the ts was found in the vector
+ late_packets = late_packets - 1;
+ else
+ temp_ix = ts_ix;
+ ts_ix = ts_ix + 1;
+ end
+ else
+ temp_ix = ts_ix;
+ ts_ix = ts_ix + 1;
+ end
+
+ rtpts(temp_ix) = temp_ts;
+ seqno(temp_ix) = fread(fid, 1, 'uint16');
+ pt(temp_ix) = fread(fid, 1, 'int32');
+ plen(temp_ix) = fread(fid, 1, 'int16');
+ recin_t(temp_ix) = clock;
+
+ case 2 % NETEQ_DELAY_LOGGING_SIGNAL_FLUSH
+ % do nothing
+
+ case 4 % NETEQ_DELAY_LOGGING_SIGNAL_EOF
+ ended = 1;
+
+ case 5 % NETEQ_DELAY_LOGGING_SIGNAL_DECODE
+ last_decode_ts = fread(fid, 1, 'uint32');
+ temp_delay = fread(fid, 1, 'uint16');
+
+ k = find(rtpts(1:(ts_ix - 1))==last_decode_ts,1,'last');
+ if ~isempty(k)
+ decode_t(k) = clock;
+ playout_delay(k) = temp_delay + ...
+ 5 * fs_now / 8000; % add overlap length
+ last_decode_k = k;
+ end
+
+ case 6 % NETEQ_DELAY_LOGGING_SIGNAL_CHANGE_FS
+ fsvec(fs_ix) = fread(fid, 1, 'uint16');
+ fschange_ts(fs_ix) = last_decode_ts;
+ fs_now = fsvec(fs_ix);
+ fs_ix = fs_ix + 1;
+
+ case 7 % NETEQ_DELAY_LOGGING_SIGNAL_MERGE_INFO
+ playout_delay(last_decode_k) = playout_delay(last_decode_k) ...
+ + fread(fid, 1, 'int32');
+
+ case 8 % NETEQ_DELAY_LOGGING_SIGNAL_EXPAND_INFO
+ temp = fread(fid, 1, 'int32');
+ if last_decode_k ~= 0
+ tot_expand = tot_expand + temp / (fs_now / 1000);
+ end
+
+ case 9 % NETEQ_DELAY_LOGGING_SIGNAL_ACCELERATE_INFO
+ temp = fread(fid, 1, 'int32');
+ if last_decode_k ~= 0
+ tot_accelerate = tot_accelerate + temp / (fs_now / 1000);
+ end
+
+ case 10 % NETEQ_DELAY_LOGGING_SIGNAL_PREEMPTIVE_INFO
+ temp = fread(fid, 1, 'int32');
+ if last_decode_k ~= 0
+ tot_preemptive = tot_preemptive + temp / (fs_now / 1000);
+ end
+
+ case 11 % NETEQ_DELAY_LOGGING_SIGNAL_OPTBUF
+ optbuf(last_decode_k) = fread(fid, 1, 'int32');
+
+ case 12 % NETEQ_DELAY_LOGGING_SIGNAL_DECODE_ONE_DESC
+ last_decode_ts = fread(fid, 1, 'uint32');
+ k = ts_ix - 1;
+
+ while (k >= 1) && (rtpts(k) ~= last_decode_ts)
+ % TODO(hlundin): use matlab vector search instead?
+ k = k - 1;
+ end
+
+ if k < 1
+ % packet not received yet
+ k = ts_ix;
+ rtpts(ts_ix) = last_decode_ts;
+ late_packets = late_packets + 1;
+ end
+
+ decode_t(k) = clock;
+ playout_delay(k) = fread(fid, 1, 'uint16') + ...
+ 5 * fs_now / 8000; % add overlap length
+ last_decode_k = k;
+
+ end
+
+end
+
+
+fclose(fid);
+
+outStruct = struct(...
+ 'ts', rtpts, ...
+ 'sn', seqno, ...
+ 'pt', pt,...
+ 'plen', plen,...
+ 'arrival', recin_t,...
+ 'decode', decode_t,...
+ 'fs', fsvec(:),...
+ 'fschange_ts', fschange_ts(:),...
+ 'playout_delay', playout_delay,...
+ 'tot_expand', tot_expand,...
+ 'tot_accelerate', tot_accelerate,...
+ 'tot_preemptive', tot_preemptive,...
+ 'optbuf', optbuf);
diff --git a/webrtc/modules/audio_coding/neteq/test/delay_tool/plot_neteq_delay.m b/webrtc/modules/audio_coding/neteq/test/delay_tool/plot_neteq_delay.m
new file mode 100644
index 0000000..bc1c85a
--- /dev/null
+++ b/webrtc/modules/audio_coding/neteq/test/delay_tool/plot_neteq_delay.m
@@ -0,0 +1,187 @@
+function [delay_struct, delayvalues] = plot_neteq_delay(delayfile, varargin)
+
+% InfoStruct = plot_neteq_delay(delayfile)
+% InfoStruct = plot_neteq_delay(delayfile, 'skipdelay', skip_seconds)
+%
+% Henrik Lundin, 2006-11-17
+% Henrik Lundin, 2011-05-17
+%
+
+try
+ s = parse_delay_file(delayfile);
+catch
+ error(lasterr);
+end
+
+delayskip=0;
+noplot=0;
+arg_ptr=1;
+delaypoints=[];
+
+s.sn=unwrap_seqno(s.sn);
+
+while arg_ptr+1 <= nargin
+ switch lower(varargin{arg_ptr})
+ case {'skipdelay', 'delayskip'}
+ % skip a number of seconds in the beginning when calculating delays
+ delayskip = varargin{arg_ptr+1};
+ arg_ptr = arg_ptr + 2;
+ case 'noplot'
+ noplot=1;
+ arg_ptr = arg_ptr + 1;
+ case {'get_delay', 'getdelay'}
+ % return a vector of delay values for the points in the given vector
+ delaypoints = varargin{arg_ptr+1};
+ arg_ptr = arg_ptr + 2;
+ otherwise
+ warning('Unknown switch %s\n', varargin{arg_ptr});
+ arg_ptr = arg_ptr + 1;
+ end
+end
+
+% find lost frames that were covered by one-descriptor decoding
+one_desc_ix=find(isnan(s.arrival));
+for k=1:length(one_desc_ix)
+ ix=find(s.ts==max(s.ts(s.ts(one_desc_ix(k))>s.ts)));
+ s.sn(one_desc_ix(k))=s.sn(ix)+1;
+ s.pt(one_desc_ix(k))=s.pt(ix);
+ s.arrival(one_desc_ix(k))=s.arrival(ix)+s.decode(one_desc_ix(k))-s.decode(ix);
+end
+
+% remove duplicate received frames that were never decoded (RED codec)
+if length(unique(s.ts(isfinite(s.ts)))) < length(s.ts(isfinite(s.ts)))
+ ix=find(isfinite(s.decode));
+ s.sn=s.sn(ix);
+ s.ts=s.ts(ix);
+ s.arrival=s.arrival(ix);
+ s.playout_delay=s.playout_delay(ix);
+ s.pt=s.pt(ix);
+ s.optbuf=s.optbuf(ix);
+ plen=plen(ix);
+ s.decode=s.decode(ix);
+end
+
+% find non-unique sequence numbers
+[~,un_ix]=unique(s.sn);
+nonun_ix=setdiff(1:length(s.sn),un_ix);
+if ~isempty(nonun_ix)
+ warning('RTP sequence numbers are in error');
+end
+
+% sort vectors
+[s.sn,sort_ix]=sort(s.sn);
+s.ts=s.ts(sort_ix);
+s.arrival=s.arrival(sort_ix);
+s.decode=s.decode(sort_ix);
+s.playout_delay=s.playout_delay(sort_ix);
+s.pt=s.pt(sort_ix);
+
+send_t=s.ts-s.ts(1);
+if length(s.fs)<1
+ warning('No info about sample rate found in file. Using default 8000.');
+ s.fs(1)=8000;
+ s.fschange_ts(1)=min(s.ts);
+elseif s.fschange_ts(1)>min(s.ts)
+ s.fschange_ts(1)=min(s.ts);
+end
+
+end_ix=length(send_t);
+for k=length(s.fs):-1:1
+ start_ix=find(s.ts==s.fschange_ts(k));
+ send_t(start_ix:end_ix)=send_t(start_ix:end_ix)/s.fs(k)*1000;
+ s.playout_delay(start_ix:end_ix)=s.playout_delay(start_ix:end_ix)/s.fs(k)*1000;
+ s.optbuf(start_ix:end_ix)=s.optbuf(start_ix:end_ix)/s.fs(k)*1000;
+ end_ix=start_ix-1;
+end
+
+tot_time=max(send_t)-min(send_t);
+
+seq_ix=s.sn-min(s.sn)+1;
+send_t=send_t+max(min(s.arrival-send_t),0);
+
+plot_send_t=nan*ones(max(seq_ix),1);
+plot_send_t(seq_ix)=send_t;
+plot_nw_delay=nan*ones(max(seq_ix),1);
+plot_nw_delay(seq_ix)=s.arrival-send_t;
+
+cng_ix=find(s.pt~=13); % find those packets that are not CNG/SID
+
+if noplot==0
+ h=plot(plot_send_t/1000,plot_nw_delay);
+ set(h,'color',0.75*[1 1 1]);
+ hold on
+ if any(s.optbuf~=0)
+ peak_ix=find(s.optbuf(cng_ix)<0); % peak mode is labeled with negative values
+ no_peak_ix=find(s.optbuf(cng_ix)>0); %setdiff(1:length(cng_ix),peak_ix);
+ h1=plot(send_t(cng_ix(peak_ix))/1000,...
+ s.arrival(cng_ix(peak_ix))+abs(s.optbuf(cng_ix(peak_ix)))-send_t(cng_ix(peak_ix)),...
+ 'r.');
+ h2=plot(send_t(cng_ix(no_peak_ix))/1000,...
+ s.arrival(cng_ix(no_peak_ix))+abs(s.optbuf(cng_ix(no_peak_ix)))-send_t(cng_ix(no_peak_ix)),...
+ 'g.');
+ set([h1, h2],'markersize',1)
+ end
+ %h=plot(send_t(seq_ix)/1000,s.decode+s.playout_delay-send_t(seq_ix));
+ h=plot(send_t(cng_ix)/1000,s.decode(cng_ix)+s.playout_delay(cng_ix)-send_t(cng_ix));
+ set(h,'linew',1.5);
+ hold off
+ ax1=axis;
+ axis tight
+ ax2=axis;
+ axis([ax2(1:3) ax1(4)])
+end
+
+
+% calculate delays and other parameters
+
+delayskip_ix = find(send_t-send_t(1)>=delayskip*1000, 1 );
+
+use_ix = intersect(cng_ix,... % use those that are not CNG/SID frames...
+ intersect(find(isfinite(s.decode)),... % ... that did arrive ...
+ (delayskip_ix:length(s.decode))')); % ... and are sent after delayskip seconds
+
+mean_delay = mean(s.decode(use_ix)+s.playout_delay(use_ix)-send_t(use_ix));
+neteq_delay = mean(s.decode(use_ix)+s.playout_delay(use_ix)-s.arrival(use_ix));
+
+Npack=max(s.sn(delayskip_ix:end))-min(s.sn(delayskip_ix:end))+1;
+nw_lossrate=(Npack-length(s.sn(delayskip_ix:end)))/Npack;
+neteq_lossrate=(length(s.sn(delayskip_ix:end))-length(use_ix))/Npack;
+
+delay_struct=struct('mean_delay',mean_delay,'neteq_delay',neteq_delay,...
+ 'nw_lossrate',nw_lossrate,'neteq_lossrate',neteq_lossrate,...
+ 'tot_expand',round(s.tot_expand),'tot_accelerate',round(s.tot_accelerate),...
+ 'tot_preemptive',round(s.tot_preemptive),'tot_time',tot_time,...
+ 'filename',delayfile,'units','ms','fs',unique(s.fs));
+
+if not(isempty(delaypoints))
+ delayvalues=interp1(send_t(cng_ix),...
+ s.decode(cng_ix)+s.playout_delay(cng_ix)-send_t(cng_ix),...
+ delaypoints,'nearest',NaN);
+else
+ delayvalues=[];
+end
+
+
+
+% SUBFUNCTIONS %
+
+function y=unwrap_seqno(x)
+
+jumps=find(abs((diff(x)-1))>65000);
+
+while ~isempty(jumps)
+ n=jumps(1);
+ if x(n+1)-x(n) < 0
+ % negative jump
+ x(n+1:end)=x(n+1:end)+65536;
+ else
+ % positive jump
+ x(n+1:end)=x(n+1:end)-65536;
+ end
+
+ jumps=find(abs((diff(x(n+1:end))-1))>65000);
+end
+
+y=x;
+
+return;
diff --git a/webrtc/modules/audio_coding/neteq/test/neteq_opus_fec_quality_test.cc b/webrtc/modules/audio_coding/neteq/test/neteq_opus_fec_quality_test.cc
new file mode 100644
index 0000000..ad6d8ec
--- /dev/null
+++ b/webrtc/modules/audio_coding/neteq/test/neteq_opus_fec_quality_test.cc
@@ -0,0 +1,178 @@
+/*
+ * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
+ *
+ * Use of this source code is governed by a BSD-style license
+ * that can be found in the LICENSE file in the root of the source
+ * tree. An additional intellectual property rights grant can be found
+ * in the file PATENTS. All contributing project authors may
+ * be found in the AUTHORS file in the root of the source tree.
+ */
+
+#include <gflags/gflags.h>
+#include "webrtc/modules/audio_coding/codecs/opus/interface/opus_interface.h"
+#include "webrtc/modules/audio_coding/neteq/tools/neteq_quality_test.h"
+#include "webrtc/test/testsupport/fileutils.h"
+
+using google::RegisterFlagValidator;
+using google::ParseCommandLineFlags;
+using std::string;
+using testing::InitGoogleTest;
+
+namespace webrtc {
+namespace test {
+
+static const int kOpusBlockDurationMs = 20;
+static const int kOpusInputSamplingKhz = 48;
+static const int kOpusOutputSamplingKhz = 32;
+
+static bool ValidateInFilename(const char* flagname, const string& value) {
+ FILE* fid = fopen(value.c_str(), "rb");
+ if (fid != NULL) {
+ fclose(fid);
+ return true;
+ }
+ printf("Invalid input filename.");
+ return false;
+}
+DEFINE_string(in_filename,
+ ResourcePath("audio_coding/speech_mono_32_48kHz", "pcm"),
+ "Filename for input audio (should be 48 kHz sampled raw data).");
+static const bool in_filename_dummy =
+ RegisterFlagValidator(&FLAGS_in_filename, &ValidateInFilename);
+
+static bool ValidateOutFilename(const char* flagname, const string& value) {
+ FILE* fid = fopen(value.c_str(), "wb");
+ if (fid != NULL) {
+ fclose(fid);
+ return true;
+ }
+ printf("Invalid output filename.");
+ return false;
+}
+DEFINE_string(out_filename, OutputPath() + "neteq4_opus_fec_quality_test.pcm",
+ "Name of output audio file.");
+static const bool out_filename_dummy =
+ RegisterFlagValidator(&FLAGS_out_filename, &ValidateOutFilename);
+
+static bool ValidateChannels(const char* flagname, int32_t value) {
+ if (value == 1 || value == 2)
+ return true;
+ printf("Invalid number of channels, should be either 1 or 2.");
+ return false;
+}
+DEFINE_int32(channels, 1, "Number of channels in input audio.");
+static const bool channels_dummy =
+ RegisterFlagValidator(&FLAGS_channels, &ValidateChannels);
+
+static bool ValidateBitRate(const char* flagname, int32_t value) {
+ if (value >= 6 && value <= 510)
+ return true;
+ printf("Invalid bit rate, should be between 6 and 510 kbps.");
+ return false;
+}
+DEFINE_int32(bit_rate_kbps, 32, "Target bit rate (kbps).");
+static const bool bit_rate_dummy =
+ RegisterFlagValidator(&FLAGS_bit_rate_kbps, &ValidateBitRate);
+
+static bool ValidatePacketLossRate(const char* flagname, int32_t value) {
+ if (value >= 0 && value <= 100)
+ return true;
+ printf("Invalid packet loss percentile, should be between 0 and 100.");
+ return false;
+}
+DEFINE_int32(reported_loss_rate, 10, "Reported percentile of packet loss.");
+static const bool reported_loss_rate_dummy =
+ RegisterFlagValidator(&FLAGS_reported_loss_rate, &ValidatePacketLossRate);
+DEFINE_int32(actual_loss_rate, 0, "Actual percentile of packet loss.");
+static const bool actual_loss_rate_dummy =
+ RegisterFlagValidator(&FLAGS_actual_loss_rate, &ValidatePacketLossRate);
+
+static bool ValidateRuntime(const char* flagname, int32_t value) {
+ if (value > 0)
+ return true;
+ printf("Invalid runtime, should be greater than 0.");
+ return false;
+}
+DEFINE_int32(runtime_ms, 10000, "Simulated runtime (milliseconds).");
+static const bool runtime_dummy =
+ RegisterFlagValidator(&FLAGS_runtime_ms, &ValidateRuntime);
+
+DEFINE_bool(fec, true, "Whether to enable FEC for encoding.");
+
+class NetEqOpusFecQualityTest : public NetEqQualityTest {
+ protected:
+ NetEqOpusFecQualityTest();
+ virtual void SetUp() OVERRIDE;
+ virtual void TearDown() OVERRIDE;
+ virtual int EncodeBlock(int16_t* in_data, int block_size_samples,
+ uint8_t* payload, int max_bytes);
+ virtual bool PacketLost(int packet_input_time_ms);
+ private:
+ WebRtcOpusEncInst* opus_encoder_;
+ int channels_;
+ int bit_rate_kbps_;
+ bool fec_;
+ int target_loss_rate_;
+ int actual_loss_rate_;
+};
+
+NetEqOpusFecQualityTest::NetEqOpusFecQualityTest()
+ : NetEqQualityTest(kOpusBlockDurationMs, kOpusInputSamplingKhz,
+ kOpusOutputSamplingKhz,
+ (FLAGS_channels == 1) ? kDecoderOpus : kDecoderOpus_2ch,
+ FLAGS_channels, 0.0f, FLAGS_in_filename,
+ FLAGS_out_filename),
+ opus_encoder_(NULL),
+ channels_(FLAGS_channels),
+ bit_rate_kbps_(FLAGS_bit_rate_kbps),
+ fec_(FLAGS_fec),
+ target_loss_rate_(FLAGS_reported_loss_rate),
+ actual_loss_rate_(FLAGS_actual_loss_rate) {
+}
+
+void NetEqOpusFecQualityTest::SetUp() {
+ // Create encoder memory.
+ WebRtcOpus_EncoderCreate(&opus_encoder_, channels_);
+ ASSERT_TRUE(opus_encoder_ != NULL);
+ // Set bitrate.
+ EXPECT_EQ(0, WebRtcOpus_SetBitRate(opus_encoder_, bit_rate_kbps_ * 1000));
+ if (fec_) {
+ EXPECT_EQ(0, WebRtcOpus_EnableFec(opus_encoder_));
+ EXPECT_EQ(0, WebRtcOpus_SetPacketLossRate(opus_encoder_,
+ target_loss_rate_));
+ }
+ NetEqQualityTest::SetUp();
+}
+
+void NetEqOpusFecQualityTest::TearDown() {
+ // Free memory.
+ EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
+ NetEqQualityTest::TearDown();
+}
+
+int NetEqOpusFecQualityTest::EncodeBlock(int16_t* in_data,
+ int block_size_samples,
+ uint8_t* payload, int max_bytes) {
+ int value = WebRtcOpus_Encode(opus_encoder_, in_data,
+ block_size_samples, max_bytes,
+ payload);
+ EXPECT_GT(value, 0);
+ return value;
+}
+
+bool NetEqOpusFecQualityTest::PacketLost(int packet_input_time_ms) {
+ static int packets = 0, lost_packets = 0;
+ packets++;
+ if (lost_packets * 100 < actual_loss_rate_ * packets) {
+ lost_packets++;
+ return true;
+ }
+ return false;
+}
+
+TEST_F(NetEqOpusFecQualityTest, Test) {
+ Simulate(FLAGS_runtime_ms);
+}
+
+} // namespace test
+} // namespace webrtc
diff --git a/webrtc/modules/audio_coding/neteq/test/neteq_performance_unittest.cc b/webrtc/modules/audio_coding/neteq/test/neteq_performance_unittest.cc
new file mode 100644
index 0000000..14857c7
--- /dev/null
+++ b/webrtc/modules/audio_coding/neteq/test/neteq_performance_unittest.cc
@@ -0,0 +1,41 @@
+/*
+ * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
+ *
+ * Use of this source code is governed by a BSD-style license
+ * that can be found in the LICENSE file in the root of the source
+ * tree. An additional intellectual property rights grant can be found
+ * in the file PATENTS. All contributing project authors may
+ * be found in the AUTHORS file in the root of the source tree.
+ */
+
+#include "testing/gtest/include/gtest/gtest.h"
+#include "webrtc/modules/audio_coding/neteq/tools/neteq_performance_test.h"
+#include "webrtc/test/testsupport/perf_test.h"
+#include "webrtc/typedefs.h"
+
+// Runs a test with 10% packet losses and 10% clock drift, to exercise
+// both loss concealment and time-stretching code.
+TEST(NetEqPerformanceTest, Run) {
+ const int kSimulationTimeMs = 10000000;
+ const int kLossPeriod = 10; // Drop every 10th packet.
+ const double kDriftFactor = 0.1;
+ int64_t runtime = webrtc::test::NetEqPerformanceTest::Run(
+ kSimulationTimeMs, kLossPeriod, kDriftFactor);
+ ASSERT_GT(runtime, 0);
+ webrtc::test::PrintResult(
+ "neteq_performance", "", "10_pl_10_drift", runtime, "ms", true);
+}
+
+// Runs a test with neither packet losses nor clock drift, to put
+// emphasis on the "good-weather" code path, which is presumably much
+// more lightweight.
+TEST(NetEqPerformanceTest, RunClean) {
+ const int kSimulationTimeMs = 10000000;
+ const int kLossPeriod = 0; // No losses.
+ const double kDriftFactor = 0.0; // No clock drift.
+ int64_t runtime = webrtc::test::NetEqPerformanceTest::Run(
+ kSimulationTimeMs, kLossPeriod, kDriftFactor);
+ ASSERT_GT(runtime, 0);
+ webrtc::test::PrintResult(
+ "neteq_performance", "", "0_pl_0_drift", runtime, "ms", true);
+}
diff --git a/webrtc/modules/audio_coding/neteq/test/neteq_speed_test.cc b/webrtc/modules/audio_coding/neteq/test/neteq_speed_test.cc
new file mode 100644
index 0000000..05e75f3
--- /dev/null
+++ b/webrtc/modules/audio_coding/neteq/test/neteq_speed_test.cc
@@ -0,0 +1,80 @@
+/*
+ * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
+ *
+ * Use of this source code is governed by a BSD-style license
+ * that can be found in the LICENSE file in the root of the source
+ * tree. An additional intellectual property rights grant can be found
+ * in the file PATENTS. All contributing project authors may
+ * be found in the AUTHORS file in the root of the source tree.
+ */
+
+#include <stdio.h>
+
+#include <iostream>
+
+#include "gflags/gflags.h"
+#include "webrtc/modules/audio_coding/neteq/tools/neteq_performance_test.h"
+#include "webrtc/typedefs.h"
+
+// Flag validators.
+static bool ValidateRuntime(const char* flagname, int value) {
+ if (value > 0) // Value is ok.
+ return true;
+ printf("Invalid value for --%s: %d\n", flagname, static_cast<int>(value));
+ return false;
+}
+static bool ValidateLossrate(const char* flagname, int value) {
+ if (value >= 0) // Value is ok.
+ return true;
+ printf("Invalid value for --%s: %d\n", flagname, static_cast<int>(value));
+ return false;
+}
+static bool ValidateDriftfactor(const char* flagname, double value) {
+ if (value >= 0.0 && value < 1.0) // Value is ok.
+ return true;
+ printf("Invalid value for --%s: %f\n", flagname, value);
+ return false;
+}
+
+// Define command line flags.
+DEFINE_int32(runtime_ms, 10000, "Simulated runtime in ms.");
+static const bool runtime_ms_dummy =
+ google::RegisterFlagValidator(&FLAGS_runtime_ms, &ValidateRuntime);
+DEFINE_int32(lossrate, 10,
+ "Packet lossrate; drop every N packets.");
+static const bool lossrate_dummy =
+ google::RegisterFlagValidator(&FLAGS_lossrate, &ValidateLossrate);
+DEFINE_double(drift, 0.1,
+ "Clockdrift factor.");
+static const bool drift_dummy =
+ google::RegisterFlagValidator(&FLAGS_drift, &ValidateDriftfactor);
+
+int main(int argc, char* argv[]) {
+ std::string program_name = argv[0];
+ std::string usage = "Tool for measuring the speed of NetEq.\n"
+ "Usage: " + program_name + " [options]\n\n"
+ " --runtime_ms=N runtime in ms; default is 10000 ms\n"
+ " --lossrate=N drop every N packets; default is 10\n"
+ " --drift=F clockdrift factor between 0.0 and 1.0; "
+ "default is 0.1\n";
+ google::SetUsageMessage(usage);
+ google::ParseCommandLineFlags(&argc, &argv, true);
+
+ if (argc != 1) {
+ // Print usage information.
+ std::cout << google::ProgramUsage();
+ return 0;
+ }
+
+ int64_t result =
+ webrtc::test::NetEqPerformanceTest::Run(FLAGS_runtime_ms, FLAGS_lossrate,
+ FLAGS_drift);
+ if (result <= 0) {
+ std::cout << "There was an error" << std::endl;
+ return -1;
+ }
+
+ std::cout << "Simulation done" << std::endl;
+ std::cout << "Runtime = " << result << " ms" << std::endl;
+ return 0;
+}
diff --git a/webrtc/modules/audio_coding/neteq/test/rtp_to_text.cc b/webrtc/modules/audio_coding/neteq/test/rtp_to_text.cc
new file mode 100644
index 0000000..1112d79
--- /dev/null
+++ b/webrtc/modules/audio_coding/neteq/test/rtp_to_text.cc
@@ -0,0 +1,124 @@
+/*
+ * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
+ *
+ * Use of this source code is governed by a BSD-style license
+ * that can be found in the LICENSE file in the root of the source
+ * tree. An additional intellectual property rights grant can be found
+ * in the file PATENTS. All contributing project authors may
+ * be found in the AUTHORS file in the root of the source tree.
+ */
+
+/*
+ * Parses an rtpdump file and outputs a text table parsable by parseLog.m.
+ * The output file will have .txt appended to the specified base name.
+ * $ rtp_to_text [-d] <input_rtp_file> <output_base_name>
+ *
+ * -d RTP headers only
+ *
+ */
+
+#include "data_log.h"
+#include "NETEQTEST_DummyRTPpacket.h"
+#include "NETEQTEST_RTPpacket.h"
+
+#include <stdio.h>
+#include <string.h>
+
+#include <iostream>
+#include <string>
+#include <vector>
+
+/*********************/
+/* Misc. definitions */
+/*********************/
+
+#define FIRSTLINELEN 40
+
+using ::webrtc::DataLog;
+
+int main(int argc, char* argv[])
+{
+ int arg_count = 1;
+ NETEQTEST_RTPpacket* packet;
+
+ if (argc < 3)
+ {
+ printf("Usage: %s [-d] <input_rtp_file> <output_base_name>\n", argv[0]);
+ return -1;
+ }
+
+ // Parse dummy option
+ if (argc >= 3 && strcmp(argv[arg_count], "-d") == 0)
+ {
+ packet = new NETEQTEST_DummyRTPpacket;
+ ++arg_count;
+ }
+ else
+ {
+ packet = new NETEQTEST_RTPpacket;
+ }
+
+ std::string input_filename = argv[arg_count++];
+ std::string table_name = argv[arg_count];
+
+ std::cout << "Input file: " << input_filename << std::endl;
+ std::cout << "Output file: " << table_name << ".txt" << std::endl;
+
+ FILE *inFile=fopen(input_filename.c_str(),"rb");
+ if (!inFile)
+ {
+ std::cout << "Cannot open input file " << input_filename << std::endl;
+ return -1;
+ }
+
+ // Set up the DataLog and define the table
+ DataLog::CreateLog();
+ if (DataLog::AddTable(table_name) < 0)
+ {
+ std::cout << "Error adding table " << table_name << ".txt" << std::endl;
+ return -1;
+ }
+
+ DataLog::AddColumn(table_name, "seq", 1);
+ DataLog::AddColumn(table_name, "ssrc", 1);
+ DataLog::AddColumn(table_name, "payload type", 1);
+ DataLog::AddColumn(table_name, "length", 1);
+ DataLog::AddColumn(table_name, "timestamp", 1);
+ DataLog::AddColumn(table_name, "marker bit", 1);
+ DataLog::AddColumn(table_name, "arrival", 1);
+
+ // read file header
+ char firstline[FIRSTLINELEN];
+ if (fgets(firstline, FIRSTLINELEN, inFile) == NULL)
+ {
+ std::cout << "Error reading file " << input_filename << std::endl;
+ return -1;
+ }
+
+ // start_sec + start_usec + source + port + padding
+ if (fread(firstline, 4+4+4+2+2, 1, inFile) != 1)
+ {
+ std::cout << "Error reading file " << input_filename << std::endl;
+ return -1;
+ }
+
+ while (packet->readFromFile(inFile) >= 0)
+ {
+ // write packet headers to
+ DataLog::InsertCell(table_name, "seq", packet->sequenceNumber());
+ DataLog::InsertCell(table_name, "ssrc", packet->SSRC());
+ DataLog::InsertCell(table_name, "payload type", packet->payloadType());
+ DataLog::InsertCell(table_name, "length", packet->dataLen());
+ DataLog::InsertCell(table_name, "timestamp", packet->timeStamp());
+ DataLog::InsertCell(table_name, "marker bit", packet->markerBit());
+ DataLog::InsertCell(table_name, "arrival", packet->time());
+ DataLog::NextRow(table_name);
+ return -1;
+ }
+
+ DataLog::ReturnLog();
+
+ fclose(inFile);
+
+ return 0;
+}