henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 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. |
| 9 | */ |
| 10 | |
| 11 | #include "NETEQTEST_DummyRTPpacket.h" |
| 12 | |
| 13 | #include <assert.h> |
| 14 | #include <stdio.h> |
| 15 | #include <string.h> |
| 16 | |
| 17 | #ifdef WIN32 |
| 18 | #include <winsock2.h> |
| 19 | #else |
| 20 | #include <netinet/in.h> // for htons, htonl, etc |
| 21 | #endif |
| 22 | |
| 23 | int NETEQTEST_DummyRTPpacket::readFromFile(FILE *fp) |
| 24 | { |
| 25 | if (!fp) |
| 26 | { |
| 27 | return -1; |
| 28 | } |
| 29 | |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 30 | uint16_t length, plen; |
| 31 | uint32_t offset; |
fbarchard@google.com | d7b4dea | 2014-08-05 23:46:42 +0000 | [diff] [blame] | 32 | int packetLen = 0; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 33 | |
henrik.lundin@webrtc.org | 5ecdef1 | 2013-12-03 08:26:49 +0000 | [diff] [blame] | 34 | bool readNextPacket = true; |
| 35 | while (readNextPacket) { |
| 36 | readNextPacket = false; |
| 37 | if (fread(&length, 2, 1, fp) == 0) |
| 38 | { |
| 39 | reset(); |
| 40 | return -2; |
| 41 | } |
| 42 | length = ntohs(length); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 43 | |
henrik.lundin@webrtc.org | 5ecdef1 | 2013-12-03 08:26:49 +0000 | [diff] [blame] | 44 | if (fread(&plen, 2, 1, fp) == 0) |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 45 | { |
| 46 | reset(); |
| 47 | return -1; |
| 48 | } |
henrik.lundin@webrtc.org | 5ecdef1 | 2013-12-03 08:26:49 +0000 | [diff] [blame] | 49 | packetLen = ntohs(plen); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 50 | |
henrik.lundin@webrtc.org | 5ecdef1 | 2013-12-03 08:26:49 +0000 | [diff] [blame] | 51 | if (fread(&offset, 4, 1, fp) == 0) |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 52 | { |
henrik.lundin@webrtc.org | 5ecdef1 | 2013-12-03 08:26:49 +0000 | [diff] [blame] | 53 | reset(); |
| 54 | return -1; |
| 55 | } |
| 56 | // Store in local variable until we have passed the reset below. |
| 57 | uint32_t receiveTime = ntohl(offset); |
| 58 | |
| 59 | // Use length here because a plen of 0 specifies rtcp. |
| 60 | length = (uint16_t) (length - _kRDHeaderLen); |
| 61 | |
| 62 | // check buffer size |
henrik.lundin@webrtc.org | e6ab21b | 2014-02-05 08:46:46 +0000 | [diff] [blame] | 63 | if (_datagram && _memSize < length + 1) |
henrik.lundin@webrtc.org | 5ecdef1 | 2013-12-03 08:26:49 +0000 | [diff] [blame] | 64 | { |
| 65 | reset(); |
| 66 | } |
| 67 | |
| 68 | if (!_datagram) |
| 69 | { |
henrik.lundin@webrtc.org | e6ab21b | 2014-02-05 08:46:46 +0000 | [diff] [blame] | 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; |
henrik.lundin@webrtc.org | 5ecdef1 | 2013-12-03 08:26:49 +0000 | [diff] [blame] | 73 | } |
henrik.lundin@webrtc.org | e6ab21b | 2014-02-05 08:46:46 +0000 | [diff] [blame] | 74 | memset(_datagram, 0, length + 1); |
henrik.lundin@webrtc.org | 5ecdef1 | 2013-12-03 08:26:49 +0000 | [diff] [blame] | 75 | |
| 76 | if (length == 0) |
| 77 | { |
| 78 | _datagramLen = 0; |
| 79 | _rtpParsed = false; |
| 80 | return packetLen; |
| 81 | } |
| 82 | |
| 83 | // Read basic header |
| 84 | if (fread((unsigned short *) _datagram, 1, _kBasicHeaderLen, fp) |
| 85 | != (size_t)_kBasicHeaderLen) |
| 86 | { |
| 87 | reset(); |
| 88 | return -1; |
| 89 | } |
| 90 | _receiveTime = receiveTime; |
| 91 | _datagramLen = _kBasicHeaderLen; |
| 92 | |
| 93 | // Parse the basic header |
| 94 | webrtc::WebRtcRTPHeader tempRTPinfo; |
| 95 | int P, X, CC; |
| 96 | parseBasicHeader(&tempRTPinfo, &P, &X, &CC); |
| 97 | |
| 98 | // Check if we have to extend the header |
| 99 | if (X != 0 || CC != 0) |
| 100 | { |
| 101 | int newLen = _kBasicHeaderLen + CC * 4 + X * 4; |
| 102 | assert(_memSize >= newLen); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 103 | |
| 104 | // Read extension from file |
henrik.lundin@webrtc.org | 5ecdef1 | 2013-12-03 08:26:49 +0000 | [diff] [blame] | 105 | size_t readLen = newLen - _kBasicHeaderLen; |
henrik.lundin@webrtc.org | e6ab21b | 2014-02-05 08:46:46 +0000 | [diff] [blame] | 106 | if (fread(&_datagram[_kBasicHeaderLen], 1, readLen, fp) != readLen) |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 107 | { |
| 108 | reset(); |
| 109 | return -1; |
| 110 | } |
henrik.lundin@webrtc.org | 5ecdef1 | 2013-12-03 08:26:49 +0000 | [diff] [blame] | 111 | _datagramLen = newLen; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 112 | |
henrik.lundin@webrtc.org | 5ecdef1 | 2013-12-03 08:26:49 +0000 | [diff] [blame] | 113 | if (X != 0) |
| 114 | { |
| 115 | int totHdrLen = calcHeaderLength(X, CC); |
| 116 | assert(_memSize >= totHdrLen); |
| 117 | |
| 118 | // Read extension from file |
| 119 | size_t readLen = totHdrLen - newLen; |
henrik.lundin@webrtc.org | e6ab21b | 2014-02-05 08:46:46 +0000 | [diff] [blame] | 120 | if (fread(&_datagram[newLen], 1, readLen, fp) != readLen) |
henrik.lundin@webrtc.org | 5ecdef1 | 2013-12-03 08:26:49 +0000 | [diff] [blame] | 121 | { |
| 122 | reset(); |
| 123 | return -1; |
| 124 | } |
| 125 | _datagramLen = totHdrLen; |
| 126 | } |
| 127 | } |
| 128 | _datagramLen = length; |
| 129 | |
| 130 | if (!_blockList.empty() && _blockList.count(payloadType()) > 0) |
| 131 | { |
| 132 | readNextPacket = true; |
| 133 | } |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | _rtpParsed = false; |
henrik.lundin@webrtc.org | e6ab21b | 2014-02-05 08:46:46 +0000 | [diff] [blame] | 137 | assert(_memSize > _datagramLen); |
| 138 | _payloadLen = 1; // Set the length to 1 byte. |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 139 | return packetLen; |
| 140 | |
| 141 | } |
| 142 | |
| 143 | int NETEQTEST_DummyRTPpacket::writeToFile(FILE *fp) |
| 144 | { |
| 145 | if (!fp) |
| 146 | { |
| 147 | return -1; |
| 148 | } |
| 149 | |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 150 | uint16_t length, plen; |
| 151 | uint32_t offset; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 152 | |
| 153 | // length including RTPplay header |
| 154 | length = htons(_datagramLen + _kRDHeaderLen); |
| 155 | if (fwrite(&length, 2, 1, fp) != 1) |
| 156 | { |
| 157 | return -1; |
| 158 | } |
| 159 | |
| 160 | // payload length |
| 161 | plen = htons(_datagramLen); |
| 162 | if (fwrite(&plen, 2, 1, fp) != 1) |
| 163 | { |
| 164 | return -1; |
| 165 | } |
| 166 | |
| 167 | // offset (=receive time) |
| 168 | offset = htonl(_receiveTime); |
| 169 | if (fwrite(&offset, 4, 1, fp) != 1) |
| 170 | { |
| 171 | return -1; |
| 172 | } |
| 173 | |
| 174 | // Figure out the length of the RTP header. |
| 175 | int headerLen; |
| 176 | if (_datagramLen == 0) |
| 177 | { |
| 178 | // No payload at all; we are done writing to file. |
| 179 | headerLen = 0; |
| 180 | } |
| 181 | else |
| 182 | { |
| 183 | parseHeader(); |
| 184 | headerLen = _payloadPtr - _datagram; |
| 185 | assert(headerLen >= 0); |
| 186 | } |
| 187 | |
| 188 | // write RTP header |
| 189 | if (fwrite((unsigned short *) _datagram, 1, headerLen, fp) != |
| 190 | static_cast<size_t>(headerLen)) |
| 191 | { |
| 192 | return -1; |
| 193 | } |
| 194 | |
| 195 | return (headerLen + _kRDHeaderLen); // total number of bytes written |
| 196 | |
| 197 | } |
| 198 | |
henrik.lundin@webrtc.org | e6ab21b | 2014-02-05 08:46:46 +0000 | [diff] [blame] | 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); |
| 203 | _payloadLen = 1; |
| 204 | } |