2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
11 #include "NETEQTEST_DummyRTPpacket.h"
20 #include <netinet/in.h> // for htons, htonl, etc
23 int NETEQTEST_DummyRTPpacket::readFromFile(FILE *fp)
30 uint16_t length, plen;
34 bool readNextPacket = true;
35 while (readNextPacket) {
36 readNextPacket = false;
37 if (fread(&length, 2, 1, fp) == 0)
42 length = ntohs(length);
44 if (fread(&plen, 2, 1, fp) == 0)
49 packetLen = ntohs(plen);
51 if (fread(&offset, 4, 1, fp) == 0)
56 // Store in local variable until we have passed the reset below.
57 uint32_t receiveTime = ntohl(offset);
59 // Use length here because a plen of 0 specifies rtcp.
60 length = (uint16_t) (length - _kRDHeaderLen);
63 if (_datagram && _memSize < length + 1)
70 // Add one extra byte, to be able to fake a dummy payload of 1 byte.
71 _datagram = new uint8_t[length + 1];
72 _memSize = length + 1;
74 memset(_datagram, 0, length + 1);
84 if (fread((unsigned short *) _datagram, 1, _kBasicHeaderLen, fp)
85 != (size_t)_kBasicHeaderLen)
90 _receiveTime = receiveTime;
91 _datagramLen = _kBasicHeaderLen;
93 // Parse the basic header
94 webrtc::WebRtcRTPHeader tempRTPinfo;
96 parseBasicHeader(&tempRTPinfo, &P, &X, &CC);
98 // Check if we have to extend the header
99 if (X != 0 || CC != 0)
101 int newLen = _kBasicHeaderLen + CC * 4 + X * 4;
102 assert(_memSize >= newLen);
104 // Read extension from file
105 size_t readLen = newLen - _kBasicHeaderLen;
106 if (fread(&_datagram[_kBasicHeaderLen], 1, readLen, fp) != readLen)
111 _datagramLen = newLen;
115 int totHdrLen = calcHeaderLength(X, CC);
116 assert(_memSize >= totHdrLen);
118 // Read extension from file
119 size_t readLen = totHdrLen - newLen;
120 if (fread(&_datagram[newLen], 1, readLen, fp) != readLen)
125 _datagramLen = totHdrLen;
128 _datagramLen = length;
130 if (!_blockList.empty() && _blockList.count(payloadType()) > 0)
132 readNextPacket = true;
137 assert(_memSize > _datagramLen);
138 _payloadLen = 1; // Set the length to 1 byte.
143 int NETEQTEST_DummyRTPpacket::writeToFile(FILE *fp)
150 uint16_t length, plen;
153 // length including RTPplay header
154 length = htons(_datagramLen + _kRDHeaderLen);
155 if (fwrite(&length, 2, 1, fp) != 1)
161 plen = htons(_datagramLen);
162 if (fwrite(&plen, 2, 1, fp) != 1)
167 // offset (=receive time)
168 offset = htonl(_receiveTime);
169 if (fwrite(&offset, 4, 1, fp) != 1)
174 // Figure out the length of the RTP header.
176 if (_datagramLen == 0)
178 // No payload at all; we are done writing to file.
184 headerLen = _payloadPtr - _datagram;
185 assert(headerLen >= 0);
189 if (fwrite((unsigned short *) _datagram, 1, headerLen, fp) !=
190 static_cast<size_t>(headerLen))
195 return (headerLen + _kRDHeaderLen); // total number of bytes written
199 void NETEQTEST_DummyRTPpacket::parseHeader() {
200 NETEQTEST_RTPpacket::parseHeader();
201 // Change _payloadLen to 1 byte. The memory should always be big enough.
202 assert(_memSize > _datagramLen);