niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
xians@webrtc.org | 6bde7a8 | 2012-02-20 08:39:25 +0000 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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 "rtp_dump_impl.h" |
| 12 | |
| 13 | #include <cassert> |
| 14 | #include <stdio.h> |
| 15 | |
| 16 | #include "critical_section_wrapper.h" |
| 17 | #include "trace.h" |
| 18 | |
| 19 | #if defined(_WIN32) |
| 20 | #include <Windows.h> |
| 21 | #include <mmsystem.h> |
| 22 | #elif defined(WEBRTC_LINUX) || defined(WEBRTC_MAC) |
| 23 | #include <string.h> |
| 24 | #include <sys/time.h> |
| 25 | #include <time.h> |
| 26 | #endif |
| 27 | |
| 28 | #if (defined(_DEBUG) && defined(_WIN32)) |
| 29 | #define DEBUG_PRINT(expr) OutputDebugString(##expr) |
| 30 | #define DEBUG_PRINTP(expr, p) \ |
| 31 | { \ |
| 32 | char msg[128]; \ |
| 33 | sprintf(msg, ##expr, p); \ |
| 34 | OutputDebugString(msg); \ |
| 35 | } |
| 36 | #else |
| 37 | #define DEBUG_PRINT(expr) ((void)0) |
| 38 | #define DEBUG_PRINTP(expr,p) ((void)0) |
| 39 | #endif // defined(_DEBUG) && defined(_WIN32) |
| 40 | |
| 41 | namespace webrtc { |
leozwang@webrtc.org | 2559cbf | 2012-02-27 19:18:25 +0000 | [diff] [blame] | 42 | const char RTPFILE_VERSION[] = "1.0"; |
pbos@webrtc.org | c75102e | 2013-04-09 13:32:55 +0000 | [diff] [blame] | 43 | const uint32_t MAX_UWORD32 = 0xffffffff; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 44 | |
| 45 | // This stucture is specified in the rtpdump documentation. |
| 46 | // This struct corresponds to RD_packet_t in |
| 47 | // http://www.cs.columbia.edu/irt/software/rtptools/ |
| 48 | typedef struct |
| 49 | { |
| 50 | // Length of packet, including this header (may be smaller than plen if not |
| 51 | // whole packet recorded). |
pbos@webrtc.org | c75102e | 2013-04-09 13:32:55 +0000 | [diff] [blame] | 52 | uint16_t length; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 53 | // Actual header+payload length for RTP, 0 for RTCP. |
pbos@webrtc.org | c75102e | 2013-04-09 13:32:55 +0000 | [diff] [blame] | 54 | uint16_t plen; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 55 | // Milliseconds since the start of recording. |
pbos@webrtc.org | c75102e | 2013-04-09 13:32:55 +0000 | [diff] [blame] | 56 | uint32_t offset; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 57 | } rtpDumpPktHdr_t; |
| 58 | |
| 59 | RtpDump* RtpDump::CreateRtpDump() |
| 60 | { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 61 | return new RtpDumpImpl(); |
| 62 | } |
| 63 | |
| 64 | void RtpDump::DestroyRtpDump(RtpDump* object) |
| 65 | { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 66 | delete object; |
| 67 | } |
| 68 | |
| 69 | RtpDumpImpl::RtpDumpImpl() |
henrike@webrtc.org | 105e071 | 2011-12-16 19:53:46 +0000 | [diff] [blame] | 70 | : _critSect(CriticalSectionWrapper::CreateCriticalSection()), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 71 | _file(*FileWrapper::Create()), |
| 72 | _startTime(0) |
| 73 | { |
| 74 | WEBRTC_TRACE(kTraceMemory, kTraceUtility, -1, "%s created", __FUNCTION__); |
| 75 | } |
| 76 | |
| 77 | RtpDump::~RtpDump() |
| 78 | { |
| 79 | } |
| 80 | |
| 81 | RtpDumpImpl::~RtpDumpImpl() |
| 82 | { |
| 83 | _file.Flush(); |
| 84 | _file.CloseFile(); |
| 85 | delete &_file; |
henrike@webrtc.org | 105e071 | 2011-12-16 19:53:46 +0000 | [diff] [blame] | 86 | delete _critSect; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 87 | WEBRTC_TRACE(kTraceMemory, kTraceUtility, -1, "%s deleted", __FUNCTION__); |
| 88 | } |
| 89 | |
pbos@webrtc.org | c75102e | 2013-04-09 13:32:55 +0000 | [diff] [blame] | 90 | int32_t RtpDumpImpl::Start(const char* fileNameUTF8) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 91 | { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 92 | |
| 93 | if (fileNameUTF8 == NULL) |
| 94 | { |
| 95 | return -1; |
| 96 | } |
| 97 | |
| 98 | CriticalSectionScoped lock(_critSect); |
| 99 | _file.Flush(); |
| 100 | _file.CloseFile(); |
| 101 | if (_file.OpenFile(fileNameUTF8, false, false, false) == -1) |
| 102 | { |
| 103 | WEBRTC_TRACE(kTraceError, kTraceUtility, -1, |
| 104 | "failed to open the specified file"); |
| 105 | return -1; |
| 106 | } |
| 107 | |
| 108 | // Store start of RTP dump (to be used for offset calculation later). |
| 109 | _startTime = GetTimeInMS(); |
| 110 | |
| 111 | // All rtp dump files start with #!rtpplay. |
leozwang@webrtc.org | 2559cbf | 2012-02-27 19:18:25 +0000 | [diff] [blame] | 112 | char magic[16]; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 113 | sprintf(magic, "#!rtpplay%s \n", RTPFILE_VERSION); |
andrew@webrtc.org | 5ae19de | 2011-12-13 22:59:33 +0000 | [diff] [blame] | 114 | if (_file.WriteText(magic) == -1) |
| 115 | { |
| 116 | WEBRTC_TRACE(kTraceError, kTraceUtility, -1, |
| 117 | "error writing to file"); |
| 118 | return -1; |
| 119 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 120 | |
| 121 | // The header according to the rtpdump documentation is sizeof(RD_hdr_t) |
| 122 | // which is 8 + 4 + 2 = 14 bytes for 32-bit architecture (and 22 bytes on |
| 123 | // 64-bit architecture). However, Wireshark use 16 bytes for the header |
| 124 | // regardless of if the binary is 32-bit or 64-bit. Go by the same approach |
| 125 | // as Wireshark since it makes more sense. |
| 126 | // http://wiki.wireshark.org/rtpdump explains that an additional 2 bytes |
| 127 | // of padding should be added to the header. |
leozwang@webrtc.org | 2559cbf | 2012-02-27 19:18:25 +0000 | [diff] [blame] | 128 | char dummyHdr[16]; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 129 | memset(dummyHdr, 0, 16); |
andrew@webrtc.org | 5ae19de | 2011-12-13 22:59:33 +0000 | [diff] [blame] | 130 | if (!_file.Write(dummyHdr, sizeof(dummyHdr))) |
| 131 | { |
| 132 | WEBRTC_TRACE(kTraceError, kTraceUtility, -1, |
| 133 | "error writing to file"); |
| 134 | return -1; |
| 135 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 136 | return 0; |
| 137 | } |
| 138 | |
pbos@webrtc.org | c75102e | 2013-04-09 13:32:55 +0000 | [diff] [blame] | 139 | int32_t RtpDumpImpl::Stop() |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 140 | { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 141 | CriticalSectionScoped lock(_critSect); |
| 142 | _file.Flush(); |
| 143 | _file.CloseFile(); |
| 144 | return 0; |
| 145 | } |
| 146 | |
| 147 | bool RtpDumpImpl::IsActive() const |
| 148 | { |
| 149 | CriticalSectionScoped lock(_critSect); |
| 150 | return _file.Open(); |
| 151 | } |
| 152 | |
pbos@webrtc.org | c75102e | 2013-04-09 13:32:55 +0000 | [diff] [blame] | 153 | int32_t RtpDumpImpl::DumpPacket(const uint8_t* packet, uint16_t packetLength) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 154 | { |
| 155 | CriticalSectionScoped lock(_critSect); |
| 156 | if (!IsActive()) |
| 157 | { |
| 158 | return 0; |
| 159 | } |
| 160 | |
| 161 | if (packet == NULL) |
| 162 | { |
| 163 | return -1; |
| 164 | } |
| 165 | |
| 166 | if (packetLength < 1) |
| 167 | { |
| 168 | return -1; |
| 169 | } |
| 170 | |
| 171 | // If the packet doesn't contain a valid RTCP header the packet will be |
| 172 | // considered RTP (without further verification). |
| 173 | bool isRTCP = RTCP(packet); |
| 174 | |
| 175 | rtpDumpPktHdr_t hdr; |
pbos@webrtc.org | c75102e | 2013-04-09 13:32:55 +0000 | [diff] [blame] | 176 | uint32_t offset; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 177 | |
| 178 | // Offset is relative to when recording was started. |
| 179 | offset = GetTimeInMS(); |
| 180 | if (offset < _startTime) |
| 181 | { |
| 182 | // Compensate for wraparound. |
| 183 | offset += MAX_UWORD32 - _startTime + 1; |
| 184 | } else { |
| 185 | offset -= _startTime; |
| 186 | } |
| 187 | hdr.offset = RtpDumpHtonl(offset); |
| 188 | |
pbos@webrtc.org | c75102e | 2013-04-09 13:32:55 +0000 | [diff] [blame] | 189 | hdr.length = RtpDumpHtons((uint16_t)(packetLength + sizeof(hdr))); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 190 | if (isRTCP) |
| 191 | { |
| 192 | hdr.plen = 0; |
| 193 | } |
| 194 | else |
| 195 | { |
pbos@webrtc.org | c75102e | 2013-04-09 13:32:55 +0000 | [diff] [blame] | 196 | hdr.plen = RtpDumpHtons((uint16_t)packetLength); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 197 | } |
andrew@webrtc.org | 5ae19de | 2011-12-13 22:59:33 +0000 | [diff] [blame] | 198 | |
| 199 | if (!_file.Write(&hdr, sizeof(hdr))) |
| 200 | { |
| 201 | WEBRTC_TRACE(kTraceError, kTraceUtility, -1, |
| 202 | "error writing to file"); |
| 203 | return -1; |
| 204 | } |
| 205 | if (!_file.Write(packet, packetLength)) |
| 206 | { |
| 207 | WEBRTC_TRACE(kTraceError, kTraceUtility, -1, |
| 208 | "error writing to file"); |
| 209 | return -1; |
| 210 | } |
| 211 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 212 | return 0; |
| 213 | } |
| 214 | |
pbos@webrtc.org | c75102e | 2013-04-09 13:32:55 +0000 | [diff] [blame] | 215 | bool RtpDumpImpl::RTCP(const uint8_t* packet) const |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 216 | { |
pbos@webrtc.org | c75102e | 2013-04-09 13:32:55 +0000 | [diff] [blame] | 217 | const uint8_t payloadType = packet[1]; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 218 | bool is_rtcp = false; |
| 219 | |
| 220 | switch(payloadType) |
| 221 | { |
| 222 | case 192: |
| 223 | is_rtcp = true; |
| 224 | break; |
| 225 | case 193: case 195: |
| 226 | break; |
| 227 | case 200: case 201: case 202: case 203: |
| 228 | case 204: case 205: case 206: case 207: |
| 229 | is_rtcp = true; |
| 230 | break; |
| 231 | } |
| 232 | return is_rtcp; |
| 233 | } |
| 234 | |
| 235 | // TODO (hellner): why is TickUtil not used here? |
pbos@webrtc.org | c75102e | 2013-04-09 13:32:55 +0000 | [diff] [blame] | 236 | inline uint32_t RtpDumpImpl::GetTimeInMS() const |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 237 | { |
| 238 | #if defined(_WIN32) |
| 239 | return timeGetTime(); |
| 240 | #elif defined(WEBRTC_LINUX) || defined(WEBRTC_MAC) |
| 241 | struct timeval tv; |
| 242 | struct timezone tz; |
| 243 | unsigned long val; |
| 244 | |
| 245 | gettimeofday(&tv, &tz); |
| 246 | val = tv.tv_sec * 1000 + tv.tv_usec / 1000; |
| 247 | return val; |
| 248 | #else |
| 249 | #error Either _WIN32 or LINUX or WEBRTC_MAC has to be defined! |
| 250 | assert(false); |
| 251 | return 0; |
| 252 | #endif |
| 253 | } |
| 254 | |
pbos@webrtc.org | c75102e | 2013-04-09 13:32:55 +0000 | [diff] [blame] | 255 | inline uint32_t RtpDumpImpl::RtpDumpHtonl(uint32_t x) const |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 256 | { |
| 257 | #if defined(WEBRTC_BIG_ENDIAN) |
| 258 | return x; |
| 259 | #elif defined(WEBRTC_LITTLE_ENDIAN) |
| 260 | return (x >> 24) + ((((x >> 16) & 0xFF) << 8) + ((((x >> 8) & 0xFF) << 16) + |
| 261 | ((x & 0xFF) << 24))); |
| 262 | #else |
| 263 | #error Either WEBRTC_BIG_ENDIAN or WEBRTC_LITTLE_ENDIAN has to be defined! |
| 264 | assert(false); |
| 265 | return 0; |
| 266 | #endif |
| 267 | } |
| 268 | |
pbos@webrtc.org | c75102e | 2013-04-09 13:32:55 +0000 | [diff] [blame] | 269 | inline uint16_t RtpDumpImpl::RtpDumpHtons(uint16_t x) const |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 270 | { |
| 271 | #if defined(WEBRTC_BIG_ENDIAN) |
| 272 | return x; |
| 273 | #elif defined(WEBRTC_LITTLE_ENDIAN) |
| 274 | return (x >> 8) + ((x & 0xFF) << 8); |
| 275 | #else |
| 276 | #error Either WEBRTC_BIG_ENDIAN or WEBRTC_LITTLE_ENDIAN has to be defined! |
| 277 | assert(false); |
| 278 | return 0; |
| 279 | #endif |
| 280 | } |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 281 | } // namespace webrtc |